diff --git a/cmd/crossplane/dependency/add.go b/cmd/crossplane/dependency/add.go index 4b1d9fcb..b534250e 100644 --- a/cmd/crossplane/dependency/add.go +++ b/cmd/crossplane/dependency/add.go @@ -49,6 +49,8 @@ type addCmd struct { ProjectFile string `default:"${project_file}" help:"Path to project definition file." short:"f"` CacheDir string `env:"CROSSPLANE_XPKG_CACHE" help:"Directory for cached xpkg package contents." name:"cache-dir"` GitToken string `env:"CROSSPLANE_GIT_TOKEN" help:"Token for git HTTPS authentication."` + GitProxy string `env:"CROSSPLANE_GIT_PROXY" help:"Proxy URL for git operations (e.g. http://proxy:8080). Supports HTTP CONNECT for SSH tunneling."` + GitInsecureHostKey bool `env:"CROSSPLANE_GIT_INSECURE_HOST_KEY" help:"Skip SSH host key verification for git operations. Only use if you understand the MITM risks."` GitUsername string `default:"x-access-token" env:"CROSSPLANE_GIT_USERNAME" help:"Username for git HTTPS authentication."` // Flags for specific dependency types. @@ -100,6 +102,12 @@ func (c *addCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter, cfg *con Password: c.GitToken, })) } + if c.GitProxy != "" { + opts = append(opts, dependency.WithGitProxyURL(c.GitProxy)) + } + if c.GitInsecureHostKey { + opts = append(opts, dependency.WithGitInsecureHostKey(true)) + } m := dependency.NewManager(proj, projFS, opts...) diff --git a/cmd/crossplane/dependency/cache.go b/cmd/crossplane/dependency/cache.go index c31b09f3..8a89bfcd 100644 --- a/cmd/crossplane/dependency/cache.go +++ b/cmd/crossplane/dependency/cache.go @@ -48,6 +48,8 @@ type updateCacheCmd struct { ProjectFile string `default:"${project_file}" help:"Path to project definition file." short:"f"` CacheDir string `env:"CROSSPLANE_XPKG_CACHE" help:"Directory for cached xpkg package contents." name:"cache-dir"` GitToken string `env:"CROSSPLANE_GIT_TOKEN" help:"Token for git HTTPS authentication."` + GitProxy string `env:"CROSSPLANE_GIT_PROXY" help:"Proxy URL for git operations (e.g. http://proxy:8080). Supports HTTP CONNECT for SSH tunneling."` + GitInsecureHostKey bool `env:"CROSSPLANE_GIT_INSECURE_HOST_KEY" help:"Skip SSH host key verification for git operations. Only use if you understand the MITM risks."` GitUsername string `default:"x-access-token" env:"CROSSPLANE_GIT_USERNAME" help:"Username for git HTTPS authentication."` } @@ -94,6 +96,12 @@ func (c *updateCacheCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter, Password: c.GitToken, })) } + if c.GitProxy != "" { + opts = append(opts, dependency.WithGitProxyURL(c.GitProxy)) + } + if c.GitInsecureHostKey { + opts = append(opts, dependency.WithGitInsecureHostKey(true)) + } m := dependency.NewManager(proj, projFS, opts...) diff --git a/cmd/crossplane/project/build.go b/cmd/crossplane/project/build.go index 8e24b277..b8e02798 100644 --- a/cmd/crossplane/project/build.go +++ b/cmd/crossplane/project/build.go @@ -58,6 +58,8 @@ type buildCmd struct { MaxConcurrency uint `default:"8" help:"Max concurrent function builds."` CacheDir string `env:"CROSSPLANE_XPKG_CACHE" help:"Directory for cached xpkg package contents." name:"cache-dir"` GitToken string `env:"CROSSPLANE_GIT_TOKEN" help:"Token for git HTTPS authentication."` + GitProxy string `env:"CROSSPLANE_GIT_PROXY" help:"Proxy URL for git operations (e.g. http://proxy:8080). Supports HTTP CONNECT for SSH tunneling."` + GitInsecureHostKey bool `env:"CROSSPLANE_GIT_INSECURE_HOST_KEY" help:"Skip SSH host key verification for git operations. Only use if you understand the MITM risks."` GitUsername string `default:"x-access-token" env:"CROSSPLANE_GIT_USERNAME" help:"Username for git HTTPS authentication."` proj *devv1alpha1.Project @@ -128,6 +130,12 @@ func (c *buildCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter, cfg *c Password: c.GitToken, })) } + if c.GitProxy != "" { + opts = append(opts, dependency.WithGitProxyURL(c.GitProxy)) + } + if c.GitInsecureHostKey { + opts = append(opts, dependency.WithGitInsecureHostKey(true)) + } depMgr := dependency.NewManager(c.proj, c.projFS, opts...) diff --git a/go.mod b/go.mod index c3c85a74..ded071bb 100644 --- a/go.mod +++ b/go.mod @@ -51,7 +51,9 @@ require ( github.com/shirou/gopsutil/v4 v4.26.10 github.com/spf13/afero v1.15.0 github.com/willabides/kongplete v0.4.0 + golang.org/x/crypto v0.58.0 golang.org/x/mod v0.42.0 + golang.org/x/net v0.62.0 golang.org/x/sync v0.24.0 golang.org/x/sys v0.49.0 golang.org/x/text v0.43.0 @@ -60,6 +62,7 @@ require ( google.golang.org/grpc v1.85.0 google.golang.org/protobuf v1.36.12 gopkg.in/yaml.v3 v3.0.1 + gotest.tools/v3 v3.5.2 helm.sh/helm/v3 v3.22.1 k8s.io/api v0.37.1 k8s.io/apiextensions-apiserver v0.37.1 @@ -316,9 +319,7 @@ require ( go.yaml.in/yaml/v2 v2.4.4 // indirect go.yaml.in/yaml/v3 v3.0.5 // indirect go.yaml.in/yaml/v4 v4.0.0-rc.6 // indirect - golang.org/x/crypto v0.58.0 // indirect golang.org/x/exp v0.0.0-20260824195058-e88cd73687aa // indirect - golang.org/x/net v0.62.0 // indirect golang.org/x/oauth2 v0.36.0 // indirect golang.org/x/term v0.47.0 // indirect golang.org/x/time v0.15.0 // indirect diff --git a/internal/dependency/manager.go b/internal/dependency/manager.go index 98764ce6..0940483e 100644 --- a/internal/dependency/manager.go +++ b/internal/dependency/manager.go @@ -54,8 +54,9 @@ type Manager struct { projFile string schemas *smanager.Manager - gitCloner git.Cloner - gitAuthProvider git.AuthProvider + gitCloner git.Cloner + gitAuthProvider git.AuthProvider + gitInsecureHostKey bool client clixpkg.Client @@ -71,12 +72,14 @@ type Manager struct { type ManagerOption func(*managerOptions) type managerOptions struct { - projFile string - schemaFS afero.Fs - schemaGenerators []generator.Interface - schemaRunner runner.SchemaRunner - gitAuthProvider git.AuthProvider - client clixpkg.Client + projFile string + schemaFS afero.Fs + schemaGenerators []generator.Interface + schemaRunner runner.SchemaRunner + gitAuthProvider git.AuthProvider + gitProxyURL string + gitInsecureHostKey bool + client clixpkg.Client } // WithProjectFile sets the path to the project file. @@ -114,6 +117,24 @@ func WithGitAuthProvider(p git.AuthProvider) ManagerOption { } } +// WithGitProxyURL tunnels git-based dependency clones via an HTTP CONNECT +// proxy. +func WithGitProxyURL(u string) ManagerOption { + return func(opts *managerOptions) { + opts.gitProxyURL = u + } +} + +// WithGitInsecureHostKey disables SSH host key verification for git-based +// dependencies over SSH. This makes connections vulnerable to +// man-in-the-middle attacks and should only be enabled when the user +// explicitly opts in. +func WithGitInsecureHostKey(insecure bool) ManagerOption { + return func(opts *managerOptions) { + opts.gitInsecureHostKey = insecure + } +} + // WithXpkgClient sets the xpkg client used to resolve version constraints and // to fetch and parse xpkg dependencies. func WithXpkgClient(c clixpkg.Client) ManagerOption { @@ -134,7 +155,6 @@ func NewManager(proj *v1alpha1.Project, projFS afero.Fs, opts ...ManagerOption) schemaRunner: runner.NewRealSchemaRunner( runner.WithImageConfig(proj.Spec.ImageConfigs), ), - gitAuthProvider: &git.HTTPSAuthProvider{}, } for _, opt := range opts { @@ -148,14 +168,15 @@ func NewManager(proj *v1alpha1.Project, projFS afero.Fs, opts ...ManagerOption) ) return &Manager{ - proj: proj, - projFS: projFS, - projFile: options.projFile, - schemas: schemas, - gitCloner: &git.DefaultCloner{}, - gitAuthProvider: options.gitAuthProvider, - client: options.client, - visited: make(map[string]struct{}), + proj: proj, + projFS: projFS, + projFile: options.projFile, + schemas: schemas, + gitCloner: &git.DefaultCloner{ProxyURL: options.gitProxyURL}, + gitAuthProvider: options.gitAuthProvider, + gitInsecureHostKey: options.gitInsecureHostKey, + client: options.client, + visited: make(map[string]struct{}), } } @@ -365,7 +386,7 @@ func (m *Manager) addDependencyNoWrite(ctx context.Context, dep *v1alpha1.Depend return m.addPackage(ctx, xpkgRef(dep.Xpkg.Package, dep.Xpkg.Version), refresh) case dep.Git != nil: - return nil, m.schemas.Add(ctx, smanager.NewGitSource(*dep, m.gitCloner, m.gitAuthProvider)) + return nil, m.schemas.Add(ctx, smanager.NewGitSource(*dep, m.gitCloner, m.gitProviderFor(dep.Git.Repository))) case dep.HTTP != nil: return nil, m.schemas.Add(ctx, smanager.NewHTTPSource(*dep)) case dep.K8s != nil: @@ -375,6 +396,22 @@ func (m *Manager) addDependencyNoWrite(ctx context.Context, dep *v1alpha1.Depend } } +// gitProviderFor returns the auth provider for a git dependency. When the +// caller hasn't supplied one, pick the right default for the transport: SSH +// agent for SSH URLs, anonymous HTTPS otherwise. +func (m *Manager) gitProviderFor(repo string) git.AuthProvider { + if m.gitAuthProvider != nil { + return m.gitAuthProvider + } + if git.IsSSHURL(repo) { + return &git.SSHAgentAuthProvider{ + Username: git.ExtractSSHUsername(repo), + InsecureIgnoreHostKey: m.gitInsecureHostKey, + } + } + return &git.HTTPSAuthProvider{} +} + // Clean removes all generated schemas. func (m *Manager) Clean() error { return m.projFS.RemoveAll(m.proj.Spec.Paths.Schemas) diff --git a/internal/git/git.go b/internal/git/git.go index eb89800b..74f27525 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -28,6 +28,7 @@ import ( "github.com/go-git/go-git/v5/plumbing/transport/http" "github.com/go-git/go-git/v5/plumbing/transport/ssh" "github.com/go-git/go-git/v5/storage" + cryptossh "golang.org/x/crypto/ssh" "github.com/crossplane/crossplane-runtime/v2/pkg/errors" ) @@ -38,6 +39,11 @@ type CloneOptions struct { RefName string Directory string Path string // Optional path for sparse checkout + + // ProxyURL, when set, tunnels the clone via an HTTP CONNECT proxy (e.g. + // http://proxy:8080). For SSH URLs the connection is tunneled through + // the proxy via CONNECT; see proxy.go. + ProxyURL string } // AuthProvider wraps a specific auth method. @@ -85,6 +91,10 @@ func (a *SSHAuthProvider) GetAuthMethod() (transport.AuthMethod, error) { // SSHAgentAuthProvider provides authentication using the SSH agent. type SSHAgentAuthProvider struct { Username string + // InsecureIgnoreHostKey disables SSH host key verification when true. + // This makes connections vulnerable to man-in-the-middle attacks and + // should only be enabled when the user explicitly opts in. + InsecureIgnoreHostKey bool } // GetAuthMethod returns the SSH agent auth method. @@ -93,7 +103,51 @@ func (a *SSHAgentAuthProvider) GetAuthMethod() (transport.AuthMethod, error) { if username == "" { username = "git" } - return ssh.NewSSHAgentAuth(username) + + auth, err := ssh.NewSSHAgentAuth(username) + if err != nil { + return nil, errors.Wrapf(err, "failed to create SSH agent auth for user %q", username) + } + + if a.InsecureIgnoreHostKey { + auth.HostKeyCallback = cryptossh.InsecureIgnoreHostKey() //nolint:gosec // G106: opt-in via an explicit flag. + } + + return auth, nil +} + +// IsSSHURL returns true if the given repository URL uses the SSH protocol. +// It recognizes both SCP-style URLs (user@host:path) and ssh:// scheme URLs. +// URLs with any other explicit scheme (http://, https://, git://, etc.) are +// not considered SSH, regardless of case. +func IsSSHURL(repo string) bool { + // If the URL has an explicit scheme (contains "://"), only ssh counts. + if before, _, ok := strings.Cut(repo, "://"); ok { + return strings.EqualFold(before, "ssh") + } + // SCP-style URLs have a user@host:path shape. + before, _, ok := strings.Cut(repo, ":") + return ok && strings.Contains(before, "@") +} + +// ExtractSSHUsername extracts the SSH username from a repository URL. +// For SCP-style URLs (user@host:path), it returns the part before "@". +// For ssh:// URLs (ssh://user@host/path), it returns the user component. +// Returns an empty string if no username is found or the URL is not SSH. +func ExtractSSHUsername(repo string) string { + if before, after, ok := strings.Cut(repo, "://"); ok { + if !strings.EqualFold(before, "ssh") { + return "" + } + if user, _, ok := strings.Cut(after, "@"); ok { + return user + } + return "" + } + if before, _, ok := strings.Cut(repo, "@"); ok { + return before + } + return "" } // CompositeAuthProvider tries multiple auth providers in order until one succeeds. @@ -122,8 +176,12 @@ type Cloner interface { CloneRepository(store storage.Storer, fs billy.Filesystem, auth AuthProvider, opts CloneOptions) (*plumbing.Reference, error) } -// DefaultCloner is the default implementation of Cloner. -type DefaultCloner struct{} +// DefaultCloner is the default implementation of Cloner. ProxyURL, when +// set, tunnels clones and fetches via an HTTP CONNECT proxy; it applies when +// the CloneOptions don't set their own proxy. +type DefaultCloner struct { + ProxyURL string +} // CheckSHA1Hash checks if a string is a valid git SHA hash (40 hex characters). func CheckSHA1Hash(ref string) bool { @@ -150,7 +208,7 @@ func extractBranchName(ref string) string { return "main" } -func handleSHACheckout(repoObj *git.Repository, authMethod transport.AuthMethod, sha string, sparsePath string) error { +func handleSHACheckout(repoObj *git.Repository, authMethod transport.AuthMethod, sha string, sparsePath string, proxyURL string) error { err := repoObj.Fetch(&git.FetchOptions{ Auth: authMethod, RefSpecs: []config.RefSpec{ @@ -185,6 +243,10 @@ func handleSHACheckout(repoObj *git.Repository, authMethod transport.AuthMethod, // CloneRepository clones a git repository using the provided CloneOptions and AuthProvider. func (dc *DefaultCloner) CloneRepository(store storage.Storer, fs billy.Filesystem, auth AuthProvider, opts CloneOptions) (*plumbing.Reference, error) { + if opts.ProxyURL == "" { + opts.ProxyURL = dc.ProxyURL + } + authMethod, err := auth.GetAuthMethod() if err != nil { return nil, errors.Wrap(err, "failed to get authentication method") @@ -203,6 +265,14 @@ func (dc *DefaultCloner) CloneRepository(store storage.Storer, fs billy.Filesyst Tags: git.NoTags, } + // For SSH URLs the proxy is used via HTTP CONNECT tunneling; the dialer + // registered in proxy.go handles this. + if opts.ProxyURL != "" { + cloneOptions.ProxyOptions = transport.ProxyOptions{ + URL: opts.ProxyURL, + } + } + if !isSHA { cloneOptions.ReferenceName = plumbing.ReferenceName(opts.RefName) cloneOptions.SingleBranch = !isTag @@ -214,7 +284,7 @@ func (dc *DefaultCloner) CloneRepository(store storage.Storer, fs billy.Filesyst } if isSHA { - if err := handleSHACheckout(repoObj, authMethod, refToCheck, opts.Path); err != nil { + if err := handleSHACheckout(repoObj, authMethod, refToCheck, opts.Path, opts.ProxyURL); err != nil { return nil, err } } diff --git a/internal/git/proxy.go b/internal/git/proxy.go new file mode 100644 index 00000000..8ba6f6f2 --- /dev/null +++ b/internal/git/proxy.go @@ -0,0 +1,190 @@ +/* +Copyright 2026 The Crossplane Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package git + +import ( + "bufio" + "context" + "crypto/tls" + "encoding/base64" + "io" + "net" + "net/http" + "net/url" + "sync" + + "golang.org/x/net/proxy" + + "github.com/crossplane/crossplane-runtime/v2/pkg/errors" +) + +// bufferedConn wraps a net.Conn with a buffered reader so that any data read +// during the CONNECT handshake (which may include bytes past the response +// headers) is not lost. Its Close is idempotent and also stops the +// context-cancellation watchdog started in DialContext. +type bufferedConn struct { + net.Conn + + r io.Reader + + closeOnce sync.Once + closeFn func() error +} + +func (b *bufferedConn) Read(p []byte) (int, error) { + return b.r.Read(p) +} + +func (b *bufferedConn) Close() error { + var err error + b.closeOnce.Do(func() { + err = b.closeFn() + }) + return err +} + +// httpConnectDialer implements proxy.ContextDialer for HTTP CONNECT proxies. +// This allows SSH connections to be tunneled through HTTP proxies, +// similar to using ssh -o ProxyCommand='nc -X connect -x proxy:port %h %p'. +type httpConnectDialer struct { + proxyURL *url.URL + forward proxy.Dialer +} + +// Dial establishes a connection to the target address through the HTTP CONNECT proxy. +func (d *httpConnectDialer) Dial(network, addr string) (net.Conn, error) { + return d.DialContext(context.Background(), network, addr) +} + +// DialContext establishes a connection to the target address through the HTTP CONNECT proxy +// with context support for cancellation and timeouts. +func (d *httpConnectDialer) DialContext(ctx context.Context, _, addr string) (net.Conn, error) { + // Try to use DialContext if forward dialer supports it + var conn net.Conn + var err error + + if ctxDialer, ok := d.forward.(proxy.ContextDialer); ok { + conn, err = ctxDialer.DialContext(ctx, "tcp", d.proxyURL.Host) + } else { + conn, err = d.forward.Dial("tcp", d.proxyURL.Host) + } + if err != nil { + return nil, errors.Wrapf(err, "failed to connect to proxy %s", d.proxyURL.Host) + } + + // Wrap with TLS if proxy uses HTTPS + if d.proxyURL.Scheme == "https" { + conn = tls.Client(conn, &tls.Config{ + ServerName: d.proxyURL.Hostname(), + MinVersion: tls.VersionTLS12, + }) + } + + // closeOnce guards conn.Close() against races between the handshake's + // error paths, the context-cancellation watchdog, and the caller's Close + // on the returned bufferedConn. + var closeOnce sync.Once + closeConn := func() error { + var err error + closeOnce.Do(func() { err = conn.Close() }) + return err + } + + // Watchdog lives until the caller closes the returned conn OR ctx fires. + // Closing done signals the watchdog to exit without touching the conn. + done := make(chan struct{}) + go func() { + select { + case <-ctx.Done(): + _ = closeConn() + case <-done: + } + }() + + // If we return before success, shut the watchdog down. + handshakeOK := false + defer func() { + if !handshakeOK { + close(done) + } + }() + + // Build CONNECT request + connectReq := &http.Request{ + Method: http.MethodConnect, + URL: &url.URL{Opaque: addr}, + Host: addr, + Header: make(http.Header), + } + + // Add proxy authentication if credentials are present in URL + if d.proxyURL.User != nil { + password, _ := d.proxyURL.User.Password() + auth := d.proxyURL.User.Username() + ":" + password + connectReq.Header.Set("Proxy-Authorization", + "Basic "+base64.StdEncoding.EncodeToString([]byte(auth))) + } + + // Send CONNECT request + if err := connectReq.Write(conn); err != nil { + _ = closeConn() + return nil, errors.Wrap(err, "failed to send CONNECT request") + } + + // Read response. Create the buffered reader once so that any bytes + // buffered beyond the HTTP response headers are not lost. + br := bufio.NewReader(conn) + resp, err := http.ReadResponse(br, connectReq) + if err != nil { + _ = closeConn() + return nil, errors.Wrap(err, "failed to read proxy response") + } + resp.Body.Close() //nolint:errcheck // Body is typically empty for CONNECT responses. + + if resp.StatusCode != http.StatusOK { + _ = closeConn() + return nil, errors.Errorf("proxy CONNECT failed with status: %s", resp.Status) + } + + // Success: transfer ownership of the watchdog to the bufferedConn. + handshakeOK = true + return &bufferedConn{ + r: br, + Conn: conn, + closeFn: func() error { + close(done) + return closeConn() + }, + }, nil +} + +// registerHTTPConnectProxy registers HTTP and HTTPS CONNECT proxy support +// with the golang.org/x/net/proxy package. It is called from init() so that +// importing this package is sufficient to enable HTTP CONNECT proxies for +// go-git's ProxyOptions (e.g. "http://proxy:8080" or "https://proxy:8443"). +func registerHTTPConnectProxy() { + newDialer := func(u *url.URL, forward proxy.Dialer) (proxy.Dialer, error) { + return &httpConnectDialer{proxyURL: u, forward: forward}, nil + } + proxy.RegisterDialerType("http", newDialer) + proxy.RegisterDialerType("https", newDialer) +} + +// init automatically registers HTTP CONNECT proxy support when this package is imported. +func init() { + registerHTTPConnectProxy() +} diff --git a/internal/git/proxy_test.go b/internal/git/proxy_test.go new file mode 100644 index 00000000..6c79f4aa --- /dev/null +++ b/internal/git/proxy_test.go @@ -0,0 +1,323 @@ +/* +Copyright 2026 The Crossplane Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package git + +import ( + "bufio" + "context" + "fmt" + "net" + "net/http" + "net/url" + "strings" + "testing" + "time" + + "golang.org/x/net/proxy" + "gotest.tools/v3/assert" +) + +func TestHTTPConnectDialer(t *testing.T) { + t.Parallel() + + // Start a mock HTTP CONNECT proxy server + listener, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0") + assert.NilError(t, err) + defer listener.Close() + + proxyAddr := listener.Addr().String() + targetAddr := "example.com:443" + + // Track if CONNECT was received + connectReceived := make(chan string, 1) + + go func() { + conn, err := listener.Accept() + if err != nil { + return + } + defer conn.Close() + + // Read the CONNECT request + reader := bufio.NewReader(conn) + req, err := http.ReadRequest(reader) + if err != nil { + return + } + + // Signal that we received the CONNECT request + connectReceived <- req.URL.String() + + // Send 200 OK response + resp := &http.Response{ + StatusCode: http.StatusOK, + Status: "200 Connection established", + Proto: "HTTP/1.1", + ProtoMajor: 1, + ProtoMinor: 1, + } + resp.Write(conn) + + // Keep connection open briefly + time.Sleep(100 * time.Millisecond) + }() + + // Create dialer using our implementation + proxyURL, err := url.Parse("http://" + proxyAddr) + assert.NilError(t, err) + + dialer := &httpConnectDialer{ + proxyURL: proxyURL, + forward: proxy.Direct, + } + + // Dial through proxy + conn, err := dialer.Dial("tcp", targetAddr) + if err != nil { + // Connection might close quickly, but we should have received CONNECT + t.Logf("Dial returned error (expected): %v", err) + } + if conn != nil { + conn.Close() + } + + // Verify CONNECT request was sent + select { + case received := <-connectReceived: + assert.Assert(t, strings.Contains(received, "example.com:443"), + "Expected CONNECT to example.com:443, got: %s", received) + case <-time.After(2 * time.Second): + t.Fatal("Timeout waiting for CONNECT request") + } +} + +func TestHTTPConnectDialerWithAuth(t *testing.T) { + t.Parallel() + + // Start a mock HTTP CONNECT proxy server that requires auth + listener, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0") + assert.NilError(t, err) + defer listener.Close() + + proxyAddr := listener.Addr().String() + targetAddr := "example.com:22" + + // Track auth header + authReceived := make(chan string, 1) + + go func() { + conn, err := listener.Accept() + if err != nil { + return + } + defer conn.Close() + + // Read the CONNECT request + reader := bufio.NewReader(conn) + req, err := http.ReadRequest(reader) + if err != nil { + return + } + + // Extract auth header + authReceived <- req.Header.Get("Proxy-Authorization") + + // Send 200 OK response + resp := &http.Response{ + StatusCode: http.StatusOK, + Status: "200 Connection established", + Proto: "HTTP/1.1", + ProtoMajor: 1, + ProtoMinor: 1, + } + resp.Write(conn) + + time.Sleep(100 * time.Millisecond) + }() + + // Create dialer with authentication + proxyURL, err := url.Parse(fmt.Sprintf("http://testuser:testpass@%s", proxyAddr)) + assert.NilError(t, err) + + dialer := &httpConnectDialer{ + proxyURL: proxyURL, + forward: proxy.Direct, + } + + // Dial through proxy + conn, _ := dialer.Dial("tcp", targetAddr) + if conn != nil { + conn.Close() + } + + // Verify auth header was sent + select { + case auth := <-authReceived: + assert.Assert(t, strings.HasPrefix(auth, "Basic "), + "Expected Basic auth header, got: %s", auth) + // Basic dGVzdHVzZXI6dGVzdHBhc3M= is base64 of "testuser:testpass" + assert.Equal(t, "Basic dGVzdHVzZXI6dGVzdHBhc3M=", auth) + case <-time.After(2 * time.Second): + t.Fatal("Timeout waiting for auth header") + } +} + +func TestHTTPConnectDialerNon200(t *testing.T) { + t.Parallel() + + // Start a mock HTTP CONNECT proxy server that returns 403 Forbidden + listener, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0") + assert.NilError(t, err) + defer listener.Close() + + proxyAddr := listener.Addr().String() + targetAddr := "example.com:22" + + go func() { + conn, err := listener.Accept() + if err != nil { + return + } + defer conn.Close() + + // Read the CONNECT request + reader := bufio.NewReader(conn) + _, err = http.ReadRequest(reader) + if err != nil { + return + } + + // Send 403 Forbidden response + resp := &http.Response{ + StatusCode: http.StatusForbidden, + Status: "403 Forbidden", + Proto: "HTTP/1.1", + ProtoMajor: 1, + ProtoMinor: 1, + } + resp.Write(conn) + }() + + proxyURL, err := url.Parse("http://" + proxyAddr) + assert.NilError(t, err) + + dialer := &httpConnectDialer{ + proxyURL: proxyURL, + forward: proxy.Direct, + } + + conn, err := dialer.Dial("tcp", targetAddr) + assert.Assert(t, err != nil, "expected error for non-200 proxy response") + assert.Assert(t, conn == nil, "expected nil connection for non-200 proxy response") + assert.Assert(t, strings.Contains(err.Error(), "403"), + "expected error to mention 403 status, got: %s", err.Error()) +} + +func TestHTTPConnectDialerHTTPS(t *testing.T) { + t.Parallel() + + // Start a plain TCP listener. The dialer should attempt a TLS + // handshake because the proxy URL uses the https scheme. Since the + // listener is plain TCP, the TLS handshake will not complete and the + // context deadline will fire, proving TLS was attempted. + listener, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0") + assert.NilError(t, err) + defer listener.Close() + + // Accept (and immediately close) so the TLS handshake fails fast + // with an EOF rather than hanging. + go func() { + conn, err := listener.Accept() + if err != nil { + return + } + conn.Close() + }() + + proxyAddr := listener.Addr().String() + targetAddr := "example.com:22" + + proxyURL, err := url.Parse("https://" + proxyAddr) + assert.NilError(t, err) + + dialer := &httpConnectDialer{ + proxyURL: proxyURL, + forward: proxy.Direct, + } + + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + conn, err := dialer.DialContext(ctx, "tcp", targetAddr) + if conn != nil { + conn.Close() + } + // We expect an error because the plain TCP listener cannot complete a TLS handshake. + assert.Assert(t, err != nil, "expected error when TLS handshake fails against plain TCP proxy") +} + +// TestHTTPConnectDialerCancellation verifies that DialContext returns promptly +// when the context is cancelled while the proxy accepts a socket but never +// responds (exercising the watchdog goroutine). +func TestHTTPConnectDialerCancellation(t *testing.T) { + t.Parallel() + + // Start a listener that accepts connections but never writes a response. + listener, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0") + assert.NilError(t, err) + defer listener.Close() + + go func() { + conn, err := listener.Accept() + if err != nil { + return + } + // Hold the connection open; never respond. + defer conn.Close() + <-t.Context().Done() + }() + + proxyURL, _ := url.Parse("http://" + listener.Addr().String()) + dialer := &httpConnectDialer{proxyURL: proxyURL, forward: proxy.Direct} + + ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond) + defer cancel() + + conn, err := dialer.DialContext(ctx, "tcp", "example.com:443") + assert.Assert(t, err != nil, "expected error on cancelled context, got nil") + assert.Assert(t, conn == nil, "expected nil conn on cancelled context") +} + +// TestRegisterHTTPConnectProxy verifies that the init()-driven dialer +// registration works. It must NOT use t.Parallel() because it validates +// package-global proxy dialer registration and must not run in parallel +// with other proxy tests that rely on that global state. +func TestRegisterHTTPConnectProxy(t *testing.T) { + // Verify that HTTP and HTTPS schemes are registered + // by checking if we can create dialers for these schemes + httpURL, _ := url.Parse("http://proxy.example.com:8080") + httpsURL, _ := url.Parse("https://proxy.example.com:8443") + + // These should not panic after registerHTTPConnectProxy is called (via init) + httpDialer, err := proxy.FromURL(httpURL, proxy.Direct) + assert.NilError(t, err) + assert.Assert(t, httpDialer != nil) + + httpsDialer, err := proxy.FromURL(httpsURL, proxy.Direct) + assert.NilError(t, err) + assert.Assert(t, httpsDialer != nil) +} diff --git a/nix/vendor-hashes.nix b/nix/vendor-hashes.nix index 95d1c311..b6ceb8aa 100644 --- a/nix/vendor-hashes.nix +++ b/nix/vendor-hashes.nix @@ -12,5 +12,5 @@ # hand.) { # Root module: github.com/crossplane/cli/v2 - root = "sha256-1lFueq+qsRx7dG9PJvBEQ29ovnB8hBDra0wZWGzbT5Q="; + root = "sha256-nI8FYG29KxFadyGMnn/OB9N4JlQWjtci9Oq1pvFSExM="; }