From 7800d4381f92b672882606f6ea05a6dce0f77693 Mon Sep 17 00:00:00 2001 From: "daniel.tezolin" Date: Mon, 2 Nov 2026 21:00:29 +0000 Subject: [PATCH 1/2] fix: support Git HTTPS auth for dependency add and project build Signed-off-by: daniel.tezolin --- cmd/crossplane/dependency/add.go | 16 +- cmd/crossplane/dependency/auth.go | 37 ---- cmd/crossplane/dependency/cache.go | 7 +- cmd/crossplane/dependency/help/add.md | 6 + cmd/crossplane/main_test.go | 255 ++++++++++++++++++++++++++ cmd/crossplane/project/build.go | 16 +- cmd/crossplane/project/help/build.md | 6 + 7 files changed, 299 insertions(+), 44 deletions(-) delete mode 100644 cmd/crossplane/dependency/auth.go diff --git a/cmd/crossplane/dependency/add.go b/cmd/crossplane/dependency/add.go index 25f0b488..4b1d9fcb 100644 --- a/cmd/crossplane/dependency/add.go +++ b/cmd/crossplane/dependency/add.go @@ -31,6 +31,7 @@ import ( "github.com/crossplane/cli/v2/apis/dev/v1alpha1" "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" @@ -47,6 +48,8 @@ type addCmd struct { Package string `arg:"" help:"Package to add (xpkg OCI reference, k8s:, 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"` @@ -79,7 +82,7 @@ func (c *addCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter, cfg *con return errors.Wrap(err, "cannot create xpkg client") } - m := dependency.NewManager(proj, projFS, + opts := []dependency.ManagerOption{ dependency.WithProjectFile(c.ProjectFile), dependency.WithSchemaGenerators(generator.Filter( generator.AllLanguages( @@ -89,7 +92,16 @@ 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, + })) + } + + m := dependency.NewManager(proj, projFS, opts...) dep, err := c.buildDependency() if err != nil { diff --git a/cmd/crossplane/dependency/auth.go b/cmd/crossplane/dependency/auth.go deleted file mode 100644 index 2a3ecca8..00000000 --- a/cmd/crossplane/dependency/auth.go +++ /dev/null @@ -1,37 +0,0 @@ -/* -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 dependency - -import ( - "github.com/go-git/go-git/v5/plumbing/transport" - "github.com/go-git/go-git/v5/plumbing/transport/http" -) - -type gitAuthProvider struct { - username string - token string -} - -func (g *gitAuthProvider) GetAuthMethod() (transport.AuthMethod, error) { - if g.token != "" { - return &http.BasicAuth{ - Username: g.username, - Password: g.token, - }, nil - } - return nil, nil -} diff --git a/cmd/crossplane/dependency/cache.go b/cmd/crossplane/dependency/cache.go index 326f226d..c31b09f3 100644 --- a/cmd/crossplane/dependency/cache.go +++ b/cmd/crossplane/dependency/cache.go @@ -31,6 +31,7 @@ import ( "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/terminal" @@ -88,9 +89,9 @@ func (c *updateCacheCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter, } if c.GitToken != "" { - opts = append(opts, dependency.WithGitAuthProvider(&gitAuthProvider{ - username: c.GitUsername, - token: c.GitToken, + opts = append(opts, dependency.WithGitAuthProvider(&git.HTTPSAuthProvider{ + Username: c.GitUsername, + Password: c.GitToken, })) } diff --git a/cmd/crossplane/dependency/help/add.md b/cmd/crossplane/dependency/help/add.md index df9a1bfc..29794745 100644 --- a/cmd/crossplane/dependency/help/add.md +++ b/cmd/crossplane/dependency/help/add.md @@ -19,6 +19,12 @@ dependencies. Use the `--api-only` flag to add a build-time xpkg dependency. Non-xpkg dependencies are always build-time dependencies. +Git dependencies fetched over HTTPS can authenticate using `--git-token` and +`--git-username`. Prefer the `CROSSPLANE_GIT_TOKEN` and +`CROSSPLANE_GIT_USERNAME` environment variables to avoid exposing credentials +in shell history. The username defaults to `x-access-token`. Credentials are not +saved in the project file. + ## Examples Retrieve the latest available semantic version of `provider-aws-eks`, generate diff --git a/cmd/crossplane/main_test.go b/cmd/crossplane/main_test.go index 1e015f3a..341f75e7 100644 --- a/cmd/crossplane/main_test.go +++ b/cmd/crossplane/main_test.go @@ -17,10 +17,25 @@ limitations under the License. package main import ( + "fmt" + "io" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" "testing" + "github.com/alecthomas/kong" + gitclient "github.com/go-git/go-git/v5/plumbing/transport/client" + githttp "github.com/go-git/go-git/v5/plumbing/transport/http" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" + + "github.com/crossplane/crossplane-runtime/v2/pkg/logging" + + "github.com/crossplane/cli/v2/internal/config" + "github.com/crossplane/cli/v2/internal/terminal" ) func TestConfigFlag(t *testing.T) { @@ -86,3 +101,243 @@ func TestConfigFlag(t *testing.T) { }) } } + +func TestGitHTTPSAuthFlags(t *testing.T) { + t.Setenv("CROSSPLANE_GIT_TOKEN", "env-token") + t.Setenv("CROSSPLANE_GIT_USERNAME", "env-user") + + projectFile := filepath.Join(t.TempDir(), "crossplane-project.yaml") + if err := os.WriteFile(projectFile, []byte(`apiVersion: dev.crossplane.io/v1alpha1 +kind: Project +metadata: + name: test +spec: + repository: example.com/test +`), 0o600); err != nil { + t.Fatal(err) + } + + type credentials func(*cli) (string, string) + cases := map[string]struct { + args []string + clearEnv bool + get credentials + wantToken string + wantUsername string + }{ + "DependencyAddWithoutToken": { + args: []string{"dependency", "add", "https://example.com/repo.git", "--git-ref=main"}, + clearEnv: true, + get: func(c *cli) (string, string) { return c.Dependency.Add.GitToken, c.Dependency.Add.GitUsername }, + wantToken: "", + wantUsername: "x-access-token", + }, + "DependencyAddDefaultUsername": { + args: []string{"dependency", "add", "https://example.com/repo.git", "--git-ref=main", "--git-token=test-token"}, + clearEnv: true, + get: func(c *cli) (string, string) { return c.Dependency.Add.GitToken, c.Dependency.Add.GitUsername }, + wantToken: "test-token", + wantUsername: "x-access-token", + }, + "ProjectBuildWithoutToken": { + args: []string{"project", "build", "-f", projectFile}, + clearEnv: true, + get: func(c *cli) (string, string) { return c.Project.Build.GitToken, c.Project.Build.GitUsername }, + wantToken: "", + wantUsername: "x-access-token", + }, + "ProjectBuildDefaultUsername": { + args: []string{"project", "build", "-f", projectFile, "--git-token=test-token"}, + clearEnv: true, + get: func(c *cli) (string, string) { return c.Project.Build.GitToken, c.Project.Build.GitUsername }, + wantToken: "test-token", + wantUsername: "x-access-token", + }, + "DependencyUpdateCacheWithoutToken": { + args: []string{"dependency", "update-cache"}, + clearEnv: true, + get: func(c *cli) (string, string) { + return c.Dependency.UpdateCache.GitToken, c.Dependency.UpdateCache.GitUsername + }, + wantToken: "", + wantUsername: "x-access-token", + }, + "DependencyUpdateCacheDefaultUsername": { + args: []string{"dependency", "update-cache", "--git-token=test-token"}, + clearEnv: true, + get: func(c *cli) (string, string) { + return c.Dependency.UpdateCache.GitToken, c.Dependency.UpdateCache.GitUsername + }, + wantToken: "test-token", + wantUsername: "x-access-token", + }, + "DependencyAddFlags": { + args: []string{"dependency", "add", "https://example.com/repo.git", "--git-ref=main", "--git-token=test-token", "--git-username=test-user"}, + get: func(c *cli) (string, string) { + return c.Dependency.Add.GitToken, c.Dependency.Add.GitUsername + }, + wantToken: "test-token", + wantUsername: "test-user", + }, + "DependencyAddEnvironment": { + args: []string{"dependency", "add", "https://example.com/repo.git", "--git-ref=main"}, + get: func(c *cli) (string, string) { + return c.Dependency.Add.GitToken, c.Dependency.Add.GitUsername + }, + wantToken: "env-token", + wantUsername: "env-user", + }, + "DependencyUpdateCacheFlags": { + args: []string{"dependency", "update-cache", "--git-token=test-token", "--git-username=test-user"}, + get: func(c *cli) (string, string) { + return c.Dependency.UpdateCache.GitToken, c.Dependency.UpdateCache.GitUsername + }, + wantToken: "test-token", + wantUsername: "test-user", + }, + "DependencyUpdateCacheEnvironment": { + args: []string{"dependency", "update-cache"}, + get: func(c *cli) (string, string) { + return c.Dependency.UpdateCache.GitToken, c.Dependency.UpdateCache.GitUsername + }, + wantToken: "env-token", + wantUsername: "env-user", + }, + "ProjectBuildFlags": { + args: []string{"project", "build", "-f", projectFile, "--git-token=test-token", "--git-username=test-user"}, + get: func(c *cli) (string, string) { + return c.Project.Build.GitToken, c.Project.Build.GitUsername + }, + wantToken: "test-token", + wantUsername: "test-user", + }, + "ProjectBuildEnvironment": { + args: []string{"project", "build", "-f", projectFile}, + get: func(c *cli) (string, string) { + return c.Project.Build.GitToken, c.Project.Build.GitUsername + }, + wantToken: "env-token", + wantUsername: "env-user", + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + if tc.clearEnv { + t.Setenv("CROSSPLANE_GIT_TOKEN", "") + t.Setenv("CROSSPLANE_GIT_USERNAME", "") + for _, key := range []string{"CROSSPLANE_GIT_TOKEN", "CROSSPLANE_GIT_USERNAME"} { + if err := os.Unsetenv(key); err != nil { + t.Fatal(err) + } + } + } + var c cli + parser, err := kong.New(&c, kongVars) + if err != nil { + t.Fatalf("kong.New(): %v", err) + } + if _, err := parser.Parse(tc.args); err != nil { + t.Fatalf("kong.Parse(%v): %v", tc.args, err) + } + + gotToken, gotUsername := tc.get(&c) + if diff := cmp.Diff(tc.wantToken, gotToken); diff != "" { + t.Errorf("git token (-want +got):\n%s", diff) + } + if diff := cmp.Diff(tc.wantUsername, gotUsername); diff != "" { + t.Errorf("git username (-want +got):\n%s", diff) + } + }) + } +} + +// TestGitHTTPSAuthReachesClone exercises the commands through their real managers +// and cloner. The server rejects the clone after recording its credentials. +// This test runs serially because it temporarily replaces the HTTP transports. +func TestGitHTTPSAuthReachesClone(t *testing.T) { + for _, scheme := range []string{"https", "http"} { + for _, command := range []string{"add", "build", "update-cache"} { + for _, token := range []string{"test-token", ""} { + t.Run(fmt.Sprintf("%s/%s/auth=%t", scheme, command, token != ""), func(t *testing.T) { + t.Setenv("CROSSPLANE_GIT_TOKEN", token) + t.Setenv("CROSSPLANE_GIT_USERNAME", "test-user") + credentials := make(chan string, 10) + server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + user, password, _ := r.BasicAuth() + credentials <- user + ":" + password + w.WriteHeader(http.StatusUnauthorized) + })) + if scheme == "https" { + server.StartTLS() + } else { + server.Start() + } + defer server.Close() + original := gitclient.Protocols[scheme] + gitclient.InstallProtocol(scheme, githttp.NewClient(server.Client())) + t.Cleanup(func() { gitclient.InstallProtocol(scheme, original) }) + dir := t.TempDir() + if err := os.MkdirAll(filepath.Join(dir, "apis"), 0o700); err != nil { + t.Fatal(err) + } + projectFile := filepath.Join(dir, "crossplane-project.yaml") + project := fmt.Sprintf(`apiVersion: dev.crossplane.io/v1alpha1 +kind: Project +metadata: + name: test +spec: + repository: example.com/test + dependencies: + - type: crd + git: + repository: %s/repo.git + ref: main +`, server.URL) + if err := os.WriteFile(projectFile, []byte(project), 0o600); err != nil { + t.Fatal(err) + } + args := []string{"dependency", command} + if command == "build" { + args = []string{"project", "build"} + } + if command == "add" { + args = append(args, server.URL+"/repo.git", "--git-ref=main") + } + args = append(args, "--project-file="+projectFile, "--cache-dir="+filepath.Join(dir, "cache")) + var c cli + parser, err := kong.New(&c, kongVars) + if err != nil { + t.Fatal(err) + } + ctx, err := parser.Parse(args) + if err != nil { + t.Fatal(err) + } + ctx.BindTo(logging.NewNopLogger(), (*logging.Logger)(nil)) + ctx.BindTo(terminal.NewSpinnerPrinter(io.Discard, false), (*terminal.SpinnerPrinter)(nil)) + ctx.Bind(&config.Config{}) + err = ctx.Run() + if err == nil { + t.Fatal("expected unauthorized clone to fail") + } + if token != "" && strings.Contains(err.Error(), token) { + t.Fatal("clone error exposes token") + } + want := ":" + if token != "" { + want = "test-user:" + token + } + select { + case got := <-credentials: + if got != want { + t.Fatal("clone received unexpected credentials") + } + default: + t.Fatalf("command did not reach git server: %v", err) + } + }) + } + } + } +} diff --git a/cmd/crossplane/project/build.go b/cmd/crossplane/project/build.go index 1e18345b..d7b0e354 100644 --- a/cmd/crossplane/project/build.go +++ b/cmd/crossplane/project/build.go @@ -34,6 +34,7 @@ import ( "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" @@ -56,6 +57,8 @@ type buildCmd struct { 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 @@ -111,13 +114,22 @@ func (c *buildCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter, cfg *c return errors.Wrap(err, "cannot create xpkg client") } - depMgr := dependency.NewManager(c.proj, c.projFS, + opts := []dependency.ManagerOption{ 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, + })) + } + + depMgr := dependency.NewManager(c.proj, c.projFS, opts...) // The builder may decompress function runtime tarballs into this directory; // the built images read from it lazily, so we remove it only after the diff --git a/cmd/crossplane/project/help/build.md b/cmd/crossplane/project/help/build.md index b1de7f2e..818febfe 100644 --- a/cmd/crossplane/project/help/build.md +++ b/cmd/crossplane/project/help/build.md @@ -17,6 +17,12 @@ The build reuses the dependency cache populated by `crossplane dependency add` and `crossplane dependency update-cache`. Override the cache location with `--cache-dir` or the `CROSSPLANE_XPKG_CACHE` environment variable. +Git dependencies fetched over HTTPS can authenticate using `--git-token` and +`--git-username`. Prefer the `CROSSPLANE_GIT_TOKEN` and +`CROSSPLANE_GIT_USERNAME` environment variables to avoid exposing credentials +in shell history. The username defaults to `x-access-token`. Credentials are not +saved in the project file. + The CLI builds embedded functions onto a runtime base image from a registry, and caches those image layers on disk under `crossplane/base-images` in your user cache directory. A build that needs a layer already in the cache reads it From d31dae121a2fd2ffedf58fbe2570de770592422d Mon Sep 17 00:00:00 2001 From: "daniel.tezolin" Date: Sat, 7 Nov 2026 00:48:08 +0000 Subject: [PATCH 2/2] test: address Git auth review feedback Signed-off-by: daniel.tezolin --- cmd/crossplane/dependency/dependency_test.go | 104 ++++++++ cmd/crossplane/dependency/help/add.md | 2 +- cmd/crossplane/main_test.go | 255 ------------------- cmd/crossplane/project/build_test.go | 97 +++++++ cmd/crossplane/project/help/build.md | 2 +- 5 files changed, 203 insertions(+), 257 deletions(-) create mode 100644 cmd/crossplane/dependency/dependency_test.go create mode 100644 cmd/crossplane/project/build_test.go diff --git a/cmd/crossplane/dependency/dependency_test.go b/cmd/crossplane/dependency/dependency_test.go new file mode 100644 index 00000000..ee254ac6 --- /dev/null +++ b/cmd/crossplane/dependency/dependency_test.go @@ -0,0 +1,104 @@ +/* +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 dependency + +import ( + "fmt" + "io" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "testing" + + gitclient "github.com/go-git/go-git/v5/plumbing/transport/client" + githttp "github.com/go-git/go-git/v5/plumbing/transport/http" + + "github.com/crossplane/crossplane-runtime/v2/pkg/logging" + + "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"} + 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"} + return c.Run(logging.NewNopLogger(), terminal.NewSpinnerPrinter(io.Discard, false), &config.Config{}) + }) +} + +// testDependencyGitAuth verifies that a command passes its token to a Git +// clone. It replaces the global HTTPS transport, so callers must run serially. +func testDependencyGitAuth(t *testing.T, run func(serverURL, projectFile, cacheDir, token string) error) { + t.Helper() + const token = "test-token" + credentials := make(chan string, 10) + server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + user, password, _ := r.BasicAuth() + credentials <- user + ":" + password + w.WriteHeader(http.StatusUnauthorized) + })) + defer server.Close() + original := gitclient.Protocols["https"] + gitclient.InstallProtocol("https", githttp.NewClient(server.Client())) + t.Cleanup(func() { gitclient.InstallProtocol("https", original) }) + dir := t.TempDir() + if err := os.MkdirAll(filepath.Join(dir, "apis"), 0o700); err != nil { + t.Fatal(err) + } + projectFile := filepath.Join(dir, "crossplane-project.yaml") + project := fmt.Sprintf(`apiVersion: dev.crossplane.io/v1alpha1 +kind: Project +metadata: + name: test +spec: + repository: example.com/test + dependencies: + - type: crd + git: + repository: %s/repo.git + ref: main +`, server.URL) + if err := os.WriteFile(projectFile, []byte(project), 0o600); err != nil { + t.Fatal(err) + } + + err := run(server.URL, projectFile, filepath.Join(dir, "cache"), token) + + if err == nil { + t.Fatal("expected unauthorized clone to fail") + } + if strings.Contains(err.Error(), token) { + t.Fatal("clone error exposes token") + } + select { + case got := <-credentials: + if got != "test-user:"+token { + t.Fatal("clone received unexpected credentials") + } + default: + t.Fatalf("command did not reach git server: %v", err) + } +} diff --git a/cmd/crossplane/dependency/help/add.md b/cmd/crossplane/dependency/help/add.md index 29794745..4677c806 100644 --- a/cmd/crossplane/dependency/help/add.md +++ b/cmd/crossplane/dependency/help/add.md @@ -22,7 +22,7 @@ Non-xpkg dependencies are always build-time dependencies. Git dependencies fetched over HTTPS can authenticate using `--git-token` and `--git-username`. Prefer the `CROSSPLANE_GIT_TOKEN` and `CROSSPLANE_GIT_USERNAME` environment variables to avoid exposing credentials -in shell history. The username defaults to `x-access-token`. Credentials are not +in shell history. The username defaults to `x-access-token`. Credentials aren't saved in the project file. ## Examples diff --git a/cmd/crossplane/main_test.go b/cmd/crossplane/main_test.go index 341f75e7..1e015f3a 100644 --- a/cmd/crossplane/main_test.go +++ b/cmd/crossplane/main_test.go @@ -17,25 +17,10 @@ limitations under the License. package main import ( - "fmt" - "io" - "net/http" - "net/http/httptest" - "os" - "path/filepath" - "strings" "testing" - "github.com/alecthomas/kong" - gitclient "github.com/go-git/go-git/v5/plumbing/transport/client" - githttp "github.com/go-git/go-git/v5/plumbing/transport/http" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" - - "github.com/crossplane/crossplane-runtime/v2/pkg/logging" - - "github.com/crossplane/cli/v2/internal/config" - "github.com/crossplane/cli/v2/internal/terminal" ) func TestConfigFlag(t *testing.T) { @@ -101,243 +86,3 @@ func TestConfigFlag(t *testing.T) { }) } } - -func TestGitHTTPSAuthFlags(t *testing.T) { - t.Setenv("CROSSPLANE_GIT_TOKEN", "env-token") - t.Setenv("CROSSPLANE_GIT_USERNAME", "env-user") - - projectFile := filepath.Join(t.TempDir(), "crossplane-project.yaml") - if err := os.WriteFile(projectFile, []byte(`apiVersion: dev.crossplane.io/v1alpha1 -kind: Project -metadata: - name: test -spec: - repository: example.com/test -`), 0o600); err != nil { - t.Fatal(err) - } - - type credentials func(*cli) (string, string) - cases := map[string]struct { - args []string - clearEnv bool - get credentials - wantToken string - wantUsername string - }{ - "DependencyAddWithoutToken": { - args: []string{"dependency", "add", "https://example.com/repo.git", "--git-ref=main"}, - clearEnv: true, - get: func(c *cli) (string, string) { return c.Dependency.Add.GitToken, c.Dependency.Add.GitUsername }, - wantToken: "", - wantUsername: "x-access-token", - }, - "DependencyAddDefaultUsername": { - args: []string{"dependency", "add", "https://example.com/repo.git", "--git-ref=main", "--git-token=test-token"}, - clearEnv: true, - get: func(c *cli) (string, string) { return c.Dependency.Add.GitToken, c.Dependency.Add.GitUsername }, - wantToken: "test-token", - wantUsername: "x-access-token", - }, - "ProjectBuildWithoutToken": { - args: []string{"project", "build", "-f", projectFile}, - clearEnv: true, - get: func(c *cli) (string, string) { return c.Project.Build.GitToken, c.Project.Build.GitUsername }, - wantToken: "", - wantUsername: "x-access-token", - }, - "ProjectBuildDefaultUsername": { - args: []string{"project", "build", "-f", projectFile, "--git-token=test-token"}, - clearEnv: true, - get: func(c *cli) (string, string) { return c.Project.Build.GitToken, c.Project.Build.GitUsername }, - wantToken: "test-token", - wantUsername: "x-access-token", - }, - "DependencyUpdateCacheWithoutToken": { - args: []string{"dependency", "update-cache"}, - clearEnv: true, - get: func(c *cli) (string, string) { - return c.Dependency.UpdateCache.GitToken, c.Dependency.UpdateCache.GitUsername - }, - wantToken: "", - wantUsername: "x-access-token", - }, - "DependencyUpdateCacheDefaultUsername": { - args: []string{"dependency", "update-cache", "--git-token=test-token"}, - clearEnv: true, - get: func(c *cli) (string, string) { - return c.Dependency.UpdateCache.GitToken, c.Dependency.UpdateCache.GitUsername - }, - wantToken: "test-token", - wantUsername: "x-access-token", - }, - "DependencyAddFlags": { - args: []string{"dependency", "add", "https://example.com/repo.git", "--git-ref=main", "--git-token=test-token", "--git-username=test-user"}, - get: func(c *cli) (string, string) { - return c.Dependency.Add.GitToken, c.Dependency.Add.GitUsername - }, - wantToken: "test-token", - wantUsername: "test-user", - }, - "DependencyAddEnvironment": { - args: []string{"dependency", "add", "https://example.com/repo.git", "--git-ref=main"}, - get: func(c *cli) (string, string) { - return c.Dependency.Add.GitToken, c.Dependency.Add.GitUsername - }, - wantToken: "env-token", - wantUsername: "env-user", - }, - "DependencyUpdateCacheFlags": { - args: []string{"dependency", "update-cache", "--git-token=test-token", "--git-username=test-user"}, - get: func(c *cli) (string, string) { - return c.Dependency.UpdateCache.GitToken, c.Dependency.UpdateCache.GitUsername - }, - wantToken: "test-token", - wantUsername: "test-user", - }, - "DependencyUpdateCacheEnvironment": { - args: []string{"dependency", "update-cache"}, - get: func(c *cli) (string, string) { - return c.Dependency.UpdateCache.GitToken, c.Dependency.UpdateCache.GitUsername - }, - wantToken: "env-token", - wantUsername: "env-user", - }, - "ProjectBuildFlags": { - args: []string{"project", "build", "-f", projectFile, "--git-token=test-token", "--git-username=test-user"}, - get: func(c *cli) (string, string) { - return c.Project.Build.GitToken, c.Project.Build.GitUsername - }, - wantToken: "test-token", - wantUsername: "test-user", - }, - "ProjectBuildEnvironment": { - args: []string{"project", "build", "-f", projectFile}, - get: func(c *cli) (string, string) { - return c.Project.Build.GitToken, c.Project.Build.GitUsername - }, - wantToken: "env-token", - wantUsername: "env-user", - }, - } - - for name, tc := range cases { - t.Run(name, func(t *testing.T) { - if tc.clearEnv { - t.Setenv("CROSSPLANE_GIT_TOKEN", "") - t.Setenv("CROSSPLANE_GIT_USERNAME", "") - for _, key := range []string{"CROSSPLANE_GIT_TOKEN", "CROSSPLANE_GIT_USERNAME"} { - if err := os.Unsetenv(key); err != nil { - t.Fatal(err) - } - } - } - var c cli - parser, err := kong.New(&c, kongVars) - if err != nil { - t.Fatalf("kong.New(): %v", err) - } - if _, err := parser.Parse(tc.args); err != nil { - t.Fatalf("kong.Parse(%v): %v", tc.args, err) - } - - gotToken, gotUsername := tc.get(&c) - if diff := cmp.Diff(tc.wantToken, gotToken); diff != "" { - t.Errorf("git token (-want +got):\n%s", diff) - } - if diff := cmp.Diff(tc.wantUsername, gotUsername); diff != "" { - t.Errorf("git username (-want +got):\n%s", diff) - } - }) - } -} - -// TestGitHTTPSAuthReachesClone exercises the commands through their real managers -// and cloner. The server rejects the clone after recording its credentials. -// This test runs serially because it temporarily replaces the HTTP transports. -func TestGitHTTPSAuthReachesClone(t *testing.T) { - for _, scheme := range []string{"https", "http"} { - for _, command := range []string{"add", "build", "update-cache"} { - for _, token := range []string{"test-token", ""} { - t.Run(fmt.Sprintf("%s/%s/auth=%t", scheme, command, token != ""), func(t *testing.T) { - t.Setenv("CROSSPLANE_GIT_TOKEN", token) - t.Setenv("CROSSPLANE_GIT_USERNAME", "test-user") - credentials := make(chan string, 10) - server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - user, password, _ := r.BasicAuth() - credentials <- user + ":" + password - w.WriteHeader(http.StatusUnauthorized) - })) - if scheme == "https" { - server.StartTLS() - } else { - server.Start() - } - defer server.Close() - original := gitclient.Protocols[scheme] - gitclient.InstallProtocol(scheme, githttp.NewClient(server.Client())) - t.Cleanup(func() { gitclient.InstallProtocol(scheme, original) }) - dir := t.TempDir() - if err := os.MkdirAll(filepath.Join(dir, "apis"), 0o700); err != nil { - t.Fatal(err) - } - projectFile := filepath.Join(dir, "crossplane-project.yaml") - project := fmt.Sprintf(`apiVersion: dev.crossplane.io/v1alpha1 -kind: Project -metadata: - name: test -spec: - repository: example.com/test - dependencies: - - type: crd - git: - repository: %s/repo.git - ref: main -`, server.URL) - if err := os.WriteFile(projectFile, []byte(project), 0o600); err != nil { - t.Fatal(err) - } - args := []string{"dependency", command} - if command == "build" { - args = []string{"project", "build"} - } - if command == "add" { - args = append(args, server.URL+"/repo.git", "--git-ref=main") - } - args = append(args, "--project-file="+projectFile, "--cache-dir="+filepath.Join(dir, "cache")) - var c cli - parser, err := kong.New(&c, kongVars) - if err != nil { - t.Fatal(err) - } - ctx, err := parser.Parse(args) - if err != nil { - t.Fatal(err) - } - ctx.BindTo(logging.NewNopLogger(), (*logging.Logger)(nil)) - ctx.BindTo(terminal.NewSpinnerPrinter(io.Discard, false), (*terminal.SpinnerPrinter)(nil)) - ctx.Bind(&config.Config{}) - err = ctx.Run() - if err == nil { - t.Fatal("expected unauthorized clone to fail") - } - if token != "" && strings.Contains(err.Error(), token) { - t.Fatal("clone error exposes token") - } - want := ":" - if token != "" { - want = "test-user:" + token - } - select { - case got := <-credentials: - if got != want { - t.Fatal("clone received unexpected credentials") - } - default: - t.Fatalf("command did not reach git server: %v", err) - } - }) - } - } - } -} diff --git a/cmd/crossplane/project/build_test.go b/cmd/crossplane/project/build_test.go new file mode 100644 index 00000000..54979ef5 --- /dev/null +++ b/cmd/crossplane/project/build_test.go @@ -0,0 +1,97 @@ +/* +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 project + +import ( + "fmt" + "io" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "testing" + + gitclient "github.com/go-git/go-git/v5/plumbing/transport/client" + githttp "github.com/go-git/go-git/v5/plumbing/transport/http" + + "github.com/crossplane/crossplane-runtime/v2/pkg/logging" + + "github.com/crossplane/cli/v2/internal/config" + "github.com/crossplane/cli/v2/internal/terminal" +) + +// TestBuildGitAuth verifies that build passes its token to a Git clone. +// It runs serially because it temporarily replaces the HTTPS transport. +func TestBuildGitAuth(t *testing.T) { + const token = "test-token" + credentials := make(chan string, 10) + server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + user, password, _ := r.BasicAuth() + credentials <- user + ":" + password + w.WriteHeader(http.StatusUnauthorized) + })) + defer server.Close() + original := gitclient.Protocols["https"] + gitclient.InstallProtocol("https", githttp.NewClient(server.Client())) + t.Cleanup(func() { gitclient.InstallProtocol("https", original) }) + dir := t.TempDir() + if err := os.MkdirAll(filepath.Join(dir, "apis"), 0o700); err != nil { + t.Fatal(err) + } + projectFile := filepath.Join(dir, "crossplane-project.yaml") + project := fmt.Sprintf(`apiVersion: dev.crossplane.io/v1alpha1 +kind: Project +metadata: + name: test +spec: + repository: example.com/test + dependencies: + - type: crd + git: + repository: %s/repo.git + ref: main +`, server.URL) + if err := os.WriteFile(projectFile, []byte(project), 0o600); err != nil { + t.Fatal(err) + } + + logger := logging.NewNopLogger() + 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} + if err := c.AfterApply(); err != nil { + t.Fatal(err) + } + err := c.Run(logger, sp, cfg) + + if err == nil { + t.Fatal("expected unauthorized clone to fail") + } + if strings.Contains(err.Error(), token) { + t.Fatal("clone error exposes token") + } + select { + case got := <-credentials: + if got != "test-user:"+token { + t.Fatal("clone received unexpected credentials") + } + default: + t.Fatalf("command did not reach git server: %v", err) + } +} diff --git a/cmd/crossplane/project/help/build.md b/cmd/crossplane/project/help/build.md index 818febfe..3117e30c 100644 --- a/cmd/crossplane/project/help/build.md +++ b/cmd/crossplane/project/help/build.md @@ -20,7 +20,7 @@ and `crossplane dependency update-cache`. Override the cache location with Git dependencies fetched over HTTPS can authenticate using `--git-token` and `--git-username`. Prefer the `CROSSPLANE_GIT_TOKEN` and `CROSSPLANE_GIT_USERNAME` environment variables to avoid exposing credentials -in shell history. The username defaults to `x-access-token`. Credentials are not +in shell history. The username defaults to `x-access-token`. Credentials aren't saved in the project file. The CLI builds embedded functions onto a runtime base image from a registry, and