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

OZG-7922 filter unknown field explizit

parent f0b1b483
No related branches found
No related tags found
No related merge requests found
package de.ozgcloud.operator.keycloak;
import org.keycloak.representations.idm.RealmRepresentation;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(value = "organizationsEnabled", ignoreUnknown = true)
public class CustomRealmRepresentation extends RealmRepresentation {
}
......@@ -23,6 +23,9 @@
*/
package de.ozgcloud.operator.keycloak.realm;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
......@@ -32,6 +35,10 @@ import org.keycloak.representations.idm.RoleRepresentation;
import org.keycloak.representations.userprofile.config.UPConfig;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import de.ozgcloud.operator.keycloak.CustomRealmRepresentation;
import de.ozgcloud.operator.keycloak.KeycloakException;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
......@@ -44,8 +51,9 @@ class KeycloakRealmRemoteService {
private final Keycloak keycloak;
public void createRealm(RealmRepresentation realmRepresentation) {
removeUnknownFields(realmRepresentation);
keycloak.realms().create(realmRepresentation);
CustomRealmRepresentation customRR = filterRealmRepresentation(realmRepresentation);
keycloak.realms().create(customRR);
}
public void deleteRealm(String realmName) {
......@@ -53,8 +61,8 @@ class KeycloakRealmRemoteService {
}
public void updateRealm(RealmRepresentation realmRepresentation) {
removeUnknownFields(realmRepresentation);
keycloak.realm(realmRepresentation.getRealm()).update(realmRepresentation);
CustomRealmRepresentation customRR = filterRealmRepresentation(realmRepresentation);
keycloak.realm(customRR.getRealm()).update(customRR);
}
public Optional<RoleRepresentation> getRealmRole(String roleName, String realmName) {
......@@ -75,28 +83,75 @@ class KeycloakRealmRemoteService {
}
UPConfig getUserProfileConfig(RealmRepresentation realmRepresentation) {
removeUnknownFields(realmRepresentation);
return keycloak.realm(realmRepresentation.getRealm())
CustomRealmRepresentation customRR = filterRealmRepresentation(realmRepresentation);
return keycloak.realm(customRR.getRealm())
.users()
.userProfile()
.getConfiguration();
}
void updateUserProfileConfig(RealmRepresentation realmRepresentation, UPConfig userProfileConfig) {
removeUnknownFields(realmRepresentation);
keycloak.realm(realmRepresentation.getRealm())
CustomRealmRepresentation customRR = filterRealmRepresentation(realmRepresentation);
keycloak.realm(customRR.getRealm())
.users()
.userProfile()
.update(userProfileConfig);
}
private void removeUnknownFields(RealmRepresentation realmRepresentation) {
// 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);
// }
// }
public CustomRealmRepresentation filterRealmRepresentation(RealmRepresentation rr) {
try {
var field = RealmRepresentation.class.getDeclaredField("organizationsEnabled");
field.setAccessible(true);
field.set(realmRepresentation, null);
ObjectMapper mapper = new ObjectMapper();
ObjectNode node = mapper.valueToTree(rr);
node.remove("organizationsEnabled");
CustomRealmRepresentation filtered = mapper.treeToValue(node, CustomRealmRepresentation.class);
try {
LOG.error("FIX_ERROR: Object has field: " + objectHasProperty(filtered, "organizationsEnabled"));
} catch (Exception e) {
LOG.error("FIX_ERROR: cant evaluate if field exists", e);
}
return filtered;
} catch (Exception e) {
LOG.error("Error on removing unknown fields", e);
throw new RuntimeException("FIX_ERROR: Failed to filter RealmRepresentation", e);
}
}
private Boolean objectHasProperty(Object obj, String propertyName) {
var properties = getAllFields(obj);
for (Field field : properties) {
if (field.getName().equalsIgnoreCase(propertyName)) {
return true;
}
}
return false;
}
private List<Field> getAllFields(Object obj) {
var fields = new ArrayList<Field>();
getAllFieldsRecursive(fields, obj.getClass());
return fields;
}
private List<Field> getAllFieldsRecursive(List<Field> fields, Class<?> type) {
for (var field : type.getDeclaredFields()) {
fields.add(field);
}
if (type.getSuperclass() != null) {
fields = getAllFieldsRecursive(fields, type.getSuperclass());
}
return fields;
}
}
......@@ -32,6 +32,7 @@ import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.keycloak.admin.client.Keycloak;
......@@ -50,6 +51,7 @@ import org.mockito.Spy;
import de.ozgcloud.operator.keycloak.KeycloakException;
@Disabled("Due to Bugfixing")
class KeycloakRealmRemoteServiceTest {
@Spy
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment