Skip to content

Commit 25543b3

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 25543b3

22 files changed

+301
-71
lines changed

api/common/attributes.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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:Schemaless
10+
// +kubebuilder:pruning:PreserveUnknownFields
11+
type UserAttributes map[string][]string
12+
13+
// UnmarshalJSON implements json.Unmarshaler.
14+
func (a *UserAttributes) UnmarshalJSON(data []byte) error {
15+
var raw map[string]interface{}
16+
if err := json.Unmarshal(data, &raw); err != nil {
17+
return err
18+
}
19+
20+
if *a == nil {
21+
*a = make(UserAttributes, len(raw))
22+
}
23+
24+
result := *a
25+
26+
for k, v := range raw {
27+
switch value := v.(type) {
28+
case string:
29+
result[k] = []string{value}
30+
case []interface{}:
31+
var strSlice []string
32+
33+
for _, item := range value {
34+
if str, ok := item.(string); ok {
35+
strSlice = append(strSlice, str)
36+
} else {
37+
return fmt.Errorf("attribute '%s' contains a non-string value in the list", k)
38+
}
39+
}
40+
41+
result[k] = strSlice
42+
default:
43+
return fmt.Errorf("unsupported type for attribute '%s': %T", k, v)
44+
}
45+
}
46+
47+
return nil
48+
}

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:

deploy-templates/crds/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.

deploy-templates/crds/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:

docs/api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3392,7 +3392,7 @@ ServiceAccount is a service account configuration.
33923392
</thead>
33933393
<tbody><tr>
33943394
<td><b>attributes</b></td>
3395-
<td>map[string]string</td>
3395+
<td>map[string][]string</td>
33963396
<td>
33973397
Attributes is a map of service account attributes.<br/>
33983398
</td>
@@ -6417,7 +6417,7 @@ KeycloakRealmUserSpec defines the desired state of KeycloakRealmUser.
64176417
<td>true</td>
64186418
</tr><tr>
64196419
<td><b>attributes</b></td>
6420-
<td>map[string]string</td>
6420+
<td>map[string][]string</td>
64216421
<td>
64226422
Attributes is a map of user attributes.<br/>
64236423
</td>

0 commit comments

Comments
 (0)