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

Provide a way to disable the SecretKeysHandler #937

Merged
merged 1 commit into from
May 17, 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 @@ -7,9 +7,11 @@
public class SecretKeysHandlerConfigSourceInterceptor implements ConfigSourceInterceptor {
private static final long serialVersionUID = -5228028387733656005L;

private final boolean enabled;
private final Map<String, SecretKeysHandler> handlers = new HashMap<>();

public SecretKeysHandlerConfigSourceInterceptor(final List<SecretKeysHandler> handlers) {
public SecretKeysHandlerConfigSourceInterceptor(final boolean enabled, final List<SecretKeysHandler> handlers) {
this.enabled = enabled;
for (SecretKeysHandler handler : handlers) {
this.handlers.put(handler.getName(), handler);
}
Expand All @@ -18,7 +20,7 @@ public SecretKeysHandlerConfigSourceInterceptor(final List<SecretKeysHandler> ha
@Override
public ConfigValue getValue(final ConfigSourceInterceptorContext context, final String name) {
ConfigValue configValue = context.proceed(name);
if (configValue != null && configValue.getValue() != null) {
if (enabled && configValue != null && configValue.getValue() != null) {
String handler = configValue.getExtendedExpressionHandler();
if (handler != null) {
return configValue.withValue(getSecretValue(handler, configValue.getValue()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,8 @@ public ConfigSourceInterceptor getInterceptor(final ConfigSourceInterceptorConte
if (isAddDiscoveredSecretKeysHandlers()) {
secretKeysHandlers.addAll(discoverSecretKeysHandlers(context));
}
return new SecretKeysHandlerConfigSourceInterceptor(secretKeysHandlers);
return new SecretKeysHandlerConfigSourceInterceptor(
isAddDiscoveredSecretKeysHandlers() || !secretKeysHandlers.isEmpty(), secretKeysHandlers);
}

@Override
Expand Down Expand Up @@ -611,6 +612,11 @@ public SmallRyeConfigBuilder setAddDiscoveredInterceptors(final boolean addDisco
return this;
}

public SmallRyeConfigBuilder setAddDiscoveredSecretKeysHandlers(final boolean addDiscoveredSecretKeysHandlers) {
this.addDiscoveredSecretKeysHandlers = addDiscoveredSecretKeysHandlers;
return this;
}

public SmallRyeConfigBuilder setAddDiscoveredValidator(final boolean addDiscoveredValidator) {
this.addDiscoveredValidator = addDiscoveredValidator;
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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 All @@ -12,6 +13,7 @@ class SecretKeysHandlerTest {
void notFound() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.addDefaultInterceptors()
.addDiscoveredSecretKeysHandlers()
.withDefaultValue("my.secret", "${missing::secret}")
.build();

Expand All @@ -38,4 +40,15 @@ public String getName() {

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

@Test
void disabled() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.addDefaultInterceptors()
.setAddDiscoveredSecretKeysHandlers(false)
.withDefaultValue("my.secret", "${handler::secret}")
.build();

assertNotNull(config.getRawValue("my.secret"));
}
}