Skip to content

Commit 44597aa

Browse files
dougkirkleySergK
authored andcommitted
fix: KeycloakClient service account users groups aren't being populated correctly
Signed-off-by: Douglass Kirkley <doug.kirkley@gmail.com>
1 parent b583e35 commit 44597aa

File tree

6 files changed

+78
-91
lines changed

6 files changed

+78
-91
lines changed

internal/controller/keycloakclient/chain/service_account.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ func (el *ServiceAccount) Serve(_ context.Context, keycloakClient *keycloakApi.K
3838
return errors.Wrap(err, "unable to sync service account roles")
3939
}
4040

41+
if keycloakClient.Spec.ServiceAccount.Groups != nil {
42+
if err := el.keycloakApiClient.SyncServiceAccountGroups(realmName,
43+
keycloakClient.Status.ClientID, keycloakClient.Spec.ServiceAccount.Groups, addOnly); err != nil {
44+
return errors.Wrap(err, "unable to sync service account groups")
45+
}
46+
}
47+
4148
if keycloakClient.Spec.ServiceAccount.Attributes != nil {
4249
if err := el.keycloakApiClient.SetServiceAccountAttributes(realmName, keycloakClient.Status.ClientID,
4350
keycloakClient.Spec.ServiceAccount.Attributes, addOnly); err != nil {
4451
return errors.Wrap(err, "unable to set service account attributes")
4552
}
4653
}
4754

48-
if keycloakClient.Spec.ServiceAccount.Groups != nil {
49-
if err := el.keycloakApiClient.SetServiceAccountGroups(realmName,
50-
keycloakClient.Status.ClientID, keycloakClient.Spec.ServiceAccount.Groups, addOnly); err != nil {
51-
return errors.Wrap(err, "unable to sync service account groups")
52-
}
53-
}
54-
5555
return nil
5656
}

internal/controller/keycloakclient/chain/service_account_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ func TestServiceAccount_Serve(t *testing.T) {
4545
kc.Spec.ServiceAccount.RealmRoles,
4646
map[string][]string{
4747
kc.Spec.ServiceAccount.ClientRoles[0].ClientID: kc.Spec.ServiceAccount.ClientRoles[0].Roles}, false).Return(nil)
48+
apiClient.On("SyncServiceAccountGroups", realmName, kc.Status.ClientID,
49+
kc.Spec.ServiceAccount.Groups, false).Return(nil)
4850
apiClient.On("SetServiceAccountAttributes", realmName, kc.Status.ClientID,
4951
kc.Spec.ServiceAccount.Attributes, false).Return(nil)
50-
apiClient.On("SetServiceAccountGroups", realmName, kc.Status.ClientID,
51-
kc.Spec.ServiceAccount.Groups, false).Return(nil)
5252

5353
sa := NewServiceAccount(apiClient)
5454

internal/controller/keycloakclient/keycloakclient_controller_integration_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ var _ = Describe("KeycloakClient controller", Ordered, func() {
9595
}, timeout, interval).Should(BeTrue(), "KeycloakClient should be deleted")
9696
})
9797
It("Should create KeycloakClient with empty secret", func() {
98+
By("Creating keycloak api client")
99+
client := gocloak.NewClient(keycloakURL)
100+
token, err := client.LoginAdmin(ctx, "admin", "admin", "master")
101+
Expect(err).ShouldNot(HaveOccurred())
102+
By("Creating group for service account")
103+
_, err = client.CreateGroup(ctx, token.AccessToken, KeycloakRealmCR, gocloak.Group{
104+
Name: gocloak.StringP("test-group"),
105+
})
106+
Expect(adapter.SkipAlreadyExistsErr(err)).ShouldNot(HaveOccurred())
98107
By("Creating a KeycloakClient")
99108
keycloakClient := &keycloakApi.KeycloakClient{
100109
ObjectMeta: metav1.ObjectMeta{
@@ -187,7 +196,7 @@ var _ = Describe("KeycloakClient controller", Ordered, func() {
187196
},
188197
}
189198
Expect(k8sClient.Create(ctx, clientSecret)).Should(Succeed())
190-
By("Crating keycloak api client")
199+
By("Creating keycloak api client")
191200
client := gocloak.NewClient(keycloakURL)
192201
token, err := client.LoginAdmin(ctx, "admin", "admin", "master")
193202
Expect(err).ShouldNot(HaveOccurred())

pkg/client/keycloak/adapter/gocloak_adapter_service_account.go

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ func (a GoCloakAdapter) SyncServiceAccountRoles(realm, clientID string, realmRol
4242
return nil
4343
}
4444

45+
func (a GoCloakAdapter) SyncServiceAccountGroups(realm, clientID string, groups []string, addOnly bool) error {
46+
user, err := a.client.GetClientServiceAccount(context.Background(), a.token.AccessToken, realm, clientID)
47+
if err != nil {
48+
return errors.Wrap(err, "unable to get client service account")
49+
}
50+
51+
return a.syncUserGroups(context.Background(), realm, *user.ID, groups, addOnly)
52+
}
53+
4554
func doNotDeleteRealmRoleFromUser(ctx context.Context, token, realm, entityID string, roles []gocloak.Role) error {
4655
return nil
4756
}
@@ -74,34 +83,3 @@ func (a GoCloakAdapter) SetServiceAccountAttributes(realm, clientID string, attr
7483

7584
return nil
7685
}
77-
78-
func (a GoCloakAdapter) SetServiceAccountGroups(realm, clientID string, groups []string, addOnly bool) error {
79-
user, err := a.client.GetClientServiceAccount(context.Background(), a.token.AccessToken, realm, clientID)
80-
if err != nil {
81-
return errors.Wrap(err, "unable to get client service account")
82-
}
83-
84-
svcGroups := make(map[string]struct{})
85-
if addOnly && user.Groups != nil {
86-
for _, group := range *user.Groups {
87-
svcGroups[group] = struct{}{}
88-
}
89-
}
90-
91-
for _, group := range groups {
92-
svcGroups[group] = struct{}{}
93-
}
94-
95-
var newGroups []string
96-
for group := range svcGroups {
97-
newGroups = append(newGroups, group)
98-
}
99-
100-
user.Groups = &newGroups
101-
102-
if err = a.client.UpdateUser(context.Background(), a.token.AccessToken, realm, *user); err != nil {
103-
return errors.Wrapf(err, "unable to update service account user: %s", clientID)
104-
}
105-
106-
return nil
107-
}

pkg/client/keycloak/keycloak_client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ type Client interface {
2626
GetOpenIdConfig(realm *dto.Realm) (string, error)
2727
SyncServiceAccountRoles(realm, clientID string, realmRoles []string,
2828
clientRoles map[string][]string, addOnly bool) error
29+
SyncServiceAccountGroups(realm, clientID string, groups []string, addOnly bool) error
2930
SetServiceAccountAttributes(realm, clientID string, attributes map[string]string, addOnly bool) error
30-
SetServiceAccountGroups(realm, clientID string, groups []string, addOnly bool) error
3131
ExportToken() ([]byte, error)
3232
}
3333

pkg/client/keycloak/mocks/client_mock.go

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

0 commit comments

Comments
 (0)