Skip to content

Commit 7bc0cb2

Browse files
authored
Fix/remove initialized flag (#17)
* Remove the flag to initialize only once. In a Spring Cloud context, it should be initialized twice to get higher on the property sources list. Otherwise it's high on the bootstrap context, but once the normal Spring Boot context starts it is lower (assuming no Spring Config Server is used). * Update readme
1 parent 6651b47 commit 7bc0cb2

File tree

6 files changed

+11
-62
lines changed

6 files changed

+11
-62
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.coveo</groupId>
77
<artifactId>spring-boot-parameter-store-integration</artifactId>
8-
<version>1.3.0</version>
8+
<version>1.3.1</version>
99

1010
<name>Spring Boot Parameter Store Integration</name>
1111
<description>An integration of Amazon Web Services' Systems Manager Parameter Store for Spring Boot's properties injection.</description>

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ If you want to halt the boot when a property prefixed with `/` isn't found in th
7777

7878
TL;DR: Define the enabling properties in the bootstrap properties (`bootstrap.yml`, `bootstrap.properties`, [etc.](https://cloud.spring.io/spring-cloud-static/spring-cloud.html#_the_bootstrap_application_context))(see [Unleashing the Magic](#there-are-3-ways-to-enable-this-lib-after-importing-it-in-your-pomxml-pick-yours)).
7979

80-
Spring Cloud has a second application context named bootstrap that gets initialized before Spring Boot's normal application context. Since this library uses an EnvironmentPostPrecessor to add the Parameter Store PropertySource, it would normally be triggered twice. To prevent this, the post processor will only run in the first context that triggers it. For this reason, you must use the bootstrap properties to enable this library since it is the first one to run. More details on this [Stack Overflow Thread](https://stackoverflow.com/questions/50935915/adding-a-conditional-external-propertysource-in-a-spring-boot-application).
80+
Spring Cloud has a second application context named bootstrap that gets initialized before Spring Boot's normal application context. Since this library uses an EnvironmentPostPrecessor to add the Parameter Store PropertySource, it will get triggered twice if you enabled in the bootstrap properties. This allows it to work in both context and to be on top of the property sources in both. For this reason, if you need to fetch Parameter Store properties in the bootstrap context, you should use the bootstrap properties to enable the library. Otherwise you can enable it in the normal Spring Boot context and it will work fine.
8181

8282
If you still want the post processor to run twice or if you are using [spring-boot-devtools](https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-devtools-restart), you can set the optional property `awsParameterStorePropertySource.supportMultipleApplicationContexts` to `true`. The default property value is `false`to prevent multiple initializations. If you are also using Spring Cloud, this property will only work if set in the bootstrap properties.
8383

src/main/java/com/coveo/configuration/parameterstore/ParameterStorePropertySourceConfigurationProperties.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ public final class ParameterStorePropertySourceConfigurationProperties
1313
public static final String ENABLED = joinWithDot(PROPERTY_SOURCE_PREFIX, "enabled");
1414
public static final String ACCEPTED_PROFILES = joinWithDot(PROPERTY_SOURCE_PREFIX, "enabledProfiles");
1515
public static final String HALT_BOOT = joinWithDot(PROPERTY_SOURCE_PREFIX, "haltBoot");
16-
public static final String SUPPORT_MULTIPLE_APPLICATION_CONTEXTS = joinWithDot(PROPERTY_SOURCE_PREFIX,
17-
"supportMultipleApplicationContexts");
1816

1917
public static final String SSM_CLIENT_CUSTOM_ENDPOINT = joinWithDot(SSM_CLIENT_ENDPOINT_CONFIG_PREFIX, "endpoint");
2018
public static final String SSM_CLIENT_SIGNING_REGION = joinWithDot(SSM_CLIENT_ENDPOINT_CONFIG_PREFIX,

src/main/java/com/coveo/configuration/parameterstore/ParameterStorePropertySourceEnvironmentPostProcessor.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,23 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.env.EnvironmentPostProcessor;
5+
import org.springframework.core.Ordered;
56
import org.springframework.core.env.ConfigurableEnvironment;
67
import org.springframework.util.ObjectUtils;
78

89
import com.coveo.configuration.parameterstore.strategy.ParameterStorePropertySourceConfigurationStrategy;
910
import com.coveo.configuration.parameterstore.strategy.ParameterStorePropertySourceConfigurationStrategyFactory;
1011
import com.coveo.configuration.parameterstore.strategy.StrategyType;
1112

12-
public class ParameterStorePropertySourceEnvironmentPostProcessor implements EnvironmentPostProcessor
13+
public class ParameterStorePropertySourceEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered
1314
{
14-
static boolean initialized;
1515
static ParameterStorePropertySourceConfigurationStrategyFactory strategyFactory = new ParameterStorePropertySourceConfigurationStrategyFactory();
1616

1717
@Override
1818
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application)
1919
{
20-
if (!initialized && isParameterStorePropertySourceEnabled(environment)) {
20+
if (isParameterStorePropertySourceEnabled(environment)) {
2121
getParameterStorePropertySourceConfigurationStrategy(environment).configureParameterStorePropertySources(environment);
22-
23-
if (doesNotSupportMultipleApplicationContexts(environment)) {
24-
initialized = true;
25-
}
2622
}
2723
}
2824

@@ -44,15 +40,14 @@ private boolean isParameterStorePropertySourceEnabled(ConfigurableEnvironment en
4440
&& environment.acceptsProfiles(userDefinedEnabledProfiles));
4541
}
4642

47-
private boolean doesNotSupportMultipleApplicationContexts(ConfigurableEnvironment environment)
43+
private boolean isMultiRegionEnabled(ConfigurableEnvironment environment)
4844
{
49-
return !environment.getProperty(ParameterStorePropertySourceConfigurationProperties.SUPPORT_MULTIPLE_APPLICATION_CONTEXTS,
50-
Boolean.class,
51-
Boolean.FALSE);
45+
return environment.containsProperty(ParameterStorePropertySourceConfigurationProperties.MULTI_REGION_SSM_CLIENT_REGIONS);
5246
}
5347

54-
private boolean isMultiRegionEnabled(ConfigurableEnvironment environment)
48+
@Override
49+
public int getOrder()
5550
{
56-
return environment.containsProperty(ParameterStorePropertySourceConfigurationProperties.MULTI_REGION_SSM_CLIENT_REGIONS);
51+
return Ordered.LOWEST_PRECEDENCE;
5752
}
5853
}

src/main/java/com/coveo/configuration/parameterstore/strategy/ParameterStorePropertySourceConfigurationStrategyFactory.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ public class ParameterStorePropertySourceConfigurationStrategyFactory
1111
static {
1212
strategies.put(StrategyType.DEFAULT,
1313
new DefaultParameterStorePropertySourceConfigurationStrategy(new DefaultAwsRegionProviderChain()));
14-
strategies.put(StrategyType.MULTI_REGION,
15-
new MultiRegionParameterStorePropertySourceConfigurationStrategy());
14+
strategies.put(StrategyType.MULTI_REGION, new MultiRegionParameterStorePropertySourceConfigurationStrategy());
1615
}
1716

1817
public ParameterStorePropertySourceConfigurationStrategy getStrategy(StrategyType strategyType)

src/test/java/com/coveo/configuration/parameterstore/ParameterStorePropertySourceEnvironmentPostProcessorTest.java

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,13 @@ public class ParameterStorePropertySourceEnvironmentPostProcessorTest
4343
@Before
4444
public void setUp()
4545
{
46-
ParameterStorePropertySourceEnvironmentPostProcessor.initialized = false;
47-
4846
when(strategyFactoryMock.getStrategy(StrategyType.DEFAULT)).thenReturn(defaultPostProcessStrategyMock);
4947
when(strategyFactoryMock.getStrategy(StrategyType.MULTI_REGION)).thenReturn(multiRegionPostProcessStrategyMock);
5048
ParameterStorePropertySourceEnvironmentPostProcessor.strategyFactory = strategyFactoryMock;
5149

5250
when(configurableEnvironmentMock.getProperty(ParameterStorePropertySourceConfigurationProperties.ENABLED,
5351
Boolean.class,
5452
Boolean.FALSE)).thenReturn(Boolean.FALSE);
55-
when(configurableEnvironmentMock.getProperty(ParameterStorePropertySourceConfigurationProperties.SUPPORT_MULTIPLE_APPLICATION_CONTEXTS,
56-
Boolean.class,
57-
Boolean.FALSE)).thenReturn(Boolean.FALSE);
5853

5954
System.setProperty(ACCESS_KEY_ENV_VAR, "id");
6055
System.setProperty(SECRET_KEY_ENV_VAR, "secret");
@@ -140,44 +135,6 @@ public void testParameterStoreIsNotEnabledWithCustomProfilesButNoneOfTheProfiles
140135
verifyZeroInteractions(multiRegionPostProcessStrategyMock);
141136
}
142137

143-
@Test
144-
public void testParameterStorePropertySourceEnvironmentPostProcessorCantBeCalledTwice()
145-
{
146-
when(configurableEnvironmentMock.getProperty(ParameterStorePropertySourceConfigurationProperties.ENABLED,
147-
Boolean.class,
148-
Boolean.FALSE)).thenReturn(Boolean.TRUE);
149-
150-
parameterStorePropertySourceEnvironmentPostProcessor.postProcessEnvironment(configurableEnvironmentMock,
151-
applicationMock);
152-
153-
parameterStorePropertySourceEnvironmentPostProcessor.postProcessEnvironment(configurableEnvironmentMock,
154-
applicationMock);
155-
156-
verify(defaultPostProcessStrategyMock).configureParameterStorePropertySources(configurableEnvironmentMock);
157-
verifyZeroInteractions(multiRegionPostProcessStrategyMock);
158-
}
159-
160-
@Test
161-
public void testParameterStorePropertySourceEnvironmentPostProcessorCanBeCalledTwiceWhenDisablingMultipleContextSupport()
162-
{
163-
when(configurableEnvironmentMock.getProperty(ParameterStorePropertySourceConfigurationProperties.ENABLED,
164-
Boolean.class,
165-
Boolean.FALSE)).thenReturn(Boolean.TRUE);
166-
when(configurableEnvironmentMock.getProperty(ParameterStorePropertySourceConfigurationProperties.SUPPORT_MULTIPLE_APPLICATION_CONTEXTS,
167-
Boolean.class,
168-
Boolean.FALSE)).thenReturn(Boolean.TRUE);
169-
170-
parameterStorePropertySourceEnvironmentPostProcessor.postProcessEnvironment(configurableEnvironmentMock,
171-
applicationMock);
172-
173-
parameterStorePropertySourceEnvironmentPostProcessor.postProcessEnvironment(configurableEnvironmentMock,
174-
applicationMock);
175-
176-
verify(defaultPostProcessStrategyMock,
177-
times(2)).configureParameterStorePropertySources(configurableEnvironmentMock);
178-
verifyZeroInteractions(multiRegionPostProcessStrategyMock);
179-
}
180-
181138
@Test
182139
public void testWhenMultiRegionIsEnabled()
183140
{

0 commit comments

Comments
 (0)