Skip to content

Secrets Manager config import not using custom AWSSecretsManager bean #136

@sberss

Description

@sberss

Type: Bug

Component:

Secrets Manager

Describe the bug
Version: 2.3.1

Hi 👋,

I'm having a problem with the new style of secrets retrieval that was introduced spring-attic/spring-cloud-aws#721. This new mechanism is great as it allows us more flexibility in how we store our secrets, however I am having an issue getting it to work with our spring cloud boostrap configuration class. We have a custom definition of the AWSSecretsManager bean as we need to do some custom configuration to get to the right secretsmanager (basically just SSO + role assumption). This worked great with the old method, but I cannot get it to work with the new method.

It seems in this new mechanism the AWSSecretsManager bean is created by calling a static method that registers the bean to the bootstrap context if it is absent, however it seems to think that the bean is absent despite me having defined the bean in our bootstrap configuration.

protected AWSSecretsManager createAwsSecretsManagerClient(BootstrapContext context) {
AwsSecretsManagerProperties properties = context.get(AwsSecretsManagerProperties.class);
return AwsSecretsManagerBootstrapConfiguration.createSecretsManagerClient(properties);
}

protected <T> void registerBean(ConfigDataLocationResolverContext context, Class<T> type,
BootstrapRegistry.InstanceSupplier<T> supplier) {
ConfigurableBootstrapContext bootstrapContext = context.getBootstrapContext();
bootstrapContext.registerIfAbsent(type, supplier);
}

This is not behaving as I would expect it to, however my understanding here is quite limited and I may be completely misunderstanding what this is trying to do.

Sample
Here is a really dumb example that should illustrate my issue. The credentials are hard-coded in my AWSSecretsManager bean. This will not be used as the AwsSecretsManagerConfigDataLocationResolver builds the secretsmanager client itself, and thus I get an error that no credentials have been provided.

AwsSecretsManagerBootstrapConfiguration.java:

package com.example;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicSessionCredentials;
import com.amazonaws.services.secretsmanager.AWSSecretsManager;
import com.amazonaws.services.secretsmanager.AWSSecretsManagerClientBuilder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.aws.core.SpringCloudClientConfiguration;
import org.springframework.cloud.aws.secretsmanager.AwsSecretsManagerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration()
public class AwsSecretsManagerBootstrapConfiguration {

  @Bean
  @ConditionalOnMissingBean
  AWSSecretsManager smClient(AwsSecretsManagerProperties awsSecretsManagerProperties) {

    AWSSecretsManagerClientBuilder smClientBuilder =
        AWSSecretsManagerClientBuilder.standard()
            .withClientConfiguration(SpringCloudClientConfiguration.getClientConfiguration())
            .withRegion(awsSecretsManagerProperties.getRegion());

    BasicSessionCredentials sessionCredentials =
      new BasicSessionCredentials("REDACTEDACCESSKEY", "REDACTEDSECRETKEY");

    smClientBuilder.withCredentials(new AWSStaticCredentialsProvider(sessionCredentials));

    return smClientBuilder.build();
  }
}

resources/META-INF/spring.factories:

org.springframework.cloud.bootstrap.BootstrapConfiguration=com.example.AwsSecretsManagerBootstrapConfiguration

resources/bootstrap.properties:

...
spring.config.import=aws-secretsmanager:/foo/bar;/baz/qux;

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions