This commit introduces fixes to enable operators to use AWS dynamic credentials on the storage side of this plugin, namely validation, improved credential lifecycle management to allow for storage buckets to be updated from static to dynamic credentials and vice versa, and a new way to determine credential types. It also introduces new tests for dynamic credential usage for the various functions.
77 lines
1.9 KiB
Go
77 lines
1.9 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
package storage
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/boundary-plugin-aws/internal/credential"
|
|
awsutilv2 "github.com/hashicorp/go-secure-stdlib/awsutil/v2"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestAwsStoragePersistedStateS3Client(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
creds *awsutilv2.CredentialsConfig
|
|
opts []awsStoragePersistedStateOption
|
|
awsOpts []awsutilv2.Option
|
|
s3Opts []s3Option
|
|
expectedRegion string
|
|
expectedEndpoint string
|
|
}{
|
|
{
|
|
name: "static credentials",
|
|
creds: func() *awsutilv2.CredentialsConfig {
|
|
creds, err := awsutilv2.NewCredentialsConfig(
|
|
awsutilv2.WithRegion("us-west-2"),
|
|
awsutilv2.WithAccessKey("foobar"),
|
|
awsutilv2.WithSecretKey("barfoo"),
|
|
)
|
|
require.NoError(t, err)
|
|
return creds
|
|
}(),
|
|
awsOpts: []awsutilv2.Option{
|
|
awsutilv2.WithIAMAPIFunc(
|
|
awsutilv2.NewMockIAM(),
|
|
),
|
|
},
|
|
opts: []awsStoragePersistedStateOption{
|
|
withTestS3APIFunc(newTestMockS3(nil)),
|
|
},
|
|
s3Opts: []s3Option{
|
|
WithEndpoint("0.0.0.0"),
|
|
},
|
|
expectedRegion: "us-west-2",
|
|
expectedEndpoint: "0.0.0.0",
|
|
},
|
|
}
|
|
for _, tc := range cases {
|
|
tc := tc
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
require := require.New(t)
|
|
|
|
cred, err := credential.NewAwsCredentialPersistedState(
|
|
credential.WithCredentialsConfig(tc.creds),
|
|
credential.WithStateTestOpts(tc.awsOpts),
|
|
)
|
|
require.NoError(err)
|
|
|
|
state, err := newAwsStoragePersistedState(
|
|
append(tc.opts, withCredentials(cred))...,
|
|
)
|
|
require.NoError(err)
|
|
|
|
clientRaw, err := state.S3Client(context.Background(), tc.s3Opts...)
|
|
require.NoError(err)
|
|
require.NotNil(clientRaw)
|
|
client, ok := clientRaw.(*testMockS3)
|
|
require.True(ok)
|
|
require.NotNil(client)
|
|
require.Equal(tc.expectedRegion, client.Region)
|
|
})
|
|
}
|
|
}
|