gnodet-bot commented on code in PR #26500: URL: https://github.com/apache/camel/pull/26500#discussion_r4034543566
########## 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: ⚠️ **Missing negative assertion — test passes vacuously without it.** At this point the server already requires `secret2` (set two lines above). Nothing in the test verifies that the old credentials (`secret1`) are now rejected. Without that step, the test would pass even if `expectedCreds.set()` had no effect — the subsequent Phase 3 request would still succeed because the endpoint might still be sending `secret1` (which the server now rejects), but the test only asserts Phase 3 success, not that Phase 2 with old credentials fails. Add a probe request here, before the route restart, using the still-live endpoint (which carries `secret1`), and assert it gets HTTP 401. That proves the server-side rotation actually took effect and that the old credentials are dead: ```java // Prove the server now rejects the OLD credentials before we restart the route 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 rotation"); ``` ########## 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: 📝 **Missing upgrade guide / component docs entry.** `SecretRotationAware` was added in 4.23 (`@since 4.23` on the SPI). This PR makes `HttpComponent` the first built-in HTTP component to implement it — users who configure `camel-http` with vault-backed auth credentials need to know this now works without a full context restart. Please add a brief entry to `docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_23.adoc` (or wherever CAMEL-24636/4.23 new-features are documented) noting that `camel-http`'s `HttpComponent` now implements `SecretRotationAware` and that vault-rotated credentials are picked up automatically on the next route restart after a vault reload event. ########## 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: 💡 **Consider converting the block comment to `@implNote` Javadoc.** The `SecretRotationAware` Javadoc says implementations should "re-establish the authenticated resource." Someone reading only the method signature — without the surrounding comment — will assume the no-op is a bug and add explicit credential-refresh code, breaking the ordering contract (the callback fires *before* `reloadRoutes()`, so re-authenticating here against endpoints that are still running is wrong). A `@implNote` in the Javadoc makes the intentional no-op explicit and visible in IDEs: ```suggestion /** * {@inheritDoc} * * @implNote This implementation intentionally does nothing beyond logging. The component * options (authUsername, authPassword, proxyAuthUsername, proxyAuthPassword, etc.) have * already been re-applied with newly resolved secret values by * {@code DefaultContextReloadStrategy.reloadComponentProperties()} before this callback * fires. The route restart that immediately follows ({@code reloadRoutes()}) stops the * active routes, clears the endpoint registry, and recreates all endpoints from the * updated component fields — so the new credentials reach the wire without any explicit * action here. The shared {@code PoolingHttpClientConnectionManager} is preserved across * the restart because {@code HttpEndpoint.createHttpClient()} marks it as shared, letting * the pool drain naturally rather than being closed abruptly. */ @Override public void onSecretRotation(Object source) throws Exception { LOG.info("Secret rotation triggered (source={}): HTTP component credentials have been updated; " + "endpoints will be rebuilt with the new credentials on the next route start", source); } ``` -- 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]
