diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 94f06f1d..56d6de11 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -170,7 +170,7 @@ jobs: if: ${{ !cancelled() && steps.build.outcome == 'success' }} run: make -s coverage-report >> "$GITHUB_STEP_SUMMARY" - # Runs the integration tests with legacy auth, the default for users. + # Runs the integration tests with legacy auth, which users can still enable with GO_AUTH=0. # TODO: remove once legacy auth is no longer used. integration-test-legacy-auth: runs-on: ubuntu-latest diff --git a/CLAUDE.md b/CLAUDE.md index 9ccd1674..a2e75cb1 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -104,7 +104,7 @@ The CLI operates as a wrapper around a legacy PHP CLI: - Handles authentication, organizations, and resource management **Authentication**: `internal/auth/` -- Authentication is moving from PHP to Go, behind a feature flag: `GO_AUTH=1` (an env var for us and support, read by both Go and PHP). Without it, the legacy PHP CLI handles auth as before, and Go gets tokens from it (`LegacyCLIClient`) +- Authentication has moved from PHP to Go. Legacy auth can still be enabled with `GO_AUTH=0`: the legacy PHP CLI then handles auth as before, and Go gets tokens from it (`LegacyCLIClient`). The wrapper passes `GO_AUTH=1` or `0` to PHP, which defaults to legacy auth when run without it - With Go auth, Go is the only component that stores or refreshes credentials. `auth.Manager` resolves tokens (in order: an API token set via config, `api.access_token`, an API token stored by `auth:api-token-login`, stored sessions) and refreshes them under a per-session flock (`/auth/.lock`), re-reading the store under the lock because refresh tokens rotate - `internal/auth/store`: one entry per session ID, in the system keychain (go-keyring) or in `/auth/.json`. On Linux the keychain is only used if the Secret Service is GNOME Keyring or KWallet, and unlocked: other providers (e.g. KeePassXC) may ask the user to confirm each process that reads a secret - Auth settings are read by `config.Auth()` with the legacy CLI's precedence: embedded config, the user's `config.yaml`, env vars diff --git a/commands/auth_test.go b/commands/auth_test.go index c076cdc9..8d24889c 100644 --- a/commands/auth_test.go +++ b/commands/auth_test.go @@ -87,7 +87,7 @@ func TestLoginRequiredMessage(t *testing.T) { } func TestAuthCommands_GoAuthDisabled(t *testing.T) { - t.Setenv("TEST_GO_AUTH", "") + t.Setenv("TEST_GO_AUTH", "0") assert.Empty(t, authCommands(testConfig())) root := newRootCommand(testConfig()) c, _, err := root.Find([]string{"auth:internal"}) diff --git a/docs/configuration.md b/docs/configuration.md index 003a3acc..438960e6 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -24,6 +24,8 @@ You can override configuration in the user config file, certificate when running login or SSH commands. - `UPSUN_CLI_SHELL_CONFIG_FILE`: the shell config file that `self:install` writes to (an absolute path). Set it to an empty string to skip writing one. +- `UPSUN_CLI_GO_AUTH=0`: use the legacy (PHP) implementation of authentication. + Switching may require logging in again. - `UPSUN_CLI_REPORT_DEPRECATIONS=1`: show PHP deprecation notices in debug mode (`-vvv`). - `NO_COLOR=1` or `CLICOLOR_FORCE=0`/`1`: turn colors off or force them on. diff --git a/integration-tests/tests.go b/integration-tests/tests.go index 98e5c548..a0fa3dbd 100644 --- a/integration-tests/tests.go +++ b/integration-tests/tests.go @@ -108,8 +108,7 @@ func (f *cmdFactory) forSubtest(t *testing.T) *cmdFactory { // goAuthMode reports whether the tests run with Go auth. func goAuthMode() bool { - v := goAuthEnv() - return v != "" && v != "0" + return goAuthEnv() != "0" } // Run runs a command, asserts that it did not error, and returns its normal (stdout) output. diff --git a/internal/config/auth.go b/internal/config/auth.go index ffb0e010..b0ef53b2 100644 --- a/internal/config/auth.go +++ b/internal/config/auth.go @@ -82,9 +82,9 @@ var authKeys = []authKey{ // GoAuthEnabled reports whether authentication is handled in Go, instead of by the legacy CLI. // -// It is set by the GO_AUTH environment variable, which the legacy CLI also reads. +// It is enabled unless the GO_AUTH environment variable is set to 0. func (c *Config) GoAuthEnabled() bool { - return phpBool(os.Getenv(c.Application.EnvPrefix + "GO_AUTH")) + return os.Getenv(c.Application.EnvPrefix+"GO_AUTH") != "0" } var sessionIDPattern = regexp.MustCompile(`(?i)^[a-z0-9_-]+$`) diff --git a/internal/config/auth_test.go b/internal/config/auth_test.go index 7cfcaa0b..2773e2d7 100644 --- a/internal/config/auth_test.go +++ b/internal/config/auth_test.go @@ -232,3 +232,29 @@ func TestAuth(t *testing.T) { }) } } + +func TestGoAuthEnabled(t *testing.T) { + cases := []struct { + name string + value *string // nil if unset + want bool + }{ + {name: "unset", want: true}, + {name: "empty", value: new(""), want: true}, + {name: "1", value: new("1"), want: true}, + {name: "0", value: new("0"), want: false}, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + cnf := &config.Config{} + cnf.Application.EnvPrefix = "TEST_GO_AUTH_ENABLED_" + t.Setenv("TEST_GO_AUTH_ENABLED_GO_AUTH", "") + if c.value == nil { + require.NoError(t, os.Unsetenv("TEST_GO_AUTH_ENABLED_GO_AUTH")) + } else { + t.Setenv("TEST_GO_AUTH_ENABLED_GO_AUTH", *c.value) + } + assert.Equal(t, c.want, cnf.GoAuthEnabled()) + }) + } +} diff --git a/internal/legacy/legacy.go b/internal/legacy/legacy.go index cc36ba94..b186beed 100644 --- a/internal/legacy/legacy.go +++ b/internal/legacy/legacy.go @@ -140,50 +140,62 @@ func (c *CLIWrapper) Exec(ctx context.Context, args ...string) error { cmd.Stdin = c.Stdin cmd.Stdout = c.Stdout cmd.Stderr = c.Stderr - cmd.Env = append(cmd.Env, os.Environ()...) + cmd.Env = append(cmd.Env, c.env(cacheDir)...) + started := func() {} + if c.PrepareCmd != nil { + s, done, err := c.PrepareCmd(cmd) + if err != nil { + return err + } + defer done() + started = s + } + if err := cmd.Start(); err != nil { + return fmt.Errorf("could not run PHP CLI command: %w", err) + } + started() + if err := cmd.Wait(); err != nil { + return fmt.Errorf("could not run PHP CLI command: %w", err) + } + + return nil +} + +// env returns the environment for legacy CLI commands. +func (c *CLIWrapper) env(cacheDir string) []string { + env := os.Environ() envPrefix := c.Config.Application.EnvPrefix - cmd.Env = append( - cmd.Env, + // Go decides whether Go auth is enabled, as the legacy CLI's default differs. + goAuth := "0" + if c.Config.GoAuthEnabled() { + goAuth = "1" + } + env = append( + env, "CLI_CONFIG_FILE="+filepath.Join(cacheDir, configBasename), envPrefix+"MIGRATE_CHECK=0", envPrefix+"APPLICATION_PROMPT_SELF_INSTALL=0", envPrefix+"WRAPPED=1", envPrefix+"APPLICATION_VERSION="+c.Version, + envPrefix+"GO_AUTH="+goAuth, ) // The legacy CLI gets tokens and auth state by running this executable's hidden auth:internal command. if exe, err := os.Executable(); err == nil { - cmd.Env = append(cmd.Env, envPrefix+"WRAPPER_EXECUTABLE="+exe) + env = append(env, envPrefix+"WRAPPER_EXECUTABLE="+exe) } if c.DisableInteraction { - cmd.Env = append(cmd.Env, envPrefix+"NO_INTERACTION=1") + env = append(env, envPrefix+"NO_INTERACTION=1") } if c.ForceColor { - cmd.Env = append(cmd.Env, "CLICOLOR_FORCE=1") + env = append(env, "CLICOLOR_FORCE=1") } - cmd.Env = append(cmd.Env, fmt.Sprintf( + env = append(env, fmt.Sprintf( "%sUSER_AGENT={APP_NAME_DASH}/%s ({UNAME_S}; {UNAME_R}; PHP %s)", envPrefix, c.Version, PHPVersion, )) - started := func() {} - if c.PrepareCmd != nil { - s, done, err := c.PrepareCmd(cmd) - if err != nil { - return err - } - defer done() - started = s - } - if err := cmd.Start(); err != nil { - return fmt.Errorf("could not run PHP CLI command: %w", err) - } - started() - if err := cmd.Wait(); err != nil { - return fmt.Errorf("could not run PHP CLI command: %w", err) - } - - return nil + return env } // makeCmd makes a legacy CLI command with the given context and arguments. diff --git a/internal/legacy/legacy_test.go b/internal/legacy/legacy_test.go index a3bb76bd..55481656 100644 --- a/internal/legacy/legacy_test.go +++ b/internal/legacy/legacy_test.go @@ -127,3 +127,36 @@ func TestLegacyCLI(t *testing.T) { assert.NotContains(t, combined, "Unable to load dynamic library") assert.NotContains(t, combined, "Failed loading") } + +func TestEnvGoAuth(t *testing.T) { + cases := []struct { + name string + value *string // nil if unset + want string + }{ + {name: "unset", want: "1"}, + {name: "empty", value: new(""), want: "1"}, + {name: "1", value: new("1"), want: "1"}, + {name: "0", value: new("0"), want: "0"}, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + w := &CLIWrapper{Config: &config.Config{}} + w.Config.Application.EnvPrefix = "TEST_ENV_" + t.Setenv("TEST_ENV_GO_AUTH", "") + if c.value == nil { + require.NoError(t, os.Unsetenv("TEST_ENV_GO_AUTH")) + } else { + t.Setenv("TEST_ENV_GO_AUTH", *c.value) + } + // The last value is the one used. + var got string + for _, e := range w.env(t.TempDir()) { + if v, ok := strings.CutPrefix(e, "TEST_ENV_GO_AUTH="); ok { + got = v + } + } + assert.Equal(t, c.want, got) + }) + } +} diff --git a/legacy/src/Service/GoAuth.php b/legacy/src/Service/GoAuth.php index 921b37d6..db61d125 100644 --- a/legacy/src/Service/GoAuth.php +++ b/legacy/src/Service/GoAuth.php @@ -59,7 +59,7 @@ public function __construct(private readonly Config $config, ?OutputInterface $o } /** - * Checks whether authentication is handled by the Go wrapper, which is set by the {prefix}GO_AUTH environment + * Checks whether authentication is handled by the Go wrapper, which sets the {prefix}GO_AUTH environment * variable. Otherwise the legacy implementation is used. */ public function isEnabled(): bool