//go:build !integration package dfcmd import ( "context" "crypto/ecdsa" "crypto/elliptic" "crypto/rand" "crypto/tls" "crypto/x509" "crypto/x509/pkix" "encoding/pem" "errors" "fmt" "io" "math/big" "net/http" "net/http/httptest" "net/url" "os" "os/exec" "path/filepath" "strings" "testing" "time" "github.com/MakeNowJust/heredoc/v2" "github.com/spf13/cobra" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "software.sslmate.com/src/go-pkcs12" "gitlab.com/gitlab-org/cli/internal/cmdutils" "gitlab.com/gitlab-org/cli/internal/config" "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/pm" "gitlab.com/gitlab-org/cli/internal/testing/cmdtest" ) type fakeExecutor struct { gotName string gotArgs []string gotEnv []string exitErr error } func (f *fakeExecutor) LookPath(file string) (string, error) { return "/usr/bin/" + file, nil } func (f *fakeExecutor) Exec(ctx context.Context, name string, args, env []string) error { return nil } func (f *fakeExecutor) ExecWithCombinedOutput(ctx context.Context, name string, args, env []string) ([]byte, error) { return nil, nil } func (f *fakeExecutor) ExecWithIO(ctx context.Context, name string, args, env []string, stdin io.Reader, stdout, stderr io.Writer) error { f.gotName = name f.gotArgs = args f.gotEnv = env return f.exitErr } var _ cmdutils.Executor = (*fakeExecutor)(nil) func envValue(env []string, prefix string) string { for _, e := range env { if strings.HasPrefix(e, prefix) { return e } } return "" } func testSpec() ProxySpec { return ProxySpec{ Manager: pm.NPM, Use: "npm ", Short: "Run npm through the GitLab Dependency Firewall.", Long: heredoc.Doc("Run npm behind the firewall."), Example: heredoc.Doc("glab dependency-firewall npm ..."), } } func newTestCmd(f cmdutils.Factory) *cobra.Command { return NewProxyCmd(f, testSpec()) } func TestEnvironmentHelp(t *testing.T) { t.Parallel() t.Run("when the spec sets no environment help", func(t *testing.T) { t.Parallel() spec := testSpec() spec.Manager, spec.Use = pm.Pip, "pip " cmd := NewProxyCmd(cmdtest.NewTestFactory(nil), spec) assert.Contains(t, cmd.Annotations["help:environment"], "HTTP_PROXY: outbound proxy for plain-HTTP registries. When it's unset, glab uses HTTPS_PROXY.") }) t.Run("when the manager sends plain HTTP through HTTPS_PROXY", func(t *testing.T) { t.Parallel() help := newTestCmd(cmdtest.NewTestFactory(nil)).Annotations["help:environment"] assert.Contains(t, help, "HTTP_PROXY: outbound proxy for plain-HTTP registries when HTTPS_PROXY is unset.") assert.NotContains(t, help, "When it's unset, glab uses HTTPS_PROXY.") }) t.Run("when the manager reads several proxy variables of its own", func(t *testing.T) { t.Parallel() cmd := newTestCmd(cmdtest.NewTestFactory(nil)) assert.Contains(t, cmd.Annotations["help:environment"], "npm_config_https_proxy, npm_config_proxy: proxy settings for npm. glab reads them first, so the outbound proxy can differ from HTTPS_PROXY and HTTP_PROXY. glab also reads the same settings from your user and global config files (~/.npmrc, $PREFIX/etc/npmrc). glab doesn't read proxy settings from files inside the project.\n") }) t.Run("when the manager reads one proxy variable of its own", func(t *testing.T) { t.Parallel() spec := testSpec() spec.Manager, spec.Use = pm.Pip, "pip " cmd := NewProxyCmd(cmdtest.NewTestFactory(nil), spec) assert.Contains(t, cmd.Annotations["help:environment"], "PIP_PROXY: proxy setting for pip. glab reads it first, so the outbound proxy can differ from HTTPS_PROXY and HTTP_PROXY. glab also reads the same settings from your user and global config files (pip.conf, $PIP_CONFIG_FILE). glab doesn't read proxy settings from files inside the project. glab reads pip's --proxy option ahead of all of these.\n") }) t.Run("when only some versions of the manager read a setting", func(t *testing.T) { t.Parallel() spec := testSpec() spec.Manager, spec.Use = pm.Yarn, "yarn " cmd := NewProxyCmd(cmdtest.NewTestFactory(nil), spec) assert.Contains(t, cmd.Annotations["help:environment"], "glab doesn't read proxy settings from files inside the project. For Yarn 2 and later, glab reads only YARN_HTTPS_PROXY and ~/.yarnrc.yml.\n") }) t.Run("when the manager is pipenv, which has no --proxy to take", func(t *testing.T) { t.Parallel() spec := testSpec() spec.Manager, spec.Use = pm.Pipenv, "pipenv " cmd := NewProxyCmd(cmdtest.NewTestFactory(nil), spec) assert.NotContains(t, cmd.Annotations["help:environment"], "--proxy") }) t.Run("when the manager reads no config file of its own", func(t *testing.T) { t.Parallel() spec := testSpec() spec.Manager, spec.Use = pm.Pnpm, "pnpm " cmd := NewProxyCmd(cmdtest.NewTestFactory(nil), spec) assert.Contains(t, cmd.Annotations["help:environment"], "npm_config_https_proxy, npm_config_proxy: proxy settings for pnpm. glab reads them first, so the outbound proxy can differ from HTTPS_PROXY and HTTP_PROXY.\n") }) t.Run("when the manager reads config files but no proxy variable of its own", func(t *testing.T) { t.Parallel() spec := testSpec() spec.Manager, spec.Use = pm.Maven, "maven " cmd := NewProxyCmd(cmdtest.NewTestFactory(nil), spec) assert.Contains(t, cmd.Annotations["help:environment"], "NO_PROXY: hosts glab reaches directly instead of through an outbound proxy.\nglab reads the proxy settings for maven from your user and global config files first (~/.m2/settings.xml, ${maven.home}/conf/settings.xml), so the outbound proxy can differ from HTTPS_PROXY and HTTP_PROXY. glab doesn't read proxy settings from files inside the project. glab doesn't support proxy passwords encrypted with settings-security.xml.\n") }) t.Run("when the manager reads no proxy variable of its own", func(t *testing.T) { t.Parallel() spec := testSpec() spec.Manager, spec.Use = pm.Poetry, "poetry " cmd := NewProxyCmd(cmdtest.NewTestFactory(nil), spec) assert.NotContains(t, cmd.Annotations["help:environment"], "reads it first") assert.NotContains(t, cmd.Annotations["help:environment"], "reads them first") }) t.Run("when the spec sets its own environment help", func(t *testing.T) { t.Parallel() spec := testSpec() spec.Environment = "GOPROXY: module proxy.\n" cmd := NewProxyCmd(cmdtest.NewTestFactory(nil), spec) assert.Equal(t, "GOPROXY: module proxy.\n", cmd.Annotations["help:environment"]) }) } func TestForwardsArgsAndRoutesThroughProxy(t *testing.T) { t.Chdir(t.TempDir()) fe := &fakeExecutor{} run := cmdtest.SetupCmdForTest( t, newTestCmd, false, cmdtest.WithExecutor(fe), cmdtest.WithBaseRepo("g", "p", "gitlab.com"), ) _, err := run("install --save-dev left-pad") require.NoError(t, err) assert.Equal(t, "/usr/bin/npm", fe.gotName) assert.Equal(t, []string{"install", "--save-dev", "left-pad"}, fe.gotArgs) v := envValue(fe.gotEnv, "HTTPS_PROXY=") require.NotEmpty(t, v, "child env must set HTTPS_PROXY") assert.Contains(t, v, "127.0.0.1", "HTTPS_PROXY must route through the local inspection proxy, got %q", v) } func TestExitErrorPropagates(t *testing.T) { t.Chdir(t.TempDir()) badCmd := exec.Command("sh", "-c", "exit 3") realErr := badCmd.Run() var ee *exec.ExitError require.ErrorAs(t, realErr, &ee) fe := &fakeExecutor{exitErr: realErr} run := cmdtest.SetupCmdForTest( t, newTestCmd, false, cmdtest.WithExecutor(fe), cmdtest.WithBaseRepo("g", "p", "gitlab.com"), ) _, err := run("install --save-dev left-pad") require.Error(t, err) var cmdErr *cmdutils.ExitError require.ErrorAs(t, err, &cmdErr) assert.Equal(t, 3, cmdErr.Code) // The Dependency Firewall summary already explains the failure, so the // wrapper must not also trigger fang's ERROR block. Wrapping SilentError // preserves the exit code while suppressing that duplicate output. assert.ErrorIs(t, err, cmdutils.SilentError, "wrapper failure must be silent so no ERROR block prints after the summary") } // TestChildNeverRanErrorIsNotSilenced covers the path where the package // manager never actually executed (e.g. fork/exec failure). No summary box is // rendered in that case, so the error must stay visible — silencing it would // exit non-zero with nothing printed. func TestChildNeverRanErrorIsNotSilenced(t *testing.T) { t.Chdir(t.TempDir()) // A plain error (not an *exec.ExitError) models the child never running. fe := &fakeExecutor{exitErr: errors.New("fork/exec /usr/bin/npm: permission denied")} run := cmdtest.SetupCmdForTest( t, newTestCmd, false, cmdtest.WithExecutor(fe), cmdtest.WithBaseRepo("g", "p", "gitlab.com"), ) _, err := run("install --save-dev left-pad") require.Error(t, err) var cmdErr *cmdutils.ExitError require.ErrorAs(t, err, &cmdErr) assert.Equal(t, 1, cmdErr.Code, "a generic run failure maps to exit code 1") assert.NotErrorIs(t, err, cmdutils.SilentError, "a child that never ran must surface a visible error, not be silenced") } // fetchingExecutor GETs url through the inspection proxy the child is handed, // so the test observes whether the proxy trusted the registry. The proxy's // leaf for an IP authority carries a DNS SAN only, so this child skips // hostname verification on its own hop to the proxy. type fetchingExecutor struct { fakeExecutor url string status int } func (f *fetchingExecutor) ExecWithIO(ctx context.Context, _ string, _ []string, env []string, _ io.Reader, _, _ io.Writer) error { proxyURL, err := url.Parse(strings.TrimPrefix(envValue(env, "HTTPS_PROXY="), "HTTPS_PROXY=")) if err != nil { return err } client := &http.Client{Transport: &http.Transport{ Proxy: http.ProxyURL(proxyURL), TLSClientConfig: &tls.Config{MinVersion: tls.VersionTLS12, InsecureSkipVerify: true}, //nolint:gosec // child -> inspection proxy hop only }} req, err := http.NewRequestWithContext(ctx, http.MethodGet, f.url, http.NoBody) if err != nil { return err } resp, err := client.Do(req) if err != nil { return err } defer resp.Body.Close() _, _ = io.Copy(io.Discard, resp.Body) f.status = resp.StatusCode return nil } func TestRegistryTrustsCACertForRepoHost(t *testing.T) { t.Chdir(t.TempDir()) t.Setenv("GITLAB_CI", "") t.Setenv("GLAB_DF_FAKE_BLOCK", "npm:left-pad@1.3.0") upstream := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { _, _ = io.WriteString(w, "ok") })) t.Cleanup(upstream.Close) caPath := filepath.Join(t.TempDir(), "ca.pem") require.NoError(t, os.WriteFile(caPath, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: upstream.Certificate().Raw}), 0o600)) cfg := config.NewFromString(heredoc.Docf(` hosts: gitlab.example.com: ca_cert: %s `, caPath)) fe := &fetchingExecutor{url: upstream.URL + "/ping"} run := cmdtest.SetupCmdForTest( t, newTestCmd, false, cmdtest.WithExecutor(fe), cmdtest.WithConfig(cfg), cmdtest.WithBaseRepo("g", "p", "gitlab.example.com"), ) _, err := run("install") require.NoError(t, err) assert.Equal(t, http.StatusOK, fe.status, "the inspection proxy must trust the registry via ca_cert") } func TestRunFailsWhenClientCertForRepoHostCannotBeLoaded(t *testing.T) { t.Chdir(t.TempDir()) t.Setenv("GITLAB_CI", "") dir := t.TempDir() certPath := filepath.Join(dir, "client.pem") keyPath := filepath.Join(dir, "client-key.pem") require.NoError(t, os.WriteFile(certPath, []byte("not a certificate"), 0o600)) require.NoError(t, os.WriteFile(keyPath, []byte("not a key"), 0o600)) cfg := config.NewFromString(heredoc.Docf(` hosts: gitlab.example.com: client_cert: %s client_key: %s `, certPath, keyPath)) fe := &fakeExecutor{} run := cmdtest.SetupCmdForTest( t, newTestCmd, false, cmdtest.WithExecutor(fe), cmdtest.WithConfig(cfg), cmdtest.WithBaseRepo("g", "p", "gitlab.example.com"), ) _, err := run("install") require.ErrorContains(t, err, "failed to load client certificate") assert.Empty(t, fe.gotName, "npm must not run without the outbound proxy client certificate") } func newClientIdentity(t *testing.T) (*ecdsa.PrivateKey, *x509.Certificate) { t.Helper() key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) require.NoError(t, err) template := &x509.Certificate{ SerialNumber: big.NewInt(1), Subject: pkix.Name{CommonName: "glab client"}, NotBefore: time.Now().Add(-time.Hour), NotAfter: time.Now().Add(time.Hour), ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}, } der, err := x509.CreateCertificate(rand.Reader, template, template, key.Public(), key) require.NoError(t, err) cert, err := x509.ParseCertificate(der) require.NoError(t, err) return key, cert } func runInstallWithHostConfig(t *testing.T, hostConfig string) (*fakeExecutor, error) { t.Helper() t.Chdir(t.TempDir()) t.Setenv("GITLAB_CI", "") cfg := config.NewFromString("hosts:\n gitlab.example.com:\n" + hostConfig) fe := &fakeExecutor{} run := cmdtest.SetupCmdForTest( t, newTestCmd, false, cmdtest.WithExecutor(fe), cmdtest.WithConfig(cfg), cmdtest.WithBaseRepo("g", "p", "gitlab.example.com"), ) _, err := run("install") return fe, err } func TestRunAcceptsDERClientCertForRepoHost(t *testing.T) { key, cert := newClientIdentity(t) keyDER, err := x509.MarshalPKCS8PrivateKey(key) require.NoError(t, err) dir := t.TempDir() certPath := filepath.Join(dir, "client.der") keyPath := filepath.Join(dir, "client-key.der") require.NoError(t, os.WriteFile(certPath, cert.Raw, 0o600)) require.NoError(t, os.WriteFile(keyPath, keyDER, 0o600)) fe, err := runInstallWithHostConfig(t, fmt.Sprintf(" client_cert: %s\n client_key: %s\n", certPath, keyPath)) require.NoError(t, err) assert.Equal(t, "/usr/bin/npm", fe.gotName, "a DER pair that the API client accepts must not stop the run") } func TestRunLoadsPKCS12ClientCertForRepoHost(t *testing.T) { key, cert := newClientIdentity(t) bundle, err := pkcs12.Modern.Encode(key, cert, nil, "secret") require.NoError(t, err) bundlePath := filepath.Join(t.TempDir(), "client.p12") require.NoError(t, os.WriteFile(bundlePath, bundle, 0o600)) t.Run("correct password", func(t *testing.T) { fe, err := runInstallWithHostConfig(t, fmt.Sprintf(" client_cert: %s\n client_cert_password: secret\n", bundlePath)) require.NoError(t, err) assert.Equal(t, "/usr/bin/npm", fe.gotName) }) t.Run("wrong password", func(t *testing.T) { fe, err := runInstallWithHostConfig(t, fmt.Sprintf(" client_cert: %s\n client_cert_password: wrong\n", bundlePath)) require.ErrorContains(t, err, "incorrect password for the PKCS#12 client certificate") assert.Empty(t, fe.gotName, "npm must not run when the PKCS#12 client certificate cannot be opened") }) } func TestRunIgnoresClientCertWithoutKeyThatIsNotPKCS12(t *testing.T) { _, cert := newClientIdentity(t) certPath := filepath.Join(t.TempDir(), "client.pem") require.NoError(t, os.WriteFile(certPath, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw}), 0o600)) fe, err := runInstallWithHostConfig(t, fmt.Sprintf(" client_cert: %s\n", certPath)) require.NoError(t, err) assert.Equal(t, "/usr/bin/npm", fe.gotName, "a lone client_cert is ignored, as the API client ignores it") } type noGitExecutor struct{ fakeExecutor } func (e *noGitExecutor) LookPath(file string) (string, error) { return "", exec.ErrNotFound } func TestBaseRepoErrorSaysGitIsRequired(t *testing.T) { t.Chdir(t.TempDir()) fe := &noGitExecutor{} run := cmdtest.SetupCmdForTest(t, newTestCmd, false, cmdtest.WithExecutor(fe), cmdtest.WithBaseRepoError(errors.New("no GitLab remote found"))) _, err := run("ci") require.EqualError(t, err, "no GitLab remote found. git is required") assert.Empty(t, fe.gotName) } func TestBaseRepoErrorWithGitInstalledHasNoGitHint(t *testing.T) { t.Chdir(t.TempDir()) run := cmdtest.SetupCmdForTest(t, newTestCmd, false, cmdtest.WithExecutor(&fakeExecutor{}), cmdtest.WithBaseRepoError(errors.New("no GitLab remote found"))) _, err := run("ci") require.Error(t, err) assert.NotContains(t, err.Error(), "git is required") }