diff --git a/components-starter/camel-hashicorp-vault-starter/src/main/java/org/apache/camel/component/hashicorp/vault/springboot/SpringBootHashicorpVaultPropertiesParser.java b/components-starter/camel-hashicorp-vault-starter/src/main/java/org/apache/camel/component/hashicorp/vault/springboot/SpringBootHashicorpVaultPropertiesParser.java index 1a8b75be651..c2b995b0411 100644 --- a/components-starter/camel-hashicorp-vault-starter/src/main/java/org/apache/camel/component/hashicorp/vault/springboot/SpringBootHashicorpVaultPropertiesParser.java +++ b/components-starter/camel-hashicorp-vault-starter/src/main/java/org/apache/camel/component/hashicorp/vault/springboot/SpringBootHashicorpVaultPropertiesParser.java @@ -22,7 +22,6 @@ import org.apache.camel.component.hashicorp.vault.HashicorpVaultPropertiesFunction; import org.apache.camel.spi.PropertiesFunction; import org.apache.camel.spring.boot.AbstractEarlyResolutionPropertiesParser; -import org.apache.camel.support.service.ServiceHelper; import org.apache.camel.util.ObjectHelper; import org.apache.camel.vault.HashicorpVaultConfiguration; import org.springframework.boot.context.properties.bind.Binder; @@ -33,7 +32,8 @@ * Resolves the {@code {{hashicorp:...}}} placeholders of the Spring Boot properties before the CamelContext exists. It * reads the secrets as the properties function of the CamelContext does, with the same settings: the * {@code CAMEL_HASHICORP_VAULT_*} environment variables when one of them is set, else the {@code camel.vault.hashicorp} - * options. It logs in once, and revokes the token of its login once the properties are resolved. + * options. It logs in once, and revokes the token of its login once the properties are resolved: the base class stops + * the function, which closes its client. */ public class SpringBootHashicorpVaultPropertiesParser extends AbstractEarlyResolutionPropertiesParser { @@ -80,13 +80,6 @@ protected PropertiesFunction createPropertiesFunction(ConfigurableEnvironment en } } - @Override - protected void release(PropertiesFunction propertiesFunction) { - // stopping the function closes its client: it stops renewing the token, and revokes the token of the login (but - // not a token given for the token login) - ServiceHelper.stopService(propertiesFunction); - } - private static void required(String value, String option, String message) { if (ObjectHelper.isEmpty(value)) { throw new RuntimeCamelException( diff --git a/components-starter/camel-hashicorp-vault-starter/src/test/java/org/apache/camel/component/hashicorp/vault/springboot/SpringBootHashicorpVaultPropertiesParserTest.java b/components-starter/camel-hashicorp-vault-starter/src/test/java/org/apache/camel/component/hashicorp/vault/springboot/SpringBootHashicorpVaultPropertiesParserTest.java index f11cf4fbac9..013557bf926 100644 --- a/components-starter/camel-hashicorp-vault-starter/src/test/java/org/apache/camel/component/hashicorp/vault/springboot/SpringBootHashicorpVaultPropertiesParserTest.java +++ b/components-starter/camel-hashicorp-vault-starter/src/test/java/org/apache/camel/component/hashicorp/vault/springboot/SpringBootHashicorpVaultPropertiesParserTest.java @@ -18,6 +18,7 @@ import org.apache.camel.RuntimeCamelException; import org.apache.camel.spi.PropertiesFunction; +import org.apache.camel.support.service.ServiceHelper; import org.junit.jupiter.api.Test; import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.StandardEnvironment; @@ -156,8 +157,9 @@ void anAppRoleLoginNeedsNoToken() { try { assertEquals("hashicorp", function.getName()); } finally { - // the client logs in at its first request, so there is no token to revoke - parser.release(function); + // stopping the function closes its client, which logs in at its first request: so there is no token to + // revoke + ServiceHelper.stopService(function); } } diff --git a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/AbstractEarlyResolutionPropertiesParser.java b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/AbstractEarlyResolutionPropertiesParser.java index a7fc432d17e..c43899746f6 100644 --- a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/AbstractEarlyResolutionPropertiesParser.java +++ b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/AbstractEarlyResolutionPropertiesParser.java @@ -21,6 +21,7 @@ import java.util.Properties; import org.apache.camel.RuntimeCamelException; import org.apache.camel.spi.PropertiesFunction; +import org.apache.camel.support.service.ServiceHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; @@ -76,10 +77,15 @@ public abstract class AbstractEarlyResolutionPropertiesParser protected abstract PropertiesFunction createPropertiesFunction(ConfigurableEnvironment environment); /** - * Releases what the properties function holds, such as the login of its client, once the properties are resolved, - * also when they could not be. A failure to release is logged and ignored. Does nothing by default. + * Releases what the properties function holds, such as its client, once the properties are resolved, also when + * they could not be. A failure to release is logged and ignored. + *

+ * By default, stops the function when it is a Camel {@code Service}: the properties functions of the Camel vaults + * close their client when they stop, also when they were never started. A parser whose function is shared with + * something else can override this to do nothing. */ protected void release(PropertiesFunction propertiesFunction) { + ServiceHelper.stopService(propertiesFunction); } /** diff --git a/core/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/AbstractEarlyResolutionPropertiesParserTest.java b/core/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/AbstractEarlyResolutionPropertiesParserTest.java index f01ffd867ac..d32c04c8a2c 100644 --- a/core/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/AbstractEarlyResolutionPropertiesParserTest.java +++ b/core/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/AbstractEarlyResolutionPropertiesParserTest.java @@ -23,6 +23,7 @@ import java.util.Set; import org.apache.camel.RuntimeCamelException; import org.apache.camel.spi.PropertiesFunction; +import org.apache.camel.support.service.ServiceSupport; import org.junit.jupiter.api.Test; import org.springframework.boot.SpringApplication; import org.springframework.boot.bootstrap.DefaultBootstrapContext; @@ -88,6 +89,63 @@ public String apply(String remainder) { } } + /** + * A properties function that is a Camel service, as the functions of the Camel vaults are, and counts how often it + * was stopped. + */ + private static final class StubServiceFunction extends ServiceSupport implements PropertiesFunction { + private final Map values; + private int stops; + + StubServiceFunction(Map values) { + this.values = values; + } + + @Override + public String getName() { + return "test"; + } + + @Override + public String apply(String remainder) { + if (!values.containsKey(remainder)) { + throw new IllegalStateException("cannot resolve " + remainder); + } + return values.get(remainder); + } + + @Override + protected void doStop() { + stops++; + } + } + + /** + * A parser that keeps the default release of the properties function. + */ + private static final class DefaultReleaseParser extends AbstractEarlyResolutionPropertiesParser { + private final PropertiesFunction function; + + DefaultReleaseParser(PropertiesFunction function) { + this.function = function; + } + + @Override + protected String getEarlyResolutionProperty() { + return GUARD; + } + + @Override + protected String getOverridePropertySourceName() { + return OVERRIDE_SOURCE; + } + + @Override + protected PropertiesFunction createPropertiesFunction(ConfigurableEnvironment environment) { + return function; + } + } + private static final class TestParser extends AbstractEarlyResolutionPropertiesParser { private final StubPropertiesFunction function; private int factoryCalls; @@ -202,6 +260,39 @@ void aFailingReleaseDoesNotAbortStartup() { assertEquals(1, parser.releaseCalls); } + @Test + void stopsAPropertiesFunctionThatIsAServiceByDefault() { + RecordingEnvironment environment = environmentWith(Map.of(GUARD, "true", "my.secret", "{{test:db}}")); + StubServiceFunction function = new StubServiceFunction(Map.of("db", "pazzword")); + + new DefaultReleaseParser(function).onApplicationEvent(eventFor(environment)); + + assertEquals("pazzword", environment.getProperty("my.secret")); + assertEquals(1, function.stops, "stopping the function closes what it holds, such as the client of a vault"); + } + + @Test + void stopsAPropertiesFunctionThatIsAServiceByDefaultAlsoWhenResolutionFails() { + RecordingEnvironment environment = environmentWith(Map.of(GUARD, "true", "my.secret", "{{test:missing}}")); + StubServiceFunction function = new StubServiceFunction(Map.of()); + DefaultReleaseParser parser = new DefaultReleaseParser(function); + + assertThrows(RuntimeCamelException.class, () -> parser.onApplicationEvent(eventFor(environment))); + + assertEquals(1, function.stops, "the function is stopped also when startup is aborted"); + } + + @Test + void releasesAPropertiesFunctionThatIsNotAServiceByDefault() { + RecordingEnvironment environment = environmentWith(Map.of(GUARD, "true", "my.secret", "{{test:db}}")); + DefaultReleaseParser parser + = new DefaultReleaseParser(new StubPropertiesFunction(Map.of("db", "pazzword"), Set.of())); + + parser.onApplicationEvent(eventFor(environment)); + + assertEquals("pazzword", environment.getProperty("my.secret")); + } + @Test public void guardEnabledBuildsTheClientAndAddsTheOverrideSource() { RecordingEnvironment environment = environmentWith(Map.of(GUARD, "true"));