This is an automated email from the ASF dual-hosted git repository.
Croway pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-spring-boot.git
The following commit(s) were added to refs/heads/main by this push:
new 21af232026d CAMEL-26919: camel-hashicorp-vault-starter - the early
resolution reads the secrets as the properties function of the CamelContext
(#2091)
21af232026d is described below
commit 21af232026d89c4a2920493fa14ec0fcd1ba4363
Author: Andrea Cosentino <[email protected]>
AuthorDate: Mon Nov 16 18:04:47 2026 +0100
CAMEL-26919: camel-hashicorp-vault-starter - the early resolution reads the
secrets as the properties function of the CamelContext (#2091)
* CAMEL-26919: camel-hashicorp-vault-starter - the early resolution logs in
with the camel.vault.hashicorp options
The early resolution of hashicorp: properties built its own VaultTemplate
with camel.vault.hashicorp.token,
so the logins of CAMEL-25675 (AppRole, Kubernetes, JWT, cert, cloud) and
the TLS options did not apply to it.
- SpringBootHashicorpVaultPropertiesParser binds camel.vault.hashicorp to
HashicorpVaultConfiguration with
the Spring Boot Binder (relaxed names, such as
camel.vault.hashicorp.role-id), and creates the client with
HashicorpVaultClient.create(HashicorpVaultClientSettings.fromConfiguration(...)),
which needs no
CamelContext. The messages for a missing host, port, scheme or token are
unchanged; an incomplete login
fails with a RuntimeCamelException naming the camel.vault.hashicorp
options.
- AbstractEarlyResolutionPropertiesParser calls a new
release(PropertiesFunction) hook once the properties
are resolved, also when they could not be. It does nothing by default;
the hashicorp parser closes its
client there, which stops the token renewal and revokes the token of the
login.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Andrea Cosentino <[email protected]>
* CAMEL-26919: camel-hashicorp-vault-starter - the early resolution reads
as the properties function of the CamelContext
After Croway's review:
- The parser uses
HashicorpVaultPropertiesFunction(HashicorpVaultClientSettings) from
apache/camel#29819: it
reads in the namespace on HashiCorp Cloud, logs in once, and uses a token
given for the token login without
looking it up. release stops the function, which closes its client; the
private subclass is gone.
- The settings are fromEnvironmentOrConfiguration, as for the function of
the CamelContext: the
CAMEL_HASHICORP_VAULT_* variables win when one of them is set. The host,
port and scheme messages name the
variable too.
- The Binder resolves the placeholders with
environment.resolveRequiredPlaceholders, so an unresolvable ${...}
fails at once, as environment.getProperty did.
- The login-specific token check is left to Camel's check of the login; any
RuntimeException from creating
the function is wrapped with the hint about the options.
- AbstractEarlyResolutionPropertiesParser logs and ignores a failure to
release, which no longer hides the
error of the resolution nor aborts a startup whose properties are
resolved.
- EarlyResolvedPropertiesAppRoleTest skips an accessor revoked between the
list and its lookup.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Andrea Cosentino <[email protected]>
---------
Signed-off-by: Andrea Cosentino <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../SpringBootHashicorpVaultPropertiesParser.java | 70 ++++++----
.../EarlyResolvedPropertiesAppRoleTest.java | 152 +++++++++++++++++++++
...ringBootHashicorpVaultPropertiesParserTest.java | 64 ++++++++-
.../AbstractEarlyResolutionPropertiesParser.java | 23 +++-
...bstractEarlyResolutionPropertiesParserTest.java | 58 ++++++++
5 files changed, 341 insertions(+), 26 deletions(-)
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 1a90708674d..1a8b75be651 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
@@ -16,18 +16,30 @@
*/
package org.apache.camel.component.hashicorp.vault.springboot;
+import java.util.Locale;
import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.component.hashicorp.vault.HashicorpVaultClientSettings;
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;
+import
org.springframework.boot.context.properties.source.ConfigurationPropertySources;
import org.springframework.core.env.ConfigurableEnvironment;
-import org.springframework.vault.authentication.TokenAuthentication;
-import org.springframework.vault.client.VaultEndpoint;
-import org.springframework.vault.core.VaultTemplate;
+/**
+ * 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.
+ */
public class SpringBootHashicorpVaultPropertiesParser extends
AbstractEarlyResolutionPropertiesParser {
+ private static final String PREFIX = "camel.vault.hashicorp";
+ private static final String ENVIRONMENT_PREFIX = "CAMEL_HASHICORP_VAULT_";
+
@Override
protected String getEarlyResolutionProperty() {
return "camel.component.hashicorp-vault.early-resolve-properties";
@@ -40,35 +52,47 @@ public class SpringBootHashicorpVaultPropertiesParser
extends AbstractEarlyResol
@Override
protected PropertiesFunction
createPropertiesFunction(ConfigurableEnvironment environment) {
- String token = required(environment, "camel.vault.hashicorp.token",
"Hashicorp Vault token is required");
- String host = required(environment, "camel.vault.hashicorp.host",
"Hashicorp Vault host is required");
- String portValue = required(environment, "camel.vault.hashicorp.port",
"Hashicorp Vault port is required");
- String scheme = required(environment, "camel.vault.hashicorp.scheme",
"Hashicorp Vault scheme is required");
-
- int port;
+ // bound as Spring Boot binds the options of the CamelContext, such as
camel.vault.hashicorp.role-id, while a
+ // ${...} placeholder that cannot be resolved fails, as when the
property is read from the environment
+ HashicorpVaultConfiguration configuration = new
Binder(ConfigurationPropertySources.get(environment),
+ value -> value instanceof String text ?
environment.resolveRequiredPlaceholders(text) : value)
+ .bind(PREFIX, HashicorpVaultConfiguration.class)
+ .orElseGet(HashicorpVaultConfiguration::new);
+ HashicorpVaultClientSettings settings =
HashicorpVaultClientSettings.fromEnvironmentOrConfiguration(configuration);
+ required(settings.getHost(), "host", "Hashicorp Vault host is
required");
+ required(settings.getPort(), "port", "Hashicorp Vault port is
required");
+ required(settings.getScheme(), "scheme", "Hashicorp Vault scheme is
required");
try {
- port = Integer.parseInt(portValue);
+ Integer.parseInt(settings.getPort());
} catch (NumberFormatException e) {
throw new RuntimeCamelException(
- "camel.vault.hashicorp.port must be a number but was: " +
portValue, e);
+ PREFIX + ".port (or " + ENVIRONMENT_PREFIX + "PORT) must
be a number but was: " + settings.getPort(), e);
}
- VaultEndpoint vaultEndpoint = new VaultEndpoint();
- vaultEndpoint.setHost(host);
- vaultEndpoint.setPort(port);
- vaultEndpoint.setScheme(scheme);
+ try {
+ return new HashicorpVaultPropertiesFunction(settings);
+ } catch (RuntimeException e) {
+ throw new RuntimeCamelException(
+ "Cannot create the Hashicorp Vault client of the early
resolution: " + e.getMessage() + " (set the "
+ + PREFIX + " options, or the " +
ENVIRONMENT_PREFIX
+ + "* environment variables)",
+ e);
+ }
+ }
- VaultTemplate client = new VaultTemplate(
- vaultEndpoint,
- new TokenAuthentication(token));
- return new HashicorpVaultPropertiesFunction(client);
+ @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 String required(ConfigurableEnvironment environment, String
key, String message) {
- String value = environment.getProperty(key);
+ private static void required(String value, String option, String message) {
if (ObjectHelper.isEmpty(value)) {
- throw new RuntimeCamelException(message + " (set " + key + ")");
+ throw new RuntimeCamelException(
+ message + " (set " + PREFIX + "." + option + ", or " +
ENVIRONMENT_PREFIX
+ + option.toUpperCase(Locale.ROOT)
+ " when a " + ENVIRONMENT_PREFIX
+ + "* environment variable is
set)");
}
- return value;
}
}
diff --git
a/components-starter/camel-hashicorp-vault-starter/src/test/java/org/apache/camel/component/hashicorp/vault/springboot/EarlyResolvedPropertiesAppRoleTest.java
b/components-starter/camel-hashicorp-vault-starter/src/test/java/org/apache/camel/component/hashicorp/vault/springboot/EarlyResolvedPropertiesAppRoleTest.java
new file mode 100644
index 00000000000..1f43d33e9a5
--- /dev/null
+++
b/components-starter/camel-hashicorp-vault-starter/src/test/java/org/apache/camel/component/hashicorp/vault/springboot/EarlyResolvedPropertiesAppRoleTest.java
@@ -0,0 +1,152 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.hashicorp.vault.springboot;
+
+import org.apache.camel.spring.boot.CamelAutoConfiguration;
+import
org.apache.camel.test.infra.hashicorp.vault.services.HashicorpServiceFactory;
+import
org.apache.camel.test.infra.hashicorp.vault.services.HashicorpVaultService;
+import org.apache.camel.test.spring.junit6.CamelSpringBootTest;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.autoconfigure.AutoConfigureBefore;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.vault.VaultException;
+import org.springframework.vault.authentication.TokenAuthentication;
+import org.springframework.vault.client.VaultEndpoint;
+import org.springframework.vault.core.VaultKeyValueOperations;
+import org.springframework.vault.core.VaultKeyValueOperationsSupport;
+import org.springframework.vault.core.VaultTemplate;
+import org.springframework.vault.support.VaultMount;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * The early resolution logs in with the AppRole login of the {@code
camel.vault.hashicorp} options, and revokes the
+ * token of its login once the properties are resolved.
+ */
+@CamelSpringBootTest
+@DirtiesContext
+@SpringBootApplication
+@SpringBootTest(
+ classes = { EarlyResolvedPropertiesAppRoleTest.TestConfiguration.class
},
+ properties = {
+
"camel.component.hashicorp-vault.early-resolve-properties=true",
+
"early.resolved.property.approle={{hashicorp:secret:approle#string}}"
+ })
+class EarlyResolvedPropertiesAppRoleTest {
+
+ @RegisterExtension
+ static HashicorpVaultService service =
HashicorpServiceFactory.createService();
+
+ // the system properties are seen by the early resolution, and the JVM is
shared with the other tests
+ private static final List<String> OPTIONS = List.of(
+ "camel.vault.hashicorp.host", "camel.vault.hashicorp.port",
"camel.vault.hashicorp.scheme",
+ "camel.vault.hashicorp.token",
"camel.vault.hashicorp.authentication", "camel.vault.hashicorp.role-id",
+ "camel.vault.hashicorp.secret-id");
+
+ @BeforeAll
+ static void setup() {
+ VaultTemplate root = root();
+ VaultKeyValueOperations secrets
+ = root.opsForKeyValue("secret",
VaultKeyValueOperationsSupport.KeyValueBackend.versioned());
+ secrets.put("approle", Map.of("string", "from-approle"));
+ // read by application.properties
+ secrets.put("database/password", Map.of("string", "pazzword"));
+
+ root.write("sys/policies/acl/early-resolution",
+ Map.of("policy", "path \"secret/data/*\" { capabilities =
[\"read\"] }"));
+ if (!root.opsForSys().getAuthMounts().containsKey("approle/")) {
+ root.opsForSys().authMount("approle",
VaultMount.create("approle"));
+ }
+ root.write("auth/approle/role/camel", Map.of("token_policies",
"early-resolution"));
+ String roleId = (String)
root.read("auth/approle/role/camel/role-id").getRequiredData().get("role_id");
+ String secretId = (String)
root.write("auth/approle/role/camel/secret-id", Map.of())
+ .getRequiredData().get("secret_id");
+
+ // no token: the AppRole login gets one
+ OPTIONS.forEach(System::clearProperty);
+ System.setProperty("camel.vault.hashicorp.host", service.host());
+ System.setProperty("camel.vault.hashicorp.port",
String.valueOf(service.port()));
+ System.setProperty("camel.vault.hashicorp.scheme", "http");
+ // the kebab-case names of Spring Boot
+ System.setProperty("camel.vault.hashicorp.authentication", "approle");
+ System.setProperty("camel.vault.hashicorp.role-id", roleId);
+ System.setProperty("camel.vault.hashicorp.secret-id", secretId);
+ }
+
+ @AfterAll
+ static void clearOptions() {
+ OPTIONS.forEach(System::clearProperty);
+ }
+
+ @Value("${early.resolved.property}")
+ private String earlyResolvedProperty;
+
+ @Value("${early.resolved.property.approle}")
+ private String earlyResolvedPropertyAppRole;
+
+ @Test
+ void resolvesThePropertiesWithTheAppRoleLoginAndRevokesItsToken() {
+ assertEquals("pazzword", earlyResolvedProperty);
+ assertEquals("from-approle", earlyResolvedPropertyAppRole);
+
+ await().atMost(10, TimeUnit.SECONDS).untilAsserted(() ->
assertTrue(appRoleTokens().isEmpty(),
+ "the token of the AppRole login must be revoked once the
properties are resolved"));
+ }
+
+ private static VaultTemplate root() {
+ VaultEndpoint endpoint = VaultEndpoint.create(service.host(),
service.port());
+ endpoint.setScheme("http");
+ return new VaultTemplate(endpoint, new
TokenAuthentication(service.token()));
+ }
+
+ // the accessors of the tokens that the AppRole login gave and Vault has
not revoked
+ private static List<String> appRoleTokens() {
+ VaultTemplate root = root();
+ List<String> tokens = new ArrayList<>();
+ for (String accessor : root.list("auth/token/accessors")) {
+ try {
+ Object path = root.write("auth/token/lookup-accessor",
Map.of("accessor", accessor))
+ .getRequiredData().get("path");
+ if ("auth/approle/login".equals(path)) {
+ tokens.add(accessor);
+ }
+ } catch (VaultException e) {
+ // the token was revoked after the accessors were listed
+ }
+ }
+ return tokens;
+ }
+
+ @Configuration
+ @AutoConfigureBefore(CamelAutoConfiguration.class)
+ static class TestConfiguration {
+ }
+}
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 c6c79905ad6..f11cf4fbac9 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
@@ -17,6 +17,7 @@
package org.apache.camel.component.hashicorp.vault.springboot;
import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.spi.PropertiesFunction;
import org.junit.jupiter.api.Test;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.StandardEnvironment;
@@ -62,14 +63,38 @@ public class SpringBootHashicorpVaultPropertiesParserTest {
}
@Test
- public void missingTokenFailsAsACamelExceptionNamingTheProperty() {
+ public void missingTokenFailsAsACamelExceptionNamingTheOptions() {
+ // the token is checked with the other settings of the login, by Camel
RuntimeCamelException thrown =
assertThrows(RuntimeCamelException.class,
- () ->
parser.createPropertiesFunction(environmentWith(Map.of())),
+ () -> parser.createPropertiesFunction(environmentWith(Map.of(
+ "camel.vault.hashicorp.host", "127.0.0.1",
+ "camel.vault.hashicorp.port", "8200",
+ "camel.vault.hashicorp.scheme", "http"))),
"a missing setting is an operator error, not a programming
defect, so it must not surface "
+ "as NullPointerException");
assertTrue(thrown.getMessage().contains("token"),
"the failure must name the missing setting, was: " +
thrown.getMessage());
+ assertTrue(thrown.getMessage().contains("camel.vault.hashicorp"),
+ "the failure must name the options, was: " +
thrown.getMessage());
+ }
+
+ @Test
+ void anUnresolvablePlaceholderFailsAtOnce() {
+ RuntimeException thrown = assertThrows(RuntimeException.class,
+ () -> parser.createPropertiesFunction(environmentWith(Map.of(
+ "camel.vault.hashicorp.host", "127.0.0.1",
+ "camel.vault.hashicorp.port", "8200",
+ "camel.vault.hashicorp.scheme", "http",
+ "camel.vault.hashicorp.token", "${VAULT_TOKEN}"))),
+ "the literal placeholder would otherwise be sent to Vault as
the token");
+
+ Throwable cause = thrown;
+ while (cause.getCause() != null) {
+ cause = cause.getCause();
+ }
+ assertTrue(cause.getMessage().contains("VAULT_TOKEN"),
+ "the failure must name the placeholder, was: " +
cause.getMessage());
}
@Test
@@ -117,4 +142,39 @@ public class SpringBootHashicorpVaultPropertiesParserTest {
assertInstanceOf(NumberFormatException.class, thrown.getCause(),
"the cause must be preserved so the original
NumberFormatException is not lost");
}
+
+ @Test
+ void anAppRoleLoginNeedsNoToken() {
+ // the kebab-case names of Spring Boot, as for the options of the
CamelContext
+ PropertiesFunction function =
parser.createPropertiesFunction(environmentWith(Map.of(
+ "camel.vault.hashicorp.host", "127.0.0.1",
+ "camel.vault.hashicorp.port", "8200",
+ "camel.vault.hashicorp.scheme", "http",
+ "camel.vault.hashicorp.authentication", "approle",
+ "camel.vault.hashicorp.role-id", "a-role-id",
+ "camel.vault.hashicorp.secret-id", "a-secret-id")));
+ try {
+ assertEquals("hashicorp", function.getName());
+ } finally {
+ // the client logs in at its first request, so there is no token
to revoke
+ parser.release(function);
+ }
+ }
+
+ @Test
+ void anIncompleteLoginFailsAsACamelExceptionNamingTheOptions() {
+ RuntimeCamelException thrown =
assertThrows(RuntimeCamelException.class,
+ () -> parser.createPropertiesFunction(environmentWith(Map.of(
+ "camel.vault.hashicorp.host", "127.0.0.1",
+ "camel.vault.hashicorp.port", "8200",
+ "camel.vault.hashicorp.scheme", "http",
+ "camel.vault.hashicorp.authentication", "approle",
+ "camel.vault.hashicorp.role-id", "a-role-id"))));
+
+ assertTrue(thrown.getMessage().contains("secret ID"),
+ "the failure must name what the login misses, was: " +
thrown.getMessage());
+ assertTrue(thrown.getMessage().contains("camel.vault.hashicorp"),
+ "the failure must name the options, was: " +
thrown.getMessage());
+ assertInstanceOf(IllegalArgumentException.class, thrown.getCause());
+ }
}
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 3e2c81168fc..a7fc432d17e 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
@@ -75,6 +75,13 @@ 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.
+ */
+ protected void release(PropertiesFunction propertiesFunction) {
+ }
+
/**
* Wording used in diagnostics to name where a value was being resolved
from.
*/
@@ -89,12 +96,26 @@ public abstract class
AbstractEarlyResolutionPropertiesParser
return;
}
+ PropertiesFunction propertiesFunction =
createPropertiesFunction(environment);
+ try {
+ resolve(environment, propertiesFunction);
+ } finally {
+ try {
+ release(propertiesFunction);
+ } catch (RuntimeException e) {
+ // neither hides the error of the resolution nor aborts a
startup whose properties are resolved
+ LOG.warn("Failed to release the {} properties function. This
exception is ignored.",
+ propertiesFunction.getName(), e);
+ }
+ }
+ }
+
+ private void resolve(ConfigurableEnvironment environment,
PropertiesFunction propertiesFunction) {
// an unresolved placeholder would otherwise stay in the property
value and become the effective
// secret, so resolution failures abort startup unless the operator
opts back into the old behaviour
final boolean ignoreResolutionFailures
=
Boolean.parseBoolean(environment.getProperty(IGNORE_RESOLUTION_FAILURES));
- PropertiesFunction propertiesFunction =
createPropertiesFunction(environment);
LOG.debug("Early resolving properties using the {} function",
propertiesFunction.getName());
final String prefix = "{{" + propertiesFunction.getName() + ":";
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 8ecf326f2d0..f01ffd867ac 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
@@ -91,6 +91,8 @@ public class AbstractEarlyResolutionPropertiesParserTest {
private static final class TestParser extends
AbstractEarlyResolutionPropertiesParser {
private final StubPropertiesFunction function;
private int factoryCalls;
+ private int releaseCalls;
+ private boolean failingRelease;
TestParser(StubPropertiesFunction function) {
this.function = function;
@@ -111,6 +113,15 @@ public class AbstractEarlyResolutionPropertiesParserTest {
factoryCalls++;
return function;
}
+
+ @Override
+ protected void release(PropertiesFunction propertiesFunction) {
+ assertEquals(function, propertiesFunction);
+ releaseCalls++;
+ if (failingRelease) {
+ throw new IllegalStateException("cannot release");
+ }
+ }
}
private static ApplicationEnvironmentPreparedEvent
eventFor(ConfigurableEnvironment environment) {
@@ -144,6 +155,53 @@ public class AbstractEarlyResolutionPropertiesParserTest {
"no override property source may be added when early
resolution is disabled");
}
+ @Test
+ void releasesThePropertiesFunctionOnceResolved() {
+ RecordingEnvironment environment = environmentWith(Map.of(GUARD,
"true", "my.secret", "{{test:db}}"));
+ TestParser parser = new TestParser(new
StubPropertiesFunction(Map.of("db", "pazzword"), Set.of()));
+
+ parser.onApplicationEvent(eventFor(environment));
+
+ assertEquals("pazzword", environment.getProperty("my.secret"));
+ assertEquals(1, parser.releaseCalls, "the function is released once
the properties are resolved");
+ }
+
+ @Test
+ void releasesThePropertiesFunctionAlsoWhenResolutionFails() {
+ RecordingEnvironment environment = environmentWith(Map.of(GUARD,
"true", "my.secret", "{{test:missing}}"));
+ TestParser parser = new TestParser(new
StubPropertiesFunction(Map.of(), Set.of("missing")));
+
+ assertThrows(RuntimeCamelException.class, () ->
parser.onApplicationEvent(eventFor(environment)));
+
+ assertEquals(1, parser.releaseCalls, "the function is released also
when startup is aborted");
+ }
+
+ @Test
+ void aFailingReleaseDoesNotHideTheResolutionError() {
+ RecordingEnvironment environment = environmentWith(Map.of(GUARD,
"true", "my.secret", "{{test:missing}}"));
+ TestParser parser = new TestParser(new
StubPropertiesFunction(Map.of(), Set.of("missing")));
+ parser.failingRelease = true;
+
+ RuntimeCamelException thrown =
assertThrows(RuntimeCamelException.class,
+ () -> parser.onApplicationEvent(eventFor(environment)));
+
+ assertTrue(thrown.getMessage().contains("my.secret"),
+ "the error must still name the unresolved property, was: " +
thrown.getMessage());
+ assertEquals(1, parser.releaseCalls);
+ }
+
+ @Test
+ void aFailingReleaseDoesNotAbortStartup() {
+ RecordingEnvironment environment = environmentWith(Map.of(GUARD,
"true", "my.secret", "{{test:db}}"));
+ TestParser parser = new TestParser(new
StubPropertiesFunction(Map.of("db", "pazzword"), Set.of()));
+ parser.failingRelease = true;
+
+ parser.onApplicationEvent(eventFor(environment));
+
+ assertEquals("pazzword", environment.getProperty("my.secret"));
+ assertEquals(1, parser.releaseCalls);
+ }
+
@Test
public void guardEnabledBuildsTheClientAndAddsTheOverrideSource() {
RecordingEnvironment environment = environmentWith(Map.of(GUARD,
"true"));