@Test public void test_createAppRole_successfully_metadata_failure_reverted() { Response response =getMockResponse(HttpStatus.NO_CONTENT, true, ""); Response response_403 =getMockResponse(HttpStatus.UNAUTHORIZED, true, ""); Response responseList = getMockResponse(HttpStatus.OK, true, "{\"keys\": [ \"role1\" ]}"); Response responseAfterHide = getMockResponse(HttpStatus.OK, true, "{\"keys\": [ \"role1\" ]}"); when(ControllerUtil.hideMasterAppRoleFromResponse(Mockito.any())).thenReturn(responseAfterHide); String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; String [] policies = {"default"}; AppRole appRole = new AppRole("approle1", policies, true, 1, 100, 0); String jsonStr = "{\"role_name\":\"approle1\",\"policies\":[\"default\"],\"bind_secret_id\":true,\"secret_id_num_uses\":\"1\",\"secret_id_ttl\":\"100m\",\"token_num_uses\":0,\"token_ttl\":null,\"token_max_ttl\":null}"; ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"AppRole creation failed.\"]}"); Map<String,Object> appRolesList = new HashMap<>(); ArrayList<String> arrayList = new ArrayList<>(); arrayList.add("role1"); appRolesList.put("keys", arrayList); when(ControllerUtil.parseJson("{\"keys\": [ \"role1\" ]}")).thenReturn(appRolesList); when(reqProcessor.process("/auth/approle/role/create", jsonStr,token)).thenReturn(response); when(reqProcessor.process("/auth/approle/role/list","{}",token)).thenReturn(responseList); when(ControllerUtil.areAppRoleInputsValid(appRole)).thenReturn(true); when(JSONUtil.getJSON(appRole)).thenReturn(jsonStr); when(ControllerUtil.convertAppRoleInputsToLowerCase(Mockito.any())).thenReturn(jsonStr); when(reqProcessor.process("/auth/approle/role/delete",jsonStr,token)).thenReturn(response); UserDetails userDetails = getMockUser(true); when(reqProcessor.process(eq("/write"),Mockito.any(),eq(token))).thenReturn(response_403); ResponseEntity<String> responseEntityActual = appRoleService.createAppRole(token, appRole, userDetails); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntityActual.getStatusCode()); assertEquals(responseEntityExpected, responseEntityActual); }
public ResponseEntity<String> createAppRole(String token, AppRole appRole, UserDetails userDetails){ String jsonStr = JSONUtil.getJSON(appRole); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create AppRole"). put(LogMessage.MESSAGE, String.format("Trying to create AppRole [%s]", jsonStr)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); if (!ControllerUtil.areAppRoleInputsValid(appRole)) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid input values for AppRole creation\"]}"); } if (appRole.getRole_name().equals(TVaultConstants.SELF_SERVICE_APPROLE_NAME)) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Access denied: no permission to create an approle named "+TVaultConstants.SELF_SERVICE_APPROLE_NAME+"\"]}"); } jsonStr = ControllerUtil.convertAppRoleInputsToLowerCase(jsonStr); boolean isDuplicate = isAppRoleDuplicate(appRole.getRole_name().toLowerCase(), token); if (!isDuplicate) { Response response = reqProcessor.process("/auth/approle/role/create", jsonStr,token); if(response.getHttpstatus().equals(HttpStatus.NO_CONTENT) || response.getHttpstatus().equals(HttpStatus.OK)) { String metadataJson = ControllerUtil.populateAppRoleMetaJson(appRole.getRole_name(), userDetails.getUsername()); boolean appRoleMetaDataCreationStatus = ControllerUtil.createMetadata(metadataJson, token); String appRoleUsermetadataJson = ControllerUtil.populateUserMetaJson(appRole.getRole_name(), userDetails.getUsername()); boolean appRoleUserMetaDataCreationStatus = ControllerUtil.createMetadata(appRoleUsermetadataJson, token); if(appRoleMetaDataCreationStatus && appRoleUserMetaDataCreationStatus) { return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"AppRole created successfully\"]}"); } Response deleteResponse = reqProcessor.process("/auth/approle/role/delete",jsonStr,token); if (deleteResponse.getHttpstatus().equals(HttpStatus.NO_CONTENT)) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"AppRole creation failed.\"]}"); } else { return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"AppRole created however metadata update failed. Please try with AppRole/update \"]}"); } } log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create AppRole"). put(LogMessage.MESSAGE, "Creation of AppRole failed"). put(LogMessage.RESPONSE, response.getResponse()). 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 { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"AppRole already exists and can't be created\"]}"); } }
AppRoleService { public ResponseEntity<String> createAppRole(String token, AppRole appRole, UserDetails userDetails){ String jsonStr = JSONUtil.getJSON(appRole); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create AppRole"). put(LogMessage.MESSAGE, String.format("Trying to create AppRole [%s]", jsonStr)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); if (!ControllerUtil.areAppRoleInputsValid(appRole)) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid input values for AppRole creation\"]}"); } if (appRole.getRole_name().equals(TVaultConstants.SELF_SERVICE_APPROLE_NAME)) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Access denied: no permission to create an approle named "+TVaultConstants.SELF_SERVICE_APPROLE_NAME+"\"]}"); } jsonStr = ControllerUtil.convertAppRoleInputsToLowerCase(jsonStr); boolean isDuplicate = isAppRoleDuplicate(appRole.getRole_name().toLowerCase(), token); if (!isDuplicate) { Response response = reqProcessor.process("/auth/approle/role/create", jsonStr,token); if(response.getHttpstatus().equals(HttpStatus.NO_CONTENT) || response.getHttpstatus().equals(HttpStatus.OK)) { String metadataJson = ControllerUtil.populateAppRoleMetaJson(appRole.getRole_name(), userDetails.getUsername()); boolean appRoleMetaDataCreationStatus = ControllerUtil.createMetadata(metadataJson, token); String appRoleUsermetadataJson = ControllerUtil.populateUserMetaJson(appRole.getRole_name(), userDetails.getUsername()); boolean appRoleUserMetaDataCreationStatus = ControllerUtil.createMetadata(appRoleUsermetadataJson, token); if(appRoleMetaDataCreationStatus && appRoleUserMetaDataCreationStatus) { return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"AppRole created successfully\"]}"); } Response deleteResponse = reqProcessor.process("/auth/approle/role/delete",jsonStr,token); if (deleteResponse.getHttpstatus().equals(HttpStatus.NO_CONTENT)) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"AppRole creation failed.\"]}"); } else { return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"AppRole created however metadata update failed. Please try with AppRole/update \"]}"); } } log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create AppRole"). put(LogMessage.MESSAGE, "Creation of AppRole failed"). put(LogMessage.RESPONSE, response.getResponse()). 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 { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"AppRole already exists and can't be created\"]}"); } } }
AppRoleService { public ResponseEntity<String> createAppRole(String token, AppRole appRole, UserDetails userDetails){ String jsonStr = JSONUtil.getJSON(appRole); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create AppRole"). put(LogMessage.MESSAGE, String.format("Trying to create AppRole [%s]", jsonStr)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); if (!ControllerUtil.areAppRoleInputsValid(appRole)) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid input values for AppRole creation\"]}"); } if (appRole.getRole_name().equals(TVaultConstants.SELF_SERVICE_APPROLE_NAME)) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Access denied: no permission to create an approle named "+TVaultConstants.SELF_SERVICE_APPROLE_NAME+"\"]}"); } jsonStr = ControllerUtil.convertAppRoleInputsToLowerCase(jsonStr); boolean isDuplicate = isAppRoleDuplicate(appRole.getRole_name().toLowerCase(), token); if (!isDuplicate) { Response response = reqProcessor.process("/auth/approle/role/create", jsonStr,token); if(response.getHttpstatus().equals(HttpStatus.NO_CONTENT) || response.getHttpstatus().equals(HttpStatus.OK)) { String metadataJson = ControllerUtil.populateAppRoleMetaJson(appRole.getRole_name(), userDetails.getUsername()); boolean appRoleMetaDataCreationStatus = ControllerUtil.createMetadata(metadataJson, token); String appRoleUsermetadataJson = ControllerUtil.populateUserMetaJson(appRole.getRole_name(), userDetails.getUsername()); boolean appRoleUserMetaDataCreationStatus = ControllerUtil.createMetadata(appRoleUsermetadataJson, token); if(appRoleMetaDataCreationStatus && appRoleUserMetaDataCreationStatus) { return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"AppRole created successfully\"]}"); } Response deleteResponse = reqProcessor.process("/auth/approle/role/delete",jsonStr,token); if (deleteResponse.getHttpstatus().equals(HttpStatus.NO_CONTENT)) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"AppRole creation failed.\"]}"); } else { return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"AppRole created however metadata update failed. Please try with AppRole/update \"]}"); } } log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create AppRole"). put(LogMessage.MESSAGE, "Creation of AppRole failed"). put(LogMessage.RESPONSE, response.getResponse()). 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 { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"AppRole already exists and can't be created\"]}"); } }  }
AppRoleService { public ResponseEntity<String> createAppRole(String token, AppRole appRole, UserDetails userDetails){ String jsonStr = JSONUtil.getJSON(appRole); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create AppRole"). put(LogMessage.MESSAGE, String.format("Trying to create AppRole [%s]", jsonStr)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); if (!ControllerUtil.areAppRoleInputsValid(appRole)) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid input values for AppRole creation\"]}"); } if (appRole.getRole_name().equals(TVaultConstants.SELF_SERVICE_APPROLE_NAME)) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Access denied: no permission to create an approle named "+TVaultConstants.SELF_SERVICE_APPROLE_NAME+"\"]}"); } jsonStr = ControllerUtil.convertAppRoleInputsToLowerCase(jsonStr); boolean isDuplicate = isAppRoleDuplicate(appRole.getRole_name().toLowerCase(), token); if (!isDuplicate) { Response response = reqProcessor.process("/auth/approle/role/create", jsonStr,token); if(response.getHttpstatus().equals(HttpStatus.NO_CONTENT) || response.getHttpstatus().equals(HttpStatus.OK)) { String metadataJson = ControllerUtil.populateAppRoleMetaJson(appRole.getRole_name(), userDetails.getUsername()); boolean appRoleMetaDataCreationStatus = ControllerUtil.createMetadata(metadataJson, token); String appRoleUsermetadataJson = ControllerUtil.populateUserMetaJson(appRole.getRole_name(), userDetails.getUsername()); boolean appRoleUserMetaDataCreationStatus = ControllerUtil.createMetadata(appRoleUsermetadataJson, token); if(appRoleMetaDataCreationStatus && appRoleUserMetaDataCreationStatus) { return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"AppRole created successfully\"]}"); } Response deleteResponse = reqProcessor.process("/auth/approle/role/delete",jsonStr,token); if (deleteResponse.getHttpstatus().equals(HttpStatus.NO_CONTENT)) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"AppRole creation failed.\"]}"); } else { return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"AppRole created however metadata update failed. Please try with AppRole/update \"]}"); } } log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create AppRole"). put(LogMessage.MESSAGE, "Creation of AppRole failed"). put(LogMessage.RESPONSE, response.getResponse()). 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 { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"AppRole already exists and can't be created\"]}"); } }  ResponseEntity<String> createAppRole(String token, AppRole appRole, UserDetails userDetails); ResponseEntity<String> readAppRole(String token, String rolename); ResponseEntity<String> readAppRoles(String token); ResponseEntity<String> listAppRoles(String token,  UserDetails userDetails); ResponseEntity<String> readAppRoleRoleId(String token, String rolename); String readRoleId(String token, String rolename); List<String> readAccessorIds(String token, String rolename); AppRoleMetadata readAppRoleMetadata(String token, String rolename); AppRole readAppRoleBasicDetails(String token, String rolename); ResponseEntity<String> readAppRoleRoleId(String token, String rolename, UserDetails userDetails); ResponseEntity<String> readAppRoleSecretId(String token, String rolename, UserDetails userDetails); ResponseEntity<String> readSecretIdAccessors(String token, String rolename, UserDetails userDetails); ResponseEntity<String> readAppRoleDetails(String token, String rolename, UserDetails userDetails); ResponseEntity<String> createsecretId(String token, AppRoleSecretData appRoleSecretData); ResponseEntity<String> readAppRoleSecretId(String token, String rolename); ResponseEntity<String> deleteSecretId(String token, AppRoleNameSecretId appRoleNameSecretId); ResponseEntity<String> deleteAppRole(String token, AppRole appRole, UserDetails userDetails); ResponseEntity<String> deleteSecretIds(String token, AppRoleAccessorIds appRoleAccessorIds, UserDetails userDetails); ResponseEntity<String> login(AppRoleIdSecretId appRoleIdSecretId); ResponseEntity<String> associateApprole(String token, SafeAppRoleAccess safeAppRoleAccess); ResponseEntity<String> updateAppRole(String token, AppRole appRole, UserDetails userDetails); Response configureApprole(String rolename,String policies,String token ); }
AppRoleService { public ResponseEntity<String> createAppRole(String token, AppRole appRole, UserDetails userDetails){ String jsonStr = JSONUtil.getJSON(appRole); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create AppRole"). put(LogMessage.MESSAGE, String.format("Trying to create AppRole [%s]", jsonStr)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); if (!ControllerUtil.areAppRoleInputsValid(appRole)) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid input values for AppRole creation\"]}"); } if (appRole.getRole_name().equals(TVaultConstants.SELF_SERVICE_APPROLE_NAME)) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Access denied: no permission to create an approle named "+TVaultConstants.SELF_SERVICE_APPROLE_NAME+"\"]}"); } jsonStr = ControllerUtil.convertAppRoleInputsToLowerCase(jsonStr); boolean isDuplicate = isAppRoleDuplicate(appRole.getRole_name().toLowerCase(), token); if (!isDuplicate) { Response response = reqProcessor.process("/auth/approle/role/create", jsonStr,token); if(response.getHttpstatus().equals(HttpStatus.NO_CONTENT) || response.getHttpstatus().equals(HttpStatus.OK)) { String metadataJson = ControllerUtil.populateAppRoleMetaJson(appRole.getRole_name(), userDetails.getUsername()); boolean appRoleMetaDataCreationStatus = ControllerUtil.createMetadata(metadataJson, token); String appRoleUsermetadataJson = ControllerUtil.populateUserMetaJson(appRole.getRole_name(), userDetails.getUsername()); boolean appRoleUserMetaDataCreationStatus = ControllerUtil.createMetadata(appRoleUsermetadataJson, token); if(appRoleMetaDataCreationStatus && appRoleUserMetaDataCreationStatus) { return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"AppRole created successfully\"]}"); } Response deleteResponse = reqProcessor.process("/auth/approle/role/delete",jsonStr,token); if (deleteResponse.getHttpstatus().equals(HttpStatus.NO_CONTENT)) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"AppRole creation failed.\"]}"); } else { return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"AppRole created however metadata update failed. Please try with AppRole/update \"]}"); } } log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create AppRole"). put(LogMessage.MESSAGE, "Creation of AppRole failed"). put(LogMessage.RESPONSE, response.getResponse()). 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 { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"AppRole already exists and can't be created\"]}"); } }  ResponseEntity<String> createAppRole(String token, AppRole appRole, UserDetails userDetails); ResponseEntity<String> readAppRole(String token, String rolename); ResponseEntity<String> readAppRoles(String token); ResponseEntity<String> listAppRoles(String token,  UserDetails userDetails); ResponseEntity<String> readAppRoleRoleId(String token, String rolename); String readRoleId(String token, String rolename); List<String> readAccessorIds(String token, String rolename); AppRoleMetadata readAppRoleMetadata(String token, String rolename); AppRole readAppRoleBasicDetails(String token, String rolename); ResponseEntity<String> readAppRoleRoleId(String token, String rolename, UserDetails userDetails); ResponseEntity<String> readAppRoleSecretId(String token, String rolename, UserDetails userDetails); ResponseEntity<String> readSecretIdAccessors(String token, String rolename, UserDetails userDetails); ResponseEntity<String> readAppRoleDetails(String token, String rolename, UserDetails userDetails); ResponseEntity<String> createsecretId(String token, AppRoleSecretData appRoleSecretData); ResponseEntity<String> readAppRoleSecretId(String token, String rolename); ResponseEntity<String> deleteSecretId(String token, AppRoleNameSecretId appRoleNameSecretId); ResponseEntity<String> deleteAppRole(String token, AppRole appRole, UserDetails userDetails); ResponseEntity<String> deleteSecretIds(String token, AppRoleAccessorIds appRoleAccessorIds, UserDetails userDetails); ResponseEntity<String> login(AppRoleIdSecretId appRoleIdSecretId); ResponseEntity<String> associateApprole(String token, SafeAppRoleAccess safeAppRoleAccess); ResponseEntity<String> updateAppRole(String token, AppRole appRole, UserDetails userDetails); Response configureApprole(String rolename,String policies,String token );  }