GitLabgitlab-org

cli

main
ply9 git calls309 ms101 KiB6 cachedprotocol v2
185 lines6.6 KiBRaw
1package start
3import (
4 "context"
5 "errors"
6 "fmt"
7 "io/fs"
8 "os"
9 "os/exec"
10 "path/filepath"
11 "strings"
12 "time"
14 "github.com/MakeNowJust/heredoc/v2"
15 "github.com/spf13/cobra"
17 "gitlab.com/gitlab-org/cli/internal/cmdutils"
18 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/cilog"
19 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/session"
20 "gitlab.com/gitlab-org/cli/internal/iostreams"
21 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
24// readyTimeout bounds how long start waits for the daemon to listen.
25const readyTimeout = 20 * time.Second
27type options struct {
28 io *iostreams.IOStreams
31func NewCmd(f cmdutils.Factory) *cobra.Command {
32 opts := &options{io: f.IO()}
33 cmd := &cobra.Command{
34 Use: "start",
35 Short: "Start the job-level proxy and print shell exports to route the job through it.",
36 Long: heredoc.Docf(`
37 Start the job-level Dependency Firewall proxy as a background process and print %[1]sexport%[1]s lines that route the current shell through it. Evaluate the output so the exports apply to the rest of the job.
39 If the proxy cannot start, the command prints %[1]sfalse%[1]s, so the %[1]seval%[1]s fails and a CI/CD job (which runs with %[1]sset -e%[1]s) stops instead of running without the firewall.
40 `, "`"),
41 Example: heredoc.Doc(`
42 # In before_script: route the rest of the CI/CD job through the proxy
43 eval "$(glab dependency-firewall job-proxy start)"
45 # As the last command in script: fail the job if a package was blocked
46 glab dependency-firewall ci-summary
47 `),
48 Args: cobra.NoArgs,
49 ValidArgsFunction: cobra.NoFileCompletions,
50 Annotations: map[string]string{
51 mcpannotations.Exclude: "true",
52 "help:environment": session.StateDirEnv + ": directory for the proxy's runtime state (address, pid, CA). Set it to pin a known location; otherwise start creates a fresh per-user directory and exports this variable so `job-proxy stop` finds the same proxy.\n",
53 },
54 RunE: func(cmd *cobra.Command, args []string) error {
55 return opts.run(cmd.Context())
56 },
57 }
58 return cmd
61// run starts the proxy. Its output is eval'd, so on failure it prints `false`:
62// empty output would make the eval succeed and leave the job running green
63// with no firewall.
64func (o *options) run(ctx context.Context) error {
65 if err := o.start(ctx); err != nil {
66 o.io.LogInfo("false")
67 return err
68 }
69 return nil
72func (o *options) start(ctx context.Context) error {
73 stateDir, err := session.NewStateDir()
74 if err != nil {
75 return fmt.Errorf("creating the Dependency Firewall proxy state directory: %w", err)
76 }
77 // Clear an earlier run's state: start waits for the address file, so a stale
78 // one would hand the job a dead proxy and a CA the new daemon doesn't use.
79 for _, name := range []string{session.AddrFile, session.PIDFile, session.CACertFile} {
80 if err := os.Remove(filepath.Join(stateDir, name)); err != nil && !errors.Is(err, fs.ErrNotExist) {
81 return fmt.Errorf("clearing stale Dependency Firewall proxy state: %w", err)
82 }
83 }
85 exe, err := os.Executable()
86 if err != nil {
87 return err
88 }
89 logPath := filepath.Join(stateDir, "daemon.log")
90 logFile, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o644)
91 if err != nil {
92 return err
93 }
94 defer logFile.Close()
96 daemon := exec.Command(exe, "dependency-firewall", "job-proxy", "serve", "--state-dir", stateDir)
97 daemon.Stdout = logFile
98 daemon.Stderr = logFile
99 // The daemon keeps the caller's original HTTP(S)_PROXY/NO_PROXY so its own
100 // upstream fetches chain to a corporate egress proxy; the exports printed
101 // below only repoint the job, not the daemon.
102 daemon.Env = os.Environ()
103 if err := detach(daemon); err != nil {
104 return err
105 }
106 if err := daemon.Start(); err != nil {
107 return fmt.Errorf("starting the Dependency Firewall proxy daemon: %w", err)
108 }
109 _ = daemon.Process.Release()
111 addr, err := waitForFile(ctx, filepath.Join(stateDir, session.AddrFile), readyTimeout)
112 if err != nil {
113 return fmt.Errorf("the Dependency Firewall proxy did not become ready: %w\n--- daemon log ---\n%s", err, tail(logPath))
114 }
115 proxyURL := "http://" + strings.TrimSpace(addr)
116 caPath := filepath.Join(stateDir, session.CACertFile)
118 // Trust, additive: install into the system store (covers curl, git, apt),
119 // and write a merged bundle (system roots + the caller's bundles + the proxy
120 // CA) for tools that read a bundle instead of the system store.
121 session.InstallSystemTrust(stateDir, caPath, o.io.LogErrorf)
122 mergedPath, err := session.WriteMergedBundle(stateDir, caPath, session.CallerBundlePaths(os.Environ()), o.io.LogErrorf)
123 if err != nil {
124 return fmt.Errorf("writing merged CA bundle: %w", err)
125 }
127 // Never route traffic to the GitLab instance itself through the firewall
128 // proxy: the job's git fetches and API calls must reach GitLab directly, not
129 // be MITM'd. CI_SERVER_HOST is exempted on top of the caller's NO_PROXY.
130 noProxy := os.Getenv("NO_PROXY") + "," + os.Getenv("no_proxy")
131 if host := os.Getenv("CI_SERVER_HOST"); host != "" {
132 noProxy = noProxy + "," + host
133 }
134 env := session.BuildJobEnv(proxyURL, mergedPath, noProxy)
135 // Maven/Gradle need a generated settings.xml and JVM trust store rather than
136 // the env-var routing/trust the other managers use; best-effort, so a failure
137 // here never blocks the rest of the job's coverage.
138 jvm, jvmErr := session.JVMTrustEnv(stateDir, proxyURL, caPath, o.io.LogErrorf)
139 if jvmErr != nil {
140 o.io.LogErrorf("dependency firewall: Java (Maven/Gradle) not fully routed through the proxy: %v\n", jvmErr)
141 }
142 env = append(env, jvm...)
143 env = append(env, session.StateDirEnv+"="+stateDir)
144 logDir, err := session.LogBaseDir()
145 if err != nil {
146 return err
147 }
148 env = append(env, cilog.DirEnv+"="+logDir)
150 o.io.LogInfof("%s", session.ShellExports(env))
151 return nil
154// waitForFile polls path until it exists and is non-empty, returning its
155// contents, or errors on timeout or context cancellation.
156func waitForFile(ctx context.Context, path string, timeout time.Duration) (string, error) {
157 deadline := time.Now().Add(timeout)
158 for {
159 if b, err := os.ReadFile(path); err == nil && strings.TrimSpace(string(b)) != "" {
160 return string(b), nil
161 }
162 if time.Now().After(deadline) {
163 return "", fmt.Errorf("timed out after %s waiting for %s", timeout, path)
164 }
165 select {
166 case <-ctx.Done():
167 return "", ctx.Err()
168 case <-time.After(100 * time.Millisecond):
169 }
170 }
173// tail returns roughly the last 2 KiB of the file at path, for surfacing a
174// failed daemon's log when startup times out.
175func tail(path string) string {
176 b, err := os.ReadFile(path)
177 if err != nil {
178 return ""
179 }
180 const maxBytes = 2048
181 if len(b) > maxBytes {
182 b = b[len(b)-maxBytes:]
183 }
184 return string(b)
Diagnostics9 git calls · 309 ms
CallStatusSizeTime0309 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 KiB3 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=UPDATING
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
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=3
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=44
Open 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: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=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=2
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:0200195 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=195
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=b26ad1838d5161708f3c9df74fdaebb3c5546fcb
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=195
git.pkt.payload.max=168
git.sideband.band1.bytes=168
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=168
git.pack.trailer=405c9835aff5fd29fec3c746d859efdbbac4687f
Open pack map
fetchtree:0200206 B · hit4 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=206
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=7417b8c3a84c431500e333445f597eeb9f0463d9
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=206
git.pkt.payload.max=179
git.sideband.band1.bytes=179
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=179
git.pack.trailer=6d19f11275df986a3ee15af7ae7849f80825a95c
Open pack map
fetch2002.8 KiB291 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=291
git.response.bytes=2864
git.response.cache=MISS
git.response.duration_ms=291
git.fetch.wants=77498d2dddc882eba015a7989f9f41e5f2ad2535
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=2864
git.pkt.payload.max=2837
git.sideband.band1.bytes=2837
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
a5e2d6c97dd8689d
Request
1990b208…
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