From bb1e769ce38ead4f3a51ac99124d940ce1c74fe8 Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Thu, 14 Feb 2019 18:51:22 -0500 Subject: [PATCH] Revert #473 --- ...ironmentDecryptApplicationInitializer.java | 33 +++++----- ...entDecryptApplicationInitializerTests.java | 63 ++++--------------- 2 files changed, 28 insertions(+), 68 deletions(-) diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializer.java b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializer.java index 196c0599..53a892b9 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializer.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializer.java @@ -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; 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 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 private void decrypt(PropertySource source, Map overrides) { - if (source instanceof EnumerablePropertySource) { + if (source instanceof CompositePropertySource) { + + for (PropertySource nested : ((CompositePropertySource) source) + .getPropertySources()) { + decrypt(nested, overrides); + } + + } + else if (source instanceof EnumerablePropertySource) { Map otherCollectionProperties = new LinkedHashMap<>(); boolean sourceHasDecryptedCollection = false; @@ -235,15 +243,6 @@ public class EnvironmentDecryptApplicationInitializer implements } } - else if (source instanceof CompositePropertySource) { - - for (PropertySource nested : ((CompositePropertySource) source) - .getPropertySources()) { - decrypt(nested, overrides); - } - - } - } } diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializerTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializerTests.java index e004ba88..f433d73c 100644 --- a/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializerTests.java +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializerTests.java @@ -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; 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 { 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.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 { @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 map1 = new HashMap<>(); - map1.put("key1", "{cipher}value1b"); - map1.put("key2", "value2b"); - cps.addPropertySource(new MapPropertySource("profile1", map1)); - Map 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.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); } }