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

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


The following commit(s) were added to refs/heads/camel-4.8.x by this push:
     new a3a36beaec0 CAMEL-21280: azure-files: Only 
CredentialType.SHARED_ACCOUNT_KEY is handled (#15727)
a3a36beaec0 is described below

commit a3a36beaec01554fd083cedc9c64dd83608d1f0e
Author: s-clauw <[email protected]>
AuthorDate: Thu Sep 26 19:28:28 2024 +0200

    CAMEL-21280: azure-files: Only CredentialType.SHARED_ACCOUNT_KEY is handled 
(#15727)
    
    * CAMEL-21280: azure-files: Only CredentialType.SHARED_ACCOUNT_KEY is 
handled
    
    * CAMEL-21280 add unit test
    
    * CAMEL-21280: Add license & code formatting
---
 .../component/file/azure/FilesOperations.java      | 16 ++--
 .../component/file/azure/FilesOperationsTest.java  | 89 ++++++++++++++++++++++
 2 files changed, 96 insertions(+), 9 deletions(-)

diff --git 
a/components/camel-azure/camel-azure-files/src/main/java/org/apache/camel/component/file/azure/FilesOperations.java
 
b/components/camel-azure/camel-azure-files/src/main/java/org/apache/camel/component/file/azure/FilesOperations.java
index ad4aeb5d437..151f9732f07 100644
--- 
a/components/camel-azure/camel-azure-files/src/main/java/org/apache/camel/component/file/azure/FilesOperations.java
+++ 
b/components/camel-azure/camel-azure-files/src/main/java/org/apache/camel/component/file/azure/FilesOperations.java
@@ -710,15 +710,13 @@ public class FilesOperations extends NormalizedOperations 
{
 
         var builder = new ShareServiceClientBuilder().endpoint(HTTPS + "://" + 
configuration.getHost());
         var sharedKey = configuration.getSharedKey();
-        if 
(configuration.getCredentialType().equals(CredentialType.SHARED_ACCOUNT_KEY)) {
-            if (sharedKey != null) {
-                var keyB64 = 
FilesURIStrings.reconstructBase64EncodedValue(sharedKey);
-                builder.credential(new 
StorageSharedKeyCredential(configuration.getAccount(), keyB64));
-            } else if 
(configuration.getCredentialType().equals(CredentialType.AZURE_SAS)) {
-                builder = builder.sasToken(token.toURIQuery());
-            } else if 
(configuration.getCredentialType().equals(CredentialType.AZURE_IDENTITY)) {
-                builder = builder.credential(new 
DefaultAzureCredentialBuilder().build());
-            }
+        if 
(configuration.getCredentialType().equals(CredentialType.SHARED_ACCOUNT_KEY) && 
sharedKey != null) {
+            var keyB64 = 
FilesURIStrings.reconstructBase64EncodedValue(sharedKey);
+            builder.credential(new 
StorageSharedKeyCredential(configuration.getAccount(), keyB64));
+        } else if 
(configuration.getCredentialType().equals(CredentialType.AZURE_SAS)) {
+            builder = builder.sasToken(token.toURIQuery());
+        } else if 
(configuration.getCredentialType().equals(CredentialType.AZURE_IDENTITY)) {
+            builder = builder.credential(new 
DefaultAzureCredentialBuilder().build());
         }
         return builder.buildClient();
     }
diff --git 
a/components/camel-azure/camel-azure-files/src/test/java/org/apache/camel/component/file/azure/FilesOperationsTest.java
 
b/components/camel-azure/camel-azure-files/src/test/java/org/apache/camel/component/file/azure/FilesOperationsTest.java
new file mode 100644
index 00000000000..35140ef3563
--- /dev/null
+++ 
b/components/camel-azure/camel-azure-files/src/test/java/org/apache/camel/component/file/azure/FilesOperationsTest.java
@@ -0,0 +1,89 @@
+/*
+ * 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.file.azure;
+
+import com.azure.core.http.policy.AzureSasCredentialPolicy;
+import com.azure.core.http.policy.BearerTokenAuthenticationPolicy;
+import com.azure.storage.common.policy.StorageSharedKeyCredentialPolicy;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class FilesOperationsTest extends CamelTestSupport {
+
+    @Test
+    void testCreateClientAzureIdentity() {
+        var endpoint = context
+                
.getEndpoint("azure-files://account.file.core.windows.net/share?credentialType=AZURE_IDENTITY",
+                        FilesEndpoint.class);
+
+        var filesOperations = new FilesOperations(endpoint);
+        var client = filesOperations.getClient();
+
+        var hasAuthPolicy = false;
+        for (int i = 0; i < client.getHttpPipeline().getPolicyCount(); i++) {
+            if (client.getHttpPipeline().getPolicy(i) instanceof 
BearerTokenAuthenticationPolicy) {
+                hasAuthPolicy = true;
+                break;
+            }
+        }
+
+        Assertions.assertTrue(hasAuthPolicy);
+    }
+
+    @Test
+    void testCreateClientSharedAccountKey() {
+        var endpoint = context
+                .getEndpoint(
+                        
"azure-files://account.file.core.windows.net/share?credentialType=SHARED_ACCOUNT_KEY&sharedKey=sharedKey",
+                        FilesEndpoint.class);
+
+        var filesOperations = new FilesOperations(endpoint);
+        var client = filesOperations.getClient();
+
+        var hasAuthPolicy = false;
+        for (int i = 0; i < client.getHttpPipeline().getPolicyCount(); i++) {
+            if (client.getHttpPipeline().getPolicy(i) instanceof 
StorageSharedKeyCredentialPolicy) {
+                hasAuthPolicy = true;
+                break;
+            }
+        }
+
+        Assertions.assertTrue(hasAuthPolicy);
+    }
+
+    @Test
+    void testCreateClientAzureSAS() {
+        var endpoint = context
+                .getEndpoint(
+                        
"azure-files://account.file.core.windows.net/share?credentialType=AZURE_SAS&sv=2022-11-02&ss=f&srt=sco&sp=rwdlc&se=2023-06-18T22:29:13Z&st=2023-06-05T14:29:13Z&spr=https&sig=MPsMh8zci0v3To7IT9SKdaFGZV8ezno63m9C8s9bdVQ%3D",
+                        FilesEndpoint.class);
+
+        var filesOperations = new FilesOperations(endpoint);
+        var client = filesOperations.getClient();
+
+        var hasAuthPolicy = false;
+        for (int i = 0; i < client.getHttpPipeline().getPolicyCount(); i++) {
+            if (client.getHttpPipeline().getPolicy(i) instanceof 
AzureSasCredentialPolicy) {
+                hasAuthPolicy = true;
+                break;
+            }
+        }
+
+        Assertions.assertTrue(hasAuthPolicy);
+    }
+}

Reply via email to