GitLabgitlab-org
cli
main
1// Package artifactregistry provides a client for exchanging a GitLab session
2// token for a short-lived GitLab Artifact Registry access token via the
3// POST /api/v4/token_exchange endpoint.
4//
5// It lives under internal/api rather than beside the command that uses it so
6// that the glab auth docker credential helper can consume it too once it
7// learns about artifact registries: command packages must not import each
8// other.
9package artifactregistry
11import (
12 "context"
13 "fmt"
14 "time"
16 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
18 "gitlab.com/gitlab-org/cli/internal/api/tokenexchange"
19)
21// tokenExchangeAudience identifies the Artifact Registry as the intended
22// recipient of the exchanged token.
23const tokenExchangeAudience = "gitlab-artifact-registry"
25const (
26 // MinDuration is the shortest lifetime a caller may request through
27 // ExchangeToken: the server's actual floor. These tokens cannot be
28 // revoked once minted, and AppSec is pushing for shorter-lived tokens
29 // (gitlab#601725, artifact-registry#229), so the CLI does not add a
30 // floor of its own above the server's: a caller asking for less
31 // exposure should never be blocked from getting it.
32 MinDuration = 1 * time.Second
33 // MaxDuration is the longest lifetime a caller may request through
34 // ExchangeToken. A CLI-side choice, not a server contract; see
35 // MinDuration.
36 MaxDuration = 12 * time.Hour
37 // DefaultDuration is what callers get when they do not choose a
38 // duration explicitly, e.g. `glab artifact-registry get-token` with no
39 // --duration. A CLI-side choice, independent of MinDuration and of the
40 // server's own 5-minute default, which applies only when expires_in is
41 // omitted entirely (see ExchangeDefaultToken).
42 DefaultDuration = 15 * time.Minute
43 // DockerHelperDuration is the token lifetime the Docker credential helper
44 // requests for every artifact-registry exchange it makes. Docker invokes
45 // the helper with no way to pass a duration, so this is the only lever;
46 // it is entirely independent of `get-token`, which always validates and
47 // uses its own --duration flag (defaulting to DefaultDuration, not
48 // MinDuration). It is deliberately longer than MinDuration: a docker
49 // pull/push holds the credential for the whole operation, not just the
50 // initial handshake, and a token expiring mid-pull hard-fails it.
51 // Changing it changes how long every docker pull/push's token stays valid.
52 DockerHelperDuration = time.Hour
53)
55// Client exchanges a GitLab session token for a short-lived Artifact
56// Registry access token.
57type Client struct {
58 gl *gitlab.Client
59}
61// NewClient returns a Client that issues token-exchange requests through gl.
62func NewClient(gl *gitlab.Client) *Client {
63 return &Client{gl: gl}
64}
66// ExchangeResult is the decoded result of a successful token exchange.
67type ExchangeResult = tokenexchange.Result
69// ExchangeToken exchanges the caller's GitLab credential for a short-lived
70// Artifact Registry access token valid for duration. duration must be within
71// [MinDuration, MaxDuration].
72func (c *Client) ExchangeToken(ctx context.Context, duration time.Duration) (*ExchangeResult, error) {
73 if err := ValidateDuration(duration); err != nil {
74 return nil, err
75 }
76 return tokenexchange.Exchange(ctx, c.gl, tokenExchangeAudience, duration)
77}
79// ExchangeDefaultToken exchanges the caller's GitLab credential for a
80// short-lived Artifact Registry access token, omitting expires_in so the
81// server applies its own default lifetime. Callers that only read the
82// token's claims and then discard it, like `status`, want the server default
83// rather than a CLI-chosen duration.
84func (c *Client) ExchangeDefaultToken(ctx context.Context) (*ExchangeResult, error) {
85 return tokenexchange.Exchange(ctx, c.gl, tokenExchangeAudience, 0)
86}
88// ValidateDuration reports whether d is within [MinDuration, MaxDuration].
89// This is an advisory, CLI-side guard so flag parsing fails fast with a clear
90// message; it is not a mirror of server policy, which can change
91// independently (artifact-registry#229) and whose 400 response is always the
92// authoritative answer.
93func ValidateDuration(d time.Duration) error {
94 if d < MinDuration || d > MaxDuration {
95 return fmt.Errorf("duration must be between %s and %s", MinDuration, MaxDuration)
96 }
97 return nil
98}
Diagnostics7 git calls · 407 ms
CallStatusSizeTime0407 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 KiB183 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=179 git.response.bytes=59849 git.response.cache=EXPIRED git.response.duration_ms=183 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 · hit8 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=8 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=8 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 · hit8 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=8 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=8 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=44Open pack map
fetchtree:02001.1 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=1165 git.response.cache=HIT git.response.duration_ms=1 git.fetch.filter=tree:0 git.fetch.wants=71c8407e0ff0e0af10f6a041ae330f754b68e7eb 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=1165 git.pkt.payload.max=1138 git.sideband.band1.bytes=1138 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1138 git.pack.trailer=61886367c69619d8db2b2cf2d7d24dca2016141cOpen pack map
fetchtree:0200148 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=148 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=f193dbf7c81afce45df7f6e0e0227e5abc78bf3a 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=148 git.pkt.payload.max=121 git.sideband.band1.bytes=121 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=121 git.pack.trailer=2125fef90ae9a520d4b5d8ceee3c23653a798ba8Open pack map
fetch2001.7 KiB212 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=212 git.response.bytes=1760 git.response.cache=MISS git.response.duration_ms=212 git.fetch.wants=e152a30ced2a436ff44023062731dd307f19b906 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=1760 git.pkt.payload.max=1733 git.sideband.band1.bytes=1733 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
- a5e14bcd1f18a140
- Request
- 1a4ca15a…
- 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