2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-09-05 15:56:30 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
|
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
|
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials/providers"
|
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/services/sts"
|
2019-01-09 00:48:57 +00:00
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
|
|
|
uuid "github.com/hashicorp/go-uuid"
|
2018-09-05 15:56:30 +00:00
|
|
|
vaultalicloud "github.com/hashicorp/vault-plugin-auth-alicloud"
|
|
|
|
"github.com/hashicorp/vault/api"
|
2023-05-17 13:38:34 +00:00
|
|
|
"github.com/hashicorp/vault/command/agentproxyshared/auth"
|
|
|
|
agentalicloud "github.com/hashicorp/vault/command/agentproxyshared/auth/alicloud"
|
|
|
|
"github.com/hashicorp/vault/command/agentproxyshared/sink"
|
|
|
|
"github.com/hashicorp/vault/command/agentproxyshared/sink/file"
|
2022-11-17 18:55:17 +00:00
|
|
|
"github.com/hashicorp/vault/helper/testhelpers"
|
2018-09-05 15:56:30 +00:00
|
|
|
vaulthttp "github.com/hashicorp/vault/http"
|
2019-04-13 07:44:06 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/logging"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2018-09-05 15:56:30 +00:00
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-09-12 20:30:57 +00:00
|
|
|
envVarAlicloudAccessKey = "ALICLOUD_TEST_ACCESS_KEY"
|
|
|
|
envVarAlicloudSecretKey = "ALICLOUD_TEST_SECRET_KEY"
|
|
|
|
envVarAlicloudRoleArn = "ALICLOUD_TEST_ROLE_ARN"
|
2018-09-05 15:56:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAliCloudEndToEnd(t *testing.T) {
|
|
|
|
if !runAcceptanceTests {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
2022-11-17 18:55:17 +00:00
|
|
|
// Ensure each cred is populated.
|
|
|
|
credNames := []string{
|
|
|
|
envVarAlicloudAccessKey,
|
|
|
|
envVarAlicloudSecretKey,
|
|
|
|
envVarAlicloudRoleArn,
|
|
|
|
}
|
|
|
|
testhelpers.SkipUnlessEnvVarsSet(t, credNames)
|
|
|
|
|
2018-09-05 15:56:30 +00:00
|
|
|
logger := logging.NewVaultLogger(hclog.Trace)
|
|
|
|
coreConfig := &vault.CoreConfig{
|
|
|
|
Logger: logger,
|
|
|
|
CredentialBackends: map[string]logical.Factory{
|
|
|
|
"alicloud": vaultalicloud.Factory,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cluster := vault.NewTestCluster(t, coreConfig, &vault.TestClusterOptions{
|
|
|
|
HandlerFunc: vaulthttp.Handler,
|
|
|
|
})
|
|
|
|
cluster.Start()
|
|
|
|
defer cluster.Cleanup()
|
|
|
|
|
|
|
|
vault.TestWaitActive(t, cluster.Cores[0].Core)
|
|
|
|
client := cluster.Cores[0].Client
|
|
|
|
|
|
|
|
// Setup Vault
|
|
|
|
if err := client.Sys().EnableAuthWithOptions("alicloud", &api.EnableAuthOptions{
|
|
|
|
Type: "alicloud",
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-04-07 19:12:58 +00:00
|
|
|
if _, err := client.Logical().Write("auth/alicloud/role/test", map[string]interface{}{
|
2018-09-12 20:30:57 +00:00
|
|
|
"arn": os.Getenv(envVarAlicloudRoleArn),
|
2018-09-05 15:56:30 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-09-30 01:03:09 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
2018-09-05 15:56:30 +00:00
|
|
|
|
|
|
|
// We're going to feed alicloud auth creds via env variables.
|
|
|
|
if err := setAliCloudEnvCreds(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := unsetAliCloudEnvCreds(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
am, err := agentalicloud.NewAliCloudAuthMethod(&auth.AuthConfig{
|
|
|
|
Logger: logger.Named("auth.alicloud"),
|
|
|
|
MountPath: "auth/alicloud",
|
|
|
|
Config: map[string]interface{}{
|
2018-09-11 18:10:50 +00:00
|
|
|
"role": "test",
|
|
|
|
"region": "us-west-1",
|
|
|
|
"credential_poll_interval": 1,
|
2018-09-05 15:56:30 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ahConfig := &auth.AuthHandlerConfig{
|
|
|
|
Logger: logger.Named("auth.handler"),
|
|
|
|
Client: client,
|
|
|
|
}
|
|
|
|
|
|
|
|
ah := auth.NewAuthHandler(ahConfig)
|
2020-09-30 01:03:09 +00:00
|
|
|
errCh := make(chan error)
|
|
|
|
go func() {
|
|
|
|
errCh <- ah.Run(ctx, am)
|
|
|
|
}()
|
2018-09-05 15:56:30 +00:00
|
|
|
defer func() {
|
2020-09-30 01:03:09 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
case err := <-errCh:
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2018-09-05 15:56:30 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
tmpFile, err := ioutil.TempFile("", "auth.tokensink.test.")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
tokenSinkFileName := tmpFile.Name()
|
|
|
|
tmpFile.Close()
|
|
|
|
os.Remove(tokenSinkFileName)
|
|
|
|
t.Logf("output: %s", tokenSinkFileName)
|
|
|
|
|
|
|
|
config := &sink.SinkConfig{
|
|
|
|
Logger: logger.Named("sink.file"),
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"path": tokenSinkFileName,
|
|
|
|
},
|
|
|
|
WrapTTL: 10 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
fs, err := file.NewFileSink(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
config.Sink = fs
|
|
|
|
|
|
|
|
ss := sink.NewSinkServer(&sink.SinkServerConfig{
|
|
|
|
Logger: logger.Named("sink.server"),
|
|
|
|
Client: client,
|
|
|
|
})
|
2020-09-30 01:03:09 +00:00
|
|
|
go func() {
|
|
|
|
errCh <- ss.Run(ctx, ah.OutputCh, []*sink.SinkConfig{config})
|
|
|
|
}()
|
2018-09-05 15:56:30 +00:00
|
|
|
defer func() {
|
2020-09-30 01:03:09 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
case err := <-errCh:
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2018-09-05 15:56:30 +00:00
|
|
|
}()
|
|
|
|
|
2020-09-30 01:03:09 +00:00
|
|
|
// This has to be after the other defers so it happens first. It allows
|
|
|
|
// successful test runs to immediately cancel all of the runner goroutines
|
|
|
|
// and unblock any of the blocking defer calls by the runner's DoneCh that
|
|
|
|
// comes before this and avoid successful tests from taking the entire
|
|
|
|
// timeout duration.
|
|
|
|
defer cancel()
|
|
|
|
|
2018-09-05 15:56:30 +00:00
|
|
|
if stat, err := os.Lstat(tokenSinkFileName); err == nil {
|
|
|
|
t.Fatalf("expected err but got %s", stat)
|
|
|
|
} else if !os.IsNotExist(err) {
|
|
|
|
t.Fatal("expected notexist err")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait 2 seconds for the env variables to be detected and an auth to be generated.
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
|
|
|
|
token, err := readToken(tokenSinkFileName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if token.Token == "" {
|
|
|
|
t.Fatal("expected token but didn't receive it")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setAliCloudEnvCreds() error {
|
|
|
|
config := sdk.NewConfig()
|
|
|
|
config.Scheme = "https"
|
2018-09-12 20:30:57 +00:00
|
|
|
client, err := sts.NewClientWithOptions("us-west-1", config, credentials.NewAccessKeyCredential(os.Getenv(envVarAlicloudAccessKey), os.Getenv(envVarAlicloudSecretKey)))
|
2018-09-05 15:56:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
roleSessionName, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
assumeRoleReq := sts.CreateAssumeRoleRequest()
|
2018-09-12 20:30:57 +00:00
|
|
|
assumeRoleReq.RoleArn = os.Getenv(envVarAlicloudRoleArn)
|
2022-08-03 19:22:48 +00:00
|
|
|
assumeRoleReq.RoleSessionName = strings.ReplaceAll(roleSessionName, "-", "")
|
2018-09-05 15:56:30 +00:00
|
|
|
assumeRoleResp, err := client.AssumeRole(assumeRoleReq)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Setenv(providers.EnvVarAccessKeyID, assumeRoleResp.Credentials.AccessKeyId); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.Setenv(providers.EnvVarAccessKeySecret, assumeRoleResp.Credentials.AccessKeySecret); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.Setenv(providers.EnvVarAccessKeyStsToken, assumeRoleResp.Credentials.SecurityToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
func unsetAliCloudEnvCreds() error {
|
|
|
|
if err := os.Unsetenv(providers.EnvVarAccessKeyID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.Unsetenv(providers.EnvVarAccessKeySecret); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.Unsetenv(providers.EnvVarAccessKeyStsToken)
|
|
|
|
}
|