@Test public void test_removeGroupFromServiceAccount_metadata_failure() { String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; ServiceAccountGroup serviceAccountGroup = new ServiceAccountGroup("svc_vault_test7", "group1", "reset"); UserDetails userDetails = getMockUser(false); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Group configuration failed. Please try again\"]}"); ResponseEntity<String> response = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Group configuration failed. Please try again\"]}"); Response responseNoContent = getMockResponse(HttpStatus.NO_CONTENT, true, ""); Response response404 = getMockResponse(HttpStatus.NOT_FOUND, true, ""); String [] policies = {"o_svcacct_svc_vault_test7"}; when(policyUtils.getCurrentPolicies(token, userDetails.getUsername(), userDetails)).thenReturn(policies); Response groupResp = getMockResponse(HttpStatus.OK, true, "{\"data\":{\"bound_cidrs\":[],\"max_ttl\":0,\"policies\":[\"default\",\"w_shared_mysafe01\",\"w_shared_mysafe02\"],\"ttl\":0,\"groups\":\"admin\"}}"); when(reqProcessor.process("/auth/ldap/groups","{\"groupname\":\"group1\"}",token)).thenReturn(groupResp); ObjectMapper objMapper = new ObjectMapper(); String responseJson = groupResp.getResponse(); try { List<String> resList = new ArrayList<>(); resList.add("default"); resList.add("w_shared_mysafe01"); resList.add("w_shared_mysafe02"); when(ControllerUtil.getPoliciesAsListFromJson(objMapper, responseJson)).thenReturn(resList); } catch (IOException e) { e.printStackTrace(); } when(ControllerUtil.configureLDAPGroup(any(),any(),any())).thenReturn(responseNoContent); when(ControllerUtil.updateMetadata(any(),eq(token))).thenReturn(response404); when(tokenUtils.getSelfServiceToken()).thenReturn(token); when(reqProcessor.process(eq("/sdb"),Mockito.any(),eq(token))).thenReturn(getMockResponse(HttpStatus.OK, true, "{\"data\":{\"initialPasswordReset\":true,\"managedBy\":\"smohan11\",\"name\":\"svc_vault_test5\",\"users\":{\"smohan11\":\"sudo\"}}}")); ResponseEntity<String> responseEntity = serviceAccountsService.removeGroupFromServiceAccount(token, serviceAccountGroup, userDetails); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }
public ResponseEntity<String> removeGroupFromServiceAccount(String token, ServiceAccountGroup serviceAccountGroup, UserDetails userDetails) { OIDCGroup oidcGroup = new OIDCGroup(); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Remove Group from Service Account"). put(LogMessage.MESSAGE, String.format ("Trying to remove Group from Service Account")). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); if (!userDetails.isAdmin()) { token = tokenUtils.getSelfServiceToken(); } if(!isSvcaccPermissionInputValid(serviceAccountGroup.getAccess())) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid value specified for access. Valid values are read, reset, deny\"]}"); } if (serviceAccountGroup.getAccess().equalsIgnoreCase("reset")) { serviceAccountGroup.setAccess(TVaultConstants.WRITE_POLICY); } String groupName = serviceAccountGroup.getGroupname().toLowerCase(); String svcAccName = serviceAccountGroup.getSvcAccName(); String access = serviceAccountGroup.getAccess(); boolean isAuthorized = true; if (userDetails != null) { isAuthorized = hasAddOrRemovePermission(userDetails, svcAccName, token); } if(isAuthorized){ if (!ifInitialPwdReset(token, userDetails, serviceAccountGroup.getSvcAccName())) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Remove Group from ServiceAccount"). put(LogMessage.MESSAGE, "Failed to remove group permission from Service account. Initial password reset is pending for this Service Account."). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Failed to remove group permission from Service account. Initial password reset is pending for this Service Account. Please reset the password and try again.\"]}"); } String r_policy = new StringBuffer().append(TVaultConstants.SVC_ACC_POLICIES_PREFIXES.getKey(TVaultConstants.READ_POLICY)).append(TVaultConstants.SVC_ACC_PATH_PREFIX).append("_").append(svcAccName).toString(); String w_policy = new StringBuffer().append(TVaultConstants.SVC_ACC_POLICIES_PREFIXES.getKey(TVaultConstants.WRITE_POLICY)).append(TVaultConstants.SVC_ACC_PATH_PREFIX).append("_").append(svcAccName).toString(); String d_policy = new StringBuffer().append(TVaultConstants.SVC_ACC_POLICIES_PREFIXES.getKey(TVaultConstants.DENY_POLICY)).append(TVaultConstants.SVC_ACC_PATH_PREFIX).append("_").append(svcAccName).toString(); String o_policy = new StringBuffer().append(TVaultConstants.SVC_ACC_POLICIES_PREFIXES.getKey(TVaultConstants.SUDO_POLICY)).append(TVaultConstants.SVC_ACC_PATH_PREFIX).append("_").append(svcAccName).toString(); log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Remove group from Service Account"). put(LogMessage.MESSAGE, String.format ("Policies are, read - [%s], write - [%s], deny -[%s], owner - [%s]", r_policy, w_policy, d_policy, o_policy)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); Response groupResp = new Response(); if (TVaultConstants.LDAP.equals(vaultAuthMethod)) { groupResp = reqProcessor.process("/auth/ldap/groups", "{\"groupname\":\"" + groupName + "\"}", token); } else if (TVaultConstants.OIDC.equals(vaultAuthMethod)) { oidcGroup = oidcUtil.getIdentityGroupDetails(groupName, token); if (oidcGroup != null) { groupResp.setHttpstatus(HttpStatus.OK); groupResp.setResponse(oidcGroup.getPolicies().toString()); } else { groupResp.setHttpstatus(HttpStatus.BAD_REQUEST); } } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Remove group from ServiceAccount"). put(LogMessage.MESSAGE, String.format ("userResponse status is [%s]", groupResp.getHttpstatus())). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); String responseJson=""; List<String> policies = new ArrayList<>(); List<String> currentpolicies = new ArrayList<>(); String policy = new StringBuffer().append(TVaultConstants.SVC_ACC_POLICIES_PREFIXES.getKey(access)).append(TVaultConstants.SVC_ACC_PATH_PREFIX).append("_").append(svcAccName).toString(); if(HttpStatus.OK.equals(groupResp.getHttpstatus())){ responseJson = groupResp.getResponse(); try { ObjectMapper objMapper = new ObjectMapper(); if (TVaultConstants.LDAP.equals(vaultAuthMethod)) { currentpolicies = ControllerUtil.getPoliciesAsListFromJson(objMapper, responseJson); } else if (TVaultConstants.OIDC.equals(vaultAuthMethod)) { currentpolicies.addAll(oidcGroup.getPolicies()); } } 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, "Remove group from ServiceAccount"). put(LogMessage.MESSAGE, String.format ("Exception while creating currentpolicies or groups")). put(LogMessage.STACKTRACE, Arrays.toString(e.getStackTrace())). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); } policies.addAll(currentpolicies); policies.remove(r_policy); policies.remove(w_policy); policies.remove(d_policy); } String policiesString = org.apache.commons.lang3.StringUtils.join(policies, ","); String currentpoliciesString = org.apache.commons.lang3.StringUtils.join(currentpolicies, ","); Response ldapConfigresponse = new Response(); if (TVaultConstants.LDAP.equals(vaultAuthMethod)) { ldapConfigresponse = ControllerUtil.configureLDAPGroup(groupName, policiesString, token); } else if (TVaultConstants.OIDC.equals(vaultAuthMethod)) { ldapConfigresponse = oidcUtil.updateGroupPolicies(token, groupName, policies, currentpolicies, oidcGroup.getId()); oidcUtil.renewUserToken(userDetails.getClientToken()); } if(ldapConfigresponse.getHttpstatus().equals(HttpStatus.NO_CONTENT) || ldapConfigresponse.getHttpstatus().equals(HttpStatus.OK)){ String path = new StringBuffer(TVaultConstants.SVC_ACC_ROLES_PATH).append(svcAccName).toString(); Map<String,String> params = new HashMap<String,String>(); params.put("type", "groups"); params.put("name",groupName); params.put("path",path); params.put("access","delete"); Response metadataResponse = ControllerUtil.updateMetadata(params,token); if(metadataResponse !=null && (HttpStatus.NO_CONTENT.equals(metadataResponse.getHttpstatus()) || HttpStatus.OK.equals(metadataResponse.getHttpstatus()))){ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Remove Group to Service Account"). put(LogMessage.MESSAGE, "Group configuration Success."). put(LogMessage.STATUS, metadataResponse.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"Group is successfully removed from Service Account\"]}"); } if (TVaultConstants.LDAP.equals(vaultAuthMethod)) { ldapConfigresponse = ControllerUtil.configureLDAPGroup(groupName, currentpoliciesString, token); } else if (TVaultConstants.OIDC.equals(vaultAuthMethod)) { ldapConfigresponse = oidcUtil.updateGroupPolicies(token, groupName, currentpolicies, currentpolicies, oidcGroup.getId()); oidcUtil.renewUserToken(userDetails.getClientToken()); } if(ldapConfigresponse.getHttpstatus().equals(HttpStatus.NO_CONTENT)){ log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Add Group to Service Account"). put(LogMessage.MESSAGE, "Reverting, group policy update success"). put(LogMessage.RESPONSE, (null!=metadataResponse)?metadataResponse.getResponse():TVaultConstants.EMPTY). put(LogMessage.STATUS, (null!=metadataResponse)?metadataResponse.getHttpstatus().toString():TVaultConstants.EMPTY). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Group configuration failed. Please try again\"]}"); }else{ log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Add Group to Service Account"). put(LogMessage.MESSAGE, "Reverting group policy update failed"). put(LogMessage.RESPONSE, (null!=metadataResponse)?metadataResponse.getResponse():TVaultConstants.EMPTY). put(LogMessage.STATUS, (null!=metadataResponse)?metadataResponse.getHttpstatus().toString():TVaultConstants.EMPTY). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Group configuration failed. Contact Admin \"]}"); } } else { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Failed to remove the group from the Service Account\"]}"); } } else { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Access denied: No permission to remove groups from this service account\"]}"); } }
ServiceAccountsService { public ResponseEntity<String> removeGroupFromServiceAccount(String token, ServiceAccountGroup serviceAccountGroup, UserDetails userDetails) { OIDCGroup oidcGroup = new OIDCGroup(); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Remove Group from Service Account"). put(LogMessage.MESSAGE, String.format ("Trying to remove Group from Service Account")). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); if (!userDetails.isAdmin()) { token = tokenUtils.getSelfServiceToken(); } if(!isSvcaccPermissionInputValid(serviceAccountGroup.getAccess())) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid value specified for access. Valid values are read, reset, deny\"]}"); } if (serviceAccountGroup.getAccess().equalsIgnoreCase("reset")) { serviceAccountGroup.setAccess(TVaultConstants.WRITE_POLICY); } String groupName = serviceAccountGroup.getGroupname().toLowerCase(); String svcAccName = serviceAccountGroup.getSvcAccName(); String access = serviceAccountGroup.getAccess(); boolean isAuthorized = true; if (userDetails != null) { isAuthorized = hasAddOrRemovePermission(userDetails, svcAccName, token); } if(isAuthorized){ if (!ifInitialPwdReset(token, userDetails, serviceAccountGroup.getSvcAccName())) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Remove Group from ServiceAccount"). put(LogMessage.MESSAGE, "Failed to remove group permission from Service account. Initial password reset is pending for this Service Account."). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Failed to remove group permission from Service account. Initial password reset is pending for this Service Account. Please reset the password and try again.\"]}"); } String r_policy = new StringBuffer().append(TVaultConstants.SVC_ACC_POLICIES_PREFIXES.getKey(TVaultConstants.READ_POLICY)).append(TVaultConstants.SVC_ACC_PATH_PREFIX).append("_").append(svcAccName).toString(); String w_policy = new StringBuffer().append(TVaultConstants.SVC_ACC_POLICIES_PREFIXES.getKey(TVaultConstants.WRITE_POLICY)).append(TVaultConstants.SVC_ACC_PATH_PREFIX).append("_").append(svcAccName).toString(); String d_policy = new StringBuffer().append(TVaultConstants.SVC_ACC_POLICIES_PREFIXES.getKey(TVaultConstants.DENY_POLICY)).append(TVaultConstants.SVC_ACC_PATH_PREFIX).append("_").append(svcAccName).toString(); String o_policy = new StringBuffer().append(TVaultConstants.SVC_ACC_POLICIES_PREFIXES.getKey(TVaultConstants.SUDO_POLICY)).append(TVaultConstants.SVC_ACC_PATH_PREFIX).append("_").append(svcAccName).toString(); log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Remove group from Service Account"). put(LogMessage.MESSAGE, String.format ("Policies are, read - [%s], write - [%s], deny -[%s], owner - [%s]", r_policy, w_policy, d_policy, o_policy)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); Response groupResp = new Response(); if (TVaultConstants.LDAP.equals(vaultAuthMethod)) { groupResp = reqProcessor.process("/auth/ldap/groups", "{\"groupname\":\"" + groupName + "\"}", token); } else if (TVaultConstants.OIDC.equals(vaultAuthMethod)) { oidcGroup = oidcUtil.getIdentityGroupDetails(groupName, token); if (oidcGroup != null) { groupResp.setHttpstatus(HttpStatus.OK); groupResp.setResponse(oidcGroup.getPolicies().toString()); } else { groupResp.setHttpstatus(HttpStatus.BAD_REQUEST); } } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Remove group from ServiceAccount"). put(LogMessage.MESSAGE, String.format ("userResponse status is [%s]", groupResp.getHttpstatus())). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); String responseJson=""; List<String> policies = new ArrayList<>(); List<String> currentpolicies = new ArrayList<>(); String policy = new StringBuffer().append(TVaultConstants.SVC_ACC_POLICIES_PREFIXES.getKey(access)).append(TVaultConstants.SVC_ACC_PATH_PREFIX).append("_").append(svcAccName).toString(); if(HttpStatus.OK.equals(groupResp.getHttpstatus())){ responseJson = groupResp.getResponse(); try { ObjectMapper objMapper = new ObjectMapper(); if (TVaultConstants.LDAP.equals(vaultAuthMethod)) { currentpolicies = ControllerUtil.getPoliciesAsListFromJson(objMapper, responseJson); } else if (TVaultConstants.OIDC.equals(vaultAuthMethod)) { currentpolicies.addAll(oidcGroup.getPolicies()); } } 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, "Remove group from ServiceAccount"). put(LogMessage.MESSAGE, String.format ("Exception while creating currentpolicies or groups")). put(LogMessage.STACKTRACE, Arrays.toString(e.getStackTrace())). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); } policies.addAll(currentpolicies); policies.remove(r_policy); policies.remove(w_policy); policies.remove(d_policy); } String policiesString = org.apache.commons.lang3.StringUtils.join(policies, ","); String currentpoliciesString = org.apache.commons.lang3.StringUtils.join(currentpolicies, ","); Response ldapConfigresponse = new Response(); if (TVaultConstants.LDAP.equals(vaultAuthMethod)) { ldapConfigresponse = ControllerUtil.configureLDAPGroup(groupName, policiesString, token); } else if (TVaultConstants.OIDC.equals(vaultAuthMethod)) { ldapConfigresponse = oidcUtil.updateGroupPolicies(token, groupName, policies, currentpolicies, oidcGroup.getId()); oidcUtil.renewUserToken(userDetails.getClientToken()); } if(ldapConfigresponse.getHttpstatus().equals(HttpStatus.NO_CONTENT) || ldapConfigresponse.getHttpstatus().equals(HttpStatus.OK)){ String path = new StringBuffer(TVaultConstants.SVC_ACC_ROLES_PATH).append(svcAccName).toString(); Map<String,String> params = new HashMap<String,String>(); params.put("type", "groups"); params.put("name",groupName); params.put("path",path); params.put("access","delete"); Response metadataResponse = ControllerUtil.updateMetadata(params,token); if(metadataResponse !=null && (HttpStatus.NO_CONTENT.equals(metadataResponse.getHttpstatus()) || HttpStatus.OK.equals(metadataResponse.getHttpstatus()))){ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Remove Group to Service Account"). put(LogMessage.MESSAGE, "Group configuration Success."). put(LogMessage.STATUS, metadataResponse.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"Group is successfully removed from Service Account\"]}"); } if (TVaultConstants.LDAP.equals(vaultAuthMethod)) { ldapConfigresponse = ControllerUtil.configureLDAPGroup(groupName, currentpoliciesString, token); } else if (TVaultConstants.OIDC.equals(vaultAuthMethod)) { ldapConfigresponse = oidcUtil.updateGroupPolicies(token, groupName, currentpolicies, currentpolicies, oidcGroup.getId()); oidcUtil.renewUserToken(userDetails.getClientToken()); } if(ldapConfigresponse.getHttpstatus().equals(HttpStatus.NO_CONTENT)){ log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Add Group to Service Account"). put(LogMessage.MESSAGE, "Reverting, group policy update success"). put(LogMessage.RESPONSE, (null!=metadataResponse)?metadataResponse.getResponse():TVaultConstants.EMPTY). put(LogMessage.STATUS, (null!=metadataResponse)?metadataResponse.getHttpstatus().toString():TVaultConstants.EMPTY). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Group configuration failed. Please try again\"]}"); }else{ log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Add Group to Service Account"). put(LogMessage.MESSAGE, "Reverting group policy update failed"). put(LogMessage.RESPONSE, (null!=metadataResponse)?metadataResponse.getResponse():TVaultConstants.EMPTY). put(LogMessage.STATUS, (null!=metadataResponse)?metadataResponse.getHttpstatus().toString():TVaultConstants.EMPTY). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Group configuration failed. Contact Admin \"]}"); } } else { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Failed to remove the group from the Service Account\"]}"); } } else { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Access denied: No permission to remove groups from this service account\"]}"); } } }
ServiceAccountsService { public ResponseEntity<String> removeGroupFromServiceAccount(String token, ServiceAccountGroup serviceAccountGroup, UserDetails userDetails) { OIDCGroup oidcGroup = new OIDCGroup(); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Remove Group from Service Account"). put(LogMessage.MESSAGE, String.format ("Trying to remove Group from Service Account")). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); if (!userDetails.isAdmin()) { token = tokenUtils.getSelfServiceToken(); } if(!isSvcaccPermissionInputValid(serviceAccountGroup.getAccess())) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid value specified for access. Valid values are read, reset, deny\"]}"); } if (serviceAccountGroup.getAccess().equalsIgnoreCase("reset")) { serviceAccountGroup.setAccess(TVaultConstants.WRITE_POLICY); } String groupName = serviceAccountGroup.getGroupname().toLowerCase(); String svcAccName = serviceAccountGroup.getSvcAccName(); String access = serviceAccountGroup.getAccess(); boolean isAuthorized = true; if (userDetails != null) { isAuthorized = hasAddOrRemovePermission(userDetails, svcAccName, token); } if(isAuthorized){ if (!ifInitialPwdReset(token, userDetails, serviceAccountGroup.getSvcAccName())) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Remove Group from ServiceAccount"). put(LogMessage.MESSAGE, "Failed to remove group permission from Service account. Initial password reset is pending for this Service Account."). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Failed to remove group permission from Service account. Initial password reset is pending for this Service Account. Please reset the password and try again.\"]}"); } String r_policy = new StringBuffer().append(TVaultConstants.SVC_ACC_POLICIES_PREFIXES.getKey(TVaultConstants.READ_POLICY)).append(TVaultConstants.SVC_ACC_PATH_PREFIX).append("_").append(svcAccName).toString(); String w_policy = new StringBuffer().append(TVaultConstants.SVC_ACC_POLICIES_PREFIXES.getKey(TVaultConstants.WRITE_POLICY)).append(TVaultConstants.SVC_ACC_PATH_PREFIX).append("_").append(svcAccName).toString(); String d_policy = new StringBuffer().append(TVaultConstants.SVC_ACC_POLICIES_PREFIXES.getKey(TVaultConstants.DENY_POLICY)).append(TVaultConstants.SVC_ACC_PATH_PREFIX).append("_").append(svcAccName).toString(); String o_policy = new StringBuffer().append(TVaultConstants.SVC_ACC_POLICIES_PREFIXES.getKey(TVaultConstants.SUDO_POLICY)).append(TVaultConstants.SVC_ACC_PATH_PREFIX).append("_").append(svcAccName).toString(); log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Remove group from Service Account"). put(LogMessage.MESSAGE, String.format ("Policies are, read - [%s], write - [%s], deny -[%s], owner - [%s]", r_policy, w_policy, d_policy, o_policy)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); Response groupResp = new Response(); if (TVaultConstants.LDAP.equals(vaultAuthMethod)) { groupResp = reqProcessor.process("/auth/ldap/groups", "{\"groupname\":\"" + groupName + "\"}", token); } else if (TVaultConstants.OIDC.equals(vaultAuthMethod)) { oidcGroup = oidcUtil.getIdentityGroupDetails(groupName, token); if (oidcGroup != null) { groupResp.setHttpstatus(HttpStatus.OK); groupResp.setResponse(oidcGroup.getPolicies().toString()); } else { groupResp.setHttpstatus(HttpStatus.BAD_REQUEST); } } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Remove group from ServiceAccount"). put(LogMessage.MESSAGE, String.format ("userResponse status is [%s]", groupResp.getHttpstatus())). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); String responseJson=""; List<String> policies = new ArrayList<>(); List<String> currentpolicies = new ArrayList<>(); String policy = new StringBuffer().append(TVaultConstants.SVC_ACC_POLICIES_PREFIXES.getKey(access)).append(TVaultConstants.SVC_ACC_PATH_PREFIX).append("_").append(svcAccName).toString(); if(HttpStatus.OK.equals(groupResp.getHttpstatus())){ responseJson = groupResp.getResponse(); try { ObjectMapper objMapper = new ObjectMapper(); if (TVaultConstants.LDAP.equals(vaultAuthMethod)) { currentpolicies = ControllerUtil.getPoliciesAsListFromJson(objMapper, responseJson); } else if (TVaultConstants.OIDC.equals(vaultAuthMethod)) { currentpolicies.addAll(oidcGroup.getPolicies()); } } 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, "Remove group from ServiceAccount"). put(LogMessage.MESSAGE, String.format ("Exception while creating currentpolicies or groups")). put(LogMessage.STACKTRACE, Arrays.toString(e.getStackTrace())). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); } policies.addAll(currentpolicies); policies.remove(r_policy); policies.remove(w_policy); policies.remove(d_policy); } String policiesString = org.apache.commons.lang3.StringUtils.join(policies, ","); String currentpoliciesString = org.apache.commons.lang3.StringUtils.join(currentpolicies, ","); Response ldapConfigresponse = new Response(); if (TVaultConstants.LDAP.equals(vaultAuthMethod)) { ldapConfigresponse = ControllerUtil.configureLDAPGroup(groupName, policiesString, token); } else if (TVaultConstants.OIDC.equals(vaultAuthMethod)) { ldapConfigresponse = oidcUtil.updateGroupPolicies(token, groupName, policies, currentpolicies, oidcGroup.getId()); oidcUtil.renewUserToken(userDetails.getClientToken()); } if(ldapConfigresponse.getHttpstatus().equals(HttpStatus.NO_CONTENT) || ldapConfigresponse.getHttpstatus().equals(HttpStatus.OK)){ String path = new StringBuffer(TVaultConstants.SVC_ACC_ROLES_PATH).append(svcAccName).toString(); Map<String,String> params = new HashMap<String,String>(); params.put("type", "groups"); params.put("name",groupName); params.put("path",path); params.put("access","delete"); Response metadataResponse = ControllerUtil.updateMetadata(params,token); if(metadataResponse !=null && (HttpStatus.NO_CONTENT.equals(metadataResponse.getHttpstatus()) || HttpStatus.OK.equals(metadataResponse.getHttpstatus()))){ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Remove Group to Service Account"). put(LogMessage.MESSAGE, "Group configuration Success."). put(LogMessage.STATUS, metadataResponse.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"Group is successfully removed from Service Account\"]}"); } if (TVaultConstants.LDAP.equals(vaultAuthMethod)) { ldapConfigresponse = ControllerUtil.configureLDAPGroup(groupName, currentpoliciesString, token); } else if (TVaultConstants.OIDC.equals(vaultAuthMethod)) { ldapConfigresponse = oidcUtil.updateGroupPolicies(token, groupName, currentpolicies, currentpolicies, oidcGroup.getId()); oidcUtil.renewUserToken(userDetails.getClientToken()); } if(ldapConfigresponse.getHttpstatus().equals(HttpStatus.NO_CONTENT)){ log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Add Group to Service Account"). put(LogMessage.MESSAGE, "Reverting, group policy update success"). put(LogMessage.RESPONSE, (null!=metadataResponse)?metadataResponse.getResponse():TVaultConstants.EMPTY). put(LogMessage.STATUS, (null!=metadataResponse)?metadataResponse.getHttpstatus().toString():TVaultConstants.EMPTY). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Group configuration failed. Please try again\"]}"); }else{ log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Add Group to Service Account"). put(LogMessage.MESSAGE, "Reverting group policy update failed"). put(LogMessage.RESPONSE, (null!=metadataResponse)?metadataResponse.getResponse():TVaultConstants.EMPTY). put(LogMessage.STATUS, (null!=metadataResponse)?metadataResponse.getHttpstatus().toString():TVaultConstants.EMPTY). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Group configuration failed. Contact Admin \"]}"); } } else { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Failed to remove the group from the Service Account\"]}"); } } else { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Access denied: No permission to remove groups from this service account\"]}"); } }  }
ServiceAccountsService { public ResponseEntity<String> removeGroupFromServiceAccount(String token, ServiceAccountGroup serviceAccountGroup, UserDetails userDetails) { OIDCGroup oidcGroup = new OIDCGroup(); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Remove Group from Service Account"). put(LogMessage.MESSAGE, String.format ("Trying to remove Group from Service Account")). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); if (!userDetails.isAdmin()) { token = tokenUtils.getSelfServiceToken(); } if(!isSvcaccPermissionInputValid(serviceAccountGroup.getAccess())) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid value specified for access. Valid values are read, reset, deny\"]}"); } if (serviceAccountGroup.getAccess().equalsIgnoreCase("reset")) { serviceAccountGroup.setAccess(TVaultConstants.WRITE_POLICY); } String groupName = serviceAccountGroup.getGroupname().toLowerCase(); String svcAccName = serviceAccountGroup.getSvcAccName(); String access = serviceAccountGroup.getAccess(); boolean isAuthorized = true; if (userDetails != null) { isAuthorized = hasAddOrRemovePermission(userDetails, svcAccName, token); } if(isAuthorized){ if (!ifInitialPwdReset(token, userDetails, serviceAccountGroup.getSvcAccName())) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Remove Group from ServiceAccount"). put(LogMessage.MESSAGE, "Failed to remove group permission from Service account. Initial password reset is pending for this Service Account."). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Failed to remove group permission from Service account. Initial password reset is pending for this Service Account. Please reset the password and try again.\"]}"); } String r_policy = new StringBuffer().append(TVaultConstants.SVC_ACC_POLICIES_PREFIXES.getKey(TVaultConstants.READ_POLICY)).append(TVaultConstants.SVC_ACC_PATH_PREFIX).append("_").append(svcAccName).toString(); String w_policy = new StringBuffer().append(TVaultConstants.SVC_ACC_POLICIES_PREFIXES.getKey(TVaultConstants.WRITE_POLICY)).append(TVaultConstants.SVC_ACC_PATH_PREFIX).append("_").append(svcAccName).toString(); String d_policy = new StringBuffer().append(TVaultConstants.SVC_ACC_POLICIES_PREFIXES.getKey(TVaultConstants.DENY_POLICY)).append(TVaultConstants.SVC_ACC_PATH_PREFIX).append("_").append(svcAccName).toString(); String o_policy = new StringBuffer().append(TVaultConstants.SVC_ACC_POLICIES_PREFIXES.getKey(TVaultConstants.SUDO_POLICY)).append(TVaultConstants.SVC_ACC_PATH_PREFIX).append("_").append(svcAccName).toString(); log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Remove group from Service Account"). put(LogMessage.MESSAGE, String.format ("Policies are, read - [%s], write - [%s], deny -[%s], owner - [%s]", r_policy, w_policy, d_policy, o_policy)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); Response groupResp = new Response(); if (TVaultConstants.LDAP.equals(vaultAuthMethod)) { groupResp = reqProcessor.process("/auth/ldap/groups", "{\"groupname\":\"" + groupName + "\"}", token); } else if (TVaultConstants.OIDC.equals(vaultAuthMethod)) { oidcGroup = oidcUtil.getIdentityGroupDetails(groupName, token); if (oidcGroup != null) { groupResp.setHttpstatus(HttpStatus.OK); groupResp.setResponse(oidcGroup.getPolicies().toString()); } else { groupResp.setHttpstatus(HttpStatus.BAD_REQUEST); } } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Remove group from ServiceAccount"). put(LogMessage.MESSAGE, String.format ("userResponse status is [%s]", groupResp.getHttpstatus())). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); String responseJson=""; List<String> policies = new ArrayList<>(); List<String> currentpolicies = new ArrayList<>(); String policy = new StringBuffer().append(TVaultConstants.SVC_ACC_POLICIES_PREFIXES.getKey(access)).append(TVaultConstants.SVC_ACC_PATH_PREFIX).append("_").append(svcAccName).toString(); if(HttpStatus.OK.equals(groupResp.getHttpstatus())){ responseJson = groupResp.getResponse(); try { ObjectMapper objMapper = new ObjectMapper(); if (TVaultConstants.LDAP.equals(vaultAuthMethod)) { currentpolicies = ControllerUtil.getPoliciesAsListFromJson(objMapper, responseJson); } else if (TVaultConstants.OIDC.equals(vaultAuthMethod)) { currentpolicies.addAll(oidcGroup.getPolicies()); } } 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, "Remove group from ServiceAccount"). put(LogMessage.MESSAGE, String.format ("Exception while creating currentpolicies or groups")). put(LogMessage.STACKTRACE, Arrays.toString(e.getStackTrace())). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); } policies.addAll(currentpolicies); policies.remove(r_policy); policies.remove(w_policy); policies.remove(d_policy); } String policiesString = org.apache.commons.lang3.StringUtils.join(policies, ","); String currentpoliciesString = org.apache.commons.lang3.StringUtils.join(currentpolicies, ","); Response ldapConfigresponse = new Response(); if (TVaultConstants.LDAP.equals(vaultAuthMethod)) { ldapConfigresponse = ControllerUtil.configureLDAPGroup(groupName, policiesString, token); } else if (TVaultConstants.OIDC.equals(vaultAuthMethod)) { ldapConfigresponse = oidcUtil.updateGroupPolicies(token, groupName, policies, currentpolicies, oidcGroup.getId()); oidcUtil.renewUserToken(userDetails.getClientToken()); } if(ldapConfigresponse.getHttpstatus().equals(HttpStatus.NO_CONTENT) || ldapConfigresponse.getHttpstatus().equals(HttpStatus.OK)){ String path = new StringBuffer(TVaultConstants.SVC_ACC_ROLES_PATH).append(svcAccName).toString(); Map<String,String> params = new HashMap<String,String>(); params.put("type", "groups"); params.put("name",groupName); params.put("path",path); params.put("access","delete"); Response metadataResponse = ControllerUtil.updateMetadata(params,token); if(metadataResponse !=null && (HttpStatus.NO_CONTENT.equals(metadataResponse.getHttpstatus()) || HttpStatus.OK.equals(metadataResponse.getHttpstatus()))){ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Remove Group to Service Account"). put(LogMessage.MESSAGE, "Group configuration Success."). put(LogMessage.STATUS, metadataResponse.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"Group is successfully removed from Service Account\"]}"); } if (TVaultConstants.LDAP.equals(vaultAuthMethod)) { ldapConfigresponse = ControllerUtil.configureLDAPGroup(groupName, currentpoliciesString, token); } else if (TVaultConstants.OIDC.equals(vaultAuthMethod)) { ldapConfigresponse = oidcUtil.updateGroupPolicies(token, groupName, currentpolicies, currentpolicies, oidcGroup.getId()); oidcUtil.renewUserToken(userDetails.getClientToken()); } if(ldapConfigresponse.getHttpstatus().equals(HttpStatus.NO_CONTENT)){ log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Add Group to Service Account"). put(LogMessage.MESSAGE, "Reverting, group policy update success"). put(LogMessage.RESPONSE, (null!=metadataResponse)?metadataResponse.getResponse():TVaultConstants.EMPTY). put(LogMessage.STATUS, (null!=metadataResponse)?metadataResponse.getHttpstatus().toString():TVaultConstants.EMPTY). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Group configuration failed. Please try again\"]}"); }else{ log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Add Group to Service Account"). put(LogMessage.MESSAGE, "Reverting group policy update failed"). put(LogMessage.RESPONSE, (null!=metadataResponse)?metadataResponse.getResponse():TVaultConstants.EMPTY). put(LogMessage.STATUS, (null!=metadataResponse)?metadataResponse.getHttpstatus().toString():TVaultConstants.EMPTY). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Group configuration failed. Contact Admin \"]}"); } } else { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Failed to remove the group from the Service Account\"]}"); } } else { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Access denied: No permission to remove groups from this service account\"]}"); } }  ResponseEntity<ADServiceAccountObjects> getADServiceAccounts(String token, UserDetails userDetails, String UserPrincipalName, boolean excludeOnboarded); ResponseEntity<String> onboardServiceAccount(String token, ServiceAccount serviceAccount, UserDetails userDetails); ResponseEntity<String> offboardServiceAccount(String token, OnboardedServiceAccount serviceAccount, UserDetails userDetails); ResponseEntity<String> addUserToServiceAccount(String token, ServiceAccountUser serviceAccountUser, UserDetails userDetails, boolean isPartOfSvcAccOnboard); ResponseEntity<String> removeUserFromServiceAccount(String token, ServiceAccountUser serviceAccountUser, UserDetails userDetails); ResponseEntity<String> resetSvcAccPassword(String token, String svcAccName, UserDetails userDetails); ResponseEntity<String> readSvcAccPassword(String token, String svcAccName, UserDetails userDetails); ResponseEntity<String> getOnboarderdServiceAccount(String token, String svcAccName, UserDetails userDetails); boolean canAddOrRemoveUser(UserDetails userDetails, ServiceAccountUser serviceAccountUser, String action); ResponseEntity<String> getOnboardedServiceAccounts(String token,  UserDetails userDetails); boolean hasAddOrRemovePermission(UserDetails userDetails, String serviceAccount, String token); boolean hasAddUserPermission(UserDetails userDetails, String serviceAccount, String token, boolean isPartOfSvcAccOnboard); static boolean isSvcaccPermissionInputValid(String access); ResponseEntity<String> addGroupToServiceAccount(String token, ServiceAccountGroup serviceAccountGroup, UserDetails userDetails); ResponseEntity<String> removeGroupFromServiceAccount(String token, ServiceAccountGroup serviceAccountGroup, UserDetails userDetails); ResponseEntity<String> associateApproletoSvcAcc(UserDetails userDetails, String token, ServiceAccountApprole serviceAccountApprole); ResponseEntity<String> getServiceAccountMeta(String token, UserDetails userDetails, String path); ResponseEntity<String> removeApproleFromSvcAcc(UserDetails userDetails, String token, ServiceAccountApprole serviceAccountApprole); ResponseEntity<String> addAwsRoleToSvcacc(UserDetails userDetails, String token, ServiceAccountAWSRole serviceAccountAWSRole); ResponseEntity<String> removeAWSRoleFromSvcacc(UserDetails userDetails, String token, ServiceAccountAWSRole serviceAccountAWSRole); ResponseEntity<String> createAWSRole(UserDetails userDetails, String token, AWSLoginRole awsLoginRole); ResponseEntity<String> createIAMRole(UserDetails userDetails, String token, AWSIAMRole awsiamRole); ResponseEntity<String> updateOnboardedServiceAccount(String token, ServiceAccount serviceAccount, UserDetails userDetails); ResponseEntity<String> transferSvcAccountOwner(UserDetails userDetails, String token, String svcAccName); ResponseEntity<String> getServiceAccounts(UserDetails userDetails, String userToken); }
ServiceAccountsService { public ResponseEntity<String> removeGroupFromServiceAccount(String token, ServiceAccountGroup serviceAccountGroup, UserDetails userDetails) { OIDCGroup oidcGroup = new OIDCGroup(); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Remove Group from Service Account"). put(LogMessage.MESSAGE, String.format ("Trying to remove Group from Service Account")). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); if (!userDetails.isAdmin()) { token = tokenUtils.getSelfServiceToken(); } if(!isSvcaccPermissionInputValid(serviceAccountGroup.getAccess())) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid value specified for access. Valid values are read, reset, deny\"]}"); } if (serviceAccountGroup.getAccess().equalsIgnoreCase("reset")) { serviceAccountGroup.setAccess(TVaultConstants.WRITE_POLICY); } String groupName = serviceAccountGroup.getGroupname().toLowerCase(); String svcAccName = serviceAccountGroup.getSvcAccName(); String access = serviceAccountGroup.getAccess(); boolean isAuthorized = true; if (userDetails != null) { isAuthorized = hasAddOrRemovePermission(userDetails, svcAccName, token); } if(isAuthorized){ if (!ifInitialPwdReset(token, userDetails, serviceAccountGroup.getSvcAccName())) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Remove Group from ServiceAccount"). put(LogMessage.MESSAGE, "Failed to remove group permission from Service account. Initial password reset is pending for this Service Account."). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Failed to remove group permission from Service account. Initial password reset is pending for this Service Account. Please reset the password and try again.\"]}"); } String r_policy = new StringBuffer().append(TVaultConstants.SVC_ACC_POLICIES_PREFIXES.getKey(TVaultConstants.READ_POLICY)).append(TVaultConstants.SVC_ACC_PATH_PREFIX).append("_").append(svcAccName).toString(); String w_policy = new StringBuffer().append(TVaultConstants.SVC_ACC_POLICIES_PREFIXES.getKey(TVaultConstants.WRITE_POLICY)).append(TVaultConstants.SVC_ACC_PATH_PREFIX).append("_").append(svcAccName).toString(); String d_policy = new StringBuffer().append(TVaultConstants.SVC_ACC_POLICIES_PREFIXES.getKey(TVaultConstants.DENY_POLICY)).append(TVaultConstants.SVC_ACC_PATH_PREFIX).append("_").append(svcAccName).toString(); String o_policy = new StringBuffer().append(TVaultConstants.SVC_ACC_POLICIES_PREFIXES.getKey(TVaultConstants.SUDO_POLICY)).append(TVaultConstants.SVC_ACC_PATH_PREFIX).append("_").append(svcAccName).toString(); log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Remove group from Service Account"). put(LogMessage.MESSAGE, String.format ("Policies are, read - [%s], write - [%s], deny -[%s], owner - [%s]", r_policy, w_policy, d_policy, o_policy)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); Response groupResp = new Response(); if (TVaultConstants.LDAP.equals(vaultAuthMethod)) { groupResp = reqProcessor.process("/auth/ldap/groups", "{\"groupname\":\"" + groupName + "\"}", token); } else if (TVaultConstants.OIDC.equals(vaultAuthMethod)) { oidcGroup = oidcUtil.getIdentityGroupDetails(groupName, token); if (oidcGroup != null) { groupResp.setHttpstatus(HttpStatus.OK); groupResp.setResponse(oidcGroup.getPolicies().toString()); } else { groupResp.setHttpstatus(HttpStatus.BAD_REQUEST); } } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Remove group from ServiceAccount"). put(LogMessage.MESSAGE, String.format ("userResponse status is [%s]", groupResp.getHttpstatus())). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); String responseJson=""; List<String> policies = new ArrayList<>(); List<String> currentpolicies = new ArrayList<>(); String policy = new StringBuffer().append(TVaultConstants.SVC_ACC_POLICIES_PREFIXES.getKey(access)).append(TVaultConstants.SVC_ACC_PATH_PREFIX).append("_").append(svcAccName).toString(); if(HttpStatus.OK.equals(groupResp.getHttpstatus())){ responseJson = groupResp.getResponse(); try { ObjectMapper objMapper = new ObjectMapper(); if (TVaultConstants.LDAP.equals(vaultAuthMethod)) { currentpolicies = ControllerUtil.getPoliciesAsListFromJson(objMapper, responseJson); } else if (TVaultConstants.OIDC.equals(vaultAuthMethod)) { currentpolicies.addAll(oidcGroup.getPolicies()); } } 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, "Remove group from ServiceAccount"). put(LogMessage.MESSAGE, String.format ("Exception while creating currentpolicies or groups")). put(LogMessage.STACKTRACE, Arrays.toString(e.getStackTrace())). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); } policies.addAll(currentpolicies); policies.remove(r_policy); policies.remove(w_policy); policies.remove(d_policy); } String policiesString = org.apache.commons.lang3.StringUtils.join(policies, ","); String currentpoliciesString = org.apache.commons.lang3.StringUtils.join(currentpolicies, ","); Response ldapConfigresponse = new Response(); if (TVaultConstants.LDAP.equals(vaultAuthMethod)) { ldapConfigresponse = ControllerUtil.configureLDAPGroup(groupName, policiesString, token); } else if (TVaultConstants.OIDC.equals(vaultAuthMethod)) { ldapConfigresponse = oidcUtil.updateGroupPolicies(token, groupName, policies, currentpolicies, oidcGroup.getId()); oidcUtil.renewUserToken(userDetails.getClientToken()); } if(ldapConfigresponse.getHttpstatus().equals(HttpStatus.NO_CONTENT) || ldapConfigresponse.getHttpstatus().equals(HttpStatus.OK)){ String path = new StringBuffer(TVaultConstants.SVC_ACC_ROLES_PATH).append(svcAccName).toString(); Map<String,String> params = new HashMap<String,String>(); params.put("type", "groups"); params.put("name",groupName); params.put("path",path); params.put("access","delete"); Response metadataResponse = ControllerUtil.updateMetadata(params,token); if(metadataResponse !=null && (HttpStatus.NO_CONTENT.equals(metadataResponse.getHttpstatus()) || HttpStatus.OK.equals(metadataResponse.getHttpstatus()))){ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Remove Group to Service Account"). put(LogMessage.MESSAGE, "Group configuration Success."). put(LogMessage.STATUS, metadataResponse.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"Group is successfully removed from Service Account\"]}"); } if (TVaultConstants.LDAP.equals(vaultAuthMethod)) { ldapConfigresponse = ControllerUtil.configureLDAPGroup(groupName, currentpoliciesString, token); } else if (TVaultConstants.OIDC.equals(vaultAuthMethod)) { ldapConfigresponse = oidcUtil.updateGroupPolicies(token, groupName, currentpolicies, currentpolicies, oidcGroup.getId()); oidcUtil.renewUserToken(userDetails.getClientToken()); } if(ldapConfigresponse.getHttpstatus().equals(HttpStatus.NO_CONTENT)){ log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Add Group to Service Account"). put(LogMessage.MESSAGE, "Reverting, group policy update success"). put(LogMessage.RESPONSE, (null!=metadataResponse)?metadataResponse.getResponse():TVaultConstants.EMPTY). put(LogMessage.STATUS, (null!=metadataResponse)?metadataResponse.getHttpstatus().toString():TVaultConstants.EMPTY). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Group configuration failed. Please try again\"]}"); }else{ log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Add Group to Service Account"). put(LogMessage.MESSAGE, "Reverting group policy update failed"). put(LogMessage.RESPONSE, (null!=metadataResponse)?metadataResponse.getResponse():TVaultConstants.EMPTY). put(LogMessage.STATUS, (null!=metadataResponse)?metadataResponse.getHttpstatus().toString():TVaultConstants.EMPTY). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Group configuration failed. Contact Admin \"]}"); } } else { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Failed to remove the group from the Service Account\"]}"); } } else { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Access denied: No permission to remove groups from this service account\"]}"); } }  ResponseEntity<ADServiceAccountObjects> getADServiceAccounts(String token, UserDetails userDetails, String UserPrincipalName, boolean excludeOnboarded); ResponseEntity<String> onboardServiceAccount(String token, ServiceAccount serviceAccount, UserDetails userDetails); ResponseEntity<String> offboardServiceAccount(String token, OnboardedServiceAccount serviceAccount, UserDetails userDetails); ResponseEntity<String> addUserToServiceAccount(String token, ServiceAccountUser serviceAccountUser, UserDetails userDetails, boolean isPartOfSvcAccOnboard); ResponseEntity<String> removeUserFromServiceAccount(String token, ServiceAccountUser serviceAccountUser, UserDetails userDetails); ResponseEntity<String> resetSvcAccPassword(String token, String svcAccName, UserDetails userDetails); ResponseEntity<String> readSvcAccPassword(String token, String svcAccName, UserDetails userDetails); ResponseEntity<String> getOnboarderdServiceAccount(String token, String svcAccName, UserDetails userDetails); boolean canAddOrRemoveUser(UserDetails userDetails, ServiceAccountUser serviceAccountUser, String action); ResponseEntity<String> getOnboardedServiceAccounts(String token,  UserDetails userDetails); boolean hasAddOrRemovePermission(UserDetails userDetails, String serviceAccount, String token); boolean hasAddUserPermission(UserDetails userDetails, String serviceAccount, String token, boolean isPartOfSvcAccOnboard); static boolean isSvcaccPermissionInputValid(String access); ResponseEntity<String> addGroupToServiceAccount(String token, ServiceAccountGroup serviceAccountGroup, UserDetails userDetails); ResponseEntity<String> removeGroupFromServiceAccount(String token, ServiceAccountGroup serviceAccountGroup, UserDetails userDetails); ResponseEntity<String> associateApproletoSvcAcc(UserDetails userDetails, String token, ServiceAccountApprole serviceAccountApprole); ResponseEntity<String> getServiceAccountMeta(String token, UserDetails userDetails, String path); ResponseEntity<String> removeApproleFromSvcAcc(UserDetails userDetails, String token, ServiceAccountApprole serviceAccountApprole); ResponseEntity<String> addAwsRoleToSvcacc(UserDetails userDetails, String token, ServiceAccountAWSRole serviceAccountAWSRole); ResponseEntity<String> removeAWSRoleFromSvcacc(UserDetails userDetails, String token, ServiceAccountAWSRole serviceAccountAWSRole); ResponseEntity<String> createAWSRole(UserDetails userDetails, String token, AWSLoginRole awsLoginRole); ResponseEntity<String> createIAMRole(UserDetails userDetails, String token, AWSIAMRole awsiamRole); ResponseEntity<String> updateOnboardedServiceAccount(String token, ServiceAccount serviceAccount, UserDetails userDetails); ResponseEntity<String> transferSvcAccountOwner(UserDetails userDetails, String token, String svcAccName); ResponseEntity<String> getServiceAccounts(UserDetails userDetails, String userToken);  }