ControllerUtil { public static Response updateMetadata(Map<String,String> params,String token){ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "updateMetadata"). put(LogMessage.MESSAGE, String.format ("Trying to upate metadata with params")). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); String _type = params.get("type"); String name = params.get("name"); String access = params.get("access"); String path = params.get("path"); path = "metadata/"+path; ObjectMapper objMapper = new ObjectMapper(); String pathjson ="{\"path\":\""+path+"\"}"; Response metadataResponse = reqProcessor.process("/read",pathjson,token); Map<String,Object> _metadataMap = null; if(HttpStatus.OK.equals(metadataResponse.getHttpstatus())){ try { _metadataMap = objMapper.readValue(metadataResponse.getResponse(), new TypeReference<Map<String,Object>>() {}); } catch (IOException e) { log.error(e); log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "updateMetadata"). put(LogMessage.MESSAGE, String.format ("Error creating _metadataMap for type [%s], name [%s], access [%s] and path [%s] message [%s]", _type, name, access, path, e.getMessage())). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); } @SuppressWarnings("unchecked") Map<String,Object> metadataMap = (Map<String,Object>) _metadataMap.get("data"); @SuppressWarnings("unchecked") Map<String,String> dataMap = (Map<String,String>) metadataMap.get(_type); if(dataMap == null) { dataMap = new HashMap<String,String>(); metadataMap.put(_type, dataMap);} dataMap.remove(name); if(!"delete".equals(access)) dataMap.put(name, access); String metadataJson = ""; try { metadataJson = objMapper.writeValueAsString(metadataMap); } catch (JsonProcessingException e) { log.error(e); log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "updateMetadata"). put(LogMessage.MESSAGE, String.format ("Error in creating metadataJson for type [%s], name [%s], access [%s] and path [%s] with message [%s]", _type, name, access, path, e.getMessage())). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); } String writeJson = "{\"path\":\""+path+"\",\"data\":"+ metadataJson +"}"; metadataResponse = reqProcessor.process("/write",writeJson,token); return metadataResponse; } return null; }  @Autowired(required = true) void setreqProcessor(RequestProcessor reqProcessor); static RequestProcessor getReqProcessor(); static OIDCUtil getOidcUtil(); @Autowired(required = true) void setOidcUtil(OIDCUtil oidcUtil); static void recursivedeletesdb(String jsonstr,String token,  Response responseVO); static void recursiveRead(String jsonstr,String token,  Response responseVO, SafeNode safeNode); static void getFoldersAndSecrets(String jsonstr,String token,  Response responseVO, SafeNode safeNode); static Response configureLDAPUser(String userName,String policies,String groups,String token ); static Response configureUserpassUser(String userName,String policies,String token ); static Response configureLDAPGroup(String groupName,String policies,String token ); static Response configureAWSRole(String roleName,String policies,String token ); static Response configureAWSIAMRole(String roleName,String policies,String token ); static Response updateMetadata(Map<String,String> params,String token); static Response updateMetaDataOnConfigChanges(String name, String type,String currentPolicies, String latestPolicies, String token); static Response updateMetadataOnSvcUpdate(String path, ServiceAccount serviceAccount, String token); static Response updateMetadataOnSvcaccPwdReset(Map<String,String> params,String token); static Map<String,Object> parseJson(String jsonString); static String convetToJson(Map<String,Object> jsonMap); static String getPoliciesAsStringFromJson(ObjectMapper objMapper, String policyJson); static List<String> getPoliciesAsListFromJson(ObjectMapper objMapper, String policyJson); static void updateUserPolicyAssociationOnSDBDelete(String sdb,Map<String,String> acessInfo,String token, UserDetails userDetails); static void updateGroupPolicyAssociationOnSDBDelete(String sdb,Map<String,String> acessInfo,String token, UserDetails userDetails); static void updateAwsRolePolicyAssociationOnSDBDelete(String sdb,Map<String,String> acessInfo,String token); static void deleteAwsRoleOnSDBDelete(String sdb,Map<String,String> acessInfo,String token); static boolean isValidDataPath(String path); static boolean isPathValid(String path); static boolean isValidSafePath(String path); static String getSafePath(String path); static String getSafeType(String path); static String getSafeName(String path); static boolean canAddPermission(String path,String token); static boolean isValidSafe(String path,String token); static boolean areSDBInputsValid(Map<String, Object> requestParams); static boolean areSDBInputsValid(Safe safe); static boolean areSDBInputsValidForUpdate(Map<String, Object> requestParams); static boolean areSafeGroupInputsValid(Map<String,String> requestMap); static boolean areAWSRoleInputsValid(Map<String, String> requestMap); static boolean areSafeUserInputsValid(SafeUser safeUser); static boolean areSafeUserInputsValid(Map<String,Object> requestMap); static boolean areSafeGroupInputsValid(SafeGroup safeGroup); static boolean areSafeAppRoleInputsValid(Map<String,Object> requestMap); static boolean areAWSRoleInputsValid(AWSRole awsRole); static String converSDBInputsToLowerCase(String jsonStr); static void converSDBInputsToLowerCase(Safe safe); static String convertAppRoleInputsToLowerCase(String jsonstr); static String convertSafeAppRoleAccessToLowerCase(String jsonstr); static String convertAppRoleSecretIdToLowerCase(String jsonstr); static boolean isSecretKeyValid(String jsonString); static boolean areAppRoleInputsValid(String jsonstr); static boolean areAppRoleInputsValid(AppRole approle); static AppRole getAppRoleObjFromString(String jsonstr); static boolean areSecretKeysValid(String jsonString); static String addDefaultSecretKey(String jsonString); static boolean areAwsLoginInputsValid(AWSAuthType authType, AWSAuthLogin awsAuthLogin); static boolean areAWSEC2RoleInputsValid(AWSLoginRole awsLoginRole); static boolean areAWSEC2RoleInputsValid(String jsonStr); static boolean areAWSIAMRoleInputsValid(AWSIAMRole awsiamRole); static HashMap<String, List<String>> getAllExistingSafeNames(String token); static List<String> getAllExistingSafeNames(String type, String token); static int getCountOfSafesForGivenSafeName(String safeName, String token); static int getCountOfSafesForGivenSafeName(String safeName, String safeType, String token); static String generateSafePath(String safeName, String safeType); static String populateAWSMetaJson(String appRoleName, String username); static boolean createMetadata(String metadataJson, String token); static Response canDeleteRole(String roleName, String token, UserDetails userDetails, String metadataPath); static String populateAppRoleMetaJson(String appRoleName, String username); static String populateUserMetaJson(String appRoleName, String username); static SSCred readSSCredFile(String fileLocation, boolean isDelete); static String getNclmUsername(); static String getNclmPassword(); static String getCwmToken(); static String getSsUsername(); static void setSscred(SSCred sscred); static String getSsPassword(); static SSCred getSscred(); static OIDCCred readOIDCCredFile(String fileLocation, boolean isDelete); static String getOidcClientName(); static String getOidcClientId(); static String getOidcClientSecret(); static String getOidcBoundAudiences(); static String getOidcDiscoveryUrl(); static String getOidcADLoginUrl(); static OIDCCred getOidcCred(); static Response hideMasterAppRoleFromResponse(Response response); static boolean updateMetaData(String path, Map<String,String> params,String token); static boolean arecertificateGroupInputsValid(CertificateGroup certificateGroup); static boolean canAddCertPermission(String path,String certificateName,String token); static List<String> getAllExistingCertNames(String type, String token); static Response updateSslCertificateMetadata(Map<String,String> params,String token); static Response updateSslMetaDataOnConfigChanges(String name, String type,String currentPolicies, String latestPolicies, String token); static Map<String,Object> filterDuplicateSafePermissions(Map<String,Object> access); static Map<String,Object> filterDuplicateSvcaccPermissions(Map<String,Object> access); static final Logger log; }
@Test public void test_updateMetadata_successfully() { String token = "7QPMPIGiyDFlJkrK3jFykUqa"; String path = "users/safe01"; Map<String,String> params = new HashMap<>(); params.put("type", "users"); params.put("name", "safe01"); params.put("access", "write"); params.put("path", path); String pathjson ="{\"path\":\"metadata/"+path+"\"}"; Response metaResponse = getMockResponse(HttpStatus.OK, true, "{\"data\":{\"description\":\"My first safe\",\"name\":\"safe01\",\"owner\":\"youremail@yourcompany.com\",\"ownerid\":\"normaluser\",\"type\":\"\"}}"); when(reqProcessor.process("/read",pathjson,token)).thenReturn(metaResponse); Response response = getMockResponse(HttpStatus.CREATED, true, ""); when(reqProcessor.process(eq("/write"),Mockito.any(),eq(token))).thenReturn(response); Response actualResponse = ControllerUtil.updateMetadata(params, token); assertEquals(HttpStatus.CREATED, actualResponse.getHttpstatus()); }
@Test public void test_updateMetadata1_successfully() throws JsonProcessingException { String token = "7QPMPIGiyDFlJkrK3jFykUqa"; Map<String,String> params = new HashMap<String,String>(); params.put("status", "Revoked"); String path = SSLCertificateConstants.SSL_CERT_PATH + "/testCert"; Response response = getMockResponse(HttpStatus.NO_CONTENT, true, ""); when(reqProcessor.process(eq("/write"),Mockito.any(),eq(token))).thenReturn(response); Boolean isUpdated = ControllerUtil.updateMetaData(path, params, token); assertEquals(Boolean.TRUE, isUpdated); }
@Test public void test_updateMetadata1_failed() throws JsonProcessingException { String token = "7QPMPIGiyDFlJkrK3jFykUqa"; Map<String,String> params = new HashMap<String,String>(); params.put("status", "Revoked"); String path = SSLCertificateConstants.SSL_CERT_PATH + "/testCert"; Response response = getMockResponse(HttpStatus.BAD_REQUEST, false, ""); when(reqProcessor.process(eq("/write"),Mockito.any(),eq(token))).thenReturn(response); Boolean isUpdated = ControllerUtil.updateMetaData(path, params, token); assertEquals(Boolean.FALSE, isUpdated); }