use of software.amazon.awssdk.services.secretsmanager.model.SecretsManagerException in project micronaut-aws by micronaut-projects.
the class SecretsManagerKeyValueFetcher method keyValuesByPrefix.
@Override
@NonNull
public Optional<Map> keyValuesByPrefix(@NonNull String prefix) {
Map result = new HashMap<>();
try {
String nextToken = null;
do {
ListSecretsRequest.Builder builder = ListSecretsRequest.builder().nextToken(nextToken).filters(Filter.builder().key(FilterNameStringType.NAME).values(prefix).build());
if (nextToken != null) {
builder = builder.nextToken(nextToken);
}
ListSecretsRequest listSecretsRequest = builder.build();
ListSecretsResponse secretsResponse = secretsClient.listSecrets(listSecretsRequest);
List<SecretListEntry> secrets = secretsResponse.secretList();
if (LOG.isTraceEnabled()) {
if (secrets.isEmpty()) {
LOG.trace("zero secrets for prefix: {}", prefix);
} else {
LOG.trace("# {} secrets for prefix: {}", secrets.size(), prefix);
}
}
for (SecretListEntry secret : secrets) {
if (LOG.isTraceEnabled()) {
LOG.trace("Evaluating secret {}", secret.name());
}
Optional<String> secretValueOptional = fetchSecretValue(secretsClient, secret.name());
if (secretValueOptional.isPresent()) {
try {
result.putAll(objectMapper.readValue(secretValueOptional.get(), Map.class));
} catch (JsonProcessingException e) {
if (LOG.isWarnEnabled()) {
LOG.warn("could not read secret ({}) value from JSON to Map", secret.name());
}
}
}
}
nextToken = secretsResponse.nextToken();
} while (nextToken != null);
} catch (SecretsManagerException e) {
if (LOG.isWarnEnabled()) {
LOG.warn("SecretsManagerException {}", e.awsErrorDetails().errorMessage());
}
return Optional.empty();
}
return result.isEmpty() ? Optional.empty() : Optional.of(result);
}
use of software.amazon.awssdk.services.secretsmanager.model.SecretsManagerException in project aws-doc-sdk-examples by awsdocs.
the class CreateSecret method createNewSecret.
// snippet-start:[secretsmanager.java2.create_secret.main]
public static String createNewSecret(SecretsManagerClient secretsClient, String secretName, String secretValue) {
try {
CreateSecretRequest secretRequest = CreateSecretRequest.builder().name(secretName).description("This secret was created by the AWS Secret Manager Java API").secretString(secretValue).build();
CreateSecretResponse secretResponse = secretsClient.createSecret(secretRequest);
return secretResponse.arn();
} catch (SecretsManagerException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
return "";
}
use of software.amazon.awssdk.services.secretsmanager.model.SecretsManagerException in project aws-doc-sdk-examples by awsdocs.
the class DescribeSecret method describeGivenSecret.
// snippet-start:[secretsmanager.java2.describe_secret.main]
public static void describeGivenSecret(SecretsManagerClient secretsClient, String secretName) {
try {
DescribeSecretRequest secretRequest = DescribeSecretRequest.builder().secretId(secretName).build();
DescribeSecretResponse secretResponse = secretsClient.describeSecret(secretRequest);
Instant lastChangedDate = secretResponse.lastChangedDate();
// Convert the Instant to readable date
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withLocale(Locale.US).withZone(ZoneId.systemDefault());
formatter.format(lastChangedDate);
System.out.println("The date of the last change to " + secretResponse.name() + " is " + lastChangedDate);
} catch (SecretsManagerException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.secretsmanager.model.SecretsManagerException in project aws-doc-sdk-examples by awsdocs.
the class UpdateSecret method updateMySecret.
// snippet-start:[secretsmanager.java2.update_secret.main]
public static void updateMySecret(SecretsManagerClient secretsClient, String secretName, String secretValue) {
try {
UpdateSecretRequest secretRequest = UpdateSecretRequest.builder().secretId(secretName).secretString(secretValue).build();
secretsClient.updateSecret(secretRequest);
} catch (SecretsManagerException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.secretsmanager.model.SecretsManagerException in project aws-doc-sdk-examples by awsdocs.
the class GetSecretValue method getValue.
// snippet-start:[secretsmanager.java2.get_secret.main]
public static void getValue(SecretsManagerClient secretsClient, String secretName) {
try {
GetSecretValueRequest valueRequest = GetSecretValueRequest.builder().secretId(secretName).build();
GetSecretValueResponse valueResponse = secretsClient.getSecretValue(valueRequest);
String secret = valueResponse.secretString();
System.out.println(secret);
} catch (SecretsManagerException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
Aggregations