Files
threshold/testing/internal/e2e/boundary/authenticate.go
T
Michael Li 8ad861f426 test(e2e): Update migration test to use vault docker container (#2909)
* fix(enos): Make sure enos reports test as failure

After updating this command to save off the output from running `go test`, enos would not report when tests failed. This was due to the extra pipe hiding the exit status of the prior commands. Adding this option should return the status of the last command to exit with a non-zero status.

* doc(e2e): Fix comments

* refact(e2e): Update AuthenticateCli to accept auth method id

* refact(e2e/migration): Use docker to set up vault instead of enos

* refact(e2e/migration): Adjust where environment variables are set
2023-02-07 14:00:05 -05:00

98 lines
2.7 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package boundary
import (
"context"
"encoding/json"
"fmt"
"testing"
"github.com/hashicorp/boundary/api"
"github.com/hashicorp/boundary/api/authmethods"
"github.com/hashicorp/boundary/testing/internal/e2e"
"github.com/stretchr/testify/require"
)
// NewApiClient creates a new Api client for the specified Boundary instance and
// attempts to authenticate it. Returns the client.
func NewApiClient() (*api.Client, error) {
c, err := LoadConfig()
if err != nil {
return nil, err
}
client, err := api.NewClient(&api.Config{Addr: c.Address})
if err != nil {
return nil, err
}
ctx := context.Background()
authmethodsClient := authmethods.NewClient(client)
authenticationResult, err := authmethodsClient.Authenticate(ctx, c.AuthMethodId, "login",
map[string]any{
"login_name": c.AdminLoginName,
"password": c.AdminLoginPassword,
},
)
if err != nil {
return nil, err
}
client.SetToken(fmt.Sprint(authenticationResult.Attributes["token"]))
return client, err
}
// AuthenticateAdminCli uses the cli to authenticate the specified Boundary instance as an admin
func AuthenticateAdminCli(t testing.TB, ctx context.Context) {
c, err := LoadConfig()
require.NoError(t, err)
AuthenticateCli(t, ctx, c.AuthMethodId, c.AdminLoginName, c.AdminLoginPassword)
}
// AuthenticateCli uses the cli to authenticate the specified Boundary instance
func AuthenticateCli(t testing.TB, ctx context.Context, authMethodId string, loginName string, password string) {
c, err := LoadConfig()
require.NoError(t, err)
output := e2e.RunCommand(ctx, "boundary",
e2e.WithArgs(
"authenticate", "password",
"-addr", c.Address,
"-auth-method-id", authMethodId,
"-login-name", loginName,
"-password", "env://E2E_TEST_BOUNDARY_PASSWORD",
),
e2e.WithEnv("E2E_TEST_BOUNDARY_PASSWORD", password),
)
require.NoError(t, output.Err, string(output.Stderr))
}
// GetAuthenticationTokenCli uses the cli to get an auth token that can be used in subsequent
// commands
func GetAuthenticationTokenCli(t testing.TB, ctx context.Context, loginName string, password string) string {
c, err := LoadConfig()
require.NoError(t, err)
output := e2e.RunCommand(ctx, "boundary",
e2e.WithArgs(
"authenticate", "password",
"-addr", c.Address,
"-auth-method-id", c.AuthMethodId,
"-login-name", loginName,
"-password", "env://E2E_TEST_BOUNDARY_PASSWORD",
"-format", "json",
),
e2e.WithEnv("E2E_TEST_BOUNDARY_PASSWORD", password),
)
require.NoError(t, output.Err, string(output.Stderr))
var authenticationResult AuthenticateCliOutput
err = json.Unmarshal(output.Stdout, &authenticationResult)
require.NoError(t, err)
return fmt.Sprint(authenticationResult.Item.Attributes["token"])
}