This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch CAMEL-27497-es-apikey-user-no-password
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 7ffaee9d43ccb3b9b5ae6fa0a6155d61173d4f9c
Author: Claus Ibsen <[email protected]>
AuthorDate: Tue Nov 24 07:53:58 2026 +0100

    CAMEL-27497: camel-elasticsearch / camel-opensearch - apiKey no longer 
fails when user is set without a password
    
    A user without a password is never used for basic authentication, so it
    does not clash with an apiKey. The check now fails only when apiKey, user
    and password are all set, and empty values count as not set. Inside a
    Kamelet an optional {{?user}} that is not set can resolve from the USER
    environment variable, which made the index sinks fail with only an apiKey.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Signed-off-by: Claus Ibsen <[email protected]>
---
 .../catalog/docs/elasticsearch-component.adoc      |  5 +--
 .../camel/catalog/docs/opensearch-component.adoc   |  6 ++--
 .../src/main/docs/elasticsearch-component.adoc     |  5 +--
 .../camel/component/es/ElasticsearchComponent.java |  7 ++--
 .../camel/component/es/ElasticsearchProducer.java  |  5 +--
 .../es/ElasticsearchProducerApiKeyTest.java        | 42 ++++++++++++++++++++--
 .../src/main/docs/opensearch-component.adoc        |  6 ++--
 .../component/opensearch/OpensearchComponent.java  |  7 ++--
 .../component/opensearch/OpensearchProducer.java   |  2 +-
 .../opensearch/OpensearchProducerApiKeyTest.java   | 40 +++++++++++++++++++--
 10 files changed, 102 insertions(+), 23 deletions(-)

diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/elasticsearch-component.adoc
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/elasticsearch-component.adoc
index 24e4198b8f07..a1d2af4e872f 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/elasticsearch-component.adoc
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/elasticsearch-component.adoc
@@ -149,8 +149,9 @@ 
camel.component.elasticsearch.api-key={{elasticsearch.api-key}}
 ----
 
 The key is only sent to the hosts of `hostAddresses` and to the nodes the 
sniffer finds, never to another host, such as
-the target of a redirect. Setting both `apiKey` and `user` fails when the 
endpoint is created. When the component uses a
-client set with the `client` option, the `apiKey` option is not used, as for 
`user` and `password`.
+the target of a redirect. Setting `apiKey` together with `user` and `password` 
fails when the endpoint is created; a
+`user` without a `password` is not used, and the `apiKey` is sent. When the 
component uses a client set with the
+`client` option, the `apiKey` option is not used, as for `user` and `password`.
 
 === Document type
 
diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/opensearch-component.adoc
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/opensearch-component.adoc
index 7330f6b165ad..85ac4ed21362 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/opensearch-component.adoc
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/opensearch-component.adoc
@@ -145,9 +145,9 @@ camel.component.opensearch.api-key={{opensearch.api-key}}
 ----
 
 The key is only sent to the hosts of `hostAddresses` and to the nodes the 
sniffer finds, never to another host, such as
-the target of a redirect. Setting both `apiKey` and `user` fails when the 
endpoint is created. When the component uses a
-client set with the `client` or `openSearchClient` option, the `apiKey` option 
is not used, as for `user` and
-`password`.
+the target of a redirect. Setting `apiKey` together with `user` and `password` 
fails when the endpoint is created; a
+`user` without a `password` is not used, and the `apiKey` is sent. When the 
component uses a client set with the
+`client` or `openSearchClient` option, the `apiKey` option is not used, as for 
`user` and `password`.
 
 NOTE: A key to index documents needs the `indices:data/write/bulk` cluster 
permission, besides its index permissions:
 OpenSearch runs the index of a single document as a bulk request.
diff --git 
a/components/camel-elasticsearch/src/main/docs/elasticsearch-component.adoc 
b/components/camel-elasticsearch/src/main/docs/elasticsearch-component.adoc
index 24e4198b8f07..a1d2af4e872f 100644
--- a/components/camel-elasticsearch/src/main/docs/elasticsearch-component.adoc
+++ b/components/camel-elasticsearch/src/main/docs/elasticsearch-component.adoc
@@ -149,8 +149,9 @@ 
camel.component.elasticsearch.api-key={{elasticsearch.api-key}}
 ----
 
 The key is only sent to the hosts of `hostAddresses` and to the nodes the 
sniffer finds, never to another host, such as
-the target of a redirect. Setting both `apiKey` and `user` fails when the 
endpoint is created. When the component uses a
-client set with the `client` option, the `apiKey` option is not used, as for 
`user` and `password`.
+the target of a redirect. Setting `apiKey` together with `user` and `password` 
fails when the endpoint is created; a
+`user` without a `password` is not used, and the `apiKey` is sent. When the 
component uses a client set with the
+`client` option, the `apiKey` option is not used, as for `user` and `password`.
 
 === Document type
 
diff --git 
a/components/camel-elasticsearch/src/main/java/org/apache/camel/component/es/ElasticsearchComponent.java
 
b/components/camel-elasticsearch/src/main/java/org/apache/camel/component/es/ElasticsearchComponent.java
index bd5bfbf3d5e7..fc9d6c5fa485 100644
--- 
a/components/camel-elasticsearch/src/main/java/org/apache/camel/component/es/ElasticsearchComponent.java
+++ 
b/components/camel-elasticsearch/src/main/java/org/apache/camel/component/es/ElasticsearchComponent.java
@@ -28,6 +28,7 @@ import org.apache.camel.spi.Metadata;
 import org.apache.camel.spi.annotations.Component;
 import org.apache.camel.support.DefaultComponent;
 import org.apache.camel.support.jsse.SSLContextParameters;
+import org.apache.camel.util.ObjectHelper;
 import org.apache.http.HttpHost;
 import org.elasticsearch.client.RestClient;
 
@@ -98,9 +99,11 @@ public class ElasticsearchComponent extends DefaultComponent 
implements SSLConte
         config.setClusterName(remaining);
         config.setEnableDocumentOnlyMode(this.isEnableDocumentOnlyMode());
 
-        if (config.getApiKey() != null && config.getUser() != null) {
+        // a user without a password is not used for basic authentication, so 
it does not clash with an apiKey
+        if (ObjectHelper.isNotEmpty(config.getApiKey()) && 
ObjectHelper.isNotEmpty(config.getUser())
+                && ObjectHelper.isNotEmpty(config.getPassword())) {
             throw new IllegalArgumentException(
-                    "Both apiKey and user are configured on the elasticsearch 
component: use either apiKey, or user and password");
+                    "Both apiKey and user and password are configured on the 
elasticsearch component: use either apiKey, or user and password");
         }
 
         Endpoint endpoint = new ElasticsearchEndpoint(uri, this, config, 
client);
diff --git 
a/components/camel-elasticsearch/src/main/java/org/apache/camel/component/es/ElasticsearchProducer.java
 
b/components/camel-elasticsearch/src/main/java/org/apache/camel/component/es/ElasticsearchProducer.java
index e42895622a61..85c1ce19f55d 100644
--- 
a/components/camel-elasticsearch/src/main/java/org/apache/camel/component/es/ElasticsearchProducer.java
+++ 
b/components/camel-elasticsearch/src/main/java/org/apache/camel/component/es/ElasticsearchProducer.java
@@ -62,6 +62,7 @@ import org.apache.camel.Message;
 import org.apache.camel.support.DefaultAsyncProducer;
 import org.apache.camel.support.ResourceHelper;
 import org.apache.camel.util.IOHelper;
+import org.apache.camel.util.ObjectHelper;
 import org.apache.http.HttpHost;
 import org.apache.http.auth.UsernamePasswordCredentials;
 import org.apache.http.client.CredentialsProvider;
@@ -531,7 +532,7 @@ class ElasticsearchProducer extends DefaultAsyncProducer {
             if (credentialsProvider != null) {
                 
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
             }
-            if (configuration.getApiKey() != null) {
+            if (ObjectHelper.isNotEmpty(configuration.getApiKey())) {
                 httpClientBuilder.addInterceptorLast(
                         new 
NodeScopedApiKeyInterceptor(configuration.getApiKey(), () -> 
nodeHosts(restClientRef.get())));
             }
@@ -565,7 +566,7 @@ class ElasticsearchProducer extends DefaultAsyncProducer {
      * null when no credentials are configured.
      */
     CredentialsProvider createCredentialsProvider(Supplier<? extends 
Collection<HttpHost>> nodes) {
-        if (configuration.getUser() == null || configuration.getPassword() == 
null) {
+        if (ObjectHelper.isEmpty(configuration.getUser()) || 
ObjectHelper.isEmpty(configuration.getPassword())) {
             return null;
         }
         return new NodeScopedCredentialsProvider(
diff --git 
a/components/camel-elasticsearch/src/test/java/org/apache/camel/component/es/ElasticsearchProducerApiKeyTest.java
 
b/components/camel-elasticsearch/src/test/java/org/apache/camel/component/es/ElasticsearchProducerApiKeyTest.java
index 6adaba7f9d4f..a1a1bf39d4a9 100644
--- 
a/components/camel-elasticsearch/src/test/java/org/apache/camel/component/es/ElasticsearchProducerApiKeyTest.java
+++ 
b/components/camel-elasticsearch/src/test/java/org/apache/camel/component/es/ElasticsearchProducerApiKeyTest.java
@@ -34,6 +34,7 @@ import org.junit.jupiter.api.BeforeEach;
 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.assertThrows;
 
@@ -122,7 +123,7 @@ class ElasticsearchProducerApiKeyTest {
     }
 
     @Test
-    void failsWhenBothApiKeyAndUserAreSet() throws Exception {
+    void failsWhenApiKeyAndUserAndPasswordAreSet() throws Exception {
         try (CamelContext context = new DefaultCamelContext()) {
             context.start();
             ElasticsearchComponent component = component(context);
@@ -133,12 +134,47 @@ class ElasticsearchProducerApiKeyTest {
             ResolveEndpointFailedException e = 
assertThrows(ResolveEndpointFailedException.class,
                     () -> context.getEndpoint("elasticsearch:test-cluster"));
             IllegalArgumentException cause = 
assertInstanceOf(IllegalArgumentException.class, e.getCause());
-            assertEquals("Both apiKey and user are configured on the 
elasticsearch component: use either apiKey, or user and"
-                         + " password",
+            assertEquals(
+                    "Both apiKey and user and password are configured on the 
elasticsearch component: use either apiKey, or"
+                         + " user and password",
                     cause.getMessage());
         }
     }
 
+    @Test
+    void usesTheApiKeyWhenTheUserHasNoPassword() throws Exception {
+        // inside a Kamelet an optional {{?user}} that is not set can resolve 
from the USER environment variable
+        try (CamelContext context = new DefaultCamelContext()) {
+            context.start();
+            ElasticsearchComponent component = component(context);
+            component.setApiKey("my-id:my-key");
+            component.setUser("elastic");
+
+            send(context);
+
+            assertEquals("ApiKey " + 
Base64.getEncoder().encodeToString("my-id:my-key".getBytes(StandardCharsets.UTF_8)),
+                    authorizations.get("node"));
+            assertEquals(NO_HEADER, authorizations.get("other"));
+        }
+    }
+
+    @Test
+    void ignoresAnEmptyApiKey() throws Exception {
+        try (CamelContext context = new DefaultCamelContext()) {
+            context.start();
+            ElasticsearchComponent component = component(context);
+            component.setApiKey("");
+            component.setUser("elastic");
+            component.setPassword("secret");
+
+            send(context);
+
+            // user and password are used instead, which the client may send 
before it is challenged
+            assertFalse(authorizations.getOrDefault("node", 
NO_HEADER).startsWith("ApiKey"));
+            assertEquals(NO_HEADER, authorizations.get("other"));
+        }
+    }
+
     private void record(String server, String authorization) {
         authorizations.put(server, authorization != null ? authorization : 
NO_HEADER);
     }
diff --git 
a/components/camel-opensearch/src/main/docs/opensearch-component.adoc 
b/components/camel-opensearch/src/main/docs/opensearch-component.adoc
index 7330f6b165ad..85ac4ed21362 100644
--- a/components/camel-opensearch/src/main/docs/opensearch-component.adoc
+++ b/components/camel-opensearch/src/main/docs/opensearch-component.adoc
@@ -145,9 +145,9 @@ camel.component.opensearch.api-key={{opensearch.api-key}}
 ----
 
 The key is only sent to the hosts of `hostAddresses` and to the nodes the 
sniffer finds, never to another host, such as
-the target of a redirect. Setting both `apiKey` and `user` fails when the 
endpoint is created. When the component uses a
-client set with the `client` or `openSearchClient` option, the `apiKey` option 
is not used, as for `user` and
-`password`.
+the target of a redirect. Setting `apiKey` together with `user` and `password` 
fails when the endpoint is created; a
+`user` without a `password` is not used, and the `apiKey` is sent. When the 
component uses a client set with the
+`client` or `openSearchClient` option, the `apiKey` option is not used, as for 
`user` and `password`.
 
 NOTE: A key to index documents needs the `indices:data/write/bulk` cluster 
permission, besides its index permissions:
 OpenSearch runs the index of a single document as a bulk request.
diff --git 
a/components/camel-opensearch/src/main/java/org/apache/camel/component/opensearch/OpensearchComponent.java
 
b/components/camel-opensearch/src/main/java/org/apache/camel/component/opensearch/OpensearchComponent.java
index 7ef116becf8b..c148cbc442d8 100644
--- 
a/components/camel-opensearch/src/main/java/org/apache/camel/component/opensearch/OpensearchComponent.java
+++ 
b/components/camel-opensearch/src/main/java/org/apache/camel/component/opensearch/OpensearchComponent.java
@@ -28,6 +28,7 @@ import org.apache.camel.spi.Metadata;
 import org.apache.camel.spi.annotations.Component;
 import org.apache.camel.support.DefaultComponent;
 import org.apache.camel.support.jsse.SSLContextParameters;
+import org.apache.camel.util.ObjectHelper;
 import org.apache.hc.core5.http.HttpHost;
 import org.opensearch.client.RestClient;
 import org.opensearch.client.opensearch.OpenSearchClient;
@@ -95,9 +96,11 @@ public class OpensearchComponent extends DefaultComponent 
implements SSLContextP
         config.setClusterName(remaining);
         config.setSslContextParameters(this.getSslContextParameters());
 
-        if (config.getApiKey() != null && config.getUser() != null) {
+        // a user without a password is not used for basic authentication, so 
it does not clash with an apiKey
+        if (ObjectHelper.isNotEmpty(config.getApiKey()) && 
ObjectHelper.isNotEmpty(config.getUser())
+                && ObjectHelper.isNotEmpty(config.getPassword())) {
             throw new IllegalArgumentException(
-                    "Both apiKey and user are configured on the opensearch 
component: use either apiKey, or user and password");
+                    "Both apiKey and user and password are configured on the 
opensearch component: use either apiKey, or user and password");
         }
 
         Endpoint endpoint = new OpensearchEndpoint(uri, this, config, client, 
openSearchClient);
diff --git 
a/components/camel-opensearch/src/main/java/org/apache/camel/component/opensearch/OpensearchProducer.java
 
b/components/camel-opensearch/src/main/java/org/apache/camel/component/opensearch/OpensearchProducer.java
index aa4dff25b098..2d1a8019a782 100644
--- 
a/components/camel-opensearch/src/main/java/org/apache/camel/component/opensearch/OpensearchProducer.java
+++ 
b/components/camel-opensearch/src/main/java/org/apache/camel/component/opensearch/OpensearchProducer.java
@@ -520,7 +520,7 @@ class OpensearchProducer extends DefaultAsyncProducer {
             if (credentialsProvider != null) {
                 
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
             }
-            if (configuration.getApiKey() != null) {
+            if (ObjectHelper.isNotEmpty(configuration.getApiKey())) {
                 httpClientBuilder.addRequestInterceptorLast(
                         new 
NodeScopedApiKeyInterceptor(configuration.getApiKey(), () -> 
nodeHosts(restClientRef.get())));
             }
diff --git 
a/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/OpensearchProducerApiKeyTest.java
 
b/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/OpensearchProducerApiKeyTest.java
index d8cc1ac45882..64a2bfcee1e0 100644
--- 
a/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/OpensearchProducerApiKeyTest.java
+++ 
b/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/OpensearchProducerApiKeyTest.java
@@ -33,6 +33,7 @@ import org.opensearch.client.Response;
 import org.opensearch.client.RestClient;
 
 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.assertThrows;
 
@@ -106,7 +107,7 @@ class OpensearchProducerApiKeyTest {
     }
 
     @Test
-    void failsWhenBothApiKeyAndUserAreSet() throws Exception {
+    void failsWhenApiKeyAndUserAndPasswordAreSet() throws Exception {
         try (CamelContext context = new DefaultCamelContext()) {
             context.start();
             OpensearchComponent component = component(context);
@@ -117,12 +118,45 @@ class OpensearchProducerApiKeyTest {
             ResolveEndpointFailedException e = 
assertThrows(ResolveEndpointFailedException.class,
                     () -> context.getEndpoint("opensearch:test-cluster"));
             IllegalArgumentException cause = 
assertInstanceOf(IllegalArgumentException.class, e.getCause());
-            assertEquals("Both apiKey and user are configured on the 
opensearch component: use either apiKey, or user and"
-                         + " password",
+            assertEquals("Both apiKey and user and password are configured on 
the opensearch component: use either apiKey, or"
+                         + " user and password",
                     cause.getMessage());
         }
     }
 
+    @Test
+    void usesTheApiKeyWhenTheUserHasNoPassword() throws Exception {
+        // inside a Kamelet an optional {{?user}} that is not set can resolve 
from the USER environment variable
+        try (CamelContext context = new DefaultCamelContext()) {
+            context.start();
+            OpensearchComponent component = component(context);
+            component.setApiKey("os_3jVHq8X2mK9pLwR5tN7bYcD1fA4eZ6uS");
+            component.setUser("admin");
+
+            send(context);
+
+            assertEquals("ApiKey os_3jVHq8X2mK9pLwR5tN7bYcD1fA4eZ6uS", 
authorizations.get("node"));
+            assertEquals(NO_HEADER, authorizations.get("other"));
+        }
+    }
+
+    @Test
+    void ignoresAnEmptyApiKey() throws Exception {
+        try (CamelContext context = new DefaultCamelContext()) {
+            context.start();
+            OpensearchComponent component = component(context);
+            component.setApiKey("");
+            component.setUser("admin");
+            component.setPassword("secret");
+
+            send(context);
+
+            // user and password are used instead, which the client may send 
before it is challenged
+            assertFalse(authorizations.getOrDefault("node", 
NO_HEADER).startsWith("ApiKey"));
+            assertEquals(NO_HEADER, authorizations.get("other"));
+        }
+    }
+
     private void record(String server, String authorization) {
         authorizations.put(server, authorization != null ? authorization : 
NO_HEADER);
     }

Reply via email to