ServiceDBStore extends AbstractServiceStore { @Override public RangerService updateService(RangerService service, Map<String, Object> options) throws Exception { if(LOG.isDebugEnabled()) { LOG.debug("==> ServiceDBStore.updateService()"); } XXService existing = daoMgr.getXXService().getById(service.getId()); if(existing == null) { throw restErrorUtil.createRESTException( "no service exists with ID=" + service.getId(), MessageEnums.DATA_NOT_FOUND); } String existingName = existing.getName(); boolean renamed = !StringUtils.equalsIgnoreCase(service.getName(), existingName); if(renamed) { XXService newNameService = daoMgr.getXXService().findByName(service.getName()); if (newNameService != null) { throw restErrorUtil.createRESTException("another service already exists with name '" + service.getName() + "'. ID=" + newNameService.getId(), MessageEnums.DATA_NOT_UPDATABLE); } long countOfTaggedResources = daoMgr.getXXServiceResource().countTaggedResourcesInServiceId(existing.getId()); Boolean isForceRename = options != null && options.get(ServiceStore.OPTION_FORCE_RENAME) != null ? (Boolean) options.get(ServiceStore.OPTION_FORCE_RENAME) : Boolean.FALSE; if (countOfTaggedResources != 0L) { if (isForceRename) { LOG.warn("Forcing the renaming of service from " + existingName + " to " + service.getName() + " although it is associated with " + countOfTaggedResources + " service-resources!"); } else { throw restErrorUtil.createRESTException("Service " + existingName + " cannot be renamed, as it has associated service-resources", MessageEnums.DATA_NOT_UPDATABLE); } } } Map<String, String> configs = service.getConfigs(); Map<String, String> validConfigs = validateRequiredConfigParams(service, configs); if (validConfigs == null) { if (LOG.isDebugEnabled()) { LOG.debug("==> ConfigParams cannot be null, ServiceDBStore.createService(" + service + ")"); } throw restErrorUtil.createRESTException("ConfigParams cannot be null.", MessageEnums.ERROR_CREATING_OBJECT); } boolean hasTagServiceValueChanged = false; Long existingTagServiceId = existing.getTagService(); String newTagServiceName = service.getTagService(); Long newTagServiceId = null; if(newTagServiceName == null) { if(existingTagServiceId != null) { newTagServiceName = getServiceName(existingTagServiceId); service.setTagService(newTagServiceName); LOG.info("ServiceDBStore.updateService(id=" + service.getId() + "; name=" + service.getName() + "): tagService is null; using existing tagService '" + newTagServiceName + "'"); } } if (StringUtils.isNotBlank(newTagServiceName)) { RangerService tmp = getServiceByName(newTagServiceName); if (tmp == null || !EmbeddedServiceDefsUtil.EMBEDDED_SERVICEDEF_TAG_NAME.equals(tmp.getType())) { if (LOG.isDebugEnabled()) { LOG.debug("ServiceDBStore.updateService() - " + newTagServiceName + " does not refer to a valid tag service.(" + service + ")"); } throw restErrorUtil.createRESTException("Invalid tag service name " + newTagServiceName, MessageEnums.ERROR_CREATING_OBJECT); } else { newTagServiceId = tmp.getId(); } } if (existingTagServiceId == null) { if (newTagServiceId != null) { hasTagServiceValueChanged = true; } } else if (!existingTagServiceId.equals(newTagServiceId)) { hasTagServiceValueChanged = true; } boolean hasIsEnabledChanged = !existing.getIsenabled().equals(service.getIsEnabled()); List<XXServiceConfigMap> dbConfigMaps = daoMgr.getXXServiceConfigMap().findByServiceId(service.getId()); boolean hasServiceConfigForPluginChanged = hasServiceConfigForPluginChanged(dbConfigMaps, validConfigs); List<XXTrxLog> trxLogList = svcService.getTransactionLog(service, existing, RangerServiceService.OPERATION_UPDATE_CONTEXT); if(populateExistingBaseFields) { svcServiceWithAssignedId.setPopulateExistingBaseFields(true); service = svcServiceWithAssignedId.update(service); svcServiceWithAssignedId.setPopulateExistingBaseFields(false); } else { service.setCreateTime(existing.getCreateTime()); service.setGuid(existing.getGuid()); service.setVersion(existing.getVersion()); service = svcService.update(service); if (hasTagServiceValueChanged || hasIsEnabledChanged || hasServiceConfigForPluginChanged) { updatePolicyVersion(service, RangerPolicyDelta.CHANGE_TYPE_SERVICE_CHANGE, null, false); } } XXService xUpdService = daoMgr.getXXService().getById(service.getId()); String oldPassword = null; for(XXServiceConfigMap dbConfigMap : dbConfigMaps) { if(StringUtils.equalsIgnoreCase(dbConfigMap.getConfigkey(), CONFIG_KEY_PASSWORD)) { oldPassword = dbConfigMap.getConfigvalue(); } daoMgr.getXXServiceConfigMap().remove(dbConfigMap); } VXUser vXUser = null; XXServiceConfigMapDao xConfMapDao = daoMgr.getXXServiceConfigMap(); for (Entry<String, String> configMap : validConfigs.entrySet()) { String configKey = configMap.getKey(); String configValue = configMap.getValue(); if(StringUtils.equalsIgnoreCase(configKey, "username")) { String userName = stringUtil.getValidUserName(configValue); XXUser xxUser = daoMgr.getXXUser().findByUserName(userName); if (xxUser != null) { vXUser = xUserService.populateViewBean(xxUser); } else { UserSessionBase usb = ContextUtil.getCurrentUserSession(); if (usb != null && !usb.isUserAdmin()) { throw restErrorUtil.createRESTException("User does not exist with given username: [" + userName + "] please use existing user", MessageEnums.OPER_NO_PERMISSION); } vXUser = xUserMgr.createServiceConfigUser(userName); } } if (StringUtils.equalsIgnoreCase(configKey, CONFIG_KEY_PASSWORD)) { if (StringUtils.equalsIgnoreCase(configValue, HIDDEN_PASSWORD_STR)) { if (oldPassword != null && oldPassword.contains(",")) { PasswordUtils util = PasswordUtils.build(oldPassword); if (!util.getCryptAlgo().equalsIgnoreCase(CRYPT_ALGO)) { String decryptedPwd = PasswordUtils.decryptPassword(oldPassword); String paddingString = Joiner.on(",").skipNulls().join(CRYPT_ALGO, new String(util.getEncryptKey()), new String(util.getSalt()), util.getIterationCount(), PasswordUtils.generateIvIfNeeded(CRYPT_ALGO)); String encryptedPwd = PasswordUtils.encryptPassword(paddingString + "," + decryptedPwd); String newDecryptedPwd = PasswordUtils.decryptPassword(paddingString + "," + encryptedPwd); if (StringUtils.equals(newDecryptedPwd, decryptedPwd)) { configValue = paddingString + "," + encryptedPwd; } } else { configValue = oldPassword; } } else { configValue = oldPassword; } } else { String paddingString = Joiner.on(",").skipNulls().join(CRYPT_ALGO, ENCRYPT_KEY, SALT, ITERATION_COUNT, PasswordUtils.generateIvIfNeeded(CRYPT_ALGO)); String encryptedPwd = PasswordUtils.encryptPassword(paddingString + "," + configValue); String decryptedPwd = PasswordUtils.decryptPassword(paddingString + "," + encryptedPwd); if (StringUtils.equals(decryptedPwd, configValue)) { configValue = paddingString + "," + encryptedPwd; } } } XXServiceConfigMap xConfMap = new XXServiceConfigMap(); xConfMap = (XXServiceConfigMap) rangerAuditFields.populateAuditFields(xConfMap, xUpdService); xConfMap.setServiceId(service.getId()); xConfMap.setConfigkey(configKey); xConfMap.setConfigvalue(configValue); xConfMapDao.create(xConfMap); } updateTabPermissions(service.getType(), validConfigs); if (LOG.isDebugEnabled()) { LOG.debug("vXUser:[" + vXUser + "]"); } RangerService updService = svcService.getPopulatedViewObject(xUpdService); dataHistService.createObjectDataHistory(updService, RangerDataHistService.ACTION_UPDATE); bizUtil.createTrxLog(trxLogList); return updService; }  @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 test20updateService() throws Exception { XXServiceDao xServiceDao = Mockito.mock(XXServiceDao.class); XXService xService = Mockito.mock(XXService.class); XXServiceConfigMapDao xServiceConfigMapDao = Mockito .mock(XXServiceConfigMapDao.class); XXServiceConfigDefDao xServiceConfigDefDao = Mockito .mock(XXServiceConfigDefDao.class); XXUserDao xUserDao = Mockito.mock(XXUserDao.class); XXServiceResourceDao xServiceResourceDao = Mockito.mock(XXServiceResourceDao.class); RangerService rangerService = rangerService(); Map<String, Object> options = null; String name = "fdfdfds"; List<XXTrxLog> trxLogList = new ArrayList<XXTrxLog>(); XXTrxLog xTrxLogObj = new XXTrxLog(); xTrxLogObj.setAction("create"); xTrxLogObj.setAddedByUserId(Id); xTrxLogObj.setAttributeName("User Role"); xTrxLogObj.setCreateTime(new Date()); xTrxLogObj.setId(Id); xTrxLogObj.setNewValue("admin"); xTrxLogObj.setObjectClassType(0); xTrxLogObj.setObjectId(1L); xTrxLogObj.setParentObjectClassType(0); xTrxLogObj.setParentObjectId(Id); trxLogList.add(xTrxLogObj); Mockito.when(daoManager.getXXService()).thenReturn(xServiceDao); Mockito.when(xServiceDao.getById(Id)).thenReturn(xService); Mockito.when(daoManager.getXXService()).thenReturn(xServiceDao); List<XXServiceConfigDef> xServiceConfigDefList = new ArrayList<XXServiceConfigDef>(); XXServiceConfigDef serviceConfigDefObj = new XXServiceConfigDef(); serviceConfigDefObj.setId(Id); xServiceConfigDefList.add(serviceConfigDefObj); Mockito.when(daoManager.getXXServiceConfigDef()).thenReturn( xServiceConfigDefDao); Mockito.when(daoManager.getXXServiceResource()).thenReturn(xServiceResourceDao); Mockito.when(xServiceResourceDao.countTaggedResourcesInServiceId(xService.getId())).thenReturn(0L); Mockito.when(svcService.update(rangerService)) .thenReturn(rangerService); Mockito.when(daoManager.getXXService()).thenReturn(xServiceDao); Mockito.when(xServiceDao.getById(Id)).thenReturn(xService); 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.setUpdatedByUserId(null); xConfMap.setUpdateTime(new Date()); xConfMapList.add(xConfMap); Mockito.when(daoManager.getXXServiceConfigMap()).thenReturn( xServiceConfigMapDao); Mockito.when(xServiceConfigMapDao.findByServiceId(Id)).thenReturn( xConfMapList); Mockito.when(daoManager.getXXServiceConfigMap()).thenReturn( xServiceConfigMapDao); Mockito.when(xServiceConfigMapDao.remove(xConfMap)).thenReturn(true); Mockito.when(daoManager.getXXServiceConfigMap()).thenReturn( xServiceConfigMapDao); Mockito.when(daoManager.getXXUser()).thenReturn(xUserDao); Mockito.when( rangerAuditFields.populateAuditFields( Mockito.isA(XXServiceConfigMap.class), Mockito.isA(XXService.class))).thenReturn(xConfMap); Mockito.when(svcService.getPopulatedViewObject(xService)).thenReturn( rangerService); RangerService dbRangerService = serviceDBStore .updateService(rangerService, options); Assert.assertNotNull(dbRangerService); Assert.assertEquals(dbRangerService, rangerService); Assert.assertEquals(dbRangerService.getId(), rangerService.getId()); Assert.assertEquals(dbRangerService.getName(), rangerService.getName()); Assert.assertEquals(dbRangerService.getCreatedBy(), rangerService.getCreatedBy()); Assert.assertEquals(dbRangerService.getDescription(), rangerService.getDescription()); Assert.assertEquals(dbRangerService.getType(), rangerService.getType()); Assert.assertEquals(dbRangerService.getVersion(), rangerService.getVersion()); Mockito.verify(daoManager).getXXUser(); }
@Test public void test41updateServiceCryptAlgo() throws Exception { XXServiceDao xServiceDao = Mockito.mock(XXServiceDao.class); XXService xService = Mockito.mock(XXService.class); XXServiceConfigMapDao xServiceConfigMapDao = Mockito .mock(XXServiceConfigMapDao.class); XXServiceConfigDefDao xServiceConfigDefDao = Mockito .mock(XXServiceConfigDefDao.class); XXUserDao xUserDao = Mockito.mock(XXUserDao.class); RangerService rangerService = rangerService(); rangerService.getConfigs().put(ServiceDBStore.CONFIG_KEY_PASSWORD, "*****"); Mockito.when(daoManager.getXXService()).thenReturn(xServiceDao); Mockito.when(xServiceDao.getById(Id)).thenReturn(xService); Mockito.when(daoManager.getXXService()).thenReturn(xServiceDao); List<XXServiceConfigDef> xServiceConfigDefList = new ArrayList<XXServiceConfigDef>(); XXServiceConfigDef serviceConfigDefObj = new XXServiceConfigDef(); serviceConfigDefObj.setId(Id); xServiceConfigDefList.add(serviceConfigDefObj); Mockito.when(daoManager.getXXServiceConfigDef()).thenReturn( xServiceConfigDefDao); Mockito.when(svcService.update(rangerService)) .thenReturn(rangerService); Mockito.when(daoManager.getXXService()).thenReturn(xServiceDao); Mockito.when(xServiceDao.getById(Id)).thenReturn(xService); List<XXServiceConfigMap> xConfMapList = new ArrayList<XXServiceConfigMap>(); XXServiceConfigMap xConfMap = new XXServiceConfigMap(); xConfMap.setAddedByUserId(null); xConfMap.setConfigkey(ServiceDBStore.CONFIG_KEY_PASSWORD); xConfMap.setConfigvalue("PBEWithSHA1AndDESede,ENCRYPT_KEY,SALTSALT,4,lXintlvY73rdk3jXvD7CqB5mcSKl0AMhouBbI5m3whrhLdbKddnzxA=="); xConfMap.setCreateTime(new Date()); xConfMap.setServiceId(null); xConfMap.setUpdatedByUserId(null); xConfMap.setUpdateTime(new Date()); xConfMapList.add(xConfMap); Mockito.when(daoManager.getXXServiceConfigMap()).thenReturn( xServiceConfigMapDao); Mockito.when(xServiceConfigMapDao.findByServiceId(Id)).thenReturn( xConfMapList); Mockito.when(daoManager.getXXServiceConfigMap()).thenReturn( xServiceConfigMapDao); Mockito.when(xServiceConfigMapDao.remove(xConfMap)).thenReturn(true); Mockito.when(daoManager.getXXServiceConfigMap()).thenReturn( xServiceConfigMapDao); Mockito.when(daoManager.getXXUser()).thenReturn(xUserDao); Mockito.when( rangerAuditFields.populateAuditFields( Mockito.isA(XXServiceConfigMap.class), Mockito.isA(XXService.class))).thenReturn(xConfMap); Mockito.when(svcService.getPopulatedViewObject(xService)).thenReturn( rangerService); XXServiceResourceDao xServiceResourceDao = Mockito.mock(XXServiceResourceDao.class); Mockito.when(daoManager.getXXServiceResource()).thenReturn(xServiceResourceDao); Mockito.when(xServiceResourceDao.countTaggedResourcesInServiceId(xService.getId())).thenReturn(0L); Map<String, Object> options = null; RangerService dbRangerService = serviceDBStore .updateService(rangerService, options); Assert.assertNotNull(dbRangerService); Assert.assertEquals(dbRangerService, rangerService); Assert.assertEquals(dbRangerService.getId(), rangerService.getId()); Assert.assertEquals(dbRangerService.getName(), rangerService.getName()); Assert.assertEquals(dbRangerService.getCreatedBy(), rangerService.getCreatedBy()); Assert.assertEquals(dbRangerService.getDescription(), rangerService.getDescription()); Assert.assertEquals(dbRangerService.getType(), rangerService.getType()); Assert.assertEquals(dbRangerService.getVersion(), rangerService.getVersion()); Mockito.verify(daoManager).getXXUser(); }