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

OZG-5400 add update realm function

parent 7cc800cd
Branches
Tags
No related merge requests found
......@@ -35,6 +35,11 @@ public class KeycloakGenericRemoteService {
.anyMatch(group -> Objects.equals(groupName, group.getName()));
}
public Optional<RealmRepresentation> getRealmRepresentation(String realmName) {
return Optional.of(keycloak.realm(realmName).toRepresentation());
}
public Optional<RoleRepresentation> getClientRole(String roleName, String realClientId, String realm) {
return Optional.ofNullable(keycloak.realm(realm).clients().get(realClientId))
.orElseThrow(() -> new KeycloakException("Client with ID " + realClientId + " for realm " + realm + " not found."))
......@@ -43,7 +48,5 @@ public class KeycloakGenericRemoteService {
.stream().filter(role -> Objects.equals(roleName, role.getName()))
.findFirst();
}
public Optional<RealmResource> getRealm(String realm) {
return Optional.ofNullable(keycloak.realm(realm)).findFirst();
}
}
......@@ -35,6 +35,7 @@ import org.keycloak.representations.idm.RoleRepresentation;
import org.keycloak.representations.idm.RolesRepresentation;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.Named;
import org.mapstruct.ReportingPolicy;
......@@ -56,6 +57,18 @@ interface KeycloakRealmMapper {
public RealmRepresentation map(OzgCloudKeycloakRealmSpec realm);
@Mapping(target = "displayName", source = "displayName")
@Mapping(target = "enabled", constant = "true")
@Mapping(target = "resetPasswordAllowed", constant = "true")
@Mapping(target = "supportedLocales", source = ".", qualifiedByName = "supportedLocales")
@Mapping(target = "defaultLocale", constant = "de")
@Mapping(target = "internationalizationEnabled", constant = "true")
@Mapping(target = "passwordPolicy", constant = "upperCase(1) and lowerCase(1) and length(8) and notUsername")
@Mapping(target = "actionTokenGeneratedByUserLifespan", constant = "900")
@Mapping(target = "smtpServer", source = "smtpServer", qualifiedByName = "smtpServer")
@Mapping(target = "roles.realm", source = "realmRoles")
RealmRepresentation update(@MappingTarget RealmRepresentation existingRealm, OzgCloudKeycloakRealmSpec spec);
@Mapping(target = "name", source = "name")
RoleRepresentation map(OzgCloudKeycloakRealmSpec.RealmRole role);
......
......@@ -51,7 +51,7 @@ public class KeycloakRealmReconciler implements Reconciler<OzgCloudKeycloakRealm
var realmName = resource.getMetadata().getNamespace();
service.createRealm(resource.getSpec(), realmName);
service.createOrUpdateClient(resource.getSpec(), resource.getMetadata().getNamespace());
service.createOrUpdateRealm(resource.getSpec(), resource.getMetadata().getNamespace());
resource.setStatus(OzgCloudKeycloakRealmStatus.builder().status(OzgCloudCustomResourceStatus.OK).message(null).build());
return UpdateControl.updateStatus(resource);
......
......@@ -43,6 +43,9 @@ class KeycloakRealmRemoteService {
public void deleteRealm(String realmName) {
keycloak.realm(realmName).remove();
}
public void updateRealm(RealmRepresentation realm) {
keycloak.realm(realm.getRealm()).update(realm);
}
}
......@@ -49,24 +49,17 @@ class KeycloakRealmService {
.ifPresent(remoteService::createRealm);
}
void createOrUpdateRealm(OzgCloudKeycloakRealmSpec spec, String realmName) {
keycloakGenericRemoteService.getRealm(realmName)
.ifPresentOrElse(existingRealm -> updateRealm(existingRealm, spec, namespace),
() -> createRealm(spec, namespace));
void createOrUpdateRealm(OzgCloudKeycloakRealmSpec realm, String realmName) {
keycloakGenericRemoteService.getRealmRepresentation(realmName)
.ifPresentOrElse(existingRealm -> updateRealm(existingRealm, realm),
() -> createRealm(realm, realmName));
}
void createOrUpdateClient(OzgCloudKeycloakClientSpec spec, String namespace) {
genericRemoteService.getByClientId(spec.getClientId(), namespace)
.ifPresentOrElse(existingClient -> updateClient(existingClient, spec, namespace),
() -> createClient(spec, namespace));
}
void updateClient(ClientRepresentation existingClient, OzgCloudKeycloakClientSpec spec, String realm) {
var clientRepresentation = mapper.update(existingClient, spec);
setProtocolMapper(clientRepresentation);
remoteService.updateClient(clientRepresentation, realm);
void updateRealm(RealmRepresentation existingRealm, OzgCloudKeycloakRealmSpec spec) {
var realmRepresentation = mapper.update(existingRealm, spec);
remoteService.updateRealm(realmRepresentation);
addOrUpdateClientRoles(spec, realm, existingClient.getId());
//addOrUpdateRealmRoles(spec, existingRealm.getRealm());
}
......@@ -75,9 +68,6 @@ class KeycloakRealmService {
return realm;
}
String getRealmName(RealmRepresentation realm) {
return realm.getRealm();
}
public void deleteRealm(String realmName) {
remoteService.deleteRealm(realmName);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment