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
1 change: 1 addition & 0 deletions .nextchanges/cli/aitools-auto-install-sudo-home.md
Original file line number Diff line number Diff line change
@@ -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))
14 changes: 12 additions & 2 deletions cmd/aitools/autoinstall_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
45 changes: 45 additions & 0 deletions cmd/aitools/autoinstall_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
11 changes: 10 additions & 1 deletion libs/aitools/autoinstall/decide.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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:
Expand Down
8 changes: 7 additions & 1 deletion libs/aitools/autoinstall/decide_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Comment thread
anton-107 marked this conversation as resolved.
Resolved

{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},
Expand Down Expand Up @@ -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)
Expand Down
38 changes: 38 additions & 0 deletions libs/aitools/autoinstall/home_unix.go
Original file line number Diff line number Diff line change
@@ -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)
Comment thread
anton-107 marked this conversation as resolved.
Resolved
if err != nil {
return false
}
var st unix.Stat_t
if err := unix.Stat(home, &st); err != nil {
return false
}
return st.Uid != 0
}
52 changes: 52 additions & 0 deletions libs/aitools/autoinstall/home_unix_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
12 changes: 12 additions & 0 deletions libs/aitools/autoinstall/home_windows.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading