Skip to content

Commit 12b0850

Browse files
authored
Display License file from blob storage/local files (#6675)
* Display license file from blob storage * fix some reviews and add tests * Support local license files and add more tests * Interface for ServiceDiscoveryClient and some changes * Update * some updates
1 parent 57e2420 commit 12b0850

15 files changed

Lines changed: 381 additions & 7 deletions
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Threading.Tasks;
7+
8+
namespace NuGet.Services.Search.Client
9+
{
10+
/// <summary>
11+
/// This interface is used to discover the service client.
12+
/// </summary>
13+
public interface IServiceDiscoveryClient
14+
{
15+
/// <summary>
16+
/// The function is used to retrieve the endpoint for the resource type.
17+
/// </summary>
18+
/// <param name="resourceType"> The resource type needed for retrieval</param>
19+
Task<IEnumerable<Uri>> GetEndpointsForResourceType(string resourceType);
20+
}
21+
}

src/NuGet.Services.Search.Client/Client/ServiceDiscoveryClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace NuGet.Services.Search.Client
1212
{
13-
public class ServiceDiscoveryClient
13+
public class ServiceDiscoveryClient : IServiceDiscoveryClient
1414
{
1515
private readonly HttpClient _httpClient;
1616
private readonly Uri _serviceDiscoveryEndpoint;

src/NuGet.Services.Search.Client/NuGet.Services.Search.Client.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<Compile Include="Client\BaseUrlHealthIndicatorStore.cs" />
4949
<Compile Include="Client\IEndpointHealthIndicatorStore.cs" />
5050
<Compile Include="Client\IHealthIndicatorLogger.cs" />
51+
<Compile Include="Client\IServiceDiscoveryClient.cs" />
5152
<Compile Include="Client\NullHealthIndicatorLogger.cs" />
5253
<Compile Include="Client\RetryingHttpClientWrapper.cs" />
5354
<Compile Include="Client\SearchClient.cs" />

src/NuGetGallery.Core/Services/CorePackageFileService.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ namespace NuGetGallery
1313
{
1414
public class CorePackageFileService : ICorePackageFileService
1515
{
16-
private const string LicenseFileName = "license";
17-
1816
private readonly ICoreFileStorageService _fileStorageService;
1917
private readonly IFileMetadataService _metadata;
2018

@@ -263,7 +261,7 @@ public Task DeleteLicenseFileAsync(string id, string version)
263261
return _fileStorageService.DeleteFileAsync(_metadata.PackageContentFolderName, fileName);
264262
}
265263

266-
private string LicensePathTemplate => $"{_metadata.PackageContentPathTemplate}/{LicenseFileName}";
264+
private string LicensePathTemplate => $"{_metadata.PackageContentPathTemplate}/{CoreConstants.LicenseFileName}";
267265

268266
private string BuildLicenseFileName(Package package)
269267
=> BuildFileName(package, LicensePathTemplate, string.Empty);

src/NuGetGallery/App_Start/DefaultDependenciesModule.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using NuGet.Services.Logging;
2626
using NuGet.Services.Messaging;
2727
using NuGet.Services.Messaging.Email;
28+
using NuGet.Services.Search.Client;
2829
using NuGet.Services.ServiceBus;
2930
using NuGet.Services.Sql;
3031
using NuGet.Services.Validation;
@@ -332,6 +333,14 @@ protected override void Load(ContainerBuilder builder)
332333
.As<ITyposquattingCheckListCacheService>()
333334
.SingleInstance();
334335

336+
builder.RegisterType<FlatContainerService>()
337+
.As<IFlatContainerService>()
338+
.InstancePerLifetimeScope();
339+
340+
builder.Register<ServiceDiscoveryClient>(c =>
341+
new ServiceDiscoveryClient(c.Resolve<IAppConfiguration>().ServiceDiscoveryUri))
342+
.As<IServiceDiscoveryClient>();
343+
335344
RegisterMessagingService(builder, configuration);
336345

337346
builder.Register(c => HttpContext.Current.User)

src/NuGetGallery/App_Start/Routes.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ public static void RegisterUIRoutes(RouteCollection routes)
227227
"account/ConfirmationRequired",
228228
new { controller = "Users", action = "ConfirmationRequired" });
229229

230+
routes.MapRoute(
231+
RouteName.License,
232+
"packages/{id}/{version}/license",
233+
new { controller = "Packages", action = "License" });
234+
230235
//Redirecting v1 Confirmation Route
231236
routes.Redirect(
232237
r => r.MapRoute(

src/NuGetGallery/Controllers/PackagesController.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public partial class PackagesController
9494
private readonly IContentObjectService _contentObjectService;
9595
private readonly ISymbolPackageUploadService _symbolPackageUploadService;
9696
private readonly IDiagnosticsSource _trace;
97+
private readonly IFlatContainerService _flatContainerService;
9798

9899
public PackagesController(
99100
IPackageService packageService,
@@ -118,7 +119,8 @@ public PackagesController(
118119
IPackageOwnershipManagementService packageOwnershipManagementService,
119120
IContentObjectService contentObjectService,
120121
ISymbolPackageUploadService symbolPackageUploadService,
121-
IDiagnosticsService diagnosticsService)
122+
IDiagnosticsService diagnosticsService,
123+
IFlatContainerService flatContainerService)
122124
{
123125
_packageService = packageService;
124126
_uploadFileService = uploadFileService;
@@ -143,6 +145,7 @@ public PackagesController(
143145
_contentObjectService = contentObjectService;
144146
_symbolPackageUploadService = symbolPackageUploadService;
145147
_trace = diagnosticsService?.SafeGetSource(nameof(PackagesController)) ?? throw new ArgumentNullException(nameof(diagnosticsService));
148+
_flatContainerService = flatContainerService;
146149
}
147150

148151
[HttpGet]
@@ -663,6 +666,36 @@ public virtual async Task<ActionResult> DisplayPackage(string id, string version
663666
return View(model);
664667
}
665668

669+
public virtual async Task<ActionResult> License(string id, string version)
670+
{
671+
var package = _packageService.FindPackageByIdAndVersionStrict(id, version);
672+
if (package == null)
673+
{
674+
return HttpNotFound();
675+
}
676+
677+
if (package.EmbeddedLicenseType == EmbeddedLicenseFileType.Absent)
678+
{
679+
return HttpNotFound();
680+
}
681+
682+
if (!_config.AsynchronousPackageValidationEnabled)
683+
{
684+
try
685+
{
686+
var licenseFileContent = await _packageFileService.DownloadLicenseFileAsync(package);
687+
return new FileStreamResult(licenseFileContent, "text/plain");
688+
}
689+
catch (Exception ex)
690+
{
691+
_telemetryService.TraceException(ex);
692+
return HttpNotFound();
693+
}
694+
}
695+
696+
return Redirect(await _flatContainerService.GetLicenseFileFlatContainerUrlAsync(package.Id, package.NormalizedVersion));
697+
}
698+
666699
public virtual async Task<ActionResult> ListPackages(PackageListSearchViewModel searchAndListModel)
667700
{
668701
var page = searchAndListModel.Page;

src/NuGetGallery/GalleryConstants.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public static class GalleryConstants
6060
public const string UrlValidationRegEx = @"(https?):\/\/[^ ""]+$";
6161
public const string UrlValidationErrorMessage = "This doesn't appear to be a valid HTTP/HTTPS URL";
6262

63+
public const string PackageBaseAddress = "PackageBaseAddress/3.0.0";
64+
6365
// Note: regexes must be tested to work in JavaScript
6466
// We do NOT follow strictly the RFCs at this time, and we choose not to support many obscure email address variants.
6567
// Specifically the following are not supported by-design:

src/NuGetGallery/NuGetGallery.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,8 @@
443443
<Compile Include="Services\ITyposquattingService.cs" />
444444
<Compile Include="Services\IValidationMessage.cs" />
445445
<Compile Include="Services\JsonValidationMessage.cs" />
446+
<Compile Include="Services\IFlatContainerService.cs" />
447+
<Compile Include="Services\FlatContainerService.cs" />
446448
<Compile Include="Services\LicenseUrlDeprecationValidationMessage.cs" />
447449
<Compile Include="Services\PackageShouldNotBeSignedUserFixableValidationMessage.cs" />
448450
<Compile Include="Services\PlainTextOnlyValidationMessage.cs" />

src/NuGetGallery/RouteName.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,6 @@ public static class RouteName
9898
public const string GetOrganizationCertificate = "GetOrganizationCertificate";
9999
public const string GetOrganizationCertificates = "GetOrganizationCertificates";
100100
public const string SetRequiredSigner = "SetRequiredSigner";
101+
public const string License = "License";
101102
}
102103
}

0 commit comments

Comments
 (0)