Skip to content

Commit

Permalink
POC: support evaluting entity attributes with policyConditions
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilbonte21 committed Feb 29, 2024
1 parent 57e56f6 commit 2fa7ac3
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 8 deletions.
6 changes: 6 additions & 0 deletions auth-agents-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
</properties>

<dependencies>
<!-- <dependency>
<groupId>org.apache.ranger</groupId>
<artifactId>ranger-plugins-common</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>-->

<dependency>
<groupId>org.apache.atlas</groupId>
<artifactId>atlas-intg</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,14 @@ public boolean isMatched(RangerAccessRequest request) {
RangerScriptExecutionContext context = new RangerScriptExecutionContext(readOnlyRequest);
RangerTagForEval currentTag = context.getCurrentTag();
Map<String, String> tagAttribs = currentTag != null ? currentTag.getAttributes() : Collections.emptyMap();
Map<String, String> attributes = (Map<String, String>) readOnlyRequest.getContext().get("entityAttributes");

Bindings bindings = scriptEngine.createBindings();

bindings.put("ctx", context);
bindings.put("tag", currentTag);
bindings.put("tagAttr", tagAttribs);
bindings.put("attributes", attributes);

if (enableJsonCtx) {
bindings.put(SCRIPT_VAR_CONTEXT_JSON, context.toJson());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public List<RangerConditionEvaluator> getRangerPolicyConditionEvaluator(RangerPo

RangerPerfTracer perf = null;

long policyId = policy.getId();
String policyId = policy.getGuid();

if(RangerPerfTracer.isPerfTraceEnabled(PERF_POLICY_INIT_LOG)) {
perf = RangerPerfTracer.getPerfTracer(PERF_POLICY_INIT_LOG, "RangerCustomConditionEvaluator.init(policyId=" + policyId + ")");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1389,7 +1389,7 @@ private boolean matchPolicyCustomConditions(RangerAccessRequest request) {
conditionType = ((RangerAbstractConditionEvaluator)conditionEvaluator).getPolicyItemCondition().getType();
}

perf = RangerPerfTracer.getPerfTracer(PERF_POLICYCONDITION_REQUEST_LOG, "RangerConditionEvaluator.matchPolicyCustomConditions(policyId=" + getId() + ",policyConditionType=" + conditionType + ")");
perf = RangerPerfTracer.getPerfTracer(PERF_POLICYCONDITION_REQUEST_LOG, "RangerConditionEvaluator.matchPolicyCustomConditions(policyId=" + getGuid() + ",policyConditionType=" + conditionType + ")");
}

boolean conditionEvalResult = conditionEvaluator.isMatched(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.atlas.model.discovery.AtlasSearchResult;
import org.apache.atlas.model.discovery.IndexSearchParams;
import org.apache.atlas.model.instance.AtlasEntityHeader;
import org.apache.atlas.model.instance.AtlasStruct;
import org.apache.atlas.plugin.util.ServicePolicies;
import org.apache.atlas.plugin.model.RangerPolicy;
import org.apache.atlas.plugin.model.RangerPolicy.RangerDataMaskPolicyItem;
Expand Down Expand Up @@ -384,11 +385,12 @@ private List<RangerPolicyItemCondition> getPolicyConditions(AtlasEntityHeader at

List<HashMap<String, Object>> conditions = (List<HashMap<String, Object>>) atlasPolicy.getAttribute("policyConditions");

for (HashMap<String, Object> condition : conditions) {
for (Object condition : conditions) {
AtlasStruct toStruct = (AtlasStruct) condition;
RangerPolicyItemCondition rangerCondition = new RangerPolicyItemCondition();

rangerCondition.setType((String) condition.get("policyConditionType"));
rangerCondition.setValues((List<String>) condition.get("policyConditionValues"));
rangerCondition.setType((String) toStruct.getAttribute("policyConditionType"));
rangerCondition.setValues((List<String>) toStruct.getAttribute("policyConditionValues"));

ret.add(rangerCondition);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,5 +498,17 @@
],
"options": {
"enableDenyAndExceptionsInPolicies": "true"
}
},

"policyConditions":
[
{
"itemId":2,
"name":"expression",
"evaluator": "org.apache.atlas.plugin.conditionevaluator.RangerScriptConditionEvaluator",
"evaluatorOptions" : {"engineName":"JavaScript", "ui.isMultiline":"true"},
"label":"Enter boolean expression",
"description": "Boolean expression"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -686,9 +686,12 @@ private boolean isAccessAllowed(AtlasEntityAccessRequest request, RangerAtlasAud
rangerResource.setValue(RESOURCE_CLASSIFICATION, request.getClassificationTypeAndAllSuperTypes(classification));
}

Map<String, Object> contextOjb = rangerRequest.getContext();
contextOjb.put("entityAttributes", request.getEntity().getAttributes());

if (CollectionUtils.isNotEmpty(request.getEntityClassifications())) {
Set<AtlasClassification> entityClassifications = request.getEntityClassifications();
Map<String, Object> contextOjb = rangerRequest.getContext();


Set<RangerTagForEval> rangerTagForEval = getRangerServiceTag(entityClassifications);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1499,7 +1499,7 @@ private EntityMutationResponse createOrUpdate(EntityStream entityStream, boolean
if (skipAuthBaseConditions && (skipAuthMeaningsUpdate || skipAuthStarredDetailsUpdate)) {
//do nothing, only diff is relationshipAttributes.meanings or starred, allow update
} else {
AtlasAuthorizationUtils.verifyUpdateEntityAccess(typeRegistry, entityHeader,"update entity: type=" + entity.getTypeName());
AtlasAuthorizationUtils.verifyUpdateEntityAccess(typeRegistry, new AtlasEntityHeader(entityRetriever.toAtlasEntity(entityHeader.getGuid())), "update entity: type=" + entity.getTypeName());
}
}
}
Expand Down

0 comments on commit 2fa7ac3

Please sign in to comment.