Skip to content
This repository was archived by the owner on Jan 19, 2022. It is now read-only.

Cannot add several AwsSecretsManagerPropertySourceLocator in a project #472

Closed
anthony-foulfoin opened this issue Jun 25, 2019 · 1 comment
Labels
component: secrets-manager Secrets Manager integration related issue status: waiting-for-triage An issue we've not yet triaged

Comments

@anthony-foulfoin
Copy link

Enhancement

Suppose you have several AWS Secrets you want to add in your application. I did it the same way for having multiple datasources in Spring Boot.

These 2 AWS Secrets are for instance:

  • my-app-rds-dev
  • salesforce-dev

They are separated because some apps might want the salesforce credentials, without the rds which is specific to each app. To do so I modified the bootstrap.yml to add a new level which is the name of the secret source:

aws:
  secretsmanager:
    rds:
      default-context: my-app-rds-dev
    salesforce:
      default-context: salesforce-dev

Of course the AwsSecretsManagerBootstrapConfiguration would crash with this because it doesn't match the expected AwsSecretsManagerProperties.

So I overloaded the beans this way in an @Configuration class:

@Configuration
public class AwsSecretsManagerBootstrapConfiguration {

	@Bean
	@ConfigurationProperties("aws.secretsmanager.rds")
	AwsSecretsManagerProperties rdsSecretsProperties() {
		return new AwsSecretsManagerProperties();
	}

	@Bean
	AwsSecretsManagerPropertySourceLocator salesforceSecretsManagerPropertySourceLocator(AWSSecretsManager smClient, AwsSecretsManagerProperties salesforceSecretsProperties) {
		return new AwsSecretsManagerPropertySourceLocator(smClient, salesforceSecretsProperties);
	}

	@Bean
	@ConfigurationProperties("aws.secretsmanager.salesforce")
	AwsSecretsManagerProperties salesforceSecretsProperties() {
		return new AwsSecretsManagerProperties();
	}

	@Bean
	AwsSecretsManagerPropertySourceLocator rdsSecretsManagerPropertySourceLocator(AWSSecretsManager smClient, AwsSecretsManagerProperties rdsSecretsProperties) {
		return new AwsSecretsManagerPropertySourceLocator(smClient, rdsSecretsProperties);
	}

	@Bean
	@ConditionalOnMissingBean
	AWSSecretsManager smClient() {
		return AWSSecretsManagerClientBuilder.defaultClient();
	}
}

Creating 2 PropertySourceLocator beans should work because PropertySourceBootstrapConfiguration creates a composite CompositePropertySource from all the PropertySourceLocator it finds.

But it doesn't work, for a very simple reason: in CompositePropertySource the sources are stored in a Set. The hashCode of a PropertySource is the hash of its name.

Which leads to the issue: in AwsSecretsManagerPropertySourceLocator.locate() the CompositePropertySource name is always set to "aws-secrets-manager". If you create 2 AwsSecretsManagerPropertySourceLocator they will have the same name, and then the same hashCode that will be considered as a duplicate in the Set of CompositePropertySource.

The solution is easy and straightforward: allow to set the PropertySourceLocator name in the constructor so you can have 2 different names (first constructor property):

	@Bean
	AwsSecretsManagerPropertySourceLocator salesforceSecretsManagerPropertySourceLocator(AWSSecretsManager smClient, AwsSecretsManagerProperties salesforceSecretsProperties) {
		return new AwsSecretsManagerPropertySourceLocator("aws-secrets-manager-rds", smClient, salesforceSecretsProperties);
	}

	@Bean
	AwsSecretsManagerPropertySourceLocator rdsSecretsManagerPropertySourceLocator(AWSSecretsManager smClient, AwsSecretsManagerProperties rdsSecretsProperties) {
		return new AwsSecretsManagerPropertySourceLocator("aws-secrets-manager-sf", smClient, rdsSecretsProperties);
	}

Problem solved !

@maciejwalkowiak
Copy link
Contributor

@anthofo thanks for reporting an issue and PR. Since it stays backward compatible we can merge it now to branch 2.2.x but i am wondering if it would make sense to support an example you provided out of the box:

aws:
  secretsmanager:
    rds:
      default-context: my-app-rds-dev
    salesforce:
      default-context: salesforce-dev

maciejwalkowiak pushed a commit that referenced this issue Jun 3, 2020
tmnuwan12 pushed a commit to tmnuwan12/spring-cloud-aws that referenced this issue Jun 7, 2020
maciejwalkowiak pushed a commit to maciejwalkowiak/spring-cloud-aws that referenced this issue Oct 15, 2020
maciejwalkowiak pushed a commit to maciejwalkowiak/spring-cloud-aws that referenced this issue Oct 15, 2020
maciejwalkowiak pushed a commit to maciejwalkowiak/spring-cloud-aws that referenced this issue Oct 15, 2020
maciejwalkowiak pushed a commit to maciejwalkowiak/spring-cloud-aws that referenced this issue Oct 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
component: secrets-manager Secrets Manager integration related issue status: waiting-for-triage An issue we've not yet triaged
Development

Successfully merging a pull request may close this issue.

3 participants