Skip to content

Commit 61def6b

Browse files
zmotsodougkirkley
authored andcommitted
feat: Add support for list of attributes with the same key
Signed-off-by: Douglass Kirkley <doug.kirkley@gmail.com>
1 parent 86424d4 commit 61def6b

33 files changed

+313
-992
lines changed

api/common/attributes.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package common
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
)
7+
8+
// UserAttributes is a map of user attributes. It supports both string and []string values for attributes.
9+
// +kubebuilder:validation:Type=string,stringSlice
10+
type UserAttributes map[string][]string
11+
12+
// UnmarshalJSON implements json.Unmarshaler.
13+
func (a *UserAttributes) UnmarshalJSON(data []byte) error {
14+
var raw map[string]interface{}
15+
if err := json.Unmarshal(data, &raw); err != nil {
16+
return err
17+
}
18+
19+
if *a == nil {
20+
*a = make(UserAttributes, len(raw))
21+
}
22+
23+
result := *a
24+
25+
for k, v := range raw {
26+
switch value := v.(type) {
27+
case string:
28+
result[k] = []string{value}
29+
case []interface{}:
30+
var strSlice []string
31+
32+
for _, item := range value {
33+
if str, ok := item.(string); ok {
34+
strSlice = append(strSlice, str)
35+
} else {
36+
return fmt.Errorf("attribute '%s' contains a non-string value in the list", k)
37+
}
38+
}
39+
40+
result[k] = strSlice
41+
default:
42+
return fmt.Errorf("unsupported type for attribute '%s': %T", k, v)
43+
}
44+
}
45+
46+
return nil
47+
}

api/common/zz_generated.deepcopy.go

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1/keycloakclient_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ type ServiceAccount struct {
215215
// Attributes is a map of service account attributes.
216216
// +nullable
217217
// +optional
218-
Attributes map[string]string `json:"attributes,omitempty"`
218+
Attributes common.UserAttributes `json:"attributes,omitempty"`
219219

220220
// Groups is a list of groups assigned to service account
221221
// +nullable

api/v1/keycloakrealmuser_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ type KeycloakRealmUserSpec struct {
5858
// Attributes is a map of user attributes.
5959
// +nullable
6060
// +optional
61-
Attributes map[string]string `json:"attributes,omitempty"`
61+
Attributes common.UserAttributes `json:"attributes,omitempty"`
6262

6363
// ReconciliationStrategy is a strategy for reconciliation. Possible values: full, create-only.
6464
// Default value: full. If set to create-only, user will be created only if it does not exist. If user exists, it will not be updated.

api/v1/zz_generated.deepcopy.go

Lines changed: 22 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/v1.edp.epam.com_keycloakclients.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,10 +612,13 @@ spec:
612612
properties:
613613
attributes:
614614
additionalProperties:
615-
type: string
615+
items:
616+
type: string
617+
type: array
616618
description: Attributes is a map of service account attributes.
617619
nullable: true
618620
type: object
621+
x-kubernetes-preserve-unknown-fields: true
619622
clientRoles:
620623
description: ClientRoles is a list of client roles assigned to
621624
service account.

config/crd/bases/v1.edp.epam.com_keycloakrealmusers.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ spec:
4646
properties:
4747
attributes:
4848
additionalProperties:
49-
type: string
49+
items:
50+
type: string
51+
type: array
5052
description: Attributes is a map of user attributes.
5153
nullable: true
5254
type: object
55+
x-kubernetes-preserve-unknown-fields: true
5356
clientRoles:
5457
description: ClientRoles is a list of client roles assigned to user.
5558
items:

0 commit comments

Comments
 (0)