This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 612fd07907ca CAMEL-26919: camel-hashicorp-vault - a properties 
function and a Vault client without a CamelContext, for the Spring Boot early 
resolution (#29819)
612fd07907ca is described below

commit 612fd07907ca2900762b56e71d825be8ae2a8514
Author: Andrea Cosentino <[email protected]>
AuthorDate: Mon Nov 16 14:31:39 2026 +0100

    CAMEL-26919: camel-hashicorp-vault - a properties function and a Vault 
client without a CamelContext, for the Spring Boot early resolution (#29819)
    
    Fixes [CAMEL-26919](https://issues.apache.org/jira/browse/CAMEL-26919), a 
follow-up of [CAMEL-25675](https://issues.apache.org/jira/browse/CAMEL-25675) 
(#29235, #29619, #29708).
    
    Camel on Spring Boot can resolve `hashicorp:` properties early 
(`camel.component.hashicorp-vault.early-resolve-properties=true`), before there 
is a CamelContext. That early resolution builds its own `VaultTemplate` with 
`camel.vault.hashicorp.token`. So the logins of CAMEL-25675 (AppRole, 
Kubernetes, JWT, cert, cloud), the TLS options and the namespace on HashiCorp 
Cloud do not apply to it: an application that logs in with AppRole needs a 
fixed token again just for the early resolutio [...]
    
    _Claude Code on behalf of oscerd_
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../catalog/docs/hashicorp-vault-component.adoc    | 11 +++
 .../src/main/docs/hashicorp-vault-component.adoc   | 11 +++
 .../hashicorp/vault/HashicorpVaultClient.java      | 88 ++++++++++++++++++----
 .../vault/HashicorpVaultClientSettings.java        | 65 ++++++++++------
 .../vault/HashicorpVaultPropertiesFunction.java    | 23 +++++-
 .../vault/HashicorpVaultClientSettingsTest.java    | 19 +++++
 .../hashicorp/vault/HashicorpVaultClientTest.java  | 50 ++++++++++++
 .../HashicorpVaultPropertiesFunctionTest.java      | 30 +++++++-
 8 files changed, 257 insertions(+), 40 deletions(-)

diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/hashicorp-vault-component.adoc
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/hashicorp-vault-component.adoc
index 61f201a4c40d..117b6175c821 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/hashicorp-vault-component.adoc
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/hashicorp-vault-component.adoc
@@ -860,3 +860,14 @@ This will enable the feature so you'll be able to resolved 
properties, in your a
 ----
 foo = hashicorp:secret:database/password#string
 ----
+
+The early resolution logs in as the properties function does, with the same 
settings: the `CAMEL_HASHICORP_VAULT_*`
+environment variables when one of them is set, else the 
`camel.vault.hashicorp` options. So it supports any login of
+<<_logging_in_to_hashicorp_vault,Logging in to HashiCorp Vault>>, the trust 
store and key store of <<_tls,TLS>>, and the
+namespace on HashiCorp Cloud. It logs in once: when the login fails, the 
properties that are left fail with the same
+error, without logging in again for each of them, since failed AppRole logins 
count towards the user lockout of Vault.
+Once the properties are resolved, it revokes the token of its login (a token 
given for the token login is not revoked).
+
+The properties function of the CamelContext, and the secret refresh task when 
it is enabled, log in again with the same
+settings when they first read a secret. So an AppRole secret ID that can be 
used only once (`secret_id_num_uses=1`) is
+used up by the early resolution, and that later login fails.
diff --git 
a/components/camel-hashicorp-vault/src/main/docs/hashicorp-vault-component.adoc 
b/components/camel-hashicorp-vault/src/main/docs/hashicorp-vault-component.adoc
index 61f201a4c40d..117b6175c821 100644
--- 
a/components/camel-hashicorp-vault/src/main/docs/hashicorp-vault-component.adoc
+++ 
b/components/camel-hashicorp-vault/src/main/docs/hashicorp-vault-component.adoc
@@ -860,3 +860,14 @@ This will enable the feature so you'll be able to resolved 
properties, in your a
 ----
 foo = hashicorp:secret:database/password#string
 ----
+
+The early resolution logs in as the properties function does, with the same 
settings: the `CAMEL_HASHICORP_VAULT_*`
+environment variables when one of them is set, else the 
`camel.vault.hashicorp` options. So it supports any login of
+<<_logging_in_to_hashicorp_vault,Logging in to HashiCorp Vault>>, the trust 
store and key store of <<_tls,TLS>>, and the
+namespace on HashiCorp Cloud. It logs in once: when the login fails, the 
properties that are left fail with the same
+error, without logging in again for each of them, since failed AppRole logins 
count towards the user lockout of Vault.
+Once the properties are resolved, it revokes the token of its login (a token 
given for the token login is not revoked).
+
+The properties function of the CamelContext, and the secret refresh task when 
it is enabled, log in again with the same
+settings when they first read a secret. So an AppRole secret ID that can be 
used only once (`secret_id_num_uses=1`) is
+used up by the early resolution, and that later login fails.
diff --git 
a/components/camel-hashicorp-vault/src/main/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultClient.java
 
b/components/camel-hashicorp-vault/src/main/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultClient.java
index c9ae0996a35d..ebf473477f7d 100644
--- 
a/components/camel-hashicorp-vault/src/main/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultClient.java
+++ 
b/components/camel-hashicorp-vault/src/main/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultClient.java
@@ -25,7 +25,9 @@ import java.net.URLEncoder;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Consumer;
 import java.util.function.Supplier;
 
@@ -72,7 +74,10 @@ import org.springframework.web.client.RestClient;
  * but never revoked.
  * <p/>
  * The endpoint, the properties function and the secret refresh task each 
create their client with
- * {@link #create(CamelContext, Object, HashicorpVaultClientSettings)}, and 
close it when they stop.
+ * {@link #create(CamelContext, Object, HashicorpVaultClientSettings)}, and 
close it when they stop. Code that runs
+ * before there is a CamelContext, such as the early resolution of properties 
of Camel on Spring Boot, creates a client
+ * for a short use with {@link #create(HashicorpVaultClientSettings)}, which 
logs in once and does not renew a given
+ * token.
  */
 public final class HashicorpVaultClient implements Closeable {
 
@@ -82,23 +87,22 @@ public final class HashicorpVaultClient implements 
Closeable {
     private static final String AWS_SDK_CLASS = 
"software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider";
     // the resource of the Azure login in the documentation of HashiCorp Vault
     static final String DEFAULT_AZURE_RESOURCE = 
"https://management.azure.com/";;
+    private static final String RENEWAL_THREAD = "HashicorpVaultTokenRenewal";
     private static final String AZURE_IDENTITY_TOKEN_URI
             = 
"http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01";;
 
     private final VaultTemplate vaultTemplate;
     private final LifecycleAwareSessionManager sessionManager;
-    private final ExecutorServiceManager executorServiceManager;
-    private final ScheduledExecutorService renewal;
+    // stops the thread that renews the token
+    private final Runnable stopRenewal;
     // what the login holds, such as the credentials provider of the AWS SDK, 
closed with the client
     private final List<AutoCloseable> resources;
 
     private HashicorpVaultClient(VaultTemplate vaultTemplate, 
LifecycleAwareSessionManager sessionManager,
-                                 ExecutorServiceManager 
executorServiceManager, ScheduledExecutorService renewal,
-                                 List<AutoCloseable> resources) {
+                                 Runnable stopRenewal, List<AutoCloseable> 
resources) {
         this.vaultTemplate = vaultTemplate;
         this.sessionManager = sessionManager;
-        this.executorServiceManager = executorServiceManager;
-        this.renewal = renewal;
+        this.stopRenewal = stopRenewal;
         this.resources = resources;
     }
 
@@ -112,6 +116,40 @@ public final class HashicorpVaultClient implements 
Closeable {
      * @throws IllegalArgumentException when the settings are incomplete or 
not valid
      */
     public static HashicorpVaultClient create(CamelContext camelContext, 
Object source, HashicorpVaultClientSettings settings) {
+        ExecutorServiceManager executorServiceManager = 
camelContext.getExecutorServiceManager();
+        return create(settings, false,
+                () -> 
executorServiceManager.newSingleThreadScheduledExecutor(source, RENEWAL_THREAD),
+                executorServiceManager::shutdownNow);
+    }
+
+    /**
+     * Creates a client without a CamelContext, for a short use such as 
resolving properties before the context exists.
+     * It logs in once, at its first request to Vault: when the login fails, 
the following requests fail with the same
+     * error rather than log in again, since every failed login counts towards 
the user lockout of Vault (for AppRole).
+     * A token given for the token login is used as it is, without looking it 
up, so it is not renewed. The token of the
+     * other logins is renewed on a daemon thread of the client, which closing 
the client stops.
+     *
+     * @param  settings                 where Vault is and how to log in to it
+     * @return                          the client, to close when it is no 
longer used
+     * @throws IllegalArgumentException when the settings are incomplete or 
not valid
+     * @since                           4.24
+     */
+    public static HashicorpVaultClient create(HashicorpVaultClientSettings 
settings) {
+        return create(settings, true, () -> 
Executors.newSingleThreadScheduledExecutor(runnable -> {
+            Thread thread = new Thread(runnable, "Camel " + RENEWAL_THREAD);
+            // the renewal does not keep the JVM running
+            thread.setDaemon(true);
+            return thread;
+        }), ScheduledExecutorService::shutdownNow);
+    }
+
+    /**
+     * @param shortUse whether the client is for a short use: it then logs in 
once, and uses a given token without
+     *                 looking it up
+     */
+    private static HashicorpVaultClient create(
+            HashicorpVaultClientSettings settings, boolean shortUse, 
Supplier<ScheduledExecutorService> renewals,
+            Consumer<ScheduledExecutorService> shutdown) {
         validate(settings);
         VaultEndpoint endpoint = new VaultEndpoint();
         endpoint.setHost(settings.getHost());
@@ -132,18 +170,23 @@ public final class HashicorpVaultClient implements 
Closeable {
             close(resources);
             throw e;
         }
+        if (shortUse) {
+            authentication = loginOnce(authentication);
+        }
 
-        ExecutorServiceManager executorServiceManager = 
camelContext.getExecutorServiceManager();
-        ScheduledExecutorService renewal
-                = 
executorServiceManager.newSingleThreadScheduledExecutor(source, 
"HashicorpVaultTokenRenewal");
+        ScheduledExecutorService renewal = renewals.get();
         try {
             LifecycleAwareSessionManager sessionManager
                     = new LifecycleAwareSessionManager(authentication, new 
ConcurrentTaskScheduler(renewal), loginClient);
+            if (shortUse) {
+                // looking up a given token would be one more request, which 
also uses up one of its uses
+                sessionManager.setTokenSelfLookupEnabled(false);
+            }
             return new HashicorpVaultClient(
-                    new VaultTemplate(client, sessionManager), sessionManager, 
executorServiceManager, renewal, resources);
+                    new VaultTemplate(client, sessionManager), sessionManager, 
() -> shutdown.accept(renewal), resources);
         } catch (RuntimeException e) {
             // nothing is scheduled on the executor yet, so there is nothing 
to wait for
-            executorServiceManager.shutdownNow(renewal);
+            shutdown.accept(renewal);
             close(resources);
             throw e;
         }
@@ -221,6 +264,25 @@ public final class HashicorpVaultClient implements 
Closeable {
         }
     }
 
+    /**
+     * The login, which is not tried again once it failed: the failure is 
thrown again for the following logins.
+     */
+    static ClientAuthentication loginOnce(ClientAuthentication authentication) 
{
+        AtomicReference<RuntimeException> failure = new AtomicReference<>();
+        return () -> {
+            RuntimeException failed = failure.get();
+            if (failed != null) {
+                throw failed;
+            }
+            try {
+                return authentication.login();
+            } catch (RuntimeException e) {
+                failure.compareAndSet(null, e);
+                throw e;
+            }
+        };
+    }
+
     /**
      * The login of the settings, which logs in with the given client.
      *
@@ -393,7 +455,7 @@ public final class HashicorpVaultClient implements 
Closeable {
     @Override
     public void close() {
         try {
-            executorServiceManager.shutdownNow(renewal);
+            stopRenewal.run();
         } finally {
             try {
                 sessionManager.destroy();
diff --git 
a/components/camel-hashicorp-vault/src/main/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultClientSettings.java
 
b/components/camel-hashicorp-vault/src/main/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultClientSettings.java
index 9c95106d50ef..2c34812d8a62 100644
--- 
a/components/camel-hashicorp-vault/src/main/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultClientSettings.java
+++ 
b/components/camel-hashicorp-vault/src/main/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultClientSettings.java
@@ -81,10 +81,12 @@ public final class HashicorpVaultClientSettings {
     private String azureClientId;
 
     /**
-     * The settings of the properties function and the secret refresh task: 
from the CAMEL_HASHICORP_VAULT_* environment
-     * variables when one of them is set, else from the vault configuration.
+     * The settings of the properties function and the secret refresh task, 
and of the early resolution of properties of
+     * Camel on Spring Boot: from the CAMEL_HASHICORP_VAULT_* environment 
variables when one of them is set, else from
+     * the vault configuration.
      *
-     * @param configuration the vault configuration (camel.vault.hashicorp), 
may be null
+     * @param  configuration the vault configuration (camel.vault.hashicorp), 
may be null
+     * @return               the settings
      */
     public static HashicorpVaultClientSettings 
fromEnvironmentOrConfiguration(HashicorpVaultConfiguration configuration) {
         return fromEnvironmentOrConfiguration(configuration, System::getenv);
@@ -117,32 +119,45 @@ public final class HashicorpVaultClientSettings {
             settings.setAzureResource(environment.apply(AZURE_RESOURCE_ENV));
             settings.setAzureClientId(environment.apply(AZURE_CLIENT_ID_ENV));
         } else if (configuration != null) {
-            settings.setHost(configuration.getHost());
-            settings.setPort(configuration.getPort());
-            settings.setScheme(configuration.getScheme());
-            settings.setCloud(configuration.isCloud());
-            settings.setNamespace(configuration.getNamespace());
-            settings.setAuthentication(configuration.getAuthentication());
-            settings.setToken(configuration.getToken());
-            settings.setRoleId(configuration.getRoleId());
-            settings.setSecretId(configuration.getSecretId());
-            settings.setRole(configuration.getRole());
-            settings.setJwt(configuration.getJwt());
-            settings.setJwtFile(configuration.getJwtFile());
-            settings.setAuthMountPath(configuration.getAuthMountPath());
-            settings.setTrustStore(configuration.getTrustStore());
-            
settings.setTrustStorePassword(configuration.getTrustStorePassword());
-            settings.setKeyStore(configuration.getKeyStore());
-            settings.setKeyStorePassword(configuration.getKeyStorePassword());
-            settings.setAwsIamServerId(configuration.getAwsIamServerId());
-            settings.setAwsRegion(configuration.getAwsRegion());
-            settings.setAwsStsEndpoint(configuration.getAwsStsEndpoint());
-            settings.setAzureResource(configuration.getAzureResource());
-            settings.setAzureClientId(configuration.getAzureClientId());
+            settings = fromConfiguration(configuration);
         }
         return settings;
     }
 
+    /**
+     * The settings of the vault configuration (camel.vault.hashicorp) alone, 
without the CAMEL_HASHICORP_VAULT_*
+     * environment variables.
+     *
+     * @param  configuration the vault configuration
+     * @return               the settings
+     */
+    static HashicorpVaultClientSettings 
fromConfiguration(HashicorpVaultConfiguration configuration) {
+        HashicorpVaultClientSettings settings = new 
HashicorpVaultClientSettings();
+        settings.setHost(configuration.getHost());
+        settings.setPort(configuration.getPort());
+        settings.setScheme(configuration.getScheme());
+        settings.setCloud(configuration.isCloud());
+        settings.setNamespace(configuration.getNamespace());
+        settings.setAuthentication(configuration.getAuthentication());
+        settings.setToken(configuration.getToken());
+        settings.setRoleId(configuration.getRoleId());
+        settings.setSecretId(configuration.getSecretId());
+        settings.setRole(configuration.getRole());
+        settings.setJwt(configuration.getJwt());
+        settings.setJwtFile(configuration.getJwtFile());
+        settings.setAuthMountPath(configuration.getAuthMountPath());
+        settings.setTrustStore(configuration.getTrustStore());
+        settings.setTrustStorePassword(configuration.getTrustStorePassword());
+        settings.setKeyStore(configuration.getKeyStore());
+        settings.setKeyStorePassword(configuration.getKeyStorePassword());
+        settings.setAwsIamServerId(configuration.getAwsIamServerId());
+        settings.setAwsRegion(configuration.getAwsRegion());
+        settings.setAwsStsEndpoint(configuration.getAwsStsEndpoint());
+        settings.setAzureResource(configuration.getAzureResource());
+        settings.setAzureClientId(configuration.getAzureClientId());
+        return settings;
+    }
+
     /**
      * The namespace on HashiCorp Cloud: the namespace when deployed on 
HashiCorp Cloud and a namespace is set, else
      * null. The secrets engines and the login methods are in this namespace.
diff --git 
a/components/camel-hashicorp-vault/src/main/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultPropertiesFunction.java
 
b/components/camel-hashicorp-vault/src/main/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultPropertiesFunction.java
index a9cae400383a..54c4c1e93297 100644
--- 
a/components/camel-hashicorp-vault/src/main/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultPropertiesFunction.java
+++ 
b/components/camel-hashicorp-vault/src/main/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultPropertiesFunction.java
@@ -108,7 +108,8 @@ public class HashicorpVaultPropertiesFunction extends 
ServiceSupport implements
     private static final String DEFAULT_ENGINE = "secret";
     private CamelContext camelContext;
     private volatile VaultTemplate client;
-    // the client the function created, which it closes on stop (a client 
given to the function is not closed)
+    // the client the function created (when it starts, or from the settings 
it is given), which it closes on stop: a
+    // VaultTemplate given to the function is not closed
     private HashicorpVaultClient vaultClient;
     private final Set<String> secrets = ConcurrentHashMap.newKeySet();
     private final Map<String, Integer> secretVersions = new 
ConcurrentHashMap<>();
@@ -125,6 +126,26 @@ public class HashicorpVaultPropertiesFunction extends 
ServiceSupport implements
         this.client = client;
     }
 
+    /**
+     * A function that reads the secrets without a CamelContext, such as to 
resolve properties before the context
+     * exists. It reads them as the function of the CamelContext does with 
these settings, also in the namespace on
+     * HashiCorp Cloud, with a client for a short use of its own (see
+     * {@link HashicorpVaultClient#create(HashicorpVaultClientSettings)}). 
Stopping the function closes the client, also
+     * when the function was not started.
+     *
+     * @param  settings                 where Vault is and how to log in to it
+     * @throws IllegalArgumentException when the settings are incomplete or 
not valid
+     * @since                           4.24
+     */
+    public HashicorpVaultPropertiesFunction(HashicorpVaultClientSettings 
settings) {
+        this(HashicorpVaultClient.create(settings), settings);
+    }
+
+    private HashicorpVaultPropertiesFunction(HashicorpVaultClient vaultClient, 
HashicorpVaultClientSettings settings) {
+        this(vaultClient.getVaultTemplate(), settings.isCloud(), 
settings.getNamespace());
+        this.vaultClient = vaultClient;
+    }
+
     HashicorpVaultPropertiesFunction(VaultTemplate client, boolean cloud, 
String namespace) {
         this(client);
         this.cloud = cloud;
diff --git 
a/components/camel-hashicorp-vault/src/test/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultClientSettingsTest.java
 
b/components/camel-hashicorp-vault/src/test/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultClientSettingsTest.java
index 01f7de1e912e..5d99a3666fe4 100644
--- 
a/components/camel-hashicorp-vault/src/test/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultClientSettingsTest.java
+++ 
b/components/camel-hashicorp-vault/src/test/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultClientSettingsTest.java
@@ -151,6 +151,25 @@ class HashicorpVaultClientSettingsTest {
         assertEquals("1b2c3d4e", settings.getAzureClientId());
     }
 
+    @Test
+    void takesTheSettingsOfTheConfigurationAlone() {
+        HashicorpVaultConfiguration configuration = configuration();
+        configuration.setTrustStore("/etc/vault/ca.pem");
+        configuration.setAwsRegion("eu-west-1");
+
+        HashicorpVaultClientSettings settings = 
HashicorpVaultClientSettings.fromConfiguration(configuration);
+
+        assertEquals("config-host", settings.getHost());
+        assertEquals("8200", settings.getPort());
+        assertEquals("https", settings.getScheme());
+        assertEquals("approle", settings.getAuthentication());
+        assertEquals("config-role-id", settings.getRoleId());
+        assertEquals("config-secret-id", settings.getSecretId());
+        assertEquals("/etc/vault/ca.pem", settings.getTrustStore());
+        assertEquals("eu-west-1", settings.getAwsRegion());
+        assertEquals("https://management.azure.com/";, 
settings.getAzureResource());
+    }
+
     @Test
     void takesTheConfigurationWhenNoEnvironmentVariableIsSet() {
         Map<String, String> environment = Map.of(CLOUD_ENV, "true");
diff --git 
a/components/camel-hashicorp-vault/src/test/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultClientTest.java
 
b/components/camel-hashicorp-vault/src/test/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultClientTest.java
index 92ce1cad3534..0fee1e27c54f 100644
--- 
a/components/camel-hashicorp-vault/src/test/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultClientTest.java
+++ 
b/components/camel-hashicorp-vault/src/test/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultClientTest.java
@@ -34,6 +34,7 @@ import org.springframework.vault.support.VaultResponse;
 
 import static org.awaitility.Awaitility.await;
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -171,6 +172,55 @@ class HashicorpVaultClientTest {
         assertEquals("kubernetes-token", 
vault.requests("secret/data/api").get(0).token());
     }
 
+    @Test
+    void 
aClientWithoutCamelContextRenewsItsTokenOnADaemonThreadAndRevokesItOnClose() {
+        vault.on("auth/approle/login", request -> 
FakeVault.login("approle-token", RENEWED_SOON));
+        vault.on("auth/token/renew-self", request -> 
FakeVault.login("approle-token", RENEWED_LATER));
+        client = HashicorpVaultClient.create(approle());
+
+        assertEquals("pw", readPassword());
+        await().atMost(10, TimeUnit.SECONDS).until(() -> 
!vault.requests("auth/token/renew-self").isEmpty());
+
+        List<Thread> renewal = Thread.getAllStackTraces().keySet().stream()
+                .filter(thread -> "Camel 
HashicorpVaultTokenRenewal".equals(thread.getName()))
+                .toList();
+        assertFalse(renewal.isEmpty());
+        assertTrue(renewal.stream().allMatch(Thread::isDaemon), "the renewal 
does not keep the JVM running");
+
+        client.close();
+        client = null;
+        assertEquals("approle-token", 
vault.requests("auth/token/revoke-self").get(0).token());
+    }
+
+    @Test
+    void aClientWithoutCamelContextLogsInOnce() {
+        vault.on("auth/approle/login",
+                request -> new FakeVault.Reply(400, "{\"errors\":[\"invalid 
role or secret ID\"]}"));
+        client = HashicorpVaultClient.create(approle());
+
+        for (int read = 0; read < 3; read++) {
+            assertThrows(RuntimeException.class, this::readPassword);
+        }
+        assertEquals(1, vault.requests("auth/approle/login").size(), "a failed 
login is not tried again");
+        assertTrue(vault.requests("secret/data/api").isEmpty());
+    }
+
+    @Test
+    void aClientWithoutCamelContextUsesAGivenTokenWithoutLookingItUp() {
+        HashicorpVaultClientSettings settings = settings(null);
+        settings.setToken("my-token");
+        client = HashicorpVaultClient.create(settings);
+
+        assertEquals("pw", readPassword());
+        assertEquals("pw", readPassword());
+        assertEquals("my-token", 
vault.requests("secret/data/api").get(1).token());
+        assertTrue(vault.requests("auth/token/lookup-self").isEmpty(), "the 
given token is used as it is");
+
+        client.close();
+        client = null;
+        assertTrue(vault.requests("auth/token/revoke-self").isEmpty(), "a 
given token is never revoked");
+    }
+
     @Test
     void renewsTheTokenBeforeItExpires() {
         vault.on("auth/approle/login", request -> 
FakeVault.login("approle-token", RENEWED_SOON));
diff --git 
a/components/camel-hashicorp-vault/src/test/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultPropertiesFunctionTest.java
 
b/components/camel-hashicorp-vault/src/test/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultPropertiesFunctionTest.java
index 7b8a728d3249..88b40b2a5493 100644
--- 
a/components/camel-hashicorp-vault/src/test/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultPropertiesFunctionTest.java
+++ 
b/components/camel-hashicorp-vault/src/test/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultPropertiesFunctionTest.java
@@ -40,7 +40,8 @@ import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 /**
- * Lookups of the properties function against a fake {@link VaultTemplate}, 
which answers the KV v2 data paths.
+ * Lookups of the properties function against a fake {@link VaultTemplate}, 
which answers the KV v2 data paths, and
+ * against a {@link FakeVault} for a function that creates its own client.
  */
 class HashicorpVaultPropertiesFunctionTest {
 
@@ -130,6 +131,33 @@ class HashicorpVaultPropertiesFunctionTest {
         assertEquals(List.of("admin/secret/data/api?version=2"), vault.reads);
     }
 
+    @Test
+    void 
aFunctionWithoutCamelContextReadsInTheNamespaceAndRevokesItsTokenWhenStopped() 
throws Exception {
+        try (FakeVault vault = new FakeVault()) {
+            vault.on("auth/approle/login", request -> 
FakeVault.login("approle-token", 3600));
+            vault.on("admin/secret/data/api", request -> 
FakeVault.secret("pw"));
+            vault.on("auth/token/revoke-self", request -> 
FakeVault.noContent());
+            HashicorpVaultClientSettings settings = new 
HashicorpVaultClientSettings();
+            settings.setHost("localhost");
+            settings.setPort(String.valueOf(vault.port()));
+            settings.setScheme("http");
+            settings.setAuthentication("approle");
+            settings.setRoleId("my-role-id");
+            settings.setSecretId("my-secret-id");
+            settings.setCloud(true);
+            settings.setNamespace("admin");
+            HashicorpVaultPropertiesFunction function = new 
HashicorpVaultPropertiesFunction(settings);
+
+            assertEquals("pw", function.apply("secret:api#password"));
+            assertEquals("admin", 
vault.requests("auth/approle/login").get(0).namespace());
+            assertEquals("approle-token", 
vault.requests("admin/secret/data/api").get(0).token());
+
+            // stopped without being started, as before there is a CamelContext
+            function.stop();
+            assertEquals("approle-token", 
vault.requests("auth/token/revoke-self").get(0).token());
+        }
+    }
+
     @Test
     void keepsTheClientItIsGiven() throws Exception {
         FakeVaultTemplate vault = new 
FakeVaultTemplate(Map.of("secret/data/api", Map.of("password", "pw")));

Reply via email to