GitLabgitlab-org

cli

main
ply9 git calls238 ms100 KiB7 cachedprotocol v2
260 lines7.9 KiBRaw
1package get_token
3import (
4 "context"
5 "crypto/rand"
6 "encoding/base64"
7 "encoding/json"
8 "errors"
9 "fmt"
10 "net/http"
11 "strings"
12 "time"
14 "github.com/MakeNowJust/heredoc/v2"
15 "github.com/spf13/cobra"
16 "github.com/zalando/go-keyring"
17 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
18 clientauthenticationv1 "k8s.io/client-go/pkg/apis/clientauthentication/v1"
20 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
22 "gitlab.com/gitlab-org/cli/internal/cmdutils"
23 "gitlab.com/gitlab-org/cli/internal/commands/cluster/agent/agentutils"
24 "gitlab.com/gitlab-org/cli/internal/iostreams"
25 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
28const (
29 clientAuthenticationApiV1 = "client.authentication.k8s.io/v1"
30 clientAuthenticationExecCredential = "ExecCredential"
31 buffer = 5 * time.Minute
32 flagTokenExpiryDuration = "token-expiry-duration"
33 flagCheckRevoked = "check-revoked"
34 tokenExpiryDurationDefault = 24 * time.Hour
35 minTokenExpiryDuration = 24 * time.Hour
38var patScopes = []string{"k8s_proxy"}
40type options struct {
41 gitlabClient func() (*gitlab.Client, error)
42 io *iostreams.IOStreams
44 agentID int64
45 tokenExpiryDuration time.Duration
46 cacheMode agentutils.CacheMode
47 checkRevoked bool
50func NewCmdAgentGetToken(f cmdutils.Factory) *cobra.Command {
51 opts := options{
52 io: f.IO(),
53 gitlabClient: f.GitLabClient,
54 }
55 agentGetTokenCmd := &cobra.Command{
56 Use: "get-token [flags]",
57 Short: `Create a personal access token for a GitLab Agent for Kubernetes.`,
58 Long: heredoc.Docf(`
59 The token has the %[1]sk8s_proxy%[1]s scope and is valid until the end of the current day.
61 You might receive an email from your GitLab instance that a new personal
62 access token has been created.
63 `, "`"),
64 Example: heredoc.Doc(`
65 # Get a token for agent 123
66 glab cluster agent get-token --agent 123`),
67 Args: cobra.NoArgs,
68 ValidArgsFunction: cobra.NoFileCompletions,
69 Annotations: map[string]string{
70 mcpannotations.Exclude: "true",
71 },
72 RunE: func(cmd *cobra.Command, args []string) error {
73 if err := opts.validate(); err != nil {
74 return err
75 }
76 return opts.run(cmd.Context())
77 },
78 }
79 fl := agentGetTokenCmd.Flags()
80 fl.Int64VarP(&opts.agentID, "agent", "a", 0, "The numerical Agent ID to connect to.")
81 fl.DurationVar(&opts.tokenExpiryDuration, flagTokenExpiryDuration, tokenExpiryDurationDefault, "Duration for how long the generated tokens should be valid for. Minimum is 1 day and the effective expiry is always at the end of the day, the time is ignored.")
82 fl.BoolVar(&opts.checkRevoked, flagCheckRevoked, false, "Check if a cached token is revoked. This requires an API call to GitLab which adds latency every time a cached token is accessed.")
83 agentutils.AddTokenCacheModeFlag(fl, &opts.cacheMode)
84 cobra.CheckErr(agentGetTokenCmd.MarkFlagRequired("agent"))
86 cmdutils.AddJQFlag(agentGetTokenCmd, f.IO())
87 return agentGetTokenCmd
90func (o *options) validate() error {
91 if o.tokenExpiryDuration < minTokenExpiryDuration {
92 return fmt.Errorf("token expiry duration must be at least 24 hours")
93 }
94 return nil
97func (o *options) run(ctx context.Context) error {
98 pat, err := o.cachedPAT(ctx)
99 if err != nil {
100 return err
101 }
103 execCredential := clientauthenticationv1.ExecCredential{
104 TypeMeta: metav1.TypeMeta{
105 APIVersion: clientAuthenticationApiV1,
106 Kind: clientAuthenticationExecCredential,
107 },
108 Status: &clientauthenticationv1.ExecCredentialStatus{
109 ExpirationTimestamp: &metav1.Time{Time: time.Time(*pat.ExpiresAt).Add(-buffer)},
110 Token: fmt.Sprintf("pat:%d:%s", o.agentID, pat.Token),
111 },
112 }
114 return o.io.PrintJSON(execCredential)
117func (o *options) cachedPAT(ctx context.Context) (*gitlab.PersonalAccessToken, error) {
118 client, err := o.gitlabClient()
119 if err != nil {
120 return nil, err
121 }
123 createFunc := func() (*gitlab.PersonalAccessToken, error) {
124 randomBytes := make([]byte, 16)
126 _, err = rand.Read(randomBytes)
127 if err != nil {
128 return nil, err
129 }
131 patName := fmt.Sprintf("glab-k8s-proxy-%x", randomBytes)
132 patExpiresAt := time.Now().Add(o.tokenExpiryDuration).UTC()
134 pat, _, err := client.Users.CreatePersonalAccessTokenForCurrentUser(&gitlab.CreatePersonalAccessTokenForCurrentUserOptions{
135 Name: new(patName),
136 Scopes: new(patScopes),
137 ExpiresAt: new(gitlab.ISOTime(patExpiresAt)),
138 }, gitlab.WithContext(ctx))
139 if err != nil {
140 return nil, err
141 }
142 return pat, nil
143 }
145 if o.cacheMode == agentutils.NoCacheCacheMode {
146 return createFunc()
147 }
149 gitlabInstance := base64.StdEncoding.EncodeToString([]byte(client.BaseURL().String()))
150 id := fmt.Sprintf("%s-%d", gitlabInstance, o.agentID)
152 ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
153 defer cancel()
155 pat, err := withLock(ctx, id, func() (*gitlab.PersonalAccessToken, error) {
156 isTokenRevoked := func(t *gitlab.PersonalAccessToken) (bool, error) {
157 if !o.checkRevoked {
158 return false, nil
159 }
161 return isTokenRevoked(ctx, client, t)
162 }
163 switch o.cacheMode {
164 case agentutils.ForcedKeyringCacheMode:
165 return fromKeyringCache(id, createFunc, isTokenRevoked)
166 case agentutils.ForcedFilesystemCacheMode:
167 return fromFilesystemCache(id, createFunc, isTokenRevoked)
168 case agentutils.KeyringFilesystemFallback:
169 pat, err := fromKeyringCache(id, createFunc, isTokenRevoked)
170 if err != nil {
171 if !errors.Is(err, keyring.ErrUnsupportedPlatform) {
172 return nil, err
173 }
174 return fromFilesystemCache(id, createFunc, isTokenRevoked)
175 }
176 return pat, nil
177 default:
178 panic(fmt.Sprintf("unimplemented cache mode: %s. This is a programming error, please report at https://gitlab.com/gitlab-org/cli/-/issues", o.cacheMode))
179 }
180 })
181 if err != nil {
182 if errors.Is(err, context.DeadlineExceeded) {
183 o.io.LogErrorf("Unable to use cache, proceeding without it: %s\n", err)
184 return createFunc()
185 }
187 return nil, err
188 }
190 return pat, nil
193func fromKeyringCache(id string, createFunc func() (*gitlab.PersonalAccessToken, error), isTokenRevoked func(t *gitlab.PersonalAccessToken) (bool, error)) (*gitlab.PersonalAccessToken, error) {
194 c := cache{
195 storage: &keyringStorage{},
196 id: id,
197 createFunc: createFunc,
198 isTokenRevoked: isTokenRevoked,
199 }
201 return c.get()
204func fromFilesystemCache(id string, createFunc func() (*gitlab.PersonalAccessToken, error), isTokenRevoked func(t *gitlab.PersonalAccessToken) (bool, error)) (*gitlab.PersonalAccessToken, error) {
205 fs, err := newFileStorage()
206 if err != nil {
207 return nil, err
208 }
209 defer fs.close()
211 c := cache{
212 id: id,
213 createFunc: createFunc,
214 storage: fs,
215 isTokenRevoked: isTokenRevoked,
216 }
217 return c.get()
220func isTokenRevoked(ctx context.Context, client *gitlab.Client, token *gitlab.PersonalAccessToken) (bool, error) {
221 req, err := client.NewRequest(http.MethodGet, "personal_access_tokens/self", nil, []gitlab.RequestOptionFunc{
222 gitlab.WithHeader(gitlab.AccessTokenHeaderName, token.Token),
223 gitlab.WithContext(ctx),
224 })
225 if err != nil {
226 return false, fmt.Errorf("unable to create request to check if token is revoked: %w", err)
227 }
229 var t gitlab.PersonalAccessToken
230 _, err = client.Do(req, &t)
231 if err == nil {
232 return t.Revoked, nil
233 }
235 var errResp *gitlab.ErrorResponse
236 if !errors.As(err, &errResp) {
237 return false, nil
238 }
240 // check if token is revoked, the endpoint returns the following if it is (status code is 401):
241 // {
242 // "error": "invalid_token",
243 // "error_description": "Token was revoked. You have to re-authorize from the user."
244 // }
246 if !errResp.HasStatusCode(http.StatusUnauthorized) {
247 return false, err
248 }
249 var response errorResponse
250 jsonErr := json.Unmarshal(errResp.Body, &response)
251 if jsonErr != nil {
252 return false, err // we want to return the original API error
253 }
255 return strings.HasPrefix(response.ErrorDescription, "Token was revoked."), nil
258type errorResponse struct {
259 ErrorDescription string `json:"error_description"`
Diagnostics9 git calls · 238 ms
CallStatusSizeTime0238 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 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=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:0200200 B · 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=200
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=9988d47506dcbe3004b8b72cbdb9d66d6d75cda4
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=200
git.pkt.payload.max=173
git.sideband.band1.bytes=173
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=173
git.pack.trailer=948ee807a1ee817af67f2df94421a202e82325f9
Open pack map
fetchtree:0200405 B · 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=405
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=d271680e941992259de0ff84d4a04bcbba55f531
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=405
git.pkt.payload.max=378
git.sideband.band1.bytes=378
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=378
git.pack.trailer=adfacba5ff1f4fcf28dcaac12e2874f6d48a7934
Open pack map
fetchtree:0200263 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=263
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=2c1df054e09d18899b4c74c207905511ed57f752
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=263
git.pkt.payload.max=236
git.sideband.band1.bytes=236
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=236
git.pack.trailer=6ae3f3b63c9b4fb1e4060b9efa24a84240bbb33e
Open pack map
fetch2002.9 KiB223 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=221
git.response.bytes=2928
git.response.cache=MISS
git.response.duration_ms=223
git.fetch.wants=26be4809d05733787f418780d7c39dc0721594b5
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=2928
git.pkt.payload.max=2901
git.sideband.band1.bytes=2901
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
a5eb08c88e2e21ca
Request
cec1710d…
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