GitLabgitlab-org

cli

main
ply8 git calls221 ms99 KiB6 cachedprotocol v2
138 lines4.2 KiBRaw
1// Package status implements `glab artifact-registry status`, which reports the caller's
2// access to the GitLab Artifact Registry by exchanging their GitLab session
3// for a short-lived access token and inspecting its claims.
4package status
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/tableprinter"
21 "gitlab.com/gitlab-org/cli/internal/text"
24type options struct {
25 io *iostreams.IOStreams
26 apiClient func(repoHost string) (*api.Client, error)
28 hostname string
29 outputFormat string
32// NewCmd returns the `glab artifact-registry status` 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: "status",
41 Short: "Check your access to the GitLab Artifact Registry. (EXPERIMENTAL)",
42 Long: heredoc.Doc(`
43 Exchange a GitLab credential for a short-lived Artifact Registry access
44 token, then print the token's issuer, subject, audience, and expiry so
45 you can confirm which identity and instance you are authenticated as. No
46 credentials are written to disk.
48 Prerequisites:
50 - A GitLab Enterprise Edition (EE) instance on GitLab 19.4 or later.
51 `) + text.ExperimentalString,
52 Example: heredoc.Doc(`
53 # Show Artifact Registry access status
54 glab artifact-registry status
56 # Show Artifact Registry access status as JSON
57 glab artifact-registry status --output json
58 `),
59 Args: cobra.NoArgs,
60 ValidArgsFunction: cobra.NoFileCompletions,
61 // Not mcpannotations.Safe: reading the claims requires minting a token,
62 // so each run creates server-side state rather than only reading it.
63 Annotations: map[string]string{
64 mcpannotations.Destructive: "true",
65 },
66 RunE: func(cmd *cobra.Command, _ []string) error {
67 if err := opts.validate(); err != nil {
68 return err
69 }
70 return opts.run(cmd.Context())
71 },
72 }
74 cmd.Flags().StringVar(&opts.hostname, "hostname", "", "GitLab hostname to check. Defaults to the configured GitLab instance.")
75 cmdutils.EnableJSONOutput(cmd, opts.io, &opts.outputFormat)
77 return cmd
80// validate rejects a --hostname that is not a bare hostname, because the value
81// is otherwise forwarded to f.ApiClient untouched and decides which host
82// receives the caller's credential. Empty means "use the configured instance",
83// so it is left alone. Same check as `glab api --hostname`.
84func (o *options) validate() error {
85 if o.hostname == "" {
86 return nil
87 }
88 if err := glinstance.HostnameValidator(o.hostname); err != nil {
89 return &cmdutils.FlagError{Err: fmt.Errorf("error parsing --hostname: %w", err)}
90 }
91 return nil
94func (o *options) run(ctx context.Context) error {
95 apiClient, err := o.apiClient(o.hostname)
96 if err != nil {
97 return err
98 }
100 // The token is read for its claims and thrown away, so let the server pick
101 // the lifetime instead of asking for a CLI-chosen one.
102 result, err := artifactregistry.NewClient(apiClient.Lab()).ExchangeDefaultToken(ctx)
103 if err != nil {
104 return fmt.Errorf("failed to check artifact registry status: %w", err)
105 }
107 switch o.outputFormat {
108 case "json":
109 return o.printJSON(result)
110 default:
111 return o.printTable(result)
112 }
115func (o *options) printJSON(result *artifactregistry.ExchangeResult) error {
116 return o.io.PrintJSON(struct {
117 Issuer string `json:"issuer"`
118 Subject string `json:"subject"`
119 Audience string `json:"audience"`
120 ExpiresAt time.Time `json:"expires_at"`
121 }{
122 Issuer: result.Issuer,
123 Subject: result.Subject,
124 Audience: result.Audience,
125 ExpiresAt: result.ExpiresAt,
126 })
129func (o *options) printTable(result *artifactregistry.ExchangeResult) error {
130 table := tableprinter.NewTablePrinter()
131 table.AddRow("Issuer", result.Issuer)
132 table.AddRow("Subject", result.Subject)
133 table.AddRow("Audience", result.Audience)
134 table.AddRow("Expires At", result.ExpiresAt.Format(time.RFC3339))
135 o.io.LogInfo(table.String())
137 return nil
Diagnostics8 git calls · 221 ms
CallStatusSizeTime0221 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 · hit3 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=3
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
fetchtree:0 · deepen 1020032 KiB · hit2 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=2
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
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · 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=4273
git.response.cache=HIT
git.response.duration_ms=4
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:02001.8 KiB · hit2 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=1857
git.response.cache=HIT
git.response.duration_ms=2
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 · 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=242
git.response.cache=HIT
git.response.duration_ms=3
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:0200138 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=138
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=b0826c05487ddcb0a998bd4b65170b76913da918
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=138
git.pkt.payload.max=111
git.sideband.band1.bytes=111
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=111
git.pack.trailer=a9c5cadae9ab881146a570ac590a9ff8a03cfd03
Open pack map
fetch2001.8 KiB206 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=206
git.response.bytes=1792
git.response.cache=MISS
git.response.duration_ms=206
git.fetch.wants=4607157e58c1eee912565db72071aaabedaa0d04
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=1792
git.pkt.payload.max=1765
git.sideband.band1.bytes=1765
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
a5e2e5cffb442947
Request
a684fac0…
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