Repository navigation
feat(ENG-13680): add a NuGet credential provider #458
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
Closed
Copilot
wants to merge
1
commit into
iduffy/shared-credential-helper-code
from
copilot/create-nuget-credential-provider
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # Copyright 2026 Cloudsmith Ltd | ||
| """ | ||
| NuGet credential provider command. | ||
|
|
||
| Implements the NuGet cross-platform authentication plugin protocol for | ||
| Cloudsmith feeds. | ||
|
|
||
| See: | ||
| https://learn.microsoft.com/en-us/nuget/reference/extensibility/nuget-cross-platform-plugins | ||
| """ | ||
|
|
||
| import sys | ||
|
|
||
| import click | ||
|
|
||
| from ....core.credentials.chain import CredentialProviderChain | ||
| from ....credential_helpers.nuget import execute | ||
| from ...decorators import common_api_auth_options, initialise_api | ||
|
|
||
|
|
||
| def _utf8(stream, **kwargs): | ||
| """Switch a text stream to UTF-8, which the NuGet plugin protocol requires.""" | ||
| try: | ||
| stream.reconfigure(encoding="utf-8", **kwargs) | ||
| except (AttributeError, ValueError, OSError): | ||
| pass | ||
| return stream | ||
|
|
||
|
|
||
| @click.command( | ||
| context_settings={"ignore_unknown_options": True, "allow_interspersed_args": False} | ||
| ) | ||
| @click.option( | ||
| "--domain", | ||
| "domains", | ||
| multiple=True, | ||
| help="A feed host to authenticate, as well as the Workspace's NuGet domains.", | ||
| ) | ||
| @click.option( | ||
| "--no-discover", | ||
| is_flag=True, | ||
| help="Do not look up the Workspace's NuGet domains.", | ||
| ) | ||
| @click.argument("nuget_args", nargs=-1, type=click.UNPROCESSED) | ||
| @common_api_auth_options | ||
| @initialise_api | ||
| def nuget(opts, domains, no_discover, nuget_args): | ||
| """ | ||
| NuGet credential provider for Cloudsmith feeds. | ||
|
|
||
| NuGet (dotnet, MSBuild, Visual Studio) runs this command through the | ||
| ``nuget-plugin-cloudsmith`` launcher that ``cloudsmith credential-helper | ||
| install nuget`` puts on PATH, passing ``-Plugin`` after ``--``. It then | ||
| speaks the NuGet cross-platform plugin protocol on stdin/stdout. | ||
|
|
||
| Provides credentials for all Cloudsmith NuGet feeds: ``*.cloudsmith.io``, | ||
| ``*.cloudsmith.com``, and any NuGet custom domains configured for the | ||
| Workspace (requires a Workspace - ``--workspace``, CLOUDSMITH_WORKSPACE, | ||
| or ``workspace`` in ``config.ini``; legacy aliases are also accepted - and | ||
| a valid API key/token). Any other feed is declined so NuGet falls back to | ||
| its other credential sources. | ||
|
|
||
| \b | ||
| Examples: | ||
|
|
||
| \b | ||
| # Called by NuGet via the launcher | ||
| $ nuget-plugin-cloudsmith -Plugin | ||
|
|
||
| \b | ||
| Environment variables: | ||
| CLOUDSMITH_API_KEY: API key for authentication (optional) | ||
| CLOUDSMITH_WORKSPACE: Workspace slug (CLOUDSMITH_ORG is also accepted) | ||
| """ | ||
| protocol_stdout = _utf8(sys.stdout, newline="\n") | ||
| # Other output, such as the HTTP trace of --debug, must not reach NuGet. | ||
| sys.stdout = sys.stderr | ||
| exit_code, stderr = execute( | ||
| nuget_args, | ||
| _utf8(sys.stdin, errors="replace"), | ||
| protocol_stdout, | ||
| credential=opts.credential, | ||
| api_host=opts.api_host, | ||
| workspace=opts.org, | ||
|
cloudsmith-iduffy marked this conversation as resolved.
Resolved
|
||
| refresh_credential=lambda: CredentialProviderChain().resolve( | ||
| opts.credential_context | ||
| ), | ||
| domains=domains, | ||
| discover=not no_discover, | ||
| ) | ||
|
|
||
| if stderr is not None: | ||
| click.echo(stderr, err=True) | ||
| sys.exit(exit_code) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.