Skip to content

Commit 14f5979

Browse files
authored
config: Fix bug in SDK's merging of duration_seconds shared config (#1568)
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
1 parent a1bf7dd commit 14f5979

File tree

6 files changed

+95
-19
lines changed

6 files changed

+95
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"id": "3989a774-6d05-464b-a5ad-8f91386e6e8a",
3+
"type": "bugfix",
4+
"collapse": true,
5+
"description": "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. [#1568](https://github.com/aws/aws-sdk-go-v2/pull/1568). Thanks to [Amir Szekely](https://github.com/kichik) for help reproduce this bug.",
6+
"modules": [
7+
"config",
8+
"internal/ini"
9+
]
10+
}

config/shared_config_test.go

+39-3
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,36 @@ func TestNewSharedConfig(t *testing.T) {
534534
Profile: "invaliddefaultsmode",
535535
Err: fmt.Errorf("failed to load defaults_mode from shared config, invalid value: invalid"),
536536
},
537+
"merged profiles across files": {
538+
ConfigFilenames: []string{testConfigFilename},
539+
CredentialsFilenames: []string{testCredentialsFilename},
540+
Profile: "merged_profiles",
541+
Expected: SharedConfig{
542+
Profile: "merged_profiles",
543+
RoleARN: "creds_profile_arn",
544+
RoleDurationSeconds: aws.Duration(1023 * time.Second),
545+
},
546+
},
547+
"merged profiles across config files": {
548+
ConfigFilenames: []string{testConfigFilename, testConfigFilename},
549+
CredentialsFilenames: []string{},
550+
Profile: "merged_profiles",
551+
Expected: SharedConfig{
552+
Profile: "merged_profiles",
553+
RoleARN: "config_profile_arn",
554+
RoleDurationSeconds: aws.Duration(3601 * time.Second),
555+
},
556+
},
557+
"merged profiles across credentials files": {
558+
ConfigFilenames: []string{},
559+
CredentialsFilenames: []string{testCredentialsFilename, testCredentialsFilename},
560+
Profile: "merged_profiles",
561+
Expected: SharedConfig{
562+
Profile: "merged_profiles",
563+
RoleARN: "creds_profile_arn",
564+
RoleDurationSeconds: aws.Duration(1023 * time.Second),
565+
},
566+
},
537567
}
538568

539569
for name, c := range cases {
@@ -593,6 +623,12 @@ func TestLoadSharedConfigFromSection(t *testing.T) {
593623
Profile: "profile partial_creds",
594624
Expected: SharedConfig{},
595625
},
626+
"profile with role duration": {
627+
Profile: "profile with_role_duration",
628+
Expected: SharedConfig{
629+
RoleDurationSeconds: aws.Duration(3601 * time.Second),
630+
},
631+
},
596632
"profile with complete creds": {
597633
Profile: "profile complete_creds",
598634
Expected: SharedConfig{
@@ -689,12 +725,12 @@ func TestLoadSharedConfigFromSection(t *testing.T) {
689725
}
690726
return
691727
}
692-
693728
if err != nil {
694729
t.Fatalf("expect no error, got %v", err)
695730
}
696-
if e, a := c.Expected, cfg; !reflect.DeepEqual(e, a) {
697-
t.Errorf("expect %v, got %v", e, a)
731+
732+
if diff := cmp.Diff(c.Expected, cfg); diff != "" {
733+
t.Errorf("expect shared config match\n%s", diff)
698734
}
699735
})
700736
}

config/testdata/shared_config

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ aws_secret_access_key = SECRET
1313
[profile alt_profile_name]
1414
region = alt_profile_name_region
1515

16+
[profile with_role_duration]
17+
duration_seconds = 3601
18+
19+
[profile merged_profiles]
20+
duration_seconds = 3601
21+
role_arn = config_profile_arn
22+
1623
[profile short_profile_name_first]
1724
region = short_profile_name_first_short
1825

config/testdata/shared_credentials

+4
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ region = eu-west-2
1818

1919
[DONOTNORMALIZE]
2020
region = eu-west-3
21+
22+
[merged_profiles]
23+
duration_seconds = 1023
24+
role_arn = creds_profile_arn

internal/ini/literal_tokens.go

+2-16
Original file line numberDiff line numberDiff line change
@@ -216,22 +216,8 @@ func NewStringValue(str string) (Value, error) {
216216

217217
// NewIntValue returns a Value type generated using an int64 input.
218218
func NewIntValue(i int64) (Value, error) {
219-
return newValue(IntegerType, 10, []rune{rune(i)})
220-
}
221-
222-
// Append will append values and change the type to a string
223-
// type.
224-
func (v *Value) Append(tok Token) {
225-
r := tok.Raw()
226-
if v.Type != QuotedStringType {
227-
v.Type = StringType
228-
r = tok.raw[1 : len(tok.raw)-1]
229-
}
230-
if tok.Type() != TokenLit {
231-
v.raw = append(v.raw, tok.Raw()...)
232-
} else {
233-
v.raw = append(v.raw, r...)
234-
}
219+
v := strconv.FormatInt(i, 10)
220+
return newValue(IntegerType, 10, []rune(v))
235221
}
236222

237223
func (v Value) String() string {

internal/ini/literal_tokens_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,36 @@ func TestNewLiteralToken(t *testing.T) {
196196
})
197197
}
198198
}
199+
200+
func TestNewStringValue(t *testing.T) {
201+
const expect = "abc123"
202+
203+
actual, err := NewStringValue(expect)
204+
if err != nil {
205+
t.Fatalf("expect no error, %v", err)
206+
}
207+
208+
if e, a := StringType, actual.Type; e != a {
209+
t.Errorf("expect %v type got %v", e, a)
210+
}
211+
if e, a := expect, actual.str; e != a {
212+
t.Errorf("expect %v string got %v", e, a)
213+
}
214+
}
215+
216+
func TestNewIntValue(t *testing.T) {
217+
const expect int64 = 1234
218+
219+
actual, err := NewIntValue(expect)
220+
if err != nil {
221+
t.Fatalf("expect no error, %v", err)
222+
}
223+
224+
if e, a := IntegerType, actual.Type; e != a {
225+
t.Errorf("expect %v type got %v", e, a)
226+
}
227+
if e, a := expect, actual.integer; e != a {
228+
t.Errorf("expect %v integer got %v", e, a)
229+
}
230+
231+
}

0 commit comments

Comments
 (0)