SafesService { public ResponseEntity<String> createSafe(String token, Safe safe) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create SDB"). put(LogMessage.MESSAGE, String.format ("Trying to Create SDB")). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); if (!ControllerUtil.areSDBInputsValid(safe)) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid input values\"]}"); } if (safe.getSafeBasicDetails().getName().endsWith("_")) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid Safe name: unexpected character _ in the end\"]}"); } if (safe.getSafeBasicDetails().getDescription().length() > 1024) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid input values: Description too long\"]}"); } if (safe.getSafeBasicDetails().getDescription().length() <10) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid Description: Please enter minimum 10 characters\"]}"); } if (safe.getSafeBasicDetails().getName().length() <3) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid Safe name: Please enter minimum 3 characters\"]}"); } ControllerUtil.converSDBInputsToLowerCase(safe); String path = safe.getPath(); safe.getSafeBasicDetails().setType(ControllerUtil.getSafeType(path)); String jsonStr = JSONUtil.getJSON(safe); Map<String,Object> rqstParams = ControllerUtil.parseJson(jsonStr); if(ControllerUtil.isValidSafePath(path)){ Response response = reqProcessor.process("/sdb/create",jsonStr,token); if(response.getHttpstatus().equals(HttpStatus.NO_CONTENT)){ String _path = "metadata/"+path; rqstParams.put("path",_path); String metadataJson = ControllerUtil.convetToJson(rqstParams); response = reqProcessor.process("/write",metadataJson,token); boolean isMetaDataUpdated = false; if(response.getHttpstatus().equals(HttpStatus.NO_CONTENT)){ isMetaDataUpdated = true; } String folders[] = path.split("[/]+"); if(folders.length==2){ String Safe = folders[1]; Map<String,Object> policyMap = new HashMap<String,Object>(); Map<String,String> accessMap = new HashMap<String,String>(); accessMap.put(path+"/*", TVaultConstants.READ_POLICY); policyMap.put("accessid", "r_"+folders[0]+"_"+Safe); policyMap.put("access", accessMap); String policyRequestJson = ControllerUtil.convetToJson(policyMap); Response r_response = reqProcessor.process("/access/update",policyRequestJson,token); accessMap.put(path+"/*", TVaultConstants.WRITE_POLICY); policyMap.put("accessid", "w_"+folders[0]+"_"+Safe); policyRequestJson = ControllerUtil.convetToJson(policyMap); Response w_response = reqProcessor.process("/access/update",policyRequestJson,token); accessMap.put(path+"/*", TVaultConstants.DENY_POLICY); policyMap.put("accessid", "d_"+folders[0]+"_"+Safe); policyRequestJson = ControllerUtil.convetToJson(policyMap); Response d_response = reqProcessor.process("/access/update",policyRequestJson,token); accessMap.put(path+"/*", TVaultConstants.SUDO_POLICY); accessMap.put(_path+"/*", TVaultConstants.SUDO_POLICY); policyMap.put("accessid", "s_"+folders[0]+"_"+Safe); policyRequestJson = ControllerUtil.convetToJson(policyMap); Response s_response = reqProcessor.process("/access/update",policyRequestJson,token); if( (r_response.getHttpstatus().equals(HttpStatus.NO_CONTENT) && w_response.getHttpstatus().equals(HttpStatus.NO_CONTENT) && d_response.getHttpstatus().equals(HttpStatus.NO_CONTENT) && s_response.getHttpstatus().equals(HttpStatus.NO_CONTENT) ) || (r_response.getHttpstatus().equals(HttpStatus.OK) && w_response.getHttpstatus().equals(HttpStatus.OK) && d_response.getHttpstatus().equals(HttpStatus.OK)) && s_response.getHttpstatus().equals(HttpStatus.OK) ){ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create SDB"). put(LogMessage.MESSAGE, "SDB Create Success"). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"Safe and associated read/write/deny policies created \"]}"); }else{ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create SDB"). put(LogMessage.MESSAGE, "SDB Create Success"). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.MULTI_STATUS).body("{\"messages\":[\"Safe created however one ore more policy (read/write/deny) creation failed \"]}"); } } if(isMetaDataUpdated) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create SDB"). put(LogMessage.MESSAGE, "SDB Create Success"). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"Safe created \"]}"); } else { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create SDB"). put(LogMessage.MESSAGE, "SDB Create Success"). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"Safe created however metadata update failed. Please try with Safe/update \"]}"); } }else{ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create SDB"). put(LogMessage.MESSAGE, "SDB Create completed"). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(response.getHttpstatus()).body(response.getResponse()); } }else{ log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create SDB"). put(LogMessage.MESSAGE, "SDB Creation failed"). put(LogMessage.RESPONSE, "Invalid Path"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid 'path' specified\"]}"); } }  ResponseEntity<String> getFolders( String token, String path); ResponseEntity<String> getInfo(String token, String path); ResponseEntity<String> createfolder(String token, String path); ResponseEntity<String> createSafe(String token, Safe safe); ResponseEntity<String> getSafe(String token, String path); ResponseEntity<String> deleteSafe(String token, Safe safe, UserDetails userDetails); ResponseEntity<String> updateSafe(String token, Safe safe); ResponseEntity<String> addUserToSafe(String token, SafeUser safeUser, UserDetails userDetails); ResponseEntity<String> addGroupToSafe(String token, SafeGroup safeGroup, UserDetails userDetails); ResponseEntity<String> removeUserFromSafe(String token, SafeUser safeUser, UserDetails userDetails); ResponseEntity<String> removeGroupFromSafe(String token, SafeGroup safeGroup, UserDetails userDetails); ResponseEntity<String> addAwsRoleToSafe(String token, AWSRole awsRole); ResponseEntity<String> removeAWSRoleFromSafe(String token, AWSRole awsRole, boolean detachOnly, UserDetails userDetails); ResponseEntity<String> deletefolder(String token, String path, UserDetails userDetails); ResponseEntity<String> getFoldersRecursively(String token, String path); ResponseEntity<String> createNestedfolder(String token, String path, UserDetails userDetails); ResponseEntity<String> associateApproletoSDB(String token, SafeAppRoleAccess safeAppRoleAccess); ResponseEntity<String> removeApproleFromSafe(String token, String jsonstr);  }
@Test public void test_createSafe_successfully() { String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; SafeBasicDetails safeBasicDetails = new SafeBasicDetails("mysafe01", "youremail@yourcompany.com", null, "My first safe"); Safe safe = new Safe("shared/mysafe01",safeBasicDetails); String jsonStr = "{ \"data\": {\"description\": \"My first safe\", \"name\": \"mysafe01\", \"owner\": \"youremail@yourcompany.com\"}, \"path\": \"shared/mysafe01\"}"; String metadatajson = "{\"path\":\"metadata/shared/mysafe03\",\"data\":{\"name\":\"mysafe03\",\"owner\":\"youremail@yourcompany.com\",\"type\":\"\",\"description\":\"My first safe\"}}"; Response responseNoContent = getMockResponse(HttpStatus.NO_CONTENT, true, ""); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"Safe and associated read/write/deny policies created \"]}"); Map<String,Object> reqparams = null; try { reqparams = new ObjectMapper().readValue(jsonStr, new TypeReference<Map<String, Object>>(){}); } catch (IOException e) { e.printStackTrace(); } when(ControllerUtil.converSDBInputsToLowerCase(JSONUtil.getJSON(safe))).thenReturn(jsonStr); when(ControllerUtil.parseJson(jsonStr)).thenReturn(reqparams); when(ControllerUtil.areSDBInputsValid(safe)).thenReturn(true); when(JSONUtil.getJSON(safe)).thenReturn(jsonStr); when(ControllerUtil.isValidSafePath(any())).thenReturn(true); when(reqProcessor.process("/sdb/create",jsonStr,token)).thenReturn(responseNoContent); when(ControllerUtil.convetToJson(any())).thenReturn(metadatajson); when(reqProcessor.process("/write",metadatajson,token)).thenReturn(responseNoContent); when(reqProcessor.process(eq("/access/update"),any(),eq(token))).thenReturn(responseNoContent); ResponseEntity<String> responseEntity = safesService.createSafe(token, safe); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }
@Test public void test_createSafe_failure_invalid_safename() { String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; SafeBasicDetails safeBasicDetails = new SafeBasicDetails("mysafe01_", "youremail@yourcompany.com", null, "My first safe"); Safe safe = new Safe("shared/mysafe01_",safeBasicDetails); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid Safe name: unexpected character _ in the end\"]}"); when(ControllerUtil.areSDBInputsValid(safe)).thenReturn(true); ResponseEntity<String> responseEntity = safesService.createSafe(token, safe); assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }
@Test public void test_createSafe_failure_safename_too_short() { String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; SafeBasicDetails safeBasicDetails = new SafeBasicDetails("sa", "youremail@yourcompany.com", null, "My first safe"); Safe safe = new Safe("shared/sa",safeBasicDetails); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid Safe name: Please enter minimum 3 characters\"]}"); when(ControllerUtil.areSDBInputsValid(safe)).thenReturn(true); ResponseEntity<String> responseEntity = safesService.createSafe(token, safe); assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }
@Test public void test_createSafe_failure_description_too_short() { String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; SafeBasicDetails safeBasicDetails = new SafeBasicDetails("safe1", "youremail@yourcompany.com", null, "My safe"); Safe safe = new Safe("shared/safe1",safeBasicDetails); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid Description: Please enter minimum 10 characters\"]}"); when(ControllerUtil.areSDBInputsValid(safe)).thenReturn(true); ResponseEntity<String> responseEntity = safesService.createSafe(token, safe); assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }
@Test public void test_createSafe_failure_description_too_long() { String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; SafeBasicDetails safeBasicDetails = new SafeBasicDetails("mysafe01", "youremail@yourcompany.com", null, "My first safe My first safe" + "My first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safe" + "My first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safe" + "My first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safe" + "My first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safe" + "My first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safe" + "My first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safe" + "My first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safe" + "My first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safe"); Safe safe = new Safe("shared/mysafe01",safeBasicDetails); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid input values: Description too long\"]}"); when(ControllerUtil.areSDBInputsValid(safe)).thenReturn(true); ResponseEntity<String> responseEntity = safesService.createSafe(token, safe); assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }
@Test public void test_createSafe_failure_policies_creation() { String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; SafeBasicDetails safeBasicDetails = new SafeBasicDetails("mysafe01", "youremail@yourcompany.com", null, "My first safe"); Safe safe = new Safe("shared/mysafe01",safeBasicDetails); String jsonStr = "{ \"data\": {\"description\": \"My first safe\", \"name\": \"mysafe01\", \"owner\": \"youremail@yourcompany.com\"}, \"path\": \"shared/mysafe01\"}"; String metadatajson = "{\"path\":\"metadata/shared/mysafe03\",\"data\":{\"name\":\"mysafe03\",\"owner\":\"youremail@yourcompany.com\",\"type\":\"\",\"description\":\"My first safe\"}}"; Response responseNoContent = getMockResponse(HttpStatus.NO_CONTENT, true, ""); Response responseBadRequest = getMockResponse(HttpStatus.BAD_REQUEST, true, ""); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.MULTI_STATUS).body("{\"messages\":[\"Safe created however one ore more policy (read/write/deny) creation failed \"]}"); Map<String,Object> reqparams = null; try { reqparams = new ObjectMapper().readValue(jsonStr, new TypeReference<Map<String, Object>>(){}); } catch (IOException e) { e.printStackTrace(); } when(ControllerUtil.converSDBInputsToLowerCase(JSONUtil.getJSON(safe))).thenReturn(jsonStr); when(ControllerUtil.parseJson(jsonStr)).thenReturn(reqparams); when(ControllerUtil.areSDBInputsValid(safe)).thenReturn(true); when(JSONUtil.getJSON(safe)).thenReturn(jsonStr); when(ControllerUtil.isValidSafePath(any())).thenReturn(true); when(reqProcessor.process("/sdb/create",jsonStr,token)).thenReturn(responseNoContent); when(ControllerUtil.convetToJson(any())).thenReturn(metadatajson); when(reqProcessor.process("/write",metadatajson,token)).thenReturn(responseNoContent); when(reqProcessor.process(eq("/access/update"),any(),eq(token))).thenReturn(responseBadRequest); ResponseEntity<String> responseEntity = safesService.createSafe(token, safe); assertEquals(HttpStatus.MULTI_STATUS, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }
@Test public void test_createSafe_failure_400() { String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; SafeBasicDetails safeBasicDetails = new SafeBasicDetails("mysafe01", "youremail@yourcompany.com", null, "My first safe"); Safe safe = new Safe("shared/mysafe01",safeBasicDetails); String jsonStr = "{ \"data\": {\"description\": \"My first safe\", \"name\": \"mysafe01\", \"owner\": \"youremail@yourcompany.com\"}, \"path\": \"shared/mysafe01\"}"; ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid 'path' specified\"]}"); Map<String,Object> reqparams = null; try { reqparams = new ObjectMapper().readValue(jsonStr, new TypeReference<Map<String, Object>>(){}); } catch (IOException e) { e.printStackTrace(); } when(ControllerUtil.converSDBInputsToLowerCase(JSONUtil.getJSON(safe))).thenReturn(jsonStr); when(ControllerUtil.parseJson(jsonStr)).thenReturn(reqparams); when(ControllerUtil.areSDBInputsValid(safe)).thenReturn(true); when(ControllerUtil.isValidSafePath(any())).thenReturn(false); ResponseEntity<String> responseEntity = safesService.createSafe(token, safe); assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }