Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #364 - Improve and test LoggingConfigSourceInterceptor. #400

Merged
merged 1 commit into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions implementation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.smallrye.testing</groupId>
<artifactId>smallrye-testing-utilities</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public int getLineNumber() {
return lineNumber;
}

public String getLocation() {
return lineNumber != -1 ? configSourceName + ":" + lineNumber : configSourceName;
}

public ConfigValue withName(final String name) {
return from().withName(name).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public ConfigValue getValue(final ConfigSourceInterceptorContext context, final
// Unlocked keys will run here.
ConfigValue configValue = doLocked(() -> context.proceed(name));
if (configValue != null)
ConfigLogging.log.lookup(configValue.getName(), getLocation(configValue), configValue.getValue());
ConfigLogging.log.lookup(configValue.getName(), configValue.getLocation(), configValue.getValue());
else
ConfigLogging.log.notFound(name);
return configValue;
Expand All @@ -30,9 +30,4 @@ public ConfigValue getValue(final ConfigSourceInterceptorContext context, final
ConfigLogging.log.notFound(name);
return secret;
}

private String getLocation(final ConfigValue configValue) {
return configValue.getLineNumber() != -1 ? configValue.getConfigSourceName() + ":" + configValue.getLineNumber()
: configValue.getConfigSourceName();
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,76 @@
package io.smallrye.config;

import static io.smallrye.config.KeyValuesConfigSource.config;
import static java.util.stream.Collectors.toList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.logging.Level;
import java.util.logging.LogRecord;

import org.eclipse.microprofile.config.Config;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.smallrye.testing.logging.LogCapture;

public class LoggingConfigSourceInterceptorTest {
@RegisterExtension
static LogCapture logCapture = LogCapture.with(logRecord -> logRecord.getMessage().startsWith("SRCFG"), Level.ALL);

@BeforeEach
void setUp() {
logCapture.records().clear();
}

@Test
public void interceptor() throws Exception {
Config config = buildConfig();
Config config = new SmallRyeConfigBuilder()
.addDefaultSources()
.addDefaultInterceptors()
.withSources(new ConfigValuePropertiesConfigSource(
LoggingConfigSourceInterceptorTest.class.getResource("/config-values.properties")))
.withInterceptors(new LoggingConfigSourceInterceptor())
.withSecretKeys("secret")
.build();

assertEquals("abc", config.getValue("my.prop", String.class));
// No log is done here to not expose any sensitive information
assertThrows(SecurityException.class, () -> config.getValue("secret", String.class));
assertThrows(NoSuchElementException.class, () -> config.getValue("not.found", String.class));

// This should not log the secret value:
assertEquals("12345678", SecretKeys.doUnlocked(() -> config.getValue("secret", String.class)));

// First 2 elements are the profile lookups
List<String> logs = logCapture.records().stream().map(LogRecord::getMessage).collect(toList());
// my.prop lookup
assertTrue(logs.get(2).startsWith("SRCFG01001"));
assertTrue(logs.get(2).contains("The config my.prop was loaded from ConfigValuePropertiesConfigSource"));
assertTrue(logs.get(2).contains(":1 with the value abc"));
// not.found lookup
assertEquals("SRCFG01002: The config not.found was not found", logs.get(3));
// secret lookup, shows the key but hides the source and value
assertEquals("SRCFG01001: The config secret was loaded from secret with the value secret", logs.get(4));
}

private static Config buildConfig() throws Exception {
return new SmallRyeConfigBuilder()
.addDefaultSources()
@Test
void expansion() {
final SmallRyeConfig config = new SmallRyeConfigBuilder()
.addDefaultInterceptors()
.withSources(new ConfigValuePropertiesConfigSource(
LoggingConfigSourceInterceptorTest.class.getResource("/config-values.properties")))
.withInterceptors(new LoggingConfigSourceInterceptor())
.withSecretKeys("secret")
.withSources(config("my.prop.expand", "${expand}", "expand", "1234"))
.build();

assertEquals("1234", config.getRawValue("my.prop.expand"));
// First 2 elements are the profile lookups
List<String> logs = logCapture.records().stream().map(LogRecord::getMessage).collect(toList());
assertEquals("SRCFG01001: The config my.prop.expand was loaded from KeyValuesConfigSource with the value ${expand}",
logs.get(2));
assertEquals("SRCFG01001: The config expand was loaded from KeyValuesConfigSource with the value 1234", logs.get(3));
}
}
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<version.smallrye.common>1.4.0</version.smallrye.common>

<version.weld.junit>2.0.1.Final</version.weld.junit>
<version.smallrye.testing.utilities>0.1.0</version.smallrye.testing.utilities>

<!-- Sonar settings -->
<sonar.projectName>SmallRye Config</sonar.projectName>
Expand Down Expand Up @@ -115,6 +116,12 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.smallrye.testing</groupId>
<artifactId>smallrye-testing-utilities</artifactId>
<version>${version.smallrye.testing.utilities}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.ow2.asm</groupId>
Expand Down