@ -23,6 +23,9 @@ import java.util.Map;
@@ -23,6 +23,9 @@ import java.util.Map;
import org.junit.After ;
import org.junit.Test ;
import org.springframework.boot.actuate.endpoint.EnvironmentEndpoint.EnvironmentDescriptor ;
import org.springframework.boot.actuate.endpoint.EnvironmentEndpoint.EnvironmentDescriptor.PropertySourceDescriptor ;
import org.springframework.boot.actuate.endpoint.EnvironmentEndpoint.EnvironmentDescriptor.PropertySourceDescriptor.PropertyValueDescriptor ;
import org.springframework.boot.context.properties.EnableConfigurationProperties ;
import org.springframework.boot.test.util.TestPropertyValues ;
import org.springframework.context.annotation.AnnotationConfigApplicationContext ;
@ -42,6 +45,7 @@ import static org.assertj.core.api.Assertions.assertThat;
@@ -42,6 +45,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Nicolas Lejeune
* @author Stephane Nicoll
* @author Madhura Bhave
* @author Andy Wilkinson
* /
public class EnvironmentEndpointTests extends AbstractEndpointTests < EnvironmentEndpoint > {
@ -56,13 +60,14 @@ public class EnvironmentEndpointTests extends AbstractEndpointTests<EnvironmentE
@@ -56,13 +60,14 @@ public class EnvironmentEndpointTests extends AbstractEndpointTests<EnvironmentE
}
@Test
public void invoke ( ) throws Exception {
assertThat ( getEndpointBean ( ) . invoke ( ) ) . isNotEmpty ( ) ;
public void invoke ( ) {
EnvironmentDescriptor env = getEndpointBean ( ) . invoke ( ) ;
assertThat ( env . getActiveProfiles ( ) ) . isEmpty ( ) ;
assertThat ( env . getPropertySources ( ) ) . hasSize ( 2 ) ;
}
@SuppressWarnings ( "unchecked" )
@Test
public void testCompositeSource ( ) throws Exception {
public void testCompositeSource ( ) {
EnvironmentEndpoint report = getEndpointBean ( ) ;
CompositePropertySource source = new CompositePropertySource ( "composite" ) ;
source . addPropertySource ( new MapPropertySource ( "one" ,
@ -70,86 +75,86 @@ public class EnvironmentEndpointTests extends AbstractEndpointTests<EnvironmentE
@@ -70,86 +75,86 @@ public class EnvironmentEndpointTests extends AbstractEndpointTests<EnvironmentE
source . addPropertySource ( new MapPropertySource ( "two" ,
Collections . singletonMap ( "foo" , ( Object ) "spam" ) ) ) ;
this . context . getEnvironment ( ) . getPropertySources ( ) . addFirst ( source ) ;
Map < String , Object > env = report . invoke ( ) ;
assertThat ( ( ( Map < String , Object > ) env . get ( "composite:one" ) ) . get ( "foo" ) )
EnvironmentDescriptor env = report . invoke ( ) ;
assertThat ( getSource ( "composite:one" , env ) . getProperties ( ) . get ( "foo" ) . getValue ( ) )
. isEqualTo ( "bar" ) ;
}
@SuppressWarnings ( "unchecked" )
@Test
public void testKeySanitization ( ) throws Exception {
public void testKeySanitization ( ) {
System . setProperty ( "dbPassword" , "123456" ) ;
System . setProperty ( "apiKey" , "123456" ) ;
System . setProperty ( "mySecret" , "123456" ) ;
System . setProperty ( "myCredentials" , "123456" ) ;
System . setProperty ( "VCAP_SERVICES" , "123456" ) ;
EnvironmentEndpoint report = getEndpointBean ( ) ;
Map < String , Object > env = report . invoke ( ) ;
Map < String , Object > systemProperties = ( Map < String , Object > ) env
. get ( "systemProperties" ) ;
assertThat ( systemProperties . get ( "dbPassword" ) ) . isEqualTo ( "******" ) ;
assertThat ( systemProperties . get ( "apiKey" ) ) . isEqualTo ( "******" ) ;
assertThat ( systemProperties . get ( "mySecret" ) ) . isEqualTo ( "******" ) ;
assertThat ( systemProperties . get ( "myCredentials" ) ) . isEqualTo ( "******" ) ;
assertThat ( systemProperties . get ( "VCAP_SERVICES" ) ) . isEqualTo ( "******" ) ;
clearSystemProperties ( "dbPassword" , "apiKey" , "mySecret" , "myCredentials" ) ;
EnvironmentDescriptor env = report . invoke ( ) ;
Map < String , PropertyValueDescriptor > systemProperties = getSource (
"systemProperties" , env ) . getProperties ( ) ;
assertThat ( systemProperties . get ( "dbPassword" ) . getValue ( ) ) . isEqualTo ( "******" ) ;
assertThat ( systemProperties . get ( "apiKey" ) . getValue ( ) ) . isEqualTo ( "******" ) ;
assertThat ( systemProperties . get ( "mySecret" ) . getValue ( ) ) . isEqualTo ( "******" ) ;
assertThat ( systemProperties . get ( "myCredentials" ) . getValue ( ) ) . isEqualTo ( "******" ) ;
assertThat ( systemProperties . get ( "VCAP_SERVICES" ) . getValue ( ) ) . isEqualTo ( "******" ) ;
clearSystemProperties ( "dbPassword" , "apiKey" , "mySecret" , "myCredentials" ,
"VCAP_SERVICES" ) ;
}
@SuppressWarnings ( "unchecked" )
@Test
public void testKeySanitizationCredentialsPattern ( ) throws Exception {
public void testKeySanitizationCredentialsPattern ( ) {
System . setProperty ( "my.services.amqp-free.credentials.uri" , "123456" ) ;
System . setProperty ( "credentials.http_api_uri" , "123456" ) ;
System . setProperty ( "my.services.cleardb-free.credentials" , "123456" ) ;
System . setProperty ( "foo.mycredentials.uri" , "123456" ) ;
EnvironmentEndpoint report = getEndpointBean ( ) ;
Map < String , Object > env = report . invoke ( ) ;
Map < String , Object > systemProperties = ( Map < String , Object > ) env
. get ( "systemProperties" ) ;
assertThat ( systemProperties . get ( "my.services.amqp-free.credentials.uri" ) )
EnvironmentDescriptor env = report . invoke ( ) ;
Map < String , PropertyValueDescriptor > systemProperties = getSource (
"systemProperties" , env ) . getProperties ( ) ;
assertThat (
systemProperties . get ( "my.services.amqp-free.credentials.uri" ) . getValue ( ) )
. isEqualTo ( "******" ) ;
assertThat ( systemProperties . get ( "credentials.http_api_uri" ) . getValue ( ) )
. isEqualTo ( "******" ) ;
assertThat ( systemProperties . get ( "credentials.http_api_uri" ) ) . isEqualTo ( "******" ) ;
assertThat ( systemProperties . get ( "my.services.cleardb-free.credentials" ) )
assertThat (
systemProperties . get ( "my.services.cleardb-free.credentials" ) . getValue ( ) )
. isEqualTo ( "******" ) ;
assertThat ( systemProperties . get ( "foo.mycredentials.uri" ) . getValue ( ) )
. isEqualTo ( "******" ) ;
assertThat ( systemProperties . get ( "foo.mycredentials.uri" ) ) . isEqualTo ( "******" ) ;
clearSystemProperties ( "my.services.amqp-free.credentials.uri" ,
"credentials.http_api_uri" , "my.services.cleardb-free.credentials" ,
"foo.mycredentials.uri" ) ;
}
@SuppressWarnings ( "unchecked" )
@Test
public void testKeySanitizationWithCustomKeys ( ) throws Exception {
public void testKeySanitizationWithCustomKeys ( ) {
System . setProperty ( "dbPassword" , "123456" ) ;
System . setProperty ( "apiKey" , "123456" ) ;
EnvironmentEndpoint report = getEndpointBean ( ) ;
report . setKeysToSanitize ( "key" ) ;
Map < String , Object > env = report . invoke ( ) ;
Map < String , Object > systemProperties = ( Map < String , Object > ) env
. get ( "systemProperties" ) ;
assertThat ( systemProperties . get ( "dbPassword" ) ) . isEqualTo ( "123456" ) ;
assertThat ( systemProperties . get ( "apiKey" ) ) . isEqualTo ( "******" ) ;
EnvironmentDescriptor env = report . invoke ( ) ;
Map < String , PropertyValueDescriptor > systemProperties = getSource (
"systemProperties" , env ) . getProperties ( ) ;
assertThat ( systemProperties . get ( "dbPassword" ) . getValue ( ) ) . isEqualTo ( "123456" ) ;
assertThat ( systemProperties . get ( "apiKey" ) . getValue ( ) ) . isEqualTo ( "******" ) ;
clearSystemProperties ( "dbPassword" , "apiKey" ) ;
}
@SuppressWarnings ( "unchecked" )
@Test
public void testKeySanitizationWithCustomPattern ( ) throws Exception {
public void testKeySanitizationWithCustomPattern ( ) {
System . setProperty ( "dbPassword" , "123456" ) ;
System . setProperty ( "apiKey" , "123456" ) ;
EnvironmentEndpoint report = getEndpointBean ( ) ;
report . setKeysToSanitize ( ".*pass.*" ) ;
Map < String , Object > env = report . invoke ( ) ;
Map < String , Object > systemProperties = ( Map < String , Object > ) env
. get ( "systemProperties" ) ;
assertThat ( systemProperties . get ( "dbPassword" ) ) . isEqualTo ( "******" ) ;
assertThat ( systemProperties . get ( "apiKey" ) ) . isEqualTo ( "123456" ) ;
EnvironmentDescriptor env = report . invoke ( ) ;
Map < String , PropertyValueDescriptor > systemProperties = getSource (
"systemProperties" , env ) . getProperties ( ) ;
assertThat ( systemProperties . get ( "dbPassword" ) . getValue ( ) ) . isEqualTo ( "******" ) ;
assertThat ( systemProperties . get ( "apiKey" ) . getValue ( ) ) . isEqualTo ( "123456" ) ;
clearSystemProperties ( "dbPassword" , "apiKey" ) ;
}
@SuppressWarnings ( "unchecked" )
@Test
public void testKeySanitizationWithCustomKeysByEnvironment ( ) throws Exception {
public void testKeySanitizationWithCustomKeysByEnvironment ( ) {
this . context = new AnnotationConfigApplicationContext ( ) ;
TestPropertyValues . of ( "endpoints.env.keys-to-sanitize: key" )
. applyTo ( this . context ) ;
@ -158,17 +163,16 @@ public class EnvironmentEndpointTests extends AbstractEndpointTests<EnvironmentE
@@ -158,17 +163,16 @@ public class EnvironmentEndpointTests extends AbstractEndpointTests<EnvironmentE
System . setProperty ( "dbPassword" , "123456" ) ;
System . setProperty ( "apiKey" , "123456" ) ;
EnvironmentEndpoint report = getEndpointBean ( ) ;
Map < String , Object > env = report . invoke ( ) ;
Map < String , Object > systemProperties = ( Map < String , Object > ) env
. get ( "systemProperties" ) ;
assertThat ( systemProperties . get ( "dbPassword" ) ) . isEqualTo ( "123456" ) ;
assertThat ( systemProperties . get ( "apiKey" ) ) . isEqualTo ( "******" ) ;
EnvironmentDescriptor env = report . invoke ( ) ;
Map < String , PropertyValueDescriptor > systemProperties = getSource (
"systemProperties" , env ) . getProperties ( ) ;
assertThat ( systemProperties . get ( "dbPassword" ) . getValue ( ) ) . isEqualTo ( "123456" ) ;
assertThat ( systemProperties . get ( "apiKey" ) . getValue ( ) ) . isEqualTo ( "******" ) ;
clearSystemProperties ( "dbPassword" , "apiKey" ) ;
}
@SuppressWarnings ( "unchecked" )
@Test
public void testKeySanitizationWithCustomPatternByEnvironment ( ) throws Exception {
public void testKeySanitizationWithCustomPatternByEnvironment ( ) {
this . context = new AnnotationConfigApplicationContext ( ) ;
TestPropertyValues . of ( "endpoints.env.keys-to-sanitize: .*pass.*" )
. applyTo ( this . context ) ;
@ -177,18 +181,16 @@ public class EnvironmentEndpointTests extends AbstractEndpointTests<EnvironmentE
@@ -177,18 +181,16 @@ public class EnvironmentEndpointTests extends AbstractEndpointTests<EnvironmentE
System . setProperty ( "dbPassword" , "123456" ) ;
System . setProperty ( "apiKey" , "123456" ) ;
EnvironmentEndpoint report = getEndpointBean ( ) ;
Map < String , Object > env = report . invoke ( ) ;
Map < String , Object > systemProperties = ( Map < String , Object > ) env
. get ( "systemProperties" ) ;
assertThat ( systemProperties . get ( "dbPassword" ) ) . isEqualTo ( "******" ) ;
assertThat ( systemProperties . get ( "apiKey" ) ) . isEqualTo ( "123456" ) ;
EnvironmentDescriptor env = report . invoke ( ) ;
Map < String , PropertyValueDescriptor > systemProperties = getSource (
"systemProperties" , env ) . getProperties ( ) ;
assertThat ( systemProperties . get ( "dbPassword" ) . getValue ( ) ) . isEqualTo ( "******" ) ;
assertThat ( systemProperties . get ( "apiKey" ) . getValue ( ) ) . isEqualTo ( "123456" ) ;
clearSystemProperties ( "dbPassword" , "apiKey" ) ;
}
@SuppressWarnings ( "unchecked" )
@Test
public void testKeySanitizationWithCustomPatternAndKeyByEnvironment ( )
throws Exception {
public void testKeySanitizationWithCustomPatternAndKeyByEnvironment ( ) {
this . context = new AnnotationConfigApplicationContext ( ) ;
TestPropertyValues . of ( "endpoints.env.keys-to-sanitize: .*pass.*, key" )
. applyTo ( this . context ) ;
@ -197,44 +199,43 @@ public class EnvironmentEndpointTests extends AbstractEndpointTests<EnvironmentE
@@ -197,44 +199,43 @@ public class EnvironmentEndpointTests extends AbstractEndpointTests<EnvironmentE
System . setProperty ( "dbPassword" , "123456" ) ;
System . setProperty ( "apiKey" , "123456" ) ;
EnvironmentEndpoint report = getEndpointBean ( ) ;
Map < String , Object > env = report . invoke ( ) ;
Map < String , Object > systemProperties = ( Map < String , Object > ) env
. get ( "systemProperties" ) ;
assertThat ( systemProperties . get ( "dbPassword" ) ) . isEqualTo ( "******" ) ;
assertThat ( systemProperties . get ( "apiKey" ) ) . isEqualTo ( "******" ) ;
EnvironmentDescriptor env = report . invoke ( ) ;
Map < String , PropertyValueDescriptor > systemProperties = getSource (
"systemProperties" , env ) . getProperties ( ) ;
assertThat ( systemProperties . get ( "dbPassword" ) . getValue ( ) ) . isEqualTo ( "******" ) ;
assertThat ( systemProperties . get ( "apiKey" ) . getValue ( ) ) . isEqualTo ( "******" ) ;
clearSystemProperties ( "dbPassword" , "apiKey" ) ;
}
@SuppressWarnings ( "unchecked" )
@Test
public void propertyWithPlaceholderResolved ( ) throws Exception {
public void propertyWithPlaceholderResolved ( ) {
this . context = new AnnotationConfigApplicationContext ( ) ;
TestPropertyValues . of ( "my.foo: ${bar.blah}" , "bar.blah: hello" )
. applyTo ( this . context ) ;
this . context . register ( Config . class ) ;
this . context . refresh ( ) ;
EnvironmentEndpoint report = getEndpointBean ( ) ;
Map < String , Object > env = report . invoke ( ) ;
Map < String , Object > testProperties = ( Map < String , Object > ) env . get ( "test" ) ;
assertThat ( testProperties . get ( "my.foo" ) ) . isEqualTo ( "hello" ) ;
EnvironmentDescriptor env = report . invoke ( ) ;
Map < String , PropertyValueDescriptor > testProperties = getSource ( "test" , env )
. getProperties ( ) ;
assertThat ( testProperties . get ( "my.foo" ) . getValue ( ) ) . isEqualTo ( "hello" ) ;
}
@SuppressWarnings ( "unchecked" )
@Test
public void propertyWithPlaceholderNotResolved ( ) throws Exception {
public void propertyWithPlaceholderNotResolved ( ) {
this . context = new AnnotationConfigApplicationContext ( ) ;
TestPropertyValues . of ( "my.foo: ${bar.blah}" ) . applyTo ( this . context ) ;
this . context . register ( Config . class ) ;
this . context . refresh ( ) ;
EnvironmentEndpoint report = getEndpointBean ( ) ;
Map < String , Object > env = report . invoke ( ) ;
Map < String , Object > testProperties = ( Map < String , Object > ) env . get ( "test" ) ;
assertThat ( testProperties . get ( "my.foo" ) ) . isEqualTo ( "${bar.blah}" ) ;
EnvironmentDescriptor env = report . invoke ( ) ;
Map < String , PropertyValueDescriptor > testProperties = getSource ( "test" , env )
. getProperties ( ) ;
assertThat ( testProperties . get ( "my.foo" ) . getValue ( ) ) . isEqualTo ( "${bar.blah}" ) ;
}
@SuppressWarnings ( "unchecked" )
@Test
public void propertyWithSensitivePlaceholderResolved ( ) throws Exception {
public void propertyWithSensitivePlaceholderResolved ( ) {
this . context = new AnnotationConfigApplicationContext ( ) ;
TestPropertyValues
. of ( "my.foo: http://${bar.password}://hello" , "bar.password: hello" )
@ -242,29 +243,31 @@ public class EnvironmentEndpointTests extends AbstractEndpointTests<EnvironmentE
@@ -242,29 +243,31 @@ public class EnvironmentEndpointTests extends AbstractEndpointTests<EnvironmentE
this . context . register ( Config . class ) ;
this . context . refresh ( ) ;
EnvironmentEndpoint report = getEndpointBean ( ) ;
Map < String , Object > env = report . invoke ( ) ;
Map < String , Object > testProperties = ( Map < String , Object > ) env . get ( "test" ) ;
assertThat ( testProperties . get ( "my.foo" ) ) . isEqualTo ( "http://******://hello" ) ;
EnvironmentDescriptor env = report . invoke ( ) ;
Map < String , PropertyValueDescriptor > testProperties = getSource ( "test" , env )
. getProperties ( ) ;
assertThat ( testProperties . get ( "my.foo" ) . getValue ( ) )
. isEqualTo ( "http://******://hello" ) ;
}
@SuppressWarnings ( "unchecked" )
@Test
public void propertyWithSensitivePlaceholderNotResolved ( ) throws Exception {
public void propertyWithSensitivePlaceholderNotResolved ( ) {
this . context = new AnnotationConfigApplicationContext ( ) ;
TestPropertyValues . of ( "my.foo: http://${bar.password}://hello" )
. applyTo ( this . context ) ;
this . context . register ( Config . class ) ;
this . context . refresh ( ) ;
EnvironmentEndpoint report = getEndpointBean ( ) ;
Map < String , Object > env = report . invoke ( ) ;
Map < String , Object > testProperties = ( Map < String , Object > ) env . get ( "test" ) ;
assertThat ( testProperties . get ( "my.foo" ) )
EnvironmentDescriptor env = report . invoke ( ) ;
Map < String , PropertyValueDescriptor > testProperties = getSource ( "test" , env )
. getProperties ( ) ;
assertThat ( testProperties . get ( "my.foo" ) . getValue ( ) )
. isEqualTo ( "http://${bar.password}://hello" ) ;
}
@Test
@SuppressWarnings ( "unchecked" )
public void propertyWithTypeOtherThanStringShouldNotFail ( ) throws Exception {
public void propertyWithTypeOtherThanStringShouldNotFail ( ) {
this . context = new AnnotationConfigApplicationContext ( ) ;
MutablePropertySources propertySources = this . context . getEnvironment ( )
. getPropertySources ( ) ;
@ -274,9 +277,11 @@ public class EnvironmentEndpointTests extends AbstractEndpointTests<EnvironmentE
@@ -274,9 +277,11 @@ public class EnvironmentEndpointTests extends AbstractEndpointTests<EnvironmentE
this . context . register ( Config . class ) ;
this . context . refresh ( ) ;
EnvironmentEndpoint report = getEndpointBean ( ) ;
Map < String , Object > env = report . invoke ( ) ;
Map < String , Object > testProperties = ( Map < String , Object > ) env . get ( "test" ) ;
Map < String , String > foo = ( Map < String , String > ) testProperties . get ( "foo" ) ;
EnvironmentDescriptor env = report . invoke ( ) ;
Map < String , PropertyValueDescriptor > testProperties = getSource ( "test" , env )
. getProperties ( ) ;
Map < String , String > foo = ( Map < String , String > ) testProperties . get ( "foo" )
. getValue ( ) ;
assertThat ( foo . get ( "bar" ) ) . isEqualTo ( "baz" ) ;
}
@ -286,6 +291,12 @@ public class EnvironmentEndpointTests extends AbstractEndpointTests<EnvironmentE
@@ -286,6 +291,12 @@ public class EnvironmentEndpointTests extends AbstractEndpointTests<EnvironmentE
}
}
private PropertySourceDescriptor getSource ( String name ,
EnvironmentDescriptor descriptor ) {
return descriptor . getPropertySources ( ) . stream ( )
. filter ( ( source ) - > name . equals ( source . getName ( ) ) ) . findFirst ( ) . get ( ) ;
}
@Configuration
@EnableConfigurationProperties
public static class Config {