davsclaus commented on code in PR #26500:
URL: https://github.com/apache/camel/pull/26500#discussion_r4061049870
##########
docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_23.adoc:
##########
@@ -2240,3 +2240,17 @@ The `maxRetryTimeout` endpoint and component option is
deprecated in both `camel
it had no effect. The option is kept for backward compatibility of existing
endpoint URIs but is marked
deprecated and will be removed in a future release. Routes that set
`maxRetryTimeout` can simply drop it;
behaviour is unchanged.
+
+=== camel-http - HttpComponent now implements SecretRotationAware
Review Comment:
The upgrade guide is for migration only (changed defaults, removed options,
renamed things); new features go in the component docs. This section also
states a capability camel-http already had before this PR, since endpoints are
rebuilt on reload. Suggest dropping it, and if a note is wanted, putting a
sentence in `http-component.adoc` instead.
##########
components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java:
##########
@@ -586,6 +588,30 @@ protected Endpoint createEndpoint(String uri, String
remaining, Map<String, Obje
return endpoint;
}
+ /**
+ * Notifies this component that a secret has been rotated.
+ * <p>
+ * The component options ({@code authUsername}, {@code authPassword},
{@code proxyAuthUsername},
+ * {@code proxyAuthPassword}, etc.) have already been re-applied with the
newly resolved secret values before this
+ * callback fires. No further action is taken here.
+ *
+ * @implNote This method is intentionally a no-op beyond logging. The
route restart that follows this callback —
+ * triggered by {@code
DefaultContextReloadStrategy.reloadRoutes()} — stops the active routes, shuts
down
+ * their endpoints, and recreates them from the updated
component fields, so subsequent requests will use
+ * the new credentials. Re-authenticating inside this method
would be wrong: the callback fires
+ * <em>before</em> {@code reloadRoutes()}, so any connection
re-established here would be closed again
+ * immediately by the route shutdown. The shared
+ * {@link
org.apache.hc.client5.http.io.HttpClientConnectionManager} is preserved across
the restart
+ * because {@code HttpEndpoint.createHttpClient()} marks it as
shared when it belongs to the component,
+ * letting the pool drain naturally rather than being closed
abruptly.
+ */
+ @Override
+ public void onSecretRotation(Object source) throws Exception {
Review Comment:
Since this is a no-op beyond logging, and the `@implNote` itself explains
that the route restart is what refreshes the credentials, the component does
not actually need the SPI - the refresh happens whether or not this method
exists. Implementing it makes `HttpComponent` show up as a component that
re-authenticates in place, which it does not (and does not need to).
##########
components/camel-http/src/test/java/org/apache/camel/component/http/HttpComponentSecretRotationAwareTest.java:
##########
@@ -0,0 +1,187 @@
+/*
+ * 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.http;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.http.handler.AuthenticationValidationHandler;
+import org.apache.camel.component.http.interceptor.RequestBasicAuth;
+import org.apache.camel.component.http.interceptor.ResponseBasicUnauthorized;
+import org.apache.camel.http.common.HttpConfiguration;
+import org.apache.camel.spi.SecretRotationAware;
+import org.apache.hc.core5.http.HttpRequestInterceptor;
+import org.apache.hc.core5.http.HttpResponseInterceptor;
+import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.impl.bootstrap.HttpServer;
+import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap;
+import org.apache.hc.core5.http.protocol.DefaultHttpProcessor;
+import org.apache.hc.core5.http.protocol.HttpProcessor;
+import org.apache.hc.core5.http.protocol.RequestValidateHost;
+import org.apache.hc.core5.http.protocol.ResponseContent;
+import org.junit.jupiter.api.Test;
+
+import static org.apache.camel.component.http.HttpMethods.GET;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+/**
+ * Tests that {@link HttpComponent} implements {@link SecretRotationAware} and
that calling
+ * {@link HttpComponent#onSecretRotation(Object)} does not throw and that
fresh credentials are picked up when routes
+ * are restarted after the rotation.
+ */
+class HttpComponentSecretRotationAwareTest extends BaseHttpTest {
+
+ private HttpServer localServer;
+
+ /** Server-side expected credentials: mutable so we can "rotate" them
during the test. */
+ private final AtomicReference<String[]> expectedCreds = new
AtomicReference<>(new String[] { "alice", "secret1" });
+
+ @Override
+ public void setupResources() throws Exception {
+ localServer = ServerBootstrap.bootstrap()
+ .setCanonicalHostName("localhost")
+ .setHttpProcessor(getBasicHttpProcessor())
+ .setConnectionReuseStrategy(getConnectionReuseStrategy())
+ .setResponseFactory(getHttpResponseFactory())
+ .setSslContext(getSSLContext())
+ .register("/api", (request, response, ctx) -> {
+ String[] creds = expectedCreds.get();
+ new AuthenticationValidationHandler(
+ GET.name(), null, null, getExpectedContent(),
+ creds[0], creds[1])
+ .handle(request, response, ctx);
+ })
+ .create();
+ localServer.start();
+ }
+
+ @Override
+ public void cleanupResources() throws Exception {
+ if (localServer != null) {
+ localServer.stop();
+ }
+ }
+
+ @Override
+ protected HttpProcessor getBasicHttpProcessor() {
+ List<HttpRequestInterceptor> requestInterceptors = new ArrayList<>();
+ requestInterceptors.add(new RequestValidateHost());
+ requestInterceptors.add(new RequestBasicAuth());
+ List<HttpResponseInterceptor> responseInterceptors = new ArrayList<>();
+ responseInterceptors.add(new ResponseContent());
+ responseInterceptors.add(new ResponseBasicUnauthorized());
+ return new DefaultHttpProcessor(requestInterceptors,
responseInterceptors);
+ }
+
+ @Test
+ void httpComponentImplementsSecretRotationAware() {
+ HttpComponent component = context.getComponent("http",
HttpComponent.class);
+ assertNotNull(component);
+ assertInstanceOf(SecretRotationAware.class, component,
+ "HttpComponent must implement SecretRotationAware");
+ }
+
+ @Test
+ void onSecretRotationDoesNotThrow() throws Exception {
+ HttpComponent component = context.getComponent("http",
HttpComponent.class);
+ // Must not throw regardless of current state
+ component.onSecretRotation("unit-test-source");
+ component.onSecretRotation(null);
+ }
+
+ /**
+ * Simulates the full rotation lifecycle: configure component with old
credentials → send request (succeeds) →
+ * rotate secret (update component + server expectation) → call
onSecretRotation() → restart routes → send request
+ * again (must succeed with new credentials because endpoints were
recreated).
+ */
+ @Test
+ void credentialsArePickedUpAfterRotationAndRouteRestart() throws Exception
{
+ HttpComponent component = context.getComponent("http",
HttpComponent.class);
+
+ // Configure the component with initial credentials via
HttpConfiguration
+ HttpConfiguration config = new HttpConfiguration();
+ config.setAuthMethod("Basic");
+ config.setAuthUsername("alice");
+ config.setAuthPassword("secret1");
+ component.setHttpConfiguration(config);
+ expectedCreds.set(new String[] { "alice", "secret1" });
+
+ String serverUrl = "http://localhost:" + localServer.getLocalPort() +
"/api";
+
+ // Add a route that hits the authenticated endpoint
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("direct:secured").routeId("secured-route")
+ .to(serverUrl);
+ }
+ });
+
+ // Phase 1: initial credentials work
+ Exchange ex1 = template.request("direct:secured", e -> {
+ });
+ assertNotNull(ex1);
+ assertNull(ex1.getException(), "Initial request should succeed");
+ assertEquals(HttpStatus.SC_OK,
ex1.getMessage().getHeader(Exchange.HTTP_RESPONSE_CODE));
+
+ // Phase 2: rotate — update both the server expectation and the
component configuration.
+ // In the real CAMEL-24636 flow, reloadComponentProperties()
re-applies the placeholders first,
+ // then onSecretRotation() is called, then reloadAllRoutes() clears
the endpoint registry
+ // and restarts all route definitions with fresh endpoints.
+ expectedCreds.set(new String[] { "alice", "secret2" });
+
+ // Negative assertion: the server now requires secret2, so the
existing route (still using secret1)
+ // must be rejected. This guards against a false-positive in Phase 3 —
if this assertion fails, the
+ // server never actually enforced the credential change and Phase 3
would pass vacuously.
+ Exchange exRejected = template.request("direct:secured", e -> {
+ });
+ assertNotNull(exRejected);
+ assertEquals(HttpStatus.SC_UNAUTHORIZED,
+ exRejected.getMessage().getHeader(Exchange.HTTP_RESPONSE_CODE),
+ "Old credentials must be rejected after server-side secret
rotation");
+
+ config.setAuthPassword("secret2");
+ component.onSecretRotation("vault-rotation-test");
Review Comment:
I removed this line and the test still passes (3/3), because the reload
simulation below is what makes Phase 3 succeed. So the test proves that
camel-http picks up new credentials on a route reload - which is true on `main`
today - rather than anything about `onSecretRotation`.
--
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]