Skip to content

Commit a436fe6

Browse files
authored
fix: defer config value type assertion to load stage instead of parse (#2286)
1 parent 807c921 commit a436fe6

18 files changed

+96
-963
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"id": "87dcac43-e510-40c3-8f8e-72fb9b874042",
3+
"type": "bugfix",
4+
"description": "Move type assertion of config values out of the parsing stage, which resolves an issue where the contents of a profile would silently be dropped with certain numeric formats.",
5+
"modules": [
6+
"config",
7+
"internal/ini"
8+
]
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"id": "8e58ee8f-ce61-452e-b601-26004c893c2d",
3+
"type": "bugfix",
4+
"description": "Fixed a bug where merging `max_attempts` or `duration_seconds` fields across shared config files with invalid values would silently default them to 0.",
5+
"modules": [
6+
"config",
7+
"internal/ini"
8+
]
9+
}

config/shared_config.go

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,8 @@ func mergeSections(dst *ini.Sections, src ini.Sections) error {
740740
defaultsModeKey,
741741
retryModeKey,
742742
caBundleKey,
743+
roleDurationSecondsKey,
744+
retryMaxAttemptsKey,
743745

744746
ssoSessionNameKey,
745747
ssoAccountIDKey,
@@ -753,16 +755,6 @@ func mergeSections(dst *ini.Sections, src ini.Sections) error {
753755
}
754756
}
755757

756-
intKeys := []string{
757-
roleDurationSecondsKey,
758-
retryMaxAttemptsKey,
759-
}
760-
for i := range intKeys {
761-
if err := mergeIntKey(&srcSection, &dstSection, sectionName, intKeys[i]); err != nil {
762-
return err
763-
}
764-
}
765-
766758
// set srcSection on dst srcSection
767759
*dst = dst.SetSection(sectionName, dstSection)
768760
}
@@ -789,26 +781,6 @@ func mergeStringKey(srcSection *ini.Section, dstSection *ini.Section, sectionNam
789781
return nil
790782
}
791783

792-
func mergeIntKey(srcSection *ini.Section, dstSection *ini.Section, sectionName, key string) error {
793-
if srcSection.Has(key) {
794-
srcValue := srcSection.Int(key)
795-
v, err := ini.NewIntValue(srcValue)
796-
if err != nil {
797-
return fmt.Errorf("error merging %s, %w", key, err)
798-
}
799-
800-
if dstSection.Has(key) {
801-
dstSection.Logs = append(dstSection.Logs, newMergeKeyLogMessage(sectionName, key,
802-
dstSection.SourceFile[key], srcSection.SourceFile[key]))
803-
804-
}
805-
806-
dstSection.UpdateValue(key, v)
807-
dstSection.UpdateSourceFile(key, srcSection.SourceFile[key])
808-
}
809-
return nil
810-
}
811-
812784
func newMergeKeyLogMessage(sectionName, key, dstSourceFile, srcSourceFile string) string {
813785
return fmt.Sprintf("For profile: %v, overriding %v value, defined in %v "+
814786
"with a %v value found in a duplicate profile defined at file %v. \n",
@@ -962,9 +934,16 @@ func (c *SharedConfig) setFromIniSection(profile string, section ini.Section) er
962934
updateString(&c.SSOAccountID, section, ssoAccountIDKey)
963935
updateString(&c.SSORoleName, section, ssoRoleNameKey)
964936

937+
// we're retaining a behavioral quirk with this field that existed before
938+
// the removal of literal parsing for #2276:
939+
// - if the key is missing, the config field will not be set
940+
// - if the key is set to a non-numeric, the config field will be set to 0
965941
if section.Has(roleDurationSecondsKey) {
966-
d := time.Duration(section.Int(roleDurationSecondsKey)) * time.Second
967-
c.RoleDurationSeconds = &d
942+
if v, ok := section.Int(roleDurationSecondsKey); ok {
943+
c.RoleDurationSeconds = aws.Duration(time.Duration(v) * time.Second)
944+
} else {
945+
c.RoleDurationSeconds = aws.Duration(time.Duration(0))
946+
}
968947
}
969948

970949
updateString(&c.CredentialProcess, section, credentialProcessKey)
@@ -1314,12 +1293,13 @@ func updateInt(dst *int, section ini.Section, key string) error {
13141293
if !section.Has(key) {
13151294
return nil
13161295
}
1317-
if vt, _ := section.ValueType(key); vt != ini.IntegerType {
1318-
return fmt.Errorf("invalid value %s=%s, expect integer",
1319-
key, section.String(key))
13201296

1297+
v, ok := section.Int(key)
1298+
if !ok {
1299+
return fmt.Errorf("invalid value %s=%s, expect integer", key, section.String(key))
13211300
}
1322-
*dst = int(section.Int(key))
1301+
1302+
*dst = int(v)
13231303
return nil
13241304
}
13251305

@@ -1329,7 +1309,10 @@ func updateBool(dst *bool, section ini.Section, key string) {
13291309
if !section.Has(key) {
13301310
return
13311311
}
1332-
*dst = section.Bool(key)
1312+
1313+
// retains pre-#2276 behavior where non-bool value would resolve to false
1314+
v, _ := section.Bool(key)
1315+
*dst = v
13331316
}
13341317

13351318
// updateBoolPtr will only update the dst with the value in the section key,
@@ -1338,8 +1321,11 @@ func updateBoolPtr(dst **bool, section ini.Section, key string) {
13381321
if !section.Has(key) {
13391322
return
13401323
}
1324+
1325+
// retains pre-#2276 behavior where non-bool value would resolve to false
1326+
v, _ := section.Bool(key)
13411327
*dst = new(bool)
1342-
**dst = section.Bool(key)
1328+
**dst = v
13431329
}
13441330

13451331
// updateEndpointDiscoveryType will only update the dst with the value in the section, if
@@ -1371,7 +1357,8 @@ func updateUseDualStackEndpoint(dst *aws.DualStackEndpointState, section ini.Sec
13711357
return
13721358
}
13731359

1374-
if section.Bool(key) {
1360+
// retains pre-#2276 behavior where non-bool value would resolve to false
1361+
if v, _ := section.Bool(key); v {
13751362
*dst = aws.DualStackEndpointStateEnabled
13761363
} else {
13771364
*dst = aws.DualStackEndpointStateDisabled
@@ -1387,7 +1374,8 @@ func updateUseFIPSEndpoint(dst *aws.FIPSEndpointState, section ini.Section, key
13871374
return
13881375
}
13891376

1390-
if section.Bool(key) {
1377+
// retains pre-#2276 behavior where non-bool value would resolve to false
1378+
if v, _ := section.Bool(key); v {
13911379
*dst = aws.FIPSEndpointStateEnabled
13921380
} else {
13931381
*dst = aws.FIPSEndpointStateDisabled

internal/ini/ini_lexer_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
)
99

1010
func TestTokenize(t *testing.T) {
11-
numberToken := newToken(TokenLit, []rune("123"), IntegerType)
12-
numberToken.base = 10
1311
cases := []struct {
1412
r io.Reader
1513
expectedTokens []Token
@@ -22,7 +20,7 @@ func TestTokenize(t *testing.T) {
2220
newToken(TokenWS, []rune(" "), NoneType),
2321
newToken(TokenOp, []rune("="), NoneType),
2422
newToken(TokenWS, []rune(" "), NoneType),
25-
numberToken,
23+
newToken(TokenLit, []rune("123"), StringType),
2624
},
2725
},
2826
{

0 commit comments

Comments
 (0)