Skip to content

Commit 0320e9f

Browse files
authored
fix(scw): config.String() panic with an empty profile (scaleway#2542)
1 parent 732d265 commit 0320e9f

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

scw/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ func (c *Config) String() string {
145145
c2 := c.clone()
146146
c2.SecretKey = hideSecretKey(c2.SecretKey)
147147
for _, p := range c2.Profiles {
148+
if p == nil {
149+
continue
150+
}
148151
p.SecretKey = hideSecretKey(p.SecretKey)
149152
}
150153

scw/config_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,3 +847,32 @@ profiles:
847847
func TestEmptyConfig(t *testing.T) {
848848
testhelpers.Assert(t, (&Config{}).IsEmpty(), "Config must be empty")
849849
}
850+
851+
func TestEmptyProfile(t *testing.T) {
852+
incompleteProfiles := `
853+
profiles:
854+
profile1:
855+
access_key: SCW234567890ABCDEFGH
856+
secret_key: 6f6e6574-6f72-756c-6c74-68656d616c6c
857+
# default_organization_id: 11111111-1111-1111-1111-111111111111
858+
# default_project_id: 11111111-1111-1111-1111-111111111111
859+
# default_zone: fr-par-1
860+
# default_region: fr-pargs
861+
# api_url: https://api.scaleway.com
862+
# insecure: false
863+
864+
profile2:
865+
# access_key: SCW1234567890ABCDEFG
866+
# secret_key: 7363616c-6577-6573-6862-6f7579616161
867+
`
868+
869+
config, err := unmarshalConfV2([]byte(incompleteProfiles))
870+
testhelpers.AssertNoError(t, err)
871+
testhelpers.Equals(t, 2, len(config.Profiles))
872+
testhelpers.Equals(t, `profiles:
873+
profile1:
874+
access_key: SCW234567890ABCDEFGH
875+
secret_key: 6f6e6574-xxxx-xxxx-xxxx-xxxxxxxxxxxx
876+
profile2: null
877+
`, config.String())
878+
}

0 commit comments

Comments
 (0)