From 55ac71ee61d93ca409070949aaeb9ca6d60fd6a6 Mon Sep 17 00:00:00 2001 From: Marco Bungart <32584495+turing85@users.noreply.github.com> Date: Thu, 7 Mar 2024 00:52:11 +0100 Subject: [PATCH] Re-introduce a no-args constructor for LoggingConfigSourceInterceptor so that loading the service as described on https://smallrye.io/smallrye-config/Main/extensions/logging/ is still/again possible (#1127) --- .../LoggingConfigSourceInterceptor.java | 4 +++ .../LoggingConfigSourceInterceptorTest.java | 30 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/implementation/src/main/java/io/smallrye/config/LoggingConfigSourceInterceptor.java b/implementation/src/main/java/io/smallrye/config/LoggingConfigSourceInterceptor.java index fe16de083..5da68e9e9 100644 --- a/implementation/src/main/java/io/smallrye/config/LoggingConfigSourceInterceptor.java +++ b/implementation/src/main/java/io/smallrye/config/LoggingConfigSourceInterceptor.java @@ -12,6 +12,10 @@ public class LoggingConfigSourceInterceptor implements ConfigSourceInterceptor { private final boolean enabled; + public LoggingConfigSourceInterceptor() { + this(true); + } + public LoggingConfigSourceInterceptor(final boolean enabled) { this.enabled = enabled; } diff --git a/implementation/src/test/java/io/smallrye/config/LoggingConfigSourceInterceptorTest.java b/implementation/src/test/java/io/smallrye/config/LoggingConfigSourceInterceptorTest.java index 77d666527..a93e49667 100644 --- a/implementation/src/test/java/io/smallrye/config/LoggingConfigSourceInterceptorTest.java +++ b/implementation/src/test/java/io/smallrye/config/LoggingConfigSourceInterceptorTest.java @@ -73,6 +73,36 @@ void interceptor() throws Exception { assertTrue(logs.contains("SRCFG01001: The config secret was loaded from secret with the value secret")); } + @Test + void explicitInterceptor() throws Exception { + Config config = new SmallRyeConfigBuilder() + .addDefaultSources() + .addDefaultInterceptors() + .withInterceptors(new LoggingConfigSourceInterceptor()) + .withSources(new ConfigValuePropertiesConfigSource( + LoggingConfigSourceInterceptorTest.class.getResource("/config-values.properties"))) + .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))); + + List logs = logCapture.records().stream().map(LogRecord::getMessage).collect(toList()); + // my.prop lookup + assertTrue(logs.stream() + .anyMatch(log -> log.contains("The config my.prop was loaded from ConfigValuePropertiesConfigSource"))); + assertTrue(logs.stream().anyMatch(log -> log.contains(":1 with the value abc"))); + // not.found lookup + assertTrue(logs.contains("SRCFG01002: The config not.found was not found")); + // secret lookup, shows the key but hides the source and value + assertTrue(logs.contains("SRCFG01001: The config secret was loaded from secret with the value secret")); + } + @Test void expansion() { SmallRyeConfig config = new SmallRyeConfigBuilder()