-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from coveooss/feature/retrieve-ssm-params-from…
…-others-regions Added a flag to support retrieving parameters from different regions.
- Loading branch information
Showing
13 changed files
with
530 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...veo/configuration/parameterstore/ParameterStorePropertySourceConfigurationProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.coveo.configuration.parameterstore; | ||
|
||
public final class ParameterStorePropertySourceConfigurationProperties | ||
{ | ||
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", | ||
"endpointConfiguration"); | ||
|
||
public static final String ENABLED_PROFILE = "awsParameterStorePropertySourceEnabled"; | ||
|
||
public static final String ENABLED = joinWithDot(PROPERTY_SOURCE_PREFIX, "enabled"); | ||
public static final String ACCEPTED_PROFILES = joinWithDot(PROPERTY_SOURCE_PREFIX, "enabledProfiles"); | ||
public static final String HALT_BOOT = joinWithDot(PROPERTY_SOURCE_PREFIX, "haltBoot"); | ||
public static final String SUPPORT_MULTIPLE_APPLICATION_CONTEXTS = joinWithDot(PROPERTY_SOURCE_PREFIX, | ||
"supportMultipleApplicationContexts"); | ||
|
||
public static final String SSM_CLIENT_CUSTOM_ENDPOINT = joinWithDot(SSM_CLIENT_ENDPOINT_CONFIG_PREFIX, "endpoint"); | ||
public static final String SSM_CLIENT_SIGNING_REGION = joinWithDot(SSM_CLIENT_ENDPOINT_CONFIG_PREFIX, | ||
"signingRegion"); | ||
public static final String MULTI_REGION_SSM_CLIENT_REGIONS = joinWithDot(SOURCE_PREFIX, | ||
"multiRegion", | ||
"ssmClient", | ||
"regions"); | ||
|
||
private static String joinWithDot(String... elements) | ||
{ | ||
return String.join(".", elements); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...ion/parameterstore/strategy/DefaultParameterStorePropertySourceConfigurationStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.coveo.configuration.parameterstore.strategy; | ||
|
||
import org.springframework.core.env.ConfigurableEnvironment; | ||
|
||
import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration; | ||
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; | ||
|
||
public class DefaultParameterStorePropertySourceConfigurationStrategy | ||
implements ParameterStorePropertySourceConfigurationStrategy | ||
{ | ||
private static final String PARAMETER_STORE_PROPERTY_SOURCE_NAME = "AWSParameterStorePropertySource"; | ||
|
||
private AwsRegionProviderChain awsRegionProviderChain; | ||
|
||
public DefaultParameterStorePropertySourceConfigurationStrategy(AwsRegionProviderChain awsRegionProviderChain) | ||
{ | ||
this.awsRegionProviderChain = awsRegionProviderChain; | ||
} | ||
|
||
@Override | ||
public void configureParameterStorePropertySources(ConfigurableEnvironment environment) | ||
{ | ||
boolean haltBoot = environment.getProperty(ParameterStorePropertySourceConfigurationProperties.HALT_BOOT, | ||
Boolean.class, | ||
Boolean.FALSE); | ||
environment.getPropertySources() | ||
.addFirst(buildParameterStorePropertySource(buildSSMClient(environment), haltBoot)); | ||
} | ||
|
||
private ParameterStorePropertySource buildParameterStorePropertySource(AWSSimpleSystemsManagement ssmClient, | ||
boolean haltBoot) | ||
{ | ||
return new ParameterStorePropertySource(PARAMETER_STORE_PROPERTY_SOURCE_NAME, | ||
new ParameterStoreSource(ssmClient, haltBoot)); | ||
} | ||
|
||
private AWSSimpleSystemsManagement buildSSMClient(ConfigurableEnvironment environment) | ||
{ | ||
if (hasCustomEndpoint(environment)) { | ||
return AWSSimpleSystemsManagementClientBuilder.standard() | ||
.withEndpointConfiguration(new EndpointConfiguration(getCustomEndpoint(environment), | ||
getSigningRegion(environment))) | ||
.build(); | ||
} | ||
return AWSSimpleSystemsManagementClientBuilder.defaultClient(); | ||
} | ||
|
||
private boolean hasCustomEndpoint(ConfigurableEnvironment environment) | ||
{ | ||
return environment.containsProperty(ParameterStorePropertySourceConfigurationProperties.SSM_CLIENT_CUSTOM_ENDPOINT); | ||
} | ||
|
||
private String getCustomEndpoint(ConfigurableEnvironment environment) | ||
{ | ||
return environment.getProperty(ParameterStorePropertySourceConfigurationProperties.SSM_CLIENT_CUSTOM_ENDPOINT); | ||
} | ||
|
||
private String getSigningRegion(ConfigurableEnvironment environment) | ||
{ | ||
return environment.getProperty(ParameterStorePropertySourceConfigurationProperties.SSM_CLIENT_SIGNING_REGION, | ||
awsRegionProviderChain.getRegion()); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...parameterstore/strategy/MultiRegionParameterStorePropertySourceConfigurationStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.coveo.configuration.parameterstore.strategy; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.springframework.core.env.ConfigurableEnvironment; | ||
import org.springframework.util.CollectionUtils; | ||
|
||
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; | ||
|
||
public class MultiRegionParameterStorePropertySourceConfigurationStrategy | ||
implements ParameterStorePropertySourceConfigurationStrategy | ||
{ | ||
private static final String PARAMETER_STORE_PROPERTY_SOURCE_NAME = "MultiRegionAWSParameterStorePropertySource_"; | ||
|
||
@Override | ||
public void configureParameterStorePropertySources(ConfigurableEnvironment environment) | ||
{ | ||
boolean haltBoot = environment.getProperty(ParameterStorePropertySourceConfigurationProperties.HALT_BOOT, | ||
Boolean.class, | ||
Boolean.FALSE); | ||
|
||
List<String> regions = getRegions(environment); | ||
|
||
// To keep the order of precedence, we have to iterate from the last region to the first one. | ||
// If we want the first region specified to be the first property source, we have to add it last. | ||
// We cannot use addLast since it adds the property source with lowest precedence and we want the | ||
// Parameter store property sources to have highest precedence on the other property sources | ||
Collections.reverse(regions); | ||
String lastRegion = regions.get(0); | ||
|
||
// We only want to halt boot (if true) for the last region | ||
environment.getPropertySources().addFirst(buildParameterStorePropertySource(lastRegion, haltBoot)); | ||
|
||
regions.stream() | ||
.skip(1) | ||
.forEach(region -> environment.getPropertySources() | ||
.addFirst(buildParameterStorePropertySource(region, false))); | ||
} | ||
|
||
private ParameterStorePropertySource buildParameterStorePropertySource(String region, boolean haltBoot) | ||
{ | ||
return new ParameterStorePropertySource(PARAMETER_STORE_PROPERTY_SOURCE_NAME + region, | ||
new ParameterStoreSource(buildSSMClient(region), haltBoot)); | ||
} | ||
|
||
private AWSSimpleSystemsManagement buildSSMClient(String region) | ||
{ | ||
return AWSSimpleSystemsManagementClientBuilder.standard().withRegion(region).build(); | ||
} | ||
|
||
private List<String> getRegions(ConfigurableEnvironment environment) | ||
{ | ||
List<String> regions = CollectionUtils.arrayToList(environment.getProperty(ParameterStorePropertySourceConfigurationProperties.MULTI_REGION_SSM_CLIENT_REGIONS, | ||
String[].class)); | ||
|
||
if (CollectionUtils.isEmpty(regions)) { | ||
throw new IllegalArgumentException(String.format("To enable multi region support, the property '%s' must not be empty.", | ||
ParameterStorePropertySourceConfigurationProperties.MULTI_REGION_SSM_CLIENT_REGIONS)); | ||
} | ||
|
||
return regions; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...figuration/parameterstore/strategy/ParameterStorePropertySourceConfigurationStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.coveo.configuration.parameterstore.strategy; | ||
|
||
import org.springframework.core.env.ConfigurableEnvironment; | ||
|
||
public interface ParameterStorePropertySourceConfigurationStrategy | ||
{ | ||
void configureParameterStorePropertySources(ConfigurableEnvironment environment); | ||
} |
22 changes: 22 additions & 0 deletions
22
...ion/parameterstore/strategy/ParameterStorePropertySourceConfigurationStrategyFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.coveo.configuration.parameterstore.strategy; | ||
|
||
import java.util.EnumMap; | ||
|
||
import com.amazonaws.regions.DefaultAwsRegionProviderChain; | ||
|
||
public class ParameterStorePropertySourceConfigurationStrategyFactory | ||
{ | ||
private static EnumMap<StrategyType, ParameterStorePropertySourceConfigurationStrategy> strategies = new EnumMap<>(StrategyType.class); | ||
|
||
static { | ||
strategies.put(StrategyType.DEFAULT, | ||
new DefaultParameterStorePropertySourceConfigurationStrategy(new DefaultAwsRegionProviderChain())); | ||
strategies.put(StrategyType.MULTI_REGION, | ||
new MultiRegionParameterStorePropertySourceConfigurationStrategy()); | ||
} | ||
|
||
public ParameterStorePropertySourceConfigurationStrategy getStrategy(StrategyType strategyType) | ||
{ | ||
return strategies.get(strategyType); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/coveo/configuration/parameterstore/strategy/StrategyType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.coveo.configuration.parameterstore.strategy; | ||
|
||
public enum StrategyType | ||
{ | ||
DEFAULT, MULTI_REGION; | ||
} |
Oops, something went wrong.