This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-27234-rest-openapi-http-login in repository https://gitbox.apache.org/repos/asf/camel.git
commit dc0113e269ecd23ff659d51f02ed25fabdb1ddbb Author: Claus Ibsen <[email protected]> AuthorDate: Thu Nov 26 16:46:18 2026 +0100 CAMEL-27234: camel-http - each rest-openapi producer uses its own login options, also when other producers call the same url The rest producer factory of camel-http looked up the http endpoint by its url alone, and gave the options of the producer (such as the login options given leniently on a rest-openapi endpoint) only as parameters for creating it. So all producers for the same url shared the endpoint of the first one created: a second rest-openapi endpoint of the same operation sent the login of the first one, or none if the first one had no login options. The options with a plain value are now part of the endpoint url, as on a plain http endpoint. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../camel/catalog/docs/rest-openapi-component.adoc | 10 ++ .../apache/camel/component/http/HttpComponent.java | 25 +++- .../http/HttpRestProducerParametersTest.java | 100 +++++++++++++++ .../src/main/docs/rest-openapi-component.adoc | 10 ++ .../rest/openapi/RestOpenApiHttpLoginTest.java | 139 +++++++++++++++++++++ .../src/test/resources/http-login.yaml | 33 +++++ .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 12 ++ 7 files changed, 326 insertions(+), 3 deletions(-) diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/rest-openapi-component.adoc b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/rest-openapi-component.adoc index cd086731507c..829df5410f70 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/rest-openapi-component.adoc +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/rest-openapi-component.adoc @@ -99,6 +99,16 @@ invocations, so it makes sense to use this feature only for parameters that are indeed constant for all invocations -- for example API version in path such as `/api/\{version}/users/\{id}`. +The endpoint parameters that are not REST operation's parameters are +passed to the endpoint of the delegate component. With camel-http, for +example, the login options of camel-http can be given on the +rest-openapi endpoint, such as +`authMethod=Bearer&authBearerToken=mytoken&authenticationPreemptive=true`, +`authMethod=Basic&authUsername=ann&authPassword=secret&authenticationPreemptive=true`, +or the `oauth2ClientId`, `oauth2ClientSecret` and `oauth2TokenEndpoint` options. +Each rest-openapi endpoint uses its own options, also when other +rest-openapi endpoints call the same operation. + // component options: START include::partial$component-configure-options.adoc[] include::partial$component-endpoint-options.adoc[] diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java index e38bbeda94c9..c2d02f6d3f77 100644 --- a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java +++ b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java @@ -804,14 +804,24 @@ public class HttpComponent extends HttpCommonComponent implements RestProducerFa } } - url = HttpUtil.recreateUrl(map, url); - parameters = parameters != null ? new HashMap<>(parameters) : new HashMap<>(); + Map<String, Object> componentProperties = (Map<String, Object>) parameters.remove("component"); + + // the options of this producer (such as the login options given on a rest-openapi endpoint) are part of + // the endpoint uri, as on a plain http endpoint, so producers with different options get their own endpoint, + // instead of all of them using the endpoint (and the login) of the first producer created for the same url + for (Map.Entry<String, Object> entry : parameters.entrySet()) { + if (isUriValue(entry.getValue())) { + map.put(entry.getKey(), entry.getValue()); + } + } + + url = HttpUtil.recreateUrl(map, url); // there are cases where we might end up here without component being created beforehand // we need to abide by the component properties specified in the parameters when creating // the component, one such case is when we switch from "http" to "https" component name - RestProducerFactoryHelper.setupComponentFor(url, camelContext, (Map<String, Object>) parameters.remove("component")); + RestProducerFactoryHelper.setupComponentFor(url, camelContext, componentProperties); HttpEndpoint endpoint = (HttpEndpoint) camelContext.getEndpoint(url, parameters); @@ -830,6 +840,15 @@ public class HttpComponent extends HttpCommonComponent implements RestProducerFa return endpoint.createProducer(); } + /** + * Whether the option value can be written in an endpoint uri. Other values (such as a bean instance) are still + * given to the endpoint, but are not part of its uri. + */ + private static boolean isUriValue(Object value) { + return value instanceof CharSequence || value instanceof Number || value instanceof Boolean + || value instanceof Character || value instanceof Enum<?>; + } + public HttpClientConfigurer getHttpClientConfigurer() { return httpClientConfigurer; } diff --git a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpRestProducerParametersTest.java b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpRestProducerParametersTest.java new file mode 100644 index 000000000000..765aadba35af --- /dev/null +++ b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpRestProducerParametersTest.java @@ -0,0 +1,100 @@ +/* + * 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.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; + +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpServer; +import org.apache.camel.Exchange; +import org.apache.camel.Producer; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.spi.RestConfiguration; +import org.apache.camel.support.DefaultExchange; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * The producers that rest components (such as rest-openapi) create for the same url, each with its own options, do not + * share one endpoint: each producer uses its own login (CAMEL-27234). + */ +public class HttpRestProducerParametersTest { + + private final AtomicReference<String> authorization = new AtomicReference<>(); + + private HttpServer server; + private DefaultCamelContext context; + + @BeforeEach + public void start() throws IOException { + server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0); + server.createContext("/api/items", this::handle); + server.start(); + + context = new DefaultCamelContext(); + context.start(); + } + + @AfterEach + public void stop() { + context.stop(); + server.stop(0); + } + + @Test + public void shouldUseTheLoginOfEachProducer() throws Exception { + Producer anonymous = createProducer(Map.of()); + Producer basic = createProducer(Map.of("authMethod", "Basic", "authUsername", "ann", "authPassword", "secret", + "authenticationPreemptive", "true")); + Producer bearer = createProducer(Map.of("authMethod", "Bearer", "authBearerToken", "TOKEN", + "authenticationPreemptive", "true")); + + assertThat(send(anonymous)).isNull(); + assertThat(send(basic)).isEqualTo("Basic YW5uOnNlY3JldA=="); + assertThat(send(bearer)).isEqualTo("Bearer TOKEN"); + } + + private Producer createProducer(Map<String, Object> parameters) throws Exception { + HttpComponent http = context.getComponent("http", HttpComponent.class); + Producer producer = http.createProducer(context, "http://localhost:" + server.getAddress().getPort(), "GET", "api", + "items", null, null, null, new RestConfiguration(), parameters); + producer.start(); + return producer; + } + + private String send(Producer producer) throws Exception { + authorization.set(null); + Exchange exchange = new DefaultExchange(context); + producer.process(exchange); + assertThat(exchange.getException()).isNull(); + assertThat(exchange.getMessage().getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class)).isEqualTo(200); + return authorization.get(); + } + + private void handle(HttpExchange exchange) throws IOException { + authorization.set(exchange.getRequestHeaders().getFirst("Authorization")); + exchange.sendResponseHeaders(200, -1); + exchange.close(); + } +} diff --git a/components/camel-rest-openapi/src/main/docs/rest-openapi-component.adoc b/components/camel-rest-openapi/src/main/docs/rest-openapi-component.adoc index cd086731507c..829df5410f70 100644 --- a/components/camel-rest-openapi/src/main/docs/rest-openapi-component.adoc +++ b/components/camel-rest-openapi/src/main/docs/rest-openapi-component.adoc @@ -99,6 +99,16 @@ invocations, so it makes sense to use this feature only for parameters that are indeed constant for all invocations -- for example API version in path such as `/api/\{version}/users/\{id}`. +The endpoint parameters that are not REST operation's parameters are +passed to the endpoint of the delegate component. With camel-http, for +example, the login options of camel-http can be given on the +rest-openapi endpoint, such as +`authMethod=Bearer&authBearerToken=mytoken&authenticationPreemptive=true`, +`authMethod=Basic&authUsername=ann&authPassword=secret&authenticationPreemptive=true`, +or the `oauth2ClientId`, `oauth2ClientSecret` and `oauth2TokenEndpoint` options. +Each rest-openapi endpoint uses its own options, also when other +rest-openapi endpoints call the same operation. + // component options: START include::partial$component-configure-options.adoc[] include::partial$component-endpoint-options.adoc[] diff --git a/components/camel-rest-openapi/src/test/java/org/apache/camel/component/rest/openapi/RestOpenApiHttpLoginTest.java b/components/camel-rest-openapi/src/test/java/org/apache/camel/component/rest/openapi/RestOpenApiHttpLoginTest.java new file mode 100644 index 000000000000..e72aadfd363a --- /dev/null +++ b/components/camel-rest-openapi/src/test/java/org/apache/camel/component/rest/openapi/RestOpenApiHttpLoginTest.java @@ -0,0 +1,139 @@ +/* + * 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.rest.openapi; + +import java.util.Objects; + +import com.github.tomakehurst.wiremock.WireMockServer; +import org.apache.camel.CamelContext; +import org.apache.camel.RoutesBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.DefaultCamelContext; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor; +import static com.github.tomakehurst.wiremock.client.WireMock.post; +import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; + +/** + * The camel-http login options given on the rest-openapi endpoint reach the HTTP client, also when other rest-openapi + * endpoints of the same operation have other login options, or none (CAMEL-27234). + */ +public class RestOpenApiHttpLoginTest extends ManagedCamelTestSupport { + + public static WireMockServer server = new WireMockServer(wireMockConfig().dynamicPort()); + + @BeforeAll + public static void startWireMockServer() { + server.start(); + server.stubFor(get(urlEqualTo("/api/items")) + .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody("{}"))); + server.stubFor(post(urlEqualTo("/oauth/token")) + .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json") + .withBody("{\"access_token\":\"oauth-token\",\"token_type\":\"Bearer\",\"expires_in\":3600}"))); + } + + @AfterAll + public static void stopWireMockServer() { + server.stop(); + } + + @BeforeEach + public void resetWireMock() throws Exception { + server.resetRequests(); + initializeContextForComponent("http"); + } + + @Test + public void shouldNotSendLoginOfOtherEndpoints() { + template.sendBody("direct:anonymous", null); + + server.verify(getRequestedFor(urlEqualTo("/api/items")).withoutHeader("Authorization")); + } + + @Test + public void shouldSendPreemptiveBasicAuthentication() { + template.sendBody("direct:basic", null); + + server.verify(getRequestedFor(urlEqualTo("/api/items")) + .withHeader("Authorization", equalTo("Basic YW5uOnNlY3JldA=="))); + } + + @Test + public void shouldSendBearerToken() { + template.sendBody("direct:bearer", null); + + server.verify(getRequestedFor(urlEqualTo("/api/items")) + .withHeader("Authorization", equalTo("Bearer TOKEN"))); + } + + @Test + public void shouldSendOAuth2Token() { + template.sendBody("direct:oauth2", null); + + server.verify(postRequestedFor(urlEqualTo("/oauth/token"))); + server.verify(getRequestedFor(urlEqualTo("/api/items")) + .withHeader("Authorization", equalTo("Bearer oauth-token"))); + } + + @Override + protected CamelContext createCamelContext(String componentName) { + final CamelContext camelContext = new DefaultCamelContext(); + + final RestOpenApiComponent component = new RestOpenApiComponent(); + component.setComponentName(componentName); + component.setHost("http://localhost:" + server.port()); + component.setBasePath("/api"); + component.setSpecificationUri(Objects + .requireNonNull(RestOpenApiHttpLoginTest.class.getResource("/http-login.yaml")) + .toString()); + + camelContext.addComponent("items", component); + + return camelContext; + } + + @Override + protected RoutesBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:anonymous") + .to("items:listItems"); + + from("direct:basic") + .to("items:listItems?authMethod=Basic&authUsername=ann&authPassword=secret" + + "&authenticationPreemptive=true"); + + from("direct:bearer") + .to("items:listItems?authMethod=Bearer&authBearerToken=TOKEN&authenticationPreemptive=true"); + + from("direct:oauth2") + .to("items:listItems?oauth2ClientId=cid&oauth2ClientSecret=csecret" + + "&oauth2TokenEndpoint=http://localhost:" + server.port() + "/oauth/token"); + } + }; + } +} diff --git a/components/camel-rest-openapi/src/test/resources/http-login.yaml b/components/camel-rest-openapi/src/test/resources/http-login.yaml new file mode 100644 index 000000000000..28deb90f1ca6 --- /dev/null +++ b/components/camel-rest-openapi/src/test/resources/http-login.yaml @@ -0,0 +1,33 @@ +# +# 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. +# + +openapi: 3.0.0 +info: + title: HTTP login test API + version: 1.0.0 +paths: + /items: + get: + summary: Lists the items + operationId: listItems + responses: + '200': + description: The items + content: + application/json: + schema: + type: object diff --git a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc index 38b8b68c3feb..22988fb39d24 100644 --- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc +++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc @@ -1597,6 +1597,18 @@ is now sent with the first request; before, the request failed with `IllegalStat when `authDomain` is not set. Before, the endpoint answered only Basic and Digest challenges with these credentials, and the component did not use them. If the server asks for Basic credentials, remove `authMethod=NTLM`. +=== camel-http - the producer of a rest-openapi endpoint uses its own options + +The http endpoint that camel-http creates for a rest producer, such as a `rest-openapi` producer, now has the options +of that producer in its uri, such as the login options (`authMethod`, `authUsername`, `authPassword`, +`authBearerToken`, `authenticationPreemptive` and the `oauth2*` options) given on a `rest-openapi` endpoint. Producers +that call the same url with different options get their own http endpoint. + +Before, all producers for the same url used the http endpoint of the first one created, and its options: for example a +second `rest-openapi` endpoint of the same operation sent the login of the first one, or none if the first one had +no login options. The http endpoints of these producers now show their options in the endpoint list, with secret +values masked. + === camel-platform-http-main, camel-netty-http, camel-undertow - the Authorization header is removed after the login is checked When Camel checks the credential of an incoming request, the `Authorization` header of the request is now removed from
