tvaultapi/src/main/java/com/tmobile/cso/vault/api/service/IAMServiceAccountsService.java
private ResponseEntity<String> createPoliciesAndRemoveUserFromSvcAcc(String token,
			IAMServiceAccountUser iamServiceAccountUser, UserDetails userDetails, OIDCEntityResponse oidcEntityResponse,
			String uniqueIAMSvcaccName, Response userResponse, List<String> policiesToRemove) {

		String responseJson="";
		String groups="";
		List<String> policies = new ArrayList<>();
		List<String> currentpolicies = new ArrayList<>();
		if(HttpStatus.OK.equals(userResponse.getHttpstatus())){
			responseJson = userResponse.getResponse();
			try {
				ObjectMapper objMapper = new ObjectMapper();
				if (TVaultConstants.OIDC.equals(vaultAuthMethod)) {
					currentpolicies.addAll(oidcEntityResponse.getPolicies());
				} else {
					currentpolicies = ControllerUtil.getPoliciesAsListFromJson(objMapper, responseJson);
					if (TVaultConstants.LDAP.equals(vaultAuthMethod)) {
						groups = objMapper.readTree(responseJson).get("data").get("groups").asText();
					}
				}
			} catch (IOException e) {
				log.error(e);
				log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder().
						put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)).
						put(LogMessage.ACTION, IAMServiceAccountConstants.REMOVE_USER_FROM_IAMSVCACC_MSG).
						put(LogMessage.MESSAGE, "Exception while creating currentpolicies or groups").
						put(LogMessage.STACKTRACE, Arrays.toString(e.getStackTrace())).
						put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).
						build()));
			}
			policies.addAll(currentpolicies);
			for (String policy : policiesToRemove) {
				policies.remove(policy);
				log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder()
						.put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER))
						.put(LogMessage.ACTION, IAMServiceAccountConstants.REMOVE_USER_FROM_IAMSVCACC_MSG)
						.put(LogMessage.MESSAGE, String.format("Removing policy from list - [%s] for user [%s] on account [%s]",
								policy, iamServiceAccountUser.getUsername(), iamServiceAccountUser.getIamSvcAccName()))
						.put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).build()));
			}
		}
		String policiesString = org.apache.commons.lang3.StringUtils.join(policies, ",");
		String currentpoliciesString = org.apache.commons.lang3.StringUtils.join(currentpolicies, ",");

		Response ldapConfigresponse = configureRemovedUserPermissions(token, iamServiceAccountUser, userDetails,
				oidcEntityResponse, groups, policies, policiesString);

		if(ldapConfigresponse.getHttpstatus().equals(HttpStatus.NO_CONTENT) || ldapConfigresponse.getHttpstatus().equals(HttpStatus.OK)){

			return updateMetadataAfterRemovePermissionFromIAMSvcAcc(token, iamServiceAccountUser, userDetails,
					oidcEntityResponse, groups, currentpolicies, currentpoliciesString);
		}
		else {
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Failed to remove the user from the IAM Service Account\"]}");
		}
	}
/**
	 * Method to create policies for removing user from IAM service account and call the metadata update.
	 * @param token
	 * @param iamServiceAccountUser
	 * @param userDetails
	 * @param oidcEntityResponse
	 * @param uniqueIAMSvcaccName
	 * @param userResponse
	 * @param policiesToRemove
	 * @return
	 */
Method to create policies for removing user from IAM service account and call the metadata update.
Method to create policies for removing user from IAM service account and call the metadata update .
private ResponseEntity<String> createPoliciesAndRemoveUserFromSvcAcc(String token,
			IAMServiceAccountUser iamServiceAccountUser, UserDetails userDetails, OIDCEntityResponse oidcEntityResponse,
			String uniqueIAMSvcaccName, Response userResponse, List<String> policiesToRemove) {
		String responseJson="";
		String groups="";
		List<String> policies = new ArrayList<>();
		List<String> currentpolicies = new ArrayList<>();
		if(HttpStatus.OK.equals(userResponse.getHttpstatus())){
			responseJson = userResponse.getResponse();
			try {
				ObjectMapper objMapper = new ObjectMapper();
				if (TVaultConstants.OIDC.equals(vaultAuthMethod)) {
					currentpolicies.addAll(oidcEntityResponse.getPolicies());
				} else {
					currentpolicies = ControllerUtil.getPoliciesAsListFromJson(objMapper, responseJson);
					if (TVaultConstants.LDAP.equals(vaultAuthMethod)) {
						groups = objMapper.readTree(responseJson).get("data").get("groups").asText();
					}
				}
			} catch (IOException e) {
				log.error(e);
				log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder().
						put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)).
						put(LogMessage.ACTION, IAMServiceAccountConstants.REMOVE_USER_FROM_IAMSVCACC_MSG).
						put(LogMessage.MESSAGE, "Exception while creating currentpolicies or groups").
						put(LogMessage.STACKTRACE, Arrays.toString(e.getStackTrace())).
						put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).
						build()));
			}
			policies.addAll(currentpolicies);
			for (String policy : policiesToRemove) {
				policies.remove(policy);
				log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder()
						.put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER))
						.put(LogMessage.ACTION, IAMServiceAccountConstants.REMOVE_USER_FROM_IAMSVCACC_MSG)
						.put(LogMessage.MESSAGE, String.format("Removing policy from list - [%s] for user [%s] on account [%s]",
								policy, iamServiceAccountUser.getUsername(), iamServiceAccountUser.getIamSvcAccName()))
						.put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).build()));
			}
		}
		String policiesString = org.apache.commons.lang3.StringUtils.join(policies, ",");
		String currentpoliciesString = org.apache.commons.lang3.StringUtils.join(currentpolicies, ",");
		Response ldapConfigresponse = configureRemovedUserPermissions(token, iamServiceAccountUser, userDetails,
				oidcEntityResponse, groups, policies, policiesString);
		if(ldapConfigresponse.getHttpstatus().equals(HttpStatus.NO_CONTENT) || ldapConfigresponse.getHttpstatus().equals(HttpStatus.OK)){
			return updateMetadataAfterRemovePermissionFromIAMSvcAcc(token, iamServiceAccountUser, userDetails,
					oidcEntityResponse, groups, currentpolicies, currentpoliciesString);
		}
		else {
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Failed to remove the user from the IAM Service Account\"]}");
		}
	}
private ResponseEntity < String > createPoliciesAndRemoveUserFromSvcAcc ( String token , IAMServiceAccountUser iamServiceAccountUser , UserDetails userDetails , OIDCEntityResponse oidcEntityResponse , String uniqueIAMSvcaccName , Response userResponse , List < String > policiesToRemove ) { String responseJson = "" ; String groups = "" ; List < String > policies = new ArrayList < > ( ) ; List < String > currentpolicies = new ArrayList < > ( ) ; if ( HttpStatus . OK . equals ( userResponse . getHttpstatus ( ) ) ) { responseJson = userResponse . getResponse ( ) ; try { ObjectMapper objMapper = new ObjectMapper ( ) ; if ( TVaultConstants . OIDC . equals ( vaultAuthMethod ) ) { currentpolicies . addAll ( oidcEntityResponse . getPolicies ( ) ) ; } else { currentpolicies = ControllerUtil . getPoliciesAsListFromJson ( objMapper , responseJson ) ; if ( TVaultConstants . LDAP . equals ( vaultAuthMethod ) ) { groups = objMapper . readTree ( responseJson ) . get ( "data" ) . get ( "groups" ) . asText ( ) ; } } } catch ( IOException e ) { log . error ( e ) ; log . error ( JSONUtil . getJSON ( ImmutableMap . < String , String > builder ( ) . put ( LogMessage . USER , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . USER ) ) . put ( LogMessage . ACTION , IAMServiceAccountConstants . REMOVE_USER_FROM_IAMSVCACC_MSG ) . put ( LogMessage . MESSAGE , "Exception while creating currentpolicies or groups" ) . put ( LogMessage . STACKTRACE , Arrays . toString ( e . getStackTrace ( ) ) ) . put ( LogMessage . APIURL , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . APIURL ) ) . build ( ) ) ) ; } policies . addAll ( currentpolicies ) ; for ( String policy : policiesToRemove ) { policies . remove ( policy ) ; log . debug ( JSONUtil . getJSON ( ImmutableMap . < String , String > builder ( ) . put ( LogMessage . USER , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . USER ) ) . put ( LogMessage . ACTION , IAMServiceAccountConstants . REMOVE_USER_FROM_IAMSVCACC_MSG ) . put ( LogMessage . MESSAGE , String . format ( "Removing policy from list - [%s] for user [%s] on account [%s]" , policy , iamServiceAccountUser . getUsername ( ) , iamServiceAccountUser . getIamSvcAccName ( ) ) ) . put ( LogMessage . APIURL , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . APIURL ) ) . build ( ) ) ) ; } } String policiesString = org . apache . commons . lang3 . StringUtils . join ( policies , "," ) ; String currentpoliciesString = org . apache . commons . lang3 . StringUtils . join ( currentpolicies , "," ) ; Response ldapConfigresponse = configureRemovedUserPermissions ( token , iamServiceAccountUser , userDetails , oidcEntityResponse , groups , policies , policiesString ) ; if ( ldapConfigresponse . getHttpstatus ( ) . equals ( HttpStatus . NO_CONTENT ) || ldapConfigresponse . getHttpstatus ( ) . equals ( HttpStatus . OK ) ) { return updateMetadataAfterRemovePermissionFromIAMSvcAcc ( token , iamServiceAccountUser , userDetails , oidcEntityResponse , groups , currentpolicies , currentpoliciesString ) ; } else { return ResponseEntity . status ( HttpStatus . INTERNAL_SERVER_ERROR ) . body ( "{\"errors\":[\"Failed to remove the user from the IAM Service Account\"]}" ) ; } }
Method to create policies for removing user from IAM service account and call the metadata update.
Method to create policies for removing user from IAM service account and call the metadata update .