Skip to content
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

Register SecretKeysHandlerFactory with SmallRyeConfigBuilder #1009

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
Expand Down Expand Up @@ -65,7 +64,7 @@ public class SmallRyeConfigBuilder implements ConfigBuilder {
private final List<String> profiles = new ArrayList<>();
private final Set<String> secretKeys = new HashSet<>();
private final List<InterceptorWithPriority> interceptors = new ArrayList<>();
private final List<SecretKeysHandler> secretKeysHandlers = new ArrayList<>();
private final List<SecretKeysHandlerWithName> secretKeysHandlers = new ArrayList<>();
private ConfigValidator validator = ConfigValidator.EMPTY;
private final Map<String, String> defaultValues = new HashMap<>();
private final ConfigMappingProvider.Builder mappingsBuilder = ConfigMappingProvider.builder();
Expand Down Expand Up @@ -328,11 +327,30 @@ public OptionalInt getPriority() {
interceptors.add(new InterceptorWithPriority(new ConfigSourceInterceptorFactory() {
@Override
public ConfigSourceInterceptor getInterceptor(final ConfigSourceInterceptorContext context) {
List<SecretKeysHandler> secretKeysHandlers = new ArrayList<>();
for (SecretKeysHandlerWithName secretKeysHandler : SmallRyeConfigBuilder.this.secretKeysHandlers) {
secretKeysHandlers.add(secretKeysHandler.getSecretKeysHandler(new ConfigSourceContext() {
@Override
public ConfigValue getValue(final String name) {
return context.proceed(name);
}

@Override
public List<String> getProfiles() {
throw new UnsupportedOperationException();
}

@Override
public Iterator<String> iterateNames() {
return context.iterateNames();
}
}));
}

if (isAddDiscoveredSecretKeysHandlers()) {
secretKeysHandlers.addAll(discoverSecretKeysHandlers(context));
}
return new SecretKeysHandlerConfigSourceInterceptor(
isAddDiscoveredSecretKeysHandlers() || !secretKeysHandlers.isEmpty(), secretKeysHandlers);
return new SecretKeysHandlerConfigSourceInterceptor(true, secretKeysHandlers);
}

@Override
Expand Down Expand Up @@ -459,8 +477,17 @@ public SmallRyeConfigBuilder withInterceptorFactories(ConfigSourceInterceptorFac
return this;
}

public SmallRyeConfigBuilder withSecretKeysHandlers(SecretKeysHandler... secretKeysHandler) {
this.secretKeysHandlers.addAll(Arrays.asList(secretKeysHandler));
public SmallRyeConfigBuilder withSecretKeysHandlers(SecretKeysHandler... secretKeysHandlers) {
for (SecretKeysHandler secretKeysHandler : secretKeysHandlers) {
this.secretKeysHandlers.add(new SecretKeysHandlerWithName(secretKeysHandler));
}
return this;
}

public SmallRyeConfigBuilder withSecretKeyHandlerFactories(SecretKeysHandlerFactory... secretKeyHandlerFactories) {
for (SecretKeysHandlerFactory secretKeyHandlerFactory : secretKeyHandlerFactories) {
this.secretKeysHandlers.add(new SecretKeysHandlerWithName(secretKeyHandlerFactory));
}
return this;
}

Expand Down Expand Up @@ -744,4 +771,34 @@ private static int getPriority(final Class<? extends ConfigSourceInterceptor> kl
}
}
}

static class SecretKeysHandlerWithName {
private final SecretKeysHandlerFactory factory;

SecretKeysHandlerWithName(SecretKeysHandler secretKeysHandler) {
this(new SecretKeysHandlerFactory() {
@Override
public SecretKeysHandler getSecretKeysHandler(final ConfigSourceContext context) {
return secretKeysHandler;
}

@Override
public String getName() {
return secretKeysHandler.getName();
}
});
}

SecretKeysHandlerWithName(SecretKeysHandlerFactory factory) {
this.factory = factory;
}

io.smallrye.config.SecretKeysHandler getSecretKeysHandler(ConfigSourceContext context) {
return factory.getSecretKeysHandler(context);
}

String getName() {
return factory.getName();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.smallrye.config;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.NoSuchElementException;
Expand Down Expand Up @@ -42,13 +41,34 @@ public String getName() {
}

@Test
void disabled() {
void handlerFactory() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withDefaultValue("context.handler", "decoded")
.addDefaultInterceptors()
.setAddDiscoveredSecretKeysHandlers(false)
.withSecretKeyHandlerFactories(new SecretKeysHandlerFactory() {
@Override
public SecretKeysHandler getSecretKeysHandler(final ConfigSourceContext context) {
return new SecretKeysHandler() {
@Override
public String decode(final String secret) {
return context.getValue("context.handler").getValue();
}

@Override
public String getName() {
return "handler";
}
};
}

@Override
public String getName() {
return "handler";
}
})
.withDefaultValue("my.secret", "${handler::secret}")
.build();

assertNotNull(config.getRawValue("my.secret"));
assertEquals("decoded", config.getRawValue("my.secret"));
}
}