|
| 1 | +package aws |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/1Password/shell-plugins/sdk" |
| 10 | + "github.com/1Password/shell-plugins/sdk/schema/fieldname" |
| 11 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +// The first two profiles are the ~/.aws/config from issue #559. |
| 17 | +const providerChainConfig = `[profile 1pass-source-profile] |
| 18 | +region = eu-west-1 |
| 19 | +
|
| 20 | +[profile my-1password-profile] |
| 21 | +source_profile = 1pass-source-profile |
| 22 | +role_arn = arn:aws:iam::222222222222:role/MyRole |
| 23 | +region = eu-west-1 |
| 24 | +
|
| 25 | +[profile direct-role] |
| 26 | +role_arn = arn:aws:iam::222222222222:role/MyRole |
| 27 | +region = eu-west-1 |
| 28 | +
|
| 29 | +[profile chained-role] |
| 30 | +source_profile = my-1password-profile |
| 31 | +role_arn = arn:aws:iam::333333333333:role/ChainedRole |
| 32 | +region = eu-west-1 |
| 33 | +
|
| 34 | +[profile role-with-local-mfa] |
| 35 | +source_profile = 1pass-source-profile |
| 36 | +role_arn = arn:aws:iam::222222222222:role/MyRole |
| 37 | +mfa_serial = arn:aws:iam::111111111111:mfa/some.user |
| 38 | +region = eu-west-1 |
| 39 | +` |
| 40 | + |
| 41 | +// stsCallChain lists the STS calls a provider makes, outermost first, ending |
| 42 | +// with what signs the innermost call. |
| 43 | +func stsCallChain(p aws.CredentialsProvider) []string { |
| 44 | + var chain []string |
| 45 | + for p != nil { |
| 46 | + switch v := p.(type) { |
| 47 | + case *assumeRoleProvider: |
| 48 | + chain = append(chain, "AssumeRole") |
| 49 | + p = v.StsClient.Options().Credentials |
| 50 | + case *mfaSessionTokenProvider: |
| 51 | + hop := "GetSessionToken" |
| 52 | + if v.Mfa != nil && v.GetMfaSerial() != "" { |
| 53 | + hop += "(MFA)" |
| 54 | + } |
| 55 | + chain = append(chain, hop) |
| 56 | + p = v.StsClient.Options().Credentials |
| 57 | + case accessKeysProvider: |
| 58 | + chain = append(chain, "long-lived-keys") |
| 59 | + p = nil |
| 60 | + default: |
| 61 | + chain = append(chain, fmt.Sprintf("%T", p)) |
| 62 | + p = nil |
| 63 | + } |
| 64 | + } |
| 65 | + return chain |
| 66 | +} |
| 67 | + |
| 68 | +func TestSTSProviderChain(t *testing.T) { |
| 69 | + configPath := filepath.Join(t.TempDir(), "awsConfig") |
| 70 | + require.NoError(t, os.WriteFile(configPath, []byte(providerChainConfig), 0600)) |
| 71 | + t.Setenv("AWS_CONFIG_FILE", configPath) |
| 72 | + t.Setenv("AWS_PROFILE", "") |
| 73 | + t.Setenv("AWS_DEFAULT_REGION", "") |
| 74 | + |
| 75 | + keysAndOTP := map[sdk.FieldName]string{ |
| 76 | + fieldname.AccessKeyID: "AKIAHPIZFMD5EEXAMPLE", |
| 77 | + fieldname.SecretAccessKey: "lBfKB7P5ScmpxDeRoFLZvhJbqNGPoV0vIEXAMPLE", |
| 78 | + fieldname.OneTimePassword: "123456", |
| 79 | + } |
| 80 | + withItemSerial := map[sdk.FieldName]string{fieldname.MFASerial: "arn:aws:iam::111111111111:mfa/some.user"} |
| 81 | + for k, v := range keysAndOTP { |
| 82 | + withItemSerial[k] = v |
| 83 | + } |
| 84 | + |
| 85 | + for _, c := range []struct { |
| 86 | + name, profile string |
| 87 | + itemFields map[sdk.FieldName]string |
| 88 | + want []string |
| 89 | + }{ |
| 90 | + {"source_profile with role", "my-1password-profile", withItemSerial, []string{"AssumeRole", "GetSessionToken(MFA)", "long-lived-keys"}}, |
| 91 | + {"role without source_profile", "direct-role", withItemSerial, []string{"AssumeRole", "GetSessionToken(MFA)", "long-lived-keys"}}, |
| 92 | + {"role chained through role", "chained-role", withItemSerial, []string{"AssumeRole", "AssumeRole", "GetSessionToken(MFA)", "long-lived-keys"}}, |
| 93 | + // mfa_serial set only on the role profile must still reach the chain root. |
| 94 | + {"mfa_serial only on role profile", "role-with-local-mfa", keysAndOTP, []string{"AssumeRole", "GetSessionToken(MFA)", "long-lived-keys"}}, |
| 95 | + } { |
| 96 | + t.Run(c.name, func(t *testing.T) { |
| 97 | + awsConfig, err := getAWSAuthConfigurationForProfile(c.profile) |
| 98 | + require.NoError(t, err) |
| 99 | + |
| 100 | + // Cold cache: nothing to short-circuit the STS calls. |
| 101 | + factory := &CacheProviderFactory{ |
| 102 | + InCache: sdk.CacheState{}, |
| 103 | + OutCache: sdk.CacheOperations{Puts: map[string]sdk.CacheEntry{}}, |
| 104 | + ItemFields: c.itemFields, |
| 105 | + } |
| 106 | + provider, err := GetTemporaryCredentialsProviderForProfile(awsConfig, factory, c.itemFields) |
| 107 | + require.NoError(t, err) |
| 108 | + |
| 109 | + assert.Equal(t, c.want, stsCallChain(provider)) |
| 110 | + }) |
| 111 | + } |
| 112 | +} |
0 commit comments