Search in sources :

Example 6 with RangerPluginContext

use of org.apache.ranger.plugin.policyengine.RangerPluginContext in project ranger by apache.

the class RangerPolicyAdminCache method addPolicyAdmin.

private RangerPolicyAdmin addPolicyAdmin(ServicePolicies policies, RangerRoles roles, RangerPolicyEngineOptions options) {
    RangerServiceDef serviceDef = policies.getServiceDef();
    String serviceType = (serviceDef != null) ? serviceDef.getName() : "";
    RangerPluginContext rangerPluginContext = new RangerPluginContext(new RangerPluginConfig(serviceType, null, "ranger-admin", null, null, options));
    return new RangerPolicyAdminImpl(policies, rangerPluginContext, roles);
}
Also used : RangerPluginConfig(org.apache.ranger.authorization.hadoop.config.RangerPluginConfig) RangerPluginContext(org.apache.ranger.plugin.policyengine.RangerPluginContext) RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef)

Example 7 with RangerPluginContext

use of org.apache.ranger.plugin.policyengine.RangerPluginContext in project ranger by apache.

the class TestPolicyEngine method runTests.

private void runTests(InputStreamReader reader, String testName) {
    try {
        PolicyEngineTestCase testCase = gsonBuilder.fromJson(reader, PolicyEngineTestCase.class);
        assertTrue("invalid input: " + testName, testCase != null && testCase.serviceDef != null && testCase.policies != null && testCase.tests != null);
        ServicePolicies servicePolicies = new ServicePolicies();
        servicePolicies.setServiceName(testCase.serviceName);
        servicePolicies.setServiceDef(testCase.serviceDef);
        servicePolicies.setPolicies(testCase.policies);
        RangerPolicyEngineOptions policyEngineOptions = new RangerPolicyEngineOptions();
        RangerPluginContext pluginContext = new RangerPluginContext(new RangerPluginConfig("hbase", null, testName, "cl1", "on-prem", policyEngineOptions));
        RangerPolicyEngine policyEngine = new RangerPolicyEngineImpl(servicePolicies, pluginContext, null);
        RangerAccessResultProcessor auditHandler = new RangerDefaultAuditHandler(pluginContext.getConfig());
        for (TestData test : testCase.tests) {
            RangerAccessResult expected = test.result;
            RangerAccessRequest request = test.request;
            RangerAccessResult result = policyEngine.evaluatePolicies(request, RangerPolicy.POLICY_TYPE_ACCESS, auditHandler);
            assertNotNull("result was null! - " + test.name, result);
            assertEquals("isAllowed mismatched! - " + test.name, expected.getIsAllowed(), result.getIsAllowed());
            assertEquals("isAudited mismatched! - " + test.name, expected.getIsAudited(), result.getIsAudited());
            assertEquals("policyId mismatched! - " + test.name, expected.getPolicyId(), result.getPolicyId());
        }
    } catch (Throwable excp) {
        excp.printStackTrace();
    }
}
Also used : RangerPolicyEngineImpl(org.apache.ranger.plugin.policyengine.RangerPolicyEngineImpl) RangerPluginConfig(org.apache.ranger.authorization.hadoop.config.RangerPluginConfig) ServicePolicies(org.apache.ranger.plugin.util.ServicePolicies) TestData(org.apache.ranger.authorization.hbase.TestPolicyEngine.PolicyEngineTestCase.TestData) RangerAccessResult(org.apache.ranger.plugin.policyengine.RangerAccessResult) RangerPolicyEngine(org.apache.ranger.plugin.policyengine.RangerPolicyEngine) RangerAccessResultProcessor(org.apache.ranger.plugin.policyengine.RangerAccessResultProcessor) RangerPluginContext(org.apache.ranger.plugin.policyengine.RangerPluginContext) RangerDefaultAuditHandler(org.apache.ranger.plugin.audit.RangerDefaultAuditHandler) RangerAccessRequest(org.apache.ranger.plugin.policyengine.RangerAccessRequest) RangerPolicyEngineOptions(org.apache.ranger.plugin.policyengine.RangerPolicyEngineOptions)

Example 8 with RangerPluginContext

use of org.apache.ranger.plugin.policyengine.RangerPluginContext in project ranger by apache.

the class TestPolicyDb method runTests.

private void runTests(InputStreamReader reader, String testName, RangerServiceDef serviceDef) {
    PolicyDbTestCase testCase = gsonBuilder.fromJson(reader, PolicyDbTestCase.class);
    if (serviceDef != null) {
        // Override serviceDef in the json test-file with a global service-def
        testCase.servicePolicies.setServiceDef(serviceDef);
    }
    assertTrue("invalid input: " + testName, testCase != null && testCase.servicePolicies != null && testCase.tests != null && testCase.servicePolicies.getPolicies() != null);
    RangerPolicyEngineOptions policyEngineOptions = new RangerPolicyEngineOptions();
    policyEngineOptions.evaluatorType = RangerPolicyEvaluator.EVALUATOR_TYPE_OPTIMIZED;
    policyEngineOptions.cacheAuditResults = false;
    policyEngineOptions.disableContextEnrichers = true;
    policyEngineOptions.disableCustomConditions = true;
    RangerPluginContext pluginContext = new RangerPluginContext(new RangerPluginConfig("hive", null, "test-policydb", "cl1", "on-prem", policyEngineOptions));
    RangerPolicyAdmin policyAdmin = new RangerPolicyAdminImpl(testCase.servicePolicies, pluginContext, null);
    for (TestData test : testCase.tests) {
        boolean expected = test.result;
        if (test.allowedPolicies != null) {
            List<RangerPolicy> allowedPolicies = policyAdmin.getAllowedUnzonedPolicies(test.user, test.userGroups, test.accessType);
            assertEquals("allowed-policy count mismatch!", test.allowedPolicies.size(), allowedPolicies.size());
            Set<Long> allowedPolicyIds = new HashSet<>();
            for (RangerPolicy allowedPolicy : allowedPolicies) {
                allowedPolicyIds.add(allowedPolicy.getId());
            }
            assertEquals("allowed-policy list mismatch!", test.allowedPolicies, allowedPolicyIds);
        } else {
            boolean result = policyAdmin.isAccessAllowedByUnzonedPolicies(test.resources, test.user, test.userGroups, test.accessType);
            assertEquals("isAccessAllowed mismatched! - " + test.name, expected, result);
        }
    }
}
Also used : RangerPluginConfig(org.apache.ranger.authorization.hadoop.config.RangerPluginConfig) TestData(org.apache.ranger.biz.TestPolicyDb.PolicyDbTestCase.TestData) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) RangerPluginContext(org.apache.ranger.plugin.policyengine.RangerPluginContext) RangerPolicyEngineOptions(org.apache.ranger.plugin.policyengine.RangerPolicyEngineOptions) HashSet(java.util.HashSet)

Example 9 with RangerPluginContext

use of org.apache.ranger.plugin.policyengine.RangerPluginContext in project ranger by apache.

the class RangerAbstractPolicyEvaluator method getPrunedPolicy.

private RangerPolicy getPrunedPolicy(final RangerPolicy policy) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> RangerAbstractPolicyEvaluator.getPrunedPolicy(" + policy + ")");
    }
    final RangerPolicy ret;
    final boolean isPruningNeeded;
    final List<RangerPolicy.RangerPolicyItem> prunedAllowItems;
    final List<RangerPolicy.RangerPolicyItem> prunedDenyItems;
    final List<RangerPolicy.RangerPolicyItem> prunedAllowExceptions;
    final List<RangerPolicy.RangerPolicyItem> prunedDenyExceptions;
    final RangerPluginContext pluginContext = getPluginContext();
    if (pluginContext != null && pluginContext.getConfig().getPolicyEngineOptions().evaluateDelegateAdminOnly) {
        prunedAllowItems = policy.getPolicyItems().stream().filter(RangerPolicy.RangerPolicyItem::getDelegateAdmin).collect(Collectors.toList());
        prunedDenyItems = policy.getDenyPolicyItems().stream().filter(RangerPolicy.RangerPolicyItem::getDelegateAdmin).collect(Collectors.toList());
        prunedAllowExceptions = policy.getAllowExceptions().stream().filter(RangerPolicy.RangerPolicyItem::getDelegateAdmin).collect(Collectors.toList());
        prunedDenyExceptions = policy.getDenyExceptions().stream().filter(RangerPolicy.RangerPolicyItem::getDelegateAdmin).collect(Collectors.toList());
        isPruningNeeded = prunedAllowItems.size() != policy.getPolicyItems().size() || prunedDenyItems.size() != policy.getDenyPolicyItems().size() || prunedAllowExceptions.size() != policy.getAllowExceptions().size() || prunedDenyExceptions.size() != policy.getDenyExceptions().size();
    } else {
        prunedAllowItems = null;
        prunedDenyItems = null;
        prunedAllowExceptions = null;
        prunedDenyExceptions = null;
        isPruningNeeded = false;
    }
    if (!isPruningNeeded) {
        ret = policy;
    } else {
        ret = new RangerPolicy();
        ret.updateFrom(policy);
        ret.setId(policy.getId());
        ret.setGuid(policy.getGuid());
        ret.setVersion(policy.getVersion());
        ret.setServiceType(policy.getServiceType());
        ret.setPolicyItems(prunedAllowItems);
        ret.setDenyPolicyItems(prunedDenyItems);
        ret.setAllowExceptions(prunedAllowExceptions);
        ret.setDenyExceptions(prunedDenyExceptions);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== RangerAbstractPolicyEvaluator.getPrunedPolicy(isPruningNeeded=" + isPruningNeeded + ") : " + ret);
    }
    return ret;
}
Also used : RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) RangerPluginContext(org.apache.ranger.plugin.policyengine.RangerPluginContext)

Example 10 with RangerPluginContext

use of org.apache.ranger.plugin.policyengine.RangerPluginContext in project ranger by apache.

the class RangerPolicyEnginePerformanceTest method policyEngineTest.

@Test
public void policyEngineTest() throws InterruptedException {
    List<RangerAccessRequest> requests = requestsCache.getUnchecked(concurrency);
    ServicePolicies servicePolicies = servicePoliciesCache.getUnchecked(numberOfPolicies);
    RangerPluginContext pluginContext = new RangerPluginContext(new RangerPluginConfig("hive", null, "perf-test", "cl1", "on-prem", RangerPolicyFactory.createPolicyEngineOption()));
    final RangerPolicyEngineImpl rangerPolicyEngine = new RangerPolicyEngineImpl(servicePolicies, pluginContext, null);
    for (int iterations = 0; iterations < WARM_UP__ITERATIONS; iterations++) {
        // using return value of 'isAccessAllowed' with a cheap operation: System#identityHashCode so JIT wont remove it as dead code
        System.identityHashCode(rangerPolicyEngine.evaluatePolicies(requests.get(iterations % concurrency), RangerPolicy.POLICY_TYPE_ACCESS, null));
        PerfDataRecorder.clearStatistics();
    }
    final CountDownLatch latch = new CountDownLatch(concurrency);
    for (int i = 0; i < concurrency; i++) {
        final RangerAccessRequest rangerAccessRequest = requests.get(i);
        new Thread(new Runnable() {

            @Override
            public void run() {
                System.identityHashCode(rangerPolicyEngine.evaluatePolicies(rangerAccessRequest, RangerPolicy.POLICY_TYPE_ACCESS, null));
                latch.countDown();
            }
        }, String.format("Client #%s", i)).start();
    }
    latch.await();
}
Also used : RangerPolicyEngineImpl(org.apache.ranger.plugin.policyengine.RangerPolicyEngineImpl) RangerPluginConfig(org.apache.ranger.authorization.hadoop.config.RangerPluginConfig) ServicePolicies(org.apache.ranger.plugin.util.ServicePolicies) RangerPluginContext(org.apache.ranger.plugin.policyengine.RangerPluginContext) CountDownLatch(java.util.concurrent.CountDownLatch) RangerAccessRequest(org.apache.ranger.plugin.policyengine.RangerAccessRequest) Test(org.junit.Test)

Aggregations

RangerPluginContext (org.apache.ranger.plugin.policyengine.RangerPluginContext)11 RangerPluginConfig (org.apache.ranger.authorization.hadoop.config.RangerPluginConfig)6 Configuration (org.apache.hadoop.conf.Configuration)3 RangerAdminClient (org.apache.ranger.admin.client.RangerAdminClient)2 RangerPolicy (org.apache.ranger.plugin.model.RangerPolicy)2 RangerAccessRequest (org.apache.ranger.plugin.policyengine.RangerAccessRequest)2 RangerPolicyEngineImpl (org.apache.ranger.plugin.policyengine.RangerPolicyEngineImpl)2 RangerPolicyEngineOptions (org.apache.ranger.plugin.policyengine.RangerPolicyEngineOptions)2 ServicePolicies (org.apache.ranger.plugin.util.ServicePolicies)2 HashSet (java.util.HashSet)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TestData (org.apache.ranger.authorization.hbase.TestPolicyEngine.PolicyEngineTestCase.TestData)1 TestData (org.apache.ranger.biz.TestPolicyDb.PolicyDbTestCase.TestData)1 RangerDefaultAuditHandler (org.apache.ranger.plugin.audit.RangerDefaultAuditHandler)1 RangerRole (org.apache.ranger.plugin.model.RangerRole)1 RangerServiceDef (org.apache.ranger.plugin.model.RangerServiceDef)1 RangerAccessResult (org.apache.ranger.plugin.policyengine.RangerAccessResult)1 RangerAccessResultProcessor (org.apache.ranger.plugin.policyengine.RangerAccessResultProcessor)1 RangerPolicyEngine (org.apache.ranger.plugin.policyengine.RangerPolicyEngine)1 Test (org.junit.Test)1