Skip to content

Commit

Permalink
fixup! Use new constants
Browse files Browse the repository at this point in the history
  • Loading branch information
jfreden committed Oct 23, 2024
1 parent 1db4315 commit 07db034
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,28 +218,21 @@ public int minMappingVersion() {
return ADD_MANAGE_ROLES_PRIVILEGE.id();
}

// TODO REMOVE AND USE CONSTANT WHEN AVAILABLE!
public static final String METADATA_READ_ONLY_FLAG_KEY = "_read_only";
public static final String RESERVED_ROLE_MAPPING_SUFFIX = "(read only)";

// Visible for testing
protected static List<String> getDuplicateRoleMappingNames(ExpressionRoleMapping... roleMappings) {
// Partition role mappings on if they're cluster state role mappings (true) or native role mappings (false)
Map<Boolean, List<ExpressionRoleMapping>> partitionedRoleMappings = Arrays.stream(roleMappings)
.collect(
Collectors.partitioningBy(
roleMapping -> roleMapping.getMetadata().get(METADATA_READ_ONLY_FLAG_KEY) != null
&& (boolean) roleMapping.getMetadata().get(METADATA_READ_ONLY_FLAG_KEY)
roleMapping -> roleMapping.getMetadata().get(ExpressionRoleMapping.READ_ONLY_ROLE_MAPPING_METADATA_FLAG) != null
&& (boolean) roleMapping.getMetadata().get(ExpressionRoleMapping.READ_ONLY_ROLE_MAPPING_METADATA_FLAG)
)
);

Set<String> clusterStateRoleMappings = partitionedRoleMappings.get(true)
.stream()
.map(ExpressionRoleMapping::getName)
.map(name -> {
int lastIndex = name.lastIndexOf(RESERVED_ROLE_MAPPING_SUFFIX);
return lastIndex > 0 ? name.substring(0, lastIndex) : name;
})
.map(ExpressionRoleMapping::removeReadOnlySuffixIfPresent)
.collect(Collectors.toSet());

return partitionedRoleMappings.get(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ public void testGetDuplicateRoleMappingNames() {

private static ExpressionRoleMapping reservedRoleMapping(String name) {
return new ExpressionRoleMapping(
name + SecurityMigrations.CleanupRoleMappingDuplicatesMigration.RESERVED_ROLE_MAPPING_SUFFIX,
name + ExpressionRoleMapping.READ_ONLY_ROLE_MAPPING_SUFFIX,
null,
null,
null,
Map.of(SecurityMigrations.CleanupRoleMappingDuplicatesMigration.METADATA_READ_ONLY_FLAG_KEY, true),
Map.of(ExpressionRoleMapping.READ_ONLY_ROLE_MAPPING_METADATA_FLAG, true),
true
);
}
Expand Down

0 comments on commit 07db034

Please sign in to comment.