GitLabgitlab-org

cli

main
ply7 git calls244 ms98 KiB5 cachedprotocol v2
123 lines4.9 KiBRaw
1//go:build !integration
3package proxy
5import (
6 "net/http"
7 "net/http/httptest"
8 "strings"
9 "testing"
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
14 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/policy"
17type ecoStub struct {
18 eco string
19 matched bool
22func (s ecoStub) Match(*http.Request) Match {
23 if !s.matched {
24 return Match{}
25 }
26 return Match{Matched: true, Pass: true, Coordinate: policy.Coordinate{Ecosystem: s.eco}}
29func TestCompositeMatcherReturnsFirstMatch(t *testing.T) {
30 t.Parallel()
31 c := CompositeMatcher{ecoStub{"npm", false}, ecoStub{"pypi", true}, ecoStub{"gem", true}}
32 got := c.Match(&http.Request{})
33 require.True(t, got.Matched)
34 assert.Equal(t, "pypi", got.Coordinate.Ecosystem)
37func TestCompositeMatcherNoMatch(t *testing.T) {
38 t.Parallel()
39 c := CompositeMatcher{ecoStub{"npm", false}, ecoStub{"pypi", false}}
40 assert.False(t, c.Match(&http.Request{}).Matched)
41 assert.False(t, CompositeMatcher{}.Match(&http.Request{}).Matched)
44func TestDefaultMatchersClaimEachEcosystem(t *testing.T) {
45 t.Parallel()
46 tests := []struct {
47 url string
48 want policy.Coordinate
49 }{
50 {"https://registry.npmjs.org/left-pad/-/left-pad-1.3.0.tgz", policy.Coordinate{Ecosystem: "npm", Name: "left-pad", Version: "1.3.0"}},
51 {"https://files.pythonhosted.org/packages/ab/cd/requests-2.31.0-py3-none-any.whl", policy.Coordinate{Ecosystem: "pypi", Name: "requests", Version: "2.31.0"}},
52 {"https://repo.maven.apache.org/maven2/org/slf4j/slf4j-api/2.0.13/slf4j-api-2.0.13.jar", policy.Coordinate{Ecosystem: "maven", Name: "org.slf4j:slf4j-api", Version: "2.0.13"}},
53 {"https://rubygems.org/gems/rails-7.1.3.gem", policy.Coordinate{Ecosystem: "gem", Name: "rails", Version: "7.1.3"}},
54 {"https://crates.io/api/v1/crates/serde/1.0.197/download", policy.Coordinate{Ecosystem: "cargo", Name: "serde", Version: "1.0.197"}},
55 {"https://api.nuget.org/v3-flatcontainer/newtonsoft.json/13.0.1/newtonsoft.json.13.0.1.nupkg", policy.Coordinate{Ecosystem: "nuget", Name: "newtonsoft.json", Version: "13.0.1"}},
56 }
57 for _, tt := range tests {
58 t.Run(tt.want.Ecosystem, func(t *testing.T) {
59 t.Parallel()
60 got := DefaultMatchers().Match(httptest.NewRequest(http.MethodGet, tt.url, http.NoBody))
61 require.True(t, got.Matched)
62 assert.Equal(t, tt.want, got.Coordinate)
63 assert.Equal(t, policy.Download, got.Operation)
64 })
65 }
68func TestDefaultMatchersIgnoreNonJSONPut(t *testing.T) {
69 t.Parallel()
70 // A curl -T of an arbitrary file, or a cargo publish, must not be read as an
71 // npm publish by the job-wide proxy.
72 for _, contentType := range []string{"", "application/octet-stream"} {
73 req := httptest.NewRequest(http.MethodPut, "https://artifacts.example.com/upload/build.tar.gz", strings.NewReader(`{"not":"a packument"}`))
74 if contentType != "" {
75 req.Header.Set("Content-Type", contentType)
76 }
77 assert.False(t, DefaultMatchers().Match(req).Matched, "Content-Type %q", contentType)
78 }
81func TestDefaultMatchersInspectJSONPutAsNPMPublish(t *testing.T) {
82 t.Parallel()
83 req := httptest.NewRequest(http.MethodPut, "https://registry.npmjs.org/left-pad", strings.NewReader(`{"name":"left-pad","versions":{"9.9.9":{}},"_attachments":{"left-pad-9.9.9.tgz":{}}}`))
84 req.Header.Set("Content-Type", "application/json; charset=utf-8")
85 got := DefaultMatchers().Match(req)
86 require.True(t, got.Matched)
87 assert.Equal(t, "npm", got.Coordinate.Ecosystem)
88 assert.Equal(t, policy.Upload, got.Operation)
91func TestDefaultMatchersIgnoreGenericArchiveOffRegistryPath(t *testing.T) {
92 t.Parallel()
93 // A job-wide proxy sees unrelated toolchain downloads. A bare .tar.gz/.zip
94 // off a non-registry path must not be claimed as a PyPI sdist, which would
95 // evaluate a made-up coordinate and can fail the job closed on an API error.
96 for _, url := range []string{
97 "https://go.dev/dl/go1.22.3.linux-amd64.tar.gz",
98 "https://nodejs.org/dist/v20.11.1/node-v20.11.1-linux-x64.tar.gz",
99 "https://example.com/downloads/build-1.0.zip",
100 } {
101 assert.False(t, DefaultMatchers().Match(httptest.NewRequest(http.MethodGet, url, http.NoBody)).Matched, url)
102 }
105func TestDefaultMatchersClaimPyPIDownloads(t *testing.T) {
106 t.Parallel()
107 tests := []struct {
108 name, url, pkg, version string
109 }{
110 // A wheel is python-specific, so it is claimed on any path.
111 {"wheel off a non-registry path", "https://pypi.example.com/simple/requests-2.31.0-py3-none-any.whl", "requests", "2.31.0"},
112 // An sdist is claimed when the path carries the registry's /packages/ segment.
113 {"sdist under /packages/", "https://files.pythonhosted.org/packages/ab/cd/requests-2.31.0.tar.gz", "requests", "2.31.0"},
114 }
115 for _, tt := range tests {
116 t.Run(tt.name, func(t *testing.T) {
117 t.Parallel()
118 got := DefaultMatchers().Match(httptest.NewRequest(http.MethodGet, tt.url, http.NoBody))
119 require.True(t, got.Matched)
120 assert.Equal(t, policy.Coordinate{Ecosystem: "pypi", Name: tt.pkg, Version: tt.version}, got.Coordinate)
121 })
122 }
Diagnostics7 git calls · 244 ms
CallStatusSizeTime0244 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 KiB · hit3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=59849
git.response.cache=HIT
git.response.duration_ms=3
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 · 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=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 · hit1 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=1
git.response.bytes=361
git.response.cache=HIT
git.response.duration_ms=1
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:02001.0 KiB · hit6 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=6
git.response.bytes=1051
git.response.cache=HIT
git.response.duration_ms=6
git.fetch.filter=tree:0
git.fetch.wants=7a2a90605315ba5b37f63c08726e6b2caa26e48d
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=1051
git.pkt.payload.max=1024
git.sideband.band1.bytes=1024
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1024
git.pack.trailer=7503ef36a75d3595b473035c69e639fd0ce41598
Open pack map
fetch2001.7 KiB231 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=231
git.response.bytes=1765
git.response.cache=MISS
git.response.duration_ms=231
git.fetch.wants=7a3992b248dfefd4173264b58447e33d47dd7dcd
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=1765
git.pkt.payload.max=1738
git.sideband.band1.bytes=1738
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
a5e1db05de1ca151
Request
82078067…
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