diff --git a/.nextchanges/cli/aitools-auto-install-sudo-home.md b/.nextchanges/cli/aitools-auto-install-sudo-home.md new file mode 100644 index 00000000000..923ae5574e0 --- /dev/null +++ b/.nextchanges/cli/aitools-auto-install-sudo-home.md @@ -0,0 +1 @@ +* The automatic installation of Databricks AI tools no longer runs when the CLI runs as root in a home directory another user owns, as under `sudo` with `HOME` kept (the macOS default). Such a run used to leave a root-owned `~/.databricks` that broke `databricks auth login`, `databricks aitools install` and `databricks aitools auto-install disable` for the user, and could install the tools as root. ([#7828](https://github.com/databricks/cli/pull/7828)) diff --git a/cmd/aitools/autoinstall_run.go b/cmd/aitools/autoinstall_run.go index c071ab29b96..8fe979938e9 100644 --- a/cmd/aitools/autoinstall_run.go +++ b/cmd/aitools/autoinstall_run.go @@ -49,6 +49,9 @@ const ( // gateOn is autoinstall.GateOn; tests replace it. var gateOn = autoinstall.GateOn +// rootInUserHome is autoinstall.RootInUserHome; tests replace it. +var rootInUserHome = autoinstall.RootInUserHome + // notifyInterrupt returns a context that is canceled when the CLI receives // Ctrl+C or a request to terminate; tests replace it. var notifyInterrupt = func(ctx context.Context) (context.Context, context.CancelFunc) { @@ -102,13 +105,19 @@ func autoInstall(ctx context.Context, skippedCommand bool) { OnRuntime: dbr.RunsOnRuntime(ctx), CacheDisabled: autoinstall.CacheDisabled(ctx), Version: version, + RootInUserHome: rootInUserHome(ctx), CI: agents.InCI(ctx), // A "no" earlier in this process counts even if storing it failed. EnvOptOut: autoinstall.EnvOptedOut(ctx) || autoinstall.Declined(), } // What never auto-installs skips before the state is read, and without a - // log line: these are most runs. - if slices.Contains(stateFreeSkips, autoinstall.Decide(in)) { + // log line: these are most runs. Root in a user's home is rare, and a user + // who always runs the CLI under sudo needs a way to see why nothing + // installs, so it logs. + if reason := autoinstall.Decide(in); slices.Contains(stateFreeSkips, reason) { + if reason == autoinstall.ReasonRootInUserHome { + log.Debugf(ctx, "auto-install: skipped: %s", reason) + } return } var err error @@ -403,6 +412,7 @@ var stateFreeSkips = []autoinstall.Reason{ autoinstall.ReasonOnRuntime, autoinstall.ReasonCacheDisabled, autoinstall.ReasonDevelopmentBuild, + autoinstall.ReasonRootInUserHome, } // skipOutcomes are the reported outcomes of the skips that settle a version. diff --git a/cmd/aitools/autoinstall_run_test.go b/cmd/aitools/autoinstall_run_test.go index f0393e4c23f..376fa91ebe4 100644 --- a/cmd/aitools/autoinstall_run_test.go +++ b/cmd/aitools/autoinstall_run_test.go @@ -365,6 +365,51 @@ func TestAutoInstallSkipsTheGateLookupWithoutAgents(t *testing.T) { assert.Equal(t, autoinstall.State{ID: "treatment-machine"}, state, "the version is not settled") } +// Root running in a user's home (sudo with HOME kept) leaves the home alone: +// it takes no lock, reads and writes no state, looks no gate up and installs +// nothing, so the user's own runs can still write there. The same run as the +// user installs. +func TestAutoInstallSkipsRootInAUserHome(t *testing.T) { + t.Run("root in a user's home", func(t *testing.T) { + ctx, home, lookups := runRootInUserHome(t, true) + assert.NoDirExists(t, filepath.Join(home, ".databricks")) + assert.NoDirExists(t, filepath.Join(home, ".cursor", "skills")) + assert.Zero(t, *lookups) + assert.Empty(t, telemetry.BufferedEvents(ctx)) + }) + t.Run("the user", func(t *testing.T) { + ctx, home, lookups := runRootInUserHome(t, false) + assert.FileExists(t, filepath.Join(home, ".cursor", "skills", "databricks-test", "SKILL.md")) + assert.Equal(t, 1, *lookups) + state, err := autoinstall.Load(ctx) + require.NoError(t, err) + assert.Equal(t, autoinstall.State{LastVersion: "1.30.0", ID: "treatment-machine"}, state) + }) +} + +// runRootInUserHome runs the hook in a fresh home, as root in a user's home +// when rootInHome is set, and counts the gate lookups. +func runRootInUserHome(t *testing.T, rootInHome bool) (ctx context.Context, home string, lookups *int) { + home = setupAutoInstallHome(t, "") + if rootInHome { + // The state the setup seeds would hide a state file or lock file + // the run creates. + require.NoError(t, os.RemoveAll(filepath.Join(home, ".databricks"))) + } + t.Cleanup(func() { rootInUserHome = autoinstall.RootInUserHome }) + rootInUserHome = func(context.Context) bool { return rootInHome } + lookups = new(int) + t.Cleanup(func() { gateOn = autoinstall.GateOn }) + gateOn = func(context.Context, string) (bool, error) { + *lookups++ + return true, nil + } + ctx = cmdio.MockDiscard(t.Context()) + ctx = telemetry.WithNewLogger(ctx) + runAutoInstallHook(ctx) + return ctx, home, lookups +} + func TestToolsPlanNewAgents(t *testing.T) { claude := &agents.Agent{DisplayName: "Claude Code"} cursor := &agents.Agent{DisplayName: "Cursor"} diff --git a/libs/aitools/autoinstall/decide.go b/libs/aitools/autoinstall/decide.go index 0b4d2832a48..e782a593986 100644 --- a/libs/aitools/autoinstall/decide.go +++ b/libs/aitools/autoinstall/decide.go @@ -25,6 +25,7 @@ const ( ReasonOnRuntime Reason = "on_runtime" ReasonCacheDisabled Reason = "cache_disabled" ReasonDevelopmentBuild Reason = "development_build" + ReasonRootInUserHome Reason = "root_in_user_home" ReasonAlreadyRan Reason = "already_ran" ReasonRetriesExhausted Reason = "retries_exhausted" @@ -43,7 +44,7 @@ const ( // settles the version. A run that is not due changes nothing. func (r Reason) Due() bool { switch r { - case ReasonSkippedCommand, ReasonOnRuntime, ReasonCacheDisabled, ReasonDevelopmentBuild, ReasonAlreadyRan, ReasonRetriesExhausted: + case ReasonSkippedCommand, ReasonOnRuntime, ReasonCacheDisabled, ReasonDevelopmentBuild, ReasonRootInUserHome, ReasonAlreadyRan, ReasonRetriesExhausted: return false case ReasonSkippedCI, ReasonSkippedOptOut, ReasonGateOff, ReasonSkippedHoldout, ReasonRun: return true @@ -66,6 +67,10 @@ type Inputs struct { // Version is the running CLI version. Version string + // RootInUserHome is set when the CLI runs as root in a home directory + // that another user owns (see RootInUserHome). + RootInUserHome bool + // State is the persisted state: last version, attempts and opt-out. State State @@ -100,6 +105,10 @@ func Decide(in Inputs) Reason { // they would trigger on every build. case build.IsDevelopmentVersion(in.Version): return ReasonDevelopmentBuild + // Root under sudo with HOME kept would leave files the user cannot + // change in the user's home. + case in.RootInUserHome: + return ReasonRootInUserHome case in.State.LastVersion == in.Version && in.State.Attempts >= MaxAttempts: return ReasonRetriesExhausted case in.State.LastVersion == in.Version && in.State.Attempts == 0: diff --git a/libs/aitools/autoinstall/decide_test.go b/libs/aitools/autoinstall/decide_test.go index 352f78637b1..d0aa08c26fb 100644 --- a/libs/aitools/autoinstall/decide_test.go +++ b/libs/aitools/autoinstall/decide_test.go @@ -26,6 +26,12 @@ func TestDecide(t *testing.T) { {name: "cache disabled", modify: func(in *Inputs) { in.CacheDisabled = true }, want: ReasonCacheDisabled}, {name: "development build", modify: func(in *Inputs) { in.Version = "1.31.0-dev+abcdef123456" }, want: ReasonDevelopmentBuild}, {name: "snapshot build", modify: func(in *Inputs) { in.Version = "1.30.1-dev+abcdef1" }, want: ReasonDevelopmentBuild}, + {name: "root in a user's home", modify: func(in *Inputs) { in.RootInUserHome = true }, want: ReasonRootInUserHome}, + {name: "root in a user's home before the state", modify: func(in *Inputs) { + in.RootInUserHome = true + in.State = State{LastVersion: "1.30.0", Attempts: MaxAttempts} + in.CI = true + }, want: ReasonRootInUserHome}, {name: "same version, settled", modify: func(in *Inputs) { in.State.LastVersion = "1.30.0" }, want: ReasonAlreadyRan}, {name: "same version, failed before", modify: func(in *Inputs) { in.State = State{LastVersion: "1.30.0", Attempts: 3} }, want: ReasonRun}, @@ -65,7 +71,7 @@ func TestDecide(t *testing.T) { } func TestReasonDue(t *testing.T) { - notDue := []Reason{ReasonSkippedCommand, ReasonOnRuntime, ReasonCacheDisabled, ReasonDevelopmentBuild, ReasonAlreadyRan, ReasonRetriesExhausted} + notDue := []Reason{ReasonSkippedCommand, ReasonOnRuntime, ReasonCacheDisabled, ReasonDevelopmentBuild, ReasonRootInUserHome, ReasonAlreadyRan, ReasonRetriesExhausted} due := []Reason{ReasonSkippedCI, ReasonSkippedOptOut, ReasonGateOff, ReasonSkippedHoldout, ReasonRun} for _, r := range notDue { assert.False(t, r.Due(), r) diff --git a/libs/aitools/autoinstall/home_unix.go b/libs/aitools/autoinstall/home_unix.go new file mode 100644 index 00000000000..f935baf4e2f --- /dev/null +++ b/libs/aitools/autoinstall/home_unix.go @@ -0,0 +1,38 @@ +//go:build unix + +package autoinstall + +import ( + "context" + "os" + + "github.com/databricks/cli/libs/env" + "golang.org/x/sys/unix" +) + +// RootInUserHome reports whether the CLI runs as root in a home directory +// that another user owns, as under sudo with HOME kept (the macOS default, +// and `sudo -E` on Linux). What auto-install writes there, its state and the +// agents' files, would belong to root, and the user could no longer change +// it. A home that cannot be inspected counts as not. Only the home is +// checked: an agent directory set through an environment variable, such as +// CLAUDE_CONFIG_DIR kept by `sudo -E -H`, is out of scope. +func RootInUserHome(ctx context.Context) bool { + return rootInUserHome(ctx, os.Geteuid()) +} + +// rootInUserHome is RootInUserHome for the effective user ID euid. +func rootInUserHome(ctx context.Context, euid int) bool { + if euid != 0 { + return false + } + home, err := env.UserHomeDir(ctx) + if err != nil { + return false + } + var st unix.Stat_t + if err := unix.Stat(home, &st); err != nil { + return false + } + return st.Uid != 0 +} diff --git a/libs/aitools/autoinstall/home_unix_test.go b/libs/aitools/autoinstall/home_unix_test.go new file mode 100644 index 00000000000..0967326737d --- /dev/null +++ b/libs/aitools/autoinstall/home_unix_test.go @@ -0,0 +1,52 @@ +//go:build unix + +package autoinstall + +import ( + "os" + "path/filepath" + "testing" + + "github.com/databricks/cli/libs/env" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "golang.org/x/sys/unix" +) + +func TestRootInUserHome(t *testing.T) { + userHome := t.TempDir() + tests := []struct { + name string + euid int + home string + }{ + {name: "the user in their home", euid: os.Geteuid(), home: userHome}, + {name: "another user in the home", euid: os.Geteuid() + 1, home: userHome}, + {name: "root with a home that does not exist", euid: 0, home: filepath.Join(userHome, "missing")}, + {name: "root without a home", euid: 0, home: ""}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + ctx := env.WithUserHomeDir(t.Context(), tc.home) + assert.False(t, rootInUserHome(ctx, tc.euid)) + }) + } +} + +func TestRootInUserHomeOwnedByRoot(t *testing.T) { + var st unix.Stat_t + require.NoError(t, unix.Stat("/", &st)) + if st.Uid != 0 { + t.Skip("the root directory is not owned by root, as in a user namespace that does not map root") + } + ctx := env.WithUserHomeDir(t.Context(), "/") + assert.False(t, rootInUserHome(ctx, 0)) +} + +func TestRootInUserHomeOwnedByAnotherUser(t *testing.T) { + if os.Geteuid() == 0 { + t.Skip("a home the test creates is owned by root") + } + ctx := env.WithUserHomeDir(t.Context(), t.TempDir()) + assert.True(t, rootInUserHome(ctx, 0)) +} diff --git a/libs/aitools/autoinstall/home_windows.go b/libs/aitools/autoinstall/home_windows.go new file mode 100644 index 00000000000..db826d8df27 --- /dev/null +++ b/libs/aitools/autoinstall/home_windows.go @@ -0,0 +1,12 @@ +//go:build windows + +package autoinstall + +import "context" + +// RootInUserHome reports whether the CLI runs as root in a home directory +// that another user owns. Windows has no root user that the CLI switches to: +// an elevated process keeps the user's own profile, so it reports false. +func RootInUserHome(context.Context) bool { + return false +}