GitLabgitlab-org
cli
main
1//go:build !integration
3package generate
5import (
6 "crypto"
7 "crypto/elliptic"
8 "crypto/rsa"
9 "path/filepath"
10 "testing"
12 "github.com/golang-jwt/jwt/v5"
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
15)
17//go:generate go run testdata/generate_test_keys.go
19// Helper function to load pre-generated test keys
20func loadTestKey(t *testing.T, filename string) crypto.PrivateKey {
21 t.Helper()
23 key, err := loadPrivateKey(filepath.Join("testdata", filename), nil)
24 require.NoError(t, err)
25 return key
26}
28func getEd25519PrivateKey(t *testing.T) crypto.PrivateKey {
29 t.Helper()
31 return loadTestKey(t, "ed25519_key.pem")
32}
34func getEcdsaPrivateKey(t *testing.T, curve elliptic.Curve) crypto.PrivateKey {
35 t.Helper()
37 var filename string
38 switch curve {
39 case elliptic.P256():
40 filename = "ecdsa_p256_key.pem"
41 case elliptic.P384():
42 filename = "ecdsa_p384_key.pem"
43 case elliptic.P521():
44 filename = "ecdsa_p521_key.pem"
45 case elliptic.P224():
46 filename = "ecdsa_p224_key.pem"
47 default:
48 t.Fatalf("unsupported ecdsa key for curve %v", curve)
49 }
50 return loadTestKey(t, filename)
51}
53func getRsaPrivateKey(t *testing.T, bits int) crypto.PrivateKey {
54 t.Helper()
56 var filename string
57 switch bits {
58 case 2047:
59 filename = "rsa_2047_key.pem"
60 case 8193:
61 filename = "rsa_8193_key.pem"
62 case 2048:
63 filename = "rsa_2048_key.pem"
64 case 8192:
65 filename = "rsa_8192_key.pem"
66 default:
67 t.Fatalf("unsupported RSA key size %d", bits)
68 }
69 return loadTestKey(t, filename)
70}
72func TestSigningMethods(t *testing.T) {
73 tests := []struct {
74 name string
75 key crypto.PrivateKey
76 expectedSigningMethod jwt.SigningMethod
77 shouldError bool
78 }{
79 {
80 name: "supported ed25519",
81 key: getEd25519PrivateKey(t),
82 expectedSigningMethod: jwt.SigningMethodEdDSA,
83 },
84 {
85 name: "unsupported Ecdsa P256",
86 key: getEcdsaPrivateKey(t, elliptic.P256()),
87 shouldError: true, // GitLab only support the _sk variant which isn't supported by glab yet
88 },
89 {
90 name: "unsupported Ecdsa P384",
91 key: getEcdsaPrivateKey(t, elliptic.P384()),
92 shouldError: true, // GitLab only support the _sk variant which isn't supported by glab yet
93 },
94 {
95 name: "unsupported Ecdsa 521",
96 key: getEcdsaPrivateKey(t, elliptic.P521()),
97 shouldError: true, // GitLab only support the _sk variant which isn't supported by glab yet
98 },
99 {
100 name: "unsupported Ecdsa 224",
101 key: getEcdsaPrivateKey(t, elliptic.P224()),
102 shouldError: true, // ssh-keygen doesn't do 224 bit keys
103 },
104 {
105 name: "unsupported RSA 2047",
106 key: getRsaPrivateKey(t, 2047),
107 shouldError: true,
108 },
109 {
110 name: "unsupported RSA 8193",
111 key: getRsaPrivateKey(t, 8193),
112 shouldError: true,
113 },
114 {
115 name: "supported RSA 2048",
116 key: getRsaPrivateKey(t, 2048),
117 expectedSigningMethod: jwt.SigningMethodRS512,
118 },
119 {
120 name: "supported RSA 8192",
121 key: getRsaPrivateKey(t, 8192),
122 expectedSigningMethod: jwt.SigningMethodRS512,
123 },
124 {
125 name: "unsupported unknown private key",
126 key: crypto.PrivateKey(1),
127 shouldError: true,
128 },
129 }
131 for _, tt := range tests {
132 t.Run(tt.name, func(t *testing.T) {
133 signingMethod, err := getSigningMethod(tt.key)
134 if tt.shouldError {
135 assert.Error(t, err)
136 } else {
137 require.NoError(t, err)
138 assert.Equal(t, tt.expectedSigningMethod, signingMethod)
139 }
140 })
141 }
142}
144type TestPasswordReader struct {
145 password string
146}
148func NewTestPasswordReader(password string) *TestPasswordReader {
149 return &TestPasswordReader{password}
150}
152func (pr TestPasswordReader) Read() ([]byte, error) {
153 return []byte(pr.password), nil
154}
156func TestLoadPrivateKey(t *testing.T) {
157 testData := []struct {
158 path string
159 passwordReader PasswordReader
160 shouldError bool
161 }{
162 {
163 path: "./testdata/file_does_not_exist",
164 shouldError: true,
165 },
166 {
167 path: "./testdata/no_password.pem",
168 },
169 {
170 path: "./testdata/with_password.pem",
171 passwordReader: NewTestPasswordReader("test_password"),
172 },
173 {
174 path: "./testdata/with_password.pem",
175 passwordReader: NewTestPasswordReader("wrong_password"),
176 shouldError: true,
177 },
178 {
179 path: "./testdata/not_a_key.txt",
180 shouldError: true,
181 },
182 }
184 for _, testCase := range testData {
185 privateKey, err := loadPrivateKey(testCase.path, testCase.passwordReader)
186 if testCase.shouldError {
187 assert.Error(t, err)
188 } else {
189 assert.IsType(t, &rsa.PrivateKey{}, privateKey)
190 }
191 }
192}
Diagnostics8 git calls · 245 ms
CallStatusSizeTime0245 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 KiB218 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=206 git.response.bytes=59849 git.response.cache=EXPIRED git.response.duration_ms=218 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=44Open pack map
fetchtree:02001.8 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=1857 git.response.cache=HIT git.response.duration_ms=3 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=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200358 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=358 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=5d783fac547a70b19ca138c47d067b44febb52da 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=358 git.pkt.payload.max=331 git.sideband.band1.bytes=331 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=331 git.pack.trailer=22a484e5ecad6d6e62b224e8a7f90cf5ff085938Open pack map
fetchtree:0200204 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=204 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=7f57e69c1796ef0dd4d7877c582d94d8a96a5369 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=204 git.pkt.payload.max=177 git.sideband.band1.bytes=177 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=177 git.pack.trailer=abcd288a8924fff99397ff5cd51aa278233a24e2Open pack map
fetch2001.3 KiB · hit17 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=16 git.response.bytes=1315 git.response.cache=HIT git.response.duration_ms=17 git.fetch.wants=e790707ff8c39c5fba0d64ae2c87fa4234ef486a 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=1315 git.pkt.payload.max=1288 git.sideband.band1.bytes=1288 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
- a5e1fe96feaca9c0
- Request
- e3623242…
- 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