Skip to content

Commit

Permalink
Adding a flag to disable multiple context support. This relates to is… (
Browse files Browse the repository at this point in the history
#12)

* Adding a flag to disable multiple context support. This relates to issue: #10
  • Loading branch information
fredboutin authored Jan 6, 2020
1 parent 4f641cc commit 4ea69dd
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.coveo</groupId>
<artifactId>spring-boot-parameter-store-integration</artifactId>
<version>1.1.2</version>
<version>1.2.0</version>

<name>Spring Boot Parameter Store Integration</name>
<description>An integration of Amazon Web Services' Systems Manager Parameter Store for Spring Boot's properties injection.</description>
Expand Down
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ If you want to halt the boot when a property prefixed with `/` isn't found in th

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)).

Spring Cloud has a second application context named bootstrap that gets initialized before Spring Boot's normal application context. Since this lib uses an EnvironmentPostPrecessor to add the Parameter Store PropertySource, it gets triggered twice. Since you probably want it to be added on the bootstrap context, we highly recommend using the bootstrap properties to enable this lib. More details on this [Stack Overflow Thread](https://stackoverflow.com/questions/50935915/adding-a-conditional-external-propertysource-in-a-spring-boot-application).
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).

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.

## Contributing
Open an issue to report bugs or to request additional features. Pull requests are always welcome.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class ParameterStorePropertySourceEnvironmentPostProcessor implements Env
static final String PARAMETER_STORE_HALT_BOOT_CONFIGURATION_PROPERTY = "awsParameterStorePropertySource.haltBoot";
static final String PARAMETER_STORE_CLIENT_ENDPOINT_CONFIGURATION_PROPERTY = "awsParameterStoreSource.ssmClient.endpointConfiguration.endpoint";
static final String PARAMETER_STORE_CLIENT_ENDPOINT_SIGNING_REGION_CONFIGURATION_PROPERTY = "awsParameterStoreSource.ssmClient.endpointConfiguration.signingRegion";
static final String PARAMETER_STORE_SUPPORT_MULTIPLE_APPLICATION_CONTEXTS_CONFIGURATION_PROPERTY = "awsParameterStorePropertySource.supportMultipleApplicationContexts";

private static final String PARAMETER_STORE_PROPERTY_SOURCE_NAME = "AWSParameterStorePropertySource";

Expand All @@ -34,7 +35,11 @@ public void postProcessEnvironment(ConfigurableEnvironment environment, SpringAp
environment.getProperty(PARAMETER_STORE_HALT_BOOT_CONFIGURATION_PROPERTY,
Boolean.class,
Boolean.FALSE))));
initialized = true;
if (!environment.getProperty(PARAMETER_STORE_SUPPORT_MULTIPLE_APPLICATION_CONTEXTS_CONFIGURATION_PROPERTY,
Boolean.class,
Boolean.FALSE)) {
initialized = true;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public void setUp()
when(configurableEnvironmentMock.getProperty(PARAMETER_STORE_HALT_BOOT_CONFIGURATION_PROPERTY,
Boolean.class,
Boolean.FALSE)).thenReturn(Boolean.FALSE);
when(configurableEnvironmentMock.getProperty(PARAMETER_STORE_SUPPORT_MULTIPLE_APPLICATION_CONTEXTS_CONFIGURATION_PROPERTY,
Boolean.class,
Boolean.FALSE)).thenReturn(Boolean.FALSE);
when(configurableEnvironmentMock.getPropertySources()).thenReturn(mutablePropertySourcesMock);

System.setProperty(ACCESS_KEY_ENV_VAR, "id");
Expand Down Expand Up @@ -134,4 +137,23 @@ public void testParameterStorePropertySourceEnvironmentPostProcessorCantBeCalled

verify(mutablePropertySourcesMock, times(1)).addFirst(any(ParameterStorePropertySource.class));
}

@Test
public void testParameterStorePropertySourceEnvironmentPostProcessorCanBeCalledTwiceWhenDiablingMultipleContextSupport()
{
when(configurableEnvironmentMock.getProperty(PARAMETER_STORE_ENABLED_CONFIGURATION_PROPERTY,
Boolean.class,
Boolean.FALSE)).thenReturn(Boolean.TRUE);
when(configurableEnvironmentMock.getProperty(PARAMETER_STORE_SUPPORT_MULTIPLE_APPLICATION_CONTEXTS_CONFIGURATION_PROPERTY,
Boolean.class,
Boolean.FALSE)).thenReturn(Boolean.TRUE);

parameterStorePropertySourceEnvironmentPostProcessor.postProcessEnvironment(configurableEnvironmentMock,
applicationMock);

parameterStorePropertySourceEnvironmentPostProcessor.postProcessEnvironment(configurableEnvironmentMock,
applicationMock);

verify(mutablePropertySourcesMock, times(2)).addFirst(any(ParameterStorePropertySource.class));
}
}

0 comments on commit 4ea69dd

Please sign in to comment.