2022-10-24 16:08:09 -04:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-02-02 17:15:17 -08:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
2022-10-24 16:08:09 -04:00
|
|
|
package boundary
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
2024-04-03 09:44:44 -04:00
|
|
|
"fmt"
|
2022-10-24 16:08:09 -04:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/hashicorp/boundary/api"
|
|
|
|
|
"github.com/hashicorp/boundary/api/accounts"
|
|
|
|
|
"github.com/hashicorp/boundary/testing/internal/e2e"
|
|
|
|
|
"github.com/hashicorp/go-secure-stdlib/base62"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
2024-04-03 09:44:44 -04:00
|
|
|
// CreateAccountApi creates a new account using the Go api.
|
2022-10-24 16:08:09 -04:00
|
|
|
// Returns the id of the new account as well as the password that was generated
|
2024-04-03 09:44:44 -04:00
|
|
|
func CreateAccountApi(t testing.TB, ctx context.Context, client *api.Client, authMethodId string, loginName string) (string, string, error) {
|
|
|
|
|
name, err := base62.Random(16)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", "", err
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-24 16:08:09 -04:00
|
|
|
aClient := accounts.NewClient(client)
|
2024-01-29 13:22:21 -08:00
|
|
|
password, err := base62.Random(16)
|
2024-04-03 09:44:44 -04:00
|
|
|
if err != nil {
|
|
|
|
|
return "", "", err
|
|
|
|
|
}
|
|
|
|
|
createAccountResult, err := aClient.Create(ctx, authMethodId,
|
2022-10-24 16:08:09 -04:00
|
|
|
accounts.WithPasswordAccountLoginName(loginName),
|
|
|
|
|
accounts.WithPasswordAccountPassword(password),
|
2024-04-03 09:44:44 -04:00
|
|
|
accounts.WithName(fmt.Sprintf("e2e Account %s", name)),
|
2022-10-24 16:08:09 -04:00
|
|
|
)
|
2024-04-03 09:44:44 -04:00
|
|
|
if err != nil {
|
|
|
|
|
return "", "", err
|
|
|
|
|
}
|
2022-10-24 16:08:09 -04:00
|
|
|
|
2024-04-03 09:44:44 -04:00
|
|
|
accountId := createAccountResult.Item.Id
|
2022-10-24 16:08:09 -04:00
|
|
|
t.Logf("Create Account: %s", accountId)
|
2024-04-03 09:44:44 -04:00
|
|
|
return accountId, password, nil
|
2022-10-24 16:08:09 -04:00
|
|
|
}
|
|
|
|
|
|
2024-04-03 09:44:44 -04:00
|
|
|
// CreateAccountCli creates a new account using the cli.
|
2022-10-24 16:08:09 -04:00
|
|
|
// Returns the id of the new account as well as the password that was generated
|
2024-04-03 09:44:44 -04:00
|
|
|
func CreateAccountCli(t testing.TB, ctx context.Context, authMethodId string, loginName string) (string, string, error) {
|
|
|
|
|
name, err := base62.Random(16)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", "", err
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-24 16:08:09 -04:00
|
|
|
password, err := base62.Random(16)
|
|
|
|
|
require.NoError(t, err)
|
2022-10-25 15:28:39 -04:00
|
|
|
output := e2e.RunCommand(ctx, "boundary",
|
|
|
|
|
e2e.WithArgs(
|
|
|
|
|
"accounts", "create", "password",
|
2023-02-13 10:36:05 -05:00
|
|
|
"-auth-method-id", authMethodId,
|
2022-10-25 15:28:39 -04:00
|
|
|
"-login-name", loginName,
|
|
|
|
|
"-password", "env://E2E_TEST_ACCOUNT_PASSWORD",
|
2024-04-03 09:44:44 -04:00
|
|
|
"-name", fmt.Sprintf("e2e Account %s", name),
|
2023-11-30 09:38:07 -05:00
|
|
|
"-description", "e2e",
|
2022-10-25 15:28:39 -04:00
|
|
|
"-format", "json",
|
|
|
|
|
),
|
|
|
|
|
e2e.WithEnv("E2E_TEST_ACCOUNT_PASSWORD", password),
|
2022-10-24 16:08:09 -04:00
|
|
|
)
|
2024-04-03 09:44:44 -04:00
|
|
|
if output.Err != nil {
|
|
|
|
|
return "", "", fmt.Errorf("%w: %s", output.Err, string(output.Stderr))
|
|
|
|
|
}
|
2022-10-24 16:08:09 -04:00
|
|
|
|
2024-04-03 09:44:44 -04:00
|
|
|
var createAccountResult accounts.AccountCreateResult
|
|
|
|
|
err = json.Unmarshal(output.Stdout, &createAccountResult)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", "", err
|
|
|
|
|
}
|
2022-10-24 16:08:09 -04:00
|
|
|
|
2024-04-03 09:44:44 -04:00
|
|
|
accountId := createAccountResult.Item.Id
|
|
|
|
|
t.Logf("Created Account: %s", accountId)
|
|
|
|
|
return accountId, password, nil
|
2022-10-24 16:08:09 -04:00
|
|
|
}
|