GitLabgitlab-org

cli

main
ply7 git calls270 ms98 KiB4 cachedprotocol v2
149 lines5.1 KiBRaw
1//go:build !integration
3package session
5import (
6 "bytes"
7 "crypto/rand"
8 "crypto/rsa"
9 "crypto/x509"
10 "crypto/x509/pkix"
11 "encoding/pem"
12 "fmt"
13 "math/big"
14 "os"
15 "path/filepath"
16 "strings"
17 "testing"
18 "time"
20 "github.com/stretchr/testify/assert"
21 "github.com/stretchr/testify/require"
24func envMap(t *testing.T, env []string) map[string]string {
25 t.Helper()
26 m := map[string]string{}
27 for _, e := range env {
28 k, v, ok := strings.Cut(e, "=")
29 require.True(t, ok, "env entry without =: %q", e)
30 m[k] = v
31 }
32 return m
35func TestBuildJobEnvRoutesAndTrustsAdditively(t *testing.T) {
36 t.Parallel()
37 m := envMap(t, BuildJobEnv("http://127.0.0.1:9999", "/state/ca-bundle.pem", "corp.internal"))
39 // Plain HTTP is routed too, so a plain-HTTP registry is checked as well.
40 for _, k := range []string{"HTTP_PROXY", "HTTPS_PROXY", "http_proxy", "https_proxy"} {
41 assert.Equal(t, "http://127.0.0.1:9999", m[k], k)
42 }
43 // npm does not just follow HTTPS_PROXY, so its own routing variable is set.
44 assert.Equal(t, "http://127.0.0.1:9999", m["NPM_CONFIG_PROXY"])
46 // Every trust variable points at the merged bundle, which keeps the caller's
47 // own anchors, including a NODE_EXTRA_CA_CERTS or CURL_CA_BUNDLE it replaces.
48 for _, k := range []string{"NODE_EXTRA_CA_CERTS", "CURL_CA_BUNDLE", "REQUESTS_CA_BUNDLE", "PIP_CERT", "CARGO_HTTP_CAINFO", "GIT_SSL_CAINFO", "SSL_CERT_FILE", "BUNDLE_SSL_CA_CERT"} {
49 assert.Equal(t, "/state/ca-bundle.pem", m[k], k)
50 }
52 for _, k := range []string{"NO_PROXY", "no_proxy"} {
53 for _, host := range []string{"corp.internal", "localhost", "127.0.0.1", "::1"} {
54 assert.Contains(t, strings.Split(m[k], ","), host, k)
55 }
56 }
59func TestMergeNoProxyAddsLoopbackAndDedups(t *testing.T) {
60 t.Parallel()
61 // The caller's NO_PROXY and no_proxy arrive joined, so either can be empty
62 // and both can repeat a host.
63 got := strings.Split(mergeNoProxy("corp.internal, localhost,,corp.internal,svc.local"), ",")
64 assert.Equal(t, []string{"corp.internal", "localhost", "svc.local", "127.0.0.1", "::1"}, got)
67func TestWriteMergedBundleIncludesProxyCAAndCallerBundles(t *testing.T) {
68 t.Parallel()
69 dir := t.TempDir()
70 caPath := filepath.Join(dir, "proxy-ca.pem")
71 caPEM := testCAPEM(t)
72 require.NoError(t, os.WriteFile(caPath, caPEM, 0o644))
73 userBundle := filepath.Join(dir, "user.pem")
74 userPEM := testCAPEM(t)
75 require.NoError(t, os.WriteFile(userBundle, userPEM, 0o644))
77 var logged []string
78 logf := func(format string, a ...any) { logged = append(logged, fmt.Sprintf(format, a...)) }
79 missing := filepath.Join(dir, "missing.pem")
80 out, err := WriteMergedBundle(dir, caPath, []string{userBundle, userBundle, missing}, logf)
81 require.NoError(t, err)
83 merged, err := os.ReadFile(out)
84 require.NoError(t, err)
85 assert.Contains(t, string(merged), string(caPEM))
86 assert.Equal(t, 1, bytes.Count(merged, userPEM), "a repeated caller bundle is added once")
88 var missingLogged bool
89 for _, l := range logged {
90 assert.True(t, strings.HasSuffix(l, "\n"), "log line ends in a newline: %q", l)
91 missingLogged = missingLogged || strings.Contains(l, missing)
92 }
93 assert.True(t, missingLogged, "an unreadable caller bundle is reported, got %q", logged)
96func TestWriteMergedBundleFailsWithoutProxyCA(t *testing.T) {
97 t.Parallel()
98 dir := t.TempDir()
99 _, err := WriteMergedBundle(dir, filepath.Join(dir, "absent.pem"), nil, func(string, ...any) {})
100 assert.Error(t, err)
103func TestCallerBundlePaths(t *testing.T) {
104 t.Parallel()
105 got := CallerBundlePaths([]string{
106 "SSL_CERT_FILE=/etc/corp.pem",
107 "BUNDLE_SSL_CA_CERT=/etc/bundler.pem",
108 "REQUESTS_CA_BUNDLE=",
109 "PATH=/usr/bin",
110 })
111 assert.Equal(t, []string{"/etc/corp.pem", "/etc/bundler.pem"}, got)
114func TestShellExportsQuotesValues(t *testing.T) {
115 t.Parallel()
116 out := ShellExports([]string{"A=simple", "B=with spaces", "C=with'quote"})
117 assert.Equal(t, "export A='simple'\nexport B='with spaces'\nexport C='with'\\''quote'\n", out)
120// testCAPEM returns a unique self-signed CA certificate in PEM, for bundle tests.
121func testCAPEM(t *testing.T) []byte {
122 t.Helper()
123 key, err := rsa.GenerateKey(rand.Reader, 2048)
124 require.NoError(t, err)
125 serial, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128))
126 require.NoError(t, err)
127 tmpl := &x509.Certificate{
128 SerialNumber: serial,
129 Subject: pkix.Name{CommonName: "test CA"},
130 NotBefore: time.Now().Add(-time.Hour),
131 NotAfter: time.Now().Add(time.Hour),
132 IsCA: true,
133 BasicConstraintsValid: true,
134 }
135 der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
136 require.NoError(t, err)
137 return pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der})
140func TestSystemTrustAnchorNameIsPerStateDir(t *testing.T) {
141 t.Parallel()
142 // The anchor name embeds the state dir's base name so two concurrent jobs on
143 // one host stage distinct files and stop removes only its own run's CA.
144 a := systemTrustAnchorName("/tmp/glab-df-job-proxy-aaa111")
145 b := systemTrustAnchorName("/tmp/glab-df-job-proxy-bbb222")
146 assert.NotEqual(t, a, b)
147 assert.Equal(t, "glab-df-proxy-glab-df-job-proxy-aaa111.crt", a)
148 assert.True(t, strings.HasSuffix(a, ".crt"))
Diagnostics7 git calls · 270 ms
CallStatusSizeTime0270 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 KiB9 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=9
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 · 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=4273
git.response.cache=HIT
git.response.duration_ms=2
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 · hit3 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=3
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:0200361 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=361
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=506cba622ceda1473ee3cde63969809c144b7247
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=361
git.pkt.payload.max=334
git.sideband.band1.bytes=334
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=334
git.pack.trailer=26d32585a3a474717e00a318584ccd16918c9d32
Open pack map
fetchtree:0200279 B · 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=279
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=97ca01ef53be93b101ee1321413cdbf4a0c86c70
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=279
git.pkt.payload.max=252
git.sideband.band1.bytes=252
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=252
git.pack.trailer=bfb89af7a897fc72572aa6703a2786499c09a4f4
Open pack map
fetch2002.1 KiB252 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=252
git.response.bytes=2152
git.response.cache=MISS
git.response.duration_ms=252
git.fetch.wants=d50266e714adb5924089a85fac25c2d71961f321
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=2152
git.pkt.payload.max=2125
git.sideband.band1.bytes=2125
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
a5e1abb88b852c38
Request
4a531de1…
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