Skip to content
Snippets Groups Projects
Commit 73469560 authored by OZGCloud's avatar OZGCloud
Browse files

Merge pull request 'OZG-5422 add realm role in keycloak operator' (#24) from OZG-5422 into master

parents 230e3f90 85e1ea98
Branches
Tags
No related merge requests found
...@@ -25,14 +25,21 @@ package de.ozgcloud.operator.keycloak.realm; ...@@ -25,14 +25,21 @@ package de.ozgcloud.operator.keycloak.realm;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.ArrayList;
import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.representations.idm.RoleRepresentation;
import org.keycloak.representations.idm.RolesRepresentation;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.Mapping; import org.mapstruct.Mapping;
import org.mapstruct.Named; import org.mapstruct.Named;
import org.mapstruct.ReportingPolicy; import org.mapstruct.ReportingPolicy;
import de.ozgcloud.operator.keycloak.realm.OzgCloudKeycloakRealmSpec.RealmRole;
@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE, unmappedSourcePolicy = ReportingPolicy.IGNORE) @Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE, unmappedSourcePolicy = ReportingPolicy.IGNORE)
interface KeycloakRealmMapper { interface KeycloakRealmMapper {
...@@ -45,8 +52,13 @@ interface KeycloakRealmMapper { ...@@ -45,8 +52,13 @@ interface KeycloakRealmMapper {
@Mapping(target = "passwordPolicy", constant = "upperCase(1) and lowerCase(1) and length(8) and notUsername") @Mapping(target = "passwordPolicy", constant = "upperCase(1) and lowerCase(1) and length(8) and notUsername")
@Mapping(target = "actionTokenGeneratedByUserLifespan", constant = "900") @Mapping(target = "actionTokenGeneratedByUserLifespan", constant = "900")
@Mapping(target = "smtpServer", source = "smtpServer", qualifiedByName = "smtpServer") @Mapping(target = "smtpServer", source = "smtpServer", qualifiedByName = "smtpServer")
@Mapping(target = "roles.realm", source = "realmRoles")
public RealmRepresentation map(OzgCloudKeycloakRealmSpec realm); public RealmRepresentation map(OzgCloudKeycloakRealmSpec realm);
@Mapping(target = "name", source = "name")
RoleRepresentation map(OzgCloudKeycloakRealmSpec.RealmRole role);
@Named("supportedLocales") @Named("supportedLocales")
default Set<String> mapPassword(OzgCloudKeycloakRealmSpec spec) { default Set<String> mapPassword(OzgCloudKeycloakRealmSpec spec) {
return Set.of("de"); return Set.of("de");
......
...@@ -23,6 +23,9 @@ ...@@ -23,6 +23,9 @@
*/ */
package de.ozgcloud.operator.keycloak.realm; package de.ozgcloud.operator.keycloak.realm;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
...@@ -48,9 +51,7 @@ class OzgCloudKeycloakRealmSpec { ...@@ -48,9 +51,7 @@ class OzgCloudKeycloakRealmSpec {
private KeycloakRealmSMTPServer smtpServer; private KeycloakRealmSMTPServer smtpServer;
@Getter @Getter
@Setter
@Builder @Builder
@NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
static class KeycloakRealmSMTPServer { static class KeycloakRealmSMTPServer {
...@@ -69,4 +70,16 @@ class OzgCloudKeycloakRealmSpec { ...@@ -69,4 +70,16 @@ class OzgCloudKeycloakRealmSpec {
private String fromDisplayName; private String fromDisplayName;
} }
@Builder.Default
private List<RealmRole> realmRoles = new ArrayList<>();
@Getter
@Builder
@AllArgsConstructor
static class RealmRole {
@JsonProperty("name")
private String name;
}
} }
...@@ -118,4 +118,20 @@ class KeycloakRealmMapperTest { ...@@ -118,4 +118,20 @@ class KeycloakRealmMapperTest {
assertThat(mapped.getSmtpServer()).isEmpty(); assertThat(mapped.getSmtpServer()).isEmpty();
} }
@Test
void shouldMapRealmRoles() {
var mapped = mapper.map(OzgCloudKeycloakRealmSpecTestFactory.create());
assertThat(mapped.getRoles().getRealm()).hasSize(2);
}
@Test
void shouldContainRealmRolesValues() {
var mapped = mapper.map(OzgCloudKeycloakRealmSpecTestFactory.create());
var mappedRealmRoles = mapped.getRoles();
assertThat(mappedRealmRoles.getRealm().get(0).getName()).isEqualTo(OzgCloudKeycloakRealmSpecTestFactory.ROLE_NAME_1);
assertThat(mappedRealmRoles.getRealm().get(1).getName()).isEqualTo(OzgCloudKeycloakRealmSpecTestFactory.ROLE_NAME_2);
}
} }
...@@ -72,6 +72,9 @@ class KeycloakRealmReconcilerTest { ...@@ -72,6 +72,9 @@ class KeycloakRealmReconcilerTest {
assertThat(response.getResource().getStatus().getStatus()).isEqualTo(OzgCloudCustomResourceStatus.OK); assertThat(response.getResource().getStatus().getStatus()).isEqualTo(OzgCloudCustomResourceStatus.OK);
} }
} }
@DisplayName("Reconciler Cleanup") @DisplayName("Reconciler Cleanup")
...@@ -154,6 +157,7 @@ class KeycloakRealmReconcilerTest { ...@@ -154,6 +157,7 @@ class KeycloakRealmReconcilerTest {
assertThat(control).usingRecursiveComparison() assertThat(control).usingRecursiveComparison()
.isEqualTo(DeleteControl.noFinalizerRemoval().rescheduleAfter(Config.RECONCILER_RETRY_SECONDS_ON_ERROR)); .isEqualTo(DeleteControl.noFinalizerRemoval().rescheduleAfter(Config.RECONCILER_RETRY_SECONDS_ON_ERROR));
} }
} }
} }
} }
...@@ -23,11 +23,21 @@ ...@@ -23,11 +23,21 @@
*/ */
package de.ozgcloud.operator.keycloak.realm; package de.ozgcloud.operator.keycloak.realm;
import java.util.List;
import de.ozgcloud.operator.keycloak.realm.OzgCloudKeycloakRealmSpec.RealmRole;
public class OzgCloudKeycloakRealmSpecTestFactory { public class OzgCloudKeycloakRealmSpecTestFactory {
public final static String DISPLAY_NAME = "TestDisplayName"; public final static String DISPLAY_NAME = "TestDisplayName";
public final static boolean KEEP_AFTER_DELETE = false; public final static boolean KEEP_AFTER_DELETE = false;
public static final String ROLE_NAME_1 = "RoleName1";
public static final RealmRole ROLE1 = RealmRole.builder().name(ROLE_NAME_1).build();
public static final String ROLE_NAME_2 = "RoleName2";
public static final RealmRole ROLE2 = RealmRole.builder().name(ROLE_NAME_2).build();
public static final List<RealmRole> ROLES = List.of(ROLE1, ROLE2);
public static OzgCloudKeycloakRealmSpec create() { public static OzgCloudKeycloakRealmSpec create() {
return createBuilder().build(); return createBuilder().build();
} }
...@@ -36,6 +46,7 @@ public class OzgCloudKeycloakRealmSpecTestFactory { ...@@ -36,6 +46,7 @@ public class OzgCloudKeycloakRealmSpecTestFactory {
return OzgCloudKeycloakRealmSpec.builder() return OzgCloudKeycloakRealmSpec.builder()
.keepAfterDelete(KEEP_AFTER_DELETE) .keepAfterDelete(KEEP_AFTER_DELETE)
.displayName(DISPLAY_NAME) .displayName(DISPLAY_NAME)
.smtpServer(KeycloakRealmSmtpServerTestFactory.create()); .smtpServer(KeycloakRealmSmtpServerTestFactory.create())
.realmRoles(ROLES);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment