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);
}
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");
}
}
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);
}
}
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);
}
}
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;
}
Aggregations