Skip to content

Commit a4de2bd

Browse files
authored
[SDKMigration]Attempt to fix missing authentication. (#10245)
* Add parameter to use MI for storage. * Ignore SaS if using MI. Fix MI hydration. * Default to not using MI. * Attempt to pipe auth through. * Update DI for vuln2v3 for blobServiceClientFactory * Update DI for vuln2db
1 parent db3cc47 commit a4de2bd

32 files changed

Lines changed: 270 additions & 107 deletions

File tree

src/Catalog/Dnx/DnxCatalogCollector.cs

Lines changed: 3 additions & 3 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;
@@ -193,7 +193,7 @@ await catalogEntries.ForEachAsync(_maxConcurrentCommitItemsWithinBatch, async ca
193193
return processedCatalogEntries;
194194
}
195195

196-
private async Task<bool> AreRequiredPropertiesPresentAsync(Storage destinationStorage, Uri destinationUri)
196+
private async Task<bool> AreRequiredPropertiesPresentAsync(Persistence.Storage destinationStorage, Uri destinationUri)
197197
{
198198
var azureStorage = destinationStorage as IAzureStorage;
199199

@@ -561,4 +561,4 @@ internal static CatalogEntry Create(CatalogCommitItem item)
561561
}
562562
}
563563
}
564-
}
564+
}

src/Catalog/Dnx/DnxMaker.cs

Lines changed: 15 additions & 14 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;
@@ -16,6 +16,7 @@
1616
using NuGet.Versioning;
1717

1818
using ILogger = Microsoft.Extensions.Logging.ILogger;
19+
using CatalogStorage = NuGet.Services.Metadata.Catalog.Persistence.Storage;
1920

2021
namespace NuGet.Services.Metadata.Catalog.Dnx
2122
{
@@ -154,7 +155,7 @@ public async Task DeletePackageAsync(string id, string version, CancellationToke
154155
await DeleteNupkgAsync(storage, id, normalizedVersion, cancellationToken);
155156
}
156157

157-
public async Task<bool> HasPackageInIndexAsync(Storage storage, string id, string version, CancellationToken cancellationToken)
158+
public async Task<bool> HasPackageInIndexAsync(CatalogStorage storage, string id, string version, CancellationToken cancellationToken)
158159
{
159160
if (storage == null)
160161
{
@@ -179,7 +180,7 @@ public async Task<bool> HasPackageInIndexAsync(Storage storage, string id, strin
179180
return versionsContext.Versions.Contains(parsedVersion);
180181
}
181182

182-
private async Task<Uri> SaveNuspecAsync(Storage storage, string id, string version, string nuspec, CancellationToken cancellationToken)
183+
private async Task<Uri> SaveNuspecAsync(CatalogStorage storage, string id, string version, string nuspec, CancellationToken cancellationToken)
183184
{
184185
var relativeAddress = GetRelativeAddressNuspec(id, version);
185186
var nuspecUri = new Uri(storage.BaseAddress, relativeAddress);
@@ -230,7 +231,7 @@ public async Task UpdatePackageVersionIndexAsync(string id, Action<HashSet<NuGet
230231
}
231232
}
232233

233-
private async Task<VersionsResult> GetVersionsAsync(Storage storage, CancellationToken cancellationToken)
234+
private async Task<VersionsResult> GetVersionsAsync(CatalogStorage storage, CancellationToken cancellationToken)
234235
{
235236
var relativeAddress = "index.json";
236237
var resourceUri = new Uri(storage.BaseAddress, relativeAddress);
@@ -265,7 +266,7 @@ private StorageContent CreateContent(IEnumerable<string> versions)
265266
return new StringStorageContent(obj.ToString(), "application/json", Constants.NoStoreCacheControl);
266267
}
267268

268-
private async Task<Uri> SaveNupkgAsync(Stream nupkgStream, Storage storage, string id, string version, CancellationToken cancellationToken)
269+
private async Task<Uri> SaveNupkgAsync(Stream nupkgStream, CatalogStorage storage, string id, string version, CancellationToken cancellationToken)
269270
{
270271
Uri nupkgUri = new Uri(storage.BaseAddress, GetRelativeAddressNupkg(id, version));
271272
var content = new StreamStorageContent(
@@ -280,7 +281,7 @@ private async Task<Uri> SaveNupkgAsync(Stream nupkgStream, Storage storage, stri
280281

281282
private async Task<Uri> CopyNupkgAsync(
282283
IStorage sourceStorage,
283-
Storage destinationStorage,
284+
CatalogStorage destinationStorage,
284285
string id, string version, CancellationToken cancellationToken)
285286
{
286287
var packageFileName = PackageUtility.GetPackageFileName(id, version);
@@ -300,7 +301,7 @@ await sourceStorage.CopyAsync(
300301

301302
private async Task CopyIconFromAzureStorageIfExistAsync(
302303
IAzureStorage sourceStorage,
303-
Storage destinationStorage,
304+
CatalogStorage destinationStorage,
304305
string packageId,
305306
string normalizedPackageVersion,
306307
string iconFilename,
@@ -321,7 +322,7 @@ await CopyIconAsync(
321322
private async Task CopyIconFromNupkgStreamAsync(
322323
Stream nupkgStream,
323324
string iconFilename,
324-
Storage destinationStorage,
325+
CatalogStorage destinationStorage,
325326
string packageId,
326327
string normalizedPackageVersion,
327328
CancellationToken cancellationToken)
@@ -338,7 +339,7 @@ await CopyIconAsync(
338339
private async Task CopyIconAsync(
339340
Stream packageStream,
340341
string iconFilename,
341-
Storage destinationStorage,
342+
CatalogStorage destinationStorage,
342343
string packageId,
343344
string normalizedPackageVersion,
344345
CancellationToken cancellationToken)
@@ -366,7 +367,7 @@ await ExtractAndStoreIconAsync(
366367
private async Task ExtractAndStoreIconAsync(
367368
Stream packageStream,
368369
string iconPath,
369-
Storage destinationStorage,
370+
CatalogStorage destinationStorage,
370371
Uri destinationUri,
371372
CancellationToken cancellationToken,
372373
string packageId,
@@ -406,7 +407,7 @@ private async Task<Stream> GetPackageStreamAsync(
406407
return await packageSourceBlob.GetStreamAsync(cancellationToken);
407408
}
408409

409-
private async Task DeleteNuspecAsync(Storage storage, string id, string version, CancellationToken cancellationToken)
410+
private async Task DeleteNuspecAsync(CatalogStorage storage, string id, string version, CancellationToken cancellationToken)
410411
{
411412
string relativeAddress = GetRelativeAddressNuspec(id, version);
412413
Uri nuspecUri = new Uri(storage.BaseAddress, relativeAddress);
@@ -416,7 +417,7 @@ private async Task DeleteNuspecAsync(Storage storage, string id, string version,
416417
}
417418
}
418419

419-
private async Task DeleteNupkgAsync(Storage storage, string id, string version, CancellationToken cancellationToken)
420+
private async Task DeleteNupkgAsync(CatalogStorage storage, string id, string version, CancellationToken cancellationToken)
420421
{
421422
string relativeAddress = GetRelativeAddressNupkg(id, version);
422423
Uri nupkgUri = new Uri(storage.BaseAddress, relativeAddress);
@@ -426,7 +427,7 @@ private async Task DeleteNupkgAsync(Storage storage, string id, string version,
426427
}
427428
}
428429

429-
private async Task DeleteIconAsync(Storage storage, string id, string version, CancellationToken cancellationToken)
430+
private async Task DeleteIconAsync(CatalogStorage storage, string id, string version, CancellationToken cancellationToken)
430431
{
431432
string relativeAddress = GetRelativeAddressIcon(id, version);
432433
Uri iconUri = new Uri(storage.BaseAddress, relativeAddress);
@@ -479,4 +480,4 @@ public VersionsResult(string relativeAddress, Uri resourceUri, HashSet<NuGetVers
479480
public HashSet<NuGetVersion> Versions { get; }
480481
}
481482
}
482-
}
483+
}

src/Catalog/DurableCursor.cs

Lines changed: 4 additions & 4 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;
@@ -12,10 +12,10 @@ namespace NuGet.Services.Metadata.Catalog
1212
public class DurableCursor : ReadWriteCursor
1313
{
1414
Uri _address;
15-
Storage _storage;
15+
Persistence.Storage _storage;
1616
DateTime _defaultValue;
1717

18-
public DurableCursor(Uri address, Storage storage, DateTime defaultValue)
18+
public DurableCursor(Uri address, Persistence.Storage storage, DateTime defaultValue)
1919
{
2020
_address = address;
2121
_storage = storage;
@@ -43,4 +43,4 @@ public override async Task LoadAsync(CancellationToken cancellationToken)
4343
Value = obj["value"].ToObject<DateTime>();
4444
}
4545
}
46-
}
46+
}

src/Catalog/Helpers/DeletionAuditEntry.cs

Lines changed: 3 additions & 3 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;
@@ -163,7 +163,7 @@ public static Task<IEnumerable<DeletionAuditEntry>> GetAsync(
163163
DateTime? maxTime = null,
164164
ILogger logger = null)
165165
{
166-
Storage storage = auditingStorageFactory.Create(package != null ? GetAuditRecordPrefixFromPackage(package) : null);
166+
Persistence.Storage storage = auditingStorageFactory.Create(package != null ? GetAuditRecordPrefixFromPackage(package) : null);
167167
return GetAsync(storage, cancellationToken, minTime, maxTime, logger);
168168
}
169169

@@ -258,4 +258,4 @@ private static bool IsPackageDelete(StorageListItem auditRecord)
258258
return FileNameSuffixes.Any(suffix => fileName.EndsWith(suffix));
259259
}
260260
}
261-
}
261+
}

src/Catalog/Icons/CatalogLeafDataProcessor.cs

Lines changed: 4 additions & 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;
@@ -12,6 +12,8 @@
1212
using NuGet.Services.Metadata.Catalog.Helpers;
1313
using NuGet.Services.Metadata.Catalog.Persistence;
1414

15+
using CatalogStorage = NuGet.Services.Metadata.Catalog.Persistence.Storage;
16+
1517
namespace NuGet.Services.Metadata.Catalog.Icons
1618
{
1719
public class CatalogLeafDataProcessor : ICatalogLeafDataProcessor
@@ -42,7 +44,7 @@ public CatalogLeafDataProcessor(
4244
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
4345
}
4446

45-
public async Task ProcessPackageDeleteLeafAsync(Storage storage, CatalogCommitItem item, CancellationToken cancellationToken)
47+
public async Task ProcessPackageDeleteLeafAsync(CatalogStorage storage, CatalogCommitItem item, CancellationToken cancellationToken)
4648
{
4749
var targetStoragePath = GetTargetStorageIconPath(item);
4850
await _iconProcessor.DeleteIconAsync(storage, targetStoragePath, cancellationToken, item.PackageIdentity.Id, item.PackageIdentity.Version.ToNormalizedString());
Lines changed: 3 additions & 3 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.Threading;
@@ -9,7 +9,7 @@ namespace NuGet.Services.Metadata.Catalog.Icons
99
{
1010
public interface ICatalogLeafDataProcessor
1111
{
12-
Task ProcessPackageDeleteLeafAsync(Storage storage, CatalogCommitItem item, CancellationToken cancellationToken);
12+
Task ProcessPackageDeleteLeafAsync(Persistence.Storage storage, CatalogCommitItem item, CancellationToken cancellationToken);
1313
Task ProcessPackageDetailsLeafAsync(IStorage destinationStorage, IStorage iconCacheStorage, CatalogCommitItem item, string iconUrlString, string iconFile, CancellationToken cancellationToken);
1414
}
15-
}
15+
}

src/Catalog/NuGet.Services.Metadata.Catalog.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
<ProjectReference Include="..\NuGet.Services.Configuration\NuGet.Services.Configuration.csproj" />
7777
<ProjectReference Include="..\NuGet.Services.Logging\NuGet.Services.Logging.csproj" />
7878
<ProjectReference Include="..\NuGet.Services.Sql\NuGet.Services.Sql.csproj" />
79+
<ProjectReference Include="..\NuGet.Services.Storage\NuGet.Services.Storage.csproj" />
7980
<ProjectReference Include="..\NuGetGallery.Core\NuGetGallery.Core.csproj" />
8081
</ItemGroup>
8182

src/Catalog/Persistence/AzureStorage.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
using System.Threading;
1212
using System.Threading.Tasks;
1313
using Azure;
14+
using Azure.Identity;
1415
using Azure.Storage.Blobs;
1516
using Azure.Storage.Blobs.Models;
1617
using Azure.Storage.Blobs.Specialized;
1718
using NuGet.Protocol;
1819
using NuGet.Services.Metadata.Catalog.Extensions;
20+
using NuGet.Services.Storage;
1921
using NuGetGallery;
2022

2123
namespace NuGet.Services.Metadata.Catalog.Persistence
@@ -33,7 +35,7 @@ public class AzureStorage : Storage, IAzureStorage
3335
public static readonly TimeSpan DefaultMaxExecutionTime = TimeSpan.FromMinutes(10);
3436

3537
public AzureStorage(
36-
BlobServiceClient blobServiceClient,
38+
IBlobServiceClientFactory blobServiceClient,
3739
string containerName,
3840
string path,
3941
Uri baseAddress,
@@ -92,7 +94,7 @@ private static ICloudBlobDirectory GetCloudBlobDirectoryUri(Uri storageBaseUri)
9294

9395
var blobEndpoint = new Uri(storageBaseUri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped));
9496
// Create BlobServiceClient with anonymous credentials
95-
var blobServiceClient = new BlobServiceClient(blobEndpoint);
97+
var blobServiceClient = new BlobServiceClientFactory(blobEndpoint, new DefaultAzureCredential());
9698

9799
string containerName = pathSegments[0];
98100
string pathInContainer = string.Join("/", pathSegments.Skip(1));

src/Catalog/Persistence/AzureStorageFactory.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
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;
55
using Azure.Storage.Blobs;
66
using NuGet.Protocol;
7+
using NuGet.Services.Storage;
78

89
namespace NuGet.Services.Metadata.Catalog.Persistence
910
{
1011
public class AzureStorageFactory : StorageFactory
1112
{
12-
private readonly BlobServiceClient _blobServiceClient;
13+
private readonly IBlobServiceClientFactory _blobServiceClient;
1314
private readonly string _containerName;
1415
private readonly string _path;
1516
private readonly Uri _differentBaseAddress = null;
@@ -19,7 +20,7 @@ public class AzureStorageFactory : StorageFactory
1920
private readonly bool _initializeContainer;
2021

2122
public AzureStorageFactory(
22-
BlobServiceClient blobServiceClient,
23+
IBlobServiceClientFactory blobServiceClient,
2324
string containerName,
2425
TimeSpan maxExecutionTime,
2526
TimeSpan serverTimeout,
@@ -107,4 +108,4 @@ public override Storage Create(string name = null)
107108
Throttle);
108109
}
109110
}
110-
}
111+
}

src/Catalog/Persistence/CloudBlobDirectoryWrapper.cs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,32 @@
55
using System.Collections.Generic;
66
using System.Threading;
77
using System.Threading.Tasks;
8+
using Azure.Core;
9+
using Azure.Core.Pipeline;
810
using Azure.Storage.Blobs;
911
using Azure.Storage.Blobs.Models;
1012
using Azure.Storage.Blobs.Specialized;
13+
using NuGet.Services.Storage;
1114

1215
namespace NuGet.Services.Metadata.Catalog.Persistence
1316
{
1417
public class CloudBlobDirectoryWrapper : ICloudBlobDirectory
1518
{
19+
private readonly IBlobServiceClientFactory _blobServiceClientFactory;
1620
private readonly BlobContainerClient _containerClient;
1721
private readonly string _directoryPrefix;
1822
private readonly IBlobContainerClientWrapper _blobContainerClientWrapper;
1923
private readonly BlobClientOptions _defaultClientOptions;
2024

21-
public BlobServiceClient ServiceClient => _containerClient.GetParentBlobServiceClient();
25+
public IBlobServiceClientFactory ServiceClient => new SimpleBlobServiceClientFactory(_containerClient.GetParentBlobServiceClient());
2226
public Uri Uri { get; }
2327
public string DirectoryPrefix => _directoryPrefix;
2428
public BlobClientOptions ContainerOptions => _defaultClientOptions;
2529
public IBlobContainerClientWrapper ContainerClientWrapper => _blobContainerClientWrapper;
2630

27-
public CloudBlobDirectoryWrapper(BlobServiceClient serviceClient, string containerName, string directoryPrefix, BlobClientOptions blobClientOptions = null)
31+
public CloudBlobDirectoryWrapper(IBlobServiceClientFactory serviceClientFactory, string containerName, string directoryPrefix, BlobClientOptions blobClientOptions = null)
2832
{
33+
_blobServiceClientFactory = serviceClientFactory ?? throw new ArgumentNullException(nameof(serviceClientFactory));
2934
_directoryPrefix = directoryPrefix ?? throw new ArgumentNullException(nameof(directoryPrefix));
3035

3136
if (string.IsNullOrWhiteSpace(containerName))
@@ -35,19 +40,9 @@ public CloudBlobDirectoryWrapper(BlobServiceClient serviceClient, string contain
3540

3641
_defaultClientOptions = blobClientOptions ?? new BlobClientOptions();
3742

38-
// Create the container client using the provided or default options
39-
if (blobClientOptions != null)
40-
{
41-
// Extract necessary information
42-
Uri serviceUri = serviceClient.Uri;
43-
// Create a new BlobServiceClient instance with the new options
44-
var newServiceClient = new BlobServiceClient(serviceUri, _defaultClientOptions);
45-
_containerClient = newServiceClient.GetBlobContainerClient(containerName);
46-
}
47-
else
48-
{
49-
_containerClient = serviceClient.GetBlobContainerClient(containerName);
50-
}
43+
// Request a new BlobServiceClient instance with the current BlobClientOptions
44+
var serviceClient = _blobServiceClientFactory.GetBlobServiceClient(blobClientOptions);
45+
_containerClient = serviceClient.GetBlobContainerClient(containerName);
5146

5247
Uri = new Uri(Storage.RemoveQueryString(_containerClient.Uri).TrimEnd('/') + "/" + _directoryPrefix);
5348

0 commit comments

Comments
 (0)