Search in sources :

Example 11 with XXSecurityZone

use of org.apache.ranger.entity.XXSecurityZone in project ranger by apache.

the class TestServiceREST method test49importPoliciesFromFileAllowingOverride.

@SuppressWarnings("unchecked")
@Test
public void test49importPoliciesFromFileAllowingOverride() throws Exception {
    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    RangerPolicyValidator policyValidator = Mockito.mock(RangerPolicyValidator.class);
    Map<String, RangerPolicy> policiesMap = new LinkedHashMap<String, RangerPolicy>();
    RangerPolicy rangerPolicy = rangerPolicy();
    RangerService service = rangerService();
    XXService xService = xService();
    policiesMap.put("Name", rangerPolicy);
    XXServiceDao xServiceDao = Mockito.mock(XXServiceDao.class);
    XXServiceDef xServiceDef = serviceDef();
    XXServiceDefDao xServiceDefDao = Mockito.mock(XXServiceDefDao.class);
    XXSecurityZoneRefServiceDao xSecZoneRefServiceDao = Mockito.mock(XXSecurityZoneRefServiceDao.class);
    XXSecurityZoneRefTagServiceDao xSecZoneRefTagServiceDao = Mockito.mock(XXSecurityZoneRefTagServiceDao.class);
    XXSecurityZoneRefService xSecZoneRefService = Mockito.mock(XXSecurityZoneRefService.class);
    XXSecurityZoneRefTagService xSecZoneRefTagService = Mockito.mock(XXSecurityZoneRefTagService.class);
    XXSecurityZoneDao xSecZoneDao = Mockito.mock(XXSecurityZoneDao.class);
    XXSecurityZone xSecZone = Mockito.mock(XXSecurityZone.class);
    List<XXSecurityZoneRefService> zoneServiceList = new ArrayList<>();
    List<XXSecurityZoneRefTagService> zoneTagServiceList = new ArrayList<>();
    zoneServiceList.add(xSecZoneRefService);
    zoneTagServiceList.add(xSecZoneRefTagService);
    Map<String, String> zoneMappingMap = new LinkedHashMap<String, String>();
    zoneMappingMap.put("ZoneSource", "ZoneDestination");
    String PARAM_SERVICE_TYPE = "serviceType";
    String serviceTypeList = "hdfs,hbase,hive,yarn,knox,storm,solr,kafka,nifi,atlas,sqoop";
    request.setAttribute("serviceType", "hdfs,hbase,hive,yarn,knox,storm,solr,kafka,nifi,atlas,sqoop");
    SearchFilter filter = new SearchFilter();
    filter.setParam("serviceType", "value");
    File jsonPolicyFile = new File(importPoliceTestFilePath);
    InputStream uploadedInputStream = new FileInputStream(jsonPolicyFile);
    FormDataContentDisposition fileDetail = FormDataContentDisposition.name("file").fileName(jsonPolicyFile.getName()).size(uploadedInputStream.toString().length()).build();
    boolean isOverride = true;
    InputStream zoneInputStream = IOUtils.toInputStream("ZoneSource=ZoneDestination", "UTF-8");
    Mockito.when(searchUtil.getSearchFilter(request, policyService.sortFields)).thenReturn(filter);
    Mockito.when(request.getParameter(PARAM_SERVICE_TYPE)).thenReturn(serviceTypeList);
    Mockito.when(svcStore.createPolicyMap(Mockito.any(Map.class), Mockito.any(List.class), Mockito.anyString(), Mockito.any(Map.class), Mockito.any(List.class), Mockito.any(List.class), Mockito.any(RangerPolicy.class), Mockito.any(Map.class))).thenReturn(policiesMap);
    Mockito.when(validatorFactory.getPolicyValidator(svcStore)).thenReturn(policyValidator);
    Mockito.when(bizUtil.isAdmin()).thenReturn(true);
    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);
    Mockito.when(validatorFactory.getPolicyValidator(svcStore)).thenReturn(policyValidator);
    Mockito.when(svcStore.getMapFromInputStream(zoneInputStream)).thenReturn(zoneMappingMap);
    Mockito.when(daoManager.getXXSecurityZoneDao()).thenReturn(xSecZoneDao);
    Mockito.when(xSecZoneDao.findByZoneName(Mockito.anyString())).thenReturn(xSecZone);
    Mockito.when(daoManager.getXXSecurityZoneRefService()).thenReturn(xSecZoneRefServiceDao);
    Mockito.when(xSecZoneRefServiceDao.findByServiceNameAndZoneId(Mockito.anyString(), Mockito.anyLong())).thenReturn(zoneServiceList);
    Mockito.when(daoManager.getXXSecurityZoneRefTagService()).thenReturn(xSecZoneRefTagServiceDao);
    Mockito.when(xSecZoneRefTagServiceDao.findByTagServiceNameAndZoneId(Mockito.anyString(), Mockito.anyLong())).thenReturn(zoneTagServiceList);
    Mockito.when(svcStore.getServiceByName(Mockito.anyString())).thenReturn(service);
    serviceREST.importPoliciesFromFile(request, null, zoneInputStream, uploadedInputStream, fileDetail, isOverride, "unzoneToZone");
    Mockito.verify(svcStore).createPolicy(rangerPolicy);
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) XXSecurityZoneDao(org.apache.ranger.db.XXSecurityZoneDao) XXSecurityZoneRefTagServiceDao(org.apache.ranger.db.XXSecurityZoneRefTagServiceDao) ArrayList(java.util.ArrayList) SearchFilter(org.apache.ranger.plugin.util.SearchFilter) VXString(org.apache.ranger.view.VXString) XXServiceDao(org.apache.ranger.db.XXServiceDao) RangerPolicyValidator(org.apache.ranger.plugin.model.validation.RangerPolicyValidator) LinkedHashMap(java.util.LinkedHashMap) HttpServletRequest(javax.servlet.http.HttpServletRequest) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) RangerPluginInfoList(org.apache.ranger.view.RangerPluginInfoList) RangerServiceList(org.apache.ranger.view.RangerServiceList) ArrayList(java.util.ArrayList) List(java.util.List) RangerExportPolicyList(org.apache.ranger.view.RangerExportPolicyList) RangerPolicyList(org.apache.ranger.view.RangerPolicyList) RangerServiceDefList(org.apache.ranger.view.RangerServiceDefList) PList(org.apache.ranger.plugin.store.PList) RangerService(org.apache.ranger.plugin.model.RangerService) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) XXSecurityZone(org.apache.ranger.entity.XXSecurityZone) FileInputStream(java.io.FileInputStream) XXSecurityZoneRefService(org.apache.ranger.entity.XXSecurityZoneRefService) XXServiceDefDao(org.apache.ranger.db.XXServiceDefDao) XXSecurityZoneRefTagService(org.apache.ranger.entity.XXSecurityZoneRefTagService) XXSecurityZoneRefServiceDao(org.apache.ranger.db.XXSecurityZoneRefServiceDao) FormDataContentDisposition(com.sun.jersey.core.header.FormDataContentDisposition) XXService(org.apache.ranger.entity.XXService) File(java.io.File) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 12 with XXSecurityZone

use of org.apache.ranger.entity.XXSecurityZone in project ranger by apache.

the class PatchPreSql_057_ForUpdateToUniqueGUID_J10052 method updatePolicyGUIDToUniqueValue.

private void updatePolicyGUIDToUniqueValue() throws Exception {
    logger.info("==> updatePolicyGUIDToUniqueValue() ");
    List<XXSecurityZone> allXXZones = null;
    List<XXService> allXXService = null;
    allXXZones = daoMgr.getXXSecurityZoneDao().getAll();
    allXXService = daoMgr.getXXService().getAll();
    if (CollectionUtils.isNotEmpty(allXXZones) && CollectionUtils.isNotEmpty(allXXService)) {
        logger.info("Total number of zones " + allXXZones.size() + ", service :" + allXXService.size());
        for (XXSecurityZone xSecurityZone : allXXZones) {
            for (XXService xService : allXXService) {
                logger.info("serching duplicate guid policies for service :" + xService.getName() + " zone : " + xSecurityZone.getName());
                List<String> duplicateGuidList = daoMgr.getXXPolicy().findDuplicateGUIDByServiceIdAndZoneId(xService.getId(), xSecurityZone.getId());
                if (CollectionUtils.isNotEmpty(duplicateGuidList)) {
                    logger.info("Total number of duplicate GUIDs :" + duplicateGuidList.size() + " for service :" + xService.getName() + " and zone :" + xSecurityZone.getName());
                    for (String guid : duplicateGuidList) {
                        List<XXPolicy> xxPolicyList = daoMgr.getXXPolicy().findPolicyByGUIDAndServiceIdAndZoneId(guid, xService.getId(), xSecurityZone.getId());
                        boolean isFirstElement = false;
                        if (CollectionUtils.isNotEmpty(xxPolicyList)) {
                            isFirstElement = true;
                            for (XXPolicy xxPolicy : xxPolicyList) {
                                if (isFirstElement) {
                                    isFirstElement = false;
                                    continue;
                                }
                                RangerPolicy policy = svcStore.getPolicy(xxPolicy.getId());
                                if (policy != null) {
                                    guid = guidUtil.genGUID();
                                    xxPolicy.setGuid(guid);
                                    policy.setGuid(guid);
                                    xxPolicy.setPolicyText(JsonUtils.objectToJson(policy));
                                    daoMgr.getXXPolicy().update(xxPolicy);
                                }
                            }
                        } else {
                            logger.info("No policy found with guid:" + guid);
                        }
                    }
                } else {
                    logger.info("No duplicate GUID found in policy for Service :" + xService.getName() + ", Zone : " + xSecurityZone.getName());
                }
            }
        }
    } else {
        logger.info("No zone or service found");
    }
}
Also used : RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) XXService(org.apache.ranger.entity.XXService) XXPolicy(org.apache.ranger.entity.XXPolicy) XXSecurityZone(org.apache.ranger.entity.XXSecurityZone)

Example 13 with XXSecurityZone

use of org.apache.ranger.entity.XXSecurityZone in project ranger by apache.

the class XUserMgr method blockIfZoneGroup.

private void blockIfZoneGroup(Long grpId) {
    List<XXSecurityZoneRefGroup> zoneRefGrpList = daoManager.getXXSecurityZoneRefGroup().findByGroupId(grpId);
    if (CollectionUtils.isNotEmpty(zoneRefGrpList)) {
        StringBuilder zones = new StringBuilder();
        for (XXSecurityZoneRefGroup zoneRefGrp : zoneRefGrpList) {
            XXSecurityZone xSecZone = daoManager.getXXSecurityZoneDao().getById(zoneRefGrp.getZoneId());
            if (zones.indexOf(xSecZone.getName()) < 0)
                zones.append(xSecZone.getName() + ",");
        }
        this.prepareAndThrow(zoneRefGrpList.get(0).getGroupName(), RangerConstants.MODULE_SECURITY_ZONE, zones, GROUP);
    }
}
Also used : XXSecurityZoneRefGroup(org.apache.ranger.entity.XXSecurityZoneRefGroup) XXSecurityZone(org.apache.ranger.entity.XXSecurityZone)

Example 14 with XXSecurityZone

use of org.apache.ranger.entity.XXSecurityZone in project ranger by apache.

the class XUserMgr method blockIfZoneUser.

private void blockIfZoneUser(Long id) {
    List<XXSecurityZoneRefUser> zoneRefUserList = daoManager.getXXSecurityZoneRefUser().findByUserId(id);
    if (CollectionUtils.isNotEmpty(zoneRefUserList)) {
        StringBuilder zones = new StringBuilder();
        for (XXSecurityZoneRefUser zoneRefUser : zoneRefUserList) {
            XXSecurityZone xSecZone = daoManager.getXXSecurityZoneDao().getById(zoneRefUser.getZoneId());
            if (zones.indexOf(xSecZone.getName()) < 0)
                zones.append(xSecZone.getName() + ",");
        }
        this.prepareAndThrow(zoneRefUserList.get(0).getUserName(), RangerConstants.MODULE_SECURITY_ZONE, zones, USER);
    }
}
Also used : XXSecurityZoneRefUser(org.apache.ranger.entity.XXSecurityZoneRefUser) XXSecurityZone(org.apache.ranger.entity.XXSecurityZone)

Example 15 with XXSecurityZone

use of org.apache.ranger.entity.XXSecurityZone in project ranger by apache.

the class SecurityZoneDBStore method updateSecurityZoneById.

@Override
public RangerSecurityZone updateSecurityZoneById(RangerSecurityZone securityZone) throws Exception {
    XXSecurityZone xxSecurityZone = daoMgr.getXXSecurityZoneDao().findByZoneId(securityZone.getId());
    if (xxSecurityZone == null) {
        throw restErrorUtil.createRESTException("security-zone with id: " + securityZone.getId() + " does not exist");
    }
    Gson gsonBuilder = new GsonBuilder().setDateFormat("yyyyMMdd-HH:mm:ss.SSS-Z").create();
    RangerSecurityZone oldSecurityZone = gsonBuilder.fromJson(xxSecurityZone.getJsonData(), RangerSecurityZone.class);
    daoMgr.getXXGlobalState().onGlobalStateChange(RANGER_GLOBAL_STATE_NAME);
    RangerSecurityZone updatedSecurityZone = securityZoneService.update(securityZone);
    if (updatedSecurityZone == null) {
        throw new Exception("Cannot update security zone:[" + securityZone + "]");
    }
    securityZoneRefUpdater.createNewZoneMappingForRefTable(updatedSecurityZone);
    List<XXTrxLog> trxLogList = securityZoneService.getTransactionLog(updatedSecurityZone, oldSecurityZone, "update");
    bizUtil.createTrxLog(trxLogList);
    return securityZone;
}
Also used : RangerSecurityZone(org.apache.ranger.plugin.model.RangerSecurityZone) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) XXTrxLog(org.apache.ranger.entity.XXTrxLog) XXSecurityZone(org.apache.ranger.entity.XXSecurityZone)

Aggregations

XXSecurityZone (org.apache.ranger.entity.XXSecurityZone)23 RangerSecurityZone (org.apache.ranger.plugin.model.RangerSecurityZone)16 Test (org.junit.Test)13 XXSecurityZoneDao (org.apache.ranger.db.XXSecurityZoneDao)12 ArrayList (java.util.ArrayList)11 XXTrxLog (org.apache.ranger.entity.XXTrxLog)7 WebApplicationException (javax.ws.rs.WebApplicationException)4 XXGlobalStateDao (org.apache.ranger.db.XXGlobalStateDao)4 SearchFilter (org.apache.ranger.plugin.util.SearchFilter)4 XXPolicy (org.apache.ranger.entity.XXPolicy)3 XXSecurityZoneRefService (org.apache.ranger.entity.XXSecurityZoneRefService)3 XXSecurityZoneRefTagService (org.apache.ranger.entity.XXSecurityZoneRefTagService)3 XXService (org.apache.ranger.entity.XXService)3 RangerPolicy (org.apache.ranger.plugin.model.RangerPolicy)3 VXString (org.apache.ranger.view.VXString)3 FormDataContentDisposition (com.sun.jersey.core.header.FormDataContentDisposition)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2