Skip to content

Commit

Permalink
Normalize: Filter attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
sonOfRa committed Dec 2, 2022
1 parent f32816d commit 2f10390
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
| Remove clientScopeMappings | 2.5.0 | Remove existing clientScopeMappings while creating or updating realms |
| Synchronize user federation | 3.5.0 | Synchronize the user federation defined on the realm configuration |
| Synchronize user profile | 5.4.0 | Synchronize the user profile configuration defined on the realm configuration |
| Normalize realm exports | 5.5.0 | Normalize a full realm export to be more minimal |

# Specificities

Expand Down
6 changes: 6 additions & 0 deletions docs/NORMALIZE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Realm normalization



To run the normalization, run keycloak-config-cli with the CLI option `--run.operation=NORMALIZE`.
The default value for this option is `IMPORT`, which will run the regular keycloak-config-cli import.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public class RealmNormalizationService {
realmIgnoredProperties.add("applications");
realmIgnoredProperties.add("oauthClients");
realmIgnoredProperties.add("clientTemplates");
realmIgnoredProperties.add("attributes");

JAVERS = JaversBuilder.javers()
.registerEntity(new EntityDefinition(RealmRepresentation.class, "realm", realmIgnoredProperties))
Expand Down Expand Up @@ -175,13 +176,51 @@ public void normalize(RealmRepresentation exportedRealm) throws Exception {
minimizedRealm.setClientScopeMappings(clientScopeMappings);
}

var attributes = getMinimizedAttributes(exportedRealm, baselineRealm);
if (!attributes.isEmpty()) {
minimizedRealm.setAttributes(attributes);
}

var protocolMappers = getMinimizedProtocolMappers(exportedRealm.getProtocolMappers(),
baselineRealm.getProtocolMappers());
if (!protocolMappers.isEmpty()) {
minimizedRealm.setProtocolMappers(protocolMappers);
}

var outputFile = outputLocation.resolve(String.format("%s.yaml", exportedRealmRealm));

try (var os = new FileOutputStream(outputFile.toFile())) {
YAML_MAPPER.writeValue(os, minimizedRealm);
}
}

private Map<String, String> getMinimizedAttributes(RealmRepresentation exportedRealm, RealmRepresentation baselineRealm) {
var exportedAttributes = exportedRealm.getAttributesOrEmpty();
var baselineAttributes = baselineRealm.getAttributesOrEmpty();
var minimizedAttributes = new HashMap<String, String>();

for (var entry : baselineAttributes.entrySet()) {
var key = entry.getKey();
var exportedValue = exportedAttributes.get(key);
if (!Objects.equals(exportedValue, entry.getValue())) {
minimizedAttributes.put(key, exportedValue);
}
}

for (var entry : exportedAttributes.entrySet()) {
var key = entry.getKey();
if (!baselineAttributes.containsKey(key)) {
minimizedAttributes.put(key, entry.getValue());
}
}
return minimizedAttributes;
}

private List<ProtocolMapperRepresentation> getMinimizedProtocolMappers(List<ProtocolMapperRepresentation> exportedMappers,
List<ProtocolMapperRepresentation> baselineMappers) {
return List.of();
}

private List<ClientRepresentation> getMinimizedClients(RealmRepresentation exportedRealm, RealmRepresentation baselineRealm)
throws IOException, NoSuchFieldException, IllegalAccessException {
// Get a client map for better lookups
Expand Down

0 comments on commit 2f10390

Please sign in to comment.