oscerd commented on code in PR #2091:
URL: 
https://github.com/apache/camel-spring-boot/pull/2091#discussion_r4584083415


##########
components-starter/camel-hashicorp-vault-starter/src/main/java/org/apache/camel/component/hashicorp/vault/springboot/SpringBootHashicorpVaultPropertiesParser.java:
##########
@@ -40,35 +48,62 @@ protected String getOverridePropertySourceName() {
 
     @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
+        HashicorpVaultConfiguration configuration = Binder.get(environment)
+                .bind(PREFIX, HashicorpVaultConfiguration.class)
+                .orElseGet(HashicorpVaultConfiguration::new);
+        if (isTokenLogin(configuration)) {
+            required(configuration.getToken(), "token", "Hashicorp Vault token 
is required");
+        }
+        required(configuration.getHost(), "host", "Hashicorp Vault host is 
required");
+        required(configuration.getPort(), "port", "Hashicorp Vault port is 
required");
+        required(configuration.getScheme(), "scheme", "Hashicorp Vault scheme 
is required");
         try {
-            port = Integer.parseInt(portValue);
+            Integer.parseInt(configuration.getPort());
         } catch (NumberFormatException e) {
             throw new RuntimeCamelException(
-                    "camel.vault.hashicorp.port must be a number but was: " + 
portValue, e);
+                    PREFIX + ".port must be a number but was: " + 
configuration.getPort(), e);
         }
 
-        VaultEndpoint vaultEndpoint = new VaultEndpoint();
-        vaultEndpoint.setHost(host);
-        vaultEndpoint.setPort(port);
-        vaultEndpoint.setScheme(scheme);
+        HashicorpVaultClient client;
+        try {
+            client = 
HashicorpVaultClient.create(HashicorpVaultClientSettings.fromConfiguration(configuration));
+        } catch (IllegalArgumentException e) {
+            throw new RuntimeCamelException(
+                    "Cannot create the Hashicorp Vault client of the " + 
PREFIX + " options: " + e.getMessage(), e);
+        }
+        return new EarlyResolutionFunction(client);
+    }
 
-        VaultTemplate client = new VaultTemplate(
-                vaultEndpoint,
-                new TokenAuthentication(token));
-        return new HashicorpVaultPropertiesFunction(client);
+    @Override
+    protected void release(PropertiesFunction propertiesFunction) {
+        if (propertiesFunction instanceof EarlyResolutionFunction function) {
+            // stops renewing the token, and revokes the token of the login 
(but not a token given for the token login)
+            function.client.close();
+        }
+    }
+
+    private static boolean isTokenLogin(HashicorpVaultConfiguration 
configuration) {
+        String authentication = configuration.getAuthentication();
+        return ObjectHelper.isEmpty(authentication) || 
"token".equalsIgnoreCase(authentication.trim());
     }
 
-    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 + ")");
+        }
+    }
+
+    /**
+     * The properties function of the early resolution, with the client it 
reads the secrets with.
+     */
+    private static final class EarlyResolutionFunction extends 
HashicorpVaultPropertiesFunction {
+
+        private final HashicorpVaultClient client;
+
+        private EarlyResolutionFunction(HashicorpVaultClient client) {
+            super(client.getVaultTemplate());

Review Comment:
   Fixed through apache/camel#29819, which now has a public 
`HashicorpVaultPropertiesFunction(HashicorpVaultClientSettings)` constructor: 
the function creates its own client, takes `cloud` and `namespace` from the 
settings, and reads in the namespace as the function of the CamelContext does. 
The parser uses it, so the subclass is gone. Camel test: 
`aFunctionWithoutCamelContextReadsInTheNamespaceAndRevokesItsTokenWhenStopped` 
(login in the `admin` namespace, read of `admin/secret/data/api`).
   
   _Claude Code on behalf of oscerd_



##########
components-starter/camel-hashicorp-vault-starter/src/main/java/org/apache/camel/component/hashicorp/vault/springboot/SpringBootHashicorpVaultPropertiesParser.java:
##########
@@ -40,35 +48,62 @@ protected String getOverridePropertySourceName() {
 
     @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
+        HashicorpVaultConfiguration configuration = Binder.get(environment)
+                .bind(PREFIX, HashicorpVaultConfiguration.class)
+                .orElseGet(HashicorpVaultConfiguration::new);
+        if (isTokenLogin(configuration)) {
+            required(configuration.getToken(), "token", "Hashicorp Vault token 
is required");
+        }
+        required(configuration.getHost(), "host", "Hashicorp Vault host is 
required");
+        required(configuration.getPort(), "port", "Hashicorp Vault port is 
required");
+        required(configuration.getScheme(), "scheme", "Hashicorp Vault scheme 
is required");
         try {
-            port = Integer.parseInt(portValue);
+            Integer.parseInt(configuration.getPort());
         } catch (NumberFormatException e) {
             throw new RuntimeCamelException(
-                    "camel.vault.hashicorp.port must be a number but was: " + 
portValue, e);
+                    PREFIX + ".port must be a number but was: " + 
configuration.getPort(), e);
         }
 
-        VaultEndpoint vaultEndpoint = new VaultEndpoint();
-        vaultEndpoint.setHost(host);
-        vaultEndpoint.setPort(port);
-        vaultEndpoint.setScheme(scheme);
+        HashicorpVaultClient client;
+        try {
+            client = 
HashicorpVaultClient.create(HashicorpVaultClientSettings.fromConfiguration(configuration));

Review Comment:
   The parser now takes 
`HashicorpVaultClientSettings.fromEnvironmentOrConfiguration(configuration)`, 
the same settings as the function of the CamelContext: the 
`CAMEL_HASHICORP_VAULT_*` variables win when one of them is set. The host, port 
and scheme messages name both the property and the variable, and the class 
Javadoc and the Spring Boot section of the component docs say so. 
`fromConfiguration` is package-private again in apache/camel#29819.
   
   _Claude Code on behalf of oscerd_



##########
components-starter/camel-hashicorp-vault-starter/src/main/java/org/apache/camel/component/hashicorp/vault/springboot/SpringBootHashicorpVaultPropertiesParser.java:
##########
@@ -40,35 +48,62 @@ protected String getOverridePropertySourceName() {
 
     @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
+        HashicorpVaultConfiguration configuration = Binder.get(environment)
+                .bind(PREFIX, HashicorpVaultConfiguration.class)
+                .orElseGet(HashicorpVaultConfiguration::new);
+        if (isTokenLogin(configuration)) {
+            required(configuration.getToken(), "token", "Hashicorp Vault token 
is required");
+        }
+        required(configuration.getHost(), "host", "Hashicorp Vault host is 
required");
+        required(configuration.getPort(), "port", "Hashicorp Vault port is 
required");
+        required(configuration.getScheme(), "scheme", "Hashicorp Vault scheme 
is required");
         try {
-            port = Integer.parseInt(portValue);
+            Integer.parseInt(configuration.getPort());
         } catch (NumberFormatException e) {
             throw new RuntimeCamelException(
-                    "camel.vault.hashicorp.port must be a number but was: " + 
portValue, e);
+                    PREFIX + ".port must be a number but was: " + 
configuration.getPort(), e);
         }
 
-        VaultEndpoint vaultEndpoint = new VaultEndpoint();
-        vaultEndpoint.setHost(host);
-        vaultEndpoint.setPort(port);
-        vaultEndpoint.setScheme(scheme);
+        HashicorpVaultClient client;
+        try {
+            client = 
HashicorpVaultClient.create(HashicorpVaultClientSettings.fromConfiguration(configuration));
+        } catch (IllegalArgumentException e) {
+            throw new RuntimeCamelException(
+                    "Cannot create the Hashicorp Vault client of the " + 
PREFIX + " options: " + e.getMessage(), e);
+        }
+        return new EarlyResolutionFunction(client);

Review Comment:
   Fixed in apache/camel#29819: the client without a CamelContext 
(`HashicorpVaultClient.create(settings)`, now documented as a client for a 
short use) logs in once. When the login fails, the next requests fail with the 
same error without logging in again, so a stale secret ID costs one failed 
login per startup, whatever the number of placeholders. The clients of the 
CamelContext keep logging in again as before. Camel test: 
`aClientWithoutCamelContextLogsInOnce` (three reads after a failed AppRole 
login, one login; three logins without the change). The Vault docs confirm the 
lockout: it covers userpass, ldap and approle, 5 failed attempts lock the 
RoleID for 15 minutes by default.
   
   _Claude Code on behalf of oscerd_



##########
components-starter/camel-hashicorp-vault-starter/src/main/java/org/apache/camel/component/hashicorp/vault/springboot/SpringBootHashicorpVaultPropertiesParser.java:
##########
@@ -40,35 +48,62 @@ protected String getOverridePropertySourceName() {
 
     @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
+        HashicorpVaultConfiguration configuration = Binder.get(environment)
+                .bind(PREFIX, HashicorpVaultConfiguration.class)
+                .orElseGet(HashicorpVaultConfiguration::new);
+        if (isTokenLogin(configuration)) {
+            required(configuration.getToken(), "token", "Hashicorp Vault token 
is required");
+        }
+        required(configuration.getHost(), "host", "Hashicorp Vault host is 
required");
+        required(configuration.getPort(), "port", "Hashicorp Vault port is 
required");
+        required(configuration.getScheme(), "scheme", "Hashicorp Vault scheme 
is required");
         try {
-            port = Integer.parseInt(portValue);
+            Integer.parseInt(configuration.getPort());
         } catch (NumberFormatException e) {
             throw new RuntimeCamelException(
-                    "camel.vault.hashicorp.port must be a number but was: " + 
portValue, e);
+                    PREFIX + ".port must be a number but was: " + 
configuration.getPort(), e);
         }
 
-        VaultEndpoint vaultEndpoint = new VaultEndpoint();
-        vaultEndpoint.setHost(host);
-        vaultEndpoint.setPort(port);
-        vaultEndpoint.setScheme(scheme);
+        HashicorpVaultClient client;
+        try {
+            client = 
HashicorpVaultClient.create(HashicorpVaultClientSettings.fromConfiguration(configuration));

Review Comment:
   Documented in the Spring Boot section of the component docs 
(apache/camel#29819): 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 with 
`secret_id_num_uses=1` is used up by the early resolution and that later login 
fails.
   
   _Claude Code on behalf of oscerd_



##########
components-starter/camel-hashicorp-vault-starter/src/main/java/org/apache/camel/component/hashicorp/vault/springboot/SpringBootHashicorpVaultPropertiesParser.java:
##########
@@ -40,35 +48,62 @@ protected String getOverridePropertySourceName() {
 
     @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
+        HashicorpVaultConfiguration configuration = Binder.get(environment)
+                .bind(PREFIX, HashicorpVaultConfiguration.class)
+                .orElseGet(HashicorpVaultConfiguration::new);
+        if (isTokenLogin(configuration)) {
+            required(configuration.getToken(), "token", "Hashicorp Vault token 
is required");
+        }
+        required(configuration.getHost(), "host", "Hashicorp Vault host is 
required");
+        required(configuration.getPort(), "port", "Hashicorp Vault port is 
required");
+        required(configuration.getScheme(), "scheme", "Hashicorp Vault scheme 
is required");
         try {
-            port = Integer.parseInt(portValue);
+            Integer.parseInt(configuration.getPort());
         } catch (NumberFormatException e) {
             throw new RuntimeCamelException(
-                    "camel.vault.hashicorp.port must be a number but was: " + 
portValue, e);
+                    PREFIX + ".port must be a number but was: " + 
configuration.getPort(), e);
         }
 
-        VaultEndpoint vaultEndpoint = new VaultEndpoint();
-        vaultEndpoint.setHost(host);
-        vaultEndpoint.setPort(port);
-        vaultEndpoint.setScheme(scheme);
+        HashicorpVaultClient client;
+        try {
+            client = 
HashicorpVaultClient.create(HashicorpVaultClientSettings.fromConfiguration(configuration));

Review Comment:
   Fixed in apache/camel#29819: the client for a short use turns the token 
self-lookup off, so a token given for the token login is used as it is, as with 
the `VaultTemplate` before: no `lookup-self`, no renewal, never revoked. The 
other logins return their TTL with the token, so nothing changes for them. 
Camel test: `aClientWithoutCamelContextUsesAGivenTokenWithoutLookingItUp`, 
which fails without the change.
   
   _Claude Code on behalf of oscerd_



##########
components-starter/camel-hashicorp-vault-starter/src/main/java/org/apache/camel/component/hashicorp/vault/springboot/SpringBootHashicorpVaultPropertiesParser.java:
##########
@@ -40,35 +48,62 @@ protected String getOverridePropertySourceName() {
 
     @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
+        HashicorpVaultConfiguration configuration = Binder.get(environment)

Review Comment:
   Fixed: the parser binds with a placeholders resolver that calls 
`environment.resolveRequiredPlaceholders`, so an unresolvable `${VAULT_TOKEN}` 
fails at once, as `environment.getProperty` did: a `BindException` for the 
property, caused by `Could not resolve placeholder 'VAULT_TOKEN'`. Test: 
`anUnresolvablePlaceholderFailsAtOnce`, which fails with 
`Binder.get(environment)`.
   
   _Claude Code on behalf of oscerd_



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to