|
|
@ -19,8 +19,10 @@ package org.springframework.cloud.bootstrap.encrypt; |
|
|
|
import java.util.Collections; |
|
|
|
import java.util.Collections; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.junit.Rule; |
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.boot.test.rule.OutputCapture; |
|
|
|
import org.springframework.boot.test.util.TestPropertyValues; |
|
|
|
import org.springframework.boot.test.util.TestPropertyValues; |
|
|
|
import org.springframework.boot.test.util.TestPropertyValues.Type; |
|
|
|
import org.springframework.boot.test.util.TestPropertyValues.Type; |
|
|
|
import org.springframework.context.ApplicationContext; |
|
|
|
import org.springframework.context.ApplicationContext; |
|
|
@ -49,6 +51,9 @@ public class EnvironmentDecryptApplicationInitializerTests { |
|
|
|
private EnvironmentDecryptApplicationInitializer listener = new EnvironmentDecryptApplicationInitializer( |
|
|
|
private EnvironmentDecryptApplicationInitializer listener = new EnvironmentDecryptApplicationInitializer( |
|
|
|
Encryptors.noOpText()); |
|
|
|
Encryptors.noOpText()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Rule |
|
|
|
|
|
|
|
public OutputCapture outputCapture = new OutputCapture(); |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void decryptCipherKey() { |
|
|
|
public void decryptCipherKey() { |
|
|
|
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(); |
|
|
|
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(); |
|
|
@ -77,14 +82,22 @@ public class EnvironmentDecryptApplicationInitializerTests { |
|
|
|
then(context.getEnvironment().getProperty("foo")).isEqualTo("spam"); |
|
|
|
then(context.getEnvironment().getProperty("foo")).isEqualTo("spam"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test(expected = IllegalStateException.class) |
|
|
|
@Test |
|
|
|
public void errorOnDecrypt() { |
|
|
|
public void errorOnDecrypt() { |
|
|
|
this.listener = new EnvironmentDecryptApplicationInitializer( |
|
|
|
this.listener = new EnvironmentDecryptApplicationInitializer( |
|
|
|
Encryptors.text("deadbeef", "AFFE37")); |
|
|
|
Encryptors.text("deadbeef", "AFFE37")); |
|
|
|
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(); |
|
|
|
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(); |
|
|
|
TestPropertyValues.of("foo: {cipher}bar").applyTo(context); |
|
|
|
TestPropertyValues.of("foo: {cipher}bar").applyTo(context); |
|
|
|
|
|
|
|
// catch IllegalStateException and verify
|
|
|
|
|
|
|
|
try { |
|
|
|
this.listener.initialize(context); |
|
|
|
this.listener.initialize(context); |
|
|
|
then(context.getEnvironment().getProperty("foo")).isEqualTo("bar"); |
|
|
|
} |
|
|
|
|
|
|
|
catch (Exception e) { |
|
|
|
|
|
|
|
then(e).isInstanceOf(IllegalStateException.class); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Assert logs contain warning even when exception thrown
|
|
|
|
|
|
|
|
String sysOutput = this.outputCapture.toString(); |
|
|
|
|
|
|
|
then(sysOutput).contains("Cannot decrypt: key=foo"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
@ -95,6 +108,9 @@ public class EnvironmentDecryptApplicationInitializerTests { |
|
|
|
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(); |
|
|
|
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(); |
|
|
|
TestPropertyValues.of("foo: {cipher}bar").applyTo(context); |
|
|
|
TestPropertyValues.of("foo: {cipher}bar").applyTo(context); |
|
|
|
this.listener.initialize(context); |
|
|
|
this.listener.initialize(context); |
|
|
|
|
|
|
|
// Assert logs contain warning
|
|
|
|
|
|
|
|
String sysOutput = this.outputCapture.toString(); |
|
|
|
|
|
|
|
then(sysOutput).contains("Cannot decrypt: key=foo"); |
|
|
|
// Empty is safest fallback for undecryptable cipher
|
|
|
|
// Empty is safest fallback for undecryptable cipher
|
|
|
|
then(context.getEnvironment().getProperty("foo")).isEqualTo(""); |
|
|
|
then(context.getEnvironment().getProperty("foo")).isEqualTo(""); |
|
|
|
} |
|
|
|
} |
|
|
|