Skip to content

Add dotnet nuget apikey commands#7322

Open
Saibamen wants to merge 2 commits into
NuGet:devfrom
Saibamen:feature/6437-dotnet-nuget-apikey
Open

Add dotnet nuget apikey commands#7322
Saibamen wants to merge 2 commits into
NuGet:devfrom
Saibamen:feature/6437-dotnet-nuget-apikey

Conversation

@Saibamen
Copy link
Copy Markdown

Summary

  • Add dotnet nuget apikey set and dotnet nuget apikey unset to persist and remove API keys for package sources.
  • Reuse existing NuGet config source resolution and encrypted API key storage, with --configfile support.
  • Add XPlat resources/localization placeholders and focused tests for set/unset behavior.

Fixes NuGet/Home#6437
Spec: NuGet/Home#12321

Test plan

  • dotnet test test\NuGet.Core.Tests\NuGet.CommandLine.Xplat.Tests\NuGet.CommandLine.Xplat.Tests.csproj --filter ApiKey
  • dotnet format whitespace --verify-no-changes NuGet.sln --include "src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/ApiKeyCommand.cs" "src/NuGet.Core/NuGet.CommandLine.XPlat/Program.cs" "test/NuGet.Core.Tests/NuGet.CommandLine.Xplat.Tests/ApiKeyCommandTests.cs"
  • dotnet test test\NuGet.Core.Tests\NuGet.Commands.Test\NuGet.Commands.Test.csproj --filter Push partially passed: net10.0 push tests passed; net472 host failed before matching tests due to strong-name validation of Microsoft.Internal.NuGet.Testing.SignedPackages.

Notes

  • Full solution whitespace verification is currently blocked by pre-existing formatting findings outside this change, including source-package files under the local NuGet package cache and DependencyGraphResolver.cs.

@Saibamen Saibamen requested a review from a team as a code owner April 26, 2026 14:56
@Saibamen Saibamen requested review from donnie-msft and zivkan April 26, 2026 14:56
@dotnet-policy-service dotnet-policy-service Bot added the Community PRs created by someone not in the NuGet team label Apr 26, 2026
@Saibamen Saibamen changed the title Add dotnet nuget apikey commands Add dotnet nuget apikey commands Apr 26, 2026
Strings.Option_ConfigFile,
CommandOptionType.SingleValue);

CommandArgument apiKeyArgument = set.Argument(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's add a verbosity option here too.

{
public static void Register(CommandLineApplication app, Func<ILogger> getLogger)
{
app.Command("apikey", apiKey =>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this is great work, please create the spec where you enumerate all the options and follow the template.

We should be able to use that design for documentation generation and historical reasons.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The PR description links to NuGet/Home#12321, which is a spec I forgot that I wrote years ago. I stopped tracking this dotnet nuget apikey command because I think #7087 is a better solution and expect that people want dotnet nuget apikey because they're not aware of the NUGET_API_KEY environment variable, rather than this new command enabling some scenario that is too difficult given NUGET_API_KEY is available. But the point is that there is a spec for this command.

Since the NUGET_API_KEY environment variable exists, I think the spec's -e and -f options are not needed. However, I don't see any justification in this PR why the spec was not followed.

But since the spec was merged in 2023, almost exactly 3 years ago, if we go ahead with this command, we should at least run it by the dotnet CLI PMs to see if they have feedback on the command names and structure. Make sure that the best practices and recommendations for command design haven't changed since the spec was written.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Actually, the NUGET_API_KEY environment variable looks to be new to the .NET 10.0.300 SDK, so not even out yet. Once it ships I wonder how much demand there will be for a dotnet nuget apikey command.

Copy link
Copy Markdown
Member

@nkolev92 nkolev92 Apr 28, 2026

Choose a reason for hiding this comment

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

I missed the linked spec to be honest. There were a bunch of PRs with a missing spec, so I actually missed the one that did have it :D

I agree with your general sentiment.
Let's look at the spec again and drive to a final solution.

Sounds like we have 2 (?) options?

  • Spec is re-reviewed and updated and is completely followed in this implementation.
  • Spec/PR and issue are all updated saying NUGET_API_KEY should be used.

Comment thread src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/ApiKeyCommand.cs
@@ -0,0 +1,177 @@
// 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

All these tests run the runner, so integration and that's great, but I'd like to have at least 1 test that actually runs dotnet nuget apikey set or whatever the order actually is :D

You'd follow https://github.com/NuGet/NuGet.Client/blob/dev/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/ListPackageTests.cs.

We're in the process of changing up the NuGet/SDK CLI integration in how these things are set-up, so you'd need to make sure this integration is being added the same way the dotnet nuget why integration was added.

Comment thread src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/ApiKeyCommand.cs
{
public static void Register(CommandLineApplication app, Func<ILogger> getLogger)
{
app.Command("apikey", apiKey =>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The PR description links to NuGet/Home#12321, which is a spec I forgot that I wrote years ago. I stopped tracking this dotnet nuget apikey command because I think #7087 is a better solution and expect that people want dotnet nuget apikey because they're not aware of the NUGET_API_KEY environment variable, rather than this new command enabling some scenario that is too difficult given NUGET_API_KEY is available. But the point is that there is a spec for this command.

Since the NUGET_API_KEY environment variable exists, I think the spec's -e and -f options are not needed. However, I don't see any justification in this PR why the spec was not followed.

But since the spec was merged in 2023, almost exactly 3 years ago, if we go ahead with this command, we should at least run it by the dotnet CLI PMs to see if they have feedback on the command names and structure. Make sure that the best practices and recommendations for command design haven't changed since the spec was written.

public class ApiKeyCommandTests
{
private const string SourceName = "contoso";
private const string SourceUrl = "https://nuget.contoso.org/v3/index.json";
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Tests should use domains ending in .test, since that's a reserved TLD and basically eliminates a test from accidentally opening HTTP or TCP connections to remote hosts. We have many old tests that don't follow this best practise, but for new tests I'd like to see it done.

@dotnet-policy-service dotnet-policy-service Bot added the Status:No recent activity PRs that have not had any recent activity and will be closed if the label is not removed label May 5, 2026
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

This PR has been automatically marked as stale because it has no activity for 7 days. It will be closed if no further activity occurs within another 30 days of this comment. If it is closed, you may reopen it anytime when you're ready again, as long as you don't delete the branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Community PRs created by someone not in the NuGet team Status:No recent activity PRs that have not had any recent activity and will be closed if the label is not removed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Why is there no 'dotnet nuget setApiKey' command?

3 participants