Skip to content

Commit

Permalink
Fix empty identity value regression
Browse files Browse the repository at this point in the history
The regression was introduced after adding a custom unmarshaler for consumer identities which allows to specify values other than string (e.g. port: 5000), even though consumer identity is a map[string]string.
  • Loading branch information
fabianburth committed Nov 7, 2024
1 parent 2297b18 commit 809820f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
12 changes: 11 additions & 1 deletion api/credentials/internal/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,32 @@ func orMatcher(list []IdentityMatcher) IdentityMatcher {
// ConsumerIdentity describes the identity of a credential consumer.
type ConsumerIdentity map[string]string

// UnmarshalJSON allows a yaml specification containing a data type other
// string, e.g. a hostpath spec with a port. Previously, it would error if the
// user specified `port: 5000` and instead, the user had to specify
// `port: "5000"`

Check failure on line 99 in api/credentials/internal/identity.go

View workflow job for this annotation

GitHub Actions / Lint Golang

Comment should end in a period (godot)
func (c *ConsumerIdentity) UnmarshalJSON(data []byte) error {
var m map[string]interface{}
err := runtime.DefaultJSONEncoding.Unmarshal(data, &m)
if err != nil {
return err
}

if len(m) == 0 {
return nil
}
*c = make(map[string]string, len(m))
for k, v := range m {
switch v.(type) {
case nil:
(*c)[k] = ""
case map[string]interface{}:
return fmt.Errorf("cannot unmarshal complex type into consumer identity")
case []interface{}:
return fmt.Errorf("cannot unmarshal complex type into consumer identity")
default:
(*c)[k] = fmt.Sprintf("%v", v)
}
(*c)[k] = fmt.Sprintf("%v", v)
}
return nil
}
Expand Down
15 changes: 15 additions & 0 deletions api/credentials/internal/identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,19 @@ port:
cid := internal.ConsumerIdentity{}
Expect(yaml.Unmarshal([]byte(data), &cid)).NotTo(Succeed())
})
It("with nil", func() {
data := `
scheme: http
hostname: 127.0.0.1
port:
`
id := internal.ConsumerIdentity{
"scheme": "http",
"hostname": "127.0.0.1",
"port": "",
}
cid := internal.ConsumerIdentity{}
Expect(yaml.Unmarshal([]byte(data), &cid)).To(Succeed())
Expect(cid).To(Equal(id))
})
})

0 comments on commit 809820f

Please sign in to comment.