forked from smallrye/smallrye-config
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes smallrye#269. Add LoggingInterceptor so we can see how to imple…
…ment it without exposing secret keys.
- Loading branch information
Showing
9 changed files
with
86 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
implementation/src/main/java/io/smallrye/config/LoggingConfigSourceInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.smallrye.config; | ||
|
||
import org.jboss.logging.Logger; | ||
|
||
public class LoggingConfigSourceInterceptor implements ConfigSourceInterceptor { | ||
private static final Logger LOG = Logger.getLogger("io.smallrye.config"); | ||
|
||
@Override | ||
public ConfigValue getValue(final ConfigSourceInterceptorContext context, final String name) { | ||
final ConfigValue configValue = context.proceed(name); | ||
if (context.getSecretKeys().isSecret(name)) { | ||
return configValue; | ||
} | ||
|
||
if (configValue != null) { | ||
final String value = configValue.getValue(); | ||
final String configLocation = configValue.getConfigSourceName() + ":" + configValue.getLineNumber(); | ||
|
||
LOG.infov("The config {0} was loaded from {1} with the value {2}", name, configLocation, value); | ||
} else { | ||
LOG.infov("The config {0} was not found", name); | ||
} | ||
|
||
return configValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 0 additions & 41 deletions
41
implementation/src/test/java/io/smallrye/config/ConfigSourceLoggingInterceptorTest.java
This file was deleted.
Oops, something went wrong.
33 changes: 33 additions & 0 deletions
33
implementation/src/test/java/io/smallrye/config/LoggingConfigSourceInterceptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.smallrye.config; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertThrows; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
import org.eclipse.microprofile.config.Config; | ||
import org.junit.Test; | ||
|
||
public class LoggingConfigSourceInterceptorTest { | ||
@Test | ||
public void interceptor() throws Exception { | ||
SmallRyeConfig config = (SmallRyeConfig) buildConfig(); | ||
|
||
assertEquals("abc", config.getValue("my.prop", String.class)); | ||
assertThrows(SecurityException.class, () -> config.getValue("secret", String.class)); | ||
assertThrows(NoSuchElementException.class, () -> config.getValue("not.found", String.class)); | ||
|
||
// This would log the secret: | ||
config.getSecretKeys().accessSecret(() -> config.getRawValue("secret")); | ||
} | ||
|
||
private static Config buildConfig() throws Exception { | ||
return new SmallRyeConfigBuilder() | ||
.addDefaultSources() | ||
.withSources(new ConfigValuePropertiesConfigSource( | ||
LoggingConfigSourceInterceptorTest.class.getResource("/config-values.properties"))) | ||
.withInterceptors(new LoggingConfigSourceInterceptor()) | ||
.withSecretKeys("secret") | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ my.prop=abc | |
|
||
|
||
|
||
|
||
secret=secret | ||
|
||
|
||
|
||
|