GitLabgitlab-org
cli
main
1package policy
3import (
4 "context"
5 "errors"
6 "fmt"
7 "runtime/debug"
8 "sync"
10 "gitlab.com/gitlab-org/cli/internal/dbg"
11 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/verdict"
12)
14// Logf logs a diagnostic line. It matches iostreams.IOStreams.LogErrorf so
15// callers can pass io.LogErrorf directly; nil is allowed (no logging).
16type Logf func(format string, a ...any)
18// CachingChecker wraps a Checker with per-request memoization and a
19// fail-closed error policy. It is the only place caching and fail-mode live,
20// so both Checker implementations stay simple. Safe for concurrent use: the
21// proxy calls Check from multiple tunnel goroutines.
22//
23// Concurrent calls for the same request are coalesced: the first caller runs
24// the inner check while the rest block on the entry's sync.Once, so a popular
25// transitive dependency triggers a single inner check at cold start rather
26// than one per tunnel goroutine.
27//
28// Lifetime invariant: the cache grows unbounded and is never evicted. It is
29// sized for a single package-manager run (the proxy's lifetime). Callers must
30// not share one CachingChecker across unrelated or long-lived workloads.
31type CachingChecker struct {
32 inner Checker
33 log Logf
35 mu sync.Mutex
36 cache map[Request]*entry
38 // notEvaluating latches the "firewall is not evaluating this project"
39 // state per project. "Not evaluating" is a project-level condition (feature
40 // flag off, not enforced, or the token cannot see the project), so once one
41 // coordinate observes it, every later coordinate for the same project is
42 // allowed without another round trip and without repeating the log line.
43 // An npm install resolving hundreds of transitive dependencies then emits a
44 // single diagnostic and issues a single evaluate call, instead of one per
45 // coordinate.
46 //
47 // Trade-off: this latches an allow for the project for the rest of the run,
48 // so a genuinely transient 404 (rolling deploy, brief gateway blip) that
49 // happens to be the first response for a project fixes the fail-open
50 // decision for the whole run. That is deliberate: a package-manager run is
51 // short, "not evaluating" is overwhelmingly a stable configuration state,
52 // and paying one evaluate call per coordinate to catch a rare transient
53 // flip is not worth the request volume. The fail-closed branch keeps its
54 // per-request retryability (it deletes the entry), so the two paths differ
55 // on purpose.
56 notEvaluating map[string]bool
57}
59// entry memoizes and coalesces a single request. once runs the inner check
60// exactly once; res holds the decided result. Keying the cache on the
61// comparable Request means there is one definition of request identity, so
62// there is no way for two distinct requests to collide on a rendered string.
63type entry struct {
64 once sync.Once
65 res Result
66}
68// NewCachingChecker wraps inner. log may be nil.
69func NewCachingChecker(inner Checker, log Logf) *CachingChecker {
70 return &CachingChecker{
71 inner: inner,
72 log: log,
73 cache: map[Request]*entry{},
74 notEvaluating: map[string]bool{},
75 }
76}
78// Check returns the policy result for r, memoized per request for the lifetime
79// of the checker. The error return is always nil: an inner failure is mapped
80// to a fail-closed Blocked result rather than surfaced, so callers get a
81// verdict on every path. It is part of the signature only to satisfy Checker.
82func (c *CachingChecker) Check(ctx context.Context, r Request) (Result, error) {
83 c.mu.Lock()
84 // A project already observed to be not evaluating short-circuits to allow:
85 // the state is project-level, so there is nothing to gain from another
86 // evaluate call or another log line for a later coordinate.
87 if c.notEvaluating[r.ProjectID] {
88 c.mu.Unlock()
89 return Result{}, nil
90 }
91 e, ok := c.cache[r]
92 if !ok {
93 e = &entry{}
94 c.cache[r] = e
95 }
96 c.mu.Unlock()
98 e.once.Do(func() {
99 // A panicking inner check leaves this fail-closed result for every
100 // caller of the entry, and the entry is dropped so a retry runs again.
101 e.res = InternalErrorResult(r.Coordinate)
102 defer func() {
103 if v := recover(); v != nil {
104 dbg.Debugf("dependency firewall policy check panicked for %s: %v\n%s", r.Key(), v, debug.Stack())
105 // r can be one of several candidates for a request, and another
106 // candidate may decide the verdict, so the line doesn't claim one.
107 if c.log != nil {
108 c.log("warning: glab hit an internal error while checking %s. %s\n", internalErrorSubject(r.Coordinate), internalErrorAdvice)
109 }
110 c.mu.Lock()
111 delete(c.cache, r)
112 c.mu.Unlock()
113 }
114 }()
115 // The inner check is shared across every caller coalesced onto this
116 // entry, so it must not inherit one caller's cancellation: WithoutCancel
117 // keeps a single participant walking away from killing the shared check
118 // for the rest. WithoutCancel also drops the deadline, and the API
119 // client has no timeout of its own, so the deadline is put back to keep
120 // a stalled API failing closed. A hit past this point ignores ctx
121 // entirely, which is intentional for a per-run cache.
122 shared, cancel := context.WithoutCancel(ctx), context.CancelFunc(func() {})
123 if deadline, ok := ctx.Deadline(); ok {
124 shared, cancel = context.WithDeadline(shared, deadline)
125 }
126 defer cancel()
127 res, err := c.inner.Check(shared, r)
128 switch {
129 case errors.Is(err, errFirewallNotEvaluating):
130 // Fail open: the firewall is not evaluating this project (feature
131 // flag off, not enforced, or the token cannot see the project), so
132 // there is no verdict to apply and the package is allowed. Latch
133 // the project so later coordinates short-circuit, and log only on
134 // the first observation so an unexpected allow-all (a typo'd repo
135 // or under-scoped token) is visible once rather than repeated per
136 // coordinate or silent.
137 c.mu.Lock()
138 firstObservation := !c.notEvaluating[r.ProjectID]
139 c.notEvaluating[r.ProjectID] = true
140 c.mu.Unlock()
141 if firstObservation && c.log != nil {
142 c.log("dependency firewall not evaluating %s, allowing: %v\n", r.Key(), err)
143 }
144 e.res = Result{}
145 case err != nil:
146 // Fail closed: a policy decision we cannot obtain is treated as a
147 // block. Drop the entry so the failure stays retryable within the
148 // run instead of memoizing a transient error.
149 if c.log != nil {
150 c.log("warning: dependency firewall policy check failed for %s: %v\n", r.Key(), err)
151 }
152 c.mu.Lock()
153 delete(c.cache, r)
154 c.mu.Unlock()
155 e.res = Result{
156 Verdict: verdict.Blocked,
157 Reason: fmt.Sprintf("policy check failed: %v", err),
158 CheckFailed: true,
159 }
160 default:
161 e.res = res
162 }
163 })
165 return e.res, nil
166}
Diagnostics7 git calls · 281 ms
CallStatusSizeTime0281 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 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=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 · 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:0200361 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=361 git.response.cache=HIT git.response.duration_ms=2 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=26d32585a3a474717e00a318584ccd16918c9d32Open pack map
fetchtree:0200332 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=332 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=cb813f0d675e9dca14f167e8c7788d61eb19627f 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=332 git.pkt.payload.max=305 git.sideband.band1.bytes=305 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=305 git.pack.trailer=5e210c1831ecf78390a6fe6e1784efd898336da0Open pack map
fetch2002.8 KiB271 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=270 git.response.bytes=2831 git.response.cache=MISS git.response.duration_ms=271 git.fetch.wants=e94b8b826bc63ff7c32582d57ba3ab542d3e9d12 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=2831 git.pkt.payload.max=2804 git.sideband.band1.bytes=2804 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
- a5e2159beb89984a
- Request
- f5b512c7…
- 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