tvaultapi/src/main/java/com/tmobile/cso/vault/api/service/IAMServiceAccountsService.java
private boolean updateIAMSvcAccSecretWithLatestExpiryDate(String token, String awsAccountID, String iamSvcName, String accessKey, Long expiryDateEpoch) {
		iamSvcName = iamSvcName.toLowerCase();
		String iamSvcNamePath = IAMServiceAccountConstants.IAM_SVCC_ACC_PATH + awsAccountID + '_' + iamSvcName;
		String uniqueIAMSvcName = awsAccountID + '_' + iamSvcName;
		try {
			ResponseEntity<String> response = readFolders(token, iamSvcNamePath);
			ObjectMapper mapper = new ObjectMapper();
			if (HttpStatus.OK.equals(response.getStatusCode())) {
				IAMServiceAccountNode iamServiceAccountNode = mapper.readValue(response.getBody(),
						IAMServiceAccountNode.class);
				if (iamServiceAccountNode.getFolders() != null) {
					for (String folderName : iamServiceAccountNode.getFolders()) {
						ResponseEntity<String> responseEntity = getIAMServiceAccountSecretKey(token,
								uniqueIAMSvcName, folderName);
						if (HttpStatus.OK.equals(responseEntity.getStatusCode())) {
							IAMServiceAccountSecret iamServiceAccountSecret = mapper.readValue(responseEntity.getBody(),
									IAMServiceAccountSecret.class);
							int expiryDateValue = expiryDateEpoch.compareTo(0L);
							if (!accessKey.equals(iamServiceAccountSecret.getAccessKeyId()) && (expiryDateValue > 0)) {
								iamServiceAccountSecret.setExpiryDateEpoch(expiryDateEpoch);
								// Save secret in iamavcacc mount
								String path = new StringBuilder().append(IAMServiceAccountConstants.IAM_SVCC_ACC_PATH).append(uniqueIAMSvcName).append("/").append(folderName).toString();
								if (iamServiceAccountUtils.writeIAMSvcAccSecret(token, path, iamSvcName, iamServiceAccountSecret)) {
									log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder().
											put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)).
											put(LogMessage.ACTION, "rotate IAM Service Account By AccessKeyId").
											put(LogMessage.MESSAGE, "Secret saved to IAM service account mount").
											put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).
											build()));
									return true;
								}
								break;
							}
						} else {
							log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder().
				                    put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)).
				                    put(LogMessage.ACTION, "writeIAMSvcAccSecrets").
				                    put(LogMessage.MESSAGE, String.format ("No secret found for the IAM Service Account [%s] and accessKeyId [%s] ",iamSvcName, accessKey)).
				                    put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).
				                    build()));
							return false;
						}
					}
					return true;
				} else {
					log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder().
							put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)).
							put(LogMessage.ACTION, "rotate IAM Service Account By AccessKeyId").
							put(LogMessage.MESSAGE, String.format ("No secret found for the IAM Service Account [%s] and accessKeyId [%s] ",iamSvcName, accessKey)).
							put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).
							build()));
					return true;
				}
			} else if (HttpStatus.FORBIDDEN.equals(response.getStatusCode())) {
				log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder().
	                    put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)).
	                    put(LogMessage.ACTION, "writeIAMSvcAccSecrets").
	                    put(LogMessage.MESSAGE, String.format ("Access denied: No permission to read secret for IAM service account [%s] ",iamSvcName)).
	                    put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).
	                    build()));
				return false;
			} else {
				log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder().
	                    put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)).
	                    put(LogMessage.ACTION, "writeIAMSvcAccSecrets").
	                    put(LogMessage.MESSAGE, String.format ("aws_account_id [%s] or iam_svc_name [%s] not found.",awsAccountID, iamSvcName)).
	                    put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).
	                    build()));
				return false;
			}
		}catch (IOException ex) {
            log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder().
                    put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)).
                    put(LogMessage.ACTION, "writeIAMSvcAccSecrets").
                    put(LogMessage.MESSAGE, "Failed to write IAMServiceAccountSecret as string json").
                    put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).
                    build()));
            return false;
        }
    }
/**
     * To update the latest expiryDateEpoch to the existing secrets.
     * @param token
     * @param awsAccountID
     * @param iamSvcName
     * @param accessKey
     * @param expiryDateEpoch
     * @return
     */
To update the latest expiryDateEpoch to the existing secrets.
To update the latest expiryDateEpoch to the existing secrets .
private boolean updateIAMSvcAccSecretWithLatestExpiryDate(String token, String awsAccountID, String iamSvcName, String accessKey, Long expiryDateEpoch) {
		iamSvcName = iamSvcName.toLowerCase();
		String iamSvcNamePath = IAMServiceAccountConstants.IAM_SVCC_ACC_PATH + awsAccountID + '_' + iamSvcName;
		String uniqueIAMSvcName = awsAccountID + '_' + iamSvcName;
		try {
			ResponseEntity<String> response = readFolders(token, iamSvcNamePath);
			ObjectMapper mapper = new ObjectMapper();
			if (HttpStatus.OK.equals(response.getStatusCode())) {
				IAMServiceAccountNode iamServiceAccountNode = mapper.readValue(response.getBody(),
						IAMServiceAccountNode.class);
				if (iamServiceAccountNode.getFolders() != null) {
					for (String folderName : iamServiceAccountNode.getFolders()) {
						ResponseEntity<String> responseEntity = getIAMServiceAccountSecretKey(token,
								uniqueIAMSvcName, folderName);
						if (HttpStatus.OK.equals(responseEntity.getStatusCode())) {
							IAMServiceAccountSecret iamServiceAccountSecret = mapper.readValue(responseEntity.getBody(),
									IAMServiceAccountSecret.class);
							int expiryDateValue = expiryDateEpoch.compareTo(0L);
							if (!accessKey.equals(iamServiceAccountSecret.getAccessKeyId()) && (expiryDateValue > 0)) {
								iamServiceAccountSecret.setExpiryDateEpoch(expiryDateEpoch);
								String path = new StringBuilder().append(IAMServiceAccountConstants.IAM_SVCC_ACC_PATH).append(uniqueIAMSvcName).append("/").append(folderName).toString();
								if (iamServiceAccountUtils.writeIAMSvcAccSecret(token, path, iamSvcName, iamServiceAccountSecret)) {
									log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder().
											put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)).
											put(LogMessage.ACTION, "rotate IAM Service Account By AccessKeyId").
											put(LogMessage.MESSAGE, "Secret saved to IAM service account mount").
											put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).
											build()));
									return true;
								}
								break;
							}
						} else {
							log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder().
				                    put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)).
				                    put(LogMessage.ACTION, "writeIAMSvcAccSecrets").
				                    put(LogMessage.MESSAGE, String.format ("No secret found for the IAM Service Account [%s] and accessKeyId [%s] ",iamSvcName, accessKey)).
				                    put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).
				                    build()));
							return false;
						}
					}
					return true;
				} else {
					log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder().
							put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)).
							put(LogMessage.ACTION, "rotate IAM Service Account By AccessKeyId").
							put(LogMessage.MESSAGE, String.format ("No secret found for the IAM Service Account [%s] and accessKeyId [%s] ",iamSvcName, accessKey)).
							put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).
							build()));
					return true;
				}
			} else if (HttpStatus.FORBIDDEN.equals(response.getStatusCode())) {
				log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder().
	                    put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)).
	                    put(LogMessage.ACTION, "writeIAMSvcAccSecrets").
	                    put(LogMessage.MESSAGE, String.format ("Access denied: No permission to read secret for IAM service account [%s] ",iamSvcName)).
	                    put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).
	                    build()));
				return false;
			} else {
				log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder().
	                    put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)).
	                    put(LogMessage.ACTION, "writeIAMSvcAccSecrets").
	                    put(LogMessage.MESSAGE, String.format ("aws_account_id [%s] or iam_svc_name [%s] not found.",awsAccountID, iamSvcName)).
	                    put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).
	                    build()));
				return false;
			}
		}catch (IOException ex) {
            log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder().
                    put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)).
                    put(LogMessage.ACTION, "writeIAMSvcAccSecrets").
                    put(LogMessage.MESSAGE, "Failed to write IAMServiceAccountSecret as string json").
                    put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).
                    build()));
            return false;
        }
    }
private boolean updateIAMSvcAccSecretWithLatestExpiryDate ( String token , String awsAccountID , String iamSvcName , String accessKey , Long expiryDateEpoch ) { iamSvcName = iamSvcName . toLowerCase ( ) ; String iamSvcNamePath = IAMServiceAccountConstants . IAM_SVCC_ACC_PATH + awsAccountID + '_' + iamSvcName ; String uniqueIAMSvcName = awsAccountID + '_' + iamSvcName ; try { ResponseEntity < String > response = readFolders ( token , iamSvcNamePath ) ; ObjectMapper mapper = new ObjectMapper ( ) ; if ( HttpStatus . OK . equals ( response . getStatusCode ( ) ) ) { IAMServiceAccountNode iamServiceAccountNode = mapper . readValue ( response . getBody ( ) , IAMServiceAccountNode . class ) ; if ( iamServiceAccountNode . getFolders ( ) != null ) { for ( String folderName : iamServiceAccountNode . getFolders ( ) ) { ResponseEntity < String > responseEntity = getIAMServiceAccountSecretKey ( token , uniqueIAMSvcName , folderName ) ; if ( HttpStatus . OK . equals ( responseEntity . getStatusCode ( ) ) ) { IAMServiceAccountSecret iamServiceAccountSecret = mapper . readValue ( responseEntity . getBody ( ) , IAMServiceAccountSecret . class ) ; int expiryDateValue = expiryDateEpoch . compareTo ( 0L ) ; if ( ! accessKey . equals ( iamServiceAccountSecret . getAccessKeyId ( ) ) && ( expiryDateValue > 0 ) ) { iamServiceAccountSecret . setExpiryDateEpoch ( expiryDateEpoch ) ; String path = new StringBuilder ( ) . append ( IAMServiceAccountConstants . IAM_SVCC_ACC_PATH ) . append ( uniqueIAMSvcName ) . append ( "/" ) . append ( folderName ) . toString ( ) ; if ( iamServiceAccountUtils . writeIAMSvcAccSecret ( token , path , iamSvcName , iamServiceAccountSecret ) ) { log . debug ( JSONUtil . getJSON ( ImmutableMap . < String , String > builder ( ) . put ( LogMessage . USER , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . USER ) ) . put ( LogMessage . ACTION , "rotate IAM Service Account By AccessKeyId" ) . put ( LogMessage . MESSAGE , "Secret saved to IAM service account mount" ) . put ( LogMessage . APIURL , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . APIURL ) ) . build ( ) ) ) ; return true ; } break ; } } else { log . error ( JSONUtil . getJSON ( ImmutableMap . < String , String > builder ( ) . put ( LogMessage . USER , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . USER ) ) . put ( LogMessage . ACTION , "writeIAMSvcAccSecrets" ) . put ( LogMessage . MESSAGE , String . format ( "No secret found for the IAM Service Account [%s] and accessKeyId [%s] " , iamSvcName , accessKey ) ) . put ( LogMessage . APIURL , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . APIURL ) ) . build ( ) ) ) ; return false ; } } return true ; } else { log . debug ( JSONUtil . getJSON ( ImmutableMap . < String , String > builder ( ) . put ( LogMessage . USER , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . USER ) ) . put ( LogMessage . ACTION , "rotate IAM Service Account By AccessKeyId" ) . put ( LogMessage . MESSAGE , String . format ( "No secret found for the IAM Service Account [%s] and accessKeyId [%s] " , iamSvcName , accessKey ) ) . put ( LogMessage . APIURL , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . APIURL ) ) . build ( ) ) ) ; return true ; } } else if ( HttpStatus . FORBIDDEN . equals ( response . getStatusCode ( ) ) ) { log . error ( JSONUtil . getJSON ( ImmutableMap . < String , String > builder ( ) . put ( LogMessage . USER , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . USER ) ) . put ( LogMessage . ACTION , "writeIAMSvcAccSecrets" ) . put ( LogMessage . MESSAGE , String . format ( "Access denied: No permission to read secret for IAM service account [%s] " , iamSvcName ) ) . put ( LogMessage . APIURL , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . APIURL ) ) . build ( ) ) ) ; return false ; } else { log . error ( JSONUtil . getJSON ( ImmutableMap . < String , String > builder ( ) . put ( LogMessage . USER , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . USER ) ) . put ( LogMessage . ACTION , "writeIAMSvcAccSecrets" ) . put ( LogMessage . MESSAGE , String . format ( "aws_account_id [%s] or iam_svc_name [%s] not found." , awsAccountID , iamSvcName ) ) . put ( LogMessage . APIURL , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . APIURL ) ) . build ( ) ) ) ; return false ; } } catch ( IOException ex ) { log . error ( JSONUtil . getJSON ( ImmutableMap . < String , String > builder ( ) . put ( LogMessage . USER , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . USER ) ) . put ( LogMessage . ACTION , "writeIAMSvcAccSecrets" ) . put ( LogMessage . MESSAGE , "Failed to write IAMServiceAccountSecret as string json" ) . put ( LogMessage . APIURL , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . APIURL ) ) . build ( ) ) ) ; return false ; } }
To update the latest expiryDateEpoch to the existing secrets.
To update the latest expiryDateEpoch to the existing secrets .
// Save secret in iamavcacc mount