Search in sources :

Example 6 with RangerTagForEval

use of org.apache.ranger.plugin.contextenricher.RangerTagForEval in project ranger by apache.

the class RangerPolicyEngineImpl method evaluateTagPolicies.

private void evaluateTagPolicies(final RangerAccessRequest request, int policyType, RangerAccessResult result) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> RangerPolicyEngineImpl.evaluateTagPolicies(" + request + ", policyType =" + policyType + ", " + result + ")");
    }
    Date accessTime = request.getAccessTime();
    Set<RangerTagForEval> tags = RangerAccessRequestUtil.getRequestTagsFromContext(request.getContext());
    List<PolicyEvaluatorForTag> policyEvaluators = tagPolicyRepository == null ? null : tagPolicyRepository.getLikelyMatchPolicyEvaluators(tags, policyType, accessTime);
    if (CollectionUtils.isNotEmpty(policyEvaluators)) {
        for (PolicyEvaluatorForTag policyEvaluator : policyEvaluators) {
            RangerPolicyEvaluator evaluator = policyEvaluator.getEvaluator();
            RangerTagForEval tag = policyEvaluator.getTag();
            RangerAccessRequest tagEvalRequest = new RangerTagAccessRequest(tag, tagPolicyRepository.getServiceDef(), request);
            RangerAccessResult tagEvalResult = createAccessResult(tagEvalRequest, policyType);
            if (LOG.isDebugEnabled()) {
                LOG.debug("RangerPolicyEngineImpl.evaluateTagPolicies: Evaluating policies for tag (" + tag.getType() + ")");
            }
            tagEvalResult.setAccessResultFrom(result);
            tagEvalResult.setAuditResultFrom(result);
            result.incrementEvaluatedPoliciesCount();
            evaluator.evaluate(tagEvalRequest, tagEvalResult);
            if (tagEvalResult.getIsAllowed()) {
                if (!evaluator.hasDeny()) {
                    // No Deny policies left now
                    tagEvalResult.setIsAccessDetermined(true);
                }
            }
            if (tagEvalResult.getIsAudited()) {
                result.setAuditResultFrom(tagEvalResult);
            }
            if (!result.getIsAccessDetermined()) {
                if (tagEvalResult.getIsAccessDetermined()) {
                    result.setAccessResultFrom(tagEvalResult);
                } else {
                    if (!result.getIsAllowed() && tagEvalResult.getIsAllowed()) {
                        result.setAccessResultFrom(tagEvalResult);
                    }
                }
            }
            if (result.getIsAuditedDetermined() && result.getIsAccessDetermined()) {
                // Break out of policy-evaluation loop
                break;
            }
        }
    }
    if (result.getIsAllowed()) {
        result.setIsAccessDetermined(true);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== RangerPolicyEngineImpl.evaluateTagPolicies(" + request + ", policyType =" + policyType + ", " + result + ")");
    }
}
Also used : RangerTagForEval(org.apache.ranger.plugin.contextenricher.RangerTagForEval) RangerPolicyEvaluator(org.apache.ranger.plugin.policyevaluator.RangerPolicyEvaluator) Date(java.util.Date)

Example 7 with RangerTagForEval

use of org.apache.ranger.plugin.contextenricher.RangerTagForEval in project ranger by apache.

the class RangerPolicyEngineImpl method getMatchingPolicies.

/*
	* This API is used by ranger-admin
	*/
@Override
public List<RangerPolicy> getMatchingPolicies(RangerAccessResource resource) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> RangerPolicyEngineImpl.getMatchingPolicies(" + resource + ")");
    }
    List<RangerPolicy> ret = new ArrayList<>();
    RangerAccessRequestImpl request = new RangerAccessRequestImpl(resource, RangerPolicyEngine.ANY_ACCESS, null, null);
    preProcess(request);
    if (hasTagPolicies()) {
        Set<RangerTagForEval> tags = RangerAccessRequestUtil.getRequestTagsFromContext(request.getContext());
        if (CollectionUtils.isNotEmpty(tags)) {
            for (RangerTagForEval tag : tags) {
                RangerAccessRequest tagEvalRequest = new RangerTagAccessRequest(tag, tagPolicyRepository.getServiceDef(), request);
                RangerAccessResource tagResource = tagEvalRequest.getResource();
                List<RangerPolicyEvaluator> likelyEvaluators = tagPolicyRepository.getLikelyMatchPolicyEvaluators(tagResource);
                for (RangerPolicyEvaluator evaluator : likelyEvaluators) {
                    RangerPolicyResourceMatcher matcher = evaluator.getPolicyResourceMatcher();
                    if (matcher != null && matcher.isMatch(tagResource, RangerPolicyResourceMatcher.MatchScope.ANY, null)) {
                        ret.add(evaluator.getPolicy());
                    }
                }
            }
        }
    }
    if (hasResourcePolicies()) {
        List<RangerPolicyEvaluator> likelyEvaluators = policyRepository.getLikelyMatchPolicyEvaluators(resource);
        for (RangerPolicyEvaluator evaluator : likelyEvaluators) {
            RangerPolicyResourceMatcher matcher = evaluator.getPolicyResourceMatcher();
            if (matcher != null && matcher.isMatch(resource, RangerPolicyResourceMatcher.MatchScope.ANY, null)) {
                ret.add(evaluator.getPolicy());
            }
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== RangerPolicyEngineImpl.getMatchingPolicies(" + resource + ") : " + ret.size());
    }
    return ret;
}
Also used : RangerPolicyResourceMatcher(org.apache.ranger.plugin.policyresourcematcher.RangerPolicyResourceMatcher) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) RangerTagForEval(org.apache.ranger.plugin.contextenricher.RangerTagForEval) RangerPolicyEvaluator(org.apache.ranger.plugin.policyevaluator.RangerPolicyEvaluator) ArrayList(java.util.ArrayList)

Example 8 with RangerTagForEval

use of org.apache.ranger.plugin.contextenricher.RangerTagForEval in project ranger by apache.

the class RangerPolicyRepository method getLikelyMatchPolicyEvaluators.

List<PolicyEvaluatorForTag> getLikelyMatchPolicyEvaluators(Set<RangerTagForEval> tags, int policyType, Date accessTime) {
    List<PolicyEvaluatorForTag> ret = Collections.EMPTY_LIST;
    if (CollectionUtils.isNotEmpty(tags) && getServiceDef() != null) {
        ret = new ArrayList<PolicyEvaluatorForTag>();
        for (RangerTagForEval tag : tags) {
            if (tag.isApplicable(accessTime)) {
                RangerAccessResource resource = new RangerTagResource(tag.getType(), getServiceDef());
                List<RangerPolicyEvaluator> evaluators = getLikelyMatchPolicyEvaluators(resource, policyType);
                if (CollectionUtils.isNotEmpty(evaluators)) {
                    for (RangerPolicyEvaluator evaluator : evaluators) {
                        if (evaluator.isApplicable(accessTime)) {
                            ret.add(new PolicyEvaluatorForTag(evaluator, tag));
                        }
                    }
                }
            } else {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Tag:[" + tag.getType() + "] is not applicable at accessTime:[" + accessTime + "]");
                }
            }
        }
        if (CollectionUtils.isNotEmpty(ret)) {
            switch(policyType) {
                case RangerPolicy.POLICY_TYPE_ACCESS:
                    Collections.sort(ret, PolicyEvaluatorForTag.EVAL_ORDER_COMPARATOR);
                    break;
                case RangerPolicy.POLICY_TYPE_DATAMASK:
                    Collections.sort(ret, PolicyEvaluatorForTag.NAME_COMPARATOR);
                    break;
                case RangerPolicy.POLICY_TYPE_ROWFILTER:
                    Collections.sort(ret, PolicyEvaluatorForTag.NAME_COMPARATOR);
                    break;
                default:
                    LOG.warn("Unknown policy-type:[" + policyType + "]. Ignoring..");
                    break;
            }
        }
    }
    return ret;
}
Also used : RangerTagForEval(org.apache.ranger.plugin.contextenricher.RangerTagForEval) RangerPolicyEvaluator(org.apache.ranger.plugin.policyevaluator.RangerPolicyEvaluator)

Example 9 with RangerTagForEval

use of org.apache.ranger.plugin.contextenricher.RangerTagForEval in project ranger by apache.

the class RangerScriptConditionEvaluator method isMatched.

@Override
public boolean isMatched(RangerAccessRequest request) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> RangerScriptConditionEvaluator.isMatched()");
    }
    boolean result = true;
    if (scriptEngine != null) {
        String script = getScript();
        if (StringUtils.isNotBlank(script)) {
            RangerAccessRequest readOnlyRequest = request.getReadOnlyCopy();
            RangerScriptExecutionContext context = new RangerScriptExecutionContext(readOnlyRequest);
            RangerTagForEval currentTag = context.getCurrentTag();
            Map<String, String> tagAttribs = currentTag != null ? currentTag.getAttributes() : Collections.<String, String>emptyMap();
            Bindings bindings = scriptEngine.createBindings();
            bindings.put("ctx", context);
            bindings.put("tag", currentTag);
            bindings.put("tagAttr", tagAttribs);
            if (LOG.isDebugEnabled()) {
                LOG.debug("RangerScriptConditionEvaluator.isMatched(): script={" + script + "}");
            }
            try {
                Object ret = scriptEngine.eval(script, bindings);
                if (ret == null) {
                    ret = context.getResult();
                }
                if (ret instanceof Boolean) {
                    result = (Boolean) ret;
                }
            } catch (NullPointerException nullp) {
                LOG.error("RangerScriptConditionEvaluator.isMatched(): eval called with NULL argument(s)");
            } catch (ScriptException exception) {
                LOG.error("RangerScriptConditionEvaluator.isMatched(): failed to evaluate script," + " exception=" + exception);
            }
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== RangerScriptConditionEvaluator.isMatched(), result=" + result);
    }
    return result;
}
Also used : ScriptException(javax.script.ScriptException) RangerTagForEval(org.apache.ranger.plugin.contextenricher.RangerTagForEval) RangerAccessRequest(org.apache.ranger.plugin.policyengine.RangerAccessRequest) Bindings(javax.script.Bindings)

Example 10 with RangerTagForEval

use of org.apache.ranger.plugin.contextenricher.RangerTagForEval in project ranger by apache.

the class RangerScriptExecutionContext method getAllTagTypes.

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);
        }
    }
    return allTagTypes;
}
Also used : RangerTagForEval(org.apache.ranger.plugin.contextenricher.RangerTagForEval)

Aggregations

RangerTagForEval (org.apache.ranger.plugin.contextenricher.RangerTagForEval)11 RangerPolicyEvaluator (org.apache.ranger.plugin.policyevaluator.RangerPolicyEvaluator)4 JsonParseException (com.google.gson.JsonParseException)1 TypeToken (com.google.gson.reflect.TypeToken)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Set (java.util.Set)1 Bindings (javax.script.Bindings)1 ScriptException (javax.script.ScriptException)1 RangerDefaultAuditHandler (org.apache.ranger.plugin.audit.RangerDefaultAuditHandler)1 RangerPolicy (org.apache.ranger.plugin.model.RangerPolicy)1 RangerAccessRequest (org.apache.ranger.plugin.policyengine.RangerAccessRequest)1 TestData (org.apache.ranger.plugin.policyengine.TestPolicyEngine.PolicyEngineTestCase.TestData)1 RangerPolicyResourceMatcher (org.apache.ranger.plugin.policyresourcematcher.RangerPolicyResourceMatcher)1 RangerRequestedResources (org.apache.ranger.plugin.util.RangerRequestedResources)1 ServicePolicies (org.apache.ranger.plugin.util.ServicePolicies)1