Skip to content

Commit

Permalink
(#3613) Add source uri override for download uris
Browse files Browse the repository at this point in the history
This adds a new Package Downloader class and have it call a resolve
redirection for download URIs to the original source URI.

This allows the same credentials to be used for the download URI as with
the source URI when the server is configured to store binary files in
a different location.
  • Loading branch information
AdmiringWorm committed Jan 31, 2025
1 parent ccdfbee commit 6e03fae
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/chocolatey/chocolatey.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -135,6 +135,7 @@
<Compile Include="infrastructure.app\commands\ChocolateyExportCommand.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyInfoCommand.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyHelpCommand.cs" />
<Compile Include="infrastructure.app\nuget\ChocolateyPackageDownloader.cs" />
<Compile Include="infrastructure.app\registration\ChocolateyRegistrationModule.cs" />
<Compile Include="infrastructure.app\registration\IContainerRegistrator.cs" />
<Compile Include="infrastructure.app\registration\IContainerResolver.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2017 - 2021 Chocolatey Software, Inc
// Copyright © 2017 - 2025 Chocolatey Software, Inc
// Copyright © 2011 - 2017 RealDimensions Software, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -146,7 +146,7 @@ public virtual void noop(ChocolateyConfiguration configuration)
public virtual void run(ChocolateyConfiguration configuration)
{
var packageManager = NugetCommon.GetPackageManager(configuration, _nugetLogger,
new PackageDownloader(),
new ChocolateyPackageDownloader(),
installSuccessAction: null,
uninstallSuccessAction: null,
addUninstallHandler: false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2017 - 2021 Chocolatey Software, Inc
// Copyright © 2017 - 2025 Chocolatey Software, Inc
// Copyright © 2011 - 2017 RealDimensions Software, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -17,9 +17,9 @@
namespace chocolatey.infrastructure.app.nuget
{
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using commandline;
using NuGet;
using configuration;
Expand All @@ -29,6 +29,7 @@ namespace chocolatey.infrastructure.app.nuget

public sealed class ChocolateyNugetCredentialProvider : ICredentialProvider
{
private static readonly ConcurrentDictionary<Uri, Uri> _resolveDownloadUrls = new ConcurrentDictionary<Uri, Uri>();
private readonly ChocolateyConfiguration _config;

private const string INVALID_URL = "http://somewhere123zzaafasd.invalid";
Expand All @@ -38,6 +39,28 @@ public ChocolateyNugetCredentialProvider(ChocolateyConfiguration config)
_config = config;
}

public static void add_download_url_from_package(IPackageName package)
{
if (package == null)
{
return;
}

var servicePackage = package as DataServicePackage;

if (servicePackage == null)
{
return;
}

var sourceUri = get_source_url(servicePackage);

if (servicePackage != null && sourceUri != null && servicePackage.DownloadUrl != null && !servicePackage.DownloadUrl.ToString().StartsWith(sourceUri.ToString(), StringComparison.InvariantCultureIgnoreCase))
{
_resolveDownloadUrls.TryAdd(servicePackage.DownloadUrl, sourceUri);
}
}

public ICredentials GetCredentials(Uri uri, IWebProxy proxy, CredentialType credentialType, bool retrying)
{
if (uri == null)
Expand All @@ -50,6 +73,12 @@ public ICredentials GetCredentials(Uri uri, IWebProxy proxy, CredentialType cred
this.Log().Warn("Invalid credentials specified.");
}

Uri resolveUri;
if (_resolveDownloadUrls.TryGetValue(uri, out resolveUri))
{
return GetCredentials(resolveUri, proxy, credentialType, retrying);
}

var configSourceUri = new Uri(INVALID_URL);

this.Log().Debug(ChocolateyLoggers.Verbose, "Attempting to gather credentials for '{0}'".format_with(uri.OriginalString));
Expand Down Expand Up @@ -179,6 +208,26 @@ public ICredentials get_credentials_from_user(Uri uri, IWebProxy proxy, Credenti

return credentials;
}

private static Uri get_source_url(IPackage package)
{
var type = package.GetType();
var property = type.GetProperty("Context", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

if (property == null)
{
return null;
}

var context = property.GetValue(package, new object[0]) as IDataServiceContext;

if (context == null)
{
return null;
}

return context.BaseUri;
}
}

// ReSharper restore InconsistentNaming
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright © 2017 - 2025 Chocolatey Software, Inc
// Copyright © 2011 - 2017 RealDimensions Software, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace chocolatey.infrastructure.app.nuget
{
using System;
using System.IO;
using NuGet;

internal class ChocolateyPackageDownloader : PackageDownloader
{
public override void DownloadPackage(Uri uri, IPackageMetadata package, Stream targetStream)
{
ChocolateyNugetCredentialProvider.add_download_url_from_package(package);

base.DownloadPackage(uri, package, targetStream);
}

public override void DownloadPackage(IHttpClient downloadClient, IPackageName package, Stream targetStream)
{
ChocolateyNugetCredentialProvider.add_download_url_from_package(package);

base.DownloadPackage(downloadClient, package, targetStream);
}
}
}
4 changes: 2 additions & 2 deletions src/chocolatey/infrastructure.app/nuget/NugetCommon.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2017 - 2021 Chocolatey Software, Inc
// Copyright © 2017 - 2025 Chocolatey Software, Inc
// Copyright © 2011 - 2017 RealDimensions Software, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -173,7 +173,7 @@ public static IPackageRepository GetRemoteRepository(ChocolateyConfiguration con
// keep this here for the licensed edition for now
public static IPackageManager GetPackageManager(ChocolateyConfiguration configuration, ILogger nugetLogger, Action<PackageOperationEventArgs> installSuccessAction, Action<PackageOperationEventArgs> uninstallSuccessAction, bool addUninstallHandler)
{
return GetPackageManager(configuration, nugetLogger, new PackageDownloader(), installSuccessAction, uninstallSuccessAction, addUninstallHandler);
return GetPackageManager(configuration, nugetLogger, new ChocolateyPackageDownloader(), installSuccessAction, uninstallSuccessAction, addUninstallHandler);
}

public static IPackageManager GetPackageManager(ChocolateyConfiguration configuration, ILogger nugetLogger, IPackageDownloader packageDownloader, Action<PackageOperationEventArgs> installSuccessAction, Action<PackageOperationEventArgs> uninstallSuccessAction, bool addUninstallHandler)
Expand Down
4 changes: 2 additions & 2 deletions src/chocolatey/infrastructure.app/nuget/NugetList.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2017 - 2021 Chocolatey Software, Inc
// Copyright © 2017 - 2025 Chocolatey Software, Inc
// Copyright © 2011 - 2017 RealDimensions Software, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -40,7 +40,7 @@ public static int GetCount(ChocolateyConfiguration configuration, ILogger nugetL

private static IQueryable<IPackage> execute_package_search(ChocolateyConfiguration configuration, ILogger nugetLogger)
{
var packageRepository = NugetCommon.GetRemoteRepository(configuration, nugetLogger, new PackageDownloader());
var packageRepository = NugetCommon.GetRemoteRepository(configuration, nugetLogger, new ChocolateyPackageDownloader());
var searchTermLower = configuration.Input.to_lower();

// Whether or not the package is remote determines two things:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2017 - 2022 Chocolatey Software, Inc
// Copyright © 2017 - 2025 Chocolatey Software, Inc
// Copyright © 2011 - 2017 RealDimensions Software, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -44,7 +44,7 @@ public void register_dependencies(IContainerRegistrator registrator, ChocolateyC
//nuget
registrator.register_service<ILogger, ChocolateyNugetLogger>();
registrator.register_service<INugetService, NugetService>();
registrator.register_service<IPackageDownloader, PackageDownloader>();
registrator.register_service<IPackageDownloader, ChocolateyPackageDownloader>();
registrator.register_service<IPowershellService, PowershellService>();
registrator.register_service<IChocolateyPackageInformationService, ChocolateyPackageInformationService>();
registrator.register_service<IShimGenerationService, ShimGenerationService>();
Expand Down
4 changes: 2 additions & 2 deletions src/chocolatey/infrastructure.app/services/TemplateService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2017 - 2021 Chocolatey Software, Inc
// Copyright © 2017 - 2025 Chocolatey Software, Inc
// Copyright © 2011 - 2017 RealDimensions Software, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -221,7 +221,7 @@ public void list_noop(ChocolateyConfiguration configuration)
public void list(ChocolateyConfiguration configuration)
{
var packageManager = NugetCommon.GetPackageManager(configuration, _nugetLogger,
new PackageDownloader(),
new ChocolateyPackageDownloader(),
installSuccessAction: null,
uninstallSuccessAction: null,
addUninstallHandler: false);
Expand Down

0 comments on commit 6e03fae

Please sign in to comment.