Skip to content

Commit

Permalink
feat: Add DisplayName to KeycloakRealm/ClusterKeycloakRealm (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
zmotso authored and SergK committed Oct 21, 2024
1 parent 6447eb4 commit 70fbb51
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 6 deletions.
4 changes: 4 additions & 0 deletions api/v1/keycloakrealm_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ type KeycloakRealmSpec struct {
// +nullable
// +optional
TokenSettings *common.TokenSettings `json:"tokenSettings,omitempty"`

// DisplayName is the display name of the realm.
// +optional
DisplayName string `json:"displayName,omitempty"`
}

type User struct {
Expand Down
8 changes: 8 additions & 0 deletions api/v1alpha1/clusterkeycloakrealm_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ type ClusterKeycloakRealmSpec struct {
// +nullable
// +optional
AuthenticationFlow *AuthenticationFlow `json:"authenticationFlows,omitempty"`

// DisplayHTMLName name to render in the UI.
// +optional
DisplayHTMLName string `json:"displayHtmlName,omitempty"`

// DisplayName is the display name of the realm.
// +optional
DisplayName string `json:"displayName,omitempty"`
}

type AuthenticationFlow struct {
Expand Down
6 changes: 6 additions & 0 deletions config/crd/bases/v1.edp.epam.com_clusterkeycloakrealms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ spec:
description: ClusterKeycloakRef is a name of the ClusterKeycloak instance
that owns the realm.
type: string
displayHtmlName:
description: DisplayHTMLName name to render in the UI.
type: string
displayName:
description: DisplayName is the display name of the realm.
type: string
frontendUrl:
description: |-
FrontendURL Set the frontend URL for the realm.
Expand Down
3 changes: 3 additions & 0 deletions config/crd/bases/v1.edp.epam.com_keycloakrealms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ spec:
displayHtmlName:
description: DisplayHTMLName name to render in the UI
type: string
displayName:
description: DisplayName is the display name of the realm.
type: string
frontendUrl:
description: FrontendURL Set the frontend URL for the realm. Use in
combination with the default hostname provider to override the base
Expand Down
4 changes: 3 additions & 1 deletion controllers/clusterkeycloakrealm/chain/put_realm_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ func (h PutRealmSettings) ServeRequest(ctx context.Context, realm *v1alpha1.Clus
}

settings := adapter.RealmSettings{
FrontendURL: realm.Spec.FrontendURL,
FrontendURL: realm.Spec.FrontendURL,
DisplayHTMLName: realm.Spec.DisplayHTMLName,
DisplayName: realm.Spec.DisplayName,
}

if realm.Spec.Themes != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ var _ = Describe("ClusterKeycloakRealm controller", func() {
ActionTokenGeneratedByUserLifespan: 234,
ActionTokenGeneratedByAdminLifespan: 235,
},
DisplayName: "Test Realm",
DisplayHTMLName: "<b>Test Realm</b>",
},
}
Expect(k8sClient.Create(ctx, keycloakRealm)).Should(Succeed())
Expand Down
1 change: 1 addition & 0 deletions controllers/keycloakrealm/chain/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func TestCreateDefChain(t *testing.T) {
kClient.On(
"CreateRealmWithDefaultConfig", &dto.Realm{Name: realmName}).
Return(nil)
kClient.On("UpdateRealmSettings", testifymock.Anything, testifymock.Anything).Return(nil)

hm := helpermock.NewControllerHelper(t)

Expand Down
6 changes: 1 addition & 5 deletions controllers/keycloakrealm/chain/realm_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,10 @@ func (h RealmSettings) ServeRequest(ctx context.Context, realm *keycloakApi.Keyc
}
}

if realm.Spec.BrowserSecurityHeaders == nil && realm.Spec.Themes == nil && len(realm.Spec.PasswordPolicies) == 0 {
rLog.Info("Realm settings is not set, exit.")
return nextServeOrNil(ctx, h.next, realm, kClient)
}

settings := adapter.RealmSettings{
DisplayHTMLName: realm.Spec.DisplayHTMLName,
FrontendURL: realm.Spec.FrontendURL,
DisplayName: realm.Spec.DisplayName,
}

if realm.Spec.Themes != nil {
Expand Down
5 changes: 5 additions & 0 deletions controllers/keycloakrealm/chain/realm_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/pkg/errors"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

keycloakApi "github.com/epam/edp-keycloak-operator/api/v1"
Expand All @@ -19,6 +20,8 @@ func TestRealmSettings_ServeRequest(t *testing.T) {
realm := keycloakApi.KeycloakRealm{}
ctx := context.Background()

kClient.On("UpdateRealmSettings", mock.Anything, mock.Anything).Return(nil)

err := rs.ServeRequest(ctx, &realm, kClient)
require.NoError(t, err)

Expand Down Expand Up @@ -56,6 +59,7 @@ func TestRealmSettings_ServeRequest(t *testing.T) {
},
DisplayHTMLName: realm.Spec.DisplayHTMLName,
FrontendURL: realm.Spec.FrontendURL,
DisplayName: realm.Spec.DisplayName,
}).Return(nil)

kClient.On("SetRealmEventConfig", realm.Spec.RealmName, &adapter.RealmEventConfig{
Expand All @@ -68,6 +72,7 @@ func TestRealmSettings_ServeRequest(t *testing.T) {
kClient.On("SetRealmEventConfig", realm.Spec.RealmName, &adapter.RealmEventConfig{
EventsListeners: []string{"foo", "bar"},
}).Return(errors.New("event config fatal")).Once()
kClient.On("UpdateRealmSettings", mock.Anything, mock.Anything).Return(nil)

err = rs.ServeRequest(ctx, &realm, kClient)
require.Error(t, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ var _ = Describe("KeycloakRealm controller", Ordered, func() {
ActionTokenGeneratedByUserLifespan: 234,
ActionTokenGeneratedByAdminLifespan: 235,
},
DisplayName: "Test Realm",
DisplayHTMLName: "<b>Test Realm</b>",
},
}
Expect(k8sClient.Create(ctx, keycloakRealm)).Should(Succeed())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ spec:
description: ClusterKeycloakRef is a name of the ClusterKeycloak instance
that owns the realm.
type: string
displayHtmlName:
description: DisplayHTMLName name to render in the UI.
type: string
displayName:
description: DisplayName is the display name of the realm.
type: string
frontendUrl:
description: |-
FrontendURL Set the frontend URL for the realm.
Expand Down
3 changes: 3 additions & 0 deletions deploy-templates/crds/v1.edp.epam.com_keycloakrealms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ spec:
displayHtmlName:
description: DisplayHTMLName name to render in the UI
type: string
displayName:
description: DisplayName is the display name of the realm.
type: string
frontendUrl:
description: FrontendURL Set the frontend URL for the realm. Use in
combination with the default hostname provider to override the base
Expand Down
21 changes: 21 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@ ClusterKeycloakRealmSpec defines the desired state of ClusterKeycloakRealm.
BrowserSecurityHeaders is a map of security headers to apply to HTTP responses from the realm's browser clients.<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>displayHtmlName</b></td>
<td>string</td>
<td>
DisplayHTMLName name to render in the UI.<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>displayName</b></td>
<td>string</td>
<td>
DisplayName is the display name of the realm.<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>frontendUrl</b></td>
<td>string</td>
Expand Down Expand Up @@ -3877,6 +3891,13 @@ KeycloakRealmSpec defines the desired state of KeycloakRealm.
DisplayHTMLName name to render in the UI<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>displayName</b></td>
<td>string</td>
<td>
DisplayName is the display name of the realm.<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>frontendUrl</b></td>
<td>string</td>
Expand Down
3 changes: 3 additions & 0 deletions pkg/client/keycloak/adapter/gocloak_adapter_realms.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type RealmSettings struct {
DisplayHTMLName string
FrontendURL string
TokenSettings *TokenSettings
DisplayName string
}

type PasswordPolicy struct {
Expand Down Expand Up @@ -110,6 +111,8 @@ func setRealmSettings(realm *gocloak.RealmRepresentation, realmSettings *RealmSe
(*realm.Attributes)["frontendUrl"] = realmSettings.FrontendURL
}

realm.DisplayName = gocloak.StringP(realmSettings.DisplayName)

if realmSettings.TokenSettings != nil {
realm.DefaultSignatureAlgorithm = gocloak.StringP(realmSettings.TokenSettings.DefaultSignatureAlgorithm)
realm.RevokeRefreshToken = gocloak.BoolP(realmSettings.TokenSettings.RevokeRefreshToken)
Expand Down

0 comments on commit 70fbb51

Please sign in to comment.