GitLabgitlab-org
cli
main
1package verify
3import (
4 "context"
5 "crypto/sha256"
6 "encoding/hex"
7 "fmt"
8 "io"
9 "os"
10 "regexp"
11 "strings"
13 "github.com/MakeNowJust/heredoc/v2"
14 "github.com/spf13/cobra"
16 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
18 "gitlab.com/gitlab-org/cli/internal/api"
19 "gitlab.com/gitlab-org/cli/internal/cmdutils"
20 "gitlab.com/gitlab-org/cli/internal/iostreams"
21 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
22 "gitlab.com/gitlab-org/cli/internal/text"
23)
25const (
26 installationUrl = "https://docs.sigstore.dev/cosign/system_config/installation/"
27 cosign = "cosign"
28 tempFilePrefix = "glabattestationverify"
29)
31type options struct {
32 gitlabClient func() (*gitlab.Client, error)
33 io *iostreams.IOStreams
34 exec cmdutils.Executor
36 project string
37 filename string
38}
40func NewCmd(f cmdutils.Factory) *cobra.Command {
41 opts := &options{
42 gitlabClient: f.GitLabClient,
43 io: f.IO(),
44 exec: f.Executor(),
45 }
47 attestationVerifyCmd := &cobra.Command{
48 Use: "verify <project-id> <artifact-path>",
49 Short: `Verify the provenance of a specific artifact or file. (EXPERIMENTAL)`,
50 Long: heredoc.Docf(`
51 Verify the provenance of an artifact built by a GitLab CI/CD pipeline.
52 This command checks the artifact's signed attestation against the expected
53 GitLab project and pipeline.
55 This command requires the cosign binary. To install it, see
56 [Cosign installation](https://docs.sigstore.dev/cosign/system_config/installation/).
58 This command works with any GitLab instance. It finds the project and its attestations on the instance from your current context: the %[1]sGITLAB_HOST%[1]s variable, your Git remote, or your glab configuration.
60 This command verifies only attestations signed with Sigstore (%[1]sSIGSTORE%[1]s). It does not yet support attestations signed with the project's certificate authority (%[1]sLOCAL_CA%[1]s).
62 For more information about attestations, see:
64 - [Attestations API](https://docs.gitlab.com/api/attestations/)
65 - [SLSA provenance specification](https://docs.gitlab.com/ci/pipeline_security/slsa/provenance_v1/)
66 - [SLSA software attestations](https://slsa.dev/spec/v1.2/attestation-model)
67 `, "`") + text.ExperimentalString,
68 Args: cobra.ExactArgs(2),
69 Example: heredoc.Doc(`
70 # Verify attestation for filename.txt in the gitlab-org/gitlab project
71 glab attestation verify gitlab-org/gitlab filename.txt
73 # Verify attestation for filename.txt in the project with ID 123
74 glab attestation verify 123 filename.txt`),
75 Annotations: map[string]string{
76 mcpannotations.Safe: "true",
77 },
78 RunE: func(cmd *cobra.Command, args []string) error {
79 opts.project = args[0]
80 opts.filename = args[1]
82 return opts.run(cmd.Context())
83 },
84 }
86 return attestationVerifyCmd
87}
89func (o *options) run(ctx context.Context) error {
90 client, err := o.gitlabClient()
91 if err != nil {
92 return err
93 }
95 project, err := api.GetProject(client, o.project)
96 if err != nil {
97 return err
98 }
100 issuer, ok := strings.CutSuffix(project.WebURL, "/"+project.PathWithNamespace)
101 if !ok {
102 return fmt.Errorf("unexpected web URL %q for project %s", project.WebURL, project.PathWithNamespace)
103 }
105 subjectDigest, err := o.sha256(o.filename)
106 if err != nil {
107 return err
108 }
110 provenance, err := o.retrieveProvenanceMetadata(client, subjectDigest)
111 if err != nil {
112 return err
113 }
115 bundle, err := o.downloadBundle(client, provenance.IID)
116 if err != nil {
117 return err
118 }
120 err = o.verify(ctx, o.filename, project.WebURL, issuer, bundle)
121 if err != nil {
122 return err
123 }
125 o.success()
127 return nil
128}
130func (o *options) sha256(filename string) (string, error) {
131 f, err := os.Open(filename)
132 if err != nil {
133 return "", err
134 }
135 defer f.Close()
137 h := sha256.New()
138 if _, err := io.Copy(h, f); err != nil {
139 return "", err
140 }
142 return hex.EncodeToString(h.Sum(nil)), nil
143}
145func (o *options) retrieveProvenanceMetadata(client *gitlab.Client, subjectDigest string) (*gitlab.Attestation, error) {
146 attestations, _, err := client.Attestations.ListAttestations(o.project, subjectDigest)
147 if err != nil {
148 return nil, err
149 }
151 for _, attestation := range attestations {
152 if attestation.PredicateKind == "provenance" {
153 return attestation, nil
154 }
155 }
157 return nil, fmt.Errorf("unable to find a provenance statement for %s", subjectDigest)
158}
160func (o *options) downloadBundle(client *gitlab.Client, attestationIID int64) ([]byte, error) {
161 provenanceStatement, _, err := client.Attestations.DownloadAttestation(o.project, attestationIID)
162 if err != nil {
163 return nil, err
164 }
166 return provenanceStatement, nil
167}
169func (o *options) bundleTempFile(bundleBytes []byte) (filename string, err error) { //nolint:nonamedreturns
170 f, err := os.CreateTemp("", tempFilePrefix)
171 if err != nil {
172 return
173 }
174 filename = f.Name()
176 // Registered before the write so a failed write still closes the handle.
177 defer func() {
178 cerr := f.Close()
179 if err == nil {
180 err = cerr
181 }
182 }()
184 _, err = f.Write(bundleBytes)
185 return
186}
188func (o *options) verify(ctx context.Context, filename, projectURL, issuer string, bundleBytes []byte) (err error) {
189 cosignPath, err := o.exec.LookPath(cosign)
190 if err != nil {
191 return fmt.Errorf("unable to locate the `%s` binary; please install following these instructions: %s", cosign, installationUrl)
192 }
194 bundlePath, err := o.bundleTempFile(bundleBytes)
195 defer func() {
196 rerr := os.Remove(bundlePath)
197 if err == nil {
198 err = rerr
199 }
200 }()
202 if err != nil {
203 return
204 }
206 args := []string{
207 "verify-blob-attestation",
208 "--new-bundle-format",
209 "--bundle",
210 bundlePath,
211 "--type",
212 "slsaprovenance1",
213 filename,
214 "--certificate-identity-regexp",
215 "^" + regexp.QuoteMeta(projectURL) + "/",
216 "--certificate-oidc-issuer",
217 issuer,
218 }
220 out, err := o.exec.ExecWithCombinedOutput(ctx, cosignPath, args, nil)
221 if err != nil {
222 return fmt.Errorf("%w: %s", err, out)
223 }
225 return
226}
228func (o *options) success() {
229 c := o.io.Color()
231 o.io.LogInfof("%s • Artifact provenance successfully verified. Signatures confirm %s was attested by %s\n\n", c.Green("VERIFIED"), o.filename, o.project)
232}
Diagnostics8 git calls · 233 ms
CallStatusSizeTime0233 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 · 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 · 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=1857 git.response.cache=HIT git.response.duration_ms=2 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 · 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=170 git.response.cache=HIT git.response.duration_ms=2 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
fetch2002.3 KiB221 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=221 git.response.bytes=2323 git.response.cache=MISS git.response.duration_ms=221 git.fetch.wants=724778b15411ca3829e186a257855ebc0589bd07 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=2323 git.pkt.payload.max=2296 git.sideband.band1.bytes=2296 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
- a5e2d8d42b3d14c2
- Request
- 9cd9a6e0…
- 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