GitLabgitlab-org

cli

main
ply8 git calls244 ms100 KiB5 cachedprotocol v2
204 lines7.5 KiBRaw
1package pkgcmd
3import (
4 "context"
5 "errors"
7 "github.com/MakeNowJust/heredoc/v2"
8 "github.com/spf13/cobra"
10 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
12 "gitlab.com/gitlab-org/cli/internal/cmdutils"
13 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/policy"
14 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/purl"
15 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/summary"
16 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/verdict"
17 "gitlab.com/gitlab-org/cli/internal/iostreams"
18 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
19 "gitlab.com/gitlab-org/cli/internal/text"
22type options struct {
23 io *iostreams.IOStreams
24 executor cmdutils.Executor
25 baseRepo func() (projectID string, err error)
26 gitlabClient func() (*gitlab.Client, error)
28 purlArg string
31// NewCmd builds the `glab dependency-firewall package <PURL>` command.
32func NewCmd(f cmdutils.Factory) *cobra.Command {
33 opts := &options{
34 io: f.IO(),
35 executor: f.Executor(),
36 baseRepo: func() (string, error) {
37 r, err := f.BaseRepo()
38 if err != nil {
39 return "", err
40 }
41 return r.FullName(), nil
42 },
43 gitlabClient: f.GitLabClient,
44 }
46 cmd := &cobra.Command{
47 Use: "package <purl>",
48 Short: "Check a package against the GitLab Dependency Firewall. (BETA)",
49 Annotations: map[string]string{
50 mcpannotations.Safe: "true",
51 },
52 Long: heredoc.Docf(`
53 Check a single package coordinate against the GitLab Dependency Firewall policy for the current project and report the outcome (allow, warning, blocked). No package manager binary is required.
55 Supported package URL (PURL) types are %[1]snpm%[1]s, %[1]spypi%[1]s, %[1]smaven%[1]s, %[1]sgem%[1]s, %[1]snuget%[1]s, %[1]scargo%[1]s, and %[1]sgolang%[1]s. The PURL must include a version, for example %[1]spkg:npm/left-pad@1.3.0%[1]s. For %[1]sgolang%[1]s, the module path keeps its case, for example %[1]spkg:golang/github.com/BurntSushi/toml@v1.4.0%[1]s.
57 An allowed package prints one line, for example, %[1]sleft-pad@1.3.0: allowed by the Dependency Firewall.%[1]s A warned or blocked package prints the same summary as the package manager commands.
59 This command does not write to the CI log at %[1]s.gitlab/df/ci-log.json%[1]s, so %[1]sglab dependency-firewall ci-summary%[1]s does not include its result.
61 Exit codes:
63 | Exit code | Meaning |
64 |-----------|---------|
65 | %[1]s0%[1]s | Allowed or warned, or the Dependency Firewall is not evaluating the project. |
66 | %[1]s1%[1]s | The PURL is invalid or has no version, the project or credentials can't be resolved, or the request to GitLab failed. |
67 | %[1]s3%[1]s | Blocked. |
69 An exit code of %[1]s0%[1]s does not always mean GitLab checked the package. If the Dependency Firewall is not evaluating the project, the command prints a line that ends with %[1]sthe Dependency Firewall is not evaluating this project; allowing.%[1]s
71 Unlike the package manager commands, this command does not block the package when the request to GitLab fails. It exits with %[1]s1%[1]s instead.
72 `, "`") + text.BetaString,
73 Example: heredoc.Doc(`
74 # Check an npm package version
75 glab dependency-firewall package pkg:npm/left-pad@1.3.0
77 # Check a PyPI package version
78 glab dependency-firewall package pkg:pypi/requests@2.31.0
80 # Check a Maven package version
81 glab dependency-firewall package pkg:maven/org.slf4j/slf4j-api@2.0.13
83 # Check a RubyGems package version
84 glab dependency-firewall package pkg:gem/rails@7.1.3
86 # Check a NuGet package version
87 glab dependency-firewall package pkg:nuget/Newtonsoft.Json@13.0.1
89 # Check a Cargo package version
90 glab dependency-firewall package pkg:cargo/serde@1.0.197
92 # Check a Go module version
93 glab dependency-firewall package pkg:golang/github.com/pkg/errors@v0.9.1
94 `),
95 Args: cobra.ExactArgs(1),
96 RunE: func(cmd *cobra.Command, args []string) error {
97 if err := opts.complete(args); err != nil {
98 return err
99 }
100 return opts.run(cmd.Context())
101 },
102 }
104 return cmd
107func (o *options) complete(args []string) error {
108 o.purlArg = args[0]
109 return nil
112func (o *options) run(ctx context.Context) error {
113 p, err := purl.Parse(o.purlArg)
114 if err != nil {
115 return cmdutils.WrapError(err, "invalid PURL.")
116 }
118 // The evaluate endpoint requires a version and rejects a blank one with 400,
119 // so reject it here with a clear input error instead of resolving the repo
120 // and client only to turn the predictable rejection into a generic
121 // "failed to check package".
122 if p.Version == "" {
123 return cmdutils.WrapError(
124 errors.New("the PURL must include a version, for example pkg:npm/left-pad@1.3.0"),
125 "invalid PURL.")
126 }
128 projectID, err := o.baseRepo()
129 if err != nil {
130 return cmdutils.WrapError(cmdutils.WithGitRequiredHint(o.executor, err), "failed to resolve GitLab project from the git remote.")
131 }
133 client, err := o.gitlabClient()
134 if err != nil {
135 return cmdutils.WrapError(err, "failed to create a GitLab API client.")
136 }
138 // A single coordinate is checked once, so the CachingChecker's memoization
139 // and coalescing add nothing. More importantly, it never surfaces an error:
140 // it maps every transport, auth, or server failure to a Blocked result. That
141 // is the right fail-closed posture for the proxy guarding an install, but it
142 // erases the "we could not ask" case this command must report as exit 1
143 // rather than a false "blocked". So call the checker directly and own the
144 // fail-open (not evaluating) and fail-report (transport error) decisions
145 // here, bounded by the same timeout the proxy uses.
146 checker, err := policy.New(client)
147 if err != nil {
148 return cmdutils.WrapError(err, "failed to initialize the Dependency Firewall policy checker.")
149 }
151 checkCtx, cancel := context.WithTimeout(ctx, policy.CheckTimeout)
152 defer cancel()
153 res, err := checker.Check(checkCtx, policy.Request{
154 Coordinate: policy.Coordinate{
155 Ecosystem: p.Type,
156 Name: p.Name,
157 Version: p.Version,
158 },
159 ProjectID: projectID,
160 Operation: policy.Download,
161 })
162 // p.Name and p.Version are percent-decoded from user input, so a crafted
163 // PURL can carry newlines or terminal control sequences. summary.Render
164 // sanitizes its cells; the direct-output paths below must sanitize too.
165 coordinate := text.SanitizeInline(p.Name + "@" + p.Version)
167 switch {
168 case policy.IsNotEvaluating(err):
169 // Fail open: the firewall is not evaluating this project (feature flag
170 // off, not enforced, or the token cannot see the project), so there is
171 // no verdict to apply and the package is allowed.
172 o.io.LogInfof("%s: the Dependency Firewall is not evaluating this project; allowing.\n", coordinate)
173 return nil
174 case err != nil:
175 return cmdutils.WrapError(err, "failed to check package.")
176 }
178 // An allow carries no verdict entry, so summary.Render would print "no
179 // activity recorded". Report it with a single explicit line instead; only a
180 // warn or block goes through the summary box.
181 if res.Verdict == verdict.Allowed {
182 o.io.LogInfof("%s: allowed by the Dependency Firewall.\n", coordinate)
183 return nil
184 }
186 summary.Render(o.io, verdictEntries(p.Name, p.Version, res))
188 if res.Verdict == verdict.Blocked {
189 return cmdutils.WrapErrorWithCode(cmdutils.SilentError, verdict.BlockedExitCode,
190 "Dependency Firewall blocked the package.")
191 }
192 return nil
195// verdictEntries turns a warn or block Result into the single verdict entry the
196// summary renders. It is not called for an allow, which run reports directly.
197func verdictEntries(name, version string, res policy.Result) []verdict.Entry {
198 return []verdict.Entry{{
199 Package: name,
200 Version: version,
201 Verdict: res.Verdict,
202 Reason: res.Reason,
203 }}
Diagnostics8 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=fetch
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 KiB2 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=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=4
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=44
Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit4 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=4
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=2
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=fb03183b7909876778f740d196d7516811003517
Open pack map
fetchtree:0200650 B · hit2 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=650
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=cdbc4acb219224e81172d025f80739e1e383a784
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=650
git.pkt.payload.max=623
git.sideband.band1.bytes=623
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=623
git.pack.trailer=aabedefb11d4348efc4e1168413c6ec52cbf90d2
Open pack map
fetchtree:0200139 B · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=228
git.response.bytes=139
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=405066cbafa2fe3c3f417c7d5b0b4fa08af7e0c6
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=139
git.pkt.payload.max=112
git.sideband.band1.bytes=112
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=112
git.pack.trailer=6d4a07360b8e3402b6d00337f32d3ed2e0ba42e3
Open pack map
fetch2002.8 KiB228 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=2901
git.response.cache=MISS
git.response.duration_ms=228
git.fetch.wants=f5a5c6a0055170f522cbf4d725214e777268d973
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=2901
git.pkt.payload.max=2874
git.sideband.band1.bytes=2874
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
a5e2522daea21ae0
Request
0c0842db…
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