-
Notifications
You must be signed in to change notification settings - Fork 121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Secret type to represent secrets and integrate with the secrets handling #1232
Draft
radcortez
wants to merge
1
commit into
smallrye:main
Choose a base branch
from
radcortez:secrets
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+180
−8
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Creating a new
PropertyTime
every timegetValue
is called may impact performance, perhaps consider iterating theSet
and comparing the name instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was to move forward with the implementation. I haven't profiled the code yet, but I am not too worried about it because when we are populating the config, the secrets are unlocked, but I'll have a look. Thanks!