From 79bc3d48784b4d133d159f07372eafdea4d03d73 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Thu, 20 Oct 2022 10:40:29 -0400 Subject: [PATCH] refact(e2e): Create credential store function --- testing/internal/e2e/boundary/credential.go | 41 +++++++++++++++++++ testing/internal/e2e/boundary/host.go | 10 ++--- testing/internal/e2e/boundary/scope.go | 8 ++-- testing/internal/e2e/boundary/target.go | 4 +- .../host/static/connect_authz_token_test.go | 18 ++------ .../e2e/host/static/connect_ssh_test.go | 26 ++---------- 6 files changed, 58 insertions(+), 49 deletions(-) create mode 100644 testing/internal/e2e/boundary/credential.go diff --git a/testing/internal/e2e/boundary/credential.go b/testing/internal/e2e/boundary/credential.go new file mode 100644 index 0000000000..c9b9b64beb --- /dev/null +++ b/testing/internal/e2e/boundary/credential.go @@ -0,0 +1,41 @@ +package boundary + +import ( + "context" + "encoding/json" + "testing" + + "github.com/hashicorp/boundary/api" + "github.com/hashicorp/boundary/api/credentialstores" + "github.com/hashicorp/boundary/testing/internal/e2e" + "github.com/stretchr/testify/require" +) + +// CreateNewCredentialStoreStaticApi creates a new static credential store using the go api. +// 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 +} + +// CreateNewCredentialStoreStaticCli creates a new static credential store using the cli. +// Returns the id of the new credential store +func CreateNewCredentialStoreStaticCli(t testing.TB, projectId string) string { + output := e2e.RunCommand(context.Background(), "boundary", "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 +} diff --git a/testing/internal/e2e/boundary/host.go b/testing/internal/e2e/boundary/host.go index 5788d2a128..786561bff1 100644 --- a/testing/internal/e2e/boundary/host.go +++ b/testing/internal/e2e/boundary/host.go @@ -17,9 +17,7 @@ import ( // Returns the id of the new host catalog. func CreateNewHostCatalogApi(t testing.TB, ctx context.Context, client *api.Client, projectId string) string { hcClient := hostcatalogs.NewClient(client) - newHostCatalogResult, err := hcClient.Create(ctx, "static", projectId, - hostcatalogs.WithName("e2e Automated Test Host Catalog"), - ) + newHostCatalogResult, err := hcClient.Create(ctx, "static", projectId, hostcatalogs.WithName("e2e Host Catalog")) require.NoError(t, err) newHostCatalogId := newHostCatalogResult.Item.Id t.Logf("Created Host Catalog: %s", newHostCatalogId) @@ -31,7 +29,7 @@ func CreateNewHostCatalogApi(t testing.TB, ctx context.Context, client *api.Clie // Returns the id of the new host set. func CreateNewHostSetApi(t testing.TB, ctx context.Context, client *api.Client, hostCatalogId string) string { hsClient := hostsets.NewClient(client) - newHostSetResult, err := hsClient.Create(ctx, hostCatalogId) + newHostSetResult, err := hsClient.Create(ctx, hostCatalogId, hostsets.WithName("e2e Host")) require.NoError(t, err) newHostSetId := newHostSetResult.Item.Id t.Logf("Created Host Set: %s", newHostSetId) @@ -66,7 +64,7 @@ func AddHostToHostSetApi(t testing.TB, ctx context.Context, client *api.Client, func CreateNewHostCatalogCli(t testing.TB, projectId string) string { output := e2e.RunCommand(context.Background(), "boundary", "host-catalogs", "create", "static", "-scope-id", projectId, - "-name", "e2e Automated Test Host Catalog", + "-name", "e2e Host Catalog", "-format", "json", ) require.NoError(t, output.Err, string(output.Stderr)) @@ -84,7 +82,7 @@ func CreateNewHostCatalogCli(t testing.TB, projectId string) string { func CreateNewHostSetCli(t testing.TB, hostCatalogId string) string { output := e2e.RunCommand(context.Background(), "boundary", "host-sets", "create", "static", "-host-catalog-id", hostCatalogId, - "-name", "e2e Automated Test Host Set", + "-name", "e2e Host Set", "-format", "json", ) require.NoError(t, output.Err, string(output.Stderr)) diff --git a/testing/internal/e2e/boundary/scope.go b/testing/internal/e2e/boundary/scope.go index 6d39f246f0..ec21884090 100644 --- a/testing/internal/e2e/boundary/scope.go +++ b/testing/internal/e2e/boundary/scope.go @@ -15,7 +15,7 @@ import ( // Returns the id of the new org. func CreateNewOrgApi(t testing.TB, ctx context.Context, client *api.Client) string { scopeClient := scopes.NewClient(client) - newOrgResult, err := scopeClient.Create(ctx, "global", scopes.WithName("e2e Automated Test Org")) + newOrgResult, err := scopeClient.Create(ctx, "global", scopes.WithName("e2e Org")) require.NoError(t, err) newOrgId := newOrgResult.Item.Id @@ -33,7 +33,7 @@ func CreateNewOrgApi(t testing.TB, ctx context.Context, client *api.Client) stri // Returns the id of the new project. func CreateNewProjectApi(t testing.TB, ctx context.Context, client *api.Client, orgId string) string { scopeClient := scopes.NewClient(client) - newProjResult, err := scopeClient.Create(ctx, orgId, scopes.WithName("e2e Automated Test Project")) + newProjResult, err := scopeClient.Create(ctx, orgId, scopes.WithName("e2e Project")) require.NoError(t, err) newProjectId := newProjResult.Item.Id @@ -46,7 +46,7 @@ func CreateNewProjectApi(t testing.TB, ctx context.Context, client *api.Client, func CreateNewOrgCli(t testing.TB) string { ctx := context.Background() output := e2e.RunCommand(ctx, "boundary", "scopes", "create", - "-name", "e2e Automated Test Org", + "-name", "e2e Org", "-scope-id", "global", "-format", "json", ) @@ -72,7 +72,7 @@ func CreateNewOrgCli(t testing.TB) string { func CreateNewProjectCli(t testing.TB, orgId string) string { ctx := context.Background() output := e2e.RunCommand(ctx, "boundary", "scopes", "create", - "-name", "e2e Automated Test Project", + "-name", "e2e Project", "-scope-id", orgId, "-format", "json", ) diff --git a/testing/internal/e2e/boundary/target.go b/testing/internal/e2e/boundary/target.go index 54a3deecbc..1a2ab415a1 100644 --- a/testing/internal/e2e/boundary/target.go +++ b/testing/internal/e2e/boundary/target.go @@ -19,7 +19,7 @@ func CreateNewTargetApi(t testing.TB, ctx context.Context, client *api.Client, p targetPort, err := strconv.ParseInt(defaultPort, 10, 32) require.NoError(t, err) newTargetResult, err := tClient.Create(ctx, "tcp", projectId, - targets.WithName("e2e Automated Test Target"), + targets.WithName("e2e Target"), targets.WithTcpTargetDefaultPort(uint32(targetPort)), ) require.NoError(t, err) @@ -45,7 +45,7 @@ func CreateNewTargetCli(t testing.TB, projectId string, defaultPort string) stri output := e2e.RunCommand(context.Background(), "boundary", "targets", "create", "tcp", "-scope-id", projectId, "-default-port", defaultPort, - "-name", "e2e Automated Test Target", + "-name", "e2e Target", "-format", "json", ) require.NoError(t, output.Err, string(output.Stderr)) diff --git a/testing/internal/e2e/host/static/connect_authz_token_test.go b/testing/internal/e2e/host/static/connect_authz_token_test.go index 01798aba0e..3b91e264d2 100644 --- a/testing/internal/e2e/host/static/connect_authz_token_test.go +++ b/testing/internal/e2e/host/static/connect_authz_token_test.go @@ -9,7 +9,6 @@ import ( "testing" "github.com/hashicorp/boundary/api/credentials" - "github.com/hashicorp/boundary/api/credentialstores" "github.com/hashicorp/boundary/api/targets" "github.com/hashicorp/boundary/testing/internal/e2e" "github.com/hashicorp/boundary/testing/internal/e2e/boundary" @@ -33,22 +32,11 @@ func TestConnectTargetWithAuthzTokenCli(t *testing.T) { boundary.AddHostToHostSetCli(t, newHostSetId, newHostId) newTargetId := boundary.CreateNewTargetCli(t, newProjectId, c.TargetPort) boundary.AddHostSourceToTargetCli(t, newTargetId, newHostSetId) - - // Create a credential store - ctx := context.Background() - output := e2e.RunCommand(ctx, "boundary", "credential-stores", "create", "static", - "-scope-id", newProjectId, - "-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) + newCredentialStoreId := boundary.CreateNewCredentialStoreStaticCli(t, newProjectId) // Create credentials - output = e2e.RunCommand(ctx, "boundary", "credentials", "create", "ssh-private-key", + ctx := context.Background() + output := e2e.RunCommand(ctx, "boundary", "credentials", "create", "ssh-private-key", "-credential-store-id", newCredentialStoreId, "-username", c.TargetSshUser, "-private-key", "file://"+c.TargetSshKeyPath, diff --git a/testing/internal/e2e/host/static/connect_ssh_test.go b/testing/internal/e2e/host/static/connect_ssh_test.go index df0d3e9a0e..d74a6cd574 100644 --- a/testing/internal/e2e/host/static/connect_ssh_test.go +++ b/testing/internal/e2e/host/static/connect_ssh_test.go @@ -8,7 +8,6 @@ import ( "testing" "github.com/hashicorp/boundary/api/credentials" - "github.com/hashicorp/boundary/api/credentialstores" "github.com/hashicorp/boundary/api/targets" "github.com/hashicorp/boundary/testing/internal/e2e" "github.com/hashicorp/boundary/testing/internal/e2e/boundary" @@ -33,22 +32,11 @@ func TestConnectTargetWithSshCli(t *testing.T) { boundary.AddHostToHostSetCli(t, newHostSetId, newHostId) newTargetId := boundary.CreateNewTargetCli(t, newProjectId, c.TargetPort) boundary.AddHostSourceToTargetCli(t, newTargetId, newHostSetId) - - // Create a credential store - ctx := context.Background() - output := e2e.RunCommand(ctx, "boundary", "credential-stores", "create", "static", - "-scope-id", newProjectId, - "-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) + newCredentialStoreId := boundary.CreateNewCredentialStoreStaticCli(t, newProjectId) // Create credentials - output = e2e.RunCommand(ctx, "boundary", "credentials", "create", "ssh-private-key", + ctx := context.Background() + output := e2e.RunCommand(ctx, "boundary", "credentials", "create", "ssh-private-key", "-credential-store-id", newCredentialStoreId, "-username", c.TargetSshUser, "-private-key", "file://"+c.TargetSshKeyPath, @@ -115,13 +103,7 @@ func TestCreateTargetWithStaticCredentialStoreApi(t *testing.T) { boundary.AddHostToHostSetApi(t, ctx, client, newHostSetId, newHostId) newTargetId := boundary.CreateNewTargetApi(t, ctx, client, newProjectId, c.TargetPort) boundary.AddHostSourceToTargetApi(t, ctx, client, newTargetId, newHostSetId) - - // Create a credential store - csClient := credentialstores.NewClient(client) - newCredentialStoreResult, err := csClient.Create(ctx, "static", newProjectId) - require.NoError(t, err) - newCredentialStoreId := newCredentialStoreResult.Item.Id - t.Logf("Created Credential Store: %s", newCredentialStoreId) + newCredentialStoreId := boundary.CreateNewCredentialStoreStaticApi(t, ctx, client, newProjectId) // Create credentials cClient := credentials.NewClient(client)