Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add package version to query string for v2 CDN endpoint (#10082) #10083

Merged
merged 6 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/NuGetGallery.Core/CoreConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public static class CoreConstants

public const string DefaultCacheControl = "max-age=120";

public const string PackageVersionParameterName = "packageVersion";

public static class Folders
{
public const string UserCertificatesFolderName = "user-certificates";
Expand Down
44 changes: 28 additions & 16 deletions src/NuGetGallery.Services/Storage/CloudBlobFileStorageService.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using NuGetGallery.Configuration;
using NuGetGallery.Diagnostics;

using System;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using NuGetGallery.Configuration;
using NuGetGallery.Diagnostics;

namespace NuGetGallery
{
Expand All @@ -21,35 +23,36 @@ public CloudBlobFileStorageService(
IAppConfiguration configuration,
ISourceDestinationRedirectPolicy redirectPolicy,
IDiagnosticsService diagnosticsService,
ICloudBlobContainerInformationProvider cloudBlobFolderInformationProvider)
ICloudBlobContainerInformationProvider cloudBlobFolderInformationProvider)
: base(client, diagnosticsService, cloudBlobFolderInformationProvider)
{
_configuration = configuration;
_redirectPolicy = redirectPolicy;
}

public async Task<ActionResult> CreateDownloadFileActionResultAsync(Uri requestUrl, string folderName, string fileName)
public async Task<ActionResult> CreateDownloadFileActionResultAsync(Uri requestUrl, string folderName, string fileName, string versionParameter)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a low level method not specific to a package or version. What do you think about a simple queryString parameter instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would allow flexibility at the call site.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked a bit closer and the easiest approach hardcodes the parameter name lower down--this just passes the version. Making it more generic would also put the burden on a properly urlencoded query string on the caller.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems okay to me. A Dictionary<string, string> would be more flexible. but it's not a big deal to me.

{
ICloudBlobContainer container = await GetContainerAsync(folderName);
var blob = container.GetBlobReference(fileName);

var redirectUri = GetRedirectUri(requestUrl, blob.Uri);
var redirectUri = GetRedirectUri(requestUrl, blob.Uri, versionParameter);
return new RedirectResult(redirectUri.AbsoluteUri, false);
}

internal async Task<ActionResult> CreateDownloadFileActionResult(
HttpContextBase httpContext,
string folderName,
string fileName)
string fileName,
string versionParameter)
{
var container = await GetContainerAsync(folderName);
var blob = container.GetBlobReference(fileName);

var redirectUri = GetRedirectUri(httpContext.Request.Url, blob.Uri);
var redirectUri = GetRedirectUri(httpContext.Request.Url, blob.Uri, versionParameter);
return new RedirectResult(redirectUri.AbsoluteUri, false);
}

internal Uri GetRedirectUri(Uri requestUrl, Uri blobUri)
internal Uri GetRedirectUri(Uri requestUrl, Uri blobUri, string versionParameter)
{
if (!_redirectPolicy.IsAllowed(requestUrl, blobUri))
{
Expand Down Expand Up @@ -79,19 +82,17 @@ internal Uri GetRedirectUri(Uri requestUrl, Uri blobUri)
// When no blob query string is passed, we forward the request
// URI's query string to the CDN. See https://github.com/NuGet/NuGetGallery/issues/3168
// and related PR's.
var queryString = !string.IsNullOrEmpty(blobUri.Query)
? blobUri.Query
: requestUrl.Query;
var queryStringUri = !string.IsNullOrEmpty(blobUri.Query)
? blobUri
: requestUrl;

if (!string.IsNullOrEmpty(queryString))
{
queryString = queryString.TrimStart('?');
}
var queryValues = ParseQueryString(queryStringUri);
jimmylewis marked this conversation as resolved.
Show resolved Hide resolved
queryValues.Add(CoreConstants.PackageVersionParameterName, versionParameter);

var urlBuilder = new UriBuilder(scheme, host, port)
{
Path = blobUri.LocalPath,
Query = queryString
Query = queryValues.ToString()
};

return urlBuilder.Uri;
Expand All @@ -102,5 +103,16 @@ public async Task<bool> IsAvailableAsync()
var container = await GetContainerAsync(CoreConstants.Folders.PackagesFolderName);
return await container.ExistsAsync(cloudBlobLocationMode: null);
}

private static NameValueCollection ParseQueryString(Uri uri)
{
int queryIndex = uri.Query?.LastIndexOf("?") ?? -1;
clairernovotny marked this conversation as resolved.
Show resolved Hide resolved
if (queryIndex == -1) return new NameValueCollection();

string query = uri.Query.Substring(queryIndex);

var nvcol = HttpUtility.ParseQueryString(query);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explicit type please

return nvcol;
}
}
}
2 changes: 1 addition & 1 deletion src/NuGetGallery.Services/Storage/IFileStorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace NuGetGallery
{
public interface IFileStorageService : ICoreFileStorageService
{
Task<ActionResult> CreateDownloadFileActionResultAsync(Uri requestUrl, string folderName, string fileName);
Task<ActionResult> CreateDownloadFileActionResultAsync(Uri requestUrl, string folderName, string fileName, string versionParameter);

Task<bool> IsAvailableAsync();
}
Expand Down
7 changes: 4 additions & 3 deletions src/NuGetGallery/Services/FileSystemFileStorageService.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using NuGetGallery.Configuration;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this file and a few others modified in this PR don't adhere to the new style rules put in place by this PR (.editorconfig).


using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Threading.Tasks;
using System.Web.Hosting;
using System.Web.Mvc;
using NuGetGallery.Configuration;

namespace NuGetGallery
{
Expand All @@ -23,7 +24,7 @@ public FileSystemFileStorageService(IAppConfiguration configuration, IFileSystem
_fileSystemService = fileSystemService;
}

public Task<ActionResult> CreateDownloadFileActionResultAsync(Uri requestUrl, string folderName, string fileName)
public Task<ActionResult> CreateDownloadFileActionResultAsync(Uri requestUrl, string folderName, string fileName, string versionParameter)
{
if (string.IsNullOrWhiteSpace(folderName))
{
Expand Down Expand Up @@ -270,7 +271,7 @@ public Task SetMetadataAsync(

public Task SetPropertiesAsync(
string folderName,
string fileName,
string fileName,
Func<Lazy<Task<Stream>>, ICloudBlobProperties, Task<bool>> updatePropertiesAsync)
{
return Task.CompletedTask;
Expand Down
16 changes: 11 additions & 5 deletions src/NuGetGallery/Services/PackageFileService.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using NuGet.Services.Entities;

using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using NuGet.Services.Entities;

namespace NuGetGallery
{
Expand All @@ -28,13 +29,18 @@ public PackageFileService(IFileStorageService fileStorageService)
public Task<ActionResult> CreateDownloadPackageActionResultAsync(Uri requestUrl, Package package)
joelverhagen marked this conversation as resolved.
Show resolved Hide resolved
{
var fileName = FileNameHelper.BuildFileName(package, CoreConstants.PackageFileSavePathTemplate, CoreConstants.NuGetPackageFileExtension);
return _fileStorageService.CreateDownloadFileActionResultAsync(requestUrl, CoreConstants.Folders.PackagesFolderName, fileName);

var packageVersion = NuGetVersionFormatter.GetNormalizedPackageVersion(package).ToLowerInvariant(); // will not return null bc BuildFileName will throw if values are null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why lower invariant? Generally we use the original casing when we have it and allow the user of the value to do the comparison in a case insensitive manner if necessary. This means we can retain that original author intent as much as possible.

clairernovotny marked this conversation as resolved.
Show resolved Hide resolved

return _fileStorageService.CreateDownloadFileActionResultAsync(requestUrl, CoreConstants.Folders.PackagesFolderName, fileName, packageVersion);
}

public Task<ActionResult> CreateDownloadPackageActionResultAsync(Uri requestUrl, string id, string version)
{
var fileName = FileNameHelper.BuildFileName(id, version, CoreConstants.PackageFileSavePathTemplate, CoreConstants.NuGetPackageFileExtension);
return _fileStorageService.CreateDownloadFileActionResultAsync(requestUrl, CoreConstants.Folders.PackagesFolderName, fileName);

// version cannot be null here as BuildFileName will throw if it is
return _fileStorageService.CreateDownloadFileActionResultAsync(requestUrl, CoreConstants.Folders.PackagesFolderName, fileName, NuGetVersionFormatter.Normalize(version).ToLowerInvariant());
}

/// <summary>
Expand All @@ -47,7 +53,7 @@ public Task DeleteReadMeMdFileAsync(Package package)
{
throw new ArgumentNullException(nameof(package));
}

var fileName = FileNameHelper.BuildFileName(package, ReadMeFilePathTemplateActive, ServicesConstants.MarkdownFileExtension);

return _fileStorageService.DeleteFileAsync(CoreConstants.Folders.PackageReadMesFolderName, fileName);
Expand Down Expand Up @@ -83,7 +89,7 @@ public async Task<string> DownloadReadMeMdFileAsync(Package package)
{
throw new ArgumentNullException(nameof(package));
}

var fileName = FileNameHelper.BuildFileName(package, ReadMeFilePathTemplateActive, ServicesConstants.MarkdownFileExtension);

using (var readMeMdStream = await _fileStorageService.GetFileAsync(CoreConstants.Folders.PackageReadMesFolderName, fileName))
Expand Down
12 changes: 9 additions & 3 deletions src/NuGetGallery/Services/SymbolPackageFileService.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using NuGet.Services.Entities;

using System;
using System.Threading.Tasks;
using System.Web.Mvc;
using NuGet.Services.Entities;

namespace NuGetGallery
{
Expand All @@ -21,13 +22,18 @@ public SymbolPackageFileService(IFileStorageService fileStorageService)
public Task<ActionResult> CreateDownloadSymbolPackageActionResultAsync(Uri requestUrl, SymbolPackage symbolPackage)
{
var fileName = FileNameHelper.BuildFileName(symbolPackage.Package, CoreConstants.PackageFileSavePathTemplate, CoreConstants.NuGetSymbolPackageFileExtension);
return _fileStorageService.CreateDownloadFileActionResultAsync(requestUrl, CoreConstants.Folders.SymbolPackagesFolderName, fileName);

var packageVersion = NuGetVersionFormatter.GetNormalizedPackageVersion(symbolPackage.Package).ToLowerInvariant(); // will not return null bc BuildFileName will throw if values are null
clairernovotny marked this conversation as resolved.
Show resolved Hide resolved

return _fileStorageService.CreateDownloadFileActionResultAsync(requestUrl, CoreConstants.Folders.SymbolPackagesFolderName, fileName, packageVersion);
}

public Task<ActionResult> CreateDownloadSymbolPackageActionResultAsync(Uri requestUrl, string id, string version)
{
var fileName = FileNameHelper.BuildFileName(id, version, CoreConstants.PackageFileSavePathTemplate, CoreConstants.NuGetSymbolPackageFileExtension);
return _fileStorageService.CreateDownloadFileActionResultAsync(requestUrl, CoreConstants.Folders.SymbolPackagesFolderName, fileName);

// version cannot be null here as BuildFileName will throw if it is
return _fileStorageService.CreateDownloadFileActionResultAsync(requestUrl, CoreConstants.Folders.SymbolPackagesFolderName, fileName, NuGetVersionFormatter.Normalize(version).ToLowerInvariant());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Moq;

using NuGetGallery.Configuration;
clairernovotny marked this conversation as resolved.
Show resolved Hide resolved
using NuGetGallery.Diagnostics;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Moq;
using NuGetGallery.Configuration;
using NuGetGallery.Diagnostics;

using Xunit;
using Xunit.Sdk;

Expand Down Expand Up @@ -111,7 +114,7 @@ public async Task WillGetTheBlobFromTheCorrectFolderContainer(string folderName)
var httpContext = GetContext();
var service = CreateService(fakeBlobClient: fakeBlobClient);

await service.CreateDownloadFileActionResultAsync(HttpRequestUrl, folderName, "theFileName");
await service.CreateDownloadFileActionResultAsync(HttpRequestUrl, folderName, "theFileName", "theVersion");

fakeBlobContainer.Verify(x => x.GetBlobReference("theFileName"));
}
Expand All @@ -131,7 +134,7 @@ public async Task WillReturnARedirectResultToTheBlobUri(string requestUrl, strin
fakeBlob.Setup(x => x.Uri).Returns(new Uri(requestUri.Scheme + "://theUri"));
var service = CreateService(fakeBlobClient: fakeBlobClient);

var result = await service.CreateDownloadFileActionResultAsync(requestUri, CoreConstants.Folders.PackagesFolderName, "theFileName") as RedirectResult;
var result = await service.CreateDownloadFileActionResultAsync(requestUri, CoreConstants.Folders.PackagesFolderName, "theFileName", "theVersion") as RedirectResult;

Assert.NotNull(result);
Assert.Equal(scheme + "theuri/", result.Url);
Expand All @@ -152,11 +155,57 @@ public async Task WillUseBlobUriPort(string requestUrl, string blobUrl, int expe
fakeBlob.Setup(x => x.Uri).Returns(new Uri(blobUrl));
var service = CreateService(fakeBlobClient: fakeBlobClient);

var result = await service.CreateDownloadFileActionResultAsync(new Uri(requestUrl), CoreConstants.Folders.PackagesFolderName, "theFileName") as RedirectResult;
var result = await service.CreateDownloadFileActionResultAsync(new Uri(requestUrl), CoreConstants.Folders.PackagesFolderName, "theFileName", "theVersion") as RedirectResult;
var redirectUrl = new Uri(result.Url);
Assert.Equal(expectedPort, redirectUrl.Port);
}

[Theory]
[InlineData(HttpRequestUrlString, "https://theUri", "1.1.1", "1.1.1")]
[InlineData(HttpsRequestUrlString, "https://theUri", "01.01.01", "1.1.1")]
public async Task WillReturnARedirectResultWithTheNormalizedPackageVersion(string requestUrl, string blobUrl, string version, string expectedVersion)
{
var fakeBlobClient = new Mock<ICloudBlobClient>();
var fakeBlobContainer = new Mock<ICloudBlobContainer>();
var fakeBlob = new Mock<ISimpleCloudBlob>();
fakeBlobClient.Setup(x => x.GetContainerReference(It.IsAny<string>())).Returns(fakeBlobContainer.Object);
fakeBlobContainer.Setup(x => x.GetBlobReference(It.IsAny<string>())).Returns(fakeBlob.Object);
fakeBlobContainer.Setup(x => x.CreateIfNotExistAsync(It.IsAny<bool>())).Returns(Task.FromResult(0));
var requestUri = new Uri(requestUrl);
fakeBlob.Setup(x => x.Uri).Returns(new Uri(blobUrl));
var service = CreateService(fakeBlobClient: fakeBlobClient);

var result = await service.CreateDownloadFileActionResultAsync(requestUri, CoreConstants.Folders.PackagesFolderName, "theFileName", version) as RedirectResult;

Assert.NotNull(result);

var uri = new Uri(result.Url);
var qs = HttpUtility.ParseQueryString(uri.Query);
Assert.Equal(expectedVersion, qs["version"]);
}

[Theory]
[InlineData(HttpRequestUrlString, "https://theUri", null)]
[InlineData(HttpsRequestUrlString, "https://theUri", "")]
public async Task WillThrowIfVersionIsNullOrEmpty(string requestUrl, string blobUrl, string version)
{
var fakeBlobClient = new Mock<ICloudBlobClient>();
var fakeBlobContainer = new Mock<ICloudBlobContainer>();
var fakeBlob = new Mock<ISimpleCloudBlob>();
fakeBlobClient.Setup(x => x.GetContainerReference(It.IsAny<string>())).Returns(fakeBlobContainer.Object);
fakeBlobContainer.Setup(x => x.GetBlobReference(It.IsAny<string>())).Returns(fakeBlob.Object);
fakeBlobContainer.Setup(x => x.CreateIfNotExistAsync(It.IsAny<bool>())).Returns(Task.FromResult(0));
var requestUri = new Uri(requestUrl);
fakeBlob.Setup(x => x.Uri).Returns(new Uri(blobUrl));
var service = CreateService(fakeBlobClient: fakeBlobClient);

await Assert.ThrowsAsync<ArgumentException>(
() => service.CreateDownloadFileActionResultAsync(
new Uri(HttpsRequestUrlString),
CoreConstants.Folders.PackagesFolderName, "theFileName", version)
);
}

[Fact]
public async Task WillUseISourceDestinationRedirectPolicy()
{
Expand All @@ -172,9 +221,9 @@ public async Task WillUseISourceDestinationRedirectPolicy()
var service = CreateService(fakeBlobClient: fakeBlobClient, redirectPolicy: fakePolicy);

var result = await service.CreateDownloadFileActionResultAsync(
new Uri(HttpsRequestUrlString),
CoreConstants.Folders.PackagesFolderName,
"theFileName") as RedirectResult;
new Uri(HttpsRequestUrlString),
CoreConstants.Folders.PackagesFolderName,
"theFileName", "theVersion") as RedirectResult;
fakePolicy.Verify();
}

Expand All @@ -194,12 +243,12 @@ public async Task WillThrowIfRedirectIsNotAllowed()

await Assert.ThrowsAsync<InvalidOperationException>(
() => service.CreateDownloadFileActionResultAsync(
new Uri(HttpsRequestUrlString),
CoreConstants.Folders.PackagesFolderName, "theFileName")
new Uri(HttpsRequestUrlString),
CoreConstants.Folders.PackagesFolderName, "theFileName", "theVersion")
);
}
}

private static HttpContextBase GetContext(string protocol = "http://")
{
var httpRequest = new Mock<HttpRequestBase>();
Expand Down
Loading