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 2023/05/06 07:08:45 UTC

[camel] branch main updated: CAMEL-19325: Add default identity support (#10003)

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 daab82ac0b1 CAMEL-19325: Add default identity support (#10003)
daab82ac0b1 is described below

commit daab82ac0b1da97f4cdee5801a606776ad5f8a97
Author: Andreas Klug <an...@de.bosch.com>
AuthorDate: Sat May 6 09:08:38 2023 +0200

    CAMEL-19325: Add default identity support (#10003)
    
    Co-authored-by: Andreas Klug (BD/XDE1) <KG...@fe-c-014an.fritz.box>
---
 components/camel-azure/camel-azure-cosmosdb/pom.xml      |  4 ++++
 .../component/azure/cosmosdb/CosmosDbComponent.java      |  3 ---
 .../component/azure/cosmosdb/CosmosDbConfiguration.java  | 16 +++++++++++++++-
 .../azure/cosmosdb/client/CosmosDbClientFactory.java     | 10 ++++++++--
 4 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/components/camel-azure/camel-azure-cosmosdb/pom.xml b/components/camel-azure/camel-azure-cosmosdb/pom.xml
index 41e2725a717..52b95d63714 100644
--- a/components/camel-azure/camel-azure-cosmosdb/pom.xml
+++ b/components/camel-azure/camel-azure-cosmosdb/pom.xml
@@ -46,6 +46,10 @@
             <groupId>com.azure</groupId>
             <artifactId>azure-cosmos</artifactId>
         </dependency>
+        <dependency>
+            <groupId>com.azure</groupId>
+            <artifactId>azure-identity</artifactId>
+        </dependency>
 
         <!-- extras -->
         <dependency>
diff --git a/components/camel-azure/camel-azure-cosmosdb/src/main/java/org/apache/camel/component/azure/cosmosdb/CosmosDbComponent.java b/components/camel-azure/camel-azure-cosmosdb/src/main/java/org/apache/camel/component/azure/cosmosdb/CosmosDbComponent.java
index 7ff3f0273d1..21486ea59d6 100644
--- a/components/camel-azure/camel-azure-cosmosdb/src/main/java/org/apache/camel/component/azure/cosmosdb/CosmosDbComponent.java
+++ b/components/camel-azure/camel-azure-cosmosdb/src/main/java/org/apache/camel/component/azure/cosmosdb/CosmosDbComponent.java
@@ -82,9 +82,6 @@ public class CosmosDbComponent extends DefaultComponent {
             if (ObjectHelper.isEmpty(configuration.getDatabaseEndpoint())) {
                 throw new IllegalArgumentException("Azure CosmosDB database endpoint must be specified.");
             }
-            if (ObjectHelper.isEmpty(configuration.getAccountKey())) {
-                throw new IllegalArgumentException("Azure CosmosDB account key must be specified.");
-            }
         }
     }
 }
diff --git a/components/camel-azure/camel-azure-cosmosdb/src/main/java/org/apache/camel/component/azure/cosmosdb/CosmosDbConfiguration.java b/components/camel-azure/camel-azure-cosmosdb/src/main/java/org/apache/camel/component/azure/cosmosdb/CosmosDbConfiguration.java
index bb17deaf247..a504dae6976 100644
--- a/components/camel-azure/camel-azure-cosmosdb/src/main/java/org/apache/camel/component/azure/cosmosdb/CosmosDbConfiguration.java
+++ b/components/camel-azure/camel-azure-cosmosdb/src/main/java/org/apache/camel/component/azure/cosmosdb/CosmosDbConfiguration.java
@@ -35,8 +35,11 @@ public class CosmosDbConfiguration implements Cloneable {
     @UriPath
     private String containerName;
     @UriParam(label = "security", secret = true)
-    @Metadata(required = true)
+    @Metadata(required = false)
     private String accountKey;
+    @UriParam(label = "security", secret = false)
+    @Metadata(required = false)
+    private boolean useDefaultIdentity;
     @UriParam(label = "common")
     @Metadata(required = true)
     private String databaseEndpoint;
@@ -126,6 +129,17 @@ public class CosmosDbConfiguration implements Cloneable {
         this.accountKey = accountKey;
     }
 
+    /**
+     * Indicates whether to use the default identity mechanism instead of the access key.
+     */
+    public boolean isUseDefaultIdentity() {
+        return useDefaultIdentity;
+    }
+
+    public void setUseDefaultIdentity(boolean useDefaultIdentity) {
+        this.useDefaultIdentity = useDefaultIdentity;
+    }
+
     /**
      * Sets the Azure Cosmos database endpoint the component will connect to.
      */
diff --git a/components/camel-azure/camel-azure-cosmosdb/src/main/java/org/apache/camel/component/azure/cosmosdb/client/CosmosDbClientFactory.java b/components/camel-azure/camel-azure-cosmosdb/src/main/java/org/apache/camel/component/azure/cosmosdb/client/CosmosDbClientFactory.java
index 7a1b116eb5f..eba3a1f7b2a 100644
--- a/components/camel-azure/camel-azure-cosmosdb/src/main/java/org/apache/camel/component/azure/cosmosdb/client/CosmosDbClientFactory.java
+++ b/components/camel-azure/camel-azure-cosmosdb/src/main/java/org/apache/camel/component/azure/cosmosdb/client/CosmosDbClientFactory.java
@@ -21,6 +21,7 @@ import java.util.stream.Stream;
 import com.azure.cosmos.CosmosAsyncClient;
 import com.azure.cosmos.CosmosClient;
 import com.azure.cosmos.CosmosClientBuilder;
+import com.azure.identity.DefaultAzureCredentialBuilder;
 import org.apache.camel.component.azure.cosmosdb.CosmosDbConfiguration;
 import org.apache.camel.util.ObjectHelper;
 
@@ -40,8 +41,8 @@ public final class CosmosDbClientFactory {
     }
 
     private static CosmosClientBuilder createBasicClient(final CosmosDbConfiguration configuration) {
+
         CosmosClientBuilder builder = new CosmosClientBuilder()
-                .key(configuration.getAccountKey())
                 .endpoint(configuration.getDatabaseEndpoint())
                 .contentResponseOnWriteEnabled(configuration.isContentResponseOnWriteEnabled())
                 .consistencyLevel(configuration.getConsistencyLevel())
@@ -54,7 +55,12 @@ public final class CosmosDbClientFactory {
                     .map(String::trim)
                     .toList());
         }
-
+        if (configuration.isUseDefaultIdentity()) {
+            final DefaultAzureCredentialBuilder defaultAzureCredentialBuilder = new DefaultAzureCredentialBuilder();
+            builder.credential(defaultAzureCredentialBuilder.build());
+        } else {
+            builder.key(configuration.getAccountKey());
+        }
         return builder;
     }
 }