GitLabgitlab-org

cli

main
ply8 git calls249 ms99 KiB6 cachedprotocol v2
148 lines5.1 KiBRaw
1// Package gettoken implements `glab artifact-registry get-token`, which exchanges the
2// caller's GitLab credential for a short-lived GitLab Artifact Registry
3// access token.
4package gettoken
6import (
7 "context"
8 "fmt"
9 "time"
11 "github.com/MakeNowJust/heredoc/v2"
12 "github.com/spf13/cobra"
14 "gitlab.com/gitlab-org/cli/internal/api"
15 "gitlab.com/gitlab-org/cli/internal/api/artifactregistry"
16 "gitlab.com/gitlab-org/cli/internal/cmdutils"
17 "gitlab.com/gitlab-org/cli/internal/glinstance"
18 "gitlab.com/gitlab-org/cli/internal/iostreams"
19 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
20 "gitlab.com/gitlab-org/cli/internal/text"
23type options struct {
24 io *iostreams.IOStreams
25 apiClient func(repoHost string) (*api.Client, error)
27 hostname string
28 duration time.Duration
29 outputFormat string
32// NewCmd returns the `glab artifact-registry get-token` command.
33func NewCmd(f cmdutils.Factory) *cobra.Command {
34 opts := &options{
35 io: f.IO(),
36 apiClient: f.ApiClient,
37 }
39 cmd := &cobra.Command{
40 Use: "get-token",
41 Short: "Get a short-lived access token for the GitLab Artifact Registry. (EXPERIMENTAL)",
42 Long: heredoc.Docf(`
43 Exchange a GitLab credential for a short-lived access token scoped to the
44 GitLab Artifact Registry. The command prints the bare token to stdout,
45 so a shell can capture it directly, for example to feed %[1]sdocker login%[1]s.
47 Prerequisites:
49 - A GitLab Enterprise Edition (EE) instance on GitLab 19.4 or later.
50 `, "`") + text.ExperimentalString,
51 Example: heredoc.Doc(`
52 # Get a token using the default duration
53 glab artifact-registry get-token
55 # Get a token valid for one hour
56 glab artifact-registry get-token --duration 1h
58 # Get a token as JSON, including its expiry
59 glab artifact-registry get-token --output json
60 `),
61 Args: cobra.NoArgs,
62 ValidArgsFunction: cobra.NoFileCompletions,
63 // mcpannotations.Exclude, not Safe or Destructive: every run mints a
64 // live bearer credential and prints it, so an MCP agent invoking this
65 // would receive a usable secret. token/create and
66 // cluster/agent/get-token, which also print a fresh secret, are
67 // excluded for the same reason.
68 Annotations: map[string]string{
69 mcpannotations.Exclude: "true",
70 },
71 RunE: func(cmd *cobra.Command, _ []string) error {
72 if err := opts.validate(); err != nil {
73 return err
74 }
75 return opts.run(cmd.Context())
76 },
77 }
79 fl := cmd.Flags()
80 fl.StringVar(&opts.hostname, "hostname", "", "GitLab hostname to request the token from. Defaults to the configured GitLab instance.")
81 fl.DurationVar(&opts.duration, "duration", artifactregistry.DefaultDuration, fmt.Sprintf("How long the token should remain valid. Must be between %s and %s.", artifactregistry.MinDuration, artifactregistry.MaxDuration))
82 cmdutils.EnableJSONOutput(cmd, opts.io, &opts.outputFormat)
84 return cmd
87// validate rejects a --hostname that is not a bare hostname, because the value
88// is otherwise forwarded to f.ApiClient untouched and decides which host
89// receives the caller's credential. Empty means "use the configured instance",
90// so it is left alone. Same check as `glab api --hostname`.
91//
92// It also rejects a --duration outside artifactregistry's accepted bounds
93// here, at the command's own validate step, rather than leaving that guarantee
94// to fall out of ExchangeToken's own internal check: run calls ExchangeToken
95// only after validate succeeds, so an out-of-range duration must never reach
96// it, and this is the layer responsible for enforcing that regardless of what
97// ExchangeToken does internally.
98func (o *options) validate() error {
99 if o.hostname != "" {
100 if err := glinstance.HostnameValidator(o.hostname); err != nil {
101 return &cmdutils.FlagError{Err: fmt.Errorf("error parsing --hostname: %w", err)}
102 }
103 }
104 if err := artifactregistry.ValidateDuration(o.duration); err != nil {
105 return &cmdutils.FlagError{Err: fmt.Errorf("error parsing --duration: %w", err)}
106 }
107 return nil
110func (o *options) run(ctx context.Context) error {
111 apiClient, err := o.apiClient(o.hostname)
112 if err != nil {
113 return err
114 }
116 result, err := artifactregistry.NewClient(apiClient.Lab()).ExchangeToken(ctx, o.duration)
117 if err != nil {
118 return fmt.Errorf("failed to get artifact registry token: %w", err)
119 }
121 switch o.outputFormat {
122 case "json":
123 return o.printJSON(result)
124 default:
125 return o.printToken(result)
126 }
129func (o *options) printJSON(result *artifactregistry.ExchangeResult) error {
130 return o.io.PrintJSON(struct {
131 Token string `json:"token"`
132 ExpiresAt time.Time `json:"expires_at"`
133 }{
134 Token: result.Token,
135 ExpiresAt: result.ExpiresAt,
136 })
139// printToken writes the bare token so a shell can capture it, e.g.
140// TOKEN=$(glab artifact-registry get-token). It uses fmt.Fprintln directly
141// rather than o.io.LogInfo, which discards its write error: a dropped error
142// here would leave the caller with an empty $TOKEN and exit code 0.
143func (o *options) printToken(result *artifactregistry.ExchangeResult) error {
144 if _, err := fmt.Fprintln(o.io.StdOut, result.Token); err != nil { //nolint:forbidigo // write error must propagate to the caller
145 return err
146 }
147 return nil
Diagnostics8 git calls · 249 ms
CallStatusSizeTime0249 ms
discover200185 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=185
git.server.agent=git/2.56.g1891de4-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=185
git.pkt.payload.max=35
ls-refs20058 KiB · hit10 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=59849
git.response.cache=HIT
git.response.duration_ms=10
git.protocol.version=2
git.pkt.frames=731
git.pkt.data=730
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=59849
git.pkt.payload.max=143
git.ls_refs.count=730
git.ls_refs.head=refs/heads/main
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=combine:blob:none+tree:2
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=1
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=4273
git.pkt.payload.max=4173
git.sideband.band1.bytes=4173
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=11
git.pack.bytes=4173
git.fetch.wide.objects=11
git.fetch.wide.extra_bytes=3930
git.fetch.wide.blobs=0
fetchtree:0 · deepen 1020032 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=32926
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=22
git.pkt.data=20
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=32926
git.pkt.payload.max=32046
git.sideband.band1.bytes=32046
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=80
git.pack.bytes=32046
git.pack.ofs_delta=36
git.pack.ref_delta=0
git.pack.chain.max=2
git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91
git.pack.full=44
Open pack map
fetchtree:02001.8 KiB · hit1 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=1
git.response.bytes=1857
git.response.cache=HIT
git.response.duration_ms=1
git.fetch.filter=tree:0
git.fetch.wants=dce174ff8c632a19d17eea99c8a9b7c37fb2b592
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=1857
git.pkt.payload.max=1830
git.sideband.band1.bytes=1830
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1830
git.pack.trailer=fb03183b7909876778f740d196d7516811003517
Open pack map
fetchtree:0200242 B · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.bytes=242
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=5da85e254ab77f5f6e7e1419be57d524a9f8208e
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=242
git.pkt.payload.max=215
git.sideband.band1.bytes=215
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=215
git.pack.trailer=bd426fb1ae09d9fdcf755902c6903cd2eea819b2
Open pack map
fetchtree:0200140 B · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=140
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=eb7fc31ecec841eb8546c913f46e8a8530fe755b
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=140
git.pkt.payload.max=113
git.sideband.band1.bytes=113
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=113
git.pack.trailer=12eb4bffd5426cf02b3756055be2d3b0d6b78a1a
Open pack map
fetch2002.1 KiB229 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=229
git.response.bytes=2150
git.response.cache=MISS
git.response.duration_ms=229
git.fetch.wants=d37f50c3216c26228be982be58d21f62bef09281
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=2150
git.pkt.payload.max=2123
git.sideband.band1.bytes=2123
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://gitlab.com/gitlab-org/cli
Ref
refs/heads/main
Commit
0dd7574dce9e385c5c30540e6c15657adc6bdce9
cf-ray
a5e2d0ec4a5aefad
Request
e3827212…
Protocol
v2 · sha1
Agent
git/2.56.g1891de4-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format