ServiceDBStore extends AbstractServiceStore { @Override public RangerPolicy createPolicy(RangerPolicy policy) throws Exception { RangerService service = getServiceByName(policy.getService()); if(service == null) { throw new Exception("service does not exist - name=" + policy.getService()); } XXServiceDef xServiceDef = daoMgr.getXXServiceDef().findByName(service.getType()); if(xServiceDef == null) { throw new Exception("service-def does not exist - name=" + service.getType()); } Long zoneId = RangerSecurityZone.RANGER_UNZONED_SECURITY_ZONE_ID; String zoneName = policy.getZoneName(); if (StringUtils.isNotEmpty(zoneName)) { RangerSecurityZone zone = getSecurityZone(zoneName); if (zone == null) { throw new Exception("zone does not exist - name=" + zoneName); } else { zoneId = zone.getId(); } } XXPolicy existing = daoMgr.getXXPolicy().findByNameAndServiceIdAndZoneId(policy.getName(), service.getId(), zoneId); if(existing != null) { throw new Exception("policy already exists: ServiceName=" + policy.getService() + "; PolicyName=" + policy.getName() + ". ID=" + existing.getId()); } List<String> policyLabels = policy.getPolicyLabels(); Set<String> uniquePolicyLabels = new TreeSet<>(policyLabels); policy.setVersion(Long.valueOf(1)); updatePolicySignature(policy); boolean updateServiceInfoRoleVersion = false; if (isSupportsRolesDownloadByService()) { updateServiceInfoRoleVersion = isRoleDownloadRequired(policy, service); } if(populateExistingBaseFields) { assignedIdPolicyService.setPopulateExistingBaseFields(true); daoMgr.getXXPolicy().setIdentityInsert(true); policy = assignedIdPolicyService.create(policy, true); daoMgr.getXXPolicy().setIdentityInsert(false); daoMgr.getXXPolicy().updateSequence(); assignedIdPolicyService.setPopulateExistingBaseFields(false); } else { policy = policyService.create(policy, true); } XXPolicy xCreatedPolicy = daoMgr.getXXPolicy().getById(policy.getId()); policyRefUpdater.createNewPolMappingForRefTable(policy, xCreatedPolicy, xServiceDef); createOrMapLabels(xCreatedPolicy, uniquePolicyLabels); RangerPolicy createdPolicy = policyService.getPopulatedViewObject(xCreatedPolicy); handlePolicyUpdate(service, RangerPolicyDelta.CHANGE_TYPE_POLICY_CREATE, createdPolicy, updateServiceInfoRoleVersion); dataHistService.createObjectDataHistory(createdPolicy, RangerDataHistService.ACTION_CREATE); List<XXTrxLog> trxLogList = getTransactionLogList(createdPolicy, RangerPolicyService.OPERATION_IMPORT_CREATE_CONTEXT, RangerPolicyService.OPERATION_CREATE_CONTEXT); bizUtil.createTrxLog(trxLogList); return createdPolicy; }  @Override void init(); @PostConstruct void initStore(); @Override RangerServiceDef createServiceDef(RangerServiceDef serviceDef); @Override RangerServiceDef updateServiceDef(RangerServiceDef serviceDef); void deleteServiceDef(Long serviceDefId, Boolean forceDelete); void deleteXXAccessTypeDef(XXAccessTypeDef xAccess); void deleteXXResourceDef(XXResourceDef xRes); @Override RangerServiceDef getServiceDef(Long id); @Override RangerServiceDef getServiceDefByName(String name); @Override RangerServiceDef getServiceDefByDisplayName(String displayName); @Override List<RangerServiceDef> getServiceDefs(SearchFilter filter); @Override PList<RangerServiceDef> getPaginatedServiceDefs(SearchFilter filter); @Override RangerService createService(RangerService service); @Override RangerService updateService(RangerService service, Map<String, Object> options); @Override void deleteService(Long id); @Override List<RangerPolicy> getPoliciesByResourceSignature(String serviceName, String policySignature, Boolean isPolicyEnabled); @Override RangerService getService(Long id); @Override RangerService getServiceByName(String name); @Override RangerService getServiceByDisplayName(String displayName); RangerService getServiceByNameForDP(String name); @Override List<RangerService> getServices(SearchFilter filter); PList<RangerService> getPaginatedServices(SearchFilter filter); @Override RangerPolicy createPolicy(RangerPolicy policy); void createOrMapLabels(XXPolicy xPolicy, Set<String> uniquePolicyLabels); @Override RangerPolicy updatePolicy(RangerPolicy policy); @Override void deletePolicy(RangerPolicy policy); @Override void deletePolicy(RangerPolicy policy, RangerService service); @Override boolean policyExists(Long id); @Override RangerPolicy getPolicy(Long id); @Override List<RangerPolicy> getPolicies(SearchFilter filter); @Override Long getPolicyId(final Long serviceId, final String policyName, final Long zoneId); void getPoliciesInExcel(List<RangerPolicy> policies, HttpServletResponse response); void getPoliciesInCSV(List<RangerPolicy> policies,
			HttpServletResponse response); void getPoliciesInJson(List<RangerPolicy> policies,
			HttpServletResponse response); PList<RangerPolicy> getPaginatedPolicies(SearchFilter filter); @Override List<RangerPolicy> getServicePolicies(Long serviceId, SearchFilter filter); List<RangerPolicy> noZoneFilter(List<RangerPolicy> servicePolicies); PList<RangerPolicy> getPaginatedServicePolicies(Long serviceId, SearchFilter filter); @Override List<RangerPolicy> getServicePolicies(String serviceName, SearchFilter filter); PList<RangerPolicy> getPaginatedServicePolicies(String serviceName, SearchFilter filter); @Override ServicePolicies getServicePoliciesIfUpdated(String serviceName, Long lastKnownVersion, boolean needsBackwardCompatibility); @Override Long getServicePolicyVersion(String serviceName); @Override ServicePolicies getServicePolicyDeltasOrPolicies(String serviceName, Long lastKnownVersion); @Override ServicePolicies getServicePolicyDeltas(String serviceName, Long lastKnownVersion); @Override ServicePolicies getServicePolicies(String serviceName, Long lastKnownVersion); void createZoneDefaultPolicies(Collection<String> serviceNames, RangerSecurityZone zone); void deleteZonePolicies(Collection<String> serviceNames, Long zoneId); static void persistVersionChange(ServiceVersionUpdater serviceVersionUpdater); @Override Boolean getPopulateExistingBaseFields(); @Override void setPopulateExistingBaseFields(Boolean populateExistingBaseFields); RangerPolicy getPolicyFromEventTime(String eventTime, Long policyId); VXString getPolicyVersionList(Long policyId); RangerPolicy getPolicyForVersionNumber(Long policyId, int versionNo); @Override RangerSecurityZone getSecurityZone(Long id); @Override RangerSecurityZone getSecurityZone(String name); @Override long getPoliciesCount(final String serviceName); void putMetaDataInfo(RangerExportPolicyList rangerExportPolicyList); Map<String, String> getMapFromInputStream(InputStream mapStream); Map<String, RangerPolicy> setPolicyMapKeyValue(Map<String, RangerPolicy> policiesMap, RangerPolicy policy); Map<String, RangerPolicy> createPolicyMap(Map<String, String> zoneMappingMap, List<String> sourceZones,
			String destinationZoneName, Map<String, String> servicesMappingMap, List<String> sourceServices,
			List<String> destinationServices, RangerPolicy policy, Map<String, RangerPolicy> policiesMap); void getServiceUpgraded(); void resetPolicyUpdateLog(int retentionInDays, Integer policyChangeType); void resetTagUpdateLog(int retentionInDays, ServiceTags.TagsChangeType tagChangeType); List<String> getPolicyLabels(SearchFilter searchFilter); String getMetricByType(String type); boolean isServiceAdminUser(String serviceName, String userName); static boolean isSupportsPolicyDeltas(); static boolean isSupportsRolesDownloadByService(); @Override Map<String, String> getServiceConfigForPlugin(Long serviceId); static final String SERVICE_ADMIN_USERS; static final String CRYPT_ALGO; static final String ENCRYPT_KEY; static final String SALT; static final Integer ITERATION_COUNT; static boolean SUPPORTS_POLICY_DELTAS; static Integer RETENTION_PERIOD_IN_DAYS; static Integer TAG_RETENTION_PERIOD_IN_DAYS; static final String HIDDEN_PASSWORD_STR; static final String CONFIG_KEY_PASSWORD; static final String ACCESS_TYPE_DECRYPT_EEK; static final String ACCESS_TYPE_GENERATE_EEK; static final String ACCESS_TYPE_GET_METADATA; }
@Test public void test26createPolicy() throws Exception { setup(); XXServiceDefDao xServiceDefDao = Mockito.mock(XXServiceDefDao.class); XXPolicy xPolicy = Mockito.mock(XXPolicy.class); XXPolicyDao xPolicyDao = Mockito.mock(XXPolicyDao.class); XXServiceDao xServiceDao = Mockito.mock(XXServiceDao.class); XXService xService = Mockito.mock(XXService.class); XXServiceDef xServiceDef = serviceDef(); Map<String, String> configs = new HashMap<String, String>(); configs.put("username", "servicemgr"); configs.put("password", "servicemgr"); configs.put("namenode", "servicemgr"); configs.put("hadoop.security.authorization", "No"); configs.put("hadoop.security.authentication", "Simple"); configs.put("hadoop.security.auth_to_local", ""); configs.put("dfs.datanode.kerberos.principal", ""); configs.put("dfs.namenode.kerberos.principal", ""); configs.put("dfs.secondary.namenode.kerberos.principal", ""); configs.put("hadoop.rpc.protection", "Privacy"); configs.put("commonNameForCertificate", ""); RangerService rangerService = new RangerService(); rangerService.setId(Id); rangerService.setConfigs(configs); rangerService.setCreateTime(new Date()); rangerService.setDescription("service policy"); rangerService.setGuid("1427365526516_835_0"); rangerService.setIsEnabled(true); rangerService.setName("HDFS_1"); rangerService.setPolicyUpdateTime(new Date()); rangerService.setType("1"); rangerService.setUpdatedBy("Admin"); String policyName = "HDFS_1-1-20150316062345"; String name = "HDFS_1-1-20150316062453"; List<RangerPolicyItemAccess> accessesList = new ArrayList<RangerPolicyItemAccess>(); RangerPolicyItemAccess policyItemAccess = new RangerPolicyItemAccess(); policyItemAccess.setIsAllowed(true); policyItemAccess.setType("1"); List<String> usersList = new ArrayList<String>(); List<String> groupsList = new ArrayList<String>(); List<String> rolesList = new ArrayList<String>(); List<String> policyLabels = new ArrayList<String>(); List<RangerPolicyItemCondition> conditionsList = new ArrayList<RangerPolicyItemCondition>(); RangerPolicyItemCondition policyItemCondition = new RangerPolicyItemCondition(); policyItemCondition.setType("1"); policyItemCondition.setValues(usersList); conditionsList.add(policyItemCondition); List<RangerPolicyItem> policyItems = new ArrayList<RangerPolicy.RangerPolicyItem>(); RangerPolicyItem rangerPolicyItem = new RangerPolicyItem(); rangerPolicyItem.setDelegateAdmin(false); rangerPolicyItem.setAccesses(accessesList); rangerPolicyItem.setConditions(conditionsList); rangerPolicyItem.setGroups(groupsList); rangerPolicyItem.setUsers(usersList); policyItems.add(rangerPolicyItem); List<RangerPolicyItem> policyItemsSet = new ArrayList<RangerPolicy.RangerPolicyItem>(); RangerPolicyItem paramPolicyItem = new RangerPolicyItem(accessesList, usersList, groupsList, rolesList, conditionsList, false); paramPolicyItem.setDelegateAdmin(false); paramPolicyItem.setAccesses(accessesList); paramPolicyItem.setConditions(conditionsList); paramPolicyItem.setGroups(groupsList); rangerPolicyItem.setUsers(usersList); policyItemsSet.add(paramPolicyItem); XXPolicyItem xPolicyItem = new XXPolicyItem(); xPolicyItem.setDelegateAdmin(false); xPolicyItem.setAddedByUserId(null); xPolicyItem.setCreateTime(new Date()); xPolicyItem.setGUID(null); xPolicyItem.setId(Id); xPolicyItem.setOrder(null); xPolicyItem.setPolicyId(Id); xPolicyItem.setUpdatedByUserId(null); xPolicyItem.setUpdateTime(new Date()); XXPolicy xxPolicy = new XXPolicy(); xxPolicy.setId(Id); xxPolicy.setName(name); xxPolicy.setAddedByUserId(Id); xxPolicy.setCreateTime(new Date()); xxPolicy.setDescription("test"); xxPolicy.setIsAuditEnabled(true); xxPolicy.setIsEnabled(true); xxPolicy.setService(1L); xxPolicy.setUpdatedByUserId(Id); xxPolicy.setUpdateTime(new Date()); List<XXServiceConfigDef> xServiceConfigDefList = new ArrayList<XXServiceConfigDef>(); XXServiceConfigDef serviceConfigDefObj = new XXServiceConfigDef(); serviceConfigDefObj.setId(Id); xServiceConfigDefList.add(serviceConfigDefObj); List<XXServiceConfigMap> xConfMapList = new ArrayList<XXServiceConfigMap>(); XXServiceConfigMap xConfMap = new XXServiceConfigMap(); xConfMap.setAddedByUserId(null); xConfMap.setConfigkey(name); xConfMap.setConfigvalue(name); xConfMap.setCreateTime(new Date()); xConfMap.setServiceId(null); xConfMap.setId(Id); xConfMap.setUpdatedByUserId(null); xConfMap.setUpdateTime(new Date()); xConfMapList.add(xConfMap); List<String> users = new ArrayList<String>(); RangerPolicyResource rangerPolicyResource = new RangerPolicyResource(); rangerPolicyResource.setIsExcludes(true); rangerPolicyResource.setIsRecursive(true); rangerPolicyResource.setValue("1"); rangerPolicyResource.setValues(users); Map<String, RangerPolicyResource> policyResource = new HashMap<String, RangerPolicyResource>(); policyResource.put(name, rangerPolicyResource); policyResource.put(policyName, rangerPolicyResource); RangerPolicy rangerPolicy = new RangerPolicy(); rangerPolicy.setId(Id); rangerPolicy.setCreateTime(new Date()); rangerPolicy.setDescription("policy"); rangerPolicy.setGuid("policyguid"); rangerPolicy.setIsEnabled(true); rangerPolicy.setName("HDFS_1-1-20150316062453"); rangerPolicy.setUpdatedBy("Admin"); rangerPolicy.setUpdateTime(new Date()); rangerPolicy.setService("HDFS_1-1-20150316062453"); rangerPolicy.setIsAuditEnabled(true); rangerPolicy.setPolicyItems(policyItems); rangerPolicy.setResources(policyResource); rangerPolicy.setPolicyLabels(policyLabels); XXPolicyResource xPolicyResource = new XXPolicyResource(); xPolicyResource.setAddedByUserId(Id); xPolicyResource.setCreateTime(new Date()); xPolicyResource.setId(Id); xPolicyResource.setIsExcludes(true); xPolicyResource.setIsRecursive(true); xPolicyResource.setPolicyId(Id); xPolicyResource.setResDefId(Id); xPolicyResource.setUpdatedByUserId(Id); xPolicyResource.setUpdateTime(new Date()); List<XXPolicyConditionDef> policyConditionDefList = new ArrayList<XXPolicyConditionDef>(); XXPolicyConditionDef policyConditionDefObj = new XXPolicyConditionDef(); policyConditionDefObj.setAddedByUserId(Id); policyConditionDefObj.setCreateTime(new Date()); policyConditionDefObj.setDefid(Id); policyConditionDefObj.setDescription("policy"); policyConditionDefObj.setId(Id); policyConditionDefObj.setName("country"); policyConditionDefObj.setOrder(0); policyConditionDefObj.setUpdatedByUserId(Id); policyConditionDefObj.setUpdateTime(new Date()); policyConditionDefList.add(policyConditionDefObj); Mockito.when(daoManager.getXXService()).thenReturn(xServiceDao); Mockito.when(xServiceDao.findByName(name)).thenReturn(xService); Mockito.when(svcService.getPopulatedViewObject(xService)).thenReturn( rangerService); Mockito.when(daoManager.getXXServiceDef()).thenReturn(xServiceDefDao); Mockito.when(xServiceDefDao.findByName(rangerService.getType())) .thenReturn(xServiceDef); Mockito.when(daoManager.getXXPolicy()).thenReturn(xPolicyDao); Mockito.when(policyService.create(rangerPolicy, true)).thenReturn( rangerPolicy); Mockito.when(daoManager.getXXPolicy()).thenReturn(xPolicyDao); Mockito.when(xPolicyDao.getById(Id)).thenReturn(xPolicy); Mockito.doNothing().when(policyRefUpdater).createNewPolMappingForRefTable(rangerPolicy, xPolicy, xServiceDef); Mockito.when(policyService.getPopulatedViewObject(xPolicy)).thenReturn(rangerPolicy); Mockito.when(daoManager.getXXService()).thenReturn(xServiceDao); Mockito.when(xServiceDao.getById(Id)).thenReturn(xService); Mockito.when(daoManager.getXXService()).thenReturn(xServiceDao); Mockito.when(xServiceDao.getById(Id)).thenReturn(xService); RangerPolicyResourceSignature signature = Mockito .mock(RangerPolicyResourceSignature.class); Mockito.when(factory.createPolicyResourceSignature(rangerPolicy)) .thenReturn(signature); Mockito.when(!bizUtil.hasAccess(xService, null)).thenReturn(true); RangerPolicy dbRangerPolicy = serviceDBStore.createPolicy(rangerPolicy); Assert.assertNotNull(dbRangerPolicy); Assert.assertEquals(Id, dbRangerPolicy.getId()); }