Browse Source

Revert #473

pull/484/head
Ryan Baxter 6 years ago
parent
commit
bb1e769ce3
  1. 33
      spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializer.java
  2. 63
      spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializerTests.java

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2013-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -43,7 +43,7 @@ import org.springframework.core.env.SystemEnvironmentPropertySource; @@ -43,7 +43,7 @@ import org.springframework.core.env.SystemEnvironmentPropertySource;
import org.springframework.security.crypto.encrypt.TextEncryptor;
/**
* Decrypts properties from the environment and inserts them with high priority so they
* Decrypt properties from the environment and insert them with high priority so they
* override the encrypted values.
*
* @author Dave Syer
@ -74,18 +74,18 @@ public class EnvironmentDecryptApplicationInitializer implements @@ -74,18 +74,18 @@ public class EnvironmentDecryptApplicationInitializer implements
private boolean failOnError = true;
public EnvironmentDecryptApplicationInitializer(TextEncryptor encryptor) {
this.encryptor = encryptor;
}
/**
* Strategy to determine how to handle exceptions during decryption.
* @param failOnError The flag value (default true).
* @param failOnError the flag value (default true)
*/
public void setFailOnError(boolean failOnError) {
this.failOnError = failOnError;
}
public EnvironmentDecryptApplicationInitializer(TextEncryptor encryptor) {
this.encryptor = encryptor;
}
@Override
public int getOrder() {
return this.order;
@ -185,7 +185,15 @@ public class EnvironmentDecryptApplicationInitializer implements @@ -185,7 +185,15 @@ public class EnvironmentDecryptApplicationInitializer implements
private void decrypt(PropertySource<?> source, Map<String, Object> overrides) {
if (source instanceof EnumerablePropertySource) {
if (source instanceof CompositePropertySource) {
for (PropertySource<?> nested : ((CompositePropertySource) source)
.getPropertySources()) {
decrypt(nested, overrides);
}
}
else if (source instanceof EnumerablePropertySource) {
Map<String, Object> otherCollectionProperties = new LinkedHashMap<>();
boolean sourceHasDecryptedCollection = false;
@ -235,15 +243,6 @@ public class EnvironmentDecryptApplicationInitializer implements @@ -235,15 +243,6 @@ public class EnvironmentDecryptApplicationInitializer implements
}
}
else if (source instanceof CompositePropertySource) {
for (PropertySource<?> nested : ((CompositePropertySource) source)
.getPropertySources()) {
decrypt(nested, overrides);
}
}
}
}

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

@ -17,7 +17,6 @@ @@ -17,7 +17,6 @@
package org.springframework.cloud.bootstrap.encrypt;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
@ -36,9 +35,8 @@ import org.springframework.security.crypto.encrypt.Encryptors; @@ -36,9 +35,8 @@ import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.security.crypto.encrypt.TextEncryptor;
import static org.assertj.core.api.BDDAssertions.then;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.springframework.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationInitializer.DECRYPTED_PROPERTY_SOURCE_NAME;
@ -79,17 +77,6 @@ public class EnvironmentDecryptApplicationInitializerTests { @@ -79,17 +77,6 @@ public class EnvironmentDecryptApplicationInitializerTests {
then(context.getEnvironment().getProperty("foo")).isEqualTo("spam");
}
@Test
public void propertySourcesOrderedCorrectlyWithUnencryptedOverrides() {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
TestPropertyValues.of("foo: {cipher}bar").applyTo(context);
context.getEnvironment().getPropertySources()
.addFirst(new MapPropertySource("test_override",
Collections.<String, Object>singletonMap("foo", "spam")));
this.listener.initialize(context);
then(context.getEnvironment().getProperty("foo")).isEqualTo("spam");
}
@Test(expected = IllegalStateException.class)
public void errorOnDecrypt() {
this.listener = new EnvironmentDecryptApplicationInitializer(
@ -167,49 +154,23 @@ public class EnvironmentDecryptApplicationInitializerTests { @@ -167,49 +154,23 @@ public class EnvironmentDecryptApplicationInitializerTests {
@Test
public void testDecryptCompositePropertySource() {
String expected = "always";
TextEncryptor textEncryptor = mock(TextEncryptor.class);
when(textEncryptor.decrypt(anyString())).thenReturn(expected);
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext();
EnvironmentDecryptApplicationInitializer initializer = new EnvironmentDecryptApplicationInitializer(
Encryptors.noOpText());
textEncryptor);
CompositePropertySource cps = new CompositePropertySource("testCPS");
Map<String, Object> map1 = new HashMap<>();
map1.put("key1", "{cipher}value1b");
map1.put("key2", "value2b");
cps.addPropertySource(new MapPropertySource("profile1", map1));
Map<String, Object> map2 = new HashMap<>();
map2.put("key1", "{cipher}value1");
map2.put("key2", "value2");
map1.put("key3", "value3");
cps.addPropertySource(new MapPropertySource("profile2", map2));
// add non-enumerable property source that will fail cps.getPropertyNames()
cps.addPropertySource(mock(PropertySource.class));
MapPropertySource source = new MapPropertySource("nobody",
Collections.singletonMap("key", "{cipher}value"));
CompositePropertySource cps = mock(CompositePropertySource.class);
when(cps.getPropertyNames()).thenReturn(source.getPropertyNames());
when(cps.getPropertySources()).thenReturn(Collections.singleton(source));
ctx.getEnvironment().getPropertySources().addLast(cps);
initializer.initialize(ctx);
// validate behaviour with encryption
then(ctx.getEnvironment().getProperty("key1")).isEqualTo("value1b");
// validate behaviour without encryption
then(ctx.getEnvironment().getProperty("key2")).isEqualTo("value2b");
// validate behaviour without override
then(ctx.getEnvironment().getProperty("key3")).isEqualTo("value3");
}
@Test
public void testOnlyDecryptIfNotOverridden() {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
TextEncryptor encryptor = mock(TextEncryptor.class);
when(encryptor.decrypt("bar2")).thenReturn("bar2");
EnvironmentDecryptApplicationInitializer initializer = new EnvironmentDecryptApplicationInitializer(
encryptor);
TestPropertyValues.of("foo: {cipher}bar", "foo2: {cipher}bar2").applyTo(context);
context.getEnvironment().getPropertySources()
.addFirst(new MapPropertySource("test_override",
Collections.<String, Object>singletonMap("foo", "spam")));
initializer.initialize(context);
then(context.getEnvironment().getProperty("foo")).isEqualTo("spam");
then(context.getEnvironment().getProperty("foo2")).isEqualTo("bar2");
verify(encryptor).decrypt("bar2");
verifyNoMoreInteractions(encryptor);
then(ctx.getEnvironment().getProperty("key")).isEqualTo(expected);
}
}

Loading…
Cancel
Save