Search in sources :

Example 1 with XXServiceDao

use of org.apache.ranger.db.XXServiceDao in project ranger by apache.

the class PatchForAtlasServiceDefUpdate_J10013 method updateAtlasServiceDef.

private void updateAtlasServiceDef() throws Exception {
    String serviceDefName = EmbeddedServiceDefsUtil.EMBEDDED_SERVICEDEF_ATLAS_NAME;
    XXServiceDefDao serviceDefDao = daoMgr.getXXServiceDef();
    XXServiceDef serviceDef = serviceDefDao.findByName(serviceDefName);
    // if service-def named 'atlas' does not exist then no need to process this patch further.
    if (serviceDef == null) {
        LOG.info(serviceDefName + ": service-def not found. No patching is needed");
        return;
    }
    // if older atlas service-def doesn't exist then no need to process this patch further.
    if (!checkIfHasOlderServiceDef(serviceDef)) {
        LOG.info("Older version of " + serviceDefName + " service-def not found. No patching is needed");
        return;
    }
    String suffix = null;
    for (int i = 1; true; i++) {
        suffix = ".v" + i;
        if (serviceDefDao.findByName(serviceDefName + suffix) == null) {
            break;
        }
    }
    String serviceDefNewName = serviceDefName + suffix;
    LOG.info("Renaming service-def " + serviceDefName + " as " + serviceDefNewName);
    RangerServiceDef dbAtlasServiceDef = svcDBStore.getServiceDefByName(EmbeddedServiceDefsUtil.EMBEDDED_SERVICEDEF_ATLAS_NAME);
    if (EmbeddedServiceDefsUtil.instance().getTagServiceDefId() != -1) {
        RangerServiceDef dbTagServiceDef;
        try {
            dbTagServiceDef = svcDBStore.getServiceDef(EmbeddedServiceDefsUtil.instance().getTagServiceDefId());
            if (dbTagServiceDef != null) {
                String prefix = serviceDefName + AbstractServiceStore.COMPONENT_ACCESSTYPE_SEPARATOR;
                String newPrefix = serviceDefNewName + AbstractServiceStore.COMPONENT_ACCESSTYPE_SEPARATOR;
                List<RangerServiceDef.RangerAccessTypeDef> svcDefAccessTypes = dbAtlasServiceDef.getAccessTypes();
                List<RangerServiceDef.RangerAccessTypeDef> tagDefAccessTypes = dbTagServiceDef.getAccessTypes();
                long itemIdOffset = serviceDef.getId() * (MAX_ACCESS_TYPES_IN_SERVICE_DEF + 1);
                boolean updateNeeded = updateTagAccessTypeDefs(svcDefAccessTypes, tagDefAccessTypes, itemIdOffset, prefix, newPrefix);
                if (updateNeeded) {
                    svcDBStore.updateServiceDef(dbTagServiceDef);
                }
            }
        } catch (Exception e) {
            LOG.error("updateAtlasServiceDef:" + serviceDef.getName() + "): could not find TAG ServiceDef.. ", e);
        }
    }
    serviceDef.setName(serviceDefNewName);
    serviceDef.setDisplayName(serviceDefNewName);
    serviceDefDao.update(serviceDef);
    LOG.info("Renamed service-def " + serviceDefName + " as " + serviceDefNewName);
    XXServiceDao serviceDao = daoMgr.getXXService();
    List<XXService> services = serviceDao.findByServiceDefId(serviceDef.getId());
    if (CollectionUtils.isNotEmpty(services)) {
        for (XXService service : services) {
            String serviceName = service.getName();
            String serviceNewName = serviceName + suffix;
            LOG.info("Renaming service " + serviceName + " as " + serviceNewName);
            if (serviceDao.findByName(serviceNewName) != null) {
                LOG.warn("Another service named " + serviceNewName + " already exists. Not renaming " + serviceName);
                continue;
            }
            service.setName(serviceNewName);
            service.setDisplayName(serviceNewName);
            serviceDao.update(service);
            LOG.info("Renamed service " + serviceName + " as " + serviceNewName);
        }
    }
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) XXServiceDao(org.apache.ranger.db.XXServiceDao) XXServiceDefDao(org.apache.ranger.db.XXServiceDefDao) RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef) XXService(org.apache.ranger.entity.XXService)

Example 2 with XXServiceDao

use of org.apache.ranger.db.XXServiceDao in project ranger by apache.

the class TestServiceREST method test46exportPoliciesInCSV.

@Test
public void test46exportPoliciesInCSV() throws Exception {
    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    List<RangerPolicy> rangerPolicyList = new ArrayList<RangerPolicy>();
    RangerPolicy rangerPolicy = rangerPolicy();
    rangerPolicyList.add(rangerPolicy);
    XXService xService = xService();
    XXServiceDao xServiceDao = Mockito.mock(XXServiceDao.class);
    XXServiceDef xServiceDef = serviceDef();
    XXServiceDefDao xServiceDefDao = Mockito.mock(XXServiceDefDao.class);
    request.setAttribute("serviceType", "hdfs,hbase,hive,yarn,knox,storm,solr,kafka,nifi,atlas,sqoop");
    HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
    SearchFilter filter = new SearchFilter();
    Mockito.when(searchUtil.getSearchFilter(request, policyService.sortFields)).thenReturn(filter);
    Mockito.when(svcStore.getPolicies(filter)).thenReturn(rangerPolicyList);
    Mockito.when(bizUtil.isAdmin()).thenReturn(true);
    Mockito.when(bizUtil.isKeyAdmin()).thenReturn(false);
    Mockito.when(bizUtil.getCurrentUserLoginId()).thenReturn("admin");
    Mockito.when(bizUtil.isAuditAdmin()).thenReturn(false);
    Mockito.when(bizUtil.isAuditKeyAdmin()).thenReturn(false);
    Mockito.when(daoManager.getXXService()).thenReturn(xServiceDao);
    Mockito.when(daoManager.getXXServiceDef()).thenReturn(xServiceDefDao);
    Mockito.when(daoManager.getXXService().findByName("HDFS_1-1-20150316062453")).thenReturn(xService);
    Mockito.when(daoManager.getXXServiceDef().getById(xService.getType())).thenReturn(xServiceDef);
    serviceREST.getPoliciesInCsv(request, response);
    Mockito.verify(svcStore).getPoliciesInCSV(rangerPolicyList, response);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) XXServiceDef(org.apache.ranger.entity.XXServiceDef) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) XXServiceDefDao(org.apache.ranger.db.XXServiceDefDao) ArrayList(java.util.ArrayList) HttpServletResponse(javax.servlet.http.HttpServletResponse) SearchFilter(org.apache.ranger.plugin.util.SearchFilter) XXService(org.apache.ranger.entity.XXService) XXServiceDao(org.apache.ranger.db.XXServiceDao) Test(org.junit.Test)

Example 3 with XXServiceDao

use of org.apache.ranger.db.XXServiceDao in project ranger by apache.

the class TestServiceREST method test45exportPoliciesInJSON.

@Test
public void test45exportPoliciesInJSON() throws Exception {
    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    List<RangerPolicy> rangerPolicyList = new ArrayList<RangerPolicy>();
    RangerPolicy rangerPolicy = rangerPolicy();
    rangerPolicyList.add(rangerPolicy);
    XXService xService = xService();
    XXServiceDao xServiceDao = Mockito.mock(XXServiceDao.class);
    XXServiceDef xServiceDef = serviceDef();
    XXServiceDefDao xServiceDefDao = Mockito.mock(XXServiceDefDao.class);
    request.setAttribute("serviceType", "hdfs,hbase,hive,yarn,knox,storm,solr,kafka,nifi,atlas,sqoop");
    HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
    SearchFilter filter = new SearchFilter();
    filter.setParam("zoneName", "zone1");
    Mockito.when(searchUtil.getSearchFilter(request, policyService.sortFields)).thenReturn(filter);
    Mockito.when(svcStore.getPolicies(filter)).thenReturn(rangerPolicyList);
    Mockito.when(bizUtil.isAdmin()).thenReturn(true);
    Mockito.when(bizUtil.isKeyAdmin()).thenReturn(false);
    Mockito.when(bizUtil.getCurrentUserLoginId()).thenReturn("admin");
    Mockito.when(bizUtil.isAuditAdmin()).thenReturn(false);
    Mockito.when(bizUtil.isAuditKeyAdmin()).thenReturn(false);
    Mockito.when(daoManager.getXXService()).thenReturn(xServiceDao);
    Mockito.when(daoManager.getXXServiceDef()).thenReturn(xServiceDefDao);
    Mockito.when(daoManager.getXXService().findByName("HDFS_1-1-20150316062453")).thenReturn(xService);
    Mockito.when(daoManager.getXXServiceDef().getById(xService.getType())).thenReturn(xServiceDef);
    serviceREST.getPoliciesInJson(request, response, false);
    Mockito.verify(svcStore).getPoliciesInJson(rangerPolicyList, response);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) XXServiceDef(org.apache.ranger.entity.XXServiceDef) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) XXServiceDefDao(org.apache.ranger.db.XXServiceDefDao) ArrayList(java.util.ArrayList) HttpServletResponse(javax.servlet.http.HttpServletResponse) SearchFilter(org.apache.ranger.plugin.util.SearchFilter) XXService(org.apache.ranger.entity.XXService) XXServiceDao(org.apache.ranger.db.XXServiceDao) Test(org.junit.Test)

Example 4 with XXServiceDao

use of org.apache.ranger.db.XXServiceDao in project ranger by apache.

the class TestServiceREST method test17updatePolicyFalse.

@Test
public void test17updatePolicyFalse() throws Exception {
    RangerPolicy rangerPolicy = rangerPolicy();
    String userName = "admin";
    Set<String> userGroupsList = new HashSet<String>();
    userGroupsList.add("group1");
    userGroupsList.add("group2");
    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);
    RangerPolicy dbRangerPolicy = serviceREST.updatePolicy(rangerPolicy);
    Assert.assertNull(dbRangerPolicy);
    Mockito.verify(validatorFactory).getPolicyValidator(svcStore);
}
Also used : RangerAccessTypeDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerAccessTypeDef) XXServiceDef(org.apache.ranger.entity.XXServiceDef) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) XXServiceDefDao(org.apache.ranger.db.XXServiceDefDao) RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef) ArrayList(java.util.ArrayList) VXString(org.apache.ranger.view.VXString) XXService(org.apache.ranger.entity.XXService) XXServiceDao(org.apache.ranger.db.XXServiceDao) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with XXServiceDao

use of org.apache.ranger.db.XXServiceDao in project ranger by apache.

the class TestServiceREST method test48exportPoliciesInExcel.

/*  @Test
        public void test47WhenPolicyListIsEmpty() throws Exception {
                HttpServletRequest request = Mockito.mock(HttpServletRequest.class);

                List<RangerPolicy> rangerPolicyList = new ArrayList<RangerPolicy>();
                request.setAttribute("serviceType", "hdfs,hbase,hive,yarn,knox,storm,solr,kafka,nifi,atlas,sqoop");
                HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
                SearchFilter filter = new SearchFilter();

                Mockito.when(searchUtil.getSearchFilter(request, policyService.sortFields)).thenReturn(filter);
                Mockito.when(svcStore.getPolicies(filter)).thenReturn(rangerPolicyList);

                Mockito.when(((Object) response).getStatus()).thenReturn(204);
                serviceREST.getPoliciesInCsv(request, response);

                Assert.assertEquals(HttpServletResponse.SC_NO_CONTENT, response.getStatus());
                }
*/
@Test
public void test48exportPoliciesInExcel() throws Exception {
    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    List<RangerPolicy> rangerPolicyList = new ArrayList<RangerPolicy>();
    RangerPolicy rangerPolicy = rangerPolicy();
    rangerPolicyList.add(rangerPolicy);
    XXService xService = xService();
    XXServiceDao xServiceDao = Mockito.mock(XXServiceDao.class);
    XXServiceDef xServiceDef = serviceDef();
    XXServiceDefDao xServiceDefDao = Mockito.mock(XXServiceDefDao.class);
    request.setAttribute("serviceType", "hdfs,hbase,hive,yarn,knox,storm,solr,kafka,nifi,atlas,sqoop");
    HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
    SearchFilter filter = new SearchFilter();
    Mockito.when(searchUtil.getSearchFilter(request, policyService.sortFields)).thenReturn(filter);
    Mockito.when(svcStore.getPolicies(filter)).thenReturn(rangerPolicyList);
    Mockito.when(bizUtil.isAdmin()).thenReturn(true);
    Mockito.when(bizUtil.isKeyAdmin()).thenReturn(false);
    Mockito.when(bizUtil.getCurrentUserLoginId()).thenReturn("admin");
    Mockito.when(bizUtil.isAuditAdmin()).thenReturn(false);
    Mockito.when(bizUtil.isAuditKeyAdmin()).thenReturn(false);
    Mockito.when(daoManager.getXXService()).thenReturn(xServiceDao);
    Mockito.when(daoManager.getXXServiceDef()).thenReturn(xServiceDefDao);
    Mockito.when(daoManager.getXXService().findByName("HDFS_1-1-20150316062453")).thenReturn(xService);
    Mockito.when(daoManager.getXXServiceDef().getById(xService.getType())).thenReturn(xServiceDef);
    serviceREST.getPoliciesInExcel(request, response);
    Mockito.verify(svcStore).getPoliciesInExcel(rangerPolicyList, response);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) XXServiceDef(org.apache.ranger.entity.XXServiceDef) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) XXServiceDefDao(org.apache.ranger.db.XXServiceDefDao) ArrayList(java.util.ArrayList) HttpServletResponse(javax.servlet.http.HttpServletResponse) SearchFilter(org.apache.ranger.plugin.util.SearchFilter) XXService(org.apache.ranger.entity.XXService) XXServiceDao(org.apache.ranger.db.XXServiceDao) Test(org.junit.Test)

Aggregations

XXServiceDao (org.apache.ranger.db.XXServiceDao)32 XXService (org.apache.ranger.entity.XXService)31 Test (org.junit.Test)26 XXServiceDefDao (org.apache.ranger.db.XXServiceDefDao)23 XXServiceDef (org.apache.ranger.entity.XXServiceDef)20 RangerPolicy (org.apache.ranger.plugin.model.RangerPolicy)13 ArrayList (java.util.ArrayList)11 RangerService (org.apache.ranger.plugin.model.RangerService)11 WebApplicationException (javax.ws.rs.WebApplicationException)9 VXString (org.apache.ranger.view.VXString)7 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 ServiceTags (org.apache.ranger.plugin.util.ServiceTags)6 ExpectedException (org.junit.rules.ExpectedException)6 HashSet (java.util.HashSet)5 RangerServiceDef (org.apache.ranger.plugin.model.RangerServiceDef)5 SearchFilter (org.apache.ranger.plugin.util.SearchFilter)5 RangerAccessTypeDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerAccessTypeDef)4 Date (java.util.Date)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 XXPolicy (org.apache.ranger.entity.XXPolicy)3