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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <PREFIX>GO_AUTH=0.
# TODO: remove once legacy auth is no longer used.
integration-test-legacy-auth:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: `<PREFIX>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 `<PREFIX>GO_AUTH=0`: the legacy PHP CLI then handles auth as before, and Go gets tokens from it (`LegacyCLIClient`). The wrapper passes `<PREFIX>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 (`<writable dir>/auth/<id>.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 `<writable dir>/auth/<id>.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
Expand Down
2 changes: 1 addition & 1 deletion commands/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Expand Down
2 changes: 2 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions integration-tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions internal/config/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <PREFIX>GO_AUTH environment variable, which the legacy CLI also reads.
// It is enabled unless the <PREFIX>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_-]+$`)
Expand Down
26 changes: 26 additions & 0 deletions internal/config/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
}
}
62 changes: 37 additions & 25 deletions internal/legacy/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
33 changes: 33 additions & 0 deletions internal/legacy/legacy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
2 changes: 1 addition & 1 deletion legacy/src/Service/GoAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading