davsclaus commented on code in PR #26845:
URL: https://github.com/apache/camel/pull/26845#discussion_r4114649321


##########
core/camel-support/src/main/java/org/apache/camel/support/DataSourceHelper.java:
##########
@@ -0,0 +1,82 @@
+/*
+ * 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.support;
+
+import java.lang.reflect.Method;
+
+import javax.sql.DataSource;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Utility methods for working with JDBC {@link DataSource} instances.
+ */
+public final class DataSourceHelper {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(DataSourceHelper.class);
+
+    private DataSourceHelper() {
+    }
+
+    /**
+     * Evicts stale connections from the given DataSource so that the pool 
rebuilds them with the rotated credentials.
+     * <p/>
+     * HikariCP is tried first via reflection (so the caller does not need a 
compile-time dependency on it). Any
+     * DataSource that does not expose {@code getHikariPoolMXBean()} is left 
untouched — the pool will pick up the new
+     * credentials on its own reconnect cycle when existing connections expire.
+     * <p/>
+     * <b>Important:</b> this method only evicts existing connections from the 
pool. It does <em>not</em> update the
+     * pool's credentials. For pools configured with a static password (e.g. 
Spring Boot
+     * {@code spring.datasource.password}, Quarkus {@code 
quarkus.datasource.jdbc.url}), the pool will re-open

Review Comment:
   `quarkus.datasource.jdbc.url` is the URL, not the password 
(`quarkus.datasource.password`). Quarkus also uses Agroal by default, so this 
Hikari eviction does not apply there. Line 37 ("so that the pool rebuilds them 
with the rotated credentials") contradicts this paragraph.



##########
core/camel-support/src/main/java/org/apache/camel/support/DataSourceHelper.java:
##########
@@ -0,0 +1,82 @@
+/*
+ * 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.support;
+
+import java.lang.reflect.Method;
+
+import javax.sql.DataSource;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Utility methods for working with JDBC {@link DataSource} instances.
+ */
+public final class DataSourceHelper {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(DataSourceHelper.class);
+
+    private DataSourceHelper() {
+    }
+
+    /**
+     * Evicts stale connections from the given DataSource so that the pool 
rebuilds them with the rotated credentials.
+     * <p/>
+     * HikariCP is tried first via reflection (so the caller does not need a 
compile-time dependency on it). Any
+     * DataSource that does not expose {@code getHikariPoolMXBean()} is left 
untouched — the pool will pick up the new
+     * credentials on its own reconnect cycle when existing connections expire.
+     * <p/>
+     * <b>Important:</b> this method only evicts existing connections from the 
pool. It does <em>not</em> update the
+     * pool's credentials. For pools configured with a static password (e.g. 
Spring Boot
+     * {@code spring.datasource.password}, Quarkus {@code 
quarkus.datasource.jdbc.url}), the pool will re-open
+     * connections using the <em>old</em> credentials unless the credentials 
provider resolves them dynamically (e.g.
+     * {@code HikariCredentialsProvider}, the AWS JDBC wrapper secrets plugin, 
or a custom {@code DataSource} that
+     * fetches credentials from a vault at connect time).
+     *
+     * @param ds     the DataSource whose connections should be evicted
+     * @param source an opaque label for the rotation event (used in log 
messages only)
+     */
+    public static void evictDataSourceConnections(DataSource ds, Object 
source) {
+        // HikariCP: softEvictConnections() is defined on HikariPoolMXBean, 
not on HikariDataSource directly.
+        // We retrieve the MXBean via getHikariPoolMXBean() (a public method 
on HikariDataSource) using reflection
+        // so that the caller does not need a compile-time dependency on 
HikariCP.
+        try {
+            Method getPoolMXBean = 
ds.getClass().getMethod("getHikariPoolMXBean");
+            Object poolMXBean = getPoolMXBean.invoke(ds);
+            if (poolMXBean != null) {
+                Method softEvict = 
poolMXBean.getClass().getMethod("softEvictConnections");
+                softEvict.invoke(poolMXBean);
+                LOG.info("Secret rotation (source={}): HikariCP 
softEvictConnections() called on {}", source, ds);
+            } else {
+                LOG.debug("Secret rotation (source={}): HikariCP pool on {} is 
not started yet; nothing to evict",
+                        source, ds);
+            }
+            return;
+        } catch (NoSuchMethodException e) {
+            // Not a HikariCP DataSource — fall through to generic handling
+        } catch (Exception e) {
+            LOG.warn("Secret rotation (source={}): softEvictConnections() 
failed on {}: {}", source, ds, e.getMessage());

Review Comment:
   After this WARN the method falls through to the INFO saying the DataSource 
"does not support HikariCP pool eviction", which is misleading here. 
`e.getMessage()` of an InvocationTargetException is also usually null. Suggest 
logging the cause and returning.



##########
components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlComponent.java:
##########
@@ -151,6 +153,13 @@ protected Endpoint createEndpoint(String uri, String 
remaining, Map<String, Obje
         return endpoint;
     }
 
+    @Override
+    public void onSecretRotation(Object source) throws Exception {
+        if (this.dataSource != null) {

Review Comment:
   This covers the autowired or component-configured DataSource only. An 
endpoint `dataSource=#myDs`, or a setup with more than one DataSource (no 
autowiring), is not evicted. Either collect the endpoints' DataSources or 
document the limitation.



##########
components/camel-jdbc/src/main/docs/jdbc-component.adoc:
##########
@@ -336,3 +336,19 @@ from("timer://MoveNewCustomersEveryHour?period=3600000")
         .setBody(simple("insert into processed_customer 
values('${body[ID]}','${body[NAME]}')"))
         .to("jdbc:testdb");
 ----
+
+== Secret Rotation
+
+The JDBC component implements `SecretRotationAware`. When a secret rotation 
event is triggered
+(e.g. by a vault provider), the component evicts stale connections from the 
component-level
+DataSource's connection pool so that new connections are created with the 
updated credentials.

Review Comment:
   "so that new connections are created with the updated credentials" and line 
347 ("other pool implementations will pick up the new credentials") only hold 
when credentials are resolved dynamically. Suggest rewording so it agrees with 
the IMPORTANT note below. Same in sql-component.adoc.



##########
components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcComponent.java:
##########
@@ -114,6 +116,13 @@ public void setConnectionStrategy(ConnectionStrategy 
connectionStrategy) {
         this.connectionStrategy = connectionStrategy;
     }
 
+    @Override
+    public void onSecretRotation(Object source) throws Exception {
+        if (this.dataSource != null) {

Review Comment:
   `dataSource` here is not autowired, and `createEndpoint` passes a looked-up 
or default DataSource to the endpoint without setting this field. So with 
`jdbc:myDs` / `jdbc:dataSource` it stays null and nothing is evicted. Could we 
also collect the DataSources of this component's endpoints? They are still 
registered, since notifySecretRotation runs before reloadRoutes.



-- 
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