GitLabgitlab-org
cli
main
1package serviceaccountutils
3import (
4 "context"
5 "fmt"
6 "strings"
7 "time"
9 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
11 "gitlab.com/gitlab-org/cli/internal/iostreams"
12 "gitlab.com/gitlab-org/cli/internal/tableprinter"
13)
15// ListTokens returns every personal access token of a service account, or only
16// the active ones.
17func ListTokens(ctx context.Context, client *gitlab.Client, scope Scope, accountID int64, active bool) ([]*gitlab.PersonalAccessToken, error) {
18 var state *string
19 if active {
20 state = new("active")
21 }
22 return listTokens(ctx, client, scope, accountID, state, nil)
23}
25func listTokens(ctx context.Context, client *gitlab.Client, scope Scope, accountID int64, state, search *string) ([]*gitlab.PersonalAccessToken, error) {
26 if scope.Group != "" {
27 opts := &gitlab.ListServiceAccountPersonalAccessTokensOptions{
28 ListOptions: gitlab.ListOptions{PerPage: perPage},
29 State: state,
30 Search: search,
31 }
32 return gitlab.ScanAndCollect(func(p gitlab.PaginationOptionFunc) ([]*gitlab.PersonalAccessToken, *gitlab.Response, error) {
33 return client.Groups.ListServiceAccountPersonalAccessTokens(scope.Group, accountID, opts, p, gitlab.WithContext(ctx))
34 })
35 }
37 opts := &gitlab.ListProjectServiceAccountPersonalAccessTokensOptions{
38 ListOptions: gitlab.ListOptions{PerPage: perPage},
39 State: state,
40 Search: search,
41 }
42 return gitlab.ScanAndCollect(func(p gitlab.PaginationOptionFunc) ([]*gitlab.PersonalAccessToken, *gitlab.Response, error) {
43 return client.Projects.ListProjectServiceAccountPersonalAccessTokens(scope.Project, accountID, opts, p, gitlab.WithContext(ctx))
44 })
45}
47// ResolveTokenID returns the ID of the service account token a <token-id|name>
48// argument names. Only a name costs a request, and it matches active tokens
49// only, so a rotated or revoked token of the same name is not a candidate.
50func ResolveTokenID(ctx context.Context, client *gitlab.Client, scope Scope, accountID int64, ref string) (int64, error) {
51 id, isID, err := parseID(ref, "token")
52 if err != nil || isID {
53 return id, err
54 }
56 // search is a partial, case-insensitive match, so the exact name is checked here.
57 tokens, err := listTokens(ctx, client, scope, accountID, new("active"), &ref)
58 if err != nil {
59 return 0, err
60 }
62 var matches []int64
63 for _, t := range tokens {
64 if t.Name == ref {
65 matches = append(matches, t.ID)
66 }
67 }
69 switch len(matches) {
70 case 0:
71 return 0, fmt.Errorf("no active token named %q for service account %d in %s", ref, accountID, scope)
72 case 1:
73 return matches[0], nil
74 default:
75 return 0, fmt.Errorf("multiple active tokens named %q for service account %d; use the token ID instead", ref, accountID)
76 }
77}
79// PrintToken prints the details of a newly created or rotated token to stderr
80// and only its value to stdout, so a script can capture the value alone. The
81// stdout write error is returned, not dropped as io.LogInfo would, so a failed
82// write cannot leave a script with an empty value and exit code 0.
83func PrintToken(io *iostreams.IOStreams, t *gitlab.PersonalAccessToken) error {
84 c := io.Color()
85 table := tableprinter.NewTablePrinter()
86 table.AddRow(c.Bold("ID"), t.ID)
87 table.AddRow(c.Bold("Name"), t.Name)
88 table.AddRow(c.Bold("Description"), OrDash(t.Description))
89 table.AddRow(c.Bold("Scopes"), FormatScopes(t.Scopes))
90 table.AddRow(c.Bold("Expires At"), FormatDate(t.ExpiresAt))
91 io.LogErrorf("%s", table.Render())
92 _, err := fmt.Fprintln(io.StdOut, t.Token) //nolint:forbidigo // write error must propagate to the caller
93 return err
94}
96// FormatScopes renders token scopes as a comma-separated list, or "-".
97func FormatScopes(scopes []string) string {
98 if len(scopes) == 0 {
99 return "-"
100 }
101 return strings.Join(scopes, ", ")
102}
104// FormatDate renders a token's expiry date, or "-" for a token that does not
105// expire.
106func FormatDate(d *gitlab.ISOTime) string {
107 if d == nil {
108 return "-"
109 }
110 return d.String()
111}
113// FormatTime renders a timestamp such as a token's last use, or "-".
114func FormatTime(t *time.Time) string {
115 if t == nil {
116 return "-"
117 }
118 return t.UTC().Format(time.RFC3339)
119}
Diagnostics8 git calls · 238 ms
CallStatusSizeTime0238 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch 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 KiB3 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=UPDATING 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 · hit3 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=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=44Open 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=3 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 · 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=1857 git.response.cache=HIT git.response.duration_ms=3 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=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200322 B · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=6 git.response.bytes=322 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=ba2caccf6c55211c6b29b98f40c2358479d91b1f 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=322 git.pkt.payload.max=295 git.sideband.band1.bytes=295 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=295 git.pack.trailer=3488e558d4708096e879feba968e084c249dbe22Open pack map
fetchtree:0200207 B · hit6 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=220 git.response.bytes=207 git.response.cache=HIT git.response.duration_ms=6 git.fetch.filter=tree:0 git.fetch.wants=46257722a5d9837ad7305a4a6942623f79e26348 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=207 git.pkt.payload.max=180 git.sideband.band1.bytes=180 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=180 git.pack.trailer=49c3336eecdc11d5831924e911d2e17735206333Open pack map
fetch2001.5 KiB220 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1580 git.response.cache=MISS git.response.duration_ms=220 git.fetch.wants=ca9e94caae0d67dba6482a4601dfd07e18d8e5ce 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=1580 git.pkt.payload.max=1553 git.sideband.band1.bytes=1553 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
- a5e373521a6f22ce
- Request
- aad2f672…
- 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