Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cmd/crossplane/dependency/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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...)

Expand Down
8 changes: 8 additions & 0 deletions cmd/crossplane/dependency/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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."`
}

Expand Down Expand Up @@ -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...)

Expand Down
8 changes: 8 additions & 0 deletions cmd/crossplane/project/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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...)

Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
73 changes: 55 additions & 18 deletions internal/dependency/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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{}),
}
}

Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down
80 changes: 75 additions & 5 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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{
Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -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
}
}
Expand Down
Loading
Loading