Skip to content

Commit 6a556a3

Browse files
Merge pull request #143 from gumbarros/develop
Add Azure Files identity authentication parity with Azure Blob Storage
2 parents 00a61dd + 9e8c479 commit 6a556a3

18 files changed

Lines changed: 871 additions & 189 deletions

FluentStorage.Azure.Blobs/AzureDataLakeStorage.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
1+
using System.Collections.Generic;
42
using System.Threading;
53
using System.Threading.Tasks;
64
using Azure.Storage;
@@ -10,6 +8,7 @@
108
using FluentStorage.Azure.Blobs.Gen2.Model;
119
using FluentStorage.Utils.Objects;
1210
using Azure.Core.Pipeline;
11+
using FluentStorage.Azure.Identity;
1312

1413
namespace FluentStorage.Azure.Blobs {
1514
class AzureDataLakeStorage : AzureBlobStorage, IAzureDataLakeStorage {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
using System.Runtime.CompilerServices;
2+
using FluentStorage.Azure.Identity;
3+
4+
[assembly: TypeForwardedTo(typeof(AzureCloudEnvironment))]
5+
[assembly: TypeForwardedTo(typeof(AzureCloudEndpoints))]

FluentStorage.Azure.Blobs/ExtendedSdk.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using FluentStorage.Blobs;
1818
using FluentStorage.Azure.Blobs;
1919
using FluentStorage.Azure.Blobs.Gen2.Model;
20+
using FluentStorage.Azure.Identity;
2021
using FluentStorage.Utils.Extensions;
2122

2223
namespace Blobs {

FluentStorage.Azure.Blobs/Factory.cs

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using System;
22
using Azure.Core;
3-
using Azure.Identity;
43
using Azure.Storage;
54
using Azure.Storage.Blobs;
65
using Azure.Storage.Sas;
76
using FluentStorage.ConnectionString;
87
using FluentStorage.Azure.Blobs;
8+
using FluentStorage.Azure.Identity;
99

1010
namespace FluentStorage {
1111
/// <summary>
@@ -203,18 +203,12 @@ public static IAzureBlobStorage AzureBlobStorageWithAzureAd(this IBlobStorageFac
203203
if (applicationSecret is null)
204204
throw new ArgumentNullException(nameof(applicationSecret));
205205

206-
var authorityHost = activeDirectoryAuthEndpoint is not null
207-
? new Uri(activeDirectoryAuthEndpoint)
208-
: AzureCloudEndpoints.GetAuthorityEndpoint(cloudEnvironment);
209-
210-
// Create a token credential that can use our Azure Active
211-
// Directory application to authenticate with Azure Storage
212-
TokenCredential credential =
213-
new ClientSecretCredential(
214-
tenantId,
215-
applicationId,
216-
applicationSecret,
217-
new TokenCredentialOptions { AuthorityHost = authorityHost });
206+
TokenCredential credential = AzureStorageIdentity.CreateClientSecretCredential(
207+
tenantId,
208+
applicationId,
209+
applicationSecret,
210+
activeDirectoryAuthEndpoint,
211+
cloudEnvironment);
218212

219213
// Create a client that can authenticate using our token credential
220214
var client = new BlobServiceClient(GetServiceUri(accountName, cloudEnvironment), credential);
@@ -284,21 +278,15 @@ public static IAzureDataLakeStorage AzureDataLakeStorageWithAzureAd(this IBlobSt
284278
if (applicationSecret is null)
285279
throw new ArgumentNullException(nameof(applicationSecret));
286280

287-
var authorityHost = activeDirectoryAuthEndpoint is not null
288-
? new Uri(activeDirectoryAuthEndpoint)
289-
: AzureCloudEndpoints.GetAuthorityEndpoint(cloudEnvironment);
290-
291-
// Create a token credential that can use our Azure Active
292-
// Directory application to authenticate with Azure Storage
293-
TokenCredential credential =
294-
new ClientSecretCredential(
295-
tenantId,
296-
applicationId,
297-
applicationSecret,
298-
new TokenCredentialOptions { AuthorityHost = authorityHost });
281+
TokenCredential credential = AzureStorageIdentity.CreateClientSecretCredential(
282+
tenantId,
283+
applicationId,
284+
applicationSecret,
285+
activeDirectoryAuthEndpoint,
286+
cloudEnvironment);
299287

300288
// Create a client that can authenticate using our token credential
301-
var client = new BlobServiceClient(GetServiceUri(accountName), credential);
289+
var client = new BlobServiceClient(GetServiceUri(accountName, cloudEnvironment), credential);
302290

303291
return new AzureDataLakeStorage(client, accountName, azureCloudEnvironment: cloudEnvironment);
304292
}
@@ -388,7 +376,7 @@ public static IAzureBlobStorage AzureBlobStorageWithMsi(this IBlobStorageFactory
388376
string accountName,
389377
string clientId,
390378
AzureCloudEnvironment azureCloudEnvironment) {
391-
TokenCredential credential = new ManagedIdentityCredential(clientId, null);
379+
TokenCredential credential = AzureStorageIdentity.CreateManagedIdentityCredential(clientId);
392380

393381
var client = new BlobServiceClient(GetServiceUri(accountName, azureCloudEnvironment), credential);
394382

@@ -443,7 +431,7 @@ public static IAzureDataLakeStorage AzureDataLakeStorageWithMsi(this IBlobStorag
443431
string accountName,
444432
string clientId,
445433
AzureCloudEnvironment azureCloudEnvironment) {
446-
TokenCredential credential = new ManagedIdentityCredential(clientId, null);
434+
TokenCredential credential = AzureStorageIdentity.CreateManagedIdentityCredential(clientId);
447435

448436
var client = new BlobServiceClient(GetServiceUri(accountName, azureCloudEnvironment), credential);
449437

@@ -521,8 +509,7 @@ public static StorageConnectionString ForAzureDataLakeStorageWithAzureAd(this IC
521509
}
522510

523511
private static Uri GetServiceUri(string accountName, AzureCloudEnvironment cloudEnvironment = default) {
524-
var endpoint = AzureCloudEndpoints.GetBlobEndpoint(cloudEnvironment);
525-
return new Uri($"https://{accountName}.blob.{endpoint}/");
512+
return AzureStorageIdentity.CreateBlobServiceUri(accountName, cloudEnvironment);
526513
}
527514

528515
private static bool TryParseSasUrl(string url, out string accountName, out string containerName, out string sas) {

FluentStorage.Azure.Blobs/FluentStorage.Azure.Blobs.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030

3131
<ItemGroup>
32-
<PackageReference Include="Azure.Identity" Version="1.14.2" />
3332
<PackageReference Include="Azure.Storage.Blobs" Version="12.20.0" />
3433
<PackageReference Include="MimeMapping" Version="3.1.0" />
3534
</ItemGroup>
@@ -39,6 +38,7 @@
3938
</ItemGroup>
4039

4140
<ItemGroup>
41+
<ProjectReference Include="..\FluentStorage.Azure.Identity\FluentStorage.Azure.Identity.csproj" />
4242
<ProjectReference Include="..\FluentStorage\FluentStorage.csproj" />
4343
</ItemGroup>
4444

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,62 @@
11
using System;
2-
using Microsoft.Azure.Storage.File;
3-
2+
using Azure.Storage.Files.Shares.Models;
43
using FluentStorage.Blobs;
54
using FluentStorage.Utils.Extensions;
65

76
namespace FluentStorage.Azure.Files {
87
static class AzConvert {
9-
public static Blob ToBlob(CloudFileShare share) {
8+
public static Blob ToBlob(ShareItem share) {
109
var blob = new Blob(share.Name, BlobItemKind.Folder);
1110
blob.TryAddProperties(
12-
"IsSnapshot", share.IsSnapshot.ToString(),
13-
"ETag", share.Properties.ETag,
11+
"ETag", share.Properties.ETag?.ToString(),
1412
"LastModified", share.Properties.LastModified?.ToString(),
15-
"Quota", share.Properties.Quota?.ToString(),
16-
"SnapshotTime", share.SnapshotTime?.ToString(),
13+
"QuotaInGB", share.Properties.QuotaInGB?.ToString(),
1714
"IsShare", "True");
18-
blob.Metadata.MergeRange(share.Metadata);
1915
return blob;
2016
}
2117

22-
public static Blob ToBlob(string path, IListFileItem item) {
23-
if (item is CloudFile file) {
24-
var blob = new Blob(path, file.Name, BlobItemKind.File) {
25-
LastModificationTime = file.Properties.LastWriteTime,
26-
Size = file.Properties.Length,
27-
MD5 = file.Properties.ContentMD5
28-
};
29-
blob.TryAddProperties(
30-
"CopyState", file.CopyState?.ToString(),
31-
"ChangeTime", file.Properties.ChangeTime?.ToString(),
32-
"ContentType", file.Properties.ContentType,
33-
"CreationTime", file.Properties.CreationTime?.ToString(),
34-
"ETag", file.Properties.ETag,
35-
"IsServerEncrypted", file.Properties.IsServerEncrypted.ToString(),
36-
"LastModified", file.Properties.LastModified?.ToString(),
37-
"NtfsAttributes", file.Properties.NtfsAttributes?.ToString());
38-
blob.Metadata.MergeRange(file.Metadata);
39-
return blob;
40-
}
41-
else if (item is CloudFileDirectory dir) {
42-
var blob = new Blob(path, dir.Name, BlobItemKind.Folder) {
43-
LastModificationTime = dir.Properties.LastWriteTime
44-
};
18+
public static Blob ToBlob(string path, ShareFileItem item) {
19+
if (item.IsDirectory) {
20+
var blob = new Blob(path, item.Name, BlobItemKind.Folder);
4521
blob.TryAddProperties(
46-
"ChangeTime", dir.Properties.ChangeTime?.ToString(),
47-
"CreationTime", dir.Properties.CreationTime?.ToString(),
48-
"ETag", dir.Properties.ETag,
49-
"IsServerEncrypted", dir.Properties.IsServerEncrypted.ToString(),
50-
"LastModified", dir.Properties.LastModified?.ToString(),
51-
"NtfsAttributes", dir.Properties.NtfsAttributes?.ToString());
52-
blob.Metadata.MergeRange(dir.Metadata);
22+
"ETag", item.Properties.ETag?.ToString(),
23+
"LastModified", item.Properties.LastModified?.ToString());
5324
return blob;
5425
}
55-
else {
56-
throw new NotSupportedException($"don't know '{item.GetType()}' object type");
57-
}
26+
27+
return ToFileBlob(path, item);
28+
}
29+
30+
public static Blob ToBlob(string path, string name, ShareFileProperties properties) {
31+
return ToBlob(path, name, properties, properties.Metadata);
32+
}
33+
34+
private static Blob ToFileBlob(string path, ShareFileItem item) {
35+
ShareFileItemProperties properties = item.Properties;
36+
var blob = new Blob(path, item.Name, BlobItemKind.File) {
37+
LastModificationTime = properties.LastModified,
38+
Size = item.FileSize
39+
};
40+
blob.TryAddProperties(
41+
"ETag", properties.ETag?.ToString(),
42+
"LastModified", properties.LastModified?.ToString());
43+
return blob;
44+
}
45+
46+
private static Blob ToBlob(string path, string name, ShareFileProperties properties, System.Collections.Generic.IDictionary<string, string> metadata) {
47+
var blob = new Blob(path, name, BlobItemKind.File) {
48+
LastModificationTime = properties.LastModified,
49+
Size = properties.ContentLength,
50+
MD5 = properties.ContentHash == null ? null : Convert.ToBase64String(properties.ContentHash)
51+
};
52+
blob.TryAddProperties(
53+
"CopyStatus", properties.CopyStatus.ToString(),
54+
"ContentType", properties.ContentType,
55+
"ETag", properties.ETag.ToString(),
56+
"IsServerEncrypted", properties.IsServerEncrypted.ToString(),
57+
"LastModified", properties.LastModified.ToString());
58+
blob.Metadata.MergeRange(metadata);
59+
return blob;
5860
}
5961
}
6062
}

0 commit comments

Comments
 (0)