Skip to content

Commit

Permalink
Provide a way to disable the SecretKeysHandler (#937)
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez authored May 17, 2023
1 parent 99beeae commit b62dc77
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
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"));
}
}

0 comments on commit b62dc77

Please sign in to comment.