GitLabgitlab-org
cli
main
1package revoke
3import (
4 "fmt"
5 "strconv"
7 "github.com/MakeNowJust/heredoc/v2"
8 "github.com/spf13/cobra"
10 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
12 "gitlab.com/gitlab-org/cli/internal/api"
13 "gitlab.com/gitlab-org/cli/internal/cmdutils"
14 "gitlab.com/gitlab-org/cli/internal/commands/token/filter"
15 "gitlab.com/gitlab-org/cli/internal/commands/token/tokenutils"
16 "gitlab.com/gitlab-org/cli/internal/glrepo"
17 "gitlab.com/gitlab-org/cli/internal/iostreams"
18 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
19)
21type options struct {
22 tokenID int64
23 name string
25 apiClient func(repoHost string) (*api.Client, error)
26 io *iostreams.IOStreams
27 baseRepo func() (glrepo.Interface, error)
29 user string
30 group string
31 outputFormat string
32}
34func NewCmdRevoke(f cmdutils.Factory) *cobra.Command {
35 opts := &options{
36 io: f.IO(),
37 apiClient: f.ApiClient,
38 baseRepo: f.BaseRepo,
39 }
41 cmd := &cobra.Command{
42 Use: "revoke <token-name|token-id>",
43 Short: "Revoke user, group, or project access tokens.",
44 Aliases: []string{"revoke", "rm"},
45 Args: cobra.ExactArgs(1),
46 ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
47 return tokenutils.NameOrIDCompletions(cmd.Context(), f, cmd, args, toComplete)
48 },
49 Long: heredoc.Doc(`
50 Revoke a user, group or project access token. If multiple tokens with the same name exist, you can specify
51 the ID of the token.
53 The output format can be either "JSON" or "text". The JSON output will show the meta information of the
54 revoked token. The normal text output is a description of the revoked token name and ID.
56 Administrators can revoke personal access tokens belonging to other users.
57 `),
59 Example: heredoc.Doc(`
60 # Revoke a project access token of current project
61 glab token revoke my-project-token
63 # Revoke a project access token of a specific project
64 glab token revoke --repo user/my-repo my-project-token
66 # Revoke a group access token
67 glab token revoke --group group/sub-group my-group-token
69 # Revoke my personal access token
70 glab token revoke --user @me my-personal-token
72 # Revoke a personal access token of another user (administrator only)
73 glab token revoke --user johndoe johns-personal-token`),
74 Annotations: map[string]string{
75 mcpannotations.Destructive: "true",
76 },
77 RunE: func(cmd *cobra.Command, args []string) error {
78 if err := opts.complete(cmd, args); err != nil {
79 return err
80 }
82 return opts.run()
83 },
84 }
86 cmdutils.EnableRepoOverride(cmd, f)
87 cmd.Flags().StringVarP(&opts.group, "group", "g", "", "Revoke group access token. Ignored if a user or repository argument is set.")
88 cobra.CheckErr(cmd.RegisterFlagCompletionFunc("group", tokenutils.GroupCompletions))
89 cmd.Flags().StringVarP(&opts.user, "user", "U", "", "Revoke personal access token. Use @me for the current user.")
90 cobra.CheckErr(cmd.RegisterFlagCompletionFunc("user", tokenutils.UserCompletions))
91 cmdutils.EnableJSONOutput(cmd, opts.io, &opts.outputFormat, "Format output as: text, json. 'text' provides the name and ID of the revoked token; 'json' outputs the token with metadata.")
92 cmd.MarkFlagsMutuallyExclusive("group", "user")
93 return cmd
94}
96func (o *options) complete(cmd *cobra.Command, args []string) error {
97 if tokenID, err := strconv.ParseInt(args[0], 10, 64); err != nil {
98 o.name = args[0]
99 } else {
100 o.tokenID = tokenID
101 }
102 if group, err := cmdutils.GroupOverride(cmd); err != nil {
103 return err
104 } else {
105 o.group = group
106 }
108 return nil
109}
111func (o *options) run() error {
112 // NOTE: this command can not only be used for projects,
113 // so we have to manually check for the base repo, if it doesn't exist,
114 // we bootstrap the client with the default hostname.
115 var repoHost string
116 if baseRepo, err := o.baseRepo(); err == nil {
117 repoHost = baseRepo.RepoHost()
118 }
119 apiClient, err := o.apiClient(repoHost)
120 if err != nil {
121 return err
122 }
123 client := apiClient.Lab()
125 var outputToken any
126 var outputTokenValue string
128 if o.user != "" {
129 user, err := api.UserByName(client, o.user)
130 if err != nil {
131 return cmdutils.FlagError{Err: err}
132 }
134 options := &gitlab.ListPersonalAccessTokensOptions{
135 ListOptions: gitlab.ListOptions{PerPage: 100},
136 UserID: &user.ID,
137 }
138 tokens, err := gitlab.ScanAndCollect(func(p gitlab.PaginationOptionFunc) ([]*gitlab.PersonalAccessToken, *gitlab.Response, error) {
139 return client.PersonalAccessTokens.ListPersonalAccessTokens(options, p)
140 })
141 if err != nil {
142 return err
143 }
144 var token *gitlab.PersonalAccessToken
145 tokens = filter.Filter(tokens, func(t *gitlab.PersonalAccessToken) bool {
146 return t.Active && (t.Name == o.name || t.ID == o.tokenID)
147 })
148 switch len(tokens) {
149 case 1:
150 token = tokens[0]
151 case 0:
152 return cmdutils.FlagError{Err: fmt.Errorf("no token found with the name %q", o.name)}
153 default:
154 return cmdutils.FlagError{Err: fmt.Errorf("multiple tokens found with the name %q; use the ID instead", o.name)}
155 }
156 if _, err = client.PersonalAccessTokens.RevokePersonalAccessTokenByID(token.ID); err != nil {
157 return err
158 }
159 token.Revoked = true
160 outputToken = token
161 outputTokenValue = fmt.Sprintf("revoked %s %s %d", o.user, token.Name, token.ID)
162 } else {
163 if o.group != "" {
164 options := &gitlab.ListGroupAccessTokensOptions{ListOptions: gitlab.ListOptions{PerPage: 100}}
165 tokens, err := gitlab.ScanAndCollect(func(p gitlab.PaginationOptionFunc) ([]*gitlab.GroupAccessToken, *gitlab.Response, error) {
166 return client.GroupAccessTokens.ListGroupAccessTokens(o.group, options, p)
167 })
168 if err != nil {
169 return err
170 }
171 var token *gitlab.GroupAccessToken
172 tokens = filter.Filter(tokens, func(t *gitlab.GroupAccessToken) bool {
173 return t.Active && (t.Name == o.name || t.ID == o.tokenID)
174 })
175 switch len(tokens) {
176 case 1:
177 token = tokens[0]
178 case 0:
179 return cmdutils.FlagError{Err: fmt.Errorf("no token found with the name %q", o.name)}
180 default:
181 return cmdutils.FlagError{Err: fmt.Errorf("multiple tokens found with the name %q; use the ID instead", o.name)}
182 }
184 if _, err = client.GroupAccessTokens.RevokeGroupAccessToken(o.group, token.ID); err != nil {
185 return err
186 }
187 token.Revoked = true
188 outputToken = token
189 outputTokenValue = fmt.Sprintf("revoked %s %d", token.Name, token.ID)
190 } else {
191 repo, err := o.baseRepo()
192 if err != nil {
193 return err
194 }
195 options := &gitlab.ListProjectAccessTokensOptions{ListOptions: gitlab.ListOptions{PerPage: 100}}
196 tokens, err := gitlab.ScanAndCollect(func(p gitlab.PaginationOptionFunc) ([]*gitlab.ProjectAccessToken, *gitlab.Response, error) {
197 return client.ProjectAccessTokens.ListProjectAccessTokens(repo.FullName(), options, p)
198 })
199 if err != nil {
200 return err
201 }
202 tokens = filter.Filter(tokens, func(t *gitlab.ProjectAccessToken) bool {
203 return t.Active && (t.Name == o.name || t.ID == o.tokenID)
204 })
205 var token *gitlab.ProjectAccessToken
206 switch len(tokens) {
207 case 1:
208 token = tokens[0]
209 case 0:
210 return cmdutils.FlagError{Err: fmt.Errorf("no token found with the name %q", o.name)}
211 default:
212 return cmdutils.FlagError{Err: fmt.Errorf("multiple tokens found with the name %q; use the ID instead", o.name)}
213 }
215 if _, err := client.ProjectAccessTokens.RevokeProjectAccessToken(repo.FullName(), token.ID); err != nil {
216 return err
217 }
218 token.Revoked = true
219 outputToken = token
220 outputTokenValue = fmt.Sprintf("revoked %s %d", token.Name, token.ID)
221 }
222 }
224 if o.outputFormat == "json" {
225 if err := o.io.PrintJSON(outputToken); err != nil {
226 return err
227 }
228 } else {
229 if _, err := fmt.Fprintln(o.io.StdOut, outputTokenValue); err != nil { //nolint:forbidigo // write error must propagate to the caller
230 return err
231 }
232 }
234 return nil
235}
Diagnostics8 git calls · 208 ms
CallStatusSizeTime0208 ms
discover200185 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch 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 KiB · hit2 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=HIT git.response.duration_ms=2 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=3 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:0200358 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=358 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=218b543e075f31e84c52193d193103408c52cb34 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=358 git.pkt.payload.max=331 git.sideband.band1.bytes=331 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=331 git.pack.trailer=fbb77d44047cd3e1ee682eb9483227c59489e50fOpen 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=194 git.response.bytes=138 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=fb2a827af46071634b453729567d34a70b35ae11 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=d6388980558ca093ef295f67582fc274896ef765Open pack map
fetch2002.3 KiB194 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2359 git.response.cache=MISS git.response.duration_ms=194 git.fetch.wants=5263fa5e6b134e6f9acc51d44908e241876b38be 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=2359 git.pkt.payload.max=2332 git.sideband.band1.bytes=2332 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
- a5e2e365fcc04af4
- Request
- 1c21fbe0…
- 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