Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RANGER-4783: remove duplicates for users/groups/roles during policy validation #311

Merged
merged 2 commits into from
May 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,13 @@ private String getDuplicate(List<String> values) {
}
return duplicate;
}
private static void removeDuplicates(List<String> values){
if (values==null || values.isEmpty()){
return;
}
HashSet<String> uniqueElements = new HashSet<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider replacing #965 to #975 with the following:

Set<String> uniqueElements = new HashSet<>();

for (Iterator<String> iter = values.iterator(); iter.hasNext(); ) {
  if (!uniqueElements.add(iter.next())) {
    iter.remove();
  }
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider this version as well:

Set<String> uniqueElements = new HashSet<>();

values.removeIf(e -> !uniqueElements.add(e));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in latest patch

values.removeIf(e -> !uniqueElements.add(e));
}

boolean isValidPolicyItems(List<RangerPolicyItem> policyItems, List<ValidationFailureDetails> failures, RangerServiceDef serviceDef) {
if(LOG.isDebugEnabled()) {
Expand Down Expand Up @@ -1030,6 +1037,9 @@ boolean isValidPolicyItem(RangerPolicyItem policyItem, List<ValidationFailureDet
.build());
valid = false;
} else {
removeDuplicates(policyItem.getUsers());
removeDuplicates(policyItem.getGroups());
removeDuplicates(policyItem.getRoles());
if (CollectionUtils.isNotEmpty(policyItem.getUsers()) && CollectionUtils.containsAny(policyItem.getUsers(), invalidItems)) {
ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_NULL_POLICY_ITEM_USER;
failures.add(new ValidationFailureDetailsBuilder()
Expand Down