This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-27664-weather-appid in repository https://gitbox.apache.org/repos/asf/camel.git
commit 032901abad3167e6f6702bdfc70a874ff5553cd9 Author: Claus Ibsen <[email protected]> AuthorDate: Fri Nov 27 16:23:33 2026 +0100 CAMEL-27664: camel-weather - the CamelWeatherQuery header and the log have the appid masked The consumer and the producer put the request URL, with the APPID query parameter, in the CamelWeatherQuery header, the debug and warn log and the error messages. The appid is a secret option, so these now show APPID=xxxxxx; the request still sends the real appid. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../apache/camel/catalog/components/weather.json | 2 +- .../apache/camel/component/weather/weather.json | 2 +- .../camel/component/weather/WeatherConstants.java | 2 +- .../camel/component/weather/WeatherConsumer.java | 8 +- .../camel/component/weather/WeatherProducer.java | 5 +- .../camel/component/weather/WeatherQuery.java | 20 +++ .../component/weather/WeatherAppidMaskedTest.java | 142 +++++++++++++++++++++ .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 6 + .../dsl/WeatherEndpointBuilderFactory.java | 3 +- 9 files changed, 181 insertions(+), 9 deletions(-) diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/weather.json b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/weather.json index 06aac3096b3c..95fabb25d068 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/weather.json +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/weather.json @@ -32,7 +32,7 @@ }, "headers": { "CamelWeatherLocation": { "index": 0, "kind": "header", "displayName": "", "group": "producer", "label": "producer", "required": false, "javaType": "String", "deprecated": false, "autowired": false, "secret": false, "description": "The location to query, in the format of the location option, which this header replaces for the producer. The producer ignores it when lat and lon, zip or ids are set, and current or an empty value looks up the location.", "constantName": "org.apache.camel [...] - "CamelWeatherQuery": { "index": 1, "kind": "header", "displayName": "", "group": "common", "label": "", "required": false, "javaType": "String", "deprecated": false, "autowired": false, "secret": false, "description": "The URL of the request that the consumer or the producer sent to OpenWeatherMap, with its query parameters.", "constantName": "org.apache.camel.component.weather.WeatherConstants#WEATHER_QUERY" } + "CamelWeatherQuery": { "index": 1, "kind": "header", "displayName": "", "group": "common", "label": "", "required": false, "javaType": "String", "deprecated": false, "autowired": false, "secret": false, "description": "The URL of the request that the consumer or the producer sent to OpenWeatherMap, with its query parameters. The value of the APPID parameter is masked as xxxxxx.", "constantName": "org.apache.camel.component.weather.WeatherConstants#WEATHER_QUERY" } }, "bodyTypes": { "consumerOut": { "carriers": [ "text" ], "javaType": "java.lang.String", "format": "json", "dependsOn": [ "mode", "headerName" ], "description": "The OpenWeatherMap response as a String: JSON, or XML or HTML with mode. With headerName the response is in that header and the body is null" }, diff --git a/components/camel-weather/src/generated/resources/META-INF/org/apache/camel/component/weather/weather.json b/components/camel-weather/src/generated/resources/META-INF/org/apache/camel/component/weather/weather.json index 06aac3096b3c..95fabb25d068 100644 --- a/components/camel-weather/src/generated/resources/META-INF/org/apache/camel/component/weather/weather.json +++ b/components/camel-weather/src/generated/resources/META-INF/org/apache/camel/component/weather/weather.json @@ -32,7 +32,7 @@ }, "headers": { "CamelWeatherLocation": { "index": 0, "kind": "header", "displayName": "", "group": "producer", "label": "producer", "required": false, "javaType": "String", "deprecated": false, "autowired": false, "secret": false, "description": "The location to query, in the format of the location option, which this header replaces for the producer. The producer ignores it when lat and lon, zip or ids are set, and current or an empty value looks up the location.", "constantName": "org.apache.camel [...] - "CamelWeatherQuery": { "index": 1, "kind": "header", "displayName": "", "group": "common", "label": "", "required": false, "javaType": "String", "deprecated": false, "autowired": false, "secret": false, "description": "The URL of the request that the consumer or the producer sent to OpenWeatherMap, with its query parameters.", "constantName": "org.apache.camel.component.weather.WeatherConstants#WEATHER_QUERY" } + "CamelWeatherQuery": { "index": 1, "kind": "header", "displayName": "", "group": "common", "label": "", "required": false, "javaType": "String", "deprecated": false, "autowired": false, "secret": false, "description": "The URL of the request that the consumer or the producer sent to OpenWeatherMap, with its query parameters. The value of the APPID parameter is masked as xxxxxx.", "constantName": "org.apache.camel.component.weather.WeatherConstants#WEATHER_QUERY" } }, "bodyTypes": { "consumerOut": { "carriers": [ "text" ], "javaType": "java.lang.String", "format": "json", "dependsOn": [ "mode", "headerName" ], "description": "The OpenWeatherMap response as a String: JSON, or XML or HTML with mode. With headerName the response is in that header and the body is null" }, diff --git a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConstants.java b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConstants.java index 8f93af48b372..a258f892e0d5 100644 --- a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConstants.java +++ b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConstants.java @@ -29,7 +29,7 @@ public final class WeatherConstants { javaType = "String") public static final String WEATHER_LOCATION = "CamelWeatherLocation"; @Metadata(description = "The URL of the request that the consumer or the producer sent to OpenWeatherMap," - + " with its query parameters.", + + " with its query parameters. The value of the APPID parameter is masked as xxxxxx.", javaType = "String") public static final String WEATHER_QUERY = "CamelWeatherQuery"; diff --git a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConsumer.java b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConsumer.java index 3b9bc75afc7e..553230911354 100644 --- a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConsumer.java +++ b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConsumer.java @@ -52,7 +52,9 @@ public class WeatherConsumer extends ScheduledPollConsumer { @Override protected int poll() throws Exception { - LOG.debug("Going to execute the Weather query {}", query); + // the query with the appid masked, for the header, the log and error messages + String sanitizedQuery = WeatherQuery.sanitize(query); + LOG.debug("Going to execute the Weather query {}", sanitizedQuery); HttpClient httpClient = getEndpoint().getConfiguration().getHttpClient(); HttpGet getMethod = new HttpGet(query); try { @@ -63,7 +65,7 @@ public class WeatherConsumer extends ScheduledPollConsumer { if (HttpStatus.SC_OK != response.getCode()) { LOG.warn("HTTP call for weather returned error status code {} - {} as a result with query: {}", status, - response.getCode(), query); + response.getCode(), sanitizedQuery); return 0; } String weather = EntityUtils.toString(response.getEntity(), "UTF-8"); @@ -80,7 +82,7 @@ public class WeatherConsumer extends ScheduledPollConsumer { } else { exchange.getIn().setBody(weather); } - exchange.getIn().setHeader(WeatherConstants.WEATHER_QUERY, query); + exchange.getIn().setHeader(WeatherConstants.WEATHER_QUERY, sanitizedQuery); try { getProcessor().process(exchange); diff --git a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherProducer.java b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherProducer.java index 8c6d7ce9293f..c06e692bb1c9 100644 --- a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherProducer.java +++ b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherProducer.java @@ -51,8 +51,9 @@ public class WeatherProducer extends DefaultProducer { } HttpClient httpClient = getEndpoint().getConfiguration().getHttpClient(); - String uri = q; - HttpGet method = new HttpGet(uri); + HttpGet method = new HttpGet(q); + // the query with the appid masked, for the header, the log and error messages + String uri = WeatherQuery.sanitize(q); httpClient.execute( method, response -> { diff --git a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherQuery.java b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherQuery.java index d467f79da55f..31d9014b4149 100644 --- a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherQuery.java +++ b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherQuery.java @@ -16,6 +16,8 @@ */ package org.apache.camel.component.weather; +import java.util.regex.Pattern; + import org.apache.camel.component.weather.geolocation.GeoLocation; import static org.apache.camel.component.weather.WeatherMode.JSON; @@ -25,6 +27,10 @@ import static org.apache.camel.util.ObjectHelper.isEmpty; * Logic for determining the query based on the provided configuration. */ public class WeatherQuery { + + // the value of the APPID query parameter (the API key), also when a location header adds it as appid + private static final Pattern APPID_VALUE = Pattern.compile("([?&]APPID=)[^&]*", Pattern.CASE_INSENSITIVE); + private final WeatherConfiguration weatherConfiguration; public WeatherQuery(WeatherConfiguration weatherConfiguration) { @@ -93,6 +99,20 @@ public class WeatherQuery { return answer; } + /** + * Returns the query with the value of the APPID parameter masked, as shown in the CamelWeatherQuery header, the log + * and error messages. The appid is a secret option, so only the request itself has the real value. + * + * @param query the query, such as returned by {@link #getQuery()} + * @return the query with {@code APPID=xxxxxx}, or null if the query is null + */ + public static String sanitize(String query) { + if (query == null) { + return null; + } + return APPID_VALUE.matcher(query).replaceAll("$1xxxxxx"); + } + private String createContext(String lat, String lon) { String answer; if (isBoxedQuery()) { diff --git a/components/camel-weather/src/test/java/org/apache/camel/component/weather/WeatherAppidMaskedTest.java b/components/camel-weather/src/test/java/org/apache/camel/component/weather/WeatherAppidMaskedTest.java new file mode 100644 index 000000000000..9c4833c2a079 --- /dev/null +++ b/components/camel-weather/src/test/java/org/apache/camel/component/weather/WeatherAppidMaskedTest.java @@ -0,0 +1,142 @@ +/* + * 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.weather; + +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + +import org.apache.camel.BindToRegistry; +import org.apache.camel.CamelExecutionException; +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit6.CamelTestSupport; +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.core5.http.HttpStatus; +import org.apache.hc.core5.http.io.HttpClientResponseHandler; +import org.apache.hc.core5.http.io.entity.StringEntity; +import org.apache.hc.core5.http.message.BasicClassicHttpResponse; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * The appid is sent in the request, but the CamelWeatherQuery header and the error messages have it masked. + */ +class WeatherAppidMaskedTest extends CamelTestSupport { + + private static final String APPID = "my-api-key"; + + private final List<String> requested = new CopyOnWriteArrayList<>(); + private volatile int status = HttpStatus.SC_OK; + + @BindToRegistry("http") + private final CloseableHttpClient httpClient = createHttpClient(); + + @Test + void producerHeaderHasTheAppidMasked() { + Exchange out = template.request("weather:foo?location=Madrid&appid=" + APPID + "&httpClient=#http", e -> { + }); + + String query = out.getMessage().getHeader(WeatherConstants.WEATHER_QUERY, String.class); + assertTrue(query.contains("&APPID=xxxxxx"), query); + assertFalse(query.contains(APPID), query); + assertEquals(1, requested.size()); + assertTrue(requested.get(0).contains("&APPID=" + APPID), requested.get(0)); + } + + @Test + void producerLocationHeaderWithAppidIsMasked() { + Exchange out = template.request("weather:foo?location=Madrid&appid=" + APPID + "&httpClient=#http", + e -> e.getMessage().setHeader(WeatherConstants.WEATHER_LOCATION, "Paris,France&appid=other-key")); + + String query = out.getMessage().getHeader(WeatherConstants.WEATHER_QUERY, String.class); + assertFalse(query.contains(APPID), query); + assertFalse(query.contains("other-key"), query); + assertTrue(query.contains("&appid=xxxxxx"), query); + } + + @Test + void producerErrorHasTheAppidMasked() { + status = HttpStatus.SC_UNAUTHORIZED; + + CamelExecutionException e = assertThrows(CamelExecutionException.class, + () -> template.requestBody("weather:foo?location=Madrid&appid=" + APPID + "&httpClient=#http", + (Object) null)); + + IllegalStateException cause = assertInstanceOf(IllegalStateException.class, e.getCause()); + assertFalse(cause.getMessage().contains(APPID), cause.getMessage()); + assertTrue(cause.getMessage().contains("&APPID=xxxxxx"), cause.getMessage()); + } + + @Test + void consumerHeaderHasTheAppidMasked() throws Exception { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() { + from("weather:bar?location=Madrid&appid=" + APPID + "&httpClient=#http&initialDelay=0&delay=60000") + .to("mock:result"); + } + }); + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMinimumMessageCount(1); + + MockEndpoint.assertIsSatisfied(context); + + String query = mock.getReceivedExchanges().get(0).getMessage().getHeader(WeatherConstants.WEATHER_QUERY, + String.class); + assertTrue(query.contains("&APPID=xxxxxx"), query); + assertFalse(query.contains(APPID), query); + assertTrue(requested.get(0).contains("&APPID=" + APPID), requested.get(0)); + } + + @Test + void sanitize() { + assertNull(WeatherQuery.sanitize(null)); + assertEquals("http://h/weather?q=Madrid&lang=en&APPID=xxxxxx", + WeatherQuery.sanitize("http://h/weather?q=Madrid&lang=en&APPID=" + APPID)); + assertEquals("http://h/weather?q=Madrid&appid=xxxxxx&lang=en&APPID=xxxxxx", + WeatherQuery.sanitize("http://h/weather?q=Madrid&appid=a$1b&lang=en&APPID=" + APPID)); + assertEquals("http://h/weather?q=Madrid&lang=en", WeatherQuery.sanitize("http://h/weather?q=Madrid&lang=en")); + } + + private CloseableHttpClient createHttpClient() { + try { + CloseableHttpClient client = mock(CloseableHttpClient.class); + when(client.execute(any(HttpGet.class), any(HttpClientResponseHandler.class))).thenAnswer(invocation -> { + HttpGet request = invocation.getArgument(0); + requested.add(request.getRequestUri()); + BasicClassicHttpResponse response = new BasicClassicHttpResponse(status); + response.setEntity(new StringEntity("{}")); + HttpClientResponseHandler<?> handler = invocation.getArgument(1); + return handler.handleResponse(response); + }); + return client; + } catch (Exception e) { + throw new IllegalStateException(e); + } + } +} 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 25708c2cd51b..9d67e2c0656b 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 @@ -12297,6 +12297,12 @@ With `allowUrlBody=true`, a body that does not start with `version: 1` and is no with a space or a relative name, now fails with an `InvalidPayloadException`, as a URL with an unknown scheme already did. Before, it failed with an `IllegalArgumentException`. A URL that cannot be fetched keeps its own exception. +=== camel-weather - the CamelWeatherQuery header has the appid masked + +The `CamelWeatherQuery` header that the consumer and the producer set has the value of the `APPID` query parameter +masked as `xxxxxx`, and so do the log and the error messages that show the request URL. The request to OpenWeatherMap +still sends the real `appid`. A route that read the API key from this header must use the `appid` option instead. + === Kamelets - removed Kamelets The Kamelet catalog (camel-kamelets 4.24) removes Kamelets that could not work on Camel 4.23 or 4.24: diff --git a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/WeatherEndpointBuilderFactory.java b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/WeatherEndpointBuilderFactory.java index 178891808efb..48aed6d32436 100644 --- a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/WeatherEndpointBuilderFactory.java +++ b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/WeatherEndpointBuilderFactory.java @@ -2378,7 +2378,8 @@ public interface WeatherEndpointBuilderFactory { } /** * The URL of the request that the consumer or the producer sent to - * OpenWeatherMap, with its query parameters. + * OpenWeatherMap, with its query parameters. The value of the APPID + * parameter is masked as xxxxxx. * * The option is a: {@code String} type. *
