Repository navigation
[backport camel-4.18.x] CAMEL-25706, CAMEL-25707, CAMEL-25708: camel-elasticsearch, camel-elasticsearch-rest-client, camel-opensearch - bind the basic authentication credentials to the cluster nodes #28051
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
davsclaus
merged 3 commits into
apache:camel-4.18.x
from
oscerd:backport/CAMEL-25706-25708-to-camel-4.18.x
Oct 18, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...a/org/apache/camel/component/elasticsearch/rest/client/NodeScopedCredentialsProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* | ||
| * 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.elasticsearch.rest.client; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.function.Supplier; | ||
|
|
||
| import org.apache.http.HttpHost; | ||
| import org.apache.http.auth.AuthScope; | ||
| import org.apache.http.auth.Credentials; | ||
| import org.apache.http.client.CredentialsProvider; | ||
|
|
||
| /** | ||
| * Offers the configured credentials only to the nodes of the cluster the client talks to: the configured hosts, and the | ||
| * nodes found by the sniffer when it is enabled. A node matches when the scheme, the host name and the port are the | ||
| * same, the default port of the scheme standing in for a missing one. | ||
| */ | ||
| final class NodeScopedCredentialsProvider implements CredentialsProvider { | ||
|
|
||
| private final Credentials credentials; | ||
| private final Supplier<? extends Collection<HttpHost>> nodes; | ||
|
|
||
| NodeScopedCredentialsProvider(Credentials credentials, Supplier<? extends Collection<HttpHost>> nodes) { | ||
| this.credentials = credentials; | ||
| this.nodes = nodes; | ||
| } | ||
|
|
||
| @Override | ||
| public Credentials getCredentials(AuthScope authScope) { | ||
| HttpHost origin = authScope != null ? authScope.getOrigin() : null; | ||
| if (origin != null) { | ||
| for (HttpHost node : nodes.get()) { | ||
| if (sameAuthority(node, origin)) { | ||
| return credentials; | ||
| } | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Ignored: the credentials come from the endpoint configuration only. HttpClient calls this for the user info of a | ||
| * request URI. | ||
| */ | ||
| @Override | ||
| public void setCredentials(AuthScope authScope, Credentials credentials) { | ||
| // read-only | ||
| } | ||
|
|
||
| @Override | ||
| public void clear() { | ||
| // read-only | ||
| } | ||
|
|
||
| private static boolean sameAuthority(HttpHost node, HttpHost origin) { | ||
| return node.getSchemeName().equalsIgnoreCase(origin.getSchemeName()) | ||
| && node.getHostName().equalsIgnoreCase(origin.getHostName()) | ||
| && effectivePort(node) == effectivePort(origin); | ||
| } | ||
|
|
||
| private static int effectivePort(HttpHost host) { | ||
| if (host.getPort() >= 0) { | ||
| return host.getPort(); | ||
| } | ||
| return "https".equalsIgnoreCase(host.getSchemeName()) ? 443 : 80; | ||
| } | ||
| } |
95 changes: 95 additions & 0 deletions
95
...l/component/elasticsearch/rest/client/ElasticsearchRestClientBasicAuthenticationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /* | ||
| * 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.elasticsearch.rest.client; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.OutputStream; | ||
| import java.net.InetSocketAddress; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Base64; | ||
| import java.util.List; | ||
| import java.util.concurrent.CopyOnWriteArrayList; | ||
|
|
||
| import com.sun.net.httpserver.HttpExchange; | ||
| import com.sun.net.httpserver.HttpServer; | ||
| import org.apache.camel.CamelContext; | ||
| import org.apache.camel.impl.DefaultCamelContext; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| /** | ||
| * The producer authenticates to the node it is configured with, through the client it creates itself. | ||
| */ | ||
| class ElasticsearchRestClientBasicAuthenticationTest { | ||
|
|
||
| private static final String AUTHORIZATION | ||
| = "Basic " + Base64.getEncoder().encodeToString("elastic:secret".getBytes(StandardCharsets.UTF_8)); | ||
|
|
||
| private final List<String> authorizations = new CopyOnWriteArrayList<>(); | ||
| private HttpServer node; | ||
|
|
||
| @BeforeEach | ||
| void startNode() throws IOException { | ||
| node = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0); | ||
| node.createContext("/", this::handle); | ||
| node.start(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void stopNode() { | ||
| node.stop(0); | ||
| } | ||
|
|
||
| @Test | ||
| void sendsTheConfiguredCredentialsToTheNode() throws Exception { | ||
| try (CamelContext context = new DefaultCamelContext()) { | ||
| ElasticsearchRestClientComponent component = new ElasticsearchRestClientComponent(); | ||
| component.setHostAddressesList("127.0.0.1:" + node.getAddress().getPort()); | ||
| component.setUser("elastic"); | ||
| component.setPassword("secret"); | ||
| context.addComponent("elasticsearch-rest-client", component); | ||
| context.start(); | ||
|
|
||
| Object acknowledged = context.createProducerTemplate() | ||
| .requestBody("elasticsearch-rest-client:camel?operation=CREATE_INDEX&indexName=camel", (Object) null); | ||
|
|
||
| assertEquals(Boolean.TRUE, acknowledged); | ||
| assertEquals(List.of(AUTHORIZATION), authorizations); | ||
| } | ||
| } | ||
|
|
||
| private void handle(HttpExchange exchange) throws IOException { | ||
| exchange.getRequestBody().readAllBytes(); | ||
| String authorization = exchange.getRequestHeaders().getFirst("Authorization"); | ||
| authorizations.add(String.valueOf(authorization)); | ||
| if (AUTHORIZATION.equals(authorization)) { | ||
| byte[] body = "{\"acknowledged\":true}".getBytes(StandardCharsets.UTF_8); | ||
| exchange.getResponseHeaders().add("Content-Type", "application/json"); | ||
| exchange.sendResponseHeaders(200, body.length); | ||
| try (OutputStream out = exchange.getResponseBody()) { | ||
| out.write(body); | ||
| } | ||
| } else { | ||
| exchange.getResponseHeaders().add("WWW-Authenticate", "Basic realm=\"camel\""); | ||
| exchange.sendResponseHeaders(401, -1); | ||
| exchange.close(); | ||
| } | ||
| } | ||
| } |
81 changes: 81 additions & 0 deletions
81
...l/component/elasticsearch/rest/client/ElasticsearchRestClientProducerCredentialsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* | ||
| * 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.elasticsearch.rest.client; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.apache.camel.CamelContext; | ||
| import org.apache.camel.impl.DefaultCamelContext; | ||
| import org.apache.http.HttpHost; | ||
| import org.apache.http.auth.AuthScope; | ||
| import org.apache.http.auth.Credentials; | ||
| import org.apache.http.client.CredentialsProvider; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
|
||
| class ElasticsearchRestClientProducerCredentialsTest { | ||
|
|
||
| private static final List<HttpHost> NODES | ||
| = List.of(HttpHost.create("es1.example.com:9200"), HttpHost.create("https://es2.example.com:9243")); | ||
|
|
||
| @Test | ||
| void offersTheConfiguredCredentialsOnlyToTheNodes() throws Exception { | ||
| try (CamelContext context = new DefaultCamelContext()) { | ||
| context.start(); | ||
| ElasticsearchRestClientEndpoint endpoint = endpoint(context, "elastic", "secret"); | ||
|
|
||
| CredentialsProvider provider = new ElasticsearchRestClientProducer(endpoint).createCredentialsProvider(() -> NODES); | ||
|
|
||
| Credentials credentials = provider.getCredentials(scope("http", "es1.example.com", 9200)); | ||
| assertNotNull(credentials); | ||
| assertEquals("elastic", credentials.getUserPrincipal().getName()); | ||
| assertEquals("secret", credentials.getPassword()); | ||
| assertNotNull(provider.getCredentials(scope("https", "es2.example.com", 9243))); | ||
|
|
||
| assertNull(provider.getCredentials(scope("http", "other.example.com", 9200))); | ||
| assertNull(provider.getCredentials(scope("http", "es1.example.com", 9201))); | ||
| assertNull(provider.getCredentials(scope("http", "es2.example.com", 9243))); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void createsNoProviderWithoutCredentials() throws Exception { | ||
| try (CamelContext context = new DefaultCamelContext()) { | ||
| context.start(); | ||
| ElasticsearchRestClientEndpoint endpoint = endpoint(context, null, null); | ||
|
|
||
| assertNull(new ElasticsearchRestClientProducer(endpoint).createCredentialsProvider(() -> NODES)); | ||
| } | ||
| } | ||
|
|
||
| private static ElasticsearchRestClientEndpoint endpoint(CamelContext context, String user, String password) { | ||
| ElasticsearchRestClientComponent component = new ElasticsearchRestClientComponent(); | ||
| component.setHostAddressesList("es1.example.com:9200,https://es2.example.com:9243"); | ||
| component.setUser(user); | ||
| component.setPassword(password); | ||
| context.addComponent("elasticsearch-rest-client", component); | ||
| return context.getEndpoint("elasticsearch-rest-client:camel?operation=CREATE_INDEX", | ||
| ElasticsearchRestClientEndpoint.class); | ||
| } | ||
|
|
||
| private static AuthScope scope(String scheme, String host, int port) { | ||
| return new AuthScope(new HttpHost(host, port, scheme), "realm", "basic"); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.