* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * Update copyright file headers to BUS-1.1 * Rerun make gen This will pick up the last of the license changes * Revert "Rerun make gen" This reverts commit dbe0968bffd92745d8663c6940debae098395a6b. * Remove copywrite from generation script The current version of copywrite is not sophisticated enough to only apply the copywrite changes to certain subdirectories, so to avoid accidentally relicensing our api and sdk modules, we will stop running the copywrite script until such time that it can do the right thing. * License protobuf API as MPL The protobuf API files are used to generate the SDK, since we want the SDK to be MPL, the protobuf files must also be MPL. This extends to all protobuf files whose sources end up in the SDK directory, including: - internal/proto/controller/api - internal/proto/controller/custom_options - internal/proto/plugin It also includes the generated service directory in internal/gen/controller/api/services, since they are generated from the protobuf sources used for the SDK. * Change website license file name This makes the naming scheme consistent with the rest of the repository. * Add note on licensing to README * Set copywrite license to BUSL * Revert "Add note on licensing to README" This reverts commit 4d865a91a7a4f11605f0dc4609355d7e3c3cfffe. --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
275 lines
9.1 KiB
Go
275 lines
9.1 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package boundary
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/cenkalti/backoff/v4"
|
|
"github.com/hashicorp/boundary/api"
|
|
"github.com/hashicorp/boundary/api/hostcatalogs"
|
|
"github.com/hashicorp/boundary/api/hosts"
|
|
"github.com/hashicorp/boundary/api/hostsets"
|
|
"github.com/hashicorp/boundary/testing/internal/e2e"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// CreateNewHostCatalogApi uses the Go api to create a new host catalog.
|
|
// 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 Host Catalog"))
|
|
require.NoError(t, err)
|
|
newHostCatalogId := newHostCatalogResult.Item.Id
|
|
t.Logf("Created Host Catalog: %s", newHostCatalogId)
|
|
|
|
return newHostCatalogId
|
|
}
|
|
|
|
// CreateNewHostSetApi uses the Go api to create a new host set.
|
|
// 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, hostsets.WithName("e2e Host"))
|
|
require.NoError(t, err)
|
|
newHostSetId := newHostSetResult.Item.Id
|
|
t.Logf("Created Host Set: %s", newHostSetId)
|
|
|
|
return newHostSetId
|
|
}
|
|
|
|
// CreateNewHostApi uses the Go api to create a new host.
|
|
// Returns the id of the new host.
|
|
func CreateNewHostApi(t testing.TB, ctx context.Context, client *api.Client, hostCatalogId string, address string) string {
|
|
hClient := hosts.NewClient(client)
|
|
newHostResult, err := hClient.Create(ctx, hostCatalogId,
|
|
hosts.WithName(address),
|
|
hosts.WithStaticHostAddress(address),
|
|
)
|
|
require.NoError(t, err)
|
|
newHostId := newHostResult.Item.Id
|
|
t.Logf("Created Host: %s", newHostId)
|
|
|
|
return newHostId
|
|
}
|
|
|
|
// AddHostToHostSetApi uses the Go api to add a host to a host set
|
|
func AddHostToHostSetApi(t testing.TB, ctx context.Context, client *api.Client, hostSetId string, hostId string) {
|
|
hsClient := hostsets.NewClient(client)
|
|
_, err := hsClient.AddHosts(ctx, hostSetId, 0, []string{hostId}, hostsets.WithAutomaticVersioning(true))
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
// CreateNewHostCatalogCli uses the cli to create a new host catalog.
|
|
// Returns the id of the new host catalog.
|
|
func CreateNewHostCatalogCli(t testing.TB, ctx context.Context, projectId string) string {
|
|
output := e2e.RunCommand(ctx, "boundary",
|
|
e2e.WithArgs(
|
|
"host-catalogs", "create", "static",
|
|
"-scope-id", projectId,
|
|
"-name", "e2e Host Catalog",
|
|
"-format", "json",
|
|
),
|
|
)
|
|
require.NoError(t, output.Err, string(output.Stderr))
|
|
var newHostCatalogResult hostcatalogs.HostCatalogCreateResult
|
|
err := json.Unmarshal(output.Stdout, &newHostCatalogResult)
|
|
require.NoError(t, err)
|
|
newHostCatalogId := newHostCatalogResult.Item.Id
|
|
|
|
t.Logf("Created Host Catalog: %s", newHostCatalogId)
|
|
return newHostCatalogId
|
|
}
|
|
|
|
// CreateNewHostSetCli uses the cli to create a new host set.
|
|
// Returns the id of the new host set.
|
|
func CreateNewHostSetCli(t testing.TB, ctx context.Context, hostCatalogId string) string {
|
|
output := e2e.RunCommand(ctx, "boundary",
|
|
e2e.WithArgs(
|
|
"host-sets", "create", "static",
|
|
"-host-catalog-id", hostCatalogId,
|
|
"-name", "e2e Host Set",
|
|
"-format", "json",
|
|
),
|
|
)
|
|
require.NoError(t, output.Err, string(output.Stderr))
|
|
var newHostSetResult hostsets.HostSetCreateResult
|
|
err := json.Unmarshal(output.Stdout, &newHostSetResult)
|
|
require.NoError(t, err)
|
|
newHostSetId := newHostSetResult.Item.Id
|
|
t.Logf("Created Host Set: %s", newHostSetId)
|
|
|
|
return newHostSetId
|
|
}
|
|
|
|
// CreateNewHostCli uses the cli to create a new host.
|
|
// Returns the id of the new host.
|
|
func CreateNewHostCli(t testing.TB, ctx context.Context, hostCatalogId string, address string) string {
|
|
output := e2e.RunCommand(ctx, "boundary",
|
|
e2e.WithArgs(
|
|
"hosts", "create", "static",
|
|
"-host-catalog-id", hostCatalogId,
|
|
"-name", address,
|
|
"-address", address,
|
|
"-format", "json",
|
|
),
|
|
)
|
|
require.NoError(t, output.Err, string(output.Stderr))
|
|
var newHostResult hosts.HostCreateResult
|
|
err := json.Unmarshal(output.Stdout, &newHostResult)
|
|
require.NoError(t, err)
|
|
newHostId := newHostResult.Item.Id
|
|
t.Logf("Created Host: %s", newHostId)
|
|
|
|
return newHostId
|
|
}
|
|
|
|
// AddHostToHostSetCli uses the cli to add a host to a host set
|
|
func AddHostToHostSetCli(t testing.TB, ctx context.Context, hostSetId string, hostId string) {
|
|
output := e2e.RunCommand(ctx, "boundary",
|
|
e2e.WithArgs("host-sets", "add-hosts", "-id", hostSetId, "-host", hostId),
|
|
)
|
|
require.NoError(t, output.Err, string(output.Stderr))
|
|
}
|
|
|
|
// CreateNewAwsHostCatalogCli uses the cli to create a new AWS dynamic host catalog.
|
|
// Returns the id of the new host catalog.
|
|
func CreateNewAwsHostCatalogCli(t testing.TB, ctx context.Context, projectId string, accessKeyId string, secretAccessKey string) string {
|
|
output := e2e.RunCommand(ctx, "boundary",
|
|
e2e.WithArgs(
|
|
"host-catalogs", "create", "plugin",
|
|
"-scope-id", projectId,
|
|
"-plugin-name", "aws",
|
|
"-attr", "disable_credential_rotation=true",
|
|
"-attr", "region=us-east-1",
|
|
"-secret", "access_key_id=env://E2E_AWS_ACCESS_KEY_ID",
|
|
"-secret", "secret_access_key=env://E2E_AWS_SECRET_ACCESS_KEY",
|
|
"-format", "json",
|
|
),
|
|
e2e.WithEnv("E2E_AWS_ACCESS_KEY_ID", accessKeyId),
|
|
e2e.WithEnv("E2E_AWS_SECRET_ACCESS_KEY", secretAccessKey),
|
|
)
|
|
require.NoError(t, output.Err, string(output.Stderr))
|
|
var newHostCatalogResult hostcatalogs.HostCatalogCreateResult
|
|
err := json.Unmarshal(output.Stdout, &newHostCatalogResult)
|
|
require.NoError(t, err)
|
|
newHostCatalogId := newHostCatalogResult.Item.Id
|
|
t.Logf("Created Host Catalog: %s", newHostCatalogId)
|
|
|
|
return newHostCatalogId
|
|
}
|
|
|
|
// CreateNewAwsHostSetCli uses the cli to create a new host set from an AWS dynamic host catalog.
|
|
// Returns the id of the new host set.
|
|
func CreateNewAwsHostSetCli(t testing.TB, ctx context.Context, hostCatalogId string, filter string) string {
|
|
output := e2e.RunCommand(ctx, "boundary",
|
|
e2e.WithArgs(
|
|
"host-sets", "create", "plugin",
|
|
"-host-catalog-id", hostCatalogId,
|
|
"-attr", "filters="+filter,
|
|
"-format", "json",
|
|
),
|
|
)
|
|
require.NoError(t, output.Err, string(output.Stderr))
|
|
var newHostSetResult hostsets.HostSetCreateResult
|
|
err := json.Unmarshal(output.Stdout, &newHostSetResult)
|
|
require.NoError(t, err)
|
|
|
|
newHostSetId := newHostSetResult.Item.Id
|
|
t.Logf("Created Host Set: %s", newHostSetId)
|
|
return newHostSetId
|
|
}
|
|
|
|
// WaitForHostsInHostSetCli uses the cli to check if there are any hosts in a host set. It will check a
|
|
// few times before returning a result. The method will fail if there are 0 hosts found.
|
|
func WaitForHostsInHostSetCli(t testing.TB, ctx context.Context, hostSetId string) int {
|
|
t.Logf("Looking for items in the host set...")
|
|
var actualHostSetCount int
|
|
err := backoff.RetryNotify(
|
|
func() error {
|
|
output := e2e.RunCommand(ctx, "boundary",
|
|
e2e.WithArgs(
|
|
"host-sets", "read",
|
|
"-id", hostSetId,
|
|
"-format", "json",
|
|
),
|
|
)
|
|
if output.Err != nil {
|
|
return backoff.Permanent(errors.New(string(output.Stderr)))
|
|
}
|
|
|
|
var hostSetsReadResult hostsets.HostSetReadResult
|
|
err := json.Unmarshal(output.Stdout, &hostSetsReadResult)
|
|
if err != nil {
|
|
return backoff.Permanent(err)
|
|
}
|
|
|
|
actualHostSetCount = len(hostSetsReadResult.Item.HostIds)
|
|
if actualHostSetCount == 0 {
|
|
return errors.New("No items are appearing in the host set")
|
|
}
|
|
|
|
t.Logf("Found %d host(s)", actualHostSetCount)
|
|
return nil
|
|
},
|
|
backoff.WithMaxRetries(backoff.NewConstantBackOff(3*time.Second), 5),
|
|
func(err error, td time.Duration) {
|
|
t.Logf("%s. Retrying...", err.Error())
|
|
},
|
|
)
|
|
require.NoError(t, err)
|
|
|
|
return actualHostSetCount
|
|
}
|
|
|
|
// WaitForNumberOfHostsInHostSetCli uses the cli to check if the number of hosts
|
|
// in a host set match the expected. The method will throw an error if it does
|
|
// not match after some retries.
|
|
func WaitForNumberOfHostsInHostSetCli(t testing.TB, ctx context.Context, hostSetId string, expectedHostCount int) {
|
|
t.Logf("Looking for items in the host set...")
|
|
var actualHostSetCount int
|
|
err := backoff.RetryNotify(
|
|
func() error {
|
|
output := e2e.RunCommand(ctx, "boundary",
|
|
e2e.WithArgs(
|
|
"host-sets", "read",
|
|
"-id", hostSetId,
|
|
"-format", "json",
|
|
),
|
|
)
|
|
if output.Err != nil {
|
|
return backoff.Permanent(errors.New(string(output.Stderr)))
|
|
}
|
|
|
|
var hostSetsReadResult hostsets.HostSetReadResult
|
|
err := json.Unmarshal(output.Stdout, &hostSetsReadResult)
|
|
if err != nil {
|
|
return backoff.Permanent(err)
|
|
}
|
|
|
|
actualHostSetCount = len(hostSetsReadResult.Item.HostIds)
|
|
if actualHostSetCount != expectedHostCount {
|
|
return errors.New(
|
|
fmt.Sprintf("Number of hosts in host set do not match expected. EXPECTED: %d, ACTUAL: %d",
|
|
expectedHostCount,
|
|
actualHostSetCount,
|
|
))
|
|
}
|
|
|
|
t.Logf("Found %d host(s)", actualHostSetCount)
|
|
return nil
|
|
},
|
|
backoff.WithMaxRetries(backoff.NewConstantBackOff(3*time.Second), 5),
|
|
func(err error, td time.Duration) {
|
|
t.Logf("%s. Retrying...", err.Error())
|
|
},
|
|
)
|
|
require.NoError(t, err)
|
|
}
|