com.infusers.core.secrets.aws.AWSSecretManager Maven / Gradle / Ivy
package com.infusers.core.secrets.aws;
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.logger.ILogger;
import com.infusers.core.secrets.SecretManager;
import com.infusers.core.secrets.SecretsService;
public class AWSSecretManager implements SecretManager {
private ILogger log = new ILogger(SecretsService.class);
@Override
public boolean isCloudEnvironment() {
return true;
}
@Override
public String getSecret(String region, String secretName) {
if (region == null || region.trim().length()==0 || secretName == null || secretName.trim().length()==0 ) {
log.errorWithSeparator("AWSSecretManager.getSecret() region/secretName are empty/null. region = "+region+" :: secretName = "+secretName);
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("AWSSecretManager.getSecret()" + e.getMessage());
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy