Search in sources :

Example 1 with SecretsManagerPropertySource

use of io.awspring.cloud.secretsmanager.SecretsManagerPropertySource in project spring-cloud-aws by awspring.

the class SecretsManagerConfigDataLoader method load.

@Override
@Nullable
public ConfigData load(ConfigDataLoaderContext context, SecretsManagerConfigDataResource resource) {
    try {
        SecretsManagerClient sm = context.getBootstrapContext().get(SecretsManagerClient.class);
        SecretsManagerPropertySource propertySource = resource.getPropertySources().createPropertySource(resource.getContext(), resource.isOptional(), sm);
        if (propertySource != null) {
            return new ConfigData(Collections.singletonList(propertySource));
        } else {
            return null;
        }
    } catch (Exception e) {
        throw new ConfigDataResourceNotFoundException(resource, e);
    }
}
Also used : ConfigData(org.springframework.boot.context.config.ConfigData) SecretsManagerPropertySource(io.awspring.cloud.secretsmanager.SecretsManagerPropertySource) ConfigDataResourceNotFoundException(org.springframework.boot.context.config.ConfigDataResourceNotFoundException) SecretsManagerClient(software.amazon.awssdk.services.secretsmanager.SecretsManagerClient) ConfigDataResourceNotFoundException(org.springframework.boot.context.config.ConfigDataResourceNotFoundException) Nullable(org.springframework.lang.Nullable)

Example 2 with SecretsManagerPropertySource

use of io.awspring.cloud.secretsmanager.SecretsManagerPropertySource in project spring-cloud-aws by awspring.

the class SecretsManagerPropertySources method createPropertySource.

/**
 * Creates property source for given context.
 * @param context property source context equivalent to the secret name
 * @param optional if creating context should fail with exception if secret cannot be loaded
 * @param client Secret Manager client
 * @return a property source or null if secret could not be loaded and optional is set to true
 */
@Nullable
public SecretsManagerPropertySource createPropertySource(String context, boolean optional, SecretsManagerClient client) {
    Assert.notNull(context, "context is required");
    Assert.notNull(client, "SecretsManagerClient is required");
    LOG.info("Loading secrets from AWS Secret Manager secret with name: " + context + ", optional: " + optional);
    try {
        SecretsManagerPropertySource propertySource = new SecretsManagerPropertySource(context, client);
        propertySource.init();
        return propertySource;
    // TODO: howto call close when /refresh
    } catch (Exception e) {
        if (!optional) {
            throw new AwsSecretsManagerPropertySourceNotFoundException(e);
        } else {
            LOG.warn("Unable to load AWS secret from " + context + ". " + e.getMessage());
        }
    }
    return null;
}
Also used : SecretsManagerPropertySource(io.awspring.cloud.secretsmanager.SecretsManagerPropertySource) Nullable(org.springframework.lang.Nullable)

Aggregations

SecretsManagerPropertySource (io.awspring.cloud.secretsmanager.SecretsManagerPropertySource)2 Nullable (org.springframework.lang.Nullable)2 ConfigData (org.springframework.boot.context.config.ConfigData)1 ConfigDataResourceNotFoundException (org.springframework.boot.context.config.ConfigDataResourceNotFoundException)1 SecretsManagerClient (software.amazon.awssdk.services.secretsmanager.SecretsManagerClient)1