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. Support secret keys names.
- Loading branch information
Showing
12 changed files
with
394 additions
and
45 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
50 changes: 50 additions & 0 deletions
50
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,50 @@ | ||
package io.smallrye.config; | ||
|
||
import static io.smallrye.config.SecretKeys.doLocked; | ||
|
||
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) { | ||
try { | ||
// Unlocked keys will run here. | ||
ConfigValue configValue = doLocked(() -> context.proceed(name)); | ||
if (configValue != null) | ||
lookup(configValue); | ||
else | ||
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) | ||
lookup(secret.from().withValue("secret").withConfigSourceName("secret").withLineNumber(-1).build()); | ||
else | ||
notFound(name); | ||
return secret; | ||
} | ||
|
||
private void notFound(final String name) { | ||
if (LOG.isDebugEnabled()) { | ||
LOG.debugv("The config {0} was not found", name); | ||
} | ||
} | ||
|
||
private void lookup(final ConfigValue configValue) { | ||
if (LOG.isDebugEnabled()) { | ||
LOG.debugv("The config {0} was loaded from {1} with the value {2}", | ||
configValue.getName(), getLocation(configValue), configValue.getValue()); | ||
} | ||
} | ||
|
||
private String getLocation(final ConfigValue configValue) { | ||
return configValue.getLineNumber() != -1 ? configValue.getConfigSourceName() + ":" + configValue.getLineNumber() | ||
: configValue.getConfigSourceName(); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
implementation/src/main/java/io/smallrye/config/SecretKeys.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,52 @@ | ||
package io.smallrye.config; | ||
|
||
import java.io.Serializable; | ||
import java.util.function.Supplier; | ||
|
||
public class SecretKeys implements Serializable { | ||
private static final ThreadLocal<Boolean> LOCKED = ThreadLocal.withInitial(() -> Boolean.TRUE); | ||
|
||
public static boolean isLocked() { | ||
return LOCKED.get(); | ||
} | ||
|
||
public static void doUnlocked(Runnable runnable) { | ||
doUnlocked(() -> { | ||
runnable.run(); | ||
return null; | ||
}); | ||
} | ||
|
||
public static <T> T doUnlocked(Supplier<T> supplier) { | ||
if (isLocked()) { | ||
LOCKED.set(false); | ||
try { | ||
return supplier.get(); | ||
} finally { | ||
LOCKED.set(true); | ||
} | ||
} else { | ||
return supplier.get(); | ||
} | ||
} | ||
|
||
public static void doLocked(Runnable runnable) { | ||
doLocked(() -> { | ||
runnable.run(); | ||
return null; | ||
}); | ||
} | ||
|
||
public static <T> T doLocked(Supplier<T> supplier) { | ||
if (!isLocked()) { | ||
LOCKED.set(true); | ||
try { | ||
return supplier.get(); | ||
} finally { | ||
LOCKED.set(false); | ||
} | ||
} else { | ||
return supplier.get(); | ||
} | ||
} | ||
} |
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
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 should not log the secret value: | ||
assertEquals("12345678", config.unlockSecrets(() -> 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(); | ||
} | ||
} |
Oops, something went wrong.