Skip to content

Commit

Permalink
Add a property to configure the max error retry on the ssm client use… (
Browse files Browse the repository at this point in the history
#63)

* Add a property to configure the max error retry on the ssm client used in the property source.

* Bump version in the read me
  • Loading branch information
fredboutin authored Aug 26, 2020
1 parent 03361e3 commit 9cb701b
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 35 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.4.0</version>
<version>1.5.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
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ The library was tested and worked properly with:
<dependency>
<groupId>com.coveo</groupId>
<artifactId>spring-boot-parameter-store-integration</artifactId>
<version>1.4.0</version>
<version>1.5.0</version>
</dependency>
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

public final class ParameterStorePropertySourceConfigurationProperties
{
private static final String SSM_CLIENT = "ssmClient";
private static final String PROPERTY_SOURCE_PREFIX = "awsParameterStorePropertySource";
private static final String SOURCE_PREFIX = "awsParameterStoreSource";
private static final String SSM_CLIENT_ENDPOINT_CONFIG_PREFIX = joinWithDot(SOURCE_PREFIX,
"ssmClient",
SSM_CLIENT,
"endpointConfiguration");

public static final String ENABLED_PROFILE = "awsParameterStorePropertySourceEnabled";
Expand All @@ -19,8 +20,9 @@ public final class ParameterStorePropertySourceConfigurationProperties
"signingRegion");
public static final String MULTI_REGION_SSM_CLIENT_REGIONS = joinWithDot(SOURCE_PREFIX,
"multiRegion",
"ssmClient",
SSM_CLIENT,
"regions");
public static final String MAX_ERROR_RETRY = joinWithDot(SOURCE_PREFIX, SSM_CLIENT, "maxErrorRetry");

private static String joinWithDot(String... elements)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.util.ObjectUtils;

import com.amazonaws.ClientConfigurationFactory;
import com.amazonaws.retry.PredefinedRetryPolicies;
import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagementClientBuilder;
import com.coveo.configuration.parameterstore.strategy.ParameterStorePropertySourceConfigurationStrategy;
import com.coveo.configuration.parameterstore.strategy.ParameterStorePropertySourceConfigurationStrategyFactory;
import com.coveo.configuration.parameterstore.strategy.StrategyType;
Expand All @@ -18,10 +21,20 @@ public class ParameterStorePropertySourceEnvironmentPostProcessor implements Env
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application)
{
if (isParameterStorePropertySourceEnabled(environment)) {
getParameterStorePropertySourceConfigurationStrategy(environment).configureParameterStorePropertySources(environment);
getParameterStorePropertySourceConfigurationStrategy(environment).configureParameterStorePropertySources(environment,
preconfigureSSMClientBuilder(environment));
}
}

private AWSSimpleSystemsManagementClientBuilder preconfigureSSMClientBuilder(ConfigurableEnvironment environment)
{
return AWSSimpleSystemsManagementClientBuilder.standard()
.withClientConfiguration(new ClientConfigurationFactory().getConfig()
.withRetryPolicy(PredefinedRetryPolicies.getDefaultRetryPolicyWithCustomMaxRetries(environment.getProperty(ParameterStorePropertySourceConfigurationProperties.MAX_ERROR_RETRY,
Integer.class,
PredefinedRetryPolicies.DEFAULT_MAX_ERROR_RETRY))));
}

private ParameterStorePropertySourceConfigurationStrategy getParameterStorePropertySourceConfigurationStrategy(ConfigurableEnvironment environment)
{
StrategyType type = isMultiRegionEnabled(environment) ? StrategyType.MULTI_REGION : StrategyType.DEFAULT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.amazonaws.regions.AwsRegionProviderChain;
import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagement;
import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagementClientBuilder;

import com.coveo.configuration.parameterstore.ParameterStorePropertySource;
import com.coveo.configuration.parameterstore.ParameterStorePropertySourceConfigurationProperties;
import com.coveo.configuration.parameterstore.ParameterStoreSource;
Expand All @@ -24,13 +23,15 @@ public DefaultParameterStorePropertySourceConfigurationStrategy(AwsRegionProvide
}

@Override
public void configureParameterStorePropertySources(ConfigurableEnvironment environment)
public void configureParameterStorePropertySources(ConfigurableEnvironment environment,
AWSSimpleSystemsManagementClientBuilder ssmClientBuilder)
{
boolean haltBoot = environment.getProperty(ParameterStorePropertySourceConfigurationProperties.HALT_BOOT,
Boolean.class,
Boolean.FALSE);
environment.getPropertySources()
.addFirst(buildParameterStorePropertySource(buildSSMClient(environment), haltBoot));
.addFirst(buildParameterStorePropertySource(buildSSMClient(environment, ssmClientBuilder),
haltBoot));
}

private ParameterStorePropertySource buildParameterStorePropertySource(AWSSimpleSystemsManagement ssmClient,
Expand All @@ -40,15 +41,15 @@ private ParameterStorePropertySource buildParameterStorePropertySource(AWSSimple
new ParameterStoreSource(ssmClient, haltBoot));
}

private AWSSimpleSystemsManagement buildSSMClient(ConfigurableEnvironment environment)
private AWSSimpleSystemsManagement buildSSMClient(ConfigurableEnvironment environment,
AWSSimpleSystemsManagementClientBuilder ssmClientBuilder)
{
if (hasCustomEndpoint(environment)) {
return AWSSimpleSystemsManagementClientBuilder.standard()
.withEndpointConfiguration(new EndpointConfiguration(getCustomEndpoint(environment),
getSigningRegion(environment)))
.build();
return ssmClientBuilder.withEndpointConfiguration(new EndpointConfiguration(getCustomEndpoint(environment),
getSigningRegion(environment)))
.build();
}
return AWSSimpleSystemsManagementClientBuilder.defaultClient();
return ssmClientBuilder.build();
}

private boolean hasCustomEndpoint(ConfigurableEnvironment environment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagement;
import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagementClientBuilder;

import com.coveo.configuration.parameterstore.ParameterStorePropertySource;
import com.coveo.configuration.parameterstore.ParameterStorePropertySourceConfigurationProperties;
import com.coveo.configuration.parameterstore.ParameterStoreSource;
Expand All @@ -19,7 +18,8 @@ public class MultiRegionParameterStorePropertySourceConfigurationStrategy
private static final String PARAMETER_STORE_PROPERTY_SOURCE_NAME = "MultiRegionAWSParameterStorePropertySource_";

@Override
public void configureParameterStorePropertySources(ConfigurableEnvironment environment)
public void configureParameterStorePropertySources(ConfigurableEnvironment environment,
AWSSimpleSystemsManagementClientBuilder ssmClientBuilder)
{
boolean haltBoot = environment.getProperty(ParameterStorePropertySourceConfigurationProperties.HALT_BOOT,
Boolean.class,
Expand All @@ -35,23 +35,29 @@ public void configureParameterStorePropertySources(ConfigurableEnvironment envir
String lastRegion = regions.get(0);

// We only want to halt boot (if true) for the last region
environment.getPropertySources().addFirst(buildParameterStorePropertySource(lastRegion, haltBoot));
environment.getPropertySources()
.addFirst(buildParameterStorePropertySource(ssmClientBuilder, lastRegion, haltBoot));

regions.stream()
.skip(1)
.forEach(region -> environment.getPropertySources()
.addFirst(buildParameterStorePropertySource(region, false)));
.addFirst(buildParameterStorePropertySource(ssmClientBuilder,
region,
false)));
}

private ParameterStorePropertySource buildParameterStorePropertySource(String region, boolean haltBoot)
private ParameterStorePropertySource buildParameterStorePropertySource(AWSSimpleSystemsManagementClientBuilder ssmClientBuilder,
String region,
boolean haltBoot)
{
return new ParameterStorePropertySource(PARAMETER_STORE_PROPERTY_SOURCE_NAME + region,
new ParameterStoreSource(buildSSMClient(region), haltBoot));
return new ParameterStorePropertySource(PARAMETER_STORE_PROPERTY_SOURCE_NAME
+ region, new ParameterStoreSource(buildSSMClient(ssmClientBuilder, region), haltBoot));
}

private AWSSimpleSystemsManagement buildSSMClient(String region)
private AWSSimpleSystemsManagement buildSSMClient(AWSSimpleSystemsManagementClientBuilder ssmClientBuilder,
String region)
{
return AWSSimpleSystemsManagementClientBuilder.standard().withRegion(region).build();
return ssmClientBuilder.withRegion(region).build();
}

private List<String> getRegions(ConfigurableEnvironment environment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import org.springframework.core.env.ConfigurableEnvironment;

import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagementClientBuilder;

public interface ParameterStorePropertySourceConfigurationStrategy
{
void configureParameterStorePropertySources(ConfigurableEnvironment environment);
void configureParameterStorePropertySources(ConfigurableEnvironment environment,
AWSSimpleSystemsManagementClientBuilder ssmClientBuilder);
}
Loading

0 comments on commit 9cb701b

Please sign in to comment.