Skip to content
Snippets Groups Projects
Commit f0b1b483 authored by Martin's avatar Martin
Browse files

OZG-7922 remove unknown field from json

parent 52a3fc95
No related branches found
No related tags found
No related merge requests found
package de.ozgcloud.operator.keycloak;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
class CustomObjectMapper {
public static ObjectMapper createObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return mapper;
}
}
...@@ -31,6 +31,8 @@ import org.springframework.context.annotation.Bean; ...@@ -31,6 +31,8 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.fabric8.kubernetes.api.model.Secret; import io.fabric8.kubernetes.api.model.Secret;
import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.dsl.Resource; import io.fabric8.kubernetes.client.dsl.Resource;
...@@ -83,4 +85,9 @@ public class KeycloakClient { ...@@ -83,4 +85,9 @@ public class KeycloakClient {
private String decodeBase64(String base64String) { private String decodeBase64(String base64String) {
return new String(Base64.getDecoder().decode(base64String)); return new String(Base64.getDecoder().decode(base64String));
} }
@Bean
public ObjectMapper objectMapper() {
return CustomObjectMapper.createObjectMapper();
}
} }
...@@ -34,23 +34,27 @@ import org.springframework.stereotype.Component; ...@@ -34,23 +34,27 @@ import org.springframework.stereotype.Component;
import de.ozgcloud.operator.keycloak.KeycloakException; import de.ozgcloud.operator.keycloak.KeycloakException;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
@Log4j2
@RequiredArgsConstructor @RequiredArgsConstructor
@Component @Component
class KeycloakRealmRemoteService { class KeycloakRealmRemoteService {
private final Keycloak keycloak; private final Keycloak keycloak;
public void createRealm(RealmRepresentation realm) { public void createRealm(RealmRepresentation realmRepresentation) {
keycloak.realms().create(realm); removeUnknownFields(realmRepresentation);
keycloak.realms().create(realmRepresentation);
} }
public void deleteRealm(String realmName) { public void deleteRealm(String realmName) {
keycloak.realm(realmName).remove(); keycloak.realm(realmName).remove();
} }
public void updateRealm(RealmRepresentation realm) { public void updateRealm(RealmRepresentation realmRepresentation) {
keycloak.realm(realm.getRealm()).update(realm); removeUnknownFields(realmRepresentation);
keycloak.realm(realmRepresentation.getRealm()).update(realmRepresentation);
} }
public Optional<RoleRepresentation> getRealmRole(String roleName, String realmName) { public Optional<RoleRepresentation> getRealmRole(String roleName, String realmName) {
...@@ -71,6 +75,7 @@ class KeycloakRealmRemoteService { ...@@ -71,6 +75,7 @@ class KeycloakRealmRemoteService {
} }
UPConfig getUserProfileConfig(RealmRepresentation realmRepresentation) { UPConfig getUserProfileConfig(RealmRepresentation realmRepresentation) {
removeUnknownFields(realmRepresentation);
return keycloak.realm(realmRepresentation.getRealm()) return keycloak.realm(realmRepresentation.getRealm())
.users() .users()
.userProfile() .userProfile()
...@@ -78,9 +83,20 @@ class KeycloakRealmRemoteService { ...@@ -78,9 +83,20 @@ class KeycloakRealmRemoteService {
} }
void updateUserProfileConfig(RealmRepresentation realmRepresentation, UPConfig userProfileConfig) { void updateUserProfileConfig(RealmRepresentation realmRepresentation, UPConfig userProfileConfig) {
removeUnknownFields(realmRepresentation);
keycloak.realm(realmRepresentation.getRealm()) keycloak.realm(realmRepresentation.getRealm())
.users() .users()
.userProfile() .userProfile()
.update(userProfileConfig); .update(userProfileConfig);
} }
private void removeUnknownFields(RealmRepresentation realmRepresentation) {
try {
var field = RealmRepresentation.class.getDeclaredField("organizationsEnabled");
field.setAccessible(true);
field.set(realmRepresentation, null);
} catch (Exception e) {
LOG.error("Error on removing unknown fields", e);
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment