gnodet commented on code in PR #26500: URL: https://github.com/apache/camel/pull/26500#discussion_r4047409998
########## components/camel-http/src/test/java/org/apache/camel/component/http/HttpComponentSecretRotationAwareTest.java: ########## @@ -0,0 +1,176 @@ +/* + * 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" }); + config.setAuthPassword("secret2"); + component.onSecretRotation("vault-rotation-test"); Review Comment: Fixed in 04f05050c05fc5e33a1ca9c63e4ff6bd0186e6dd. Added a negative assertion between setting `expectedCreds` to `secret2` and updating the component config — verifies that the existing route (still using `secret1`) gets a 401, proving the server-side rotation was actually enforced before Phase 3 runs. ########## components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java: ########## @@ -586,6 +588,21 @@ protected Endpoint createEndpoint(String uri, String remaining, Map<String, Obje return endpoint; } + @Override + public void onSecretRotation(Object source) throws Exception { + // The component options (authUsername, authPassword, proxyAuthUsername, proxyAuthPassword, etc.) + // have already been re-applied with the newly resolved secret values before this callback fires. + // Log the rotation event. The route restart that follows this callback (triggered by + // DefaultContextReloadStrategy.reloadRoutes()) will stop the active routes, shut down their + // endpoints, and recreate them from the updated component fields — so subsequent requests will + // use the new credentials. The shared PoolingHttpClientConnectionManager is preserved across + // the restart because HttpEndpoint.createHttpClient() marks it as shared when it belongs to + // the component, letting the pool drain naturally rather than being closed abruptly. Review Comment: Fixed in 04f05050c05fc5e33a1ca9c63e4ff6bd0186e6dd. Converted the block comment to a proper Javadoc with `@implNote` explaining the ordering contract (callback fires before `reloadRoutes()`, so re-authenticating here would be immediately undone). ########## components/camel-http/src/test/java/org/apache/camel/component/http/HttpComponentSecretRotationAwareTest.java: ########## @@ -0,0 +1,176 @@ +/* + * 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" }); + config.setAuthPassword("secret2"); + component.onSecretRotation("vault-rotation-test"); + + // Simulate reloadAllRoutes(): remove the route, clear endpoint registry, then restart. + // This replicates InternalRouteController.reloadAllRoutes() which explicitly clears the + // endpoint registry so that fresh endpoints are created from the updated component config. + context.getRouteController().stopRoute("secured-route"); + context.removeRoute("secured-route"); + context.getEndpointRegistry().clear(); + context.addRoutes(new RouteBuilder() { + @Override + public void configure() { + from("direct:secured").routeId("secured-route") + .to(serverUrl); + } + }); + + // Phase 3: the restarted route creates a new endpoint from the updated HttpConfiguration + Exchange ex2 = template.request("direct:secured", e -> { + }); + assertNotNull(ex2); + assertNull(ex2.getException(), "Request after rotation should succeed with new credentials"); + assertEquals(HttpStatus.SC_OK, ex2.getMessage().getHeader(Exchange.HTTP_RESPONSE_CODE)); Review Comment: Fixed in 04f05050c05fc5e33a1ca9c63e4ff6bd0186e6dd. Added an entry to `camel-4x-upgrade-guide-4_23.adoc` describing the new `SecretRotationAware` support in `HttpComponent` and how to use it with vault-backed property placeholders. -- 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]
