2026-05-11 22:37:05 -07:00
|
|
|
// Copyright IBM Corp. 2021, 2026
|
2023-06-05 09:52:48 -07:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
2023-05-25 22:19:43 -07:00
|
|
|
package storage
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2026-10-12 10:27:59 -07:00
|
|
|
"repo.anhonesthost.net/CyberCoveLLC/threshold-plugin-aws/internal/credential"
|
2023-09-22 12:26:08 -07:00
|
|
|
"github.com/hashicorp/go-secure-stdlib/awsutil/v2"
|
2023-05-25 22:19:43 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestAwsStoragePersistedStateS3Client(t *testing.T) {
|
2023-07-18 16:33:24 -07:00
|
|
|
cases := []struct {
|
|
|
|
|
name string
|
|
|
|
|
creds *awsutil.CredentialsConfig
|
|
|
|
|
opts []awsStoragePersistedStateOption
|
|
|
|
|
awsOpts []awsutil.Option
|
|
|
|
|
s3Opts []s3Option
|
|
|
|
|
expectedRegion string
|
|
|
|
|
expectedEndpoint string
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "static credentials",
|
|
|
|
|
creds: func() *awsutil.CredentialsConfig {
|
|
|
|
|
creds, err := awsutil.NewCredentialsConfig(
|
|
|
|
|
awsutil.WithRegion("us-west-2"),
|
|
|
|
|
awsutil.WithAccessKey("foobar"),
|
|
|
|
|
awsutil.WithSecretKey("barfoo"),
|
|
|
|
|
)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
return creds
|
|
|
|
|
}(),
|
|
|
|
|
awsOpts: []awsutil.Option{
|
|
|
|
|
awsutil.WithIAMAPIFunc(
|
|
|
|
|
awsutil.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)
|
|
|
|
|
|
2024-11-25 21:32:37 -08:00
|
|
|
clientRaw, err := state.s3Client(context.Background(), tc.s3Opts...)
|
2023-07-18 16:33:24 -07:00
|
|
|
require.NoError(err)
|
|
|
|
|
require.NotNil(clientRaw)
|
|
|
|
|
client, ok := clientRaw.(*testMockS3)
|
|
|
|
|
require.True(ok)
|
|
|
|
|
require.NotNil(client)
|
|
|
|
|
require.Equal(tc.expectedRegion, client.Region)
|
|
|
|
|
})
|
|
|
|
|
}
|
2023-05-25 22:19:43 -07:00
|
|
|
}
|