tvaultapi/src/main/java/com/tmobile/cso/vault/api/service/SSLCertificateService.java
public ResponseEntity<String> associateApproletoCertificate(CertificateApprole certificateApprole, UserDetails userDetails) {        
        String authToken = null;
        boolean isAuthorized = true;
        if(!areCertificateApproleInputsValid(certificateApprole)) {
        	log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder().
   					put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)).
   					put(LogMessage.ACTION, SSLCertificateConstants.ADD_APPROLE_TO_CERT_MSG).
   					put(LogMessage.MESSAGE, "Invalid input values").
   					put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).
   					build()));
   			return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ERRORINVALID);
        }

        // External certificate disabled check
        if (certificateApprole.getCertType().equalsIgnoreCase(SSLCertificateConstants.EXTERNAL) && !isExternalCertEnabled){
            log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder()
                    .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER))
                    .put(LogMessage.ACTION, SSLCertificateConstants.ADD_APPROLE_TO_CERT_MSG)
                    .put(LogMessage.MESSAGE,
                            "Failed to associate approle to external SSL certificate. Operation not allowed.")
                    .put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).build()));
            return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(
                    "{\"errors\":[\"Failed to associate approle to external SSL certificate. Operation not allowed.\"]}");
        }
        
        log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder().
                put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)).
                put(LogMessage.ACTION, SSLCertificateConstants.ADD_APPROLE_TO_CERT_MSG).
                put(LogMessage.MESSAGE, String.format("Trying to add Approle to Certificate - Request [%s]", certificateApprole.toString())).
                put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).
                build()));

        String approleName = certificateApprole.getApproleName().toLowerCase();
        String certificateName = certificateApprole.getCertificateName().toLowerCase();
        String access = certificateApprole.getAccess().toLowerCase();
        String certType = certificateApprole.getCertType().toLowerCase();

		if (Arrays.asList(TVaultConstants.SELF_SUPPORT_ADMIN_APPROLES).contains(approleName)) {
			return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(
					"{\"errors\":[\"Access denied: no permission to associate this AppRole to any Certificate\"]}");
		}
        

        if (!ObjectUtils.isEmpty(userDetails)) {

	        if (userDetails.isAdmin()) {
	        	authToken = userDetails.getClientToken();
	        }else {
	        	authToken = userDetails.getSelfSupportToken();
	        }

	        SSLCertificateMetadataDetails certificateMetaData = certificateUtils.getCertificateMetaData(authToken, certificateName, certType);

			isAuthorized = certificateUtils.hasAddOrRemovePermission(userDetails, certificateMetaData);

        }else {
   			log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder().
   					put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)).
   					put(LogMessage.ACTION, SSLCertificateConstants.ADD_APPROLE_TO_CERT_MSG).
   					put(LogMessage.MESSAGE, "Access denied: No permission to add approle to this certificate").
   					put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).
   					build()));

   			return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Access denied: No permission to add approle to this certificate\"]}");
   		}

        if(isAuthorized){
        	return createPoliciesAndConfigureApproleToCertificate(authToken, approleName, certificateName, access, certType);
        } else{
        	log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder().
					put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)).
					put(LogMessage.ACTION, SSLCertificateConstants.ADD_APPROLE_TO_CERT_MSG).
					put(LogMessage.MESSAGE, String.format("Access denied: No permission to add Approle [%s] to the Certificate [%s]", approleName, certificateName)).
					put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).
					build()));

            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Access denied: No permission to add Approle to this Certificate\"]}");
        }
    }
/**
     * Associate Approle to Certificate
     * @param userDetails
     * @param certificateApprole
     * @return
     */
Associate Approle to Certificate
@param userDetails
@param certificateApprole
@return
Associate Approle to Certificate @param userDetails @param certificateApprole @return
public ResponseEntity<String> associateApproletoCertificate(CertificateApprole certificateApprole, UserDetails userDetails) {        
        String authToken = null;
        boolean isAuthorized = true;
        if(!areCertificateApproleInputsValid(certificateApprole)) {
        	log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder().
   					put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)).
   					put(LogMessage.ACTION, SSLCertificateConstants.ADD_APPROLE_TO_CERT_MSG).
   					put(LogMessage.MESSAGE, "Invalid input values").
   					put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).
   					build()));
   			return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ERRORINVALID);
        }
        if (certificateApprole.getCertType().equalsIgnoreCase(SSLCertificateConstants.EXTERNAL) && !isExternalCertEnabled){
            log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder()
                    .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER))
                    .put(LogMessage.ACTION, SSLCertificateConstants.ADD_APPROLE_TO_CERT_MSG)
                    .put(LogMessage.MESSAGE,
                            "Failed to associate approle to external SSL certificate. Operation not allowed.")
                    .put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).build()));
            return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(
                    "{\"errors\":[\"Failed to associate approle to external SSL certificate. Operation not allowed.\"]}");
        }
        log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder().
                put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)).
                put(LogMessage.ACTION, SSLCertificateConstants.ADD_APPROLE_TO_CERT_MSG).
                put(LogMessage.MESSAGE, String.format("Trying to add Approle to Certificate - Request [%s]", certificateApprole.toString())).
                put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).
                build()));
        String approleName = certificateApprole.getApproleName().toLowerCase();
        String certificateName = certificateApprole.getCertificateName().toLowerCase();
        String access = certificateApprole.getAccess().toLowerCase();
        String certType = certificateApprole.getCertType().toLowerCase();
		if (Arrays.asList(TVaultConstants.SELF_SUPPORT_ADMIN_APPROLES).contains(approleName)) {
			return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(
					"{\"errors\":[\"Access denied: no permission to associate this AppRole to any Certificate\"]}");
		}
        if (!ObjectUtils.isEmpty(userDetails)) {
	        if (userDetails.isAdmin()) {
	        	authToken = userDetails.getClientToken();
	        }else {
	        	authToken = userDetails.getSelfSupportToken();
	        }
	        SSLCertificateMetadataDetails certificateMetaData = certificateUtils.getCertificateMetaData(authToken, certificateName, certType);
			isAuthorized = certificateUtils.hasAddOrRemovePermission(userDetails, certificateMetaData);
        }else {
   			log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder().
   					put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)).
   					put(LogMessage.ACTION, SSLCertificateConstants.ADD_APPROLE_TO_CERT_MSG).
   					put(LogMessage.MESSAGE, "Access denied: No permission to add approle to this certificate").
   					put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).
   					build()));
   			return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Access denied: No permission to add approle to this certificate\"]}");
   		}
        if(isAuthorized){
        	return createPoliciesAndConfigureApproleToCertificate(authToken, approleName, certificateName, access, certType);
        } else{
        	log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder().
					put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)).
					put(LogMessage.ACTION, SSLCertificateConstants.ADD_APPROLE_TO_CERT_MSG).
					put(LogMessage.MESSAGE, String.format("Access denied: No permission to add Approle [%s] to the Certificate [%s]", approleName, certificateName)).
					put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).
					build()));
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Access denied: No permission to add Approle to this Certificate\"]}");
        }
    }
public ResponseEntity < String > associateApproletoCertificate ( CertificateApprole certificateApprole , UserDetails userDetails ) { String authToken = null ; boolean isAuthorized = true ; if ( ! areCertificateApproleInputsValid ( certificateApprole ) ) { log . error ( JSONUtil . getJSON ( ImmutableMap . < String , String > builder ( ) . put ( LogMessage . USER , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . USER ) ) . put ( LogMessage . ACTION , SSLCertificateConstants . ADD_APPROLE_TO_CERT_MSG ) . put ( LogMessage . MESSAGE , "Invalid input values" ) . put ( LogMessage . APIURL , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . APIURL ) ) . build ( ) ) ) ; return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( ERRORINVALID ) ; } if ( certificateApprole . getCertType ( ) . equalsIgnoreCase ( SSLCertificateConstants . EXTERNAL ) && ! isExternalCertEnabled ) { log . error ( JSONUtil . getJSON ( ImmutableMap . < String , String > builder ( ) . put ( LogMessage . USER , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . USER ) ) . put ( LogMessage . ACTION , SSLCertificateConstants . ADD_APPROLE_TO_CERT_MSG ) . put ( LogMessage . MESSAGE , "Failed to associate approle to external SSL certificate. Operation not allowed." ) . put ( LogMessage . APIURL , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . APIURL ) ) . build ( ) ) ) ; return ResponseEntity . status ( HttpStatus . UNPROCESSABLE_ENTITY ) . body ( "{\"errors\":[\"Failed to associate approle to external SSL certificate. Operation not allowed.\"]}" ) ; } log . debug ( JSONUtil . getJSON ( ImmutableMap . < String , String > builder ( ) . put ( LogMessage . USER , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . USER ) ) . put ( LogMessage . ACTION , SSLCertificateConstants . ADD_APPROLE_TO_CERT_MSG ) . put ( LogMessage . MESSAGE , String . format ( "Trying to add Approle to Certificate - Request [%s]" , certificateApprole . toString ( ) ) ) . put ( LogMessage . APIURL , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . APIURL ) ) . build ( ) ) ) ; String approleName = certificateApprole . getApproleName ( ) . toLowerCase ( ) ; String certificateName = certificateApprole . getCertificateName ( ) . toLowerCase ( ) ; String access = certificateApprole . getAccess ( ) . toLowerCase ( ) ; String certType = certificateApprole . getCertType ( ) . toLowerCase ( ) ; if ( Arrays . asList ( TVaultConstants . SELF_SUPPORT_ADMIN_APPROLES ) . contains ( approleName ) ) { return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( "{\"errors\":[\"Access denied: no permission to associate this AppRole to any Certificate\"]}" ) ; } if ( ! ObjectUtils . isEmpty ( userDetails ) ) { if ( userDetails . isAdmin ( ) ) { authToken = userDetails . getClientToken ( ) ; } else { authToken = userDetails . getSelfSupportToken ( ) ; } SSLCertificateMetadataDetails certificateMetaData = certificateUtils . getCertificateMetaData ( authToken , certificateName , certType ) ; isAuthorized = certificateUtils . hasAddOrRemovePermission ( userDetails , certificateMetaData ) ; } else { log . error ( JSONUtil . getJSON ( ImmutableMap . < String , String > builder ( ) . put ( LogMessage . USER , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . USER ) ) . put ( LogMessage . ACTION , SSLCertificateConstants . ADD_APPROLE_TO_CERT_MSG ) . put ( LogMessage . MESSAGE , "Access denied: No permission to add approle to this certificate" ) . put ( LogMessage . APIURL , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . APIURL ) ) . build ( ) ) ) ; return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( "{\"errors\":[\"Access denied: No permission to add approle to this certificate\"]}" ) ; } if ( isAuthorized ) { return createPoliciesAndConfigureApproleToCertificate ( authToken , approleName , certificateName , access , certType ) ; } else { log . error ( JSONUtil . getJSON ( ImmutableMap . < String , String > builder ( ) . put ( LogMessage . USER , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . USER ) ) . put ( LogMessage . ACTION , SSLCertificateConstants . ADD_APPROLE_TO_CERT_MSG ) . put ( LogMessage . MESSAGE , String . format ( "Access denied: No permission to add Approle [%s] to the Certificate [%s]" , approleName , certificateName ) ) . put ( LogMessage . APIURL , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . APIURL ) ) . build ( ) ) ) ; return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( "{\"errors\":[\"Access denied: No permission to add Approle to this Certificate\"]}" ) ; } }
Associate Approle to Certificate
 @param userDetails
 @param certificateApprole
 @return
Associate Approle to Certificate @param userDetails @param certificateApprole @return
// External certificate disabled check