Skip to content

Commit

Permalink
Cleanup ARP invalid keys
Browse files Browse the repository at this point in the history
  • Loading branch information
oharsta committed Jun 19, 2024
1 parent 323d5d8 commit e6954d7
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion manage-server/src/main/java/manage/mongo/MongoChangelog.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -146,7 +147,7 @@ public void moveResourceServers(MongockTemplate mongoTemplate, MetaDataAutoConfi
}
}

@ChangeSet(order = "008", id = "removeExtraneousKeys", author = "okke.harsta@surf.nl", runAlways = true)
@ChangeSet(order = "008", id = "removeExtraneousKeys", author = "okke.harsta@surf.nl")
public void removeExtraneousKeys(MongockTemplate mongoTemplate) {
List<MetaData> relyingParties = mongoTemplate.findAll(MetaData.class, EntityType.RP.getType());
List<String> extraneousKeysRelyingParties = Arrays.asList("scopes", "isResourceServer");
Expand Down Expand Up @@ -234,6 +235,37 @@ public void addPolicy(MongockTemplate mongoTemplate) {
mongoTemplate.indexOps(schema).ensureIndex(textIndexDefinition);
}

@ChangeSet(order = "014", id = "clearInvalidARPKeys", author = "okke.harsta@surf.nl")
public void clearInvalidARPKeys(MongockTemplate mongoTemplate) {
final List<String> allowedKeys = List.of("source", "value", "motivation", "release_as", "use_as_nameid");
List.of(EntityType.RP, EntityType.SP, EntityType.STT).forEach(entityType -> {
List<MetaData> metaDataList = mongoTemplate.findAll(MetaData.class, entityType.getType());
metaDataList.forEach(metaData -> {
Map<String, Object> arp = (Map<String, Object>) metaData.getData().get("arp");
if (!CollectionUtils.isEmpty(arp)) {
Map<String, List<Map<String, Object>>> attributes = (Map<String, List<Map<String, Object>>>) arp.get("attributes");
if (!CollectionUtils.isEmpty(attributes)) {
attributes.forEach((arpName, value) -> value.forEach(attribute -> {
boolean useAsNameId = (boolean) attribute.getOrDefault("use_as_nameid",
attribute.getOrDefault("use_as_name_id",
attribute.getOrDefault("useAsNameId", false)));
attribute.put("use_as_nameid", useAsNameId);
String releaseAt = (String) attribute.getOrDefault("releaseAs", attribute.get("release_as"));
if (StringUtils.hasText(releaseAt)) {
attribute.put("release_as", releaseAt);
}
if (attribute.keySet().removeIf(key -> !allowedKeys.contains(key))) {
//We don't make a new revision as this is a cleanup action
LOG.info(String.format("Saving %s metadata type %s where ARP values are fixed",
metaData.getData().get("entityid"), entityType));
mongoTemplate.save(metaData, entityType.getType());
}
}));
}
}
});
});
}

private void migrateRelayingPartyToResourceServer(Map<String, Map<String, Object>> properties, List<Pattern> patterns, Map<String, Object> simpleProperties, MetaData rs) {
rs.setType(EntityType.RS.getType());
Expand Down

0 comments on commit e6954d7

Please sign in to comment.