From 8b7ebf944cbd9c4d00c5a67872b9f04dcc1d4632 Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Thu, 23 Feb 2023 10:16:01 -0500 Subject: [PATCH] Pr 2104 polish (#2233) * Support labels in AWS SecretsManager backend * Document label support in AWS SecretsManager * Use AWS staged labels as config version labels * Fix tests --------- Co-authored-by: Dmitry Antonyuk <32649457+dantonyuk@users.noreply.github.com> Co-authored-by: Ryan Baxter <524254+ryanjbaxter@users.noreply.github.com> --- .../main/asciidoc/spring-cloud-config.adoc | 49 +- ...wsSecretsManagerEnvironmentProperties.java | 14 + ...wsSecretsManagerEnvironmentRepository.java | 25 +- ...retsManagerEnvironmentRepositoryTests.java | 1839 ++++++++++++++++- 4 files changed, 1843 insertions(+), 84 deletions(-) diff --git a/docs/src/main/asciidoc/spring-cloud-config.adoc b/docs/src/main/asciidoc/spring-cloud-config.adoc index bfc19fa4..79096e48 100644 --- a/docs/src/main/asciidoc/spring-cloud-config.adoc +++ b/docs/src/main/asciidoc/spring-cloud-config.adoc @@ -866,7 +866,54 @@ secret value = } ---- -===== AWS Parameter Store +===== Labelled Versions + +AWS Secrets Manager repository allows to keep labelled versions of the configuration environments the same way Git backend does. + +The repository implementation maps the `{label}` parameter of the HTTP resource to https://docs.aws.amazon.com/secretsmanager/latest/userguide/getting-started.html#term_version[AWS Secrets Manager secret's staging label^]. To create a labelled secret, create a secret or update its content and define a staging label for it (sometimes it's called version stage in the AWS documentation). For example: + +[source,sh] +---- +$ aws secretsmanager create-secret \ + --name /secret/test/ \ + --secret-string '{"version":"1"}' +{ + "ARN": "arn:aws:secretsmanager:us-east-1:123456789012:secret:/secret/test/-a1b2c3", + "Name": "/secret/test/", + "VersionId": "cd291674-de2f-41de-8f3b-37dbf4880d69" +} + +$ aws secretsmanager update-secret-version-stage \ + --secret-id /secret/test/ \ + --version-stage 1.0.0 \ + --move-to-version-id cd291674-de2f-41de-8f3b-37dbf4880d69 + +{ + "ARN": "arn:aws:secretsmanager:us-east-1:123456789012:secret:/secret/test/-a1b2c3", + "Name": "/secret/test/", +} +---- + +Use `spring.cloud.config.server.aws-secretsmanager.default-label` property to set the default label. If the property is not defined, the backend uses AWSCURRENT as a staging label. + +[source,yaml] +---- +spring: + profiles: + active: aws-secretsmanager + cloud: + config: + server: + aws-secretsmanager: + region: us-east-1 + default-label: 1.0.0 +---- + +Note that if the default label is not set and a request does not define a label, the repository will use secrets as if labelled version support is disabled. Also, the default label will be used only if the labelled support is enabled. Otherwise, defining this property is pointless. + +Note that if the staging label contains a slash (`/`), then the label in the HTTP URL should instead be specified with the special string `({special-string})` (to avoid ambiguity with other URL paths) the same way <<_git_backend,Git backend's section>> describes it. + +==== AWS Parameter Store When using AWS Parameter Store as a backend, you can share configuration with all applications by placing properties within the `/application` hierarchy. diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/AwsSecretsManagerEnvironmentProperties.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/AwsSecretsManagerEnvironmentProperties.java index 1d693ab2..2895a1da 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/AwsSecretsManagerEnvironmentProperties.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/AwsSecretsManagerEnvironmentProperties.java @@ -48,6 +48,12 @@ public class AwsSecretsManagerEnvironmentProperties implements EnvironmentReposi */ private String endpoint; + /** + * The default staging label to be used to fetch the secret values. If unset, an + * active version of the secret will be fetched (AWSCURRENT). + */ + private String defaultLabel; + /** * The order of the environment repository. */ @@ -91,6 +97,14 @@ public class AwsSecretsManagerEnvironmentProperties implements EnvironmentReposi this.endpoint = endpoint; } + public String getDefaultLabel() { + return defaultLabel; + } + + public void setDefaultLabel(String defaultLabel) { + this.defaultLabel = defaultLabel; + } + public int getOrder() { return order; } diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/AwsSecretsManagerEnvironmentRepository.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/AwsSecretsManagerEnvironmentRepository.java index 3306c286..0443b5e4 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/AwsSecretsManagerEnvironmentRepository.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/AwsSecretsManagerEnvironmentRepository.java @@ -71,6 +71,7 @@ public class AwsSecretsManagerEnvironmentRepository implements EnvironmentReposi public Environment findOne(String application, String profileList, String label) { final String defaultApplication = configServerProperties.getDefaultApplicationName(); final String defaultProfile = configServerProperties.getDefaultProfile(); + final String defaultLabel = environmentProperties.getDefaultLabel(); if (ObjectUtils.isEmpty(application)) { application = defaultApplication; @@ -80,6 +81,10 @@ public class AwsSecretsManagerEnvironmentRepository implements EnvironmentReposi profileList = defaultProfile; } + if (StringUtils.isEmpty(label)) { + label = defaultLabel; + } + String[] profiles = StringUtils.trimArrayElements(StringUtils.commaDelimitedListToStringArray(profileList)); Environment environment = new Environment(application, profiles, label, null, null); @@ -89,33 +94,33 @@ public class AwsSecretsManagerEnvironmentRepository implements EnvironmentReposi } for (String profile : profiles) { - addPropertySource(environment, application, profile); + addPropertySource(environment, application, profile, label); if (!defaultApplication.equals(application)) { - addPropertySource(environment, defaultApplication, profile); + addPropertySource(environment, defaultApplication, profile, label); } } if (!Arrays.asList(profiles).contains(defaultProfile)) { - addPropertySource(environment, application, defaultProfile); + addPropertySource(environment, application, defaultProfile, label); } if (!Arrays.asList(profiles).contains(defaultProfile) && !defaultApplication.equals(application)) { - addPropertySource(environment, defaultApplication, defaultProfile); + addPropertySource(environment, defaultApplication, defaultProfile, label); } if (!defaultApplication.equals(application)) { - addPropertySource(environment, application, null); + addPropertySource(environment, application, null, label); } - addPropertySource(environment, defaultApplication, null); + addPropertySource(environment, defaultApplication, null, label); return environment; } - private void addPropertySource(Environment environment, String application, String profile) { + private void addPropertySource(Environment environment, String application, String profile, String label) { String path = buildPath(application, profile); - Map properties = findProperties(path); + Map properties = findProperties(path, label); if (!properties.isEmpty()) { environment.add(new PropertySource(environmentProperties.getOrigin() + path, properties)); } @@ -133,10 +138,10 @@ public class AwsSecretsManagerEnvironmentRepository implements EnvironmentReposi } } - private Map findProperties(String path) { + private Map findProperties(String path, String label) { Map properties = new HashMap<>(); - GetSecretValueRequest request = GetSecretValueRequest.builder().secretId(path).build(); + GetSecretValueRequest request = GetSecretValueRequest.builder().secretId(path).versionStage(label).build(); try { GetSecretValueResponse response = awsSmClient.getSecretValue(request); diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/AwsSecretsManagerEnvironmentRepositoryTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/AwsSecretsManagerEnvironmentRepositoryTests.java index b552c6c5..35f86316 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/AwsSecretsManagerEnvironmentRepositoryTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/AwsSecretsManagerEnvironmentRepositoryTests.java @@ -40,11 +40,14 @@ import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient; import software.amazon.awssdk.services.secretsmanager.model.CreateSecretRequest; +import software.amazon.awssdk.services.secretsmanager.model.CreateSecretResponse; import software.amazon.awssdk.services.secretsmanager.model.DeleteSecretRequest; +import software.amazon.awssdk.services.secretsmanager.model.UpdateSecretVersionStageRequest; import org.springframework.cloud.config.environment.Environment; import org.springframework.cloud.config.environment.PropertySource; import org.springframework.cloud.config.server.config.ConfigServerProperties; +import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; @@ -79,10 +82,159 @@ public class AwsSecretsManagerEnvironmentRepositoryTests { private final AwsSecretsManagerEnvironmentRepository repository = new AwsSecretsManagerEnvironmentRepository( smClient, configServerProperties, environmentProperties); + private final AwsSecretsManagerEnvironmentProperties labeledEnvironmentProperties = new AwsSecretsManagerEnvironmentProperties(); + + private final AwsSecretsManagerEnvironmentRepository labeledRepository = new AwsSecretsManagerEnvironmentRepository( + smClient, configServerProperties, labeledEnvironmentProperties); + private final ObjectMapper objectMapper = new ObjectMapper().configure(SerializationFeature.INDENT_OUTPUT, true); private final List toBeRemoved = new ArrayList<>(); + private static Map getFooProperties() { + return new HashMap() { + { + put("s3.accessKey", "foo-s3"); + put("s3.secretKey", "ce945da2-740a-4915-a090-2978428dad05"); + } + }; + } + + private static Map getFooDefaultProperties() { + return new HashMap() { + { + put("s3.accessKey", "foo-default-s3"); + put("s3.secretKey", "8c3c58c9-daef-4d21-96b0-c2b68a7a8234"); + } + }; + } + + private static Map getFooProdProperties() { + return new HashMap() { + { + put("s3.accessKey", "foo-prod-s3"); + put("s3.secretKey", "42ca062d-8e4b-435e-9e4a-d058835817c0"); + } + }; + } + + private static Map getFooEastProperties() { + return new HashMap() { + { + put("s3.accessKey", "foo-east-s3"); + put("s3.secretKey", "657f6ac5-2e1c-487d-9d61-1df109b29edf"); + } + }; + } + + private static Map getApplicationDefaultProperties() { + return new HashMap() { + { + put("s3.accessKey", "application-shared-default-s3"); + put("s3.secretKey", "691972aa-68d2-4e55-8d9b-eedd4c63a998"); + } + }; + } + + private static Map getApplicationProdProperties() { + return new HashMap() { + { + put("s3.accessKey", "application-shared-prod-s3"); + put("s3.secretKey", "90c1dd88-5b20-41fa-a4e9-e1d638188732"); + } + }; + } + + private static Map getApplicationEastProperties() { + return new HashMap() { + { + put("s3.accessKey", "application-east-s3"); + put("s3.secretKey", "236e01a7-623b-40f4-88c1-eb4d89229dd6"); + } + }; + } + + private static Map getApplicationProperties() { + return new HashMap() { + { + put("s3.accessKey", "application-shared-s3"); + put("s3.secretKey", "25300773-eb3b-4ace-b6fc-500c87331da7"); + } + }; + } + + private static Map getApplicationReleaseProperties() { + return new HashMap() { + { + put("s3.accessKey", "application-shared-s3"); + put("s3.secretKey", "f616d232-e777-11ec-8fea-0242ac120002"); + } + }; + } + + private static Map getApplicationDefaultReleaseProperties() { + return new HashMap() { + { + put("s3.accessKey", "application-shared-default-s3"); + put("s3.secretKey", "02db4214-e778-11ec-8fea-0242ac120002"); + } + }; + } + + private static Map getApplicationProdReleaseProperties() { + return new HashMap() { + { + put("s3.accessKey", "application-shared-prod-s3"); + put("s3.secretKey", "db0d3eae-e78b-11ec-8fea-0242ac120002"); + } + }; + } + + private static Map getApplicationEastReleaseProperties() { + return new HashMap() { + { + put("s3.accessKey", "application-east-s3"); + put("s3.secretKey", "e7e99834-e78b-11ec-8fea-0242ac120002"); + } + }; + } + + private static Map getFooReleaseProperties() { + return new HashMap() { + { + put("s3.accessKey", "foo-s3"); + put("s3.secretKey", "edec8728-e78b-11ec-8fea-0242ac120002"); + } + }; + } + + private static Map getFooDefaultReleaseProperties() { + return new HashMap() { + { + put("s3.accessKey", "foo-default-s3"); + put("s3.secretKey", "f3ebef4c-e78b-11ec-8fea-0242ac120002"); + } + }; + } + + private static Map getFooProdReleaseProperties() { + return new HashMap() { + { + put("s3.accessKey", "foo-prod-s3"); + put("s3.secretKey", "004ba75a-e78c-11ec-8fea-0242ac120002"); + } + }; + } + + private static Map getFooEastReleaseProperties() { + return new HashMap() { + { + put("s3.accessKey", "foo-east-s3"); + put("s3.secretKey", "044f287c-e78c-11ec-8fea-0242ac120002"); + } + }; + } + @AfterEach public void cleanUp() { toBeRemoved.forEach(value -> smClient @@ -90,6 +242,1584 @@ public class AwsSecretsManagerEnvironmentRepositoryTests { toBeRemoved.clear(); } + @Test + public void testFindOneWithNullApplicationAndNonExistingProfileAndNullLabelWhenDefaultLabelIsSet() { + String application = null; + String profile = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String defaultApplication = configServerProperties.getDefaultApplicationName(); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + String defaultLabel = labeledEnvironmentProperties.getDefaultLabel(); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationProperties()); + + Environment expectedEnv = new Environment(defaultApplication, profiles, defaultLabel, null, null); + expectedEnv.addAll(Arrays.asList(applicationDefaultProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, defaultLabel); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithNullApplicationAndDefaultProfileAndNullLabelWhenDefaultLabelIsSet() { + String application = null; + String profile = configServerProperties.getDefaultProfile(); + String defaultApplication = configServerProperties.getDefaultApplicationName(); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + String defaultLabel = labeledEnvironmentProperties.getDefaultLabel(); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationProperties()); + + Environment expectedEnv = new Environment(defaultApplication, profiles, defaultLabel, null, null); + expectedEnv.addAll(Arrays.asList(applicationDefaultProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, defaultLabel); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithNullApplicationAndExistingProfileAndNullLabelWhenDefaultLabelIsSet() { + String application = null; + String profile = "prod"; + String defaultApplication = configServerProperties.getDefaultApplicationName(); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + String defaultLabel = labeledEnvironmentProperties.getDefaultLabel(); + + String applicationProdPropertiesName = "aws:secrets:/secret/application-prod/"; + PropertySource applicationProdProperties = new PropertySource(applicationProdPropertiesName, + getApplicationProdProperties()); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationProperties()); + + Environment expectedEnv = new Environment(defaultApplication, profiles, defaultLabel, null, null); + expectedEnv + .addAll(Arrays.asList(applicationProdProperties, applicationDefaultProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, defaultLabel); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithDefaultApplicationAndNullProfileAndNullLabelWhenDefaultLabelIsSet() { + String application = configServerProperties.getDefaultApplicationName(); + String profile = null; + String defaultProfile = configServerProperties.getDefaultProfile(); + String[] profiles = StringUtils.commaDelimitedListToStringArray(defaultProfile); + String defaultLabel = labeledEnvironmentProperties.getDefaultLabel(); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationProperties()); + + Environment expectedEnv = new Environment(application, profiles, defaultLabel, null, null); + expectedEnv.addAll(Arrays.asList(applicationDefaultProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, defaultLabel); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithDefaultApplicationAndDefaultProfileAndNullLabelWhenDefaultLabelIsSet() { + String application = configServerProperties.getDefaultApplicationName(); + String profile = configServerProperties.getDefaultProfile(); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + String defaultLabel = labeledEnvironmentProperties.getDefaultLabel(); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationProperties()); + + Environment expectedEnv = new Environment(application, profiles, defaultLabel, null, null); + expectedEnv.addAll(Arrays.asList(applicationDefaultProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, defaultLabel); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithDefaultApplicationAndNonExistingProfileAndNullLabelWhenDefaultLabelIsSet() { + String application = configServerProperties.getDefaultApplicationName(); + String profile = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + String defaultLabel = labeledEnvironmentProperties.getDefaultLabel(); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationProperties()); + + Environment expectedEnv = new Environment(application, profiles, defaultLabel, null, null); + expectedEnv.addAll(Arrays.asList(applicationDefaultProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, defaultLabel); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithDefaultApplicationAndExistingProfileAndNullLabelWhenDefaultLabelIsSet() { + String application = configServerProperties.getDefaultApplicationName(); + String profile = "prod"; + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + String defaultLabel = labeledEnvironmentProperties.getDefaultLabel(); + + String applicationProdPropertiesName = "aws:secrets:/secret/application-prod/"; + PropertySource applicationProdProperties = new PropertySource(applicationProdPropertiesName, + getApplicationProdProperties()); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationProperties()); + + Environment expectedEnv = new Environment(application, profiles, defaultLabel, null, null); + expectedEnv + .addAll(Arrays.asList(applicationProdProperties, applicationDefaultProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, defaultLabel); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithNonExistingApplicationAndNullProfileAndNullLabelWhenDefaultLabelIsSet() { + String application = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String profile = null; + String defaultProfile = configServerProperties.getDefaultProfile(); + String[] profiles = StringUtils.commaDelimitedListToStringArray(defaultProfile); + String defaultLabel = labeledEnvironmentProperties.getDefaultLabel(); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationProperties()); + + Environment expectedEnv = new Environment(application, profiles, defaultLabel, null, null); + expectedEnv.addAll(Arrays.asList(applicationDefaultProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, defaultLabel); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithNonExistingApplicationAndDefaultProfileAndNullLabelWhenDefaultLabelIsSet() { + String application = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String profile = configServerProperties.getDefaultProfile(); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + String defaultLabel = labeledEnvironmentProperties.getDefaultLabel(); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationProperties()); + + Environment expectedEnv = new Environment(application, profiles, defaultLabel, null, null); + expectedEnv.addAll(Arrays.asList(applicationDefaultProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, defaultLabel); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithNonExistingApplicationAndNonExistingProfileAndNullLabelWhenDefaultLabelIsSet() { + String application = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String profile = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + String defaultLabel = labeledEnvironmentProperties.getDefaultLabel(); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationProperties()); + + Environment expectedEnv = new Environment(application, profiles, defaultLabel, null, null); + expectedEnv.addAll(Arrays.asList(applicationDefaultProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, defaultLabel); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithNonExistingApplicationAndExistingProfileAndNullLabelWhenDefaultLabelIsSet() { + String application = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String profile = "prod"; + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + String defaultLabel = labeledEnvironmentProperties.getDefaultLabel(); + + String applicationProdPropertiesName = "aws:secrets:/secret/application-prod/"; + PropertySource applicationProdProperties = new PropertySource(applicationProdPropertiesName, + getApplicationProdProperties()); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationProperties()); + + Environment expectedEnv = new Environment(application, profiles, defaultLabel, null, null); + expectedEnv + .addAll(Arrays.asList(applicationProdProperties, applicationDefaultProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, defaultLabel); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithExistingApplicationAndNullProfileAndNullLabelWhenDefaultLabelIsSet() { + String application = "foo"; + String profile = null; + String defaultProfile = configServerProperties.getDefaultProfile(); + String[] profiles = StringUtils.commaDelimitedListToStringArray(defaultProfile); + String defaultLabel = labeledEnvironmentProperties.getDefaultLabel(); + + String fooPropertiesName = "aws:secrets:/secret/foo/"; + PropertySource fooProperties = new PropertySource(fooPropertiesName, getFooProperties()); + + String fooDefaultPropertiesName = "aws:secrets:/secret/foo-default/"; + PropertySource fooDefaultProperties = new PropertySource(fooDefaultPropertiesName, getFooDefaultProperties()); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationProperties()); + + Environment expectedEnv = new Environment(application, profiles, defaultLabel, null, null); + expectedEnv.addAll(Arrays.asList(fooDefaultProperties, applicationDefaultProperties, fooProperties, + applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, defaultLabel); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithExistingApplicationAndDefaultProfileAndNullLabelWhenDefaultLabelIsSet() { + String application = "foo"; + String profile = configServerProperties.getDefaultProfile(); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + String defaultLabel = labeledEnvironmentProperties.getDefaultLabel(); + + String fooPropertiesName = "aws:secrets:/secret/foo/"; + PropertySource fooProperties = new PropertySource(fooPropertiesName, getFooProperties()); + + String fooDefaultPropertiesName = "aws:secrets:/secret/foo-default/"; + PropertySource fooDefaultProperties = new PropertySource(fooDefaultPropertiesName, getFooDefaultProperties()); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationProperties()); + + Environment expectedEnv = new Environment(application, profiles, defaultLabel, null, null); + expectedEnv.addAll(Arrays.asList(fooDefaultProperties, applicationDefaultProperties, fooProperties, + applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, defaultLabel); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithExistingApplicationAndNonExistingProfileAndNullLabelWhenDefaultLabelIsSet() { + String application = "foo"; + String profile = randomAlphabetic(RandomUtils.nextInt(2, 25)); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + String defaultLabel = labeledEnvironmentProperties.getDefaultLabel(); + + String fooPropertiesName = "aws:secrets:/secret/foo/"; + PropertySource fooProperties = new PropertySource(fooPropertiesName, getFooProperties()); + + String fooDefaultPropertiesName = "aws:secrets:/secret/foo-default/"; + PropertySource fooDefaultProperties = new PropertySource(fooDefaultPropertiesName, getFooDefaultProperties()); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationProperties()); + + Environment expectedEnv = new Environment(application, profiles, defaultLabel, null, null); + expectedEnv.addAll(Arrays.asList(fooDefaultProperties, applicationDefaultProperties, fooProperties, + applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, defaultLabel); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithExistingApplicationAndNonExistingProfileAndNoDefaultProfileAndNullLabelWhenDefaultLabelIsSet() { + String application = "foo"; + String profile = randomAlphabetic(RandomUtils.nextInt(2, 25)); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + String defaultLabel = labeledEnvironmentProperties.getDefaultLabel(); + + String fooPropertiesName = "aws:secrets:/secret/foo/"; + PropertySource fooProperties = new PropertySource(fooPropertiesName, getFooProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationProperties()); + + Environment expectedEnv = new Environment(application, profiles, defaultLabel, null, null); + expectedEnv.addAll(Arrays.asList(fooProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, defaultLabel); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithExistingApplicationAndNonExistingProfileAndNoDefaultProfileForFooAndNullLabelWhenDefaultLabelIsSet() { + String application = "foo"; + String profile = randomAlphabetic(RandomUtils.nextInt(2, 25)); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + String defaultLabel = labeledEnvironmentProperties.getDefaultLabel(); + + String fooPropertiesName = "aws:secrets:/secret/foo/"; + PropertySource fooProperties = new PropertySource(fooPropertiesName, getFooProperties()); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationProperties()); + + Environment expectedEnv = new Environment(application, profiles, defaultLabel, null, null); + expectedEnv.addAll(Arrays.asList(applicationDefaultProperties, fooProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, defaultLabel); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithExistingApplicationAndExistingProfileAndNullLabelWhenDefaultLabelIsSet() { + String application = "foo"; + String profile = "prod"; + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + String defaultLabel = labeledEnvironmentProperties.getDefaultLabel(); + + String fooProdPropertiesName = "aws:secrets:/secret/foo-prod/"; + PropertySource fooProdProperties = new PropertySource(fooProdPropertiesName, getFooProdProperties()); + + String fooPropertiesName = "aws:secrets:/secret/foo/"; + PropertySource fooProperties = new PropertySource(fooPropertiesName, getFooProperties()); + + String fooDefaultPropertiesName = "aws:secrets:/secret/foo-default/"; + PropertySource fooDefaultProperties = new PropertySource(fooDefaultPropertiesName, getFooDefaultProperties()); + + String applicationProdPropertiesName = "aws:secrets:/secret/application-prod/"; + PropertySource applicationProdProperties = new PropertySource(applicationProdPropertiesName, + getApplicationProdProperties()); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationProperties()); + + Environment expectedEnv = new Environment(application, profiles, defaultLabel, null, null); + expectedEnv.addAll(Arrays.asList(fooProdProperties, applicationProdProperties, fooDefaultProperties, + applicationDefaultProperties, fooProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, defaultLabel); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithExistingApplicationAndExistingProfileAndNoDefaultProfilesAndNullLabelWhenDefaultLabelIsSet() { + String application = "foo"; + String profile = "prod"; + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + String defaultLabel = labeledEnvironmentProperties.getDefaultLabel(); + + String fooProdPropertiesName = "aws:secrets:/secret/foo-prod/"; + PropertySource fooProdProperties = new PropertySource(fooProdPropertiesName, getFooProdProperties()); + + String fooPropertiesName = "aws:secrets:/secret/foo/"; + PropertySource fooProperties = new PropertySource(fooPropertiesName, getFooProperties()); + + String applicationProdPropertiesName = "aws:secrets:/secret/application-prod/"; + PropertySource applicationProdProperties = new PropertySource(applicationProdPropertiesName, + getApplicationProdProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationProperties()); + + Environment expectedEnv = new Environment(application, profiles, defaultLabel, null, null); + expectedEnv.addAll( + Arrays.asList(fooProdProperties, applicationProdProperties, fooProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, defaultLabel); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithExistingApplicationAndMultipleExistingProfileAndNullLabelWhenDefaultLabelIsSet() { + String application = "foo"; + String profile = "prod,east"; + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + String defaultLabel = labeledEnvironmentProperties.getDefaultLabel(); + + String fooProdPropertiesName = "aws:secrets:/secret/foo-prod/"; + PropertySource fooProdProperties = new PropertySource(fooProdPropertiesName, getFooProdProperties()); + + String fooEastPropertiesName = "aws:secrets:/secret/foo-east/"; + PropertySource fooEastProperties = new PropertySource(fooEastPropertiesName, getFooEastProperties()); + + String fooPropertiesName = "aws:secrets:/secret/foo/"; + PropertySource fooProperties = new PropertySource(fooPropertiesName, getFooProperties()); + + String fooDefaultPropertiesName = "aws:secrets:/secret/foo-default/"; + PropertySource fooDefaultProperties = new PropertySource(fooDefaultPropertiesName, getFooDefaultProperties()); + + String applicationProdPropertiesName = "aws:secrets:/secret/application-prod/"; + PropertySource applicationProdProperties = new PropertySource(applicationProdPropertiesName, + getApplicationProdProperties()); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationProperties()); + + String applicationEastPropertiesName = "aws:secrets:/secret/application-east/"; + PropertySource applicationEastProperties = new PropertySource(applicationEastPropertiesName, + getApplicationEastProperties()); + + Environment expectedEnv = new Environment(application, profiles, defaultLabel, null, null); + expectedEnv.addAll(Arrays.asList(fooProdProperties, applicationProdProperties, fooEastProperties, + applicationEastProperties, fooDefaultProperties, applicationDefaultProperties, fooProperties, + applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, defaultLabel); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithExistingApplicationAndMultipleExistingProfileAndNoDefaultsAndNullLabelWhenDefaultLabelIsSet() { + String application = "foo"; + String profile = "prod,east"; + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + String defaultLabel = labeledEnvironmentProperties.getDefaultLabel(); + + String fooProdPropertiesName = "aws:secrets:/secret/foo-prod/"; + PropertySource fooProdProperties = new PropertySource(fooProdPropertiesName, getFooProdProperties()); + + String fooEastPropertiesName = "aws:secrets:/secret/foo-east/"; + PropertySource fooEastProperties = new PropertySource(fooEastPropertiesName, getFooEastProperties()); + + String fooPropertiesName = "aws:secrets:/secret/foo/"; + PropertySource fooProperties = new PropertySource(fooPropertiesName, getFooProperties()); + + String applicationProdPropertiesName = "aws:secrets:/secret/application-prod/"; + PropertySource applicationProdProperties = new PropertySource(applicationProdPropertiesName, + getApplicationProdProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationProperties()); + + String applicationEastPropertiesName = "aws:secrets:/secret/application-east/"; + PropertySource applicationEastProperties = new PropertySource(applicationEastPropertiesName, + getApplicationEastProperties()); + + Environment expectedEnv = new Environment(application, profiles, defaultLabel, null, null); + expectedEnv.addAll(Arrays.asList(fooProdProperties, applicationProdProperties, fooEastProperties, + applicationEastProperties, fooProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, defaultLabel); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithNullApplicationAndNullProfileAndNonExistingLabelWhenDefaultLabelIsSet() { + String application = null; + String profile = null; + String label = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String defaultApplication = configServerProperties.getDefaultApplicationName(); + String defaultProfile = configServerProperties.getDefaultProfile(); + String[] profiles = StringUtils.commaDelimitedListToStringArray(defaultProfile); + + Environment expectedEnv = new Environment(defaultApplication, profiles, label, null, null); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithNullApplicationAndNonExistingProfileAndNonExistingLabelWhenDefaultLabelIsSet() { + String application = null; + String profile = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String label = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String defaultApplication = configServerProperties.getDefaultApplicationName(); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + Environment expectedEnv = new Environment(defaultApplication, profiles, label, null, null); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithNullApplicationAndDefaultProfileAndNonExistingLabelWhenDefaultLabelIsSet() { + String application = null; + String profile = configServerProperties.getDefaultProfile(); + String label = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String defaultApplication = configServerProperties.getDefaultApplicationName(); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + Environment expectedEnv = new Environment(defaultApplication, profiles, label, null, null); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithNullApplicationAndExistingProfileAndNonExistingLabelWhenDefaultLabelIsSet() { + String application = null; + String profile = "prod"; + String label = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String defaultApplication = configServerProperties.getDefaultApplicationName(); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + Environment expectedEnv = new Environment(defaultApplication, profiles, label, null, null); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithDefaultApplicationAndNullProfileAndNonExistingLabelWhenDefaultLabelIsSet() { + String application = configServerProperties.getDefaultApplicationName(); + String profile = null; + String label = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String defaultProfile = configServerProperties.getDefaultProfile(); + String[] profiles = StringUtils.commaDelimitedListToStringArray(defaultProfile); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithDefaultApplicationAndDefaultProfileAndNonExistingLabelWhenDefaultLabelIsSet() { + String application = configServerProperties.getDefaultApplicationName(); + String profile = configServerProperties.getDefaultProfile(); + String label = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithDefaultApplicationAndNonExistingProfileAndNonExistingLabelWhenDefaultLabelIsSet() { + String application = configServerProperties.getDefaultApplicationName(); + String profile = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String label = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithDefaultApplicationAndExistingProfileAndNonExistingLabelWhenDefaultLabelIsSet() { + String application = configServerProperties.getDefaultApplicationName(); + String profile = "prod"; + String label = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithNonExistingApplicationAndNullProfileAndNonExistingLabelWhenDefaultLabelIsSet() { + String application = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String profile = null; + String label = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String defaultProfile = configServerProperties.getDefaultProfile(); + String[] profiles = StringUtils.commaDelimitedListToStringArray(defaultProfile); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithNonExistingApplicationAndDefaultProfileAndNonExistingLabelWhenDefaultLabelIsSet() { + String application = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String profile = configServerProperties.getDefaultProfile(); + String label = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithNonExistingApplicationAndNonExistingProfileAndNonExistingLabelWhenDefaultLabelIsSet() { + String application = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String profile = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String label = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithNonExistingApplicationAndExistingProfileAndNonExistingLabelWhenDefaultLabelIsSet() { + String application = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String profile = "prod"; + String label = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithExistingApplicationAndNullProfileAndNonExistingLabelWhenDefaultLabelIsSet() { + String application = "foo"; + String profile = null; + String label = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String defaultProfile = configServerProperties.getDefaultProfile(); + String[] profiles = StringUtils.commaDelimitedListToStringArray(defaultProfile); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithExistingApplicationAndDefaultProfileAndNonExistingLabelWhenDefaultLabelIsSet() { + String application = "foo"; + String profile = configServerProperties.getDefaultProfile(); + String label = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithExistingApplicationAndNonExistingProfileAndNonExistingLabelWhenDefaultLabelIsSet() { + String application = "foo"; + String profile = randomAlphabetic(RandomUtils.nextInt(2, 25)); + String label = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithExistingApplicationAndNonExistingProfileAndNoDefaultProfileAndNonExistingLabelWhenDefaultLabelIsSet() { + String application = "foo"; + String profile = randomAlphabetic(RandomUtils.nextInt(2, 25)); + String label = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithExistingApplicationAndNonExistingProfileAndNoDefaultProfileForFooAndNonExistingLabelWhenDefaultLabelIsSet() { + String application = "foo"; + String profile = randomAlphabetic(RandomUtils.nextInt(2, 25)); + String label = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithExistingApplicationAndExistingProfileAndNonExistingLabelWhenDefaultLabelIsSet() { + String application = "foo"; + String profile = "prod"; + String label = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithExistingApplicationAndExistingProfileAndNoDefaultProfilesAndNonExistingLabelWhenDefaultLabelIsSet() { + String application = "foo"; + String profile = "prod"; + String label = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithExistingApplicationAndMultipleExistingProfileAndNonExistingLabelWhenDefaultLabelIsSet() { + String application = "foo"; + String profile = "prod,east"; + String label = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithExistingApplicationAndMultipleExistingProfileAndNoDefaultsAndNonExistingLabelWhenDefaultLabelIsSet() { + String application = "foo"; + String profile = "prod,east"; + String label = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithNullApplicationAndNullProfileAndExistingLabelWhenDefaultLabelIsSet() { + String application = null; + String profile = null; + String label = "release"; + String defaultApplication = configServerProperties.getDefaultApplicationName(); + String defaultProfile = configServerProperties.getDefaultProfile(); + String[] profiles = StringUtils.commaDelimitedListToStringArray(defaultProfile); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultReleaseProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationReleaseProperties()); + + Environment expectedEnv = new Environment(defaultApplication, profiles, label, null, null); + expectedEnv.addAll(Arrays.asList(applicationDefaultProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithNullApplicationAndNonExistingProfileAndExistingLabelWhenDefaultLabelIsSet() { + String application = null; + String profile = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String label = "release"; + String defaultApplication = configServerProperties.getDefaultApplicationName(); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultReleaseProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationReleaseProperties()); + + Environment expectedEnv = new Environment(defaultApplication, profiles, label, null, null); + expectedEnv.addAll(Arrays.asList(applicationDefaultProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithNullApplicationAndDefaultProfileAndExistingLabelWhenDefaultLabelIsSet() { + String application = null; + String profile = configServerProperties.getDefaultProfile(); + String defaultApplication = configServerProperties.getDefaultApplicationName(); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + String label = "release"; + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultReleaseProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationReleaseProperties()); + + Environment expectedEnv = new Environment(defaultApplication, profiles, label, null, null); + expectedEnv.addAll(Arrays.asList(applicationDefaultProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithNullApplicationAndExistingProfileAndExistingLabelWhenDefaultLabelIsSet() { + String application = null; + String profile = "prod"; + String label = "release"; + String defaultApplication = configServerProperties.getDefaultApplicationName(); + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + String applicationProdPropertiesName = "aws:secrets:/secret/application-prod/"; + PropertySource applicationProdProperties = new PropertySource(applicationProdPropertiesName, + getApplicationProdReleaseProperties()); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultReleaseProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationReleaseProperties()); + + Environment expectedEnv = new Environment(defaultApplication, profiles, label, null, null); + expectedEnv + .addAll(Arrays.asList(applicationProdProperties, applicationDefaultProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithDefaultApplicationAndNullProfileAndExistingLabelWhenDefaultLabelIsSet() { + String application = configServerProperties.getDefaultApplicationName(); + String profile = null; + String label = "release"; + String defaultProfile = configServerProperties.getDefaultProfile(); + String[] profiles = StringUtils.commaDelimitedListToStringArray(defaultProfile); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultReleaseProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationReleaseProperties()); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + expectedEnv.addAll(Arrays.asList(applicationDefaultProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithDefaultApplicationAndDefaultProfileAndExistingLabelWhenDefaultLabelIsSet() { + String application = configServerProperties.getDefaultApplicationName(); + String profile = configServerProperties.getDefaultProfile(); + String label = "release"; + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultReleaseProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationReleaseProperties()); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + expectedEnv.addAll(Arrays.asList(applicationDefaultProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithDefaultApplicationAndNonExistingProfileAndExistingLabelWhenDefaultLabelIsSet() { + String application = configServerProperties.getDefaultApplicationName(); + String profile = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String label = "release"; + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultReleaseProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationReleaseProperties()); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + expectedEnv.addAll(Arrays.asList(applicationDefaultProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithDefaultApplicationAndExistingProfileAndExistingLabelWhenDefaultLabelIsSet() { + String application = configServerProperties.getDefaultApplicationName(); + String profile = "prod"; + String label = "release"; + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + String applicationProdPropertiesName = "aws:secrets:/secret/application-prod/"; + PropertySource applicationProdProperties = new PropertySource(applicationProdPropertiesName, + getApplicationProdReleaseProperties()); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultReleaseProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationReleaseProperties()); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + expectedEnv + .addAll(Arrays.asList(applicationProdProperties, applicationDefaultProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithNonExistingApplicationAndNullProfileAndExistingLabelWhenDefaultLabelIsSet() { + String application = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String profile = null; + String label = "release"; + String defaultProfile = configServerProperties.getDefaultProfile(); + String[] profiles = StringUtils.commaDelimitedListToStringArray(defaultProfile); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultReleaseProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationReleaseProperties()); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + expectedEnv.addAll(Arrays.asList(applicationDefaultProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithNonExistingApplicationAndDefaultProfileAndExistingLabelWhenDefaultLabelIsSet() { + String application = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String profile = configServerProperties.getDefaultProfile(); + String label = "release"; + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultReleaseProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationReleaseProperties()); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + expectedEnv.addAll(Arrays.asList(applicationDefaultProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithNonExistingApplicationAndNonExistingProfileAndExistingLabelWhenDefaultLabelIsSet() { + String application = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String profile = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String label = "release"; + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultReleaseProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationReleaseProperties()); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + expectedEnv.addAll(Arrays.asList(applicationDefaultProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithNonExistingApplicationAndExistingProfileAndExistingLabelWhenDefaultLabelIsSet() { + String application = randomAlphabetic(RandomUtils.nextInt(3, 25)); + String profile = "prod"; + String label = "release"; + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + String applicationProdPropertiesName = "aws:secrets:/secret/application-prod/"; + PropertySource applicationProdProperties = new PropertySource(applicationProdPropertiesName, + getApplicationProdReleaseProperties()); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultReleaseProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationReleaseProperties()); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + expectedEnv + .addAll(Arrays.asList(applicationProdProperties, applicationDefaultProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithExistingApplicationAndNullProfileAndExistingLabelWhenDefaultLabelIsSet() { + String application = "foo"; + String profile = null; + String label = "release"; + String defaultProfile = configServerProperties.getDefaultProfile(); + String[] profiles = StringUtils.commaDelimitedListToStringArray(defaultProfile); + + String fooPropertiesName = "aws:secrets:/secret/foo/"; + PropertySource fooProperties = new PropertySource(fooPropertiesName, getFooReleaseProperties()); + + String fooDefaultPropertiesName = "aws:secrets:/secret/foo-default/"; + PropertySource fooDefaultProperties = new PropertySource(fooDefaultPropertiesName, + getFooDefaultReleaseProperties()); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultReleaseProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationReleaseProperties()); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + expectedEnv.addAll(Arrays.asList(fooDefaultProperties, applicationDefaultProperties, fooProperties, + applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithExistingApplicationAndDefaultProfileAndExistingLabelWhenDefaultLabelIsSet() { + String application = "foo"; + String profile = configServerProperties.getDefaultProfile(); + String label = "release"; + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + String fooPropertiesName = "aws:secrets:/secret/foo/"; + PropertySource fooProperties = new PropertySource(fooPropertiesName, getFooReleaseProperties()); + + String fooDefaultPropertiesName = "aws:secrets:/secret/foo-default/"; + PropertySource fooDefaultProperties = new PropertySource(fooDefaultPropertiesName, + getFooDefaultReleaseProperties()); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultReleaseProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationReleaseProperties()); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + expectedEnv.addAll(Arrays.asList(fooDefaultProperties, applicationDefaultProperties, fooProperties, + applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithExistingApplicationAndNonExistingProfileAndExistingLabelWhenDefaultLabelIsSet() { + String application = "foo"; + String profile = randomAlphabetic(RandomUtils.nextInt(2, 25)); + String label = "release"; + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + String fooPropertiesName = "aws:secrets:/secret/foo/"; + PropertySource fooProperties = new PropertySource(fooPropertiesName, getFooReleaseProperties()); + + String fooDefaultPropertiesName = "aws:secrets:/secret/foo-default/"; + PropertySource fooDefaultProperties = new PropertySource(fooDefaultPropertiesName, + getFooDefaultReleaseProperties()); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultReleaseProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationReleaseProperties()); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + expectedEnv.addAll(Arrays.asList(fooDefaultProperties, applicationDefaultProperties, fooProperties, + applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithExistingApplicationAndNonExistingProfileAndNoDefaultProfileAndExistingLabelWhenDefaultLabelIsSet() { + String application = "foo"; + String profile = randomAlphabetic(RandomUtils.nextInt(2, 25)); + String label = "release"; + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + String fooPropertiesName = "aws:secrets:/secret/foo/"; + PropertySource fooProperties = new PropertySource(fooPropertiesName, getFooReleaseProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationReleaseProperties()); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + expectedEnv.addAll(Arrays.asList(fooProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithExistingApplicationAndNonExistingProfileAndNoDefaultProfileForFooAndExistingLabelWhenDefaultLabelIsSet() { + String application = "foo"; + String profile = randomAlphabetic(RandomUtils.nextInt(2, 25)); + String label = "release"; + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + String fooPropertiesName = "aws:secrets:/secret/foo/"; + PropertySource fooProperties = new PropertySource(fooPropertiesName, getFooReleaseProperties()); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultReleaseProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationReleaseProperties()); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + expectedEnv.addAll(Arrays.asList(applicationDefaultProperties, fooProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithExistingApplicationAndExistingProfileAndExistingLabelWhenDefaultLabelIsSet() { + String application = "foo"; + String profile = "prod"; + String label = "release"; + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + String fooProdPropertiesName = "aws:secrets:/secret/foo-prod/"; + PropertySource fooProdProperties = new PropertySource(fooProdPropertiesName, getFooProdReleaseProperties()); + + String fooPropertiesName = "aws:secrets:/secret/foo/"; + PropertySource fooProperties = new PropertySource(fooPropertiesName, getFooReleaseProperties()); + + String fooDefaultPropertiesName = "aws:secrets:/secret/foo-default/"; + PropertySource fooDefaultProperties = new PropertySource(fooDefaultPropertiesName, + getFooDefaultReleaseProperties()); + + String applicationProdPropertiesName = "aws:secrets:/secret/application-prod/"; + PropertySource applicationProdProperties = new PropertySource(applicationProdPropertiesName, + getApplicationProdReleaseProperties()); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultReleaseProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationReleaseProperties()); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + expectedEnv.addAll(Arrays.asList(fooProdProperties, applicationProdProperties, fooDefaultProperties, + applicationDefaultProperties, fooProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithExistingApplicationAndExistingProfileAndNoDefaultProfilesAndExistingLabelWhenDefaultLabelIsSet() { + String application = "foo"; + String profile = "prod"; + String label = "release"; + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + String fooProdPropertiesName = "aws:secrets:/secret/foo-prod/"; + PropertySource fooProdProperties = new PropertySource(fooProdPropertiesName, getFooProdReleaseProperties()); + + String fooPropertiesName = "aws:secrets:/secret/foo/"; + PropertySource fooProperties = new PropertySource(fooPropertiesName, getFooReleaseProperties()); + + String applicationProdPropertiesName = "aws:secrets:/secret/application-prod/"; + PropertySource applicationProdProperties = new PropertySource(applicationProdPropertiesName, + getApplicationProdReleaseProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationReleaseProperties()); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + expectedEnv.addAll( + Arrays.asList(fooProdProperties, applicationProdProperties, fooProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithExistingApplicationAndMultipleExistingProfileAndExistingLabelWhenDefaultLabelIsSet() { + String application = "foo"; + String profile = "prod,east"; + String label = "release"; + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + String fooProdPropertiesName = "aws:secrets:/secret/foo-prod/"; + PropertySource fooProdProperties = new PropertySource(fooProdPropertiesName, getFooProdReleaseProperties()); + + String fooEastPropertiesName = "aws:secrets:/secret/foo-east/"; + PropertySource fooEastProperties = new PropertySource(fooEastPropertiesName, getFooEastReleaseProperties()); + + String fooPropertiesName = "aws:secrets:/secret/foo/"; + PropertySource fooProperties = new PropertySource(fooPropertiesName, getFooReleaseProperties()); + + String fooDefaultPropertiesName = "aws:secrets:/secret/foo-default/"; + PropertySource fooDefaultProperties = new PropertySource(fooDefaultPropertiesName, + getFooDefaultReleaseProperties()); + + String applicationProdPropertiesName = "aws:secrets:/secret/application-prod/"; + PropertySource applicationProdProperties = new PropertySource(applicationProdPropertiesName, + getApplicationProdReleaseProperties()); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultReleaseProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationReleaseProperties()); + + String applicationEastPropertiesName = "aws:secrets:/secret/application-east/"; + PropertySource applicationEastProperties = new PropertySource(applicationEastPropertiesName, + getApplicationEastReleaseProperties()); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + expectedEnv.addAll(Arrays.asList(fooProdProperties, applicationProdProperties, fooEastProperties, + applicationEastProperties, fooDefaultProperties, applicationDefaultProperties, fooProperties, + applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + + @Test + public void testFindOneWithExistingApplicationAndMultipleExistingProfileAndNoDefaultsAndExistingLabelWhenDefaultLabelIsSet() { + String application = "foo"; + String profile = "prod,east"; + String label = "release"; + String[] profiles = StringUtils.commaDelimitedListToStringArray(profile); + + String fooProdPropertiesName = "aws:secrets:/secret/foo-prod/"; + PropertySource fooProdProperties = new PropertySource(fooProdPropertiesName, getFooProdReleaseProperties()); + + String fooEastPropertiesName = "aws:secrets:/secret/foo-east/"; + PropertySource fooEastProperties = new PropertySource(fooEastPropertiesName, getFooEastReleaseProperties()); + + String fooPropertiesName = "aws:secrets:/secret/foo/"; + PropertySource fooProperties = new PropertySource(fooPropertiesName, getFooReleaseProperties()); + + String applicationProdPropertiesName = "aws:secrets:/secret/application-prod/"; + PropertySource applicationProdProperties = new PropertySource(applicationProdPropertiesName, + getApplicationProdReleaseProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationReleaseProperties()); + + String applicationEastPropertiesName = "aws:secrets:/secret/application-east/"; + PropertySource applicationEastProperties = new PropertySource(applicationEastPropertiesName, + getApplicationEastReleaseProperties()); + + Environment expectedEnv = new Environment(application, profiles, label, null, null); + expectedEnv.addAll(Arrays.asList(fooProdProperties, applicationProdProperties, fooEastProperties, + applicationEastProperties, fooProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, label); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + @Test public void testFindOneWithNullApplicationAndNullProfile() { String application = null; @@ -116,6 +1846,33 @@ public class AwsSecretsManagerEnvironmentRepositoryTests { assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(environment); } + @Test + public void testFindOneWithNullApplicationAndNullProfileAndNullLabelWhenDefaultLabelIsSet() { + String application = null; + String profile = null; + String defaultApplication = configServerProperties.getDefaultApplicationName(); + String defaultProfile = configServerProperties.getDefaultProfile(); + String[] profiles = StringUtils.commaDelimitedListToStringArray(defaultProfile); + String defaultLabel = labeledEnvironmentProperties.getDefaultLabel(); + + String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/"; + PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName, + getApplicationDefaultProperties()); + + String applicationPropertiesName = "aws:secrets:/secret/application/"; + PropertySource applicationProperties = new PropertySource(applicationPropertiesName, + getApplicationProperties()); + + Environment expectedEnv = new Environment(defaultApplication, profiles, defaultLabel, null, null); + expectedEnv.addAll(Arrays.asList(applicationDefaultProperties, applicationProperties)); + + putSecrets(expectedEnv); + + Environment resultEnv = labeledRepository.findOne(application, profile, defaultLabel); + + assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv); + } + @Test public void testFindOneWithNullApplicationAndNonExistingProfile() { String application = null; @@ -767,10 +2524,18 @@ public class AwsSecretsManagerEnvironmentRepositoryTests { } private void putSecrets(Environment environment) { + String label = environment.getLabel() != null ? environment.getLabel() + : environmentProperties.getDefaultLabel(); for (PropertySource ps : environment.getPropertySources()) { String path = StringUtils.delete(ps.getName(), environmentProperties.getOrigin()); String secrets = getSecrets(ps); - smClient.createSecret(CreateSecretRequest.builder().name(path).secretString(secrets).build()); + CreateSecretResponse response = smClient + .createSecret(CreateSecretRequest.builder().name(path).secretString(secrets).build()); + if (!ObjectUtils.isEmpty(label)) { + smClient + .updateSecretVersionStage(UpdateSecretVersionStageRequest.builder().secretId(path) + .moveToVersionId(response.versionId()).versionStage(label).build()); + } toBeRemoved.add(path); } } @@ -786,76 +2551,4 @@ public class AwsSecretsManagerEnvironmentRepositoryTests { return ""; } - private static Map getApplicationProperties() { - return new HashMap() { - { - put("s3.accessKey", "application-shared-s3"); - put("s3.secretKey", "25300773-eb3b-4ace-b6fc-500c87331da7"); - } - }; - } - - private static Map getApplicationDefaultProperties() { - return new HashMap() { - { - put("s3.accessKey", "application-shared-default-s3"); - put("s3.secretKey", "691972aa-68d2-4e55-8d9b-eedd4c63a998"); - } - }; - } - - private static Map getApplicationProdProperties() { - return new HashMap() { - { - put("s3.accessKey", "application-shared-prod-s3"); - put("s3.secretKey", "90c1dd88-5b20-41fa-a4e9-e1d638188732"); - } - }; - } - - private static Map getApplicationEastProperties() { - return new HashMap() { - { - put("s3.accessKey", "application-east-s3"); - put("s3.secretKey", "236e01a7-623b-40f4-88c1-eb4d89229dd6"); - } - }; - } - - private static Map getFooProperties() { - return new HashMap() { - { - put("s3.accessKey", "foo-s3"); - put("s3.secretKey", "ce945da2-740a-4915-a090-2978428dad05"); - } - }; - } - - private static Map getFooDefaultProperties() { - return new HashMap() { - { - put("s3.accessKey", "foo-default-s3"); - put("s3.secretKey", "8c3c58c9-daef-4d21-96b0-c2b68a7a8234"); - } - }; - } - - private static Map getFooProdProperties() { - return new HashMap() { - { - put("s3.accessKey", "foo-prod-s3"); - put("s3.secretKey", "42ca062d-8e4b-435e-9e4a-d058835817c0"); - } - }; - } - - private static Map getFooEastProperties() { - return new HashMap() { - { - put("s3.accessKey", "foo-east-s3"); - put("s3.secretKey", "657f6ac5-2e1c-487d-9d61-1df109b29edf"); - } - }; - } - }