Skip to content

Commit e024f6f

Browse files
jaartsRyan Baxter
authored and
Ryan Baxter
committed
Fix override behaviour when using CompositePropertySource. (#473)
* Fix override behaviour when using CompositePropertySource. Fixes gh-471 * Improve imports
1 parent 6cffcf9 commit e024f6f

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializer.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.cloud.bootstrap.encrypt;
1717

1818
import java.util.ArrayList;
19+
import java.util.Collections;
1920
import java.util.LinkedHashMap;
2021
import java.util.LinkedHashSet;
2122
import java.util.List;
@@ -184,9 +185,11 @@ private void collectEncryptedProperties(PropertySource<?> source,
184185
Map<String, Object> overrides) {
185186

186187
if (source instanceof CompositePropertySource) {
188+
List<PropertySource<?>> propertySources = new ArrayList<>(
189+
((CompositePropertySource) source).getPropertySources());
190+
Collections.reverse(propertySources);
187191

188-
for (PropertySource<?> nested : ((CompositePropertySource) source)
189-
.getPropertySources()) {
192+
for (PropertySource<?> nested : propertySources) {
190193
collectEncryptedProperties(nested, overrides);
191194
}
192195

spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializerTests.java

+20-12
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.cloud.bootstrap.encrypt;
1717

1818
import java.util.Collections;
19+
import java.util.HashMap;
1920
import java.util.Map;
2021

2122
import org.junit.Test;
@@ -36,7 +37,6 @@
3637
import static org.hamcrest.Matchers.is;
3738
import static org.junit.Assert.assertEquals;
3839
import static org.junit.Assert.assertThat;
39-
import static org.mockito.ArgumentMatchers.anyString;
4040
import static org.mockito.Mockito.mock;
4141
import static org.mockito.Mockito.verify;
4242
import static org.mockito.Mockito.verifyNoMoreInteractions;
@@ -168,23 +168,31 @@ public void testDecryptNonStandardParent() {
168168

169169
@Test
170170
public void testDecryptCompositePropertySource() {
171-
String expected = "always";
172-
TextEncryptor textEncryptor = mock(TextEncryptor.class);
173-
when(textEncryptor.decrypt(anyString())).thenReturn(expected);
174-
175171
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext();
176172
EnvironmentDecryptApplicationInitializer initializer = new EnvironmentDecryptApplicationInitializer(
177-
textEncryptor);
173+
Encryptors.noOpText());
178174

179-
MapPropertySource source = new MapPropertySource("nobody",
180-
Collections.singletonMap("key", "{cipher}value"));
181-
CompositePropertySource cps = mock(CompositePropertySource.class);
182-
when(cps.getPropertyNames()).thenReturn(source.getPropertyNames());
183-
when(cps.getPropertySources()).thenReturn(Collections.singleton(source));
175+
CompositePropertySource cps = new CompositePropertySource("testCPS");
176+
Map<String, Object> map1 = new HashMap<>();
177+
map1.put("key1", "{cipher}value1b");
178+
map1.put("key2", "value2b");
179+
cps.addPropertySource(new MapPropertySource("profile1", map1));
180+
Map<String, Object> map2 = new HashMap<>();
181+
map2.put("key1", "{cipher}value1");
182+
map2.put("key2", "value2");
183+
map1.put("key3", "value3");
184+
cps.addPropertySource(new MapPropertySource("profile2", map2));
185+
// add non-enumerable property source that will fail cps.getPropertyNames()
186+
cps.addPropertySource(mock(PropertySource.class));
184187
ctx.getEnvironment().getPropertySources().addLast(cps);
185188

186189
initializer.initialize(ctx);
187-
assertEquals(expected, ctx.getEnvironment().getProperty("key"));
190+
// validate behaviour with encryption
191+
assertEquals("value1b", ctx.getEnvironment().getProperty("key1"));
192+
// validate behaviour without encryption
193+
assertEquals("value2b", ctx.getEnvironment().getProperty("key2"));
194+
// validate behaviour without override
195+
assertEquals("value3", ctx.getEnvironment().getProperty("key3"));
188196
}
189197

190198
@Test

0 commit comments

Comments
 (0)