Skip to content

Commit 92b50b3

Browse files
authored
[NewSDK]Update ValidateCertificate and ProcessSignature for new SDK (#10158)
* First attempt rebase sign changes on dev. * Use constant values for config. Move GetServiceUri to common. * Use new method to get Uri base.
1 parent 746a360 commit 92b50b3

3 files changed

Lines changed: 49 additions & 3 deletions

File tree

  • src
    • NuGet.Services.Storage
    • Validation.PackageSigning.ProcessSignature
    • Validation.PackageSigning.ValidateCertificate

src/NuGet.Services.Storage/AzureStorage.cs

Lines changed: 8 additions & 1 deletion
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;
@@ -419,5 +419,12 @@ private Uri ResolvePathedUri(string filename)
419419
{
420420
return ResolveUri(Path.Combine(_path, filename));
421421
}
422+
423+
public static Uri GetPrimaryServiceUri(string storageConnectionString)
424+
{
425+
var tempClient = new BlobServiceClient(storageConnectionString);
426+
// if _storageConnectionString has SAS token, Uri will contain SAS signature, we need to strip it
427+
return new Uri(tempClient.Uri.GetLeftPart(UriPartial.Path));
428+
}
422429
}
423430
}

src/Validation.PackageSigning.ProcessSignature/Job.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// 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

4+
using System;
45
using Autofac;
6+
using Azure.Identity;
57
using Azure.Storage.Blobs;
68
using Microsoft.Extensions.Configuration;
79
using Microsoft.Extensions.DependencyInjection;
@@ -13,6 +15,7 @@
1315
using NuGet.Jobs.Validation.PackageSigning.Storage;
1416
using NuGet.Jobs.Validation.PackageSigning.Telemetry;
1517
using NuGet.Jobs.Validation.Storage;
18+
using NuGet.Services.Configuration;
1619
using NuGet.Services.ServiceBus;
1720
using NuGet.Services.Storage;
1821
using NuGet.Services.Validation.PackageSigning.ProcessSignature;
@@ -49,8 +52,24 @@ protected override void ConfigureJobServices(IServiceCollection services, IConfi
4952

5053
services.AddTransient<ICertificateStore>(p =>
5154
{
55+
var useStorageManagedIdentity = bool.Parse(configurationRoot[Constants.StorageUseManagedIdentityPropertyName]);
5256
var config = p.GetRequiredService<IOptionsSnapshot<CertificateStoreConfiguration>>().Value;
53-
var targetStorageAccount = new BlobServiceClient(AzureStorageFactory.PrepareConnectionString(config.DataStorageAccount));
57+
58+
BlobServiceClient targetStorageAccount;
59+
if (useStorageManagedIdentity)
60+
{
61+
var managedIdentityClientId =
62+
string.IsNullOrEmpty(configurationRoot[Constants.StorageManagedIdentityClientIdPropertyName]) ?
63+
configurationRoot[Constants.ManagedIdentityClientIdKey] :
64+
configurationRoot[Constants.StorageManagedIdentityClientIdPropertyName];
65+
var storageAccountUri = AzureStorage.GetPrimaryServiceUri(config.DataStorageAccount);
66+
var managedIdentity = new ManagedIdentityCredential(managedIdentityClientId);
67+
targetStorageAccount = new BlobServiceClient(storageAccountUri, managedIdentity);
68+
}
69+
else
70+
{
71+
targetStorageAccount = new BlobServiceClient(AzureStorageFactory.PrepareConnectionString(config.DataStorageAccount));
72+
}
5473

5574
var storageFactory = new AzureStorageFactory(
5675
targetStorageAccount,

src/Validation.PackageSigning.ValidateCertificate/Job.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
// 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

4+
using System;
45
using Autofac;
6+
using Azure.Identity;
57
using Azure.Storage.Blobs;
68
using Microsoft.Extensions.Configuration;
79
using Microsoft.Extensions.DependencyInjection;
810
using Microsoft.Extensions.Logging;
911
using Microsoft.Extensions.Options;
12+
using NuGet.Jobs;
1013
using NuGet.Jobs.Validation;
1114
using NuGet.Jobs.Validation.PackageSigning.Configuration;
1215
using NuGet.Jobs.Validation.PackageSigning.Messages;
1316
using NuGet.Jobs.Validation.PackageSigning.Storage;
17+
using NuGet.Services.Configuration;
1418
using NuGet.Services.ServiceBus;
1519
using NuGet.Services.Storage;
1620

@@ -30,8 +34,24 @@ protected override void ConfigureJobServices(IServiceCollection services, IConfi
3034

3135
services.AddTransient<ICertificateStore>(p =>
3236
{
37+
var useStorageManagedIdentity = bool.Parse(configurationRoot[Constants.StorageUseManagedIdentityPropertyName]);
3338
var config = p.GetRequiredService<IOptionsSnapshot<CertificateStoreConfiguration>>().Value;
34-
var targetStorageAccount = new BlobServiceClient(AzureStorageFactory.PrepareConnectionString(config.DataStorageAccount));
39+
40+
BlobServiceClient targetStorageAccount;
41+
if (useStorageManagedIdentity)
42+
{
43+
var managedIdentityClientId =
44+
string.IsNullOrEmpty(configurationRoot[Constants.StorageManagedIdentityClientIdPropertyName]) ?
45+
configurationRoot[Constants.ManagedIdentityClientIdKey] :
46+
configurationRoot[Constants.StorageManagedIdentityClientIdPropertyName];
47+
var storageAccountUri = AzureStorage.GetPrimaryServiceUri(config.DataStorageAccount);
48+
var managedIdentity = new ManagedIdentityCredential(managedIdentityClientId);
49+
targetStorageAccount = new BlobServiceClient(storageAccountUri, managedIdentity);
50+
}
51+
else
52+
{
53+
targetStorageAccount = new BlobServiceClient(AzureStorageFactory.PrepareConnectionString(config.DataStorageAccount));
54+
}
3555

3656
var storageFactory = new AzureStorageFactory(
3757
targetStorageAccount,

0 commit comments

Comments
 (0)