-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Secret type to represent secrets and integrate with the secrets handling
- Loading branch information
Showing
9 changed files
with
180 additions
and
8 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
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
26 changes: 26 additions & 0 deletions
26
implementation/src/main/java/io/smallrye/config/Secret.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.eclipse.microprofile.config.spi.Converter; | ||
|
||
public interface Secret<T> { | ||
T get(); | ||
|
||
class SecretConverter<T> implements Converter<Secret<T>> { | ||
private static final long serialVersionUID = -4624156385855243648L; | ||
private final Converter<T> delegate; | ||
|
||
public SecretConverter(final Converter<T> delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public Secret<T> convert(final String value) throws IllegalArgumentException, NullPointerException { | ||
return new Secret<T>() { | ||
@Override | ||
public T get() { | ||
return delegate.convert(value); | ||
} | ||
}; | ||
} | ||
} | ||
} |
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
63 changes: 63 additions & 0 deletions
63
implementation/src/test/java/io/smallrye/config/ConfigMappingSecretsTest.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,63 @@ | ||
package io.smallrye.config; | ||
|
||
import static io.smallrye.config.Converters.STRING_CONVERTER; | ||
import static io.smallrye.config.KeyValuesConfigSource.config; | ||
import static java.util.stream.Collectors.toSet; | ||
import static java.util.stream.StreamSupport.stream; | ||
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; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class ConfigMappingSecretsTest { | ||
@Test | ||
void secrets() throws Exception { | ||
SmallRyeConfig config = new SmallRyeConfigBuilder() | ||
.addDefaultInterceptors() | ||
.withConverter(Secret.class, 100, new Secret.SecretConverter(STRING_CONVERTER)) | ||
.withMapping(MappingSecrets.class) | ||
.withSources(config( | ||
"secrets.secret", "secret", | ||
"secrets.list[0]", "secret", | ||
"secrets.map.key", "secret", | ||
"secrets.optional", "secret")) | ||
.withSecretKeys() | ||
.build(); | ||
|
||
MappingSecrets mapping = config.getConfigMapping(MappingSecrets.class); | ||
assertEquals("secret", mapping.secret().get()); | ||
assertEquals("secret", mapping.list().get(0).get()); | ||
assertEquals("secret", mapping.map().get("key").get()); | ||
assertTrue(mapping.optional().isPresent()); | ||
assertEquals("secret", mapping.optional().get().get()); | ||
|
||
assertThrows(SecurityException.class, () -> config.getRawValue("secrets.secret")); | ||
assertThrows(SecurityException.class, () -> config.getRawValue("secrets.list[0]")); | ||
assertThrows(SecurityException.class, () -> config.getRawValue("secrets.map.key")); | ||
assertThrows(SecurityException.class, () -> config.getRawValue("secrets.optional")); | ||
|
||
Set<String> properties = stream(config.getPropertyNames().spliterator(), false).collect(toSet()); | ||
assertFalse(properties.contains("secrets.secret")); | ||
assertFalse(properties.contains("secrets.secrets[0]")); | ||
assertFalse(properties.contains("secrets.secret-map.key")); | ||
assertFalse(properties.contains("secrets.optional")); | ||
} | ||
|
||
@ConfigMapping(prefix = "secrets") | ||
interface MappingSecrets { | ||
Secret<String> secret(); | ||
|
||
List<Secret<String>> list(); | ||
|
||
Map<String, Secret<String>> map(); | ||
|
||
Optional<Secret<String>> optional(); | ||
} | ||
} |