Copilot commented on code in PR #671:
URL: https://github.com/apache/ranger/pull/671#discussion_r2353565978
##########
agents-common/src/main/java/org/apache/ranger/plugin/policyengine/RangerRequestScriptEvaluator.java:
##########
@@ -365,7 +354,7 @@ public String getResourceZone() {
public Set<String> getResourceZones() {
Set<String> ret =
RangerAccessRequestUtil.getResourceZoneNamesFromContext(getRequestContext());
- return ret != null ? Collections.emptySet() : ret;
+ return ret != null ? ret : Collections.emptySet();
Review Comment:
The null check logic is inverted. When ret is null, you should return
Collections.emptySet(), but currently you're returning ret (which is null) when
ret is not null.
##########
agents-common/src/main/java/org/apache/ranger/plugin/policyengine/RangerRequestScriptEvaluator.java:
##########
@@ -1160,124 +1030,75 @@ private Map<String, String> copyMap(Map<String,
String> obj) {
}
private List<Object> getUgAttr(String attrName) {
- List<Object> ret = new ArrayList<>();
-
- for (String groupName : userGroups) {
- Map<String, String> attrs = groupAttrs.get(groupName);
- Object val = attrs != null ? attrs.get(attrName) :
null;
-
- if (val != null) {
- ret.add(val);
- }
- }
-
- return ret;
+ return userGroups.stream()
+ .map(groupAttrs::get)
+ .filter(Objects::nonNull)
+ .map(attrs -> attrs.get(attrName))
+ .filter(Objects::nonNull)
+ .collect(Collectors.toList());
}
private List<Object> getTagAttr(String attrName) {
- List<Object> ret = new ArrayList<>();
-
- for (String tagName : tagNames) {
- Map<String, Object> attrs = tags.get(tagName);
- Object val = attrs != null ? attrs.get(attrName) :
null;
-
- if (val != null) {
- ret.add(val);
- }
- }
-
- return ret;
+ return tagNames.stream()
+ .map(tags::get)
+ .filter(Objects::nonNull)
+ .map(attrs -> attrs.get(attrName))
+ .filter(Objects::nonNull)
+ .collect(Collectors.toList());
}
private Collection<String> getUserAttrNames() {
Collection<String> ret = getSorted(userAttrs.keySet());
- if (ret.contains(SCRIPT_FIELD__NAME)) {
+ if (ret.contains(SCRIPT_FIELD__NAME)) { // this is needed to avoid
calling remove() on unmodifiable collection
ret.remove(SCRIPT_FIELD__NAME);
}
Review Comment:
The comment indicates this is to avoid calling remove() on an unmodifiable
collection, but the code still calls remove(). If getSorted() returns an
unmodifiable collection, this will throw an UnsupportedOperationException.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]