GitLabgitlab-org
cli
main
1package revoke
3import (
4 "context"
6 "github.com/MakeNowJust/heredoc/v2"
7 "github.com/spf13/cobra"
9 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
11 "gitlab.com/gitlab-org/cli/internal/api"
12 "gitlab.com/gitlab-org/cli/internal/cmdutils"
13 "gitlab.com/gitlab-org/cli/internal/commands/service-account/serviceaccountutils"
14 "gitlab.com/gitlab-org/cli/internal/glrepo"
15 "gitlab.com/gitlab-org/cli/internal/iostreams"
16 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
17)
19type options struct {
20 io *iostreams.IOStreams
21 apiClient func(repoHost string) (*api.Client, error)
22 baseRepo func() (glrepo.Interface, error)
24 account string
25 token string
26 group string
27}
29func NewCmd(f cmdutils.Factory) *cobra.Command {
30 opts := &options{
31 io: f.IO(),
32 apiClient: f.ApiClient,
33 baseRepo: f.BaseRepo,
34 }
36 cmd := &cobra.Command{
37 Use: "revoke <account-id | username> <token-id | name> [flags]",
38 Short: "Revoke a token of a service account.",
39 Args: cobra.ExactArgs(2),
40 ValidArgsFunction: cobra.NoFileCompletions,
41 Long: heredoc.Docf(`
42 Revoke a personal access token of a service account of a project or a group.
44 - Anything authenticating with the token stops working immediately.
45 - A token name matches active tokens only. If several active tokens share the name, use the token ID instead.
46 - Numeric arguments are always treated as IDs. Token IDs and names come from %[1]sglab service-account token list%[1]s.
48 Revoking a token of a group service account requires GitLab 17.11 or later.
49 `, "`"),
50 Example: heredoc.Doc(`
51 # Revoke token 7 of service account 42 of the current project
52 glab service-account token revoke 42 7
54 # Revoke a token by its name, finding the account by its username
55 glab service-account token revoke release-bot ci
57 # Revoke a token of a group's service account
58 glab service-account token revoke 42 7 --group my-group`),
59 Annotations: map[string]string{
60 mcpannotations.Destructive: "true",
61 },
62 RunE: func(cmd *cobra.Command, args []string) error {
63 if err := opts.complete(cmd, args); err != nil {
64 return err
65 }
67 return opts.run(cmd.Context())
68 },
69 }
71 cmdutils.EnableRepoOverride(cmd, f)
73 fl := cmd.Flags()
74 fl.StringVarP(&opts.group, "group", "g", "", "Revoke a token of a service account of a `group` instead of a project. Accepts a group ID or full path.")
76 cmd.MarkFlagsMutuallyExclusive("repo", "group")
78 return cmd
79}
81func (o *options) complete(cmd *cobra.Command, args []string) error {
82 o.account = args[0]
83 o.token = args[1]
85 group, err := cmdutils.GroupOverride(cmd)
86 if err != nil {
87 return err
88 }
89 o.group = group
91 return nil
92}
94func (o *options) run(ctx context.Context) error {
95 client, scope, err := serviceaccountutils.NewClient(o.baseRepo, o.apiClient, o.group)
96 if err != nil {
97 return err
98 }
100 accountID, err := serviceaccountutils.ResolveID(ctx, client, scope, o.account)
101 if err != nil {
102 return cmdutils.WrapError(err, "failed to revoke service account token")
103 }
105 tokenID, err := serviceaccountutils.ResolveTokenID(ctx, client, scope, accountID, o.token)
106 if err != nil {
107 return cmdutils.WrapError(err, "failed to revoke service account token")
108 }
110 if scope.Group != "" {
111 _, err = client.Groups.RevokeServiceAccountPersonalAccessToken(scope.Group, accountID, tokenID, gitlab.WithContext(ctx))
112 } else {
113 _, err = client.Projects.RevokeProjectServiceAccountPersonalAccessToken(scope.Project, accountID, tokenID, gitlab.WithContext(ctx))
114 }
115 if err != nil {
116 return cmdutils.WrapError(err, "failed to revoke service account token")
117 }
119 o.io.LogInfof("%s Revoked token %d of service account %d in %s.\n", o.io.Color().RedCheck(), tokenID, accountID, scope)
121 return nil
122}
Diagnostics9 git calls · 260 ms
CallStatusSizeTime0260 ms
discover200185 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 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=2 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 · 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=44Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · 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=4273 git.response.cache=HIT git.response.duration_ms=3 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=3 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 · 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=322 git.response.cache=HIT git.response.duration_ms=3 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:0200223 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=223 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=41d72d07155f99e3acc572f8fe5ad26c802e4ddc 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=223 git.pkt.payload.max=196 git.sideband.band1.bytes=196 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=196 git.pack.trailer=d9c58aac50f427eb21501a81fe28c81c7c49ae44Open 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=bc7a44691d1232fb2b11b17fa552e72df113fc88 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=47b97954622eae14af0f4bcdd0f169b854b41addOpen pack map
fetch2001.4 KiB242 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=242 git.response.bytes=1455 git.response.cache=MISS git.response.duration_ms=242 git.fetch.wants=50d78088b7882d04ea266ad0032bb86c2b6432e5 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=1455 git.pkt.payload.max=1428 git.sideband.band1.bytes=1428 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
- a5e35191ce79224d
- Request
- c616b9d1…
- 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