Skip to content

Commit 25badd3

Browse files
authored
Migrate Validation orchestrator jobs to new SDK and Managed Identities (#10161)
1 parent 4c6725a commit 25badd3

7 files changed

Lines changed: 56 additions & 17 deletions

File tree

src/NuGet.Jobs.Common/NuGet.Jobs.Common.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<ProjectReference Include="..\NuGet.Services.FeatureFlags\NuGet.Services.FeatureFlags.csproj" />
3030
<ProjectReference Include="..\NuGet.Services.Logging\NuGet.Services.Logging.csproj" />
3131
<ProjectReference Include="..\NuGet.Services.Sql\NuGet.Services.Sql.csproj" />
32+
<ProjectReference Include="..\NuGet.Services.Storage\NuGet.Services.Storage.csproj" />
3233
<ProjectReference Include="..\NuGetGallery.Core\NuGetGallery.Core.csproj" />
3334
</ItemGroup>
3435

src/NuGet.Jobs.Common/StorageAccountExtensions.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
using Autofac.Builder;
77
using Azure.Data.Tables;
88
using Azure.Identity;
9+
using Azure.Storage.Blobs;
910
using Microsoft.Extensions.Configuration;
1011
using Microsoft.Extensions.DependencyInjection;
1112
using Microsoft.Extensions.Options;
1213
using NuGet.Services.Configuration;
14+
using NuGet.Services.Storage;
1315
using NuGetGallery;
1416

1517
namespace NuGet.Jobs
@@ -180,6 +182,48 @@ private static CloudBlobClientWrapper CreateCloudBlobClient(
180182
requestTimeout);
181183
}
182184

185+
public static BlobServiceClient CreateBlobServiceClient(
186+
StorageMsiConfiguration storageMsiConfiguration,
187+
string storageConnectionString,
188+
TimeSpan? requestTimeout = null)
189+
{
190+
BlobClientOptions blobClientOptions = new BlobClientOptions();
191+
if (requestTimeout.HasValue)
192+
{
193+
blobClientOptions.Retry.NetworkTimeout = requestTimeout.Value;
194+
}
195+
196+
if (storageMsiConfiguration.UseManagedIdentity)
197+
{
198+
Uri blobEndpointUri = AzureStorage.GetPrimaryServiceUri(storageConnectionString);
199+
200+
if (string.IsNullOrWhiteSpace(storageMsiConfiguration.ManagedIdentityClientId))
201+
{
202+
// 1. Using MSI with DefaultAzureCredential (local debugging)
203+
return new BlobServiceClient(
204+
blobEndpointUri,
205+
new DefaultAzureCredential(),
206+
blobClientOptions);
207+
}
208+
else
209+
{
210+
// 2. Using MSI with ClientId
211+
return new BlobServiceClient(
212+
blobEndpointUri,
213+
new ManagedIdentityCredential(storageMsiConfiguration.ManagedIdentityClientId),
214+
blobClientOptions);
215+
}
216+
}
217+
else
218+
{
219+
// 3. Using SAS token
220+
// workaround for https://github.com/Azure/azure-sdk-for-net/issues/44373
221+
var connectionString = storageConnectionString.Replace("SharedAccessSignature=?", "SharedAccessSignature=");
222+
223+
return new BlobServiceClient(connectionString, blobClientOptions);
224+
}
225+
}
226+
183227
private static TableServiceClient CreateTableServiceClientClient(
184228
StorageMsiConfiguration msiConfiguration,
185229
string tableStorageConnectionString)

src/NuGet.Services.V3/NuGet.Services.V3.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="WindowsAzure.Storage" />
1011
<ProjectReference Include="..\Catalog\NuGet.Services.Metadata.Catalog.csproj" />
1112
<ProjectReference Include="..\Validation.Common.Job\Validation.Common.Job.csproj" />
1213
</ItemGroup>

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -386,11 +386,9 @@ private static void ConfigureLeaseService(ContainerBuilder builder)
386386
.Register(c =>
387387
{
388388
LeaseConfiguration config = c.Resolve<IOptionsSnapshot<LeaseConfiguration>>().Value;
389+
StorageMsiConfiguration storageMsiConfiguration = c.Resolve<IOptionsSnapshot<StorageMsiConfiguration>>().Value;
389390

390-
// workaround for https://github.com/Azure/azure-sdk-for-net/issues/44373
391-
var connectionString = config.ConnectionString.Replace("SharedAccessSignature=?", "SharedAccessSignature=");
392-
393-
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
391+
BlobServiceClient blobServiceClient = StorageAccountHelper.CreateBlobServiceClient(storageMsiConfiguration, config.ConnectionString);
394392
return new CloudBlobLeaseService(blobServiceClient, config.ContainerName, config.StoragePath);
395393
})
396394
.As<ILeaseService>();

src/Validation.Common.Job/Leases/CloudBlobLeaseService.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -10,7 +10,6 @@
1010
using Azure.Storage.Blobs;
1111
using Azure.Storage.Blobs.Specialized;
1212
using Azure.Storage.Blobs.Models;
13-
using System.IO;
1413

1514
namespace NuGet.Jobs.Validation.Leases
1615
{

src/Validation.Common.Job/Validation.Common.Job.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
2929
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" />
3030
<PackageReference Include="NuGet.Packaging" />
31-
<PackageReference Include="WindowsAzure.Storage" />
3231
<PackageReference Include="System.Formats.Asn1" />
3332
</ItemGroup>
3433

tests/Validation.Common.Job.Tests/Leases/BlobStorageFixture.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using Microsoft.WindowsAzure.Storage;
6-
using Microsoft.WindowsAzure.Storage.Blob;
5+
using Azure.Storage.Blobs;
76

87
namespace Validation.Common.Job.Tests.Leases
98
{
@@ -30,23 +29,21 @@ public BlobStorageFixture()
3029
TestRunId = Guid.NewGuid().ToString();
3130
ConnectionString = GetEnvironmentVariable(ConnectionStringName, required: true);
3231

33-
GetContainerReference().CreateIfNotExists();
32+
GetBlobContainerClient().CreateIfNotExists();
3433
}
3534

3635
public string TestRunId { get; }
3736
public string ConnectionString { get; }
3837

3938
public void Dispose()
4039
{
41-
GetContainerReference().DeleteIfExists();
40+
GetBlobContainerClient().DeleteIfExists();
4241
}
4342

44-
private CloudBlobContainer GetContainerReference()
43+
private BlobContainerClient GetBlobContainerClient()
4544
{
46-
var account = CloudStorageAccount.Parse(ConnectionString);
47-
var blobClient = account.CreateCloudBlobClient();
48-
var container = blobClient.GetContainerReference(TestRunId);
49-
return container;
45+
var blobServiceClient = new BlobServiceClient(ConnectionString);
46+
return blobServiceClient.GetBlobContainerClient(TestRunId);
5047
}
5148

5249
private static string GetEnvironmentVariable(string name, bool required)

0 commit comments

Comments
 (0)