Skip to content

Commit

Permalink
Changed priority of the LoggingInterceptor to avoid logging profiled …
Browse files Browse the repository at this point in the history
…lookups (#968)
  • Loading branch information
radcortez authored Aug 3, 2023
1 parent cf99fcf commit 5547830
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import jakarta.annotation.Priority;

@Priority(Priorities.LIBRARY + 150)
@Priority(Priorities.LIBRARY + 250)
public class LoggingConfigSourceInterceptor implements ConfigSourceInterceptor {
private static final long serialVersionUID = 367246512037404779L;

Expand All @@ -13,21 +13,23 @@ public ConfigValue getValue(final ConfigSourceInterceptorContext context, final
try {
// Unlocked keys will run here.
ConfigValue configValue = doLocked(() -> context.proceed(name));
if (configValue != null)
if (configValue != null) {
ConfigLogging.log.lookup(configValue.getName(), configValue.getLocation(), configValue.getValue());
else
} else {
ConfigLogging.log.notFound(name);
}
return configValue;
} catch (SecurityException e) {
// Handled next, to omit the values to log from the secret.
}

// Locked keys here.
final ConfigValue secret = context.proceed(name);
if (secret != null)
if (secret != null) {
ConfigLogging.log.lookup(secret.getName(), "secret", "secret");
else
} else {
ConfigLogging.log.notFound(name);
}
return secret;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;

import jakarta.annotation.Priority;
Expand All @@ -36,13 +35,9 @@ public ConfigValue getValue(final ConfigSourceInterceptorContext context, final
final String normalizeName = normalizeName(name);
final ConfigValue profileValue = getProfileValue(context, normalizeName);
if (profileValue != null) {
try {
final ConfigValue originalValue = context.proceed(normalizeName);
if (originalValue != null && CONFIG_SOURCE_COMPARATOR.compare(originalValue, profileValue) > 0) {
return originalValue;
}
} catch (final NoSuchElementException e) {
// We couldn't find the main property so we fallback to the profile property because it exists.
final ConfigValue originalValue = context.proceed(normalizeName);
if (originalValue != null && CONFIG_SOURCE_COMPARATOR.compare(originalValue, profileValue) > 0) {
return originalValue;
}
return profileValue.withName(normalizeName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -46,37 +47,47 @@ void interceptor() throws Exception {
// This should not log the secret value:
assertEquals("12345678", SecretKeys.doUnlocked(() -> config.getValue("secret", String.class)));

// 1st element is the SR profile parent lookup
// 2nd element is the MP profile lookup
// 3rd element is the SR profile lookup
// 4th element is the MP Property Expressions
List<String> logs = logCapture.records().stream().map(LogRecord::getMessage).collect(toList());
for (String log : logs) {
System.out.println(log);
}
// my.prop lookup
assertTrue(logs.get(4).startsWith("SRCFG01001"));
assertTrue(logs.get(4).contains("The config my.prop was loaded from ConfigValuePropertiesConfigSource"));
assertTrue(logs.get(4).contains(":1 with the value abc"));
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
assertEquals("SRCFG01002: The config not.found was not found", logs.get(5));
assertTrue(logs.contains("SRCFG01002: The config not.found was not found"));
// 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(6));
assertTrue(logs.contains("SRCFG01001: The config secret was loaded from secret with the value secret"));
}

@Test
void expansion() {
final SmallRyeConfig config = new SmallRyeConfigBuilder()
SmallRyeConfig config = new SmallRyeConfigBuilder()
.addDefaultInterceptors()
.withInterceptors(new LoggingConfigSourceInterceptor())
.withSources(config("my.prop.expand", "${expand}", "expand", "1234"))
.build();

assertEquals("1234", config.getRawValue("my.prop.expand"));
// 1st element is the SR profile parent lookup
// 2nd element is the MP profile lookup
// 3rd element is the SR profile lookup
// 4th element is the MP Property Expressions
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(4));
assertEquals("SRCFG01001: The config expand was loaded from KeyValuesConfigSource with the value 1234", logs.get(5));
assertTrue(logs.contains(
"SRCFG01001: The config my.prop.expand was loaded from KeyValuesConfigSource with the value ${expand}"));
assertTrue(logs.contains("SRCFG01001: The config expand was loaded from KeyValuesConfigSource with the value 1234"));
}

@Test
void profiles() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.addDefaultInterceptors()
.withInterceptors(new LoggingConfigSourceInterceptor())
.withSources(config("%prod.my.prop", "1234", "my.prop", "5678"))
.withProfile("prod")
.build();

assertEquals("1234", config.getRawValue("my.prop"));
List<String> logs = logCapture.records().stream().map(LogRecord::getMessage).collect(toList());
assertTrue(logs.contains("SRCFG01001: The config my.prop was loaded from KeyValuesConfigSource with the value 1234"));
assertFalse(logs.stream().anyMatch(log -> log.contains("%prod.my.prop")));
}
}

0 comments on commit 5547830

Please sign in to comment.