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
16 changed files
with
466 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
* xref:index.adoc[Index] | ||
* xref:config/config.adoc[Config] | ||
* xref:interceptors/interceptors.adoc[Interceptors] | ||
* Extensions | ||
** xref:config-sources/config-sources.adoc[Config Sources] | ||
** xref:converters/converters.adoc[Converters] | ||
** xref:interceptors/interceptors.adoc[Interceptors] | ||
** xref:cdi/cdi.adoc[CDI] |
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,10 @@ | ||
:doctype: book | ||
include::../attributes.adoc[] | ||
|
||
[[cdi-extensions]] | ||
|
||
= Config | ||
|
||
* <<secret-keys>> | ||
|
||
include::secret-keys.adoc[] |
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,43 @@ | ||
[[secret-keys]] | ||
== Secret Keys | ||
|
||
When configuration properties contain passwords or other kinds of secrets, Smallrye Config can hide them to | ||
prevent accidental exposure of such values. | ||
|
||
**This is no way a replacement for securing secrets. ** Proper security mechanisms must still be used to secure | ||
secrets. However, there is still the basic problem that passwords and secrets are generally encoded simply as | ||
strings. | ||
|
||
Secret Keys provides a way to "lock" the configuration so that secrets do not appear unless explicitly enabled. | ||
|
||
=== Configuration | ||
|
||
Secret Keys requires the list of Config property names that must be hidden. This can be supplied in | ||
`SmallRyeConfigBuilder.withSecretKeys` and initialized with `SmallRyeConfigFactory`. From this point forward, any | ||
config name retrieved from the `Config` instance that matches the Secret Keys will throw a `SecurityException`. | ||
|
||
=== Unlock Keys | ||
|
||
Access to the Secret Keys, is available via the APIs `SmallRyeConfig.unlockSecrets(java.lang.Runnable)` and | ||
`SmallRyeConfig.unlockSecrets(java.util.function.Supplier<T>)`. | ||
|
||
[source,java] | ||
---- | ||
Config config ... | ||
SmallRyeConfig smallRyeConfig = (SmallRyeConfig) config; | ||
String value = config.getValue("secret"); // fails with SecurityException | ||
String secretValue = smallRyeConfig.unlockSecrets(() -> config.getValue("secret", String.class)); | ||
---- | ||
|
||
Secret Keyes are only unlocked in the context of `unlockSecrets`. Once the execution completes, the secrets become | ||
locked again. | ||
|
||
Alternative, the `SecretKeys` API can also be used: | ||
|
||
[source,java] | ||
---- | ||
String secretValue = SecretKeys.doUnlocked(() -> { | ||
config.getValue("secret", String.class); | ||
}); | ||
---- |
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
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
Oops, something went wrong.