Skip to content
Merged
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
50 changes: 50 additions & 0 deletions cmd/crossplane/common/gitflags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
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 common

import (
"github.com/crossplane/cli/v2/internal/dependency"
"github.com/crossplane/cli/v2/internal/git"
)

// GitFlags configures authentication and connectivity for git-based
// dependencies. It is embedded in every command that resolves project
// dependencies, so users can pass the same settings across the CLI.
type GitFlags struct {
GitUsername string `default:"x-access-token" env:"CROSSPLANE_GIT_USERNAME" help:"Username for git HTTPS authentication."`
GitToken string `env:"CROSSPLANE_GIT_TOKEN" help:"Token for git HTTPS authentication."`
GitProxy string `env:"CROSSPLANE_GIT_PROXY" help:"Proxy URL to tunnel git operations through, for example 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."`
}

// ManagerOptions returns the dependency manager options the flags configure.
func (f GitFlags) ManagerOptions() []dependency.ManagerOption {
var opts []dependency.ManagerOption
if f.GitToken != "" {
opts = append(opts, dependency.WithGitAuthProvider(&git.HTTPSAuthProvider{
Username: f.GitUsername,
Password: f.GitToken,
}))
}
if f.GitProxy != "" {
opts = append(opts, dependency.WithGitProxyURL(f.GitProxy))
}
if f.GitInsecureHostKey {
opts = append(opts, dependency.WithGitInsecureHostKey(true))
}
return opts
}
20 changes: 8 additions & 12 deletions cmd/crossplane/dependency/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ import (
"github.com/crossplane/crossplane-runtime/v2/pkg/logging"

"github.com/crossplane/cli/v2/apis/dev/v1alpha1"
"github.com/crossplane/cli/v2/cmd/crossplane/common"
"github.com/crossplane/cli/v2/internal/config"
"github.com/crossplane/cli/v2/internal/dependency"
"github.com/crossplane/cli/v2/internal/git"
"github.com/crossplane/cli/v2/internal/project/projectfile"
"github.com/crossplane/cli/v2/internal/schemas/generator"
"github.com/crossplane/cli/v2/internal/terminal"
Expand All @@ -45,11 +45,11 @@ var addHelp string

// addCmd adds a dependency to the current project.
type addCmd struct {
common.GitFlags

Package string `arg:"" help:"Package to add (xpkg OCI reference, k8s:<version>, git repository URL, or HTTP(S) URL)."`
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."`
GitUsername string `default:"x-access-token" env:"CROSSPLANE_GIT_USERNAME" help:"Username for git HTTPS authentication."`

// Flags for specific dependency types.
APIOnly bool `help:"Mark an xpkg dependency as API-only (not a runtime dependency)." name:"api-only"`
Expand Down Expand Up @@ -82,7 +82,9 @@ func (c *addCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter, cfg *con
return errors.Wrap(err, "cannot create xpkg client")
}

opts := []dependency.ManagerOption{
gitOpts := c.ManagerOptions()
opts := make([]dependency.ManagerOption, 0, 3+len(gitOpts))
opts = append(opts,
dependency.WithProjectFile(c.ProjectFile),
dependency.WithSchemaGenerators(generator.Filter(
generator.AllLanguages(
Expand All @@ -92,14 +94,8 @@ func (c *addCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter, cfg *con
proj.Spec.Schemas.GetLanguages(),
)),
dependency.WithXpkgClient(client),
}

if c.GitToken != "" {
opts = append(opts, dependency.WithGitAuthProvider(&git.HTTPSAuthProvider{
Username: c.GitUsername,
Password: c.GitToken,
}))
}
)
opts = append(opts, gitOpts...)

m := dependency.NewManager(proj, projFS, opts...)

Expand Down
20 changes: 8 additions & 12 deletions cmd/crossplane/dependency/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import (
"github.com/crossplane/crossplane-runtime/v2/pkg/errors"
"github.com/crossplane/crossplane-runtime/v2/pkg/logging"

"github.com/crossplane/cli/v2/cmd/crossplane/common"
"github.com/crossplane/cli/v2/internal/async"
"github.com/crossplane/cli/v2/internal/config"
"github.com/crossplane/cli/v2/internal/dependency"
"github.com/crossplane/cli/v2/internal/git"
"github.com/crossplane/cli/v2/internal/project/projectfile"
"github.com/crossplane/cli/v2/internal/schemas/generator"
"github.com/crossplane/cli/v2/internal/schemas/manager"
Expand All @@ -47,10 +47,10 @@ var updateHelp string

// updateCacheCmd updates the dependency cache by regenerating all schemas.
type updateCacheCmd struct {
common.GitFlags

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."`
GitUsername string `default:"x-access-token" env:"CROSSPLANE_GIT_USERNAME" help:"Username for git HTTPS authentication."`
}

func (c *updateCacheCmd) Help() string {
Expand Down Expand Up @@ -86,18 +86,14 @@ func (c *updateCacheCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter,
proj.Spec.Schemas.GetLanguages(),
)

opts := []dependency.ManagerOption{
gitOpts := c.ManagerOptions()
opts := make([]dependency.ManagerOption, 0, 3+len(gitOpts))
opts = append(opts,
dependency.WithProjectFile(c.ProjectFile),
dependency.WithSchemaGenerators(generators),
dependency.WithXpkgClient(client),
}

if c.GitToken != "" {
opts = append(opts, dependency.WithGitAuthProvider(&git.HTTPSAuthProvider{
Username: c.GitUsername,
Password: c.GitToken,
}))
}
)
opts = append(opts, gitOpts...)

m := dependency.NewManager(proj, projFS, opts...)

Expand Down
5 changes: 3 additions & 2 deletions cmd/crossplane/dependency/dependency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,21 @@ import (

"github.com/crossplane/crossplane-runtime/v2/pkg/logging"

"github.com/crossplane/cli/v2/cmd/crossplane/common"
"github.com/crossplane/cli/v2/internal/config"
"github.com/crossplane/cli/v2/internal/terminal"
)

func TestAddGitAuth(t *testing.T) {
testDependencyGitAuth(t, func(serverURL, projectFile, cacheDir, token string) error {
c := &addCmd{Package: serverURL + "/repo.git", GitRef: "main", ProjectFile: projectFile, CacheDir: cacheDir, GitToken: token, GitUsername: "test-user"}
c := &addCmd{Package: serverURL + "/repo.git", GitRef: "main", ProjectFile: projectFile, CacheDir: cacheDir, GitFlags: common.GitFlags{GitToken: token, GitUsername: "test-user"}}
return c.Run(logging.NewNopLogger(), terminal.NewSpinnerPrinter(io.Discard, false), &config.Config{})
})
}

func TestUpdateCacheGitAuth(t *testing.T) {
testDependencyGitAuth(t, func(_, projectFile, cacheDir, token string) error {
c := &updateCacheCmd{ProjectFile: projectFile, CacheDir: cacheDir, GitToken: token, GitUsername: "test-user"}
c := &updateCacheCmd{ProjectFile: projectFile, CacheDir: cacheDir, GitFlags: common.GitFlags{GitToken: token, GitUsername: "test-user"}}
return c.Run(logging.NewNopLogger(), terminal.NewSpinnerPrinter(io.Discard, false), &config.Config{})
})
}
Expand Down
20 changes: 8 additions & 12 deletions cmd/crossplane/project/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ import (
"github.com/crossplane/crossplane-runtime/v2/pkg/logging"

devv1alpha1 "github.com/crossplane/cli/v2/apis/dev/v1alpha1"
"github.com/crossplane/cli/v2/cmd/crossplane/common"
"github.com/crossplane/cli/v2/internal/async"
"github.com/crossplane/cli/v2/internal/config"
"github.com/crossplane/cli/v2/internal/dependency"
"github.com/crossplane/cli/v2/internal/git"
"github.com/crossplane/cli/v2/internal/project"
"github.com/crossplane/cli/v2/internal/project/functions"
"github.com/crossplane/cli/v2/internal/project/projectfile"
Expand All @@ -52,13 +52,13 @@ var buildHelp string

// BuildCmd builds a project into Crossplane packages.
type BuildCmd struct {
common.GitFlags

ProjectFile string `default:"${project_file}" help:"Path to project definition." short:"f"`
Repository string `help:"Override the repository in the project file." optional:""`
OutputDir string `default:"_output" help:"Output directory for packages." short:"o"`
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."`
GitUsername string `default:"x-access-token" env:"CROSSPLANE_GIT_USERNAME" help:"Username for git HTTPS authentication."`

proj *devv1alpha1.Project
projFS afero.Fs
Expand Down Expand Up @@ -115,20 +115,16 @@ func (c *BuildCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter, cfg *c
return errors.Wrap(err, "cannot create xpkg client")
}

opts := []dependency.ManagerOption{
gitOpts := c.ManagerOptions()
opts := make([]dependency.ManagerOption, 0, 5+len(gitOpts))
opts = append(opts,
dependency.WithProjectFile(c.ProjectFile),
dependency.WithSchemaFS(schemasFS),
dependency.WithSchemaGenerators(generators),
dependency.WithSchemaRunner(schemaRunner),
dependency.WithXpkgClient(client),
}

if c.GitToken != "" {
opts = append(opts, dependency.WithGitAuthProvider(&git.HTTPSAuthProvider{
Username: c.GitUsername,
Password: c.GitToken,
}))
}
)
opts = append(opts, gitOpts...)

depMgr := dependency.NewManager(c.proj, c.projFS, opts...)

Expand Down
3 changes: 2 additions & 1 deletion cmd/crossplane/project/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

"github.com/crossplane/crossplane-runtime/v2/pkg/logging"

"github.com/crossplane/cli/v2/cmd/crossplane/common"
"github.com/crossplane/cli/v2/internal/config"
"github.com/crossplane/cli/v2/internal/terminal"
)
Expand Down Expand Up @@ -74,7 +75,7 @@ spec:
sp := terminal.NewSpinnerPrinter(io.Discard, false)
cfg := &config.Config{}

c := &BuildCmd{ProjectFile: projectFile, CacheDir: filepath.Join(dir, "cache"), GitToken: token, GitUsername: "test-user", MaxConcurrency: 1}
c := &BuildCmd{ProjectFile: projectFile, CacheDir: filepath.Join(dir, "cache"), GitFlags: common.GitFlags{GitToken: token, GitUsername: "test-user"}, MaxConcurrency: 1}
if err := c.AfterApply(); err != nil {
t.Fatal(err)
}
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.59.0
golang.org/x/mod v0.42.0
golang.org/x/net v0.62.0
golang.org/x/sync v0.25.0
golang.org/x/sys v0.50.0
golang.org/x/text v0.44.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 @@ -317,10 +320,8 @@ 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.59.0 // indirect
golang.org/x/crypto/openpgp v0.1.0-deprecated // 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.48.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
Loading
Loading