GitLabgitlab-org
cli
main
1package session
3import (
4 "context"
5 "fmt"
6 "os"
7 "os/signal"
8 "path/filepath"
9 "strconv"
10 "syscall"
11 "time"
13 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
15 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/cilog"
16 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/fsx"
17 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/pm"
18 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/policy"
19 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/proxy"
20 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/verdict"
21 "gitlab.com/gitlab-org/cli/internal/iostreams"
22)
24const (
25 // AddrFile holds the proxy's listen address. It is written last — after the
26 // proxy is listening and its CA is on disk — so its presence signals
27 // readiness to `job-proxy start`.
28 AddrFile = "addr"
29 // PIDFile holds the daemon's process id, for `job-proxy stop`.
30 PIDFile = "pid"
31 // CACertFile holds the proxy's MITM CA in PEM, for the job to trust.
32 CACertFile = "proxy-ca.pem"
33)
35// ServeConfig configures the job-level proxy daemon.
36type ServeConfig struct {
37 Client *gitlab.Client
38 ProjectID string
39 BaseDir string // working directory; where .gitlab/df/ci-log.json is written
40 StateDir string // where addr/pid/proxy-ca.pem are written
41 // CACertPath is the ca_cert setting for the GitLab host, trusted for
42 // upstream registry connections alongside the system roots and the CA
43 // bundles named in the environment, as a per-command run does.
44 CACertPath string
45 // ClientCertPath, ClientKeyPath and ClientCertPassword are glab's client_cert,
46 // client_key and client_cert_password for the GitLab host, presented to an
47 // https:// outbound proxy that requires a client certificate, as a
48 // per-command run does. Registries never receive the certificate.
49 ClientCertPath string
50 ClientKeyPath string
51 ClientCertPassword func() (string, error)
52 IO *iostreams.IOStreams
53}
55// flushInterval bounds how stale the CI log can be if the daemon is killed
56// abruptly (the container stops at job end) rather than signalled: at most this
57// much of the last activity could be missing.
58const flushInterval = time.Second
60// Serve runs the job-level inspection proxy until it is signalled or ctx is
61// cancelled, streaming the firewall's verdicts to the CI log so
62// `glab dependency-firewall ci-summary` can still gate the job even though no
63// single package-manager command owns the proxy. It is the body of the detached
64// `job-proxy serve` daemon.
65func Serve(ctx context.Context, cfg ServeConfig) error {
66 base, err := policy.New(cfg.Client)
67 if err != nil {
68 return err
69 }
70 checker := policy.NewCachingChecker(base, cfg.IO.LogErrorf)
72 // Write each verdict to the CI log the instant it is recorded, before the
73 // blocked response reaches the client, so a `ci-summary` that runs right
74 // after a blocked install never races the flush below. The ticker only
75 // retries a failed hook write and bounds the loss on an abrupt kill.
76 opts := []proxy.Option{
77 proxy.WithVerdictHook(func(e verdict.Entry) {
78 writeVerdicts(cfg.BaseDir, []verdict.Entry{e}, cfg.IO.LogErrorf)
79 }),
80 proxy.WithUpstreamFailureWarning(cfg.IO.LogErrorf),
81 }
82 roots, err := pm.UpstreamRootCAs(os.Environ(), cfg.CACertPath)
83 if err != nil {
84 return fmt.Errorf("failed to load registry CA certificates: %w", err)
85 }
86 if roots != nil {
87 opts = append(opts, proxy.WithUpstreamRootCAs(roots))
88 }
89 certOpts, err := pm.UpstreamProxyCertificateOptions(cfg.ClientCertPath, cfg.ClientKeyPath, cfg.ClientCertPassword)
90 if err != nil {
91 return err
92 }
93 opts = append(opts, certOpts...)
94 p, err := proxy.New(proxy.DefaultMatchers(), checker, cfg.ProjectID, opts...)
95 if err != nil {
96 return err
97 }
98 if err := p.Start(); err != nil {
99 return err
100 }
101 defer p.Stop() //nolint:contextcheck // Stop must finish shutdown and tunnel drain on its own timeout even when ctx is already cancelled.
103 if err := writeCAPEM(filepath.Join(cfg.StateDir, CACertFile), p.CACertificate()); err != nil {
104 return err
105 }
106 if err := os.WriteFile(filepath.Join(cfg.StateDir, PIDFile), []byte(strconv.Itoa(os.Getpid())), 0o644); err != nil {
107 return err
108 }
109 // Written last: `job-proxy start` waits for this file, so the proxy is listening
110 // and its CA is on disk by the time the address appears.
111 if err := os.WriteFile(filepath.Join(cfg.StateDir, AddrFile), []byte(p.Addr()), 0o644); err != nil {
112 return err
113 }
114 cfg.IO.LogErrorf("dependency firewall: job-level proxy ready at %s\n", p.Addr())
116 flush := func() { writeVerdicts(cfg.BaseDir, p.Verdicts(), cfg.IO.LogErrorf) }
118 sigs := make(chan os.Signal, 1)
119 signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT)
120 defer signal.Stop(sigs)
121 tick := time.NewTicker(flushInterval)
122 defer tick.Stop()
123 for {
124 select {
125 case <-ctx.Done():
126 flush()
127 return nil
128 case <-sigs:
129 flush()
130 return nil
131 case <-tick.C:
132 flush()
133 }
134 }
135}
137// writeVerdicts merges verdicts into the CI log under the sidecar lock, the same
138// way a package-manager run writes it, so a later `ci-summary` reads the full
139// set. cilog dedupes by Entry.Key, so re-writing the full snapshot each tick is
140// idempotent. A failed write is logged: ci-summary can't fail the job on a
141// verdict that never reached the log, so the job log is the only trace of it.
142func writeVerdicts(baseDir string, verdicts []verdict.Entry, logf func(string, ...any)) {
143 if len(verdicts) == 0 {
144 return
145 }
146 err := fsx.WithLock(cilog.LockPath(baseDir), func() error {
147 log, err := cilog.Load(baseDir)
148 if err != nil {
149 // A present-but-corrupt log: leave it for ci-summary to fail on,
150 // rather than overwriting an earlier run's blocked entries.
151 return err
152 }
153 if log.Session.Command == "" {
154 log.Session = cilog.New("dependency-firewall job-proxy").Session
155 }
156 for _, e := range verdicts {
157 log.Append(e)
158 }
159 return cilog.Save(baseDir, log)
160 })
161 if err != nil {
162 logf("dependency firewall: could not write %d verdict(s) to the CI log in %s: %v\n", len(verdicts), baseDir, err)
163 }
164}
Diagnostics7 git calls · 224 ms
CallStatusSizeTime0224 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=44Open 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:0200361 B · 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=361 git.response.cache=HIT git.response.duration_ms=3 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:0200279 B · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=212 git.response.bytes=279 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=97ca01ef53be93b101ee1321413cdbf4a0c86c70 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=279 git.pkt.payload.max=252 git.sideband.band1.bytes=252 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=252 git.pack.trailer=bfb89af7a897fc72572aa6703a2786499c09a4f4Open pack map
fetch2002.4 KiB212 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2451 git.response.cache=MISS git.response.duration_ms=212 git.fetch.wants=3b07f2f121c90b23c55f7f0743177876cfc3ff20 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=2451 git.pkt.payload.max=2424 git.sideband.band1.bytes=2424 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
- a5e25f43fab7224d
- Request
- bd765f1e…
- 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