ServiceDBStore extends AbstractServiceStore { @Override public RangerServiceDef createServiceDef(RangerServiceDef serviceDef) throws Exception { if (LOG.isDebugEnabled()) { LOG.debug("==> ServiceDBStore.createServiceDef(" + serviceDef + ")"); } XXServiceDef xServiceDef = daoMgr.getXXServiceDef().findByName( serviceDef.getName()); if (xServiceDef != null) { throw restErrorUtil.createRESTException("service-def with name: " + serviceDef.getName() + " already exists", MessageEnums.ERROR_DUPLICATE_OBJECT); } List<RangerServiceConfigDef> configs = serviceDef.getConfigs(); List<RangerResourceDef> resources = serviceDef.getResources(); if (CollectionUtils.isNotEmpty(resources)) { RangerServiceDefValidator validator = new RangerServiceDefValidator(this); List<ValidationFailureDetails> failures = new ArrayList<>(); boolean isValidResources = validator.isValidResources(serviceDef, failures, RangerValidator.Action.CREATE); if (!isValidResources) { throw restErrorUtil.createRESTException("service-def with name: " + serviceDef.getName() + " has invalid resources:[" + failures.toString() + "]", MessageEnums.INVALID_INPUT_DATA); } } List<RangerAccessTypeDef> accessTypes = serviceDef.getAccessTypes(); List<RangerPolicyConditionDef> policyConditions = serviceDef.getPolicyConditions(); List<RangerContextEnricherDef> contextEnrichers = serviceDef.getContextEnrichers(); List<RangerEnumDef> enums = serviceDef.getEnums(); RangerDataMaskDef dataMaskDef = serviceDef.getDataMaskDef(); RangerRowFilterDef rowFilterDef = serviceDef.getRowFilterDef(); List<RangerDataMaskTypeDef> dataMaskTypes = dataMaskDef == null || dataMaskDef.getMaskTypes() == null ? new ArrayList<RangerDataMaskTypeDef>() : dataMaskDef.getMaskTypes(); List<RangerAccessTypeDef> dataMaskAccessTypes = dataMaskDef == null || dataMaskDef.getAccessTypes() == null ? new ArrayList<RangerAccessTypeDef>() : dataMaskDef.getAccessTypes(); List<RangerResourceDef> dataMaskResources = dataMaskDef == null || dataMaskDef.getResources() == null ? new ArrayList<RangerResourceDef>() : dataMaskDef.getResources(); List<RangerAccessTypeDef> rowFilterAccessTypes = rowFilterDef == null || rowFilterDef.getAccessTypes() == null ? new ArrayList<RangerAccessTypeDef>() : rowFilterDef.getAccessTypes(); List<RangerResourceDef> rowFilterResources = rowFilterDef == null || rowFilterDef.getResources() == null ? new ArrayList<RangerResourceDef>() : rowFilterDef.getResources(); RangerServiceDefHelper defHelper = new RangerServiceDefHelper(serviceDef, false); defHelper.patchServiceDefWithDefaultValues(); serviceDef.setVersion(Long.valueOf(1)); if (populateExistingBaseFields) { svcDefServiceWithAssignedId.setPopulateExistingBaseFields(true); daoMgr.getXXServiceDef().setIdentityInsert(true); svcDefServiceWithAssignedId.create(serviceDef); svcDefServiceWithAssignedId.setPopulateExistingBaseFields(false); daoMgr.getXXServiceDef().updateSequence(); daoMgr.getXXServiceDef().setIdentityInsert(false); } else { serviceDef.setId(null); serviceDef.setCreateTime(null); serviceDef.setUpdateTime(null); serviceDef = serviceDefService.create(serviceDef); } Long serviceDefId = serviceDef.getId(); XXServiceDef createdSvcDef = daoMgr.getXXServiceDef().getById(serviceDefId); XXServiceConfigDefDao xxServiceConfigDao = daoMgr.getXXServiceConfigDef(); for(int i = 0; i < configs.size(); i++) { RangerServiceConfigDef config = configs.get(i); XXServiceConfigDef xConfig = new XXServiceConfigDef(); xConfig = serviceDefService.populateRangerServiceConfigDefToXX(config, xConfig, createdSvcDef, RangerServiceDefService.OPERATION_CREATE_CONTEXT); xConfig.setOrder(i); xConfig = xxServiceConfigDao.create(xConfig); } XXResourceDefDao xxResDefDao = daoMgr.getXXResourceDef(); for(int i = 0; i < resources.size(); i++) { RangerResourceDef resource = resources.get(i); XXResourceDef parent = xxResDefDao.findByNameAndServiceDefId(resource.getParent(), serviceDefId); Long parentId = (parent != null) ? parent.getId() : null; XXResourceDef xResource = new XXResourceDef(); xResource = serviceDefService.populateRangerResourceDefToXX(resource, xResource, createdSvcDef, RangerServiceDefService.OPERATION_CREATE_CONTEXT); xResource.setOrder(i); xResource.setParent(parentId); xResource = xxResDefDao.create(xResource); } XXAccessTypeDefDao xxATDDao = daoMgr.getXXAccessTypeDef(); for(int i = 0; i < accessTypes.size(); i++) { RangerAccessTypeDef accessType = accessTypes.get(i); XXAccessTypeDef xAccessType = new XXAccessTypeDef(); xAccessType = serviceDefService.populateRangerAccessTypeDefToXX(accessType, xAccessType, createdSvcDef, RangerServiceDefService.OPERATION_CREATE_CONTEXT); xAccessType.setOrder(i); xAccessType = xxATDDao.create(xAccessType); Collection<String> impliedGrants = accessType.getImpliedGrants(); XXAccessTypeDefGrantsDao xxATDGrantDao = daoMgr.getXXAccessTypeDefGrants(); for(String impliedGrant : impliedGrants) { XXAccessTypeDefGrants xImpliedGrant = new XXAccessTypeDefGrants(); xImpliedGrant.setAtdId(xAccessType.getId()); xImpliedGrant.setImpliedGrant(impliedGrant); xImpliedGrant = xxATDGrantDao.create(xImpliedGrant); } } XXPolicyConditionDefDao xxPolCondDao = daoMgr.getXXPolicyConditionDef(); for (int i = 0; i < policyConditions.size(); i++) { RangerPolicyConditionDef policyCondition = policyConditions.get(i); XXPolicyConditionDef xPolicyCondition = new XXPolicyConditionDef(); xPolicyCondition = serviceDefService .populateRangerPolicyConditionDefToXX(policyCondition, xPolicyCondition, createdSvcDef, RangerServiceDefService.OPERATION_CREATE_CONTEXT); xPolicyCondition.setOrder(i); xPolicyCondition = xxPolCondDao.create(xPolicyCondition); } XXContextEnricherDefDao xxContextEnricherDao = daoMgr.getXXContextEnricherDef(); for (int i = 0; i < contextEnrichers.size(); i++) { RangerContextEnricherDef contextEnricher = contextEnrichers.get(i); XXContextEnricherDef xContextEnricher = new XXContextEnricherDef(); xContextEnricher = serviceDefService .populateRangerContextEnricherDefToXX(contextEnricher, xContextEnricher, createdSvcDef, RangerServiceDefService.OPERATION_CREATE_CONTEXT); xContextEnricher.setOrder(i); xContextEnricher = xxContextEnricherDao.create(xContextEnricher); } XXEnumDefDao xxEnumDefDao = daoMgr.getXXEnumDef(); for(RangerEnumDef vEnum : enums) { XXEnumDef xEnum = new XXEnumDef(); xEnum = serviceDefService.populateRangerEnumDefToXX(vEnum, xEnum, createdSvcDef, RangerServiceDefService.OPERATION_CREATE_CONTEXT); xEnum = xxEnumDefDao.create(xEnum); List<RangerEnumElementDef> elements = vEnum.getElements(); XXEnumElementDefDao xxEnumEleDefDao = daoMgr.getXXEnumElementDef(); for(int i = 0; i < elements.size(); i++) { RangerEnumElementDef element = elements.get(i); XXEnumElementDef xElement = new XXEnumElementDef(); xElement = serviceDefService.populateRangerEnumElementDefToXX(element, xElement, xEnum, RangerServiceDefService.OPERATION_CREATE_CONTEXT); xElement.setOrder(i); xElement = xxEnumEleDefDao.create(xElement); } } XXDataMaskTypeDefDao xxDataMaskDefDao = daoMgr.getXXDataMaskTypeDef(); for (int i = 0; i < dataMaskTypes.size(); i++) { RangerDataMaskTypeDef dataMask = dataMaskTypes.get(i); XXDataMaskTypeDef xDataMaskDef = new XXDataMaskTypeDef(); xDataMaskDef = serviceDefService.populateRangerDataMaskDefToXX(dataMask, xDataMaskDef, createdSvcDef, RangerServiceDefService.OPERATION_CREATE_CONTEXT); xDataMaskDef.setOrder(i); xDataMaskDef = xxDataMaskDefDao.create(xDataMaskDef); } List<XXAccessTypeDef> xxAccessTypeDefs = xxATDDao.findByServiceDefId(createdSvcDef.getId()); for(RangerAccessTypeDef accessType : dataMaskAccessTypes) { if(! isAccessTypeInList(accessType.getName(), xxAccessTypeDefs)) { throw restErrorUtil.createRESTException("accessType with name: " + accessType.getName() + " does not exists", MessageEnums.DATA_NOT_FOUND); } } for(RangerAccessTypeDef accessType : rowFilterAccessTypes) { if(! isAccessTypeInList(accessType.getName(), xxAccessTypeDefs)) { throw restErrorUtil.createRESTException("accessType with name: " + accessType.getName() + " does not exists", MessageEnums.DATA_NOT_FOUND); } } for(XXAccessTypeDef xxAccessTypeDef : xxAccessTypeDefs) { String dataMaskOptions = null; String rowFilterOptions = null; for(RangerAccessTypeDef accessTypeDef : dataMaskAccessTypes) { if(StringUtils.equals(accessTypeDef.getName(), xxAccessTypeDef.getName())) { dataMaskOptions = svcDefServiceWithAssignedId.objectToJson(accessTypeDef); break; } } for(RangerAccessTypeDef accessTypeDef : rowFilterAccessTypes) { if(StringUtils.equals(accessTypeDef.getName(), xxAccessTypeDef.getName())) { rowFilterOptions = svcDefServiceWithAssignedId.objectToJson(accessTypeDef); break; } } if(!StringUtils.equals(dataMaskOptions, xxAccessTypeDef.getDataMaskOptions()) || !StringUtils.equals(rowFilterOptions, xxAccessTypeDef.getRowFilterOptions())) { xxAccessTypeDef.setDataMaskOptions(dataMaskOptions); xxAccessTypeDef.setRowFilterOptions(rowFilterOptions); xxATDDao.update(xxAccessTypeDef); } } List<XXResourceDef> xxResourceDefs = xxResDefDao.findByServiceDefId(createdSvcDef.getId()); for(RangerResourceDef resource : dataMaskResources) { if(! isResourceInList(resource.getName(), xxResourceDefs)) { throw restErrorUtil.createRESTException("resource with name: " + resource.getName() + " does not exists", MessageEnums.DATA_NOT_FOUND); } } for(RangerResourceDef resource : rowFilterResources) { if(! isResourceInList(resource.getName(), xxResourceDefs)) { throw restErrorUtil.createRESTException("resource with name: " + resource.getName() + " does not exists", MessageEnums.DATA_NOT_FOUND); } } for(XXResourceDef xxResourceDef : xxResourceDefs) { String dataMaskOptions = null; String rowFilterOptions = null; for(RangerResourceDef resource : dataMaskResources) { if(StringUtils.equals(resource.getName(), xxResourceDef.getName())) { dataMaskOptions = svcDefServiceWithAssignedId.objectToJson(resource); break; } } for(RangerResourceDef resource : rowFilterResources) { if(StringUtils.equals(resource.getName(), xxResourceDef.getName())) { rowFilterOptions = svcDefServiceWithAssignedId.objectToJson(resource); break; } } if(!StringUtils.equals(dataMaskOptions, xxResourceDef.getDataMaskOptions()) || !StringUtils.equals(rowFilterOptions, xxResourceDef.getRowFilterOptions())) { xxResourceDef.setDataMaskOptions(dataMaskOptions); xxResourceDef.setRowFilterOptions(rowFilterOptions); xxResDefDao.update(xxResourceDef); } } RangerServiceDef createdServiceDef = serviceDefService.getPopulatedViewObject(createdSvcDef); dataHistService.createObjectDataHistory(createdServiceDef, RangerDataHistService.ACTION_CREATE); postCreate(createdServiceDef); if (LOG.isDebugEnabled()) { LOG.debug("<== ServiceDBStore.createServiceDef(" + serviceDef + "): " + createdServiceDef); } return createdServiceDef; }  @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 test11createServiceDef() throws Exception { XXServiceDefDao xServiceDefDao = Mockito.mock(XXServiceDefDao.class); XXResourceDefDao xResourceDefDao = Mockito.mock(XXResourceDefDao.class); XXServiceConfigDefDao xServiceConfigDefDao = Mockito .mock(XXServiceConfigDefDao.class); XXAccessTypeDefDao xAccessTypeDefDao = Mockito .mock(XXAccessTypeDefDao.class); XXPolicyConditionDefDao xPolicyConditionDefDao = Mockito .mock(XXPolicyConditionDefDao.class); XXContextEnricherDefDao xContextEnricherDefDao = Mockito .mock(XXContextEnricherDefDao.class); XXEnumDefDao xEnumDefDao = Mockito.mock(XXEnumDefDao.class); XXServiceDef xServiceDef = Mockito.mock(XXServiceDef.class); XXResourceDef xResourceDef = Mockito.mock(XXResourceDef.class); XXAccessTypeDef xAccessTypeDef = Mockito.mock(XXAccessTypeDef.class); List<XXAccessTypeDef> xAccessTypeDefs = new ArrayList<XXAccessTypeDef>(); xAccessTypeDefs.add(xAccessTypeDef); List<XXResourceDef> xResourceDefs = new ArrayList<XXResourceDef>(); xResourceDefs.add(xResourceDef); RangerServiceDef serviceDef = new RangerServiceDef(); Mockito.when(serviceDefService.create(serviceDef)).thenReturn( serviceDef); Mockito.when(daoManager.getXXServiceDef()).thenReturn(xServiceDefDao); Mockito.when(daoManager.getXXServiceDef()).thenReturn(xServiceDefDao); Mockito.when(xServiceDefDao.getById(null)).thenReturn(xServiceDef); Mockito.when(daoManager.getXXServiceConfigDef()).thenReturn( xServiceConfigDefDao); Mockito.when(daoManager.getXXResourceDef()).thenReturn(xResourceDefDao); Mockito.when(xResourceDefDao.findByServiceDefId(xServiceDef.getId())).thenReturn(xResourceDefs); Mockito.when(daoManager.getXXAccessTypeDef()).thenReturn( xAccessTypeDefDao); Mockito.when(xAccessTypeDefDao.findByServiceDefId(xServiceDef.getId())).thenReturn(xAccessTypeDefs); Mockito.when(daoManager.getXXPolicyConditionDef()).thenReturn( xPolicyConditionDefDao); Mockito.when(daoManager.getXXContextEnricherDef()).thenReturn( xContextEnricherDefDao); Mockito.when(daoManager.getXXEnumDef()).thenReturn(xEnumDefDao); Mockito.when(serviceDefService.getPopulatedViewObject(xServiceDef)) .thenReturn(serviceDef); RangerServiceDef dbServiceDef = serviceDBStore .createServiceDef(serviceDef); Assert.assertNotNull(dbServiceDef); Assert.assertEquals(dbServiceDef, serviceDef); Assert.assertEquals(dbServiceDef.getId(), serviceDef.getId()); Assert.assertEquals(dbServiceDef.getCreatedBy(), serviceDef.getCreatedBy()); Assert.assertEquals(dbServiceDef.getDescription(), serviceDef.getDescription()); Assert.assertEquals(dbServiceDef.getGuid(), serviceDef.getGuid()); Assert.assertEquals(dbServiceDef.getImplClass(), serviceDef.getImplClass()); Assert.assertEquals(dbServiceDef.getLabel(), serviceDef.getLabel()); Assert.assertEquals(dbServiceDef.getName(), serviceDef.getName()); Assert.assertEquals(dbServiceDef.getRbKeyDescription(), serviceDef.getRbKeyDescription()); Assert.assertEquals(dbServiceDef.getRbKeyLabel(), serviceDef.getLabel()); Assert.assertEquals(dbServiceDef.getConfigs(), serviceDef.getConfigs()); Assert.assertEquals(dbServiceDef.getVersion(), serviceDef.getVersion()); Assert.assertEquals(dbServiceDef.getResources(), serviceDef.getResources()); Mockito.verify(serviceDefService).getPopulatedViewObject(xServiceDef); Mockito.verify(serviceDefService).create(serviceDef); Mockito.verify(daoManager).getXXServiceConfigDef(); Mockito.verify(daoManager).getXXEnumDef(); Mockito.verify(daoManager).getXXAccessTypeDef(); }