You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2025/04/13 14:01:32 UTC

(camel) branch main updated: CAMEL-21871: azure-eventhubs - autowire TokenCredential (#17736)

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

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new e826cfd41f4 CAMEL-21871: azure-eventhubs - autowire TokenCredential (#17736)
e826cfd41f4 is described below

commit e826cfd41f4524a4064bbeb804fa6683392d8528
Author: Anirudh Prakash <10...@users.noreply.github.com>
AuthorDate: Sun Apr 13 19:31:27 2025 +0530

    CAMEL-21871: azure-eventhubs - autowire TokenCredential (#17736)
---
 .../camel/catalog/components/azure-eventhubs.json  |  4 +--
 .../eventhubs/EventHubsComponentConfigurer.java    |  2 +-
 .../eventhubs/EventHubsEndpointConfigurer.java     |  2 +-
 .../component/azure/eventhubs/azure-eventhubs.json |  4 +--
 .../azure/eventhubs/EventHubsComponent.java        | 31 +++++++---------------
 .../azure/eventhubs/EventHubsConfiguration.java    |  5 ++--
 .../eventhubs/client/EventHubsClientFactory.java   | 24 ++++++++++++-----
 .../azure/eventhubs/EventHubsComponentTest.java    |  2 ++
 8 files changed, 38 insertions(+), 36 deletions(-)

diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/azure-eventhubs.json b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/azure-eventhubs.json
index d58171c8ee8..5b2619710c1 100644
--- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/azure-eventhubs.json
+++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/azure-eventhubs.json
@@ -47,7 +47,7 @@
     "credentialType": { "index": 20, "kind": "property", "displayName": "Credential Type", "group": "security", "label": "security", "required": false, "type": "object", "javaType": "org.apache.camel.component.azure.eventhubs.CredentialType", "enum": [ "AZURE_IDENTITY", "CONNECTION_STRING", "TOKEN_CREDENTIAL" ], "deprecated": false, "autowired": false, "secret": false, "defaultValue": "CONNECTION_STRING", "configurationClass": "org.apache.camel.component.azure.eventhubs.EventHubsConfigur [...]
     "sharedAccessKey": { "index": 21, "kind": "property", "displayName": "Shared Access Key", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": true, "configurationClass": "org.apache.camel.component.azure.eventhubs.EventHubsConfiguration", "configurationField": "configuration", "description": "The generated value for the SharedAccessName." },
     "sharedAccessName": { "index": 22, "kind": "property", "displayName": "Shared Access Name", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.azure.eventhubs.EventHubsConfiguration", "configurationField": "configuration", "description": "The name you chose for your EventHubs SAS keys." },
-    "tokenCredential": { "index": 23, "kind": "property", "displayName": "Token Credential", "group": "security", "label": "security", "required": false, "type": "object", "javaType": "com.azure.core.credential.TokenCredential", "deprecated": false, "autowired": false, "secret": true, "configurationClass": "org.apache.camel.component.azure.eventhubs.EventHubsConfiguration", "configurationField": "configuration", "description": "Provide custom authentication credentials using an implement [...]
+    "tokenCredential": { "index": 23, "kind": "property", "displayName": "Token Credential", "group": "security", "label": "security", "required": false, "type": "object", "javaType": "com.azure.core.credential.TokenCredential", "deprecated": false, "deprecationNote": "", "autowired": true, "secret": true, "configurationClass": "org.apache.camel.component.azure.eventhubs.EventHubsConfiguration", "configurationField": "configuration", "description": "Provide custom authentication credenti [...]
   },
   "headers": {
     "CamelAzureEventHubsPartitionKey": { "index": 0, "kind": "header", "displayName": "", "group": "common", "label": "", "required": false, "javaType": "String", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "description": "(producer) Overrides the hashing key to be provided for the batch of events, which instructs the Event Hubs service to map this key to a specific partition. (consumer) It sets the partition hashing key if it was set when originally  [...]
@@ -85,6 +85,6 @@
     "credentialType": { "index": 22, "kind": "parameter", "displayName": "Credential Type", "group": "security", "label": "security", "required": false, "type": "object", "javaType": "org.apache.camel.component.azure.eventhubs.CredentialType", "enum": [ "AZURE_IDENTITY", "CONNECTION_STRING", "TOKEN_CREDENTIAL" ], "deprecated": false, "autowired": false, "secret": false, "defaultValue": "CONNECTION_STRING", "configurationClass": "org.apache.camel.component.azure.eventhubs.EventHubsConfigu [...]
     "sharedAccessKey": { "index": 23, "kind": "parameter", "displayName": "Shared Access Key", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": true, "configurationClass": "org.apache.camel.component.azure.eventhubs.EventHubsConfiguration", "configurationField": "configuration", "description": "The generated value for the SharedAccessName." },
     "sharedAccessName": { "index": 24, "kind": "parameter", "displayName": "Shared Access Name", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.azure.eventhubs.EventHubsConfiguration", "configurationField": "configuration", "description": "The name you chose for your EventHubs SAS keys." },
-    "tokenCredential": { "index": 25, "kind": "parameter", "displayName": "Token Credential", "group": "security", "label": "security", "required": false, "type": "object", "javaType": "com.azure.core.credential.TokenCredential", "deprecated": false, "autowired": false, "secret": true, "configurationClass": "org.apache.camel.component.azure.eventhubs.EventHubsConfiguration", "configurationField": "configuration", "description": "Provide custom authentication credentials using an implemen [...]
+    "tokenCredential": { "index": 25, "kind": "parameter", "displayName": "Token Credential", "group": "security", "label": "security", "required": false, "type": "object", "javaType": "com.azure.core.credential.TokenCredential", "deprecated": false, "deprecationNote": "", "autowired": true, "secret": true, "configurationClass": "org.apache.camel.component.azure.eventhubs.EventHubsConfiguration", "configurationField": "configuration", "description": "Provide custom authentication credent [...]
   }
 }
diff --git a/components/camel-azure/camel-azure-eventhubs/src/generated/java/org/apache/camel/component/azure/eventhubs/EventHubsComponentConfigurer.java b/components/camel-azure/camel-azure-eventhubs/src/generated/java/org/apache/camel/component/azure/eventhubs/EventHubsComponentConfigurer.java
index 15956c56fca..4cf39976fd6 100644
--- a/components/camel-azure/camel-azure-eventhubs/src/generated/java/org/apache/camel/component/azure/eventhubs/EventHubsComponentConfigurer.java
+++ b/components/camel-azure/camel-azure-eventhubs/src/generated/java/org/apache/camel/component/azure/eventhubs/EventHubsComponentConfigurer.java
@@ -83,7 +83,7 @@ public class EventHubsComponentConfigurer extends PropertyConfigurerSupport impl
 
     @Override
     public String[] getAutowiredNames() {
-        return new String[]{"producerAsyncClient"};
+        return new String[]{"producerAsyncClient", "tokenCredential"};
     }
 
     @Override
diff --git a/components/camel-azure/camel-azure-eventhubs/src/generated/java/org/apache/camel/component/azure/eventhubs/EventHubsEndpointConfigurer.java b/components/camel-azure/camel-azure-eventhubs/src/generated/java/org/apache/camel/component/azure/eventhubs/EventHubsEndpointConfigurer.java
index 346c3c02cf6..bfa550eda55 100644
--- a/components/camel-azure/camel-azure-eventhubs/src/generated/java/org/apache/camel/component/azure/eventhubs/EventHubsEndpointConfigurer.java
+++ b/components/camel-azure/camel-azure-eventhubs/src/generated/java/org/apache/camel/component/azure/eventhubs/EventHubsEndpointConfigurer.java
@@ -77,7 +77,7 @@ public class EventHubsEndpointConfigurer extends PropertyConfigurerSupport imple
 
     @Override
     public String[] getAutowiredNames() {
-        return new String[]{"producerAsyncClient"};
+        return new String[]{"producerAsyncClient", "tokenCredential"};
     }
 
     @Override
diff --git a/components/camel-azure/camel-azure-eventhubs/src/generated/resources/META-INF/org/apache/camel/component/azure/eventhubs/azure-eventhubs.json b/components/camel-azure/camel-azure-eventhubs/src/generated/resources/META-INF/org/apache/camel/component/azure/eventhubs/azure-eventhubs.json
index d58171c8ee8..5b2619710c1 100644
--- a/components/camel-azure/camel-azure-eventhubs/src/generated/resources/META-INF/org/apache/camel/component/azure/eventhubs/azure-eventhubs.json
+++ b/components/camel-azure/camel-azure-eventhubs/src/generated/resources/META-INF/org/apache/camel/component/azure/eventhubs/azure-eventhubs.json
@@ -47,7 +47,7 @@
     "credentialType": { "index": 20, "kind": "property", "displayName": "Credential Type", "group": "security", "label": "security", "required": false, "type": "object", "javaType": "org.apache.camel.component.azure.eventhubs.CredentialType", "enum": [ "AZURE_IDENTITY", "CONNECTION_STRING", "TOKEN_CREDENTIAL" ], "deprecated": false, "autowired": false, "secret": false, "defaultValue": "CONNECTION_STRING", "configurationClass": "org.apache.camel.component.azure.eventhubs.EventHubsConfigur [...]
     "sharedAccessKey": { "index": 21, "kind": "property", "displayName": "Shared Access Key", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": true, "configurationClass": "org.apache.camel.component.azure.eventhubs.EventHubsConfiguration", "configurationField": "configuration", "description": "The generated value for the SharedAccessName." },
     "sharedAccessName": { "index": 22, "kind": "property", "displayName": "Shared Access Name", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.azure.eventhubs.EventHubsConfiguration", "configurationField": "configuration", "description": "The name you chose for your EventHubs SAS keys." },
-    "tokenCredential": { "index": 23, "kind": "property", "displayName": "Token Credential", "group": "security", "label": "security", "required": false, "type": "object", "javaType": "com.azure.core.credential.TokenCredential", "deprecated": false, "autowired": false, "secret": true, "configurationClass": "org.apache.camel.component.azure.eventhubs.EventHubsConfiguration", "configurationField": "configuration", "description": "Provide custom authentication credentials using an implement [...]
+    "tokenCredential": { "index": 23, "kind": "property", "displayName": "Token Credential", "group": "security", "label": "security", "required": false, "type": "object", "javaType": "com.azure.core.credential.TokenCredential", "deprecated": false, "deprecationNote": "", "autowired": true, "secret": true, "configurationClass": "org.apache.camel.component.azure.eventhubs.EventHubsConfiguration", "configurationField": "configuration", "description": "Provide custom authentication credenti [...]
   },
   "headers": {
     "CamelAzureEventHubsPartitionKey": { "index": 0, "kind": "header", "displayName": "", "group": "common", "label": "", "required": false, "javaType": "String", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "description": "(producer) Overrides the hashing key to be provided for the batch of events, which instructs the Event Hubs service to map this key to a specific partition. (consumer) It sets the partition hashing key if it was set when originally  [...]
@@ -85,6 +85,6 @@
     "credentialType": { "index": 22, "kind": "parameter", "displayName": "Credential Type", "group": "security", "label": "security", "required": false, "type": "object", "javaType": "org.apache.camel.component.azure.eventhubs.CredentialType", "enum": [ "AZURE_IDENTITY", "CONNECTION_STRING", "TOKEN_CREDENTIAL" ], "deprecated": false, "autowired": false, "secret": false, "defaultValue": "CONNECTION_STRING", "configurationClass": "org.apache.camel.component.azure.eventhubs.EventHubsConfigu [...]
     "sharedAccessKey": { "index": 23, "kind": "parameter", "displayName": "Shared Access Key", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": true, "configurationClass": "org.apache.camel.component.azure.eventhubs.EventHubsConfiguration", "configurationField": "configuration", "description": "The generated value for the SharedAccessName." },
     "sharedAccessName": { "index": 24, "kind": "parameter", "displayName": "Shared Access Name", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.azure.eventhubs.EventHubsConfiguration", "configurationField": "configuration", "description": "The name you chose for your EventHubs SAS keys." },
-    "tokenCredential": { "index": 25, "kind": "parameter", "displayName": "Token Credential", "group": "security", "label": "security", "required": false, "type": "object", "javaType": "com.azure.core.credential.TokenCredential", "deprecated": false, "autowired": false, "secret": true, "configurationClass": "org.apache.camel.component.azure.eventhubs.EventHubsConfiguration", "configurationField": "configuration", "description": "Provide custom authentication credentials using an implemen [...]
+    "tokenCredential": { "index": 25, "kind": "parameter", "displayName": "Token Credential", "group": "security", "label": "security", "required": false, "type": "object", "javaType": "com.azure.core.credential.TokenCredential", "deprecated": false, "deprecationNote": "", "autowired": true, "secret": true, "configurationClass": "org.apache.camel.component.azure.eventhubs.EventHubsConfiguration", "configurationField": "configuration", "description": "Provide custom authentication credent [...]
   }
 }
diff --git a/components/camel-azure/camel-azure-eventhubs/src/main/java/org/apache/camel/component/azure/eventhubs/EventHubsComponent.java b/components/camel-azure/camel-azure-eventhubs/src/main/java/org/apache/camel/component/azure/eventhubs/EventHubsComponent.java
index ae174e5bad7..04fd5b3de54 100644
--- a/components/camel-azure/camel-azure-eventhubs/src/main/java/org/apache/camel/component/azure/eventhubs/EventHubsComponent.java
+++ b/components/camel-azure/camel-azure-eventhubs/src/main/java/org/apache/camel/component/azure/eventhubs/EventHubsComponent.java
@@ -17,10 +17,8 @@
 package org.apache.camel.component.azure.eventhubs;
 
 import java.util.Map;
-import java.util.Set;
 
-import com.azure.core.credential.TokenCredential;
-import com.azure.identity.DefaultAzureCredentialBuilder;
+import com.azure.identity.DefaultAzureCredential;
 import org.apache.camel.Endpoint;
 import org.apache.camel.spi.Metadata;
 import org.apache.camel.spi.annotations.Component;
@@ -56,8 +54,15 @@ public class EventHubsComponent extends DefaultComponent {
             checkAndSetNamespaceAndHubName(configuration, remaining);
         }
 
-        if (isTokenCredentialSet(configuration)) {
-            setTokenCredential(configuration);
+        // ensure we use default credential type if not configured
+        if (endpoint.getConfiguration().getTokenCredential() == null) {
+            if (endpoint.getConfiguration().getCredentialType() == null) {
+                endpoint.getConfiguration().setCredentialType(CredentialType.CONNECTION_STRING);
+            }
+        } else {
+            boolean azure = endpoint.getConfiguration().getTokenCredential() instanceof DefaultAzureCredential;
+            endpoint.getConfiguration()
+                    .setCredentialType(azure ? CredentialType.AZURE_IDENTITY : CredentialType.TOKEN_CREDENTIAL);
         }
 
         validateConfigurations(configuration);
@@ -123,20 +128,4 @@ public class EventHubsComponent extends DefaultComponent {
         configuration.setEventHubName(parts[1]);
     }
 
-    private void setTokenCredential(final EventHubsConfiguration configuration) {
-        final Set<TokenCredential> tokenCredentialFromRegistry
-                = getCamelContext().getRegistry().findByType(TokenCredential.class);
-
-        // find exactly one from the registry or create one
-        if (tokenCredentialFromRegistry.size() == 1) {
-            final TokenCredential tokenCredential = tokenCredentialFromRegistry.stream().findFirst().get();
-            configuration.setTokenCredential(tokenCredential);
-            LOG.debug("using the provided TokenCredential instance: {} for the Azure-AD authentication", tokenCredential);
-        } else {
-            final TokenCredential tokenCredential = new DefaultAzureCredentialBuilder().build();
-            configuration.setTokenCredential(tokenCredential);
-            LOG.debug("using the DefaultAzureCredential instance: {} for the Azure-AD authentication", tokenCredential);
-        }
-    }
-
 }
diff --git a/components/camel-azure/camel-azure-eventhubs/src/main/java/org/apache/camel/component/azure/eventhubs/EventHubsConfiguration.java b/components/camel-azure/camel-azure-eventhubs/src/main/java/org/apache/camel/component/azure/eventhubs/EventHubsConfiguration.java
index e761cb4a584..a4065c01540 100644
--- a/components/camel-azure/camel-azure-eventhubs/src/main/java/org/apache/camel/component/azure/eventhubs/EventHubsConfiguration.java
+++ b/components/camel-azure/camel-azure-eventhubs/src/main/java/org/apache/camel/component/azure/eventhubs/EventHubsConfiguration.java
@@ -34,8 +34,6 @@ import org.apache.camel.spi.UriParam;
 import org.apache.camel.spi.UriParams;
 import org.apache.camel.spi.UriPath;
 
-import static org.apache.camel.component.azure.eventhubs.CredentialType.CONNECTION_STRING;
-
 @UriParams
 public class EventHubsConfiguration implements Cloneable {
 
@@ -50,6 +48,7 @@ public class EventHubsConfiguration implements Cloneable {
     @UriParam(label = "security", secret = true)
     private String connectionString;
     @UriParam(label = "security", secret = true)
+    @Metadata(autowired = true)
     private TokenCredential tokenCredential;
     @UriParam(label = "common", defaultValue = "AMQP")
     private AmqpTransportType amqpTransportType = AmqpTransportType.AMQP;
@@ -84,7 +83,7 @@ public class EventHubsConfiguration implements Cloneable {
     private String partitionId;
     @UriParam(label = "security", enums = "AZURE_IDENTITY,CONNECTION_STRING,TOKEN_CREDENTIAL",
               defaultValue = "CONNECTION_STRING")
-    private CredentialType credentialType = CONNECTION_STRING;
+    private CredentialType credentialType;
 
     /**
      * EventHubs namespace created in Azure Portal.
diff --git a/components/camel-azure/camel-azure-eventhubs/src/main/java/org/apache/camel/component/azure/eventhubs/client/EventHubsClientFactory.java b/components/camel-azure/camel-azure-eventhubs/src/main/java/org/apache/camel/component/azure/eventhubs/client/EventHubsClientFactory.java
index 48543642ab3..f88717894ad 100644
--- a/components/camel-azure/camel-azure-eventhubs/src/main/java/org/apache/camel/component/azure/eventhubs/client/EventHubsClientFactory.java
+++ b/components/camel-azure/camel-azure-eventhubs/src/main/java/org/apache/camel/component/azure/eventhubs/client/EventHubsClientFactory.java
@@ -49,11 +49,15 @@ public final class EventHubsClientFactory {
                 .transportType(configuration.getAmqpTransportType())
                 .retryOptions(configuration.getAmqpRetryOptions());
 
-        if (configuration.getCredentialType().equals(CredentialType.CONNECTION_STRING)) {
+        CredentialType type = configuration.getCredentialType();
+        if (type == null) {
+            type = CredentialType.CONNECTION_STRING;
+        }
+        if (CredentialType.CONNECTION_STRING.equals(type)) {
             return eventHubClientBuilder
                     .connectionString(buildConnectionString(configuration))
                     .buildAsyncProducerClient();
-        } else if (configuration.getCredentialType().equals(CredentialType.TOKEN_CREDENTIAL)) {
+        } else if (CredentialType.TOKEN_CREDENTIAL.equals(type)) {
 
             checkTokenCredentialConfiguration(configuration);
             return eventHubClientBuilder
@@ -76,11 +80,15 @@ public final class EventHubsClientFactory {
                 .transportType(configuration.getAmqpTransportType())
                 .retryOptions(configuration.getAmqpRetryOptions());
 
-        if (configuration.getCredentialType().equals(CredentialType.CONNECTION_STRING)) {
+        CredentialType type = configuration.getCredentialType();
+        if (type == null) {
+            type = CredentialType.CONNECTION_STRING;
+        }
+        if (CredentialType.CONNECTION_STRING.equals(type)) {
             return eventHubClientBuilder
                     .connectionString(buildConnectionString(configuration))
                     .buildAsyncConsumerClient();
-        } else if (configuration.getCredentialType().equals(CredentialType.TOKEN_CREDENTIAL)) {
+        } else if (CredentialType.TOKEN_CREDENTIAL.equals(type)) {
 
             checkTokenCredentialConfiguration(configuration);
             return eventHubClientBuilder
@@ -108,11 +116,15 @@ public final class EventHubsClientFactory {
                 .processError(processError)
                 .processEvent(processEvent);
 
-        if (configuration.getCredentialType().equals(CredentialType.CONNECTION_STRING)) {
+        CredentialType type = configuration.getCredentialType();
+        if (type == null) {
+            type = CredentialType.CONNECTION_STRING;
+        }
+        if (CredentialType.CONNECTION_STRING.equals(type)) {
             return eventProcessorClientBuilder
                     .connectionString(buildConnectionString(configuration))
                     .buildEventProcessorClient();
-        } else if (configuration.getCredentialType().equals(CredentialType.TOKEN_CREDENTIAL)) {
+        } else if (CredentialType.TOKEN_CREDENTIAL.equals(type)) {
 
             checkTokenCredentialConfiguration(configuration);
             return eventProcessorClientBuilder
diff --git a/components/camel-azure/camel-azure-eventhubs/src/test/java/org/apache/camel/component/azure/eventhubs/EventHubsComponentTest.java b/components/camel-azure/camel-azure-eventhubs/src/test/java/org/apache/camel/component/azure/eventhubs/EventHubsComponentTest.java
index 5d4ea2dd09e..15d3baa0ec3 100644
--- a/components/camel-azure/camel-azure-eventhubs/src/test/java/org/apache/camel/component/azure/eventhubs/EventHubsComponentTest.java
+++ b/components/camel-azure/camel-azure-eventhubs/src/test/java/org/apache/camel/component/azure/eventhubs/EventHubsComponentTest.java
@@ -118,6 +118,7 @@ class EventHubsComponentTest extends CamelTestSupport {
                 = context.getEndpoint("azure-eventhubs:name/hubName?tokenCredential=#tokenCredential", EventHubsEndpoint.class);
 
         assertSame(tokenCredential, endpoint2.getConfiguration().getTokenCredential());
+        assertEquals(CredentialType.AZURE_IDENTITY, endpoint2.getConfiguration().getCredentialType());
     }
 
     @Test
@@ -137,6 +138,7 @@ class EventHubsComponentTest extends CamelTestSupport {
         assertEquals(100, endpoint.getConfiguration().getPrefetchCount());
         assertEquals(100, endpoint.getConfiguration().getCheckpointBatchSize());
         assertEquals(1000, endpoint.getConfiguration().getCheckpointBatchTimeout());
+        assertEquals(CredentialType.CONNECTION_STRING, endpoint.getConfiguration().getCredentialType());
     }
 
     @Test