-
Notifications
You must be signed in to change notification settings - Fork 699
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
Do not warn http sources in package reference restore when allowInsecureConnections is set to true #5390
Do not warn http sources in package reference restore when allowInsecureConnections is set to true #5390
Conversation
...uGet.Core/NuGet.Commands/RestoreCommand/RequestFactory/DependencyGraphSpecRequestProvider.cs
Outdated
Show resolved
Hide resolved
IAssetsLogMessage logMessage = result.LockFile.LogMessages[0]; | ||
logMessage.Code.Should().Be(NuGetLogCode.NU1803); | ||
Assert.Contains(expectedWarning, logMessage.Message); | ||
Assert.DoesNotContain(unExpectedWarning, logMessage.Message); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like that this test is thorough, but is it clearer to just assert Equals
on the expected warning, then drop the unexpected warning completely?
Assert.DoesNotContain(unExpectedWarning, logMessage.Message); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you mean why not do Assert.Equals("NU1234: This is the expected message")
, then that's high risk of causing problems for people who wish to contribute to NuGet but don't use English as their locale: NuGet/Home#12820. The risk will significantly increase if we eventually do localization more like the rest of github.com/dotnet repos, since local builds can have the satellite assemblies.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you mean why not do Assert.Equals("NU1234: This is the expected message")
No, I do not mean that. I expect we would continue using the localized variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the latest commits and this thread, this hasn't been addressed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for reviewing! Sorry for taking long to address this. This is fixed.
logMessage.Code.Should().Be(NuGetLogCode.NU1803); | ||
logMessage.Message.Should().Be("You are running the 'restore' operation with an 'HTTP' source, 'http://api.source/index.json'. Non-HTTPS access will be removed in a future version. Consider migrating to an 'HTTPS' source."); | ||
|
||
string expectedWarning = "You are running the 'restore' operation with an 'HTTP' source, 'http://api.source/index.json'. Non-HTTPS access will be removed in a future version. Consider migrating to an 'HTTPS' source."; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To reduce copying the HTTP source as well as emphasize that your test is expecting the HTTP source URL, I think a variable here would be clearer:
string expectedWarning = "You are running the 'restore' operation with an 'HTTP' source, 'http://api.source/index.json'. Non-HTTPS access will be removed in a future version. Consider migrating to an 'HTTPS' source."; | |
string expectedWarning = $"You are running the 'restore' operation with an 'HTTP' source, '{testHttpSourceUrl}'. Non-HTTPS access will be removed in a future version. Consider migrating to an 'HTTPS' source."; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the latest commits and this thread, this hasn't been addressed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! This is fixed.
...uGet.Core/NuGet.Commands/RestoreCommand/RequestFactory/DependencyGraphSpecRequestProvider.cs
Outdated
Show resolved
Hide resolved
...uGet.Core/NuGet.Commands/RestoreCommand/RequestFactory/DependencyGraphSpecRequestProvider.cs
Outdated
Show resolved
Hide resolved
@@ -214,6 +214,9 @@ private static ExternalProjectReference GetExternalProject(PackageSpec rootProje | |||
// Add project references | |||
request.ExternalProjects = projectReferenceClosure.ToList(); | |||
|
|||
//Update the RestoreRequest.Project.RestoreMetadata.Sources to get the attributes from settings(e.g. AllowInsecureConnections). | |||
request.Project.RestoreMetadata.Sources = sources.Select(s => s.PackageSource).ToList(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was my bad @heng-liu, I should've done this investigation the first time I provided feedback.
I think we need a slight change here.
I think my original implementation that I added here: 4d2c5a4 is not ideal.
Some background:
- PackageSpec, or rather
Project
is considered to be themodel
for restore, the input. This will be created differently on the commandline (static graph vs standard) vs Visual Studio. It has a sources property, which is the processed sources based on the project path,RestoreSources
msbuild property etc. It's the actual end sources. The general expectation is that the PackageSpec does not get modified. It is basically what is used for no-op. - RestoreRequest has a list of
DependencyProviders
.
These dependency providers are "shared" across projects, created within this classl,Lines 175 to 182 in 41e98d3
var sharedCache = _providerCache.GetOrCreate( globalPath, fallbackPaths.AsList(), sources, restoreArgs.CacheContext, restoreArgs.Log, updateLastAccess);
When I added the original implementation, I used _request.Project.RestoreMetadata.Sources
. I think that's incorrect. Those are not how the sources are used, they are used through request.DependencyProviders
which is effectively based on the PackageSource list from the package spec.
I should've used that and it's already used in a bunch of places, like for example, detecting http sources for telemetry,
int httpSourcesCount = _request.DependencyProviders.RemoteProviders.Where(e => e.IsHttp).Count(); |
Basically, PackageSpec on the request is expected to be immutable, see: NuGet/Home#9041 (comment). We basically clone it a bunch of time because of fear of these types of changes.
Now this is a thing you've never caught yourself, and it's not incorrect to do this, but I think it'll be a smaller change if you just change it to use the dependencyproviders.
You can try that change and see how it goes, tests might break. Like, not sure if everything is mocked 100%. If that becomes the case, please lmk and I can help or just do the change from using the package spec metadata to the dependencyprovider once, since I caused the pain anyways :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the review has been re-requested mistakenly,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @nkolev92 for the detailed contexts! (I was wondering why the tests for no-op failed previously.)
I changed to use RemoteProviders in a testing branch and the build shows the newly changed test failed. There is no warning in result.LockFile.LogMessages so it failed at this line
However, I can see the warning from the console when running the commandline.
Do you have any suggestions about fixing this test? Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var sources = new List<PackageSource> { new PackageSource(pathContext.PackageSource) }; |
PathContext
source instead of the ones from the package spec. It should be using the PackageSpec ones. I'll do some quick testing to see if I can have a fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd add a new CreateRestoreRequestMethod, where the source is spec.RestoreMetadata.Sources
.
That makes the test pass. Other tests would fail if you make the change in project test helpers directly.
I know how to fix the ones I wrote, but there's many other tests that have package specs that are incomplete.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually if you replace that line with:
var sources = spec.RestoreMetadata.Sources.Any() ?
spec.RestoreMetadata.Sources :
new List<PackageSource> { new PackageSource(pathContext.PackageSource) };
All tests seem to pass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you end up choosing that, please create an issue to fix the tests and assign to me. I think our test helpers code should use as much of the production code as possible. I'm hoping to do some changes in the ProjectTestHelpers that'll help us avoid these problems.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I spent about an hour doing some refactoring: https://github.com/NuGet/NuGet.Client/tree/dev-nkolev92-cleanupTestHelpers. Feel free to make the change to fix your tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have #5407 out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @nkolev92 for your help! Please take another look.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I re-reviewed as requested by @jeffkl, but after looking at the latest commits and my unresolved threads, there are unaddressed comments by me.
b1734b8
to
7c6c3e3
Compare
7a9ce43
to
6f8a363
Compare
This reverts commit b763608.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚢
@@ -239,11 +239,12 @@ public async Task<RestoreResult> ExecuteAsync(CancellationToken token) | |||
_success = false; | |||
} | |||
|
|||
if (_request.Project?.RestoreMetadata != null) | |||
if (_request.DependencyProviders.RemoteProviders != null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent!
Thanks for fixing my mistake :)
Bug
Fixes: NuGet/Home#12787
Regression? Last working version:
Description
When
allowInsecureConnections
property inpackageSources
section is set to true in NuGet.Config files, suppress warnings for HTTP sources in PackageReference restore scenarios in all NuGet tools.The PR enable the HTTP warnings in this scenario is: Warn when http sources are used in PackageReference restore operations #4556
Updated to use
_request.DependencyProviders.RemoteProviders
, instead of_request.Project.RestoreMetadata
to check , as the first one respect the settings.Context: The original value of
allowInsecureConnections
forRestoreRequest.Project.RestoreMetadata.Sources
is always false, as the value is from: 1.https://github.com/NuGet/NuGet.Client/blob/dev/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs#L200 which doesn't contain any additional attributes likeallowInsecureConnections
. (dotnet restore)2.https://github.com/NuGet/NuGet.Client/blob/dev/src/NuGet.Clients/NuGet.CommandLine/MsBuildUtility.cs#L210 which is loading an output.dg file(generated by GenerateRestoreGraphFileTask) which also doesn't contain any additional attributes like
allowInsecureConnections
. (NuGet.exe restore)PR Checklist
PR has a meaningful title
PR has a linked issue.
Described changes
Tests
Documentation