use of org.apache.ranger.plugin.policyengine.RangerPolicyEngine 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();
RangerPolicyEngine policyEngine = new RangerPolicyEngineImpl(testName, servicePolicies, policyEngineOptions);
RangerAccessResultProcessor auditHandler = new RangerDefaultAuditHandler();
for (TestData test : testCase.tests) {
RangerAccessResult expected = test.result;
RangerAccessRequest request = test.request;
policyEngine.preProcess(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();
}
}
use of org.apache.ranger.plugin.policyengine.RangerPolicyEngine in project ranger by apache.
the class RangerBasePlugin method setPolicies.
public void setPolicies(ServicePolicies policies) {
// guard against catastrophic failure during policy engine Initialization or
try {
RangerPolicyEngine oldPolicyEngine = this.policyEngine;
if (policies == null) {
policies = getDefaultSvcPolicies();
}
if (policies == null) {
this.policyEngine = null;
} else {
RangerPolicyEngine policyEngine = new RangerPolicyEngineImpl(appId, policies, policyEngineOptions);
policyEngine.setUseForwardedIPAddress(useForwardedIPAddress);
policyEngine.setTrustedProxyAddresses(trustedProxyAddresses);
this.policyEngine = policyEngine;
}
if (oldPolicyEngine != null && !oldPolicyEngine.preCleanup()) {
LOG.error("preCleanup() failed on the previous policy engine instance !!");
}
} catch (Exception e) {
LOG.error("setPolicies: policy engine initialization failed! Leaving current policy engine as-is. Exception : ", e);
}
}
Aggregations