|
10 | 10 | using System.Threading.Tasks; |
11 | 11 | using Autofac; |
12 | 12 | using Autofac.Core; |
| 13 | +using Azure.Core; |
| 14 | +using Azure.Identity; |
13 | 15 | using Azure.Storage.Blobs; |
14 | 16 | using Microsoft.ApplicationInsights; |
15 | 17 | using Microsoft.Extensions.Configuration; |
@@ -384,11 +386,9 @@ private static void ConfigureLeaseService(ContainerBuilder builder) |
384 | 386 | .Register(c => |
385 | 387 | { |
386 | 388 | LeaseConfiguration config = c.Resolve<IOptionsSnapshot<LeaseConfiguration>>().Value; |
| 389 | + StorageMsiConfiguration storageMsiConfiguration = c.Resolve<IOptionsSnapshot<StorageMsiConfiguration>>().Value; |
387 | 390 |
|
388 | | - // workaround for https://github.com/Azure/azure-sdk-for-net/issues/44373 |
389 | | - var connectionString = config.ConnectionString.Replace("SharedAccessSignature=?", "SharedAccessSignature="); |
390 | | - |
391 | | - BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString); |
| 391 | + BlobServiceClient blobServiceClient = CreateBlobServiceClient(storageMsiConfiguration, config.ConnectionString); |
392 | 392 | return new CloudBlobLeaseService(blobServiceClient, config.ContainerName, config.StoragePath); |
393 | 393 | }) |
394 | 394 | .As<ILeaseService>(); |
@@ -605,5 +605,48 @@ private T GetRequiredService<T>() |
605 | 605 | { |
606 | 606 | return _serviceProvider.GetRequiredService<T>(); |
607 | 607 | } |
| 608 | + |
| 609 | + private static BlobServiceClient CreateBlobServiceClient( |
| 610 | + StorageMsiConfiguration msiConfiguration, |
| 611 | + string storageConnectionString, |
| 612 | + TimeSpan? requestTimeout = null) |
| 613 | + { |
| 614 | + BlobClientOptions blobClientOptions = new BlobClientOptions(); |
| 615 | + if (requestTimeout.HasValue) |
| 616 | + { |
| 617 | + blobClientOptions.Retry.NetworkTimeout = requestTimeout.Value; |
| 618 | + } |
| 619 | + |
| 620 | + if (msiConfiguration.UseManagedIdentity) |
| 621 | + { |
| 622 | + if (string.IsNullOrWhiteSpace(msiConfiguration.ManagedIdentityClientId)) |
| 623 | + { |
| 624 | + // Using MSI with DefaultAzureCredential (local debugging) |
| 625 | + var defaultAzureCredentialOptions = new DefaultAzureCredentialOptions |
| 626 | + { |
| 627 | + ManagedIdentityClientId = msiConfiguration.ManagedIdentityClientId, |
| 628 | + }; |
| 629 | + var tokenCredential = new DefaultAzureCredential(defaultAzureCredentialOptions); |
| 630 | + |
| 631 | + return new BlobServiceClient(new Uri(storageConnectionString), tokenCredential, blobClientOptions); |
| 632 | + } |
| 633 | + else |
| 634 | + { |
| 635 | + // Using MSI with ClientId |
| 636 | + var tokenCredential = new ManagedIdentityCredential(msiConfiguration.ManagedIdentityClientId); |
| 637 | + |
| 638 | + return new BlobServiceClient(new Uri(storageConnectionString), tokenCredential, blobClientOptions); |
| 639 | + } |
| 640 | + } |
| 641 | + else |
| 642 | + { |
| 643 | + // Using SAS token |
| 644 | + |
| 645 | + // workaround for https://github.com/Azure/azure-sdk-for-net/issues/44373 |
| 646 | + var connectionString = storageConnectionString.Replace("SharedAccessSignature=?", "SharedAccessSignature="); |
| 647 | + |
| 648 | + return new BlobServiceClient(connectionString, blobClientOptions); |
| 649 | + } |
| 650 | + } |
608 | 651 | } |
609 | 652 | } |
0 commit comments