Files
threshold/testing/internal/e2e/boundary/credential.go
T
Michael LiandJohan Brandhorst-Satzkorn 45171a3f56 refact(e2e): Changes to support an automated migration test (#2676)
* refact(e2e): Move cleanup steps into each test

The migration test needed to create some resources, but doesn't care about cleaning up because the test would take care of spinning down the docker container that it's using. To maintain flexibility going forward, we'll establish that tests are responsible for cleaning up (rather than the methods they use)

* refact(e2e): Use new env var for skip check

Currently, the e2e tests use the E2E_PASSWORD_AUTH_METHOD_ID environment variable to determine if the test should be skipped. However, the new migration test does not require a pre-existing boundary cluster and would not have this environment variable. As a result, we needed an alternative way to check if an e2e test should be skipped when running tests in ci.

* refact(e2e): Create more common boundary methods

* fix(e2e): Set to string value

Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
2022-12-06 15:59:17 -05:00

137 lines
5.2 KiB
Go

package boundary
import (
"context"
"encoding/json"
"testing"
"github.com/hashicorp/boundary/api"
"github.com/hashicorp/boundary/api/credentials"
"github.com/hashicorp/boundary/api/credentialstores"
"github.com/hashicorp/boundary/testing/internal/e2e"
"github.com/stretchr/testify/require"
)
// CreateNewCredentialStoreStaticApi uses the Go api to create a new static credential store.
// Returns the id of the new credential store
func CreateNewCredentialStoreStaticApi(t testing.TB, ctx context.Context, client *api.Client, projectId string) string {
csClient := credentialstores.NewClient(client)
newCredentialStoreResult, err := csClient.Create(ctx, "static", projectId, credentialstores.WithName("e2e Credential Store"))
require.NoError(t, err)
newCredentialStoreId := newCredentialStoreResult.Item.Id
t.Logf("Created Credential Store: %s", newCredentialStoreId)
return newCredentialStoreId
}
// CreateNewCredentialStoreVaultCli uses the cli to create a Vault credential store
// Returns the id of the new credential store
func CreateNewCredentialStoreVaultCli(t testing.TB, ctx context.Context, projectId string, vaultAddr string, vaultToken string) string {
output := e2e.RunCommand(ctx, "boundary",
e2e.WithArgs(
"credential-stores", "create", "vault",
"-scope-id", projectId,
"-vault-address", vaultAddr,
"-vault-token", vaultToken,
"-format", "json",
),
)
require.NoError(t, output.Err, string(output.Stderr))
var newCredentialStoreResult credentialstores.CredentialStoreCreateResult
err := json.Unmarshal(output.Stdout, &newCredentialStoreResult)
require.NoError(t, err)
newVaultCredentialStoreId := newCredentialStoreResult.Item.Id
t.Logf("Created Credential Store: %s", newVaultCredentialStoreId)
return newVaultCredentialStoreId
}
// CreateNewCredentialStoreStaticCli uses the cli to create a new static credential store.
// Returns the id of the new credential store
func CreateNewCredentialStoreStaticCli(t testing.TB, ctx context.Context, projectId string) string {
output := e2e.RunCommand(ctx, "boundary",
e2e.WithArgs(
"credential-stores", "create", "static",
"-scope-id", projectId,
"-format", "json",
),
)
require.NoError(t, output.Err, string(output.Stderr))
var newCredentialStoreResult credentialstores.CredentialStoreCreateResult
err := json.Unmarshal(output.Stdout, &newCredentialStoreResult)
require.NoError(t, err)
newCredentialStoreId := newCredentialStoreResult.Item.Id
t.Logf("Created Credential Store: %s", newCredentialStoreId)
return newCredentialStoreId
}
// CreateNewStaticCredentialPrivateKeyCli uses the cli to create a new private key credential in the
// provided static credential store.
// Returns the id of the new credential
func CreateNewStaticCredentialPrivateKeyCli(t testing.TB, ctx context.Context, credentialStoreId string, user string, filePath string) string {
output := e2e.RunCommand(ctx, "boundary",
e2e.WithArgs(
"credentials", "create", "ssh-private-key",
"-credential-store-id", credentialStoreId,
"-username", user,
"-private-key", "file://"+filePath,
"-format", "json",
),
)
require.NoError(t, output.Err, string(output.Stderr))
var newCredentialsResult credentials.CredentialCreateResult
err := json.Unmarshal(output.Stdout, &newCredentialsResult)
require.NoError(t, err)
newCredentialsId := newCredentialsResult.Item.Id
t.Logf("Created SSH Private Key Credentials: %s", newCredentialsId)
return newCredentialsId
}
// CreateNewStaticCredentialPasswordCli uses the cli to create a new password credential in the
// provided static credential store.
// Returns the id of the new credential
func CreateNewStaticCredentialPasswordCli(t testing.TB, ctx context.Context, credentialStoreId string, user string, password string) string {
output := e2e.RunCommand(ctx, "boundary",
e2e.WithArgs(
"credentials", "create", "username-password",
"-credential-store-id", credentialStoreId,
"-username", user,
"-password", "env://E2E_CREDENTIALS_PASSWORD",
"-format", "json",
),
e2e.WithEnv("E2E_CREDENTIALS_PASSWORD", password),
)
require.NoError(t, output.Err, string(output.Stderr))
var newCredentialsResult credentials.CredentialCreateResult
err := json.Unmarshal(output.Stdout, &newCredentialsResult)
require.NoError(t, err)
newCredentialsId := newCredentialsResult.Item.Id
t.Logf("Created Username/Password Credentials: %s", newCredentialsId)
return newCredentialsId
}
// CreateNewStaticCredentialJsonCli uses the cli to create a new json credential in the provided
// static credential store.
// Returns the id of the new credential
func CreateNewStaticCredentialJsonCli(t testing.TB, ctx context.Context, credentialStoreId string, jsonFilePath string) string {
output := e2e.RunCommand(ctx, "boundary",
e2e.WithArgs(
"credentials", "create", "json",
"-credential-store-id", credentialStoreId,
"-object", "file://"+jsonFilePath,
"-format", "json",
),
)
require.NoError(t, output.Err, string(output.Stderr))
var newCredentialsResult credentials.CredentialCreateResult
err := json.Unmarshal(output.Stdout, &newCredentialsResult)
require.NoError(t, err)
newCredentialsId := newCredentialsResult.Item.Id
t.Logf("Created Username/Password Credentials: %s", newCredentialsId)
return newCredentialsId
}