Skip to content

Commit

Permalink
config: Fix bug in SDK's merging of duration_seconds shared config
Browse files Browse the repository at this point in the history
Fixes the SDK's handling of `duration_sections` in the shared
credentials file or specified in multiple shared config and shared
credentials files under the same profile.

Fixes #1192
  • Loading branch information
jasdel committed Jan 21, 2022
1 parent 6625113 commit b9e1544
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 19 deletions.
42 changes: 39 additions & 3 deletions config/shared_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,36 @@ func TestNewSharedConfig(t *testing.T) {
Profile: "invaliddefaultsmode",
Err: fmt.Errorf("failed to load defaults_mode from shared config, invalid value: invalid"),
},
"merged profiles across files": {
ConfigFilenames: []string{testConfigFilename},
CredentialsFilenames: []string{testCredentialsFilename},
Profile: "merged_profiles",
Expected: SharedConfig{
Profile: "merged_profiles",
RoleARN: "creds_profile_arn",
RoleDurationSeconds: aws.Duration(1023 * time.Second),
},
},
"merged profiles across config files": {
ConfigFilenames: []string{testConfigFilename, testConfigFilename},
CredentialsFilenames: []string{},
Profile: "merged_profiles",
Expected: SharedConfig{
Profile: "merged_profiles",
RoleARN: "config_profile_arn",
RoleDurationSeconds: aws.Duration(3601 * time.Second),
},
},
"merged profiles across credentials files": {
ConfigFilenames: []string{},
CredentialsFilenames: []string{testCredentialsFilename, testCredentialsFilename},
Profile: "merged_profiles",
Expected: SharedConfig{
Profile: "merged_profiles",
RoleARN: "creds_profile_arn",
RoleDurationSeconds: aws.Duration(1023 * time.Second),
},
},
}

for name, c := range cases {
Expand Down Expand Up @@ -593,6 +623,12 @@ func TestLoadSharedConfigFromSection(t *testing.T) {
Profile: "profile partial_creds",
Expected: SharedConfig{},
},
"profile with role duration": {
Profile: "profile with_role_duration",
Expected: SharedConfig{
RoleDurationSeconds: aws.Duration(3601 * time.Second),
},
},
"profile with complete creds": {
Profile: "profile complete_creds",
Expected: SharedConfig{
Expand Down Expand Up @@ -689,12 +725,12 @@ func TestLoadSharedConfigFromSection(t *testing.T) {
}
return
}

if err != nil {
t.Fatalf("expect no error, got %v", err)
}
if e, a := c.Expected, cfg; !reflect.DeepEqual(e, a) {
t.Errorf("expect %v, got %v", e, a)

if diff := cmp.Diff(c.Expected, cfg); diff != "" {
t.Errorf("expect shared config match\n%s", diff)
}
})
}
Expand Down
7 changes: 7 additions & 0 deletions config/testdata/shared_config
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ aws_secret_access_key = SECRET
[profile alt_profile_name]
region = alt_profile_name_region

[profile with_role_duration]
duration_seconds = 3601

[profile merged_profiles]
duration_seconds = 3601
role_arn = config_profile_arn

[profile short_profile_name_first]
region = short_profile_name_first_short

Expand Down
4 changes: 4 additions & 0 deletions config/testdata/shared_credentials
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ region = eu-west-2

[DONOTNORMALIZE]
region = eu-west-3

[merged_profiles]
duration_seconds = 1023
role_arn = creds_profile_arn
18 changes: 2 additions & 16 deletions internal/ini/literal_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,22 +216,8 @@ func NewStringValue(str string) (Value, error) {

// NewIntValue returns a Value type generated using an int64 input.
func NewIntValue(i int64) (Value, error) {
return newValue(IntegerType, 10, []rune{rune(i)})
}

// Append will append values and change the type to a string
// type.
func (v *Value) Append(tok Token) {
r := tok.Raw()
if v.Type != QuotedStringType {
v.Type = StringType
r = tok.raw[1 : len(tok.raw)-1]
}
if tok.Type() != TokenLit {
v.raw = append(v.raw, tok.Raw()...)
} else {
v.raw = append(v.raw, r...)
}
v := strconv.FormatInt(i, 10)
return newValue(IntegerType, 10, []rune(v))
}

func (v Value) String() string {
Expand Down
33 changes: 33 additions & 0 deletions internal/ini/literal_tokens_test.go
Original file line number Diff line number Diff line change