mneethiraj commented on code in PR #671:
URL: https://github.com/apache/ranger/pull/671#discussion_r2357011434
##########
agents-common/src/main/java/org/apache/ranger/plugin/policyengine/RangerRequestScriptEvaluator.java:
##########
@@ -443,125 +432,59 @@ public String getCurrentTagType() {
}
public Set<String> getAllTagTypes() {
- Set<String> allTagTypes = null;
- Set<RangerTagForEval> tagObjectList = getAllTags();
-
- if (CollectionUtils.isNotEmpty(tagObjectList)) {
- for (RangerTagForEval tag : tagObjectList) {
- String tagType = tag.getType();
- if (allTagTypes == null) {
- allTagTypes = new HashSet<>();
- }
- allTagTypes.add(tagType);
- }
- }
+ Set<RangerTagForEval> tags = getAllTags();
- return allTagTypes;
+ return CollectionUtils.isEmpty(tags) ? null :
tags.stream().map(RangerTagForEval::getType).collect(Collectors.toSet());
}
public Map<String, String> getTagAttributes(final String tagType) {
- Map<String, String> ret = null;
-
- if (StringUtils.isNotBlank(tagType)) {
- Set<RangerTagForEval> tagObjectList = getAllTags();
-
- // Assumption: There is exactly one tag with given tagType in the
list of tags - may not be true ***TODO***
- // This will get attributes of the first tagType that matches
- if (CollectionUtils.isNotEmpty(tagObjectList)) {
- for (RangerTagForEval tag : tagObjectList) {
- if (tag.getType().equals(tagType)) {
- ret = tag.getAttributes();
- break;
- }
- }
- }
- }
+ Set<RangerTagForEval> tags = StringUtils.isBlank(tagType) ? null :
getAllTags();
- return ret;
+ // Assumption: There is exactly one tag with given tagType in the list
of tags - may not be true ***TODO**
+ // This will get attributes of the first tagType that matches
+ return CollectionUtils.isEmpty(tags) ? null :
+ tags.stream()
+ .filter(tag -> tagType.equals(tag.getType()))
+ .findFirst()
+ .map(RangerTagForEval::getAttributes)
+ .orElse(null);
}
public List<Map<String, String>> getTagAttributesForAllMatchingTags(final
String tagType) {
- List<Map<String, String>> ret = null;
-
- if (StringUtils.isNotBlank(tagType)) {
- Set<RangerTagForEval> tagObjectList = getAllTags();
-
- // Assumption: There is exactly one tag with given tagType in the
list of tags - may not be true ***TODO***
- // This will get attributes of the first tagType that matches
- if (CollectionUtils.isNotEmpty(tagObjectList)) {
- for (RangerTagForEval tag : tagObjectList) {
- if (tag.getType().equals(tagType)) {
- Map<String, String> tagAttributes =
tag.getAttributes();
- if (tagAttributes != null) {
- if (ret == null) {
- ret = new ArrayList<>();
- }
- ret.add(tagAttributes);
- }
- break;
- }
- }
- }
- }
+ Set<RangerTagForEval> tags = StringUtils.isBlank(tagType) ? null :
getAllTags();
- return ret;
+ return CollectionUtils.isEmpty(tags) ? null :
+ tags.stream()
+ .filter(tag -> tagType.equals(tag.getType()))
+ .map(RangerTagForEval::getAttributes)
Review Comment:
@vyommani - here are 2 methods to get value of tag attribute:
1. `getAttributeValue(tagType, attrName)`: returns value of the attribute
from the first tag instance
2. `getAttributeValueForAllMatchingTags(tagType, attrName)`: returns value
of an attribute from all instances of a given tagType
Currently `getAttributeValueForAllMatchingTags(tagType, attrName)` is not
used, hence the fact that it returns attribute value from the first instance
only went undetected. This update fixes that issue.
--
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]