GitLabgitlab-org

cli

main
ply7 git calls519 ms98 KiB4 cachedprotocol v2
146 lines4.8 KiBRaw
1// Package autoflow connects to the AutoFlow service of GitLab Relay with a
2// token-exchange token minted by GitLab for the gitlab-autoflow audience.
3package autoflow
5import (
6 "context"
7 "crypto/tls"
8 "errors"
9 "fmt"
10 "net/http"
11 "sync"
12 "time"
14 "github.com/golang-jwt/jwt/v5"
15 "github.com/google/uuid"
16 autoflowrpcv1 "gitlab.com/gitlab-org/protos/gen/go/gitlab/autoflow/rpc/v1"
17 "google.golang.org/grpc"
18 "google.golang.org/grpc/credentials"
19 "google.golang.org/grpc/credentials/insecure"
21 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
23 "gitlab.com/gitlab-org/cli/internal/api/tokenexchange"
26const audience = "gitlab-autoflow"
28// tokenLifetime is short because Relay accepts any valid token for its
29// organization's namespace, so a leaked token can start workflows there until
30// it expires.
31const tokenLifetime = time.Minute
33// refreshMargin leaves a call enough of the token's lifetime to complete
34// before Relay sees it expire.
35const refreshMargin = 20 * time.Second
37// TokenBindingSize is the exact length StartWorkflow requires of a token
38// binding.
39const TokenBindingSize = 64
41// Dial exchanges the caller's GitLab credential for a token Relay accepts on
42// its public listener and connects to the AutoFlow service at address over
43// TLS, or in cleartext if tlsConfig is nil. Each call exchanges a fresh token
44// when the current one has less than refreshMargin left, so the connection
45// outlives a single token. The caller must close the returned connection.
46func Dial(ctx context.Context, gl *gitlab.Client, address string, tlsConfig *tls.Config) (*Conn, error) {
47 src := &tokenSource{gl: gl, now: time.Now, cleartext: tlsConfig == nil}
48 // Exchanged before the dial, so a user without the feature flag gets
49 // GitLab's answer instead of a failed RPC.
50 if err := src.refresh(ctx); err != nil {
51 return nil, err
52 }
53 transport := insecure.NewCredentials()
54 if tlsConfig != nil {
55 transport = credentials.NewTLS(tlsConfig)
56 }
57 conn, err := grpc.NewClient(address,
58 grpc.WithTransportCredentials(transport),
59 grpc.WithPerRPCCredentials(src),
60 )
61 if err != nil {
62 return nil, fmt.Errorf("failed to connect to %s: %w", address, err)
63 }
64 return &Conn{AutoFlowServiceClient: autoflowrpcv1.NewAutoFlowServiceClient(conn), conn: conn, token: src.token}, nil
67// Conn is a connection to the AutoFlow service.
68type Conn struct {
69 autoflowrpcv1.AutoFlowServiceClient
71 conn *grpc.ClientConn
72 token string
75// Close closes the connection.
76func (c *Conn) Close() error {
77 return c.conn.Close()
80// Organization returns the organization the connection's token is issued
81// for, its gitlab.origin_id claim.
82func (c *Conn) Organization() (uuid.UUID, error) {
83 var claims struct {
84 jwt.RegisteredClaims
85 GitLab struct {
86 Origin string `json:"origin"`
87 OriginID string `json:"origin_id"`
88 } `json:"gitlab"`
89 }
90 // GitLab's TLS connection vouches for the token, as it does for the claims
91 // tokenexchange.Exchange reads.
92 if _, _, err := jwt.NewParser().ParseUnverified(c.token, &claims); err != nil {
93 return uuid.Nil, fmt.Errorf("failed to decode the AutoFlow token: %w", err)
94 }
95 if claims.GitLab.Origin != "organization" {
96 return uuid.Nil, fmt.Errorf("the AutoFlow token is not issued for an organization (its gitlab.origin claim is %q)", claims.GitLab.Origin)
97 }
98 id, err := uuid.Parse(claims.GitLab.OriginID)
99 if err != nil || id == uuid.Nil {
100 return uuid.Nil, fmt.Errorf("the gitlab.origin_id claim of the AutoFlow token is %q, not an organization UUID", claims.GitLab.OriginID)
101 }
102 return id, nil
105type tokenSource struct {
106 gl *gitlab.Client
107 now func() time.Time
108 cleartext bool
110 mu sync.Mutex
111 token string
112 expiresAt time.Time
115func (s *tokenSource) GetRequestMetadata(ctx context.Context, _ ...string) (map[string]string, error) {
116 s.mu.Lock()
117 defer s.mu.Unlock()
119 if s.now().Add(refreshMargin).After(s.expiresAt) {
120 if err := s.refresh(ctx); err != nil {
121 return nil, err
122 }
123 }
124 return map[string]string{"authorization": "Bearer " + s.token}, nil
127func (s *tokenSource) RequireTransportSecurity() bool {
128 return !s.cleartext
131func (s *tokenSource) refresh(ctx context.Context) error {
132 // Measured on the local clock before the request, so a GitLab clock that
133 // runs ahead cannot make a token look fresher than it is.
134 expiresAt := s.now().Add(tokenLifetime)
135 result, err := tokenexchange.Exchange(ctx, s.gl, audience, tokenLifetime)
136 if err != nil {
137 // GitLab answers 400 for an audience it does not mint for the caller.
138 if errResp, ok := errors.AsType[*gitlab.ErrorResponse](err); ok && errResp.StatusCode == http.StatusBadRequest {
139 err = fmt.Errorf("%w (GitLab mints gitlab-autoflow tokens only for users with the token_exchange_autoflow_audience feature flag, and not for CI job tokens)", err)
140 }
141 return fmt.Errorf("failed to get an AutoFlow token: %w", err)
142 }
143 s.token = result.Token
144 s.expiresAt = expiresAt
145 return nil
Diagnostics7 git calls · 519 ms
CallStatusSizeTime0519 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 KiB203 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=197
git.response.bytes=59849
git.response.cache=EXPIRED
git.response.duration_ms=203
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
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:0 · deepen 1020032 KiB · hit7 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=7
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
fetchtree:02001.1 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=1165
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=71c8407e0ff0e0af10f6a041ae330f754b68e7eb
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=1165
git.pkt.payload.max=1138
git.sideband.band1.bytes=1138
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1138
git.pack.trailer=61886367c69619d8db2b2cf2d7d24dca2016141c
Open pack map
fetchtree:0200254 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=254
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=92c0c916127a028255da99edec485fe247f8ada1
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=254
git.pkt.payload.max=227
git.sideband.band1.bytes=227
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=227
git.pack.trailer=97becb66cba7786f3c98f230357b525d141c4025
Open pack map
fetch2002.0 KiB308 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=308
git.response.bytes=2033
git.response.cache=MISS
git.response.duration_ms=308
git.fetch.wants=86fc543d0516c9f4a97329d5e4d7eec01fed197e
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=2033
git.pkt.payload.max=2006
git.sideband.band1.bytes=2006
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
a5e1a1b1f9513cd0
Request
ba4eb904…
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