GitLabgitlab-org
cli
main
1package dfcmd
3import (
4 "cmp"
5 "context"
6 "errors"
7 "fmt"
8 "os"
9 "strings"
11 "github.com/MakeNowJust/heredoc/v2"
12 "github.com/spf13/cobra"
14 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
16 "gitlab.com/gitlab-org/cli/internal/cmdutils"
17 "gitlab.com/gitlab-org/cli/internal/config"
18 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/pm"
19 "gitlab.com/gitlab-org/cli/internal/iostreams"
20 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
21 "gitlab.com/gitlab-org/cli/internal/text"
22)
24// ProxySpec describes one package-manager wrapper command. The wrapper
25// forwards all args to the package manager binary through the Dependency
26// Firewall inspection proxy.
27type ProxySpec struct {
28 Manager func() pm.PackageManager
29 Use string
30 Short string
31 Long string
32 Example string
33 // Environment replaces the help for the outbound proxy variables, for a
34 // manager that doesn't reach registries through the firewall proxy.
35 Environment string
36}
38var proxyEnvironmentHelp = heredoc.Doc(`
39 HTTPS_PROXY: outbound proxy glab uses to reach package registries. Credentials in the URL are sent to the proxy.
40 HTTP_PROXY: outbound proxy for plain-HTTP registries. When it's unset, glab uses HTTPS_PROXY.
41 NO_PROXY: hosts glab reaches directly instead of through an outbound proxy.
42`)
44var httpsFirstProxyEnvironmentHelp = heredoc.Doc(`
45 HTTPS_PROXY: outbound proxy glab uses to reach package registries, plain-HTTP registries included. Credentials in the URL are sent to the proxy.
46 HTTP_PROXY: outbound proxy for plain-HTTP registries when HTTPS_PROXY is unset.
47 NO_PROXY: hosts glab reaches directly instead of through an outbound proxy.
48`)
50func environmentHelp(name string, manager pm.PackageManager) string {
51 help := proxyEnvironmentHelp
52 if pm.PlainHTTPUsesHTTPSProxy(manager) {
53 help = httpsFirstProxyEnvironmentHelp
54 }
55 vars := pm.OwnProxyVariables(manager)
56 files := pm.OwnProxyConfigFiles(manager)
57 var own string
58 switch {
59 case len(vars) == 1:
60 own = fmt.Sprintf("%s: proxy setting for %s. glab reads it first, so the outbound proxy can differ from HTTPS_PROXY and HTTP_PROXY.", vars[0], name)
61 case len(vars) > 1:
62 own = fmt.Sprintf("%s: proxy settings for %s. glab reads them first, so the outbound proxy can differ from HTTPS_PROXY and HTTP_PROXY.", strings.Join(vars, ", "), name)
63 case len(files) > 0:
64 own = fmt.Sprintf("glab reads the proxy settings for %s from your user and global config files first (%s), so the outbound proxy can differ from HTTPS_PROXY and HTTP_PROXY.", name, strings.Join(files, ", "))
65 default:
66 return help
67 }
68 if len(files) > 0 {
69 if len(vars) > 0 {
70 own += fmt.Sprintf(" glab also reads the same settings from your user and global config files (%s).", strings.Join(files, ", "))
71 }
72 own += " glab doesn't read proxy settings from files inside the project."
73 }
74 if note := pm.OwnProxyHelpNote(manager); note != "" {
75 own += " " + note
76 }
77 return help + own + "\n"
78}
80type options struct {
81 io *iostreams.IOStreams
82 executor cmdutils.Executor
83 baseRepo func() (host, projectID string, err error)
84 gitlabClient func() (*gitlab.Client, error)
85 config func() config.Config
86 manager func() pm.PackageManager
87 name string
89 args []string
90 baseDir string
91}
93// NewProxyCmd builds a df package-manager wrapper command from spec.
94func NewProxyCmd(f cmdutils.Factory, spec ProxySpec) *cobra.Command {
95 opts := &options{
96 io: f.IO(),
97 executor: f.Executor(),
98 baseRepo: func() (string, string, error) {
99 r, err := f.BaseRepo()
100 if err != nil {
101 return "", "", err
102 }
103 return r.RepoHost(), r.FullName(), nil
104 },
105 gitlabClient: f.GitLabClient,
106 config: f.Config,
107 manager: spec.Manager,
108 name: firstWord(spec.Use),
109 }
111 return &cobra.Command{
112 Use: spec.Use,
113 Short: spec.Short + " (BETA)",
114 Annotations: map[string]string{
115 mcpannotations.Exclude: "true",
116 "help:environment": cmp.Or(spec.Environment, environmentHelp(opts.name, spec.Manager())),
117 },
118 DisableFlagParsing: true,
119 Long: spec.Long + text.BetaString,
120 Example: spec.Example,
121 RunE: func(cmd *cobra.Command, args []string) error {
122 opts.args = args
123 if err := opts.complete(); err != nil {
124 return err
125 }
126 return opts.run(cmd.Context())
127 },
128 }
129}
131// firstWord returns the command name from a cobra Use string, e.g.
132// "npm <npm args>" -> "npm".
133func firstWord(use string) string {
134 name, _, _ := strings.Cut(use, " ")
135 return name
136}
138func (o *options) complete() error {
139 wd, err := os.Getwd()
140 if err != nil {
141 return err
142 }
143 o.baseDir = wd
144 return nil
145}
147func (o *options) run(ctx context.Context) error {
148 host, projectID, err := o.baseRepo()
149 if err != nil {
150 return cmdutils.WrapError(cmdutils.WithGitRequiredHint(o.executor, err), "failed to resolve GitLab project from the git remote.")
151 }
153 client, err := o.gitlabClient()
154 if err != nil {
155 return cmdutils.WrapError(err, "failed to create a GitLab API client.")
156 }
158 caCert, err := o.config().Get(host, "ca_cert")
159 if err != nil {
160 return cmdutils.WrapError(err, "failed to read ca_cert from the glab configuration.")
161 }
162 clientCert, err := o.config().Get(host, "client_cert")
163 if err != nil {
164 return cmdutils.WrapError(err, "failed to read client_cert from the glab configuration.")
165 }
166 clientKey, err := o.config().Get(host, "client_key")
167 if err != nil {
168 return cmdutils.WrapError(err, "failed to read client_key from the glab configuration.")
169 }
171 if err := pm.Run(ctx, o.manager(), pm.RunOptions{
172 IO: o.io,
173 Executor: o.executor,
174 BaseDir: o.baseDir,
175 Client: client,
176 ProjectID: projectID,
177 Args: o.args,
178 CACertPath: caCert,
179 ClientCertPath: clientCert,
180 ClientKeyPath: clientKey,
181 ClientCertPassword: func() (string, error) {
182 password, err := o.config().Get(host, "client_cert_password")
183 if err != nil {
184 return "", fmt.Errorf("failed to read client_cert_password for %q: %w", host, err)
185 }
186 return password, nil
187 },
188 }); err != nil {
189 if ee, ok := errors.AsType[*pm.ExitError](err); ok {
190 if ee.ChildExited {
191 // The child actually ran, so the Dependency Firewall summary
192 // already tells the user what happened (including any blocked
193 // packages that made it exit non-zero). Wrap SilentError to
194 // preserve the exit code without letting fang render a
195 // duplicate ERROR block after the summary.
196 return cmdutils.WrapErrorWithCode(cmdutils.SilentError, ee.Code, ee.Error())
197 }
198 // The child never ran (e.g. fork/exec failure), so no summary was
199 // rendered. Surface a visible error instead of exiting silently.
200 return cmdutils.WrapErrorWithCode(ee.Unwrap(), ee.Code, ee.Error())
201 }
202 return cmdutils.WrapError(err, fmt.Sprintf("failed to run %s through the Dependency Firewall.", o.name))
203 }
204 return nil
205}
Diagnostics8 git calls · 253 ms
CallStatusSizeTime0253 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 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=2 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
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=5 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:0 · deepen 1020032 KiB · hit5 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=5 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 · 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=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200650 B · hit6 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=6 git.response.bytes=650 git.response.cache=HIT git.response.duration_ms=6 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=aabedefb11d4348efc4e1168413c6ec52cbf90d2Open pack map
fetchtree:0200137 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=137 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=3947fb0e8a44818807ba9856d1845e66c13cb330 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=137 git.pkt.payload.max=110 git.sideband.band1.bytes=110 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=110 git.pack.trailer=aebb7cefb2477189231b1c8f14034a2fd1f41ce4Open pack map
fetch2002.4 KiB233 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=233 git.response.bytes=2409 git.response.cache=MISS git.response.duration_ms=233 git.fetch.wants=8f9f9e59d17a96934c9279e09e4908f11c2a6f7e 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=2409 git.pkt.payload.max=2382 git.sideband.band1.bytes=2382 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
- a5e260da5c50224d
- Request
- 2bdf03cf…
- 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