GitLabgitlab-org
cli
main
1//go:build !integration
3package verify
5import (
6 "errors"
7 "os"
8 "testing"
9 "time"
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
13 "go.uber.org/mock/gomock"
15 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
16 gitlab_testing "gitlab.com/gitlab-org/api/client-go/v3/testing"
18 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
19)
21func mocks(t *testing.T, tc *gitlab_testing.TestClient, project *gitlab.Project) {
22 t.Helper()
23 tc.MockProjects.EXPECT().
24 GetProject("OWNER/REPO", gomock.Any(), gomock.Any()).
25 Return(project, &gitlab.Response{}, nil).
26 Times(1)
28 tc.MockAttestations.EXPECT().
29 ListAttestations("OWNER/REPO", "f2d4bc357309c633154f1e94c6fda3583ae429f6adc882d4d9006380ea3a79da").
30 Return([]*gitlab.Attestation{
31 {
32 ID: 1,
33 IID: 1,
34 CreatedAt: new(time.Now().Add(-24 * time.Hour)),
35 UpdatedAt: new(time.Now().Add(-24 * time.Hour)),
36 ExpireAt: new(time.Now().Add(-24 * time.Hour)),
37 ProjectID: 1,
38 BuildID: 1,
39 Status: "success",
40 PredicateKind: "provenance",
41 PredicateType: "https://slsa.dev/provenance/v1",
42 SubjectDigest: "76c34666f719ef14bd2b124a7db51e9c05e4db2e12a84800296d559064eebe2c",
43 DownloadURL: "https://gitlab.example.com/api/v4/projects/1/attestations/1/download",
44 },
45 }, &gitlab.Response{}, nil).
46 Times(1)
48 attestation, err := os.ReadFile("testdata/attestationDownload.json")
49 require.NoError(t, err)
51 tc.MockAttestations.EXPECT().
52 DownloadAttestation("OWNER/REPO", int64(1)).
53 Return(attestation, nil, nil).
54 Times(1)
55}
57func Test_AttestationVerify(t *testing.T) {
58 t.Parallel()
60 ctrl := gomock.NewController(t)
61 tc := gitlab_testing.NewTestClientWithCtrl(ctrl)
62 mockExec := cmdtest.NewMockExecutor(ctrl)
64 exec := cmdtest.SetupCmdForTest(t,
65 NewCmd,
66 false,
67 cmdtest.WithGitLabClient(tc.Client),
68 cmdtest.WithExecutor(mockExec),
69 )
71 mocks(t, tc, &gitlab.Project{
72 WebURL: "https://gitlab.com/OWNER/REPO",
73 PathWithNamespace: "OWNER/REPO",
74 })
76 mockExec.EXPECT().LookPath(gomock.Any()).Return("/usr/bin/cosign", nil)
77 mockExec.EXPECT().ExecWithCombinedOutput(gomock.Any(), "/usr/bin/cosign", cmdtest.SliceMatch[string](
78 "verify-blob-attestation",
79 "--new-bundle-format",
80 "--bundle",
81 gomock.Any(),
82 "--type",
83 "slsaprovenance1",
84 "./testdata/example_artifact.txt",
85 "--certificate-identity-regexp",
86 `^https://gitlab\.com/OWNER/REPO/`,
87 "--certificate-oidc-issuer",
88 "https://gitlab.com",
89 ), nil)
91 output, err := exec("OWNER/REPO ./testdata/example_artifact.txt")
93 require.NoError(t, err)
94 assert.Contains(t, output.String(), "Artifact provenance successfully verified")
95}
97func Test_AttestationVerify_SelfManaged(t *testing.T) {
98 t.Parallel()
100 tests := []struct {
101 name string
102 webURL string
103 path string
104 identityRegexp string
105 issuer string
106 }{
107 {
108 name: "http with a port and subgroups",
109 webURL: "http://gdk.test:3000/group/sub/project",
110 path: "group/sub/project",
111 identityRegexp: `^http://gdk\.test:3000/group/sub/project/`,
112 issuer: "http://gdk.test:3000",
113 },
114 {
115 name: "relative URL root",
116 webURL: "https://example.com/gitlab/my.group/repo",
117 path: "my.group/repo",
118 identityRegexp: `^https://example\.com/gitlab/my\.group/repo/`,
119 issuer: "https://example.com/gitlab",
120 },
121 }
123 for _, tt := range tests {
124 t.Run(tt.name, func(t *testing.T) {
125 t.Parallel()
127 ctrl := gomock.NewController(t)
128 tc := gitlab_testing.NewTestClientWithCtrl(ctrl)
129 mockExec := cmdtest.NewMockExecutor(ctrl)
131 exec := cmdtest.SetupCmdForTest(t,
132 NewCmd,
133 false,
134 cmdtest.WithGitLabClient(tc.Client),
135 cmdtest.WithExecutor(mockExec),
136 )
138 // The argument stays OWNER/REPO, so the expected identity and issuer can only come from the API response.
139 mocks(t, tc, &gitlab.Project{
140 WebURL: tt.webURL,
141 PathWithNamespace: tt.path,
142 })
144 mockExec.EXPECT().LookPath(gomock.Any()).Return("/usr/bin/cosign", nil)
145 mockExec.EXPECT().ExecWithCombinedOutput(gomock.Any(), "/usr/bin/cosign", cmdtest.SliceMatch[string](
146 "verify-blob-attestation",
147 "--new-bundle-format",
148 "--bundle",
149 gomock.Any(),
150 "--type",
151 "slsaprovenance1",
152 "./testdata/example_artifact.txt",
153 "--certificate-identity-regexp",
154 tt.identityRegexp,
155 "--certificate-oidc-issuer",
156 tt.issuer,
157 ), nil)
159 output, err := exec("OWNER/REPO ./testdata/example_artifact.txt")
161 require.NoError(t, err)
162 assert.Contains(t, output.String(), "Artifact provenance successfully verified")
163 })
164 }
165}
167func Test_AttestationVerify_UnexpectedWebURL(t *testing.T) {
168 t.Parallel()
170 ctrl := gomock.NewController(t)
171 tc := gitlab_testing.NewTestClientWithCtrl(ctrl)
172 mockExec := cmdtest.NewMockExecutor(ctrl)
174 exec := cmdtest.SetupCmdForTest(t,
175 NewCmd,
176 false,
177 cmdtest.WithGitLabClient(tc.Client),
178 cmdtest.WithExecutor(mockExec),
179 )
181 tc.MockProjects.EXPECT().
182 GetProject("OWNER/REPO", gomock.Any(), gomock.Any()).
183 Return(&gitlab.Project{
184 WebURL: "https://gitlab.example.com/other/path",
185 PathWithNamespace: "OWNER/REPO",
186 }, &gitlab.Response{}, nil).
187 Times(1)
189 _, err := exec("OWNER/REPO ./testdata/example_artifact.txt")
191 assert.EqualError(t, err, `unexpected web URL "https://gitlab.example.com/other/path" for project OWNER/REPO`)
192}
194func Test_AttestationVerify_Failure(t *testing.T) {
195 t.Parallel()
197 ctrl := gomock.NewController(t)
198 tc := gitlab_testing.NewTestClientWithCtrl(ctrl)
199 mockExec := cmdtest.NewMockExecutor(ctrl)
201 exec := cmdtest.SetupCmdForTest(t,
202 NewCmd,
203 false,
204 cmdtest.WithGitLabClient(tc.Client),
205 cmdtest.WithExecutor(mockExec),
206 )
208 mocks(t, tc, &gitlab.Project{
209 WebURL: "https://gitlab.com/OWNER/REPO",
210 PathWithNamespace: "OWNER/REPO",
211 })
213 mockExec.EXPECT().LookPath(gomock.Any()).Return("/usr/bin/cosign", nil)
214 mockExec.EXPECT().ExecWithCombinedOutput(gomock.Any(), "/usr/bin/cosign", gomock.Any(), nil).Return(nil, errors.New("some error"))
216 _, err := exec("OWNER/REPO ./testdata/example_artifact.txt")
218 assert.EqualError(t, err, "some error: ")
219}
Diagnostics8 git calls · 231 ms
CallStatusSizeTime0231 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 KiB · hit2 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=HIT 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=2 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=44Open pack map
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=2 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: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:0200144 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=144 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=d192c83fd96cac2cf4ea89fac4f5cda25c60b9b1 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=144 git.pkt.payload.max=117 git.sideband.band1.bytes=117 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=117 git.pack.trailer=e01d18bd53779a818aff8901bb36c6df59e28e09Open pack map
fetchtree:0200170 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=170 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=a4a7d2cf13784f9bf9f117ba57ed012a3e193c28 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=170 git.pkt.payload.max=143 git.sideband.band1.bytes=143 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=143 git.pack.trailer=83929781b15d9da4716ed77c1106e4329326454bOpen pack map
fetch2001.7 KiB218 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=218 git.response.bytes=1763 git.response.cache=MISS git.response.duration_ms=218 git.fetch.wants=6fa9049dcf6517e4096ae501e588742290c5a7b6 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=1763 git.pkt.payload.max=1736 git.sideband.band1.bytes=1736 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
- a5e2e065a9fe2308
- Request
- dfd94a65…
- 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