Search in sources :

Example 1 with TestData

use of org.apache.ranger.biz.TestPolicyDb.PolicyDbTestCase.TestData 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)

Aggregations

HashSet (java.util.HashSet)1 RangerPluginConfig (org.apache.ranger.authorization.hadoop.config.RangerPluginConfig)1 TestData (org.apache.ranger.biz.TestPolicyDb.PolicyDbTestCase.TestData)1 RangerPolicy (org.apache.ranger.plugin.model.RangerPolicy)1 RangerPluginContext (org.apache.ranger.plugin.policyengine.RangerPluginContext)1 RangerPolicyEngineOptions (org.apache.ranger.plugin.policyengine.RangerPolicyEngineOptions)1