com.infusers.core.secrets.aws.AWSSecretManager Maven / Gradle / Ivy
package com.infusers.core.secrets.aws;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.amazonaws.auth.InstanceProfileCredentialsProvider;
import com.amazonaws.services.secretsmanager.AWSSecretsManager;
import com.amazonaws.services.secretsmanager.AWSSecretsManagerClientBuilder;
import com.amazonaws.services.secretsmanager.model.GetSecretValueRequest;
import com.amazonaws.services.secretsmanager.model.GetSecretValueResult;
import com.infusers.core.secrets.SecretManager;
public class AWSSecretManager implements SecretManager {
private Logger log = LogManager.getLogger(AWSSecretManager.class);
@Override
public String getSecret(String region, String secretName) {
if (region == null || region.trim().length()==0 || secretName == null || secretName.trim().length()==0 ) {
log.warn("AwsSecretsUtility.getSecret() region/secretName are empty/null.");
return null;
}
try {
AWSSecretsManager client = AWSSecretsManagerClientBuilder.standard()
.withCredentials(new InstanceProfileCredentialsProvider(false))
.build();
GetSecretValueRequest request = new GetSecretValueRequest()
.withSecretId(secretName);
GetSecretValueResult result = client.getSecretValue(request);
if (result.getSecretString() != null) {
return result.getSecretString();
}
else {
return new String(result.getSecretBinary().array());
}
}
catch (Exception e) {
log.error("AwsSecretsUtility.getSecret()" + e.getMessage());
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy