Search in sources :

Example 11 with RangerPolicyResource

use of org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource in project ranger by apache.

the class TestServiceREST method test30getPolicyFromEventTime.

@Test
public void test30getPolicyFromEventTime() throws Exception {
    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    String strdt = new Date().toString();
    String userName = "Admin";
    Set<String> userGroupsList = new HashSet<String>();
    userGroupsList.add("group1");
    userGroupsList.add("group2");
    Mockito.when(request.getParameter("eventTime")).thenReturn(strdt);
    Mockito.when(request.getParameter("policyId")).thenReturn("1");
    RangerPolicy policy = new RangerPolicy();
    Map<String, RangerPolicyResource> resources = new HashMap<String, RangerPolicy.RangerPolicyResource>();
    policy.setService("services");
    policy.setResources(resources);
    Mockito.when(svcStore.getPolicyFromEventTime(strdt, 1l)).thenReturn(policy);
    Mockito.when(bizUtil.isAdmin()).thenReturn(false);
    Mockito.when(bizUtil.getCurrentUserLoginId()).thenReturn(userName);
    Mockito.when(restErrorUtil.createRESTException(Matchers.anyInt(), Matchers.anyString(), Matchers.anyBoolean())).thenThrow(new WebApplicationException());
    thrown.expect(WebApplicationException.class);
    RangerPolicy dbRangerPolicy = serviceREST.getPolicyFromEventTime(request);
    Assert.assertNull(dbRangerPolicy);
    Mockito.verify(request).getParameter("eventTime");
    Mockito.verify(request).getParameter("policyId");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) WebApplicationException(javax.ws.rs.WebApplicationException) HashMap(java.util.HashMap) RangerPolicyResource(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource) VXString(org.apache.ranger.view.VXString) Date(java.util.Date) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 12 with RangerPolicyResource

use of org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource in project ranger by apache.

the class TestRangerPolicy method test_03_PolicyResource_SetListMethods.

@Test
public void test_03_PolicyResource_SetListMethods() {
    RangerPolicyResource policyResource = new RangerPolicyResource();
    List<String> values = getList("value");
    Assert.assertEquals("RangerPolicyResource.getValues()", 0, policyResource.getValues().size());
    policyResource.getValues().add(new String());
    Assert.assertEquals("RangerPolicyResource.getValues().add()", 1, policyResource.getValues().size());
    policyResource.setValues(values);
    Assert.assertEquals("RangerPolicyResource.setValues()", values.size(), policyResource.getValues().size());
}
Also used : RangerPolicyResource(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource) Test(org.junit.Test)

Example 13 with RangerPolicyResource

use of org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource in project ranger by apache.

the class TestRangerPolicyResourceSignature method test_getResourceSignature_happyPath.

@Test
public void test_getResourceSignature_happyPath() {
    // null policy returns signature of empty resource
    RangerPolicy policy = null;
    PolicySerializer serializer = new PolicySerializer(policy);
    Assert.assertTrue("Null policy", serializer.toString() == "");
    policy = mock(RangerPolicy.class);
    when(policy.getPolicyType()).thenReturn(null);
    Map<String, RangerPolicyResource> policyResources = _utils.createPolicyResourceMap(first);
    when(policy.getResources()).thenReturn(policyResources);
    serializer = new PolicySerializer(policy);
    String expectedVersion = "version=1";
    String expectedType = "type=0";
    String expectedResource = "{" + "col={values=[col1, col2, col3],excludes=false,recursive=true}, " + "db={values=[db1, db2],excludes=false,recursive=false}, " + "table={values=[tbl1, tbl2, tbl3],excludes=true,recursive=false}" + "}";
    String serializationFormat = "{%s,%s,resource=%s}";
    String expectedFull = String.format(serializationFormat, expectedVersion, expectedType, expectedResource);
    Assert.assertEquals(expectedFull, serializer.toString());
    // order of values should not matter
    policyResources = _utils.createPolicyResourceMap(data_second);
    when(policy.getResources()).thenReturn(policyResources);
    Assert.assertEquals(expectedFull, serializer.toString());
    // changing the policy type has expected changes
    when(policy.getPolicyType()).thenReturn(1);
    expectedType = "type=1";
    expectedFull = String.format(serializationFormat, expectedVersion, expectedType, expectedResource);
    Assert.assertEquals(expectedFull, serializer.toString());
}
Also used : RangerPolicyResource(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource) PolicySerializer(org.apache.ranger.plugin.model.RangerPolicyResourceSignature.PolicySerializer) Test(org.junit.Test)

Example 14 with RangerPolicyResource

use of org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource in project ranger by apache.

the class TestRangerPolicyResourceSignature method createPolicyResource.

RangerPolicyResource createPolicyResource(String[] values, Boolean recursive, Boolean excludes) {
    RangerPolicyResource resource = mock(RangerPolicyResource.class);
    if (values == null) {
        when(resource.getValues()).thenReturn(null);
    } else {
        when(resource.getValues()).thenReturn(Arrays.asList(values));
    }
    when(resource.getIsRecursive()).thenReturn(recursive);
    when(resource.getIsExcludes()).thenReturn(excludes);
    return resource;
}
Also used : RangerPolicyResource(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource)

Example 15 with RangerPolicyResource

use of org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource in project ranger by apache.

the class TestRangerPolicyResourceSignature method test_isPolicyValidForResourceSignatureComputation.

@Test
public void test_isPolicyValidForResourceSignatureComputation() {
    // null policy is invalid
    RangerPolicy rangerPolicy = null;
    PolicySerializer policySerializer = new PolicySerializer(rangerPolicy);
    Assert.assertFalse("policy==null", policySerializer.isPolicyValidForResourceSignatureComputation());
    // null resource map is invalid
    rangerPolicy = mock(RangerPolicy.class);
    when(rangerPolicy.getResources()).thenReturn(null);
    policySerializer = new PolicySerializer(rangerPolicy);
    Assert.assertFalse("policy.getResources()==null", policySerializer.isPolicyValidForResourceSignatureComputation());
    // empty resources map is ok!
    Map<String, RangerPolicyResource> policyResources = new HashMap<>();
    when(rangerPolicy.getResources()).thenReturn(policyResources);
    policySerializer = new PolicySerializer(rangerPolicy);
    Assert.assertTrue("policy.getResources().isEmpty()", policySerializer.isPolicyValidForResourceSignatureComputation());
    // but having a resource map with null key is not ok!
    RangerPolicyResource aPolicyResource = mock(RangerPolicyResource.class);
    policyResources.put(null, aPolicyResource);
    policySerializer = new PolicySerializer(rangerPolicy);
    Assert.assertFalse("policy.getResources().contains(null)", policySerializer.isPolicyValidForResourceSignatureComputation());
}
Also used : HashMap(java.util.HashMap) RangerPolicyResource(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource) PolicySerializer(org.apache.ranger.plugin.model.RangerPolicyResourceSignature.PolicySerializer) Test(org.junit.Test)

Aggregations

RangerPolicyResource (org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource)62 HashMap (java.util.HashMap)38 RangerPolicy (org.apache.ranger.plugin.model.RangerPolicy)36 RangerPolicyItem (org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItem)28 ArrayList (java.util.ArrayList)27 RangerPolicyItemAccess (org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemAccess)25 Test (org.junit.Test)23 VXString (org.apache.ranger.view.VXString)17 Date (java.util.Date)12 RangerPolicyItemCondition (org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemCondition)11 RangerResourceDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerResourceDef)11 RangerServiceDef (org.apache.ranger.plugin.model.RangerServiceDef)8 ServicePolicies (org.apache.ranger.plugin.util.ServicePolicies)8 XXServiceDef (org.apache.ranger.entity.XXServiceDef)7 RangerPerfTracer (org.apache.ranger.plugin.util.RangerPerfTracer)7 IOException (java.io.IOException)6 XXService (org.apache.ranger.entity.XXService)5 RangerService (org.apache.ranger.plugin.model.RangerService)5 RangerServiceResource (org.apache.ranger.plugin.model.RangerServiceResource)5 Map (java.util.Map)4