-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #269. Initial work to support secrets.
- Loading branch information
Showing
8 changed files
with
284 additions
and
4 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
51 changes: 51 additions & 0 deletions
51
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,51 @@ | ||
package io.smallrye.config; | ||
|
||
import java.io.Serializable; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
public class SecretKeys implements Serializable { | ||
private static final ThreadLocal<Boolean> LOCKED = ThreadLocal.withInitial(() -> Boolean.TRUE); | ||
|
||
private final Function<String, Boolean> secrets; | ||
|
||
public SecretKeys() { | ||
this.secrets = (Function<String, Boolean> & Serializable) s -> false; | ||
} | ||
|
||
public SecretKeys(final Function<String, Boolean> secrets) { | ||
this.secrets = secrets != null ? (Function<String, Boolean> & Serializable) secrets | ||
: (Function<String, Boolean> & Serializable) s -> false; | ||
} | ||
|
||
public SecretKeys(final Set<String> secrets) { | ||
this.secrets = (Function<String, Boolean> & Serializable) secrets::contains; | ||
} | ||
|
||
public boolean isSecret(String propertyName) { | ||
return secrets.apply(propertyName); | ||
} | ||
|
||
public boolean isSecretAccessible(String propertyName) { | ||
return !isSecret(propertyName) || !LOCKED.get(); | ||
} | ||
|
||
public void accessSecrets(Runnable runnable) { | ||
LOCKED.set(false); | ||
try { | ||
runnable.run(); | ||
} finally { | ||
LOCKED.set(true); | ||
} | ||
} | ||
|
||
public String accessSecret(Supplier<String> supplier) { | ||
LOCKED.set(false); | ||
try { | ||
return supplier.get(); | ||
} finally { | ||
LOCKED.set(true); | ||
} | ||
} | ||
} |
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
58 changes: 58 additions & 0 deletions
58
implementation/src/test/java/io/smallrye/config/SecretKeysTest.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,58 @@ | ||
package io.smallrye.config; | ||
|
||
import static java.util.stream.StreamSupport.stream; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotEquals; | ||
import static org.junit.Assert.assertThrows; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.eclipse.microprofile.config.Config; | ||
import org.junit.Test; | ||
|
||
public class SecretKeysTest { | ||
@Test | ||
public void locked() { | ||
final Config config = buildConfig("secret", "12345678", "not.secret", "value"); | ||
|
||
assertThrows("Not allowed to access secret key secret", SecurityException.class, | ||
() -> config.getValue("secret", String.class)); | ||
assertEquals("value", config.getValue("not.secret", String.class)); | ||
} | ||
|
||
@Test | ||
public void unlock() { | ||
final Config config = buildConfig("secret", "12345678", "not.secret", "value"); | ||
final SmallRyeConfig smallRyeConfig = (SmallRyeConfig) config; | ||
|
||
smallRyeConfig.getSecretKeys().accessSecrets( | ||
() -> assertEquals("12345678", config.getValue("secret", String.class))); | ||
|
||
assertThrows("Not allowed to access secret key secret", SecurityException.class, | ||
() -> config.getValue("secret", String.class)); | ||
} | ||
|
||
@Test | ||
public void filterSecretsPropertyNames() { | ||
final Config config = buildConfig("secret", "12345678", "not.secret", "value"); | ||
config.getPropertyNames().forEach(s -> assertNotEquals("secret", s)); | ||
} | ||
|
||
@Test | ||
public void allPropertyNames() { | ||
final Config config = buildConfig("secret", "12345678", "not.secret", "value"); | ||
final SmallRyeConfig smallRyeConfig = (SmallRyeConfig) config; | ||
|
||
smallRyeConfig.getSecretKeys().accessSecrets( | ||
() -> assertTrue(stream(smallRyeConfig.getPropertyNames().spliterator(), false) | ||
.anyMatch(s -> s.equals("secret")))); | ||
} | ||
|
||
private static Config buildConfig(String... keyValues) { | ||
return new SmallRyeConfigBuilder() | ||
.addDefaultSources() | ||
.addDefaultInterceptors() | ||
.withSources(KeyValuesConfigSource.config(keyValues)) | ||
.withSecretKeys("secret") | ||
.build(); | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
implementation/src/test/java/io/smallrye/config/inject/ConfigInjectionTest.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,99 @@ | ||
package io.smallrye.config.inject; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
import java.net.URLConnection; | ||
import java.net.URLStreamHandler; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
|
||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
import org.jboss.weld.junit4.WeldInitiator; | ||
import org.junit.After; | ||
import org.junit.BeforeClass; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
public class ConfigInjectionTest { | ||
@BeforeClass | ||
public static void beforeClass() throws Exception { | ||
final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); | ||
final URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { | ||
new URL("memory", null, 0, "/", | ||
new InMemoryStreamHandler("io.smallrye.config.inject.ConfigInjectionTestConfigFactory")) | ||
}, contextClassLoader); | ||
Thread.currentThread().setContextClassLoader(urlClassLoader); | ||
} | ||
|
||
@After | ||
public void afterClass() { | ||
final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); | ||
Thread.currentThread().setContextClassLoader(contextClassLoader.getParent()); | ||
} | ||
|
||
@Rule | ||
public WeldInitiator weld = WeldInitiator.from(ConfigProducer.class, ConfigBean.class) | ||
.addBeans() | ||
.activate(ApplicationScoped.class) | ||
.inject(this) | ||
.build(); | ||
|
||
@Inject | ||
private ConfigBean configBean; | ||
|
||
@Test | ||
public void inject() { | ||
assertEquals("value", configBean.getConfig()); | ||
assertEquals("12345678", configBean.getSecret()); | ||
} | ||
|
||
@ApplicationScoped | ||
public static class ConfigBean { | ||
@Inject | ||
@ConfigProperty(name = "config") | ||
private String config; | ||
@Inject | ||
@ConfigProperty(name = "secret") | ||
private String secret; | ||
|
||
public String getConfig() { | ||
return config; | ||
} | ||
|
||
public String getSecret() { | ||
return secret; | ||
} | ||
} | ||
|
||
public static class InMemoryStreamHandler extends URLStreamHandler { | ||
final byte[] contents; | ||
|
||
public InMemoryStreamHandler(final String contents) { | ||
this.contents = contents.getBytes(); | ||
} | ||
|
||
@Override | ||
protected URLConnection openConnection(final URL u) throws IOException { | ||
if (!u.getFile().endsWith("SmallRyeConfigFactory")) { | ||
return null; | ||
} | ||
|
||
return new URLConnection(u) { | ||
@Override | ||
public void connect() throws IOException { | ||
} | ||
|
||
@Override | ||
public InputStream getInputStream() throws IOException { | ||
return new ByteArrayInputStream(contents); | ||
} | ||
}; | ||
} | ||
} | ||
} |
Oops, something went wrong.