GitLabgitlab-org

cli

main
ply6 git calls278 ms97 KiB3 cachedprotocol v2
115 lines3.6 KiBRaw
1package api
3import (
4 "testing"
6 "github.com/stretchr/testify/assert"
7 "github.com/stretchr/testify/require"
9 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
11 "gitlab.com/gitlab-org/cli/internal/config"
14// The OAuth flag can reach NewClientFromConfig from two places: the
15// GLAB_IS_OAUTH2 environment variable, or is_oauth2 in the configuration file
16// (where `glab auth login` persists it). The chosen auth scheme depends on
17// which one supplied it, so pin every combination.
18func TestNewClientFromConfig_OAuthFlagProvenance(t *testing.T) {
19 for _, k := range []string{
20 "GITLAB_TOKEN", "GITLAB_ACCESS_TOKEN", "OAUTH_TOKEN",
21 "GLAB_IS_OAUTH2", "JOB_TOKEN", "CI_JOB_TOKEN", "GITLAB_CI",
22 "GITLAB_HOST",
23 } {
24 t.Setenv(k, "")
25 }
27 const storedOAuthHost = `hosts:
28 example.com:
29 is_oauth2: "true"
30 api_host: example.com
31 api_protocol: https
32 user: someone
35 const storedOAuthHostWithToken = `hosts:
36 example.com:
37 is_oauth2: "true"
38 token: stored-oauth-token
39 api_host: example.com
40 api_protocol: https
41 user: someone
44 tests := []struct {
45 name string
46 configYAML string
47 envVars map[string]string
48 expectedAuthKey string
49 expectedAuthVal string
50 }{
51 {
52 // GITLAB_HOST makes the host the default one, for which environment
53 // credentials and flags resolve.
54 name: "environment token alone is a PAT",
55 envVars: map[string]string{"GITLAB_TOKEN": "a-token", "GITLAB_HOST": "example.com"},
56 expectedAuthKey: gitlab.AccessTokenHeaderName,
57 expectedAuthVal: "a-token",
58 },
59 {
60 name: "environment token with environment OAuth flag is OAuth",
61 envVars: map[string]string{"GITLAB_TOKEN": "a-token", "GLAB_IS_OAUTH2": "true", "GITLAB_HOST": "example.com"},
62 expectedAuthKey: gitlab.OAuthTokenHeaderName,
63 expectedAuthVal: "Bearer a-token",
64 },
65 {
66 // An environment token is assumed to be a PAT even when the host is
67 // configured for OAuth, so that a glpat- in GITLAB_TOKEN is not sent
68 // as a Bearer token. Changing this flips the header for anyone
69 // injecting an OAuth token through the environment.
70 name: "environment token with stored OAuth flag is a PAT",
71 configYAML: storedOAuthHost,
72 envVars: map[string]string{"GITLAB_TOKEN": "a-token"},
73 expectedAuthKey: gitlab.AccessTokenHeaderName,
74 expectedAuthVal: "a-token",
75 },
76 {
77 // The environment flag re-asserts OAuth, and is the supported escape
78 // hatch for the row above.
79 name: "environment token with stored and environment OAuth flags is OAuth",
80 configYAML: storedOAuthHost,
81 envVars: map[string]string{"GITLAB_TOKEN": "a-token", "GLAB_IS_OAUTH2": "true"},
82 expectedAuthKey: gitlab.OAuthTokenHeaderName,
83 expectedAuthVal: "Bearer a-token",
84 },
85 {
86 // The `glab auth login` path: both the flag and the token come from
87 // the configuration file, so nothing is overridden.
88 name: "stored token with stored OAuth flag is OAuth",
89 configYAML: storedOAuthHostWithToken,
90 expectedAuthKey: gitlab.OAuthTokenHeaderName,
91 expectedAuthVal: "Bearer stored-oauth-token",
92 },
93 }
95 for _, tt := range tests {
96 t.Run(tt.name, func(t *testing.T) {
97 for k, v := range tt.envVars {
98 t.Setenv(k, v)
99 }
101 cfg := config.NewBlankConfig()
102 if tt.configYAML != "" {
103 cfg = config.NewFromString(tt.configYAML)
104 }
106 client, err := NewClientFromConfig("example.com", cfg, false, "test-agent")
107 require.NoError(t, err)
109 key, value, err := client.AuthSource().Header(t.Context())
110 require.NoError(t, err)
111 assert.Equal(t, tt.expectedAuthKey, key)
112 assert.Equal(t, tt.expectedAuthVal, value)
113 })
114 }
Diagnostics6 git calls · 278 ms
CallStatusSizeTime0278 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 KiB2 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=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=5
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 · hit5 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=5
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.1 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=268
git.response.bytes=1165
git.response.cache=HIT
git.response.duration_ms=3
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
fetch2001.3 KiB268 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1318
git.response.cache=MISS
git.response.duration_ms=268
git.fetch.wants=ea0588f98cf7ac532c775dd8c3c059c36201d002
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=1318
git.pkt.payload.max=1291
git.sideband.band1.bytes=1291
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
a5e19005cd075b65
Request
588be1a8…
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