Skip to content

Commit 680149a

Browse files
committed
configure lease service with MSI
1 parent c858945 commit 680149a

1 file changed

Lines changed: 47 additions & 4 deletions

File tree

  • src/NuGet.Services.Validation.Orchestrator

src/NuGet.Services.Validation.Orchestrator/Job.cs

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
using System.Threading.Tasks;
1111
using Autofac;
1212
using Autofac.Core;
13+
using Azure.Core;
14+
using Azure.Identity;
1315
using Azure.Storage.Blobs;
1416
using Microsoft.ApplicationInsights;
1517
using Microsoft.Extensions.Configuration;
@@ -384,11 +386,9 @@ private static void ConfigureLeaseService(ContainerBuilder builder)
384386
.Register(c =>
385387
{
386388
LeaseConfiguration config = c.Resolve<IOptionsSnapshot<LeaseConfiguration>>().Value;
389+
StorageMsiConfiguration storageMsiConfiguration = c.Resolve<IOptionsSnapshot<StorageMsiConfiguration>>().Value;
387390

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);
392392
return new CloudBlobLeaseService(blobServiceClient, config.ContainerName, config.StoragePath);
393393
})
394394
.As<ILeaseService>();
@@ -605,5 +605,48 @@ private T GetRequiredService<T>()
605605
{
606606
return _serviceProvider.GetRequiredService<T>();
607607
}
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+
}
608651
}
609652
}

0 commit comments

Comments
 (0)