spring-attic / spring-cloud-aws

All development has moved to https://github.com/awspring/spring-cloud-aws Integration for Amazon Web Services APIs with Spring
https://awspring.io/
Apache License 2.0
589 stars 376 forks source link

Can't create a custom PropertySourceLocator that depends on AwsSecretsManagerPropertySourceLocator #783

Closed petersonbr closed 2 years ago

petersonbr commented 2 years ago

Type: Bug

Component: Secrets Manager

Describe the bug Version: spring-cloud-aws: 2.2.6.RELEASE

I needed to create a custom PropertySourceLocator, and this property source needed to be processed after AwsSecretsManagerPropertySourceLocator. I've followed this doc.

I debugged the code and found out that the class PropertySourceBootstrapConfiguration (spring-cloud-context project) is responsible for processing PropertySourceLocator classes. Since this class uses AnnotationAwareOrderComparator.sort(this.propertySourceLocators) to sort available locators, and AwsSecretsManagerPropertySourceLocator has no order defined, it is always processed last.

Alternative 1 - Didn't work for my scenario) Depending on the component you are trying to setup properties, it is possible to use a different approach, but it won't work for all types of components. It worked for camel-aws2-* components, but it didn't work for setting spring datasource properties, for example.

@Configuration
@Priority(Ordered.HIGHEST_PRECEDENCE)
public class MyCustomPropertySource implements InitializingBean {

    @Autowired
    private ConfigurableEnvironment env;

    @Override
    public void afterPropertiesSet() throws Exception {
        Properties props = new Properties();
        props.put("my-prop-key", "my-prop-value");
        env.getPropertySources().addLast(new PropertiesPropertySource("my-custom-props", props));
    }
}

Workaround I ended up doing the following workaround: I've "replaced" class AwsSecretsManagerPropertySourceLocator in my project (copied the class to the project using the same package/class names) and included a @Priority annotation in the class:

@Priority(value = 0)
public class AwsSecretsManagerPropertySourceLocator implements PropertySourceLocator {

After this change, PropertySourceBootstrapConfiguration correctly sorted property locators and my custom property locator could use AwsSecretsManagerPropertySourceLocator properties.

Possible Solution A possible and simple solution is to add a @Priority, @Order or implement Ordered interface in class AwsSecretsManagerPropertySourceLocator. I don't know if there is another way to create a custom PropertySource that depends on AwsSecretsManagerPropertySourceLocator.

maciejwalkowiak commented 2 years ago

Replaced with https://github.com/awspring/spring-cloud-aws/issues/175