|
5 | 5 | import org.springframework.core.env.ConfigurableEnvironment;
|
6 | 6 | import org.springframework.util.ObjectUtils;
|
7 | 7 |
|
8 |
| -import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration; |
9 |
| -import com.amazonaws.regions.DefaultAwsRegionProviderChain; |
10 |
| -import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagement; |
11 |
| -import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagementClientBuilder; |
| 8 | +import com.coveo.configuration.parameterstore.strategy.ParameterStorePropertySourceConfigurationStrategy; |
| 9 | +import com.coveo.configuration.parameterstore.strategy.ParameterStorePropertySourceConfigurationStrategyFactory; |
| 10 | +import com.coveo.configuration.parameterstore.strategy.StrategyType; |
12 | 11 |
|
13 | 12 | public class ParameterStorePropertySourceEnvironmentPostProcessor implements EnvironmentPostProcessor
|
14 | 13 | {
|
15 |
| - static final String PARAMETER_STORE_ACCEPTED_PROFILE = "awsParameterStorePropertySourceEnabled"; |
16 |
| - |
17 |
| - static final String PARAMETER_STORE_ACCEPTED_PROFILES_CONFIGURATION_PROPERTY = "awsParameterStorePropertySource.enabledProfiles"; |
18 |
| - static final String PARAMETER_STORE_ENABLED_CONFIGURATION_PROPERTY = "awsParameterStorePropertySource.enabled"; |
19 |
| - static final String PARAMETER_STORE_HALT_BOOT_CONFIGURATION_PROPERTY = "awsParameterStorePropertySource.haltBoot"; |
20 |
| - static final String PARAMETER_STORE_CLIENT_ENDPOINT_CONFIGURATION_PROPERTY = "awsParameterStoreSource.ssmClient.endpointConfiguration.endpoint"; |
21 |
| - static final String PARAMETER_STORE_CLIENT_ENDPOINT_SIGNING_REGION_CONFIGURATION_PROPERTY = "awsParameterStoreSource.ssmClient.endpointConfiguration.signingRegion"; |
22 |
| - static final String PARAMETER_STORE_SUPPORT_MULTIPLE_APPLICATION_CONTEXTS_CONFIGURATION_PROPERTY = "awsParameterStorePropertySource.supportMultipleApplicationContexts"; |
23 |
| - |
24 |
| - private static final String PARAMETER_STORE_PROPERTY_SOURCE_NAME = "AWSParameterStorePropertySource"; |
25 |
| - |
26 | 14 | static boolean initialized;
|
| 15 | + static ParameterStorePropertySourceConfigurationStrategyFactory strategyFactory = new ParameterStorePropertySourceConfigurationStrategyFactory(); |
27 | 16 |
|
28 | 17 | @Override
|
29 | 18 | public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application)
|
30 | 19 | {
|
31 | 20 | if (!initialized && isParameterStorePropertySourceEnabled(environment)) {
|
32 |
| - environment.getPropertySources() |
33 |
| - .addFirst(new ParameterStorePropertySource(PARAMETER_STORE_PROPERTY_SOURCE_NAME, |
34 |
| - new ParameterStoreSource(buildAWSSimpleSystemsManagementClient(environment), |
35 |
| - environment.getProperty(PARAMETER_STORE_HALT_BOOT_CONFIGURATION_PROPERTY, |
36 |
| - Boolean.class, |
37 |
| - Boolean.FALSE)))); |
38 |
| - if (!environment.getProperty(PARAMETER_STORE_SUPPORT_MULTIPLE_APPLICATION_CONTEXTS_CONFIGURATION_PROPERTY, |
39 |
| - Boolean.class, |
40 |
| - Boolean.FALSE)) { |
| 21 | + getParameterStorePropertySourceConfigurationStrategy(environment).configureParameterStorePropertySources(environment); |
| 22 | + |
| 23 | + if (doesNotSupportMultipleApplicationContexts(environment)) { |
41 | 24 | initialized = true;
|
42 | 25 | }
|
43 | 26 | }
|
44 | 27 | }
|
45 | 28 |
|
| 29 | + private ParameterStorePropertySourceConfigurationStrategy getParameterStorePropertySourceConfigurationStrategy(ConfigurableEnvironment environment) |
| 30 | + { |
| 31 | + StrategyType type = isMultiRegionEnabled(environment) ? StrategyType.MULTI_REGION : StrategyType.DEFAULT; |
| 32 | + return strategyFactory.getStrategy(type); |
| 33 | + } |
| 34 | + |
46 | 35 | private boolean isParameterStorePropertySourceEnabled(ConfigurableEnvironment environment)
|
47 | 36 | {
|
48 |
| - String[] userDefinedEnabledProfiles = environment.getProperty(PARAMETER_STORE_ACCEPTED_PROFILES_CONFIGURATION_PROPERTY, |
| 37 | + String[] userDefinedEnabledProfiles = environment.getProperty(ParameterStorePropertySourceConfigurationProperties.ACCEPTED_PROFILES, |
49 | 38 | String[].class);
|
50 |
| - return environment.getProperty(PARAMETER_STORE_ENABLED_CONFIGURATION_PROPERTY, Boolean.class, Boolean.FALSE) |
51 |
| - || environment.acceptsProfiles(PARAMETER_STORE_ACCEPTED_PROFILE) |
| 39 | + return environment.getProperty(ParameterStorePropertySourceConfigurationProperties.ENABLED, |
| 40 | + Boolean.class, |
| 41 | + Boolean.FALSE) |
| 42 | + || environment.acceptsProfiles(ParameterStorePropertySourceConfigurationProperties.ENABLED_PROFILE) |
52 | 43 | || (!ObjectUtils.isEmpty(userDefinedEnabledProfiles)
|
53 | 44 | && environment.acceptsProfiles(userDefinedEnabledProfiles));
|
54 | 45 | }
|
55 | 46 |
|
56 |
| - private AWSSimpleSystemsManagement buildAWSSimpleSystemsManagementClient(ConfigurableEnvironment environment) |
| 47 | + private boolean doesNotSupportMultipleApplicationContexts(ConfigurableEnvironment environment) |
57 | 48 | {
|
58 |
| - if (environment.containsProperty(PARAMETER_STORE_CLIENT_ENDPOINT_CONFIGURATION_PROPERTY)) { |
59 |
| - return AWSSimpleSystemsManagementClientBuilder.standard() |
60 |
| - .withEndpointConfiguration(new EndpointConfiguration(environment.getProperty(PARAMETER_STORE_CLIENT_ENDPOINT_CONFIGURATION_PROPERTY), |
61 |
| - environment.getProperty(PARAMETER_STORE_CLIENT_ENDPOINT_SIGNING_REGION_CONFIGURATION_PROPERTY, |
62 |
| - new DefaultAwsRegionProviderChain().getRegion()))) |
63 |
| - .build(); |
64 |
| - } |
65 |
| - return AWSSimpleSystemsManagementClientBuilder.defaultClient(); |
| 49 | + return !environment.getProperty(ParameterStorePropertySourceConfigurationProperties.SUPPORT_MULTIPLE_APPLICATION_CONTEXTS, |
| 50 | + Boolean.class, |
| 51 | + Boolean.FALSE); |
| 52 | + } |
| 53 | + |
| 54 | + private boolean isMultiRegionEnabled(ConfigurableEnvironment environment) |
| 55 | + { |
| 56 | + return environment.containsProperty(ParameterStorePropertySourceConfigurationProperties.MULTI_REGION_SSM_CLIENT_REGIONS); |
66 | 57 | }
|
67 | 58 | }
|
0 commit comments