|
| 1 | +# Copyright 2026 Cloudsmith Ltd |
| 2 | +""" |
| 3 | +Docker credential helper command. |
| 4 | +
|
| 5 | +Implements the Docker credential helper protocol for Cloudsmith registries. |
| 6 | +
|
| 7 | +See: https://github.com/docker/docker-credential-helpers |
| 8 | +""" |
| 9 | + |
| 10 | +import sys |
| 11 | + |
| 12 | +import click |
| 13 | + |
| 14 | +from ....credential_helpers.docker import execute |
| 15 | +from ...decorators import common_api_auth_options, resolve_credentials |
| 16 | + |
| 17 | + |
| 18 | +@click.command() |
| 19 | +@click.argument("operation", required=False, default="get") |
| 20 | +@common_api_auth_options |
| 21 | +@resolve_credentials |
| 22 | +def docker(opts, operation): |
| 23 | + """ |
| 24 | + Docker credential helper for Cloudsmith registries. |
| 25 | +
|
| 26 | + Reads a Docker registry server URL from stdin and returns credentials in |
| 27 | + JSON format. Implements the full Docker credential helper protocol |
| 28 | + (get/store/erase/list). |
| 29 | +
|
| 30 | + Provides credentials for all Cloudsmith Docker registries: ``*.cloudsmith.io``, |
| 31 | + ``*.cloudsmith.com``, and any custom domains configured for the organisation |
| 32 | + (requires CLOUDSMITH_ORG and a valid API key/token). |
| 33 | +
|
| 34 | + Input (stdin): |
| 35 | + Server URL as plain text (e.g. "docker.cloudsmith.io") |
| 36 | +
|
| 37 | + Output (stdout): |
| 38 | + JSON: {"Username": "token", "Secret": "<cloudsmith-token>"} |
| 39 | +
|
| 40 | + Exit codes: |
| 41 | + 0: Success |
| 42 | + 1: Error (no credentials available, not a Cloudsmith registry, etc.) |
| 43 | +
|
| 44 | + Examples: |
| 45 | + # Manual testing |
| 46 | + $ echo "docker.cloudsmith.io" | cloudsmith credential-helper docker |
| 47 | +
|
| 48 | + # Called by Docker via launcher |
| 49 | + $ echo "docker.cloudsmith.io" | docker-credential-cloudsmith get |
| 50 | +
|
| 51 | + Environment variables: |
| 52 | + CLOUDSMITH_API_KEY: API key for authentication (optional) |
| 53 | + CLOUDSMITH_ORG: Organisation slug (required for custom domain support) |
| 54 | + """ |
| 55 | + exit_code, stdout, stderr = execute( |
| 56 | + operation, |
| 57 | + sys.stdin, |
| 58 | + credential=opts.credential, |
| 59 | + api_host=opts.api_host, |
| 60 | + ) |
| 61 | + |
| 62 | + if stdout is not None: |
| 63 | + click.echo(stdout) |
| 64 | + if stderr is not None: |
| 65 | + click.echo(stderr, err=True) |
| 66 | + sys.exit(exit_code) |
0 commit comments