@ -17,23 +17,25 @@
package org.springframework.boot.actuate.env ;
import java.util.Collections ;
import java.util. HashMap;
import java.util. Linked HashMap;
import java.util.Map ;
import org.junit.After ;
import org.junit.Test ;
import org.springframework.boot.actuate.env.EnvironmentEndpoint.EnvironmentDescriptor ;
import org.springframework.boot.actuate.env.EnvironmentEndpoint.EnvironmentDescriptor.PropertySourceDescriptor ;
import org.springframework.boot.actuate.env.EnvironmentEndpoint.EnvironmentDescriptor.PropertySourceDescriptor.PropertyValueDescriptor ;
import org.springframework.boot.actuate.env.EnvironmentEndpoint.EnvironmentEntryDescriptor ;
import org.springframework.boot.actuate.env.EnvironmentEndpoint.PropertySourceDescriptor ;
import org.springframework.boot.actuate.env.EnvironmentEndpoint.PropertySourceEntryDescriptor ;
import org.springframework.boot.actuate.env.EnvironmentEndpoint.PropertyValueDescriptor ;
import org.springframework.boot.context.properties.EnableConfigurationProperties ;
import org.springframework.boot.test.util.TestPropertyValues ;
import org.springframework.context.annotation.Bean ;
import org.springframework.context.annotation.Configuration ;
import org.springframework.core.env.CompositePropertySource ;
import org.springframework.core.env.ConfigurableEnvironment ;
import org.springframework.core.env.Environment ;
import org.springframework.core.env.MapPropertySource ;
import org.springframework.core.env.MutablePropertySources ;
import org.springframework.core.env.StandardEnvironment ;
import static org.assertj.core.api.Assertions.assertThat ;
@ -57,184 +59,243 @@ public class EnvironmentEndpointTests {
@Test
public void basicResponse ( ) {
EnvironmentDescriptor env = new EnvironmentEndpoint ( new StandardEnvironment ( ) )
ConfigurableEnvironment environment = emptyEnvironment ( ) ;
environment . getPropertySources ( ) . addLast (
singleKeyPropertySource ( "one" , "my.key" , "first" ) ) ;
environment . getPropertySources ( ) . addLast (
singleKeyPropertySource ( "two" , "my.key" , "second" ) ) ;
EnvironmentDescriptor descriptor = new EnvironmentEndpoint ( environment )
. environment ( null ) ;
assertThat ( env . getActiveProfiles ( ) ) . isEmpty ( ) ;
assertThat ( env . getPropertySources ( ) ) . hasSize ( 2 ) ;
assertThat ( descriptor . getActiveProfiles ( ) ) . isEmpty ( ) ;
Map < String , PropertySourceDescriptor > sources = propertySources ( descriptor ) ;
assertThat ( sources . keySet ( ) ) . containsExactly ( "one" , "two" ) ;
assertThat ( sources . get ( "one" ) . getProperties ( ) ) . containsOnlyKeys ( "my.key" ) ;
assertThat ( sources . get ( "two" ) . getProperties ( ) ) . containsOnlyKeys ( "my.key" ) ;
}
@Test
public void compositeSourceIsHandledCorrectly ( ) {
StandardEnvironment environment = new Standard Environment( ) ;
ConfigurableEnvironment environment = empty Environment( ) ;
CompositePropertySource source = new CompositePropertySource ( "composite" ) ;
source . addPropertySource (
new MapPropertySource ( "one" , Collections . singletonMap ( "foo" , "bar" ) ) ) ;
source . addPropertySource (
new MapPropertySource ( "two" , Collections . singletonMap ( "foo" , "spam" ) ) ) ;
environment . getPropertySources ( ) . addFirst ( source ) ;
EnvironmentDescriptor env = new EnvironmentEndpoint ( environment )
EnvironmentDescriptor descriptor = new EnvironmentEndpoint ( environment )
. environment ( null ) ;
assertThat ( getSource ( "composite:one" , env ) . getProperties ( ) . get ( "foo" ) . getValue ( ) )
Map < String , PropertySourceDescriptor > sources = propertySources ( descriptor ) ;
assertThat ( sources . keySet ( ) ) . containsExactly ( "composite:one" , "composite:two" ) ;
assertThat ( sources . get ( "composite:one" ) . getProperties ( ) . get ( "foo" ) . getValue ( ) )
. isEqualTo ( "bar" ) ;
assertThat ( getSource ( "composite:two" , env ) . getProperties ( ) . get ( "foo" ) . getValue ( ) )
assertThat ( sources. get ( "composite:two" ) . getProperties ( ) . get ( "foo" ) . getValue ( ) )
. isEqualTo ( "spam" ) ;
}
@Test
public void sensitiveKeysHaveTheirValuesSanitized ( ) {
System . setProperty ( "dbPassword" , "123456" ) ;
System . setProperty ( "apiKey" , "123456" ) ;
System . setProperty ( "mySecret" , "123456" ) ;
System . setProperty ( "myCredentials" , "123456" ) ;
System . setProperty ( "VCAP_SERVICES" , "123456" ) ;
EnvironmentDescriptor env = new EnvironmentEndpoint ( new StandardEnvironment ( ) )
. environment ( null ) ;
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" ) ;
TestPropertyValues . of ( "dbPassword=123456" , "apiKey=123456" , "mySecret=123456" ,
"myCredentials=123456" , "VCAP_SERVICES=123456"
) . applyToSystemProperties ( ( ) - > {
EnvironmentDescriptor descriptor = new EnvironmentEndpoint (
new StandardEnvironment ( ) ) . environment ( null ) ;
Map < String , PropertyValueDescriptor > systemProperties = propertySources (
descriptor ) . get ( "systemProperties" ) . 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 ( "******" ) ;
return null ;
} ) ;
}
@Test
public void sensitiveKeysMatchingCredentialsPatternHaveTheirValuesSanitized ( ) {
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" ) ;
EnvironmentDescriptor env = new EnvironmentEndpoint ( new StandardEnvironment ( ) )
. environment ( null ) ;
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 ( "my.services.cleardb-free.credentials" ) . getValue ( ) )
. isEqualTo ( "******" ) ;
assertThat ( systemProperties . get ( "foo.mycredentials.uri" ) . getValue ( ) )
. isEqualTo ( "******" ) ;
clearSystemProperties ( "my.services.amqp-free.credentials.uri" ,
"credentials.http_api_uri" , "my.services.cleardb-free.credentials" ,
"foo.mycredentials.uri" ) ;
TestPropertyValues . of ( "my.services.amqp-free.credentials.uri=123456" ,
"credentials.http_api_uri=123456" ,
"my.services.cleardb-free.credentials=123456" ,
"foo.mycredentials.uri=123456" ) . applyToSystemProperties ( ( ) - > {
EnvironmentDescriptor descriptor = new EnvironmentEndpoint (
new StandardEnvironment ( ) ) . environment ( null ) ;
Map < String , PropertyValueDescriptor > systemProperties = propertySources (
descriptor ) . get ( "systemProperties" ) . getProperties ( ) ;
assertThat (
systemProperties . get ( "my.services.amqp-free.credentials.uri" ) . getValue ( ) )
. isEqualTo ( "******" ) ;
assertThat ( systemProperties . get ( "credentials.http_api_uri" ) . getValue ( ) )
. isEqualTo ( "******" ) ;
assertThat (
systemProperties . get ( "my.services.cleardb-free.credentials" ) . getValue ( ) )
. isEqualTo ( "******" ) ;
assertThat ( systemProperties . get ( "foo.mycredentials.uri" ) . getValue ( ) )
. isEqualTo ( "******" ) ;
return null ;
} ) ;
}
@Test
public void sensitiveKeysMatchingCustomNameHaveTheirValuesSanitized ( ) {
System . setProperty ( "dbPassword" , "123456" ) ;
System . setProperty ( "apiKey" , "123456" ) ;
EnvironmentEndpoint endpoint = new EnvironmentEndpoint ( new StandardEnvironment ( ) ) ;
endpoint . setKeysToSanitize ( "key" ) ;
EnvironmentDescriptor env = endpoint . environment ( null ) ;
Map < String , PropertyValueDescriptor > systemProperties = getSource (
"systemProperties" , env ) . getProperties ( ) ;
assertThat ( systemProperties . get ( "dbPassword" ) . getValue ( ) ) . isEqualTo ( "123456" ) ;
assertThat ( systemProperties . get ( "apiKey" ) . getValue ( ) ) . isEqualTo ( "******" ) ;
clearSystemProperties ( "dbPassword" , "apiKey" ) ;
TestPropertyValues . of ( "dbPassword=123456" ,
"apiKey=123456" ) . applyToSystemProperties ( ( ) - > {
EnvironmentEndpoint endpoint = new EnvironmentEndpoint ( new StandardEnvironment ( ) ) ;
endpoint . setKeysToSanitize ( "key" ) ;
EnvironmentDescriptor descriptor = endpoint . environment ( null ) ;
Map < String , PropertyValueDescriptor > systemProperties = propertySources (
descriptor ) . get ( "systemProperties" ) . getProperties ( ) ;
assertThat ( systemProperties . get ( "dbPassword" ) . getValue ( ) ) . isEqualTo ( "123456" ) ;
assertThat ( systemProperties . get ( "apiKey" ) . getValue ( ) ) . isEqualTo ( "******" ) ;
return null ;
} ) ;
}
@Test
public void sensitiveKeysMatchingCustomPatternHaveTheirValuesSanitized ( ) {
System . setProperty ( "dbPassword" , "123456" ) ;
System . setProperty ( "apiKey" , "123456" ) ;
EnvironmentEndpoint endpoint = new EnvironmentEndpoint ( new StandardEnvironment ( ) ) ;
endpoint . setKeysToSanitize ( ".*pass.*" ) ;
EnvironmentDescriptor env = endpoint . environment ( null ) ;
Map < String , PropertyValueDescriptor > systemProperties = getSource (
"systemProperties" , env ) . getProperties ( ) ;
assertThat ( systemProperties . get ( "dbPassword" ) . getValue ( ) ) . isEqualTo ( "******" ) ;
assertThat ( systemProperties . get ( "apiKey" ) . getValue ( ) ) . isEqualTo ( "123456" ) ;
clearSystemProperties ( "dbPassword" , "apiKey" ) ;
TestPropertyValues . of ( "dbPassword=123456" ,
"apiKey=123456" ) . applyToSystemProperties ( ( ) - > {
EnvironmentEndpoint endpoint = new EnvironmentEndpoint ( new StandardEnvironment ( ) ) ;
endpoint . setKeysToSanitize ( ".*pass.*" ) ;
EnvironmentDescriptor descriptor = endpoint . environment ( null ) ;
Map < String , PropertyValueDescriptor > systemProperties = propertySources (
descriptor ) . get ( "systemProperties" ) . getProperties ( ) ;
assertThat ( systemProperties . get ( "dbPassword" ) . getValue ( ) ) . isEqualTo ( "******" ) ;
assertThat ( systemProperties . get ( "apiKey" ) . getValue ( ) ) . isEqualTo ( "123456" ) ;
return null ;
} ) ;
}
@Test
public void propertyWithPlaceholderResolved ( ) {
StandardEnvironment environment = new Standard Environment( ) ;
ConfigurableEnvironment environment = empty Environment( ) ;
TestPropertyValues . of ( "my.foo: ${bar.blah}" , "bar.blah: hello" )
. applyTo ( environment ) ;
EnvironmentDescriptor env = new EnvironmentEndpoint ( environment )
EnvironmentDescriptor descriptor = new EnvironmentEndpoint ( environment )
. environment ( null ) ;
assertThat ( getSource( "test" , env ) . getProperties ( ) . get ( "my.foo" ) . getValue ( ) )
. isEqualTo( "hello" ) ;
assertThat ( propertySources( descriptor ) . get ( "test" ) . getProperties ( ) . get ( "my.foo" )
. getValue( ) ) . isEqualTo( "hello" ) ;
}
@Test
public void propertyWithPlaceholderNotResolved ( ) {
StandardEnvironment environment = new Standard Environment( ) ;
ConfigurableEnvironment environment = empty Environment( ) ;
TestPropertyValues . of ( "my.foo: ${bar.blah}" ) . applyTo ( environment ) ;
EnvironmentDescriptor env = new EnvironmentEndpoint ( environment )
EnvironmentDescriptor descriptor = new EnvironmentEndpoint ( environment )
. environment ( null ) ;
assertThat ( getSource( "test" , env ) . getProperties ( ) . get ( "my.foo" ) . getValue ( ) )
. isEqualTo( "${bar.blah}" ) ;
assertThat ( propertySources( descriptor ) . get ( "test" ) . getProperties ( ) . get ( "my.foo" )
. getValue( ) ) . isEqualTo( "${bar.blah}" ) ;
}
@Test
public void propertyWithSensitivePlaceholderResolved ( ) {
StandardEnvironment environment = new Standard Environment( ) ;
ConfigurableEnvironment environment = empty Environment( ) ;
TestPropertyValues
. of ( "my.foo: http://${bar.password}://hello" , "bar.password: hello" )
. applyTo ( environment ) ;
EnvironmentDescriptor env = new EnvironmentEndpoint ( environment )
EnvironmentDescriptor descriptor = new EnvironmentEndpoint ( environment )
. environment ( null ) ;
assertThat ( getSource( "test" , env ) . getProperties ( ) . get ( "my.foo" ) . getValue ( ) )
. isEqualTo( "http://******://hello" ) ;
assertThat ( propertySources( descriptor ) . get ( "test" ) . getProperties ( ) . get ( "my.foo" )
. getValue( ) ) . isEqualTo( "http://******://hello" ) ;
}
@Test
public void propertyWithSensitivePlaceholderNotResolved ( ) {
StandardEnvironment environment = new Standard Environment( ) ;
ConfigurableEnvironment environment = empty Environment( ) ;
TestPropertyValues . of ( "my.foo: http://${bar.password}://hello" )
. applyTo ( environment ) ;
EnvironmentDescriptor env = new EnvironmentEndpoint ( environment )
EnvironmentDescriptor descriptor = new EnvironmentEndpoint ( environment )
. environment ( null ) ;
assertThat ( getSource( "test" , env ) . getProperties ( ) . get ( "my.foo" ) . getValue ( ) )
. isEqualTo( "http://${bar.password}://hello" ) ;
assertThat ( propertySources( descriptor ) . get ( "test" ) . getProperties ( ) . get ( "my.foo" )
. getValue( ) ) . isEqualTo( "http://${bar.password}://hello" ) ;
}
@Test
@SuppressWarnings ( "unchecked" )
public void propertyWithTypeOtherThanStringShouldNotFail ( ) {
StandardEnvironment environment = new StandardEnvironment ( ) ;
MutablePropertySources propertySources = environment . getPropertySources ( ) ;
Map < String , Object > source = new HashMap < > ( ) ;
source . put ( "foo" , Collections . singletonMap ( "bar" , "baz" ) ) ;
propertySources . addFirst ( new MapPropertySource ( "test" , source ) ) ;
EnvironmentDescriptor env = new EnvironmentEndpoint ( environment )
ConfigurableEnvironment environment = emptyEnvironment ( ) ;
environment . getPropertySources ( ) . addFirst ( singleKeyPropertySource ( "test" , "foo" ,
Collections . singletonMap ( "bar" , "baz" ) ) ) ;
EnvironmentDescriptor descriptor = new EnvironmentEndpoint ( environment )
. environment ( null ) ;
Map < String , PropertyValueDescriptor > testProperties = getSource ( "test" , env )
. getProperties ( ) ;
Map < String , String > foo = ( Map < String , String > ) testProperties . get ( "foo" )
. getValue ( ) ;
Map < String , String > foo = ( Map < String , String > ) propertySources ( descriptor )
. get ( "test" ) . getProperties ( ) . get ( "foo" ) . getValue ( ) ;
assertThat ( foo . get ( "bar" ) ) . isEqualTo ( "baz" ) ;
}
@Test
public void propertyEntry ( ) {
TestPropertyValues . of ( "my.foo=another" ) . applyToSystemProperties ( ( ) - > {
StandardEnvironment environment = new StandardEnvironment ( ) ;
TestPropertyValues . of ( "my.foo=bar" , "my.foo2=bar2" ) . applyTo ( environment ,
TestPropertyValues . Type . MAP , "test" ) ;
EnvironmentEntryDescriptor descriptor = new EnvironmentEndpoint ( environment )
. environmentEntry ( "my.foo" ) ;
assertThat ( descriptor ) . isNotNull ( ) ;
assertThat ( descriptor . getProperty ( ) ) . isNotNull ( ) ;
assertThat ( descriptor . getProperty ( ) . getSource ( ) ) . isEqualTo ( "test" ) ;
assertThat ( descriptor . getProperty ( ) . getValue ( ) ) . isEqualTo ( "bar" ) ;
Map < String , PropertySourceEntryDescriptor > sources = propertySources ( descriptor ) ;
assertThat ( sources . keySet ( ) ) . containsExactly (
"test" , "systemProperties" , "systemEnvironment" ) ;
assertPropertySourceEntryDescriptor ( sources . get ( "test" ) , "bar" , null ) ;
assertPropertySourceEntryDescriptor ( sources . get ( "systemProperties" ) , "another" , null ) ;
assertPropertySourceEntryDescriptor ( sources . get ( "systemEnvironment" ) , null , null ) ;
return null ;
} ) ;
}
@Test
public void propertyEntryNotFound ( ) {
ConfigurableEnvironment environment = emptyEnvironment ( ) ;
environment . getPropertySources ( ) . addFirst (
singleKeyPropertySource ( "test" , "foo" , "bar" ) ) ;
EnvironmentEntryDescriptor descriptor = new EnvironmentEndpoint ( environment )
. environmentEntry ( "does.not.exist" ) ;
assertThat ( descriptor ) . isNotNull ( ) ;
assertThat ( descriptor . getProperty ( ) ) . isNull ( ) ;
Map < String , PropertySourceEntryDescriptor > sources = propertySources ( descriptor ) ;
assertThat ( sources . keySet ( ) ) . containsExactly (
"test" ) ;
assertPropertySourceEntryDescriptor ( sources . get ( "test" ) , null , null ) ;
}
private static ConfigurableEnvironment emptyEnvironment ( ) {
StandardEnvironment environment = new StandardEnvironment ( ) ;
TestPropertyValues . of ( "my.foo=bar" , "my.foo2=bar2" ) . applyTo ( environment ) ;
EnvironmentDescriptor env = new EnvironmentEndpoint ( environment )
. environmentEntry ( "my.foo" ) ;
assertThat ( env ) . isNotNull ( ) ;
assertThat ( getSource ( "test" , env ) . getProperties ( ) . get ( "my.foo" ) . getValue ( ) )
. isEqualTo ( "bar" ) ;
environment . getPropertySources ( ) . remove (
StandardEnvironment . SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME ) ;
environment . getPropertySources ( ) . remove (
StandardEnvironment . SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME ) ;
return environment ;
}
private void clearSystemProperties ( String . . . properties ) {
for ( String property : properties ) {
System . clearProperty ( property ) ;
}
private MapPropertySource singleKeyPropertySource ( String name , String key , Object value ) {
return new MapPropertySource ( name , Collections . singletonMap ( key , value ) ) ;
}
private PropertySourceDescriptor getSource ( String name ,
private Map< String , PropertySourceDescriptor > propertySources (
EnvironmentDescriptor descriptor ) {
return descriptor . getPropertySources ( ) . stream ( )
. filter ( ( source ) - > name . equals ( source . getName ( ) ) ) . findFirst ( ) . get ( ) ;
Map < String , PropertySourceDescriptor > sources = new LinkedHashMap < > ( ) ;
descriptor . getPropertySources ( ) . forEach ( d - > sources . put ( d . getName ( ) , d ) ) ;
return sources ;
}
private Map < String , PropertySourceEntryDescriptor > propertySources (
EnvironmentEntryDescriptor descriptor ) {
Map < String , PropertySourceEntryDescriptor > sources = new LinkedHashMap < > ( ) ;
descriptor . getPropertySources ( ) . forEach ( d - > sources . put ( d . getName ( ) , d ) ) ;
return sources ;
}
private void assertPropertySourceEntryDescriptor ( PropertySourceEntryDescriptor actual ,
Object value , String origin ) {
assertThat ( actual ) . isNotNull ( ) ;
if ( value ! = null ) {
assertThat ( actual . getProperty ( ) . getValue ( ) ) . isEqualTo ( value ) ;
assertThat ( actual . getProperty ( ) . getOrigin ( ) ) . isEqualTo ( origin ) ;
}
else {
assertThat ( actual . getProperty ( ) ) . isNull ( ) ;
}
}
@Configuration