Search in sources :

Example 26 with ServicePolicies

use of org.apache.ranger.plugin.util.ServicePolicies in project ranger by apache.

the class RangerBasePlugin method getDefaultSvcPolicies.

private ServicePolicies getDefaultSvcPolicies() {
    ServicePolicies ret = null;
    RangerServiceDef serviceDef = getServiceDef();
    if (serviceDef == null) {
        serviceDef = getDefaultServiceDef();
    }
    if (serviceDef != null) {
        ret = new ServicePolicies();
        ret.setServiceDef(serviceDef);
        ret.setServiceName(serviceName);
        ret.setPolicies(new ArrayList<RangerPolicy>());
    }
    return ret;
}
Also used : RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) ServicePolicies(org.apache.ranger.plugin.util.ServicePolicies) RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef)

Example 27 with ServicePolicies

use of org.apache.ranger.plugin.util.ServicePolicies in project ranger by apache.

the class RangerPolicyEngineCache method getPolicyEngine.

final synchronized RangerPolicyEngine getPolicyEngine(String serviceName, ServiceStore svcStore, RangerPolicyEngineOptions options) {
    RangerPolicyEngine ret = null;
    if (serviceName != null) {
        ret = policyEngineCache.get(serviceName);
        long policyVersion = ret != null ? ret.getPolicyVersion() : -1;
        if (svcStore != null) {
            try {
                ServicePolicies policies = svcStore.getServicePoliciesIfUpdated(serviceName, policyVersion);
                if (policies != null) {
                    if (ret == null) {
                        ret = addPolicyEngine(policies, options);
                    } else if (policies.getPolicyVersion() != null && !policies.getPolicyVersion().equals(policyVersion)) {
                        ret = addPolicyEngine(policies, options);
                    }
                }
            } catch (Exception excp) {
                LOG.error("getPolicyEngine(" + serviceName + "): failed to get latest policies from service-store", excp);
            }
        }
    }
    return ret;
}
Also used : ServicePolicies(org.apache.ranger.plugin.util.ServicePolicies)

Example 28 with ServicePolicies

use of org.apache.ranger.plugin.util.ServicePolicies in project ranger by apache.

the class TestServiceDBStore method test33getServicePoliciesIfUpdated.

@Test
public void test33getServicePoliciesIfUpdated() throws Exception {
    XXServiceDao xServiceDao = Mockito.mock(XXServiceDao.class);
    XXServiceVersionInfoDao xServiceVersionInfoDao = Mockito.mock(XXServiceVersionInfoDao.class);
    XXService xService = new XXService();
    xService.setAddedByUserId(Id);
    xService.setCreateTime(new Date());
    xService.setDescription("Hdfs service");
    xService.setGuid("serviceguid");
    xService.setId(Id);
    xService.setIsEnabled(true);
    xService.setName("Hdfs");
    xService.setPolicyUpdateTime(new Date());
    xService.setPolicyVersion(1L);
    xService.setType(1L);
    xService.setUpdatedByUserId(Id);
    xService.setUpdateTime(new Date());
    XXServiceVersionInfo xServiceVersionInfo = new XXServiceVersionInfo();
    xServiceVersionInfo.setServiceId(Id);
    xServiceVersionInfo.setPolicyVersion(1L);
    xServiceVersionInfo.setPolicyUpdateTime(new Date());
    xServiceVersionInfo.setTagVersion(1L);
    xServiceVersionInfo.setTagUpdateTime(new Date());
    String serviceName = "HDFS_1";
    Long lastKnownVersion = 1l;
    Mockito.when(daoManager.getXXService()).thenReturn(xServiceDao);
    Mockito.when(daoManager.getXXServiceVersionInfo()).thenReturn(xServiceVersionInfoDao);
    Mockito.when(xServiceDao.findByName(serviceName)).thenReturn(xService);
    Mockito.when(xServiceVersionInfoDao.findByServiceName(serviceName)).thenReturn(xServiceVersionInfo);
    ServicePolicies dbServicePolicies = serviceDBStore.getServicePoliciesIfUpdated(serviceName, lastKnownVersion);
    Assert.assertNull(dbServicePolicies);
}
Also used : ServicePolicies(org.apache.ranger.plugin.util.ServicePolicies) VXString(org.apache.ranger.view.VXString) Date(java.util.Date) Test(org.junit.Test)

Example 29 with ServicePolicies

use of org.apache.ranger.plugin.util.ServicePolicies in project ranger by apache.

the class TestAssetREST method testGetResourceJSON.

@Test
public void testGetResourceJSON() {
    RangerService rangerService = rangerService(Id);
    String file = "testGetResourceJSON";
    VXAsset vXAsset = vXAsset(Id);
    Date date = new Date();
    String strdt = date.toString();
    X509Certificate[] certchain = new X509Certificate[1];
    certchain[0] = Mockito.mock(X509Certificate.class);
    ServicePolicies servicePolicies = servicePolicies();
    RangerPolicy rangerPolicy = rangerPolicy(Id);
    List<RangerPolicy> policies = new ArrayList<RangerPolicy>();
    policies.add(rangerPolicy);
    Mockito.when(request.getParameter("epoch")).thenReturn(strdt);
    Mockito.when(request.getAttribute("javax.servlet.request.X509Certificate")).thenReturn(certchain);
    Mockito.when(request.getHeader("X-FORWARDED-FOR")).thenReturn("valid");
    Mockito.when(request.isSecure()).thenReturn(true);
    Mockito.when(request.getParameter("policyCount")).thenReturn("4");
    Mockito.when(request.getParameter("agentId")).thenReturn("12");
    // Mockito.when(PropertiesUtil.getBooleanProperty("ranger.service.http.enabled",true)).thenReturn(true);
    try {
        Mockito.when(serviceREST.getServicePoliciesIfUpdated(Mockito.anyString(), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyString(), Mockito.anyString(), (HttpServletRequest) Mockito.any())).thenReturn(servicePolicies);
    } catch (Exception e) {
        fail("test failed due to: " + e.getMessage());
    }
    Mockito.when(serviceUtil.getServiceByName("hdfs_dev")).thenReturn(rangerService);
    Mockito.when(serviceUtil.toVXAsset(rangerService)).thenReturn(vXAsset);
    Mockito.when(assetMgr.getLatestRepoPolicy((VXAsset) Mockito.any(), Mockito.<VXResource>anyList(), Mockito.anyLong(), (X509Certificate[]) Mockito.any(), Mockito.anyBoolean(), Mockito.anyString(), Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), Mockito.anyString())).thenReturn(file);
    String actualFile = assetREST.getResourceJSON(request, "hdfs_dev");
    Assert.assertEquals(file, actualFile);
    Mockito.verify(serviceUtil).getServiceByName("hdfs_dev");
    Mockito.verify(serviceUtil).toVXAsset(rangerService);
    Mockito.verify(request).getParameter("epoch");
    Mockito.verify(request).getAttribute("javax.servlet.request.X509Certificate");
    Mockito.verify(request).getHeader("X-FORWARDED-FOR");
    Mockito.verify(request).isSecure();
    Mockito.verify(request).getParameter("policyCount");
    Mockito.verify(request).getParameter("agentId");
    Mockito.verify(assetMgr).getLatestRepoPolicy((VXAsset) Mockito.any(), Mockito.<VXResource>anyList(), Mockito.anyLong(), (X509Certificate[]) Mockito.any(), Mockito.anyBoolean(), Mockito.anyString(), Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), Mockito.anyString());
}
Also used : VXAsset(org.apache.ranger.view.VXAsset) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) ServicePolicies(org.apache.ranger.plugin.util.ServicePolicies) ArrayList(java.util.ArrayList) RangerService(org.apache.ranger.plugin.model.RangerService) Date(java.util.Date) X509Certificate(java.security.cert.X509Certificate) WebApplicationException(javax.ws.rs.WebApplicationException) Test(org.junit.Test)

Example 30 with ServicePolicies

use of org.apache.ranger.plugin.util.ServicePolicies in project ranger by apache.

the class TestServiceREST method test16createPolicyFalse.

@Test
public void test16createPolicyFalse() throws Exception {
    RangerPolicy rangerPolicy = rangerPolicy();
    RangerServiceDef rangerServiceDef = rangerServiceDef();
    List<RangerPolicy> policies = new ArrayList<RangerPolicy>();
    RangerPolicy rangPolicy = new RangerPolicy();
    policies.add(rangPolicy);
    String userName = "admin";
    Set<String> userGroupsList = new HashSet<String>();
    userGroupsList.add("group1");
    userGroupsList.add("group2");
    ServicePolicies servicePolicies = new ServicePolicies();
    servicePolicies.setServiceId(Id);
    servicePolicies.setServiceName("Hdfs_1");
    servicePolicies.setPolicyVersion(1L);
    servicePolicies.setPolicyUpdateTime(new Date());
    servicePolicies.setServiceDef(rangerServiceDef);
    servicePolicies.setPolicies(policies);
    List<RangerAccessTypeDef> rangerAccessTypeDefList = new ArrayList<RangerServiceDef.RangerAccessTypeDef>();
    RangerAccessTypeDef rangerAccessTypeDefObj = new RangerAccessTypeDef();
    rangerAccessTypeDefObj.setLabel("Read");
    rangerAccessTypeDefObj.setName("read");
    rangerAccessTypeDefObj.setRbKeyLabel(null);
    rangerAccessTypeDefList.add(rangerAccessTypeDefObj);
    XXServiceDef xServiceDef = serviceDef();
    XXService xService = xService();
    XXServiceDefDao xServiceDefDao = Mockito.mock(XXServiceDefDao.class);
    XXServiceDao xServiceDao = Mockito.mock(XXServiceDao.class);
    Mockito.when(validatorFactory.getPolicyValidator(svcStore)).thenReturn(policyValidator);
    Mockito.when(bizUtil.isAdmin()).thenReturn(true);
    Mockito.when(bizUtil.getCurrentUserLoginId()).thenReturn(userName);
    Mockito.when(daoManager.getXXService()).thenReturn(xServiceDao);
    Mockito.when(xServiceDao.findByName(Mockito.anyString())).thenReturn(xService);
    Mockito.when(daoManager.getXXServiceDef()).thenReturn(xServiceDefDao);
    Mockito.when(xServiceDefDao.getById(xService.getType())).thenReturn(xServiceDef);
    Mockito.when(svcStore.createPolicy((RangerPolicy) Mockito.any())).thenReturn(rangPolicy);
    RangerPolicy dbRangerPolicy = serviceREST.createPolicy(rangerPolicy, null);
    Assert.assertNotNull(dbRangerPolicy);
    Mockito.verify(bizUtil, Mockito.times(2)).isAdmin();
    Mockito.verify(validatorFactory).getPolicyValidator(svcStore);
    Mockito.verify(daoManager).getXXService();
    Mockito.verify(daoManager).getXXServiceDef();
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) ServicePolicies(org.apache.ranger.plugin.util.ServicePolicies) ArrayList(java.util.ArrayList) VXString(org.apache.ranger.view.VXString) XXServiceDao(org.apache.ranger.db.XXServiceDao) Date(java.util.Date) RangerAccessTypeDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerAccessTypeDef) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) XXServiceDefDao(org.apache.ranger.db.XXServiceDefDao) RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef) XXService(org.apache.ranger.entity.XXService) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ServicePolicies (org.apache.ranger.plugin.util.ServicePolicies)31 RangerPolicy (org.apache.ranger.plugin.model.RangerPolicy)16 ArrayList (java.util.ArrayList)13 RangerServiceDef (org.apache.ranger.plugin.model.RangerServiceDef)13 Test (org.junit.Test)13 HashMap (java.util.HashMap)10 RangerPolicyItem (org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItem)8 RangerPolicyItemAccess (org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemAccess)8 RangerPolicyResource (org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource)8 VXString (org.apache.ranger.view.VXString)7 IOException (java.io.IOException)5 Date (java.util.Date)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 UnknownHostException (java.net.UnknownHostException)3 GET (javax.ws.rs.GET)3 Path (javax.ws.rs.Path)3 AccessPolicy (org.apache.nifi.authorization.AccessPolicy)3 XXService (org.apache.ranger.entity.XXService)3 RangerService (org.apache.ranger.plugin.model.RangerService)3 RangerPolicyEngineImpl (org.apache.ranger.plugin.policyengine.RangerPolicyEngineImpl)3