Skip to content

Commit 9390015

Browse files
authored
Fix context order when loading properties (#195)
Fix context order when loading properties through Parameter Store and Secrets Manager Fixes #169
1 parent dbc983e commit 9390015

File tree

8 files changed

+170
-22
lines changed

8 files changed

+170
-22
lines changed

spring-cloud-aws-parameter-store-config/src/main/java/io/awspring/cloud/paramstore/AwsParamStorePropertySources.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@
2525
import org.springframework.util.StringUtils;
2626

2727
/**
28+
* Is responsible for creating {@link AwsParamStorePropertySource} and determining
29+
* automatic contexts.
30+
*
2831
* @author Eddú Meléndez
32+
* @author Manuel Wessner
2933
* @since 2.3
3034
*/
3135
public class AwsParamStorePropertySources {
@@ -44,18 +48,18 @@ public List<String> getAutomaticContexts(List<String> profiles) {
4448
String prefix = this.properties.getPrefix();
4549
String defaultContext = getContext(prefix, this.properties.getDefaultContext());
4650

47-
String appName = this.properties.getName();
51+
contexts.add(defaultContext + "/");
52+
addProfiles(contexts, defaultContext, profiles);
4853

54+
String appName = this.properties.getName();
4955
String appContext = prefix + "/" + appName;
50-
addProfiles(contexts, appContext, profiles);
5156
contexts.add(appContext + "/");
57+
addProfiles(contexts, appContext, profiles);
5258

53-
addProfiles(contexts, defaultContext, profiles);
54-
contexts.add(defaultContext + "/");
5559
return contexts;
5660
}
5761

58-
protected String getContext(String prefix, String context) {
62+
private String getContext(String prefix, String context) {
5963
if (StringUtils.hasLength(prefix)) {
6064
return prefix + "/" + context;
6165
}

spring-cloud-aws-parameter-store-config/src/test/java/io/awspring/cloud/paramstore/AwsParamStorePropertySourceLocatorTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ void contextSpecificOrderExpected() {
100100

101101
List<String> contextToBeTested = new ArrayList<>(locator.getContexts());
102102

103-
assertThat(contextToBeTested.get(0)).isEqualTo("application/messaging-service_test/");
104-
assertThat(contextToBeTested.get(1)).isEqualTo("application/messaging-service/");
105-
assertThat(contextToBeTested.get(2)).isEqualTo("application/application_test/");
106-
assertThat(contextToBeTested.get(3)).isEqualTo("application/application/");
103+
assertThat(contextToBeTested.get(0)).isEqualTo("application/application/");
104+
assertThat(contextToBeTested.get(1)).isEqualTo("application/application_test/");
105+
assertThat(contextToBeTested.get(2)).isEqualTo("application/messaging-service/");
106+
assertThat(contextToBeTested.get(3)).isEqualTo("application/messaging-service_test/");
107107
}
108108

109109
@Test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2013-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.awspring.cloud.paramstore;
18+
19+
import java.util.Collections;
20+
import java.util.List;
21+
22+
import org.apache.commons.logging.Log;
23+
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.api.Test;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
import static org.mockito.Mockito.mock;
28+
29+
/**
30+
* Unit test for {@link AwsParamStorePropertySourceLocator}.
31+
*
32+
* @author Manuel Wessner
33+
*/
34+
class AwsParamStorePropertySourcesTest {
35+
36+
private final Log logMock = mock(Log.class);
37+
38+
private AwsParamStoreProperties properties;
39+
40+
@BeforeEach
41+
void setUp() {
42+
properties = new AwsParamStoreProperties();
43+
properties.setDefaultContext("application");
44+
properties.setName("messaging-service");
45+
}
46+
47+
@Test
48+
void getAutomaticContextsWithSingleProfile() {
49+
AwsParamStorePropertySources propertySource = new AwsParamStorePropertySources(properties, logMock);
50+
51+
List<String> contexts = propertySource.getAutomaticContexts(Collections.singletonList("production"));
52+
53+
assertThat(contexts.size()).isEqualTo(4);
54+
assertThat(contexts).containsExactly("/config/application/", "/config/application_production/",
55+
"/config/messaging-service/", "/config/messaging-service_production/");
56+
}
57+
58+
@Test
59+
void getAutomaticContextsWithoutProfile() {
60+
AwsParamStorePropertySources propertySource = new AwsParamStorePropertySources(properties, logMock);
61+
62+
List<String> contexts = propertySource.getAutomaticContexts(Collections.emptyList());
63+
64+
assertThat(contexts.size()).isEqualTo(2);
65+
assertThat(contexts).containsExactly("/config/application/", "/config/messaging-service/");
66+
}
67+
68+
}

spring-cloud-aws-secrets-manager-config/src/main/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerPropertySources.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@
2424

2525
import org.springframework.util.StringUtils;
2626

27+
/**
28+
* Is responsible for creating {@link AwsSecretsManagerPropertySource} and determining
29+
* automatic contexts.
30+
*
31+
* @author Eddú Meléndez
32+
* @author Manuel Wessner
33+
* @since 2.3
34+
*/
2735
public class AwsSecretsManagerPropertySources {
2836

2937
private final AwsSecretsManagerProperties properties;
@@ -40,18 +48,18 @@ public List<String> getAutomaticContexts(List<String> profiles) {
4048
String prefix = this.properties.getPrefix();
4149
String defaultContext = getContext(prefix, this.properties.getDefaultContext());
4250

43-
String appName = this.properties.getName();
51+
contexts.add(defaultContext);
52+
addProfiles(contexts, defaultContext, profiles);
4453

54+
String appName = this.properties.getName();
4555
String appContext = prefix + "/" + appName;
46-
addProfiles(contexts, appContext, profiles);
4756
contexts.add(appContext);
57+
addProfiles(contexts, appContext, profiles);
4858

49-
addProfiles(contexts, defaultContext, profiles);
50-
contexts.add(defaultContext);
5159
return contexts;
5260
}
5361

54-
protected String getContext(String prefix, String context) {
62+
private String getContext(String prefix, String context) {
5563
if (StringUtils.hasLength(prefix)) {
5664
return prefix + "/" + context;
5765
}

spring-cloud-aws-secrets-manager-config/src/test/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerPropertySourceLocatorTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,10 @@ void contextSpecificOrderExpected() {
128128

129129
List<String> contextToBeTested = new ArrayList<>(locator.getContexts());
130130

131-
assertThat(contextToBeTested.get(0)).isEqualTo("/secret/messaging-service_test");
132-
assertThat(contextToBeTested.get(1)).isEqualTo("/secret/messaging-service");
133-
assertThat(contextToBeTested.get(2)).isEqualTo("/secret/application_test");
134-
assertThat(contextToBeTested.get(3)).isEqualTo("/secret/application");
131+
assertThat(contextToBeTested.get(0)).isEqualTo("/secret/application");
132+
assertThat(contextToBeTested.get(1)).isEqualTo("/secret/application_test");
133+
assertThat(contextToBeTested.get(2)).isEqualTo("/secret/messaging-service");
134+
assertThat(contextToBeTested.get(3)).isEqualTo("/secret/messaging-service_test");
135135
}
136136

137137
@Test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2013-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.awspring.cloud.secretsmanager;
18+
19+
import java.util.Collections;
20+
import java.util.List;
21+
22+
import org.apache.commons.logging.Log;
23+
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.api.Test;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
import static org.mockito.Mockito.mock;
28+
29+
/**
30+
* Unit test for {@link AwsSecretsManagerPropertySources}.
31+
*
32+
* @author Manuel Wessner
33+
*/
34+
class AwsSecretsManagerPropertySourcesTests {
35+
36+
private final Log logMock = mock(Log.class);
37+
38+
private AwsSecretsManagerProperties properties;
39+
40+
@BeforeEach
41+
void setUp() {
42+
properties = new AwsSecretsManagerProperties();
43+
properties.setDefaultContext("application");
44+
properties.setName("messaging-service");
45+
}
46+
47+
@Test
48+
void getAutomaticContextsWithSingleProfile() {
49+
AwsSecretsManagerPropertySources propertySource = new AwsSecretsManagerPropertySources(properties, logMock);
50+
51+
List<String> contexts = propertySource.getAutomaticContexts(Collections.singletonList("production"));
52+
53+
assertThat(contexts.size()).isEqualTo(4);
54+
assertThat(contexts).containsExactly("/secret/application", "/secret/application_production",
55+
"/secret/messaging-service", "/secret/messaging-service_production");
56+
}
57+
58+
@Test
59+
void getAutomaticContextsWithoutProfile() {
60+
AwsSecretsManagerPropertySources propertySource = new AwsSecretsManagerPropertySources(properties, logMock);
61+
62+
List<String> contexts = propertySource.getAutomaticContexts(Collections.emptyList());
63+
64+
assertThat(contexts.size()).isEqualTo(2);
65+
assertThat(contexts).containsExactly("/secret/application", "/secret/messaging-service");
66+
}
67+
68+
}

spring-cloud-starter-aws-parameter-store-config/src/test/java/io/awspring/cloud/autoconfigure/paramstore/AwsParamStoreConfigDataLocationResolverTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ void testResolveProfileSpecificWithAutomaticPaths() {
4040
String location = "aws-parameterstore:";
4141
List<AwsParamStoreConfigDataResource> locations = testResolveProfileSpecific(location);
4242
assertThat(locations).hasSize(4);
43-
assertThat(toContexts(locations)).containsExactly("/config/testapp_dev/", "/config/testapp/",
44-
"/config/application_dev/", "/config/application/");
43+
assertThat(toContexts(locations)).containsExactly("/config/application/", "/config/application_dev/",
44+
"/config/testapp/", "/config/testapp_dev/");
4545
}
4646

4747
@Test

spring-cloud-starter-aws-secrets-manager-config/src/test/java/io/awspring/cloud/autoconfigure/secretsmanager/AwsSecretsManagerConfigDataLocationResolverTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ void testResolveProfileSpecificWithAutomaticPaths() {
4141
String location = "aws-secretsmanager:";
4242
List<AwsSecretsManagerConfigDataResource> locations = testResolveProfileSpecific(location);
4343
assertThat(locations).hasSize(4);
44-
assertThat(toContexts(locations)).containsExactly("/secret/testapp_dev", "/secret/testapp",
45-
"/secret/application_dev", "/secret/application");
44+
assertThat(toContexts(locations)).containsExactly("/secret/application", "/secret/application_dev",
45+
"/secret/testapp", "/secret/testapp_dev");
4646
}
4747

4848
@Test

0 commit comments

Comments
 (0)