Skip to content

Commit

Permalink
kms: improve AWS profile test
Browse files Browse the repository at this point in the history
Due to the AWS SDK addressing a bug, which causes it to be more strict
when loading a configured profile.

Signed-off-by: Hidde Beydals <[email protected]>
  • Loading branch information
hiddeco committed Oct 17, 2023
1 parent 2cffa0f commit 85edde7
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions kms/keysource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
logger "log"
"os"
"path/filepath"
"testing"
"time"

Expand Down Expand Up @@ -399,14 +400,14 @@ func TestMasterKey_createKMSConfig(t *testing.T) {
tests := []struct {
name string
key MasterKey
envFunc func(t *testing.T)
assertFunc func(t *testing.T, cfg *aws.Config, err error)
fallback bool
}{
{
name: "valid config",
name: "valid config with credentials provider",
key: MasterKey{
credentialsProvider: credentials.NewStaticCredentialsProvider("test-id", "test-secret", "test-token"),
AwsProfile: "test-profile",
Arn: "arn:aws:kms:us-west-2:107501996527:key/612d5f0p-p1l3-45e6-aca6-a5b005693a48",
},
assertFunc: func(t *testing.T, cfg *aws.Config, err error) {
Expand All @@ -418,6 +419,30 @@ func TestMasterKey_createKMSConfig(t *testing.T) {
assert.Equal(t, "test-id", creds.AccessKeyID)
assert.Equal(t, "test-secret", creds.SecretAccessKey)
assert.Equal(t, "test-token", creds.SessionToken)
},
},
{
name: "valid config with profile",
key: MasterKey{
AwsProfile: "test-profile",
Arn: "arn:aws:kms:us-west-2:107501996527:key/612d5f0p-p1l3-45e6-aca6-a5b005693a48",
},
envFunc: func(t *testing.T) {
credentialsFile := filepath.Join(t.TempDir(), ".aws", "credentials")
assert.NoError(t, os.MkdirAll(filepath.Dir(credentialsFile), 0o700))
assert.NoError(t, os.WriteFile(credentialsFile, []byte(`[test-profile]
aws_access_key_id = test-id
aws_secret_access_key = test-secret`), 0600))

t.Setenv("AWS_SHARED_CREDENTIALS_FILE", credentialsFile)
},
assertFunc: func(t *testing.T, cfg *aws.Config, err error) {
assert.NoError(t, err)

creds, err := cfg.Credentials.Retrieve(context.TODO())
assert.NoError(t, err)
assert.Equal(t, "test-id", creds.AccessKeyID)
assert.Equal(t, "test-secret", creds.SecretAccessKey)

// ConfigSources is a slice of config.Config, which in turn is an interface.
// Since we use a LoadOptions object, we assert the type of cfgSrc and then
Expand Down Expand Up @@ -457,7 +482,11 @@ func TestMasterKey_createKMSConfig(t *testing.T) {
key: MasterKey{
Arn: "arn:aws:kms:us-west-2:107501996527:key/612d5f0p-p1l3-45e6-aca6-a5b005693a48",
},
fallback: true,
envFunc: func(t *testing.T) {
t.Setenv("AWS_ACCESS_KEY_ID", "id")
t.Setenv("AWS_SECRET_ACCESS_KEY", "secret")
t.Setenv("AWS_SESSION_TOKEN", "token")
},
assertFunc: func(t *testing.T, cfg *aws.Config, err error) {
assert.NoError(t, err)

Expand All @@ -473,11 +502,8 @@ func TestMasterKey_createKMSConfig(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt := tt
// Set the environment variables if we want to fallback
if tt.fallback {
t.Setenv("AWS_ACCESS_KEY_ID", "id")
t.Setenv("AWS_SECRET_ACCESS_KEY", "secret")
t.Setenv("AWS_SESSION_TOKEN", "token")
if tt.envFunc != nil {
tt.envFunc(t)
}
cfg, err := tt.key.createKMSConfig()
tt.assertFunc(t, cfg, err)
Expand Down

0 comments on commit 85edde7

Please sign in to comment.