-
Notifications
You must be signed in to change notification settings - Fork 42
feat(ENG-13677): Terraform credential helper #400
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
Open
tigh-latte
wants to merge
11
commits into
master
Choose a base branch
from
tigh/terraform-credential-provider
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
823199b
feat: terraform credential provider first pass
tigh-latte fc92b5b
feat: support the args directive to set --org and -P flags
tigh-latte b8da77c
feat: add installer
tigh-latte 7281b4d
feat: Correctly formatted tokens and install QOL changes
tigh-latte 492e5ab
don't print org in token when using custom domain
tigh-latte 76ea356
fix: remove terraform-credential-cloudsmith entrypoint
tigh-latte e0d3ec0
fix: accurate changelog
tigh-latte f58fb92
fix: remove wrapper.py entrypoint as it is not used
tigh-latte d184c63
fix(ENG-13677): remove unneeded docs
tigh-latte edaf205
fix(ENG-13677): remove unused vars and imports
tigh-latte 368bc9a
fix: remove code referencing wrapper that doesn't exist
tigh-latte 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
137 changes: 137 additions & 0 deletions
137
cloudsmith_cli/cli/commands/credential_helper/terraform.py
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,137 @@ | ||
| # Copyright 2026 Cloudsmith Ltd | ||
| """ | ||
| Terraform credentials helper command. | ||
|
|
||
| Implements the ``get`` verb of Terraform's credentials-helper protocol for | ||
| Cloudsmith registries. The installed ``terraform-credentials-cloudsmith`` | ||
| launcher forwards Terraform's invocation to this command. | ||
|
|
||
| See: https://developer.hashicorp.com/terraform/internals/credentials-helpers | ||
| """ | ||
|
|
||
| import sys | ||
|
|
||
| import click | ||
|
|
||
| from ....credential_helpers.terraform import execute | ||
| from ...decorators import ( | ||
| common_api_auth_options, | ||
| common_cli_config_options, | ||
| resolve_credentials, | ||
| ) | ||
|
|
||
|
|
||
| @click.command(context_settings={"ignore_unknown_options": True}) | ||
| @click.option( | ||
| "-r", | ||
| "--repo", | ||
| "--repository", | ||
| "repo", | ||
| required=True, | ||
| envvar="CLOUDSMITH_REPO", | ||
| help="The Cloudsmith repository the registry serves. Terraform does not " | ||
| "pass the repository to a credentials helper, so it must be configured " | ||
| "here (e.g. in the terraformrc args) to build a repository-scoped token.", | ||
| ) | ||
| @click.argument("params", nargs=-1, type=click.UNPROCESSED) | ||
| @common_cli_config_options | ||
| @common_api_auth_options | ||
| @resolve_credentials | ||
| def terraform(opts, repo, params): | ||
| """ | ||
| Terraform credentials helper for Cloudsmith registries. | ||
|
|
||
| Resolves the token for a Cloudsmith Terraform registry and prints it in | ||
| Terraform's expected JSON credentials format: ``{"token": "..."}``. | ||
|
|
||
| Provides credentials for all Cloudsmith Terraform registries: | ||
| ``*.cloudsmith.io``, ``*.cloudsmith.com``, and any custom domains | ||
| configured for the organisation (requires an organisation - ``--org``, | ||
| CLOUDSMITH_ORG or ``org`` in ``config.ini`` - and a valid API key/token). | ||
|
|
||
| Accepts Terraform's calling convention — an optional verb (``get``, | ||
| ``store``, ``forget``) followed by the hostname — so the on-PATH launcher | ||
| can forward Terraform's arguments verbatim. When no verb is given the | ||
| action is ``get``. When no hostname is given it is read from stdin. Only | ||
| ``get`` is served; ``store``/``forget`` return an error and a non-zero exit. | ||
|
|
||
| A hostname that is not a Cloudsmith registry yields an empty object | ||
| (``{}``) and exit 0 so Terraform falls back to its own credential sources. | ||
|
|
||
| \b | ||
| Input (arguments or stdin): | ||
| [VERB] HOSTNAME — e.g. "get terraform.cloudsmith.io" or just | ||
| "terraform.cloudsmith.io"; HOSTNAME alone may also come from stdin. | ||
|
|
||
| \b | ||
| Output (stdout): | ||
| JSON: {"token": "<cloudsmith-token>"} (Cloudsmith host, token found) | ||
| JSON: {} (not a Cloudsmith host) | ||
|
|
||
| \b | ||
| Exit codes: | ||
| 0: Token returned, or the host is not a Cloudsmith registry | ||
| 1: Cloudsmith host with no credentials available, or an error occurred | ||
|
|
||
| The token is scoped to the repository. On a standard | ||
| ``*.cloudsmith.io``/``*.cloudsmith.com`` host it is ``{org}/{repo}/{token}`` | ||
| and the organisation is required (``--org``, ``CLOUDSMITH_ORG`` or ``org`` | ||
| in ``config.ini``); a standard host requested without an organisation is a | ||
| non-zero exit. On a custom domain — which is already bound to a single | ||
| organisation — the org is omitted and the token is ``{repo}/{token}``. The | ||
| repository is always required (``-r/--repo/--repository`` or | ||
| ``CLOUDSMITH_REPO``): Terraform does not tell a credentials helper which | ||
| repository is being requested. A non-Cloudsmith host still returns ``{}`` | ||
| and exit 0. The profile can also be supplied with ``-P/--profile``. The | ||
| launcher forwards Terraform's ``args`` verbatim, so a terraformrc block such | ||
| as ``credentials_helper "cloudsmith" { args = ["--org=acme", | ||
| "--repo=my-repo", "-P", "ci"] }`` reaches this command as those options. | ||
|
|
||
| \b | ||
| Examples: | ||
| # Direct usage | ||
| $ cloudsmith credential-helper terraform --repo my-repo terraform.cloudsmith.io | ||
| {"token": "..."} | ||
|
|
||
| # Terraform's calling convention (verb + hostname) | ||
| $ cloudsmith credential-helper terraform --repo my-repo get terraform.cloudsmith.io | ||
|
|
||
| # Select an org and profile explicitly (no env vars needed) | ||
| $ cloudsmith credential-helper terraform --org=acme -P ci get terraform.cloudsmith.io | ||
|
|
||
| \b | ||
| Environment variables: | ||
| CLOUDSMITH_API_KEY: API key for authentication (optional) | ||
| CLOUDSMITH_ORG: Organisation slug (required to scope the token) | ||
| CLOUDSMITH_REPO: Repository slug the registry serves (required) | ||
| CLOUDSMITH_PROFILE: Configuration profile to load (optional) | ||
| """ | ||
| # Terraform passes "<verb> <hostname>"; direct/manual use may pass just the | ||
| # hostname (verb defaults to "get") or nothing (hostname read from stdin). | ||
| verb = "get" | ||
| hostname: str | None = None | ||
| if len(params) >= 2: | ||
| verb, hostname = params[-2], params[-1] | ||
| elif len(params) == 1: | ||
| hostname = params[0] | ||
|
|
||
| if not hostname: | ||
| try: | ||
| hostname = sys.stdin.read().strip() | ||
| except (OSError, ValueError): | ||
| hostname = "" | ||
|
|
||
| exit_code, stdout, stderr = execute( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The helper reaches |
||
| verb, | ||
| hostname, | ||
| credential=opts.credential, | ||
| api_host=opts.api_host, | ||
| org=opts.org, | ||
| repo=repo, | ||
| ) | ||
|
|
||
| if stdout is not None: | ||
| click.echo(stdout) | ||
| 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
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.
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.
How does this work with
--config-file/--credentials-fileflags? If an installation uses either option,terraform initmay not resolve the same credentials (?)