Skip to content

Commit

Permalink
Merge pull request #3 from FBibonne/1-manage-excluded-sources-as-desc…
Browse files Browse the repository at this point in the history
…ribed-in-readme

1 manage excluded sources as described in readme
  • Loading branch information
FBibonne authored Sep 12, 2024
2 parents 5925b9c + b7b1b23 commit e8e750c
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 4 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,21 @@ When a property is displayed, there is a filter which avoid to leak secrets in l
one of the token in `properties.logger.with-hidden-values` will be displayed with the value `******` instead of the actual value.
The default value of `properties.logger.with-hidden-values` contains the most common tokens for secrets but you should
add the specific ones you may have in your application.


## Warnings

### Properties from `@DynamicPropertySource` methods will not be displayed

The Properties-Logger library cannot access to the property source of `@DynamicPropertySource` methods because
the properties logger library runs before the `@DynamicPropertySource` methods are executed. Therefore, **neither the properties
keys exclusively defined in `@DynamicPropertySource` methods, neither the values defined in this methods, whatever the
property key is, will be displayed** even if `@DynamicPropertySource` methods property source is not excluded.

`@DynamicPropertySource` methods are a specific mecanism dedicated to test which let you define or overrride properties values
very late so it is not in the scope of the Properties Logger library which aims to display properties at the earliest time
in the application lifecycle. It is strongly recommended to not define in other place properties from `@DynamicPropertySource`
methods. If you have no other choice (make sure you had a complete look to
[the numerous ways to override properties in Spring Boot](https://docs.spring.io/spring-boot/reference/features/external-config.html) )
that defining `@DynamicPropertySource` methods to override properties in a test and you want to check the value of the
overrided property, consider logging it yourself or write an assertion in your test
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>fr.insee</groupId>
<artifactId>boot-properties-logger-starter</artifactId>
<version>1.0.1</version>
<version>1.1.0</version>
<packaging>jar</packaging>

<properties>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/fr/insee/boot/PropertiesLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private boolean willBeProcessed(PropertySource<?> propertySource, Set<String> ig
}

private boolean isNotIgnored(PropertySource<?> propertySource, Set<String> ignoredPropertySources) {
if (ignoredPropertySources.contains(propertySource.getName())) {
if (ignoredPropertySources.stream().anyMatch(propertySource.getName()::contains)) {
log.trace(() -> propertySource + " is listed to be ignored");
return false;
}
Expand Down
100 changes: 100 additions & 0 deletions src/test/java/fr/insee/test/ExcludedPropertySourcesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package fr.insee.test;

import fr.insee.boot.PropertiesLogger;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(classes = ExcludedPropertySourcesTest.class, properties = {
"fr.insee.shared = inlineTestProperties",
"fr.insee.specific.inlineTestProperties = inlineTestProperties"},
args = { "--fr.insee.shared=args", "--fr.insee.specific.args=args","--spring.config.additional-location=classpath:/otherProps/,classpath:/nonExcluded/"}
)
@Configuration
class ExcludedPropertySourcesTest {

static Path externalPropertiesPath;

@BeforeAll
static void createTempPropertyFileInFileSystem() throws IOException {
externalPropertiesPath = Files.createTempFile("inFileSystem", ".properties");
String content = """
fr.insee.shared = inFileSystem
fr.insee.specific.inFileSystem = inFileSystem
fr.insee.sharedWithExternal = inFileSystem
properties.logger.sources-ignored = systemProperties, systemEnvironment,[application.properties],[otherProps/application.properties],commandLineArgs,Inlined\\ Test\\ Properties,["""
+ externalPropertiesPath.toAbsolutePath()+"]";
Files.writeString(externalPropertiesPath, content);

System.setProperty("spring.config.import", "file:" + externalPropertiesPath.toAbsolutePath());
}

@DynamicPropertySource
static void setProperties(DynamicPropertyRegistry registry) {
registry.add("fr.insee.shared", ()->"dynamicPropertySource");
registry.add("fr.insee.specific.dynamicPropertySource",()->"dynamicPropertySource");
}


@Test
void contextLoad_shouldNotPrintExcludedPropertySources(@Autowired Environment environment) {

//GIVEN context and prop properties.logger.sources-ignored= systemProperties, systemEnvironment,[application.properties],otherProps/application.properties,commandLineArgs,Inlined\ Test\ Properties
//not excluded : nonExcluded.properties,
//WHEN context loads
//THEN
String logOutput = ((Slf4jStub) LoggerFactory.getLogger(PropertiesLogger.class)).getStringBuilder().toString();

System.out.println(logOutput);

assertThat(environment.getProperty("properties.logger.sources-ignored")).hasToString("systemProperties, systemEnvironment,[application.properties],[otherProps/application.properties],commandLineArgs,Inlined Test Properties,["+ externalPropertiesPath.toAbsolutePath()+"]");
assertThat(environment.getProperty("fr.insee.shared")).hasToString("dynamicPropertySource");
assertThat(environment.getProperty("fr.insee.specific.nonExcludedFile")).hasToString("nonExcludedFile");
assertThat(logOutput).contains("fr.insee.shared = inlineTestProperties")
.doesNotContain("fr.insee.specific.dynamicPropertySource")
.contains("fr.insee.specific.nonExcludedFile = nonExcludedFile")
.doesNotContain("fr.insee.sharedWithExternal = nonExcludedFile")
.contains("fr.insee.sharedWithExternal = inFileSystem")
.doesNotContain("fr.insee.specific.inFileSystem = inFileSystem")
.doesNotContain("fr.insee.specific.applicationproperties")
.doesNotContain("fr.insee.specific.additionalPropsInClasspath")
.doesNotContain("fr.insee.specific.args")
.doesNotContain("fr.insee.specific.inFileSystem")
.doesNotContain("fr.insee.specific.inlineTestProperties")
.doesNotContain("class path resource [application.properties]")
.doesNotContain("class path resource [otherProps/application.properties]")
.doesNotContain("- commandLineArgs")
.doesNotContain("Config resource 'file ["+externalPropertiesPath+"]'")
.doesNotContain("- Inlined Test Properties")
.doesNotContain("- systemProperties")
.doesNotContain("- systemEnvironment")
.doesNotContain("class path resource [application.properties]")
;
assertThat(environment.getProperty("fr.insee.specific.inFileSystem")).hasToString("inFileSystem");
assertThat(environment.getProperty("fr.insee.specific.applicationproperties")).hasToString("application.properties");
assertThat(environment.getProperty("fr.insee.specific.additionalPropsInClasspath")).hasToString("additionalPropsInClasspath");
assertThat(environment.getProperty("fr.insee.specific.args")).hasToString("args");

}

@AfterAll
static void deleteTempPropertyFileInFileSystem() throws IOException {
Files.delete(externalPropertiesPath);
System.clearProperty("spring.config.import");
((Slf4jStub) LoggerFactory.getLogger(PropertiesLogger.class)).getStringBuilder().setLength(0);
}

}
10 changes: 9 additions & 1 deletion src/test/java/fr/insee/test/WebAppIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.insee.test;

import fr.insee.boot.PropertiesLogger;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
Expand All @@ -10,7 +11,7 @@

@SpringBootTest(classes = WebAppIntegrationTest.class, properties = {
"properties.logger.sources-ignored = systemEnvironment",
"properties.logger.prefix-for-properties = info, logging, spring, server, management, properties, springdoc, fr"
"properties.logger.prefix-for-properties = info, logging, spring, server, management, properties, springdoc, fr",
})
@Configuration
class WebAppIntegrationTest {
Expand All @@ -30,8 +31,15 @@ void contextLoads() {
properties.logger.prefix-for-properties = info, logging, spring, server, management, properties, springdoc, fr%n\
fr.insee.test = ok%n\
fr.insee.secret = ******%n\
fr.insee.shared = application.properties%n\
fr.insee.specific.applicationproperties = application.properties%n\
================================================================================%n""").formatted()
);
}

@AfterAll
static void clearLogStub(){
((Slf4jStub) LoggerFactory.getLogger(PropertiesLogger.class)).getStringBuilder().setLength(0);
}

}
43 changes: 43 additions & 0 deletions src/test/java/spring/issue/IssueWithDynamicPropertySourceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package spring.issue;

import fr.insee.boot.PropertiesLogger;
import fr.insee.test.Slf4jStub;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(classes = IssueWithDynamicPropertySourceTest.class ,properties = {
"spring.config.import=",
//"spring.config.additional-location=classpath:spring/issue/application.properties"
}
)
@Configuration
class IssueWithDynamicPropertySourceTest {

@DynamicPropertySource
static void setProperties(DynamicPropertyRegistry registry) {
registry.add("spring.config.additional-location", () -> "classpath:/spring/issue/");
}

@Test
@Disabled
void propertyFromAddtionalLocationShouldBeLoaded(@Autowired Environment environment) {
assertThat(environment.getProperty("spring.config.additional-location")).hasToString("classpath:/spring/issue/");
assertThat(environment.getProperty("property.in.addtional.file")).hasToString("ok");
}

@AfterAll
static void clearLogStub(){
((Slf4jStub) LoggerFactory.getLogger(PropertiesLogger.class)).getStringBuilder().setLength(0);
}

}
5 changes: 4 additions & 1 deletion src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
fr.insee.test = ok
fr.insee.secret = SECRET
fr.insee.secret = SECRET
fr.insee.shared = application.properties
fr.insee.specific.applicationproperties = application.properties
properties.logger.prefix-for-properties=debug, trace, info, logging, spring, server, management, springdoc, properties, fr
3 changes: 3 additions & 0 deletions src/test/resources/nonExcluded/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fr.insee.shared = nonExcludedFile
fr.insee.specific.nonExcludedFile = nonExcludedFile
fr.insee.sharedWithExternal = nonExcludedFile
2 changes: 2 additions & 0 deletions src/test/resources/otherProps/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fr.insee.shared = additionalPropsInClasspath
fr.insee.specific.additionalPropsInClasspath = additionalPropsInClasspath
1 change: 1 addition & 0 deletions src/test/resources/spring/issue/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
property.in.addtional.file=ok

0 comments on commit e8e750c

Please sign in to comment.