-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathdocker.py
More file actions
80 lines (60 loc) · 2.36 KB
/
docker.py
File metadata and controls
80 lines (60 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
Docker credential helper command.
Implements the Docker credential helper protocol for Cloudsmith registries.
See: https://github.com/docker/docker-credential-helpers
"""
import json
import sys
import click
from ....credential_helpers.docker import get_credentials
from ...decorators import common_api_auth_options, resolve_credentials
@click.command()
@common_api_auth_options
@resolve_credentials
def docker(opts):
"""
Docker credential helper for Cloudsmith registries.
Reads a Docker registry server URL from stdin and returns credentials in JSON format.
This command implements the 'get' operation of the Docker credential helper protocol.
Only provides credentials for Cloudsmith Docker registries (docker.cloudsmith.io).
Input (stdin):
Server URL as plain text (e.g., "docker.cloudsmith.io")
Output (stdout):
JSON: {"Username": "token", "Secret": "<cloudsmith-token>"}
Exit codes:
0: Success
1: Error (no credentials available, not a Cloudsmith registry, etc.)
Examples:
# Manual testing
$ echo "docker.cloudsmith.io" | cloudsmith credential-helper docker
{"Username":"token","Secret":"eyJ0eXAiOiJKV1Qi..."}
# Called by Docker via wrapper
$ echo "docker.cloudsmith.io" | docker-credential-cloudsmith get
{"Username":"token","Secret":"eyJ0eXAiOiJKV1Qi..."}
Environment variables:
CLOUDSMITH_API_KEY: API key for authentication (optional)
CLOUDSMITH_ORG: Organization slug (required for custom domain support)
"""
try:
server_url = sys.stdin.read().strip()
if not server_url:
click.echo("Error: No server URL provided on stdin", err=True)
sys.exit(1)
credentials = get_credentials(
server_url,
credential=opts.credential,
session=opts.session,
api_host=opts.api_host or "https://api.cloudsmith.io",
)
if not credentials:
click.echo(
"Error: Unable to retrieve credentials. "
"Make sure you have a valid cloudsmith-cli session, "
"this can be checked with `cloudsmith whoami`.",
err=True,
)
sys.exit(1)
click.echo(json.dumps(credentials))
except OSError as e:
click.echo(f"Error: {e}", err=True)
sys.exit(1)