From 68233ed45013f6c1cb3e98d7987f9f4783069082 Mon Sep 17 00:00:00 2001 From: Chen Shou Date: Thu, 12 Nov 2026 22:46:09 +0000 Subject: [PATCH] Record the profile auth docker configure selects for Docker auth docker configure no longer fails when several profiles match the workspace. It marks the selected profile with docker_credential_helper = true in .databrickscfg, clears the mark from the workspace's other profiles, and warns when the selection switches. auth docker token uses the recorded profile when several match, and auth docker host reports the helper as configured only if Docker's config selects it and it uses that profile. Saving a profile with a different host or workspace ID drops its mark, so it cannot follow the profile to another workspace. Co-authored-by: Isaac --- .../cli/docker-shared-workspace-profiles.md | 1 + .../auth/docker/configure-errors/output.txt | 5 - .../cmd/auth/docker/configure-errors/script | 8 - .../cmd/auth/docker/configure/output.txt | 56 +++++- acceptance/cmd/auth/docker/configure/script | 17 ++ .../auth/docker/configure/token-cache.json | 17 ++ acceptance/cmd/auth/docker/help/output.txt | 17 +- acceptance/cmd/auth/docker/token/output.txt | 15 +- acceptance/cmd/auth/docker/token/script | 16 ++ .../cmd/auth/docker/token/token-cache.json | 17 ++ cmd/auth/docker/docker_configure.go | 34 +++- cmd/auth/docker/docker_host.go | 7 +- cmd/auth/docker/docker_profile.go | 4 + libs/databrickscfg/ops.go | 34 ++++ libs/databrickscfg/ops_test.go | 144 +++++++++++++++ libs/databrickscfg/profile/file.go | 1 + libs/databrickscfg/profile/file_test.go | 24 +++ libs/databrickscfg/profile/profile.go | 9 + libs/dockercredentials/configure.go | 15 +- libs/dockercredentials/configure_test.go | 51 +++++- libs/dockercredentials/profile.go | 67 ++++--- libs/dockercredentials/profile_test.go | 173 ++++++++++++------ 22 files changed, 616 insertions(+), 116 deletions(-) create mode 100644 .nextchanges/cli/docker-shared-workspace-profiles.md create mode 100644 acceptance/cmd/auth/docker/configure/token-cache.json create mode 100644 acceptance/cmd/auth/docker/token/token-cache.json diff --git a/.nextchanges/cli/docker-shared-workspace-profiles.md b/.nextchanges/cli/docker-shared-workspace-profiles.md new file mode 100644 index 00000000000..a76c03d1126 --- /dev/null +++ b/.nextchanges/cli/docker-shared-workspace-profiles.md @@ -0,0 +1 @@ +* Allow `databricks auth docker configure` when multiple profiles match the same workspace. The command records the selected profile in `.databrickscfg`, and the Docker credential helper uses it; run the command with another profile to switch. ([#7383](https://github.com/databricks/cli/pull/7383)) diff --git a/acceptance/cmd/auth/docker/configure-errors/output.txt b/acceptance/cmd/auth/docker/configure-errors/output.txt index c3083199390..0db9fa72ed0 100644 --- a/acceptance/cmd/auth/docker/configure-errors/output.txt +++ b/acceptance/cmd/auth/docker/configure-errors/output.txt @@ -45,10 +45,5 @@ Error: profile "m2m" uses client credentials. Docker credential helper requires >>> musterr [CLI] auth docker configure blank-auth Error: profile "blank-auth" uses auth_type "". Docker credential helper requires a profile created by databricks auth login -=== Another profile already maps to the workspace - ->>> musterr [CLI] auth docker configure docker-test -Error: multiple Databricks profiles match workspace ID [WORKSPACE_ID]: duplicate and docker-test. Docker credential helper requires exactly one profile per workspace. Remove the duplicate profile with: databricks auth logout --profile --delete - === Nothing changed Docker config not created; profiles unchanged diff --git a/acceptance/cmd/auth/docker/configure-errors/script b/acceptance/cmd/auth/docker/configure-errors/script index da8890f5abe..9fc93fa483d 100644 --- a/acceptance/cmd/auth/docker/configure-errors/script +++ b/acceptance/cmd/auth/docker/configure-errors/script @@ -11,11 +11,6 @@ cat >> "$HOME/.databrickscfg" <>> databricks auth docker host --profile docker-test --output json { @@ -154,3 +155,52 @@ Warning: ensure [TEST_TMP_DIR]/home/bin is on PATH before any other docker-crede "method": "GET", "path": "/api/2.1/unity-catalog/metastore_summary" } + +=== Profiles sharing the workspace + +>>> databricks auth docker configure shared +Warning: Docker credential helper for [WORKSPACE_ID].container.us-west-2.localhost switched from profile docker-test to shared +Configured Docker credential helper for [WORKSPACE_ID].container.us-west-2.localhost +Updated Docker config: [TEST_TMP_DIR]/home/docker/config.json +Installed Docker credential helper: [TEST_TMP_DIR]/home/bin/docker-credential-databricks + +>>> databricks auth docker host --profile docker-test +Registry host: [WORKSPACE_ID].container.us-west-2.localhost +Credential helper configured: NO + +>>> databricks auth docker host --profile shared +Registry host: [WORKSPACE_ID].container.us-west-2.localhost +Credential helper configured: YES + +>>> databricks auth docker configure docker-test +Warning: Docker credential helper for [WORKSPACE_ID].container.us-west-2.localhost switched from profile shared to docker-test +Configured Docker credential helper for [WORKSPACE_ID].container.us-west-2.localhost +Updated Docker config: [TEST_TMP_DIR]/home/docker/config.json +Installed Docker credential helper: [TEST_TMP_DIR]/home/bin/docker-credential-databricks + +>>> print_requests.py --get --oneline //api +{"headers": {"X-Databricks-Workspace-Id": ["[WORKSPACE_ID]"]}, "method": "GET", "path": "/api/2.1/unity-catalog/metastore_summary"} +{"headers": {"X-Databricks-Workspace-Id": ["[WORKSPACE_ID]"]}, "method": "GET", "path": "/api/2.1/unity-catalog/metastore_summary"} +{"headers": {"X-Databricks-Workspace-Id": ["[WORKSPACE_ID]"]}, "method": "GET", "path": "/api/2.1/unity-catalog/metastore_summary"} +{"headers": {"X-Databricks-Workspace-Id": ["[WORKSPACE_ID]"]}, "method": "GET", "path": "/api/2.1/unity-catalog/metastore_summary"} + +>>> cat [TEST_TMP_DIR]/home/.databrickscfg +; The profile defined in the DEFAULT section is to be used as a fallback when no profile is explicitly specified. +[DEFAULT] + +[docker-test] +host = [DATABRICKS_URL] +auth_type = databricks-cli +workspace_id = [WORKSPACE_ID] +docker_credential_helper = true + +[shared] +host = [DATABRICKS_URL] +auth_type = databricks-cli +workspace_id = [WORKSPACE_ID] + +>>> docker-credential-databricks get +{ + "Secret": "oauth-token", + "Username": "oauthtoken" +} diff --git a/acceptance/cmd/auth/docker/configure/script b/acceptance/cmd/auth/docker/configure/script index 8df314ca720..98c89f0ba36 100644 --- a/acceptance/cmd/auth/docker/configure/script +++ b/acceptance/cmd/auth/docker/configure/script @@ -63,3 +63,20 @@ trace print_requests.py --get //api title "Configure with the installation directory absent from PATH" PATH="$path_without_cli" trace command "$HOME/bin/databricks" auth docker configure docker-test trace print_requests.py --get //api + +title "Profiles sharing the workspace" +cat >> "$HOME/.databrickscfg" <>> [CLI] auth docker host --help (Experimental) Show the Databricks Artifact Registry host and Docker credential helper status for a profile. -The --profile flag is required. +The credential helper counts as configured only if Docker's config.json selects +it for the registry and the helper uses this profile. The --profile flag is +required. Usage: databricks auth docker host [flags] diff --git a/acceptance/cmd/auth/docker/token/output.txt b/acceptance/cmd/auth/docker/token/output.txt index 71126dce314..9b0dd1c654e 100644 --- a/acceptance/cmd/auth/docker/token/output.txt +++ b/acceptance/cmd/auth/docker/token/output.txt @@ -54,4 +54,17 @@ Error: no Databricks profile found for workspace ID 444555666 from registry host === Multiple profiles for the workspace >>> musterr [CLI] auth docker token -Error: multiple Databricks profiles match workspace ID [WORKSPACE_ID]: docker-test and duplicate. Docker credential helper requires exactly one profile per workspace. Remove the duplicate profile with: databricks auth logout --profile --delete +Error: multiple Databricks profiles match workspace ID [WORKSPACE_ID]: docker-test, duplicate. Select the profile for Docker with: databricks auth docker configure + +=== Recorded profile for the workspace + +>>> [CLI] auth docker token --no-force-refresh +{ + "Secret": "duplicate-access-token", + "Username": "oauthtoken" +} + +=== Several recorded profiles for the workspace + +>>> musterr [CLI] auth docker token +Error: multiple Databricks profiles match workspace ID [WORKSPACE_ID]: duplicate, also-recorded. Select the profile for Docker with: databricks auth docker configure diff --git a/acceptance/cmd/auth/docker/token/script b/acceptance/cmd/auth/docker/token/script index 154eab1406e..25a057bd1d5 100644 --- a/acceptance/cmd/auth/docker/token/script +++ b/acceptance/cmd/auth/docker/token/script @@ -46,3 +46,19 @@ auth_type = databricks-cli workspace_id = 900800700600 ENDCFG printf '%s\n' "$registry_host" | trace musterr $CLI auth docker token + +title "Recorded profile for the workspace" +cp token-cache.json "$HOME/.databricks/token-cache.json" +update_file.py "$HOME/.databrickscfg" "[duplicate]" $'[duplicate]\ndocker_credential_helper = true' +printf '%s\n' "$registry_host" | trace $CLI auth docker token --no-force-refresh | jq -S . + +title "Several recorded profiles for the workspace" +cat >> "$HOME/.databrickscfg" < 0 { + label := "profile" + if len(previous) > 1 { + label = "profiles" + } + cmdio.LogString(ctx, fmt.Sprintf("Warning: Docker credential helper for %s switched from %s %s to %s", t.registryHost, label, strings.Join(previous, ", "), t.profile.Name)) + } + return result, nil } func (t *dockerTarget) logConfigured(ctx context.Context, result dockercredentials.Configuration) { diff --git a/cmd/auth/docker/docker_host.go b/cmd/auth/docker/docker_host.go index e26bd1f14f1..d91ed49780c 100644 --- a/cmd/auth/docker/docker_host.go +++ b/cmd/auth/docker/docker_host.go @@ -5,6 +5,7 @@ import ( "strings" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/databrickscfg/profile" "github.com/databricks/cli/libs/dockercredentials" "github.com/spf13/cobra" ) @@ -15,7 +16,9 @@ func newDockerHostCommand() *cobra.Command { Short: "(Experimental) Show the registry host and credential helper status for a profile", Long: `(Experimental) Show the Databricks Artifact Registry host and Docker credential helper status for a profile. -The --profile flag is required.`, +The credential helper counts as configured only if Docker's config.json selects +it for the registry and the helper uses this profile. The --profile flag is +required.`, Example: " databricks auth docker host --profile DEFAULT", Args: cobra.NoArgs, Annotations: map[string]string{ @@ -30,7 +33,7 @@ The --profile flag is required.`, if err != nil { return err } - status, err := dockercredentials.Inspect(cmd.Context(), target.registryHost) + status, err := dockercredentials.Inspect(cmd.Context(), profile.DefaultProfiler, target.registry(), target.profile.Name) if err != nil { return err } diff --git a/cmd/auth/docker/docker_profile.go b/cmd/auth/docker/docker_profile.go index c616157cf33..001197dc9a3 100644 --- a/cmd/auth/docker/docker_profile.go +++ b/cmd/auth/docker/docker_profile.go @@ -107,3 +107,7 @@ func (w *dockerWorkspace) target(ctx context.Context, explicitRegion *string) (* executable: w.executable, }, nil } + +func (t *dockerTarget) registry() dockercredentials.Registry { + return dockercredentials.Registry{WorkspaceID: t.workspaceID, Host: t.registryHost} +} diff --git a/libs/databrickscfg/ops.go b/libs/databrickscfg/ops.go index 2d6faf1ba9c..1a544699295 100644 --- a/libs/databrickscfg/ops.go +++ b/libs/databrickscfg/ops.go @@ -9,6 +9,7 @@ import ( "strings" "sync" + "github.com/databricks/cli/libs/databrickscfg/profile" "github.com/databricks/cli/libs/env" "github.com/databricks/cli/libs/log" "github.com/databricks/databricks-sdk-go/config" @@ -320,6 +321,30 @@ func SaveResourcesToProfile(ctx context.Context, profileName, configFilePath str return writeConfigFile(ctx, configFile) } +// SetDockerCredentialHelperProfile marks profileName as the Docker credential helper profile and unmarks the profiles in previous. +func SetDockerCredentialHelperProfile(ctx context.Context, profileName, configFilePath string, previous []string) error { + configFile, err := loadOrCreateConfigFile(ctx, configFilePath) + if err != nil { + return err + } + section, err := configFile.GetSection(profileName) + if err != nil { + return fmt.Errorf("profile %q not found: %w", profileName, err) + } + for _, name := range previous { + previousSection, err := configFile.GetSection(name) + if err != nil { + return fmt.Errorf("profile %q not found: %w", name, err) + } + previousSection.DeleteKey(profile.DockerCredentialHelperKey) + } + // See SaveToProfile for why this is not Section.Key. + if _, err := section.NewKey(profile.DockerCredentialHelperKey, profile.DockerCredentialHelperValue); err != nil { + return err + } + return writeConfigFile(ctx, configFile) +} + // ClearDefaultProfile removes the default_profile key from the [__settings__] // section if the current default matches the given profile name. func ClearDefaultProfile(ctx context.Context, profileName, configFilePath string) error { @@ -481,6 +506,7 @@ func SaveToProfile(ctx context.Context, cfg *config.Config, clearKeys ...string) if err != nil { return err } + previous := section.KeysHash() // zeroval profile name before adding it to a section cfg.Profile = "" @@ -505,6 +531,14 @@ func SaveToProfile(ctx context.Context, cfg *config.Config, clearKeys ...string) } } + // The Docker credential helper mark applies to the profile's workspace, so it must not survive a change of host or workspace. + current := section.KeysHash() + hostChanged := previous["host"] != "" && (&config.Config{Host: current["host"]}).CanonicalHostName() != (&config.Config{Host: previous["host"]}).CanonicalHostName() + workspaceChanged := previous["workspace_id"] != "" && current["workspace_id"] != previous["workspace_id"] + if hostChanged || workspaceChanged { + section.DeleteKey(profile.DockerCredentialHelperKey) + } + // Auto-set default profile when saving the first profile to the config file. if firstProfile && profileName != "" { settingsSection := configFile.Section(databricksSettingsSection) diff --git a/libs/databrickscfg/ops_test.go b/libs/databrickscfg/ops_test.go index c5ebf13ab69..49b91359c99 100644 --- a/libs/databrickscfg/ops_test.go +++ b/libs/databrickscfg/ops_test.go @@ -5,6 +5,7 @@ import ( "path/filepath" "testing" + "github.com/databricks/cli/libs/databrickscfg/profile" "github.com/databricks/cli/libs/env" "github.com/databricks/databricks-sdk-go/config" "github.com/stretchr/testify/assert" @@ -251,6 +252,149 @@ func TestSaveResourcesToProfile_MissingProfile(t *testing.T) { assert.ErrorContains(t, err, `profile "does-not-exist" not found`) } +func TestSetDockerCredentialHelperProfile(t *testing.T) { + tests := []struct { + name string + content string + selected string + previous []string + want []string + wantErr string + }{ + { + name: "marks selected profile", + content: "[prod]\nhost = https://prod.test\n", + selected: "prod", + want: []string{"prod"}, + }, + { + name: "unmarks previous profiles", + content: "[dev]\nhost = https://ws.test\ndocker_credential_helper = true\n\n[prod]\nhost = https://ws.test\n\n[stage]\nhost = https://ws.test\ndocker_credential_helper = true\n", + selected: "prod", + previous: []string{"dev", "stage"}, + want: []string{"prod"}, + }, + { + name: "dotted profile name does not change parent section", + content: "[team]\nhost = https://team.test\ndocker_credential_helper = true\n\n[team.prod]\nhost = https://prod.test\n", + selected: "team.prod", + want: []string{"team", "team.prod"}, + }, + { + name: "missing profile", + content: "[prod]\nhost = https://prod.test\n", + selected: "does-not-exist", + wantErr: `profile "does-not-exist" not found`, + }, + { + name: "missing previous profile", + content: "[prod]\nhost = https://prod.test\n", + selected: "prod", + previous: []string{"does-not-exist"}, + wantErr: `profile "does-not-exist" not found`, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + path := filepath.Join(t.TempDir(), "databrickscfg") + require.NoError(t, os.WriteFile(path, []byte(tt.content), 0o600)) + ctx := env.Set(t.Context(), "DATABRICKS_CONFIG_FILE", path) + + err := SetDockerCredentialHelperProfile(ctx, tt.selected, path, tt.previous) + if tt.wantErr != "" { + assert.ErrorContains(t, err, tt.wantErr) + return + } + require.NoError(t, err) + + profiles, err := profile.DefaultProfiler.LoadProfiles(ctx, func(p profile.Profile) bool { + return p.DockerCredentialHelper + }) + require.NoError(t, err) + assert.Equal(t, tt.want, profiles.Names()) + }) + } +} + +func TestSaveToProfileDockerCredentialHelperMark(t *testing.T) { + tests := []struct { + name string + content string + host string + workspaceID string + clearKeys []string + want bool + }{ + { + name: "workspace changes", + content: "[prod]\nhost = https://prod.test\nworkspace_id = 1111\ndocker_credential_helper = true\n", + host: "https://dev.test", + workspaceID: "2222", + }, + { + name: "workspace cleared", + content: "[prod]\nhost = https://prod.test\nworkspace_id = 1111\ndocker_credential_helper = true\n", + host: "https://prod.test", + clearKeys: []string{"workspace_id"}, + }, + { + name: "workspace cleared and saved again", + content: "[prod]\nhost = https://prod.test\nworkspace_id = 1111\ndocker_credential_helper = true\n", + host: "https://prod.test", + workspaceID: "1111", + clearKeys: []string{"workspace_id"}, + want: true, + }, + { + name: "same workspace", + content: "[prod]\nhost = https://prod.test\nworkspace_id = 1111\ndocker_credential_helper = true\n", + host: "https://prod.test", + workspaceID: "1111", + want: true, + }, + { + name: "host changes and workspace ID is kept", + content: "[prod]\nhost = https://prod.test\nworkspace_id = 1111\ndocker_credential_helper = true\n", + host: "https://dev.test", + workspaceID: "1111", + }, + { + name: "host written in another form", + content: "[prod]\nhost = https://prod.test/\nworkspace_id = 1111\ndocker_credential_helper = true\n", + host: "https://prod.test", + want: true, + }, + { + name: "workspace filled in", + content: "[prod]\nhost = https://prod.test\ndocker_credential_helper = true\n", + host: "https://prod.test", + workspaceID: "1111", + want: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + path := filepath.Join(t.TempDir(), "databrickscfg") + require.NoError(t, os.WriteFile(path, []byte(tt.content), 0o600)) + ctx := env.Set(t.Context(), "DATABRICKS_CONFIG_FILE", path) + + require.NoError(t, SaveToProfile(ctx, &config.Config{ + ConfigFile: path, + Profile: "prod", + Host: tt.host, + WorkspaceID: tt.workspaceID, + }, tt.clearKeys...)) + + profiles, err := profile.DefaultProfiler.LoadProfiles(ctx, profile.WithName("prod")) + require.NoError(t, err) + require.Len(t, profiles, 1) + assert.Equal(t, tt.want, profiles[0].DockerCredentialHelper) + }) + } +} + func TestGetDefaultProfile(t *testing.T) { testCases := []struct { name string diff --git a/libs/databrickscfg/profile/file.go b/libs/databrickscfg/profile/file.go index 0844794df3a..6a6e1a04cde 100644 --- a/libs/databrickscfg/profile/file.go +++ b/libs/databrickscfg/profile/file.go @@ -91,6 +91,7 @@ func (f FileProfilerImpl) LoadProfiles(ctx context.Context, fn ProfileMatchFunct Resources: all["resources"], AuthType: all["auth_type"], } + profile.DockerCredentialHelper = all[DockerCredentialHelperKey] == DockerCredentialHelperValue if fn(profile) { profiles = append(profiles, profile) } diff --git a/libs/databrickscfg/profile/file_test.go b/libs/databrickscfg/profile/file_test.go index 82243842a94..af08386388c 100644 --- a/libs/databrickscfg/profile/file_test.go +++ b/libs/databrickscfg/profile/file_test.go @@ -98,6 +98,30 @@ resources = https://workspace.test/ai-gateway/mcp/system.ai.github,https://works assert.Equal(t, "https://workspace.test/ai-gateway/mcp/system.ai.github,https://workspace.test/ai-gateway/mcp/system.ai.slack", profiles[0].Resources) } +func TestLoadProfilesDockerCredentialHelper(t *testing.T) { + configPath := filepath.Join(t.TempDir(), ".databrickscfg") + err := os.WriteFile(configPath, []byte(`[selected] +host = https://workspace.test +docker_credential_helper = true + +[unselected] +host = https://workspace.test + +[invalid] +host = https://workspace.test +docker_credential_helper = yes +`), 0o600) + require.NoError(t, err) + + ctx := env.Set(t.Context(), "DATABRICKS_CONFIG_FILE", configPath) + profiles, err := (FileProfilerImpl{}).LoadProfiles(ctx, MatchAllProfiles) + require.NoError(t, err) + require.Equal(t, []string{"selected", "unselected", "invalid"}, profiles.Names()) + assert.True(t, profiles[0].DockerCredentialHelper) + assert.False(t, profiles[1].DockerCredentialHelper) + assert.False(t, profiles[2].DockerCredentialHelper) +} + func TestLoadProfilesMatchWorkspace(t *testing.T) { ctx := t.Context() ctx = env.Set(ctx, "DATABRICKS_CONFIG_FILE", "./testdata/databrickscfg") diff --git a/libs/databrickscfg/profile/profile.go b/libs/databrickscfg/profile/profile.go index d3627946ee5..df052442189 100644 --- a/libs/databrickscfg/profile/profile.go +++ b/libs/databrickscfg/profile/profile.go @@ -10,6 +10,13 @@ import ( // user explicitly skips workspace selection for SPOG account-level access. const WorkspaceIDNone = "none" +const ( + // DockerCredentialHelperKey marks the profile that the Docker credential helper uses for its workspace. + DockerCredentialHelperKey = "docker_credential_helper" + // DockerCredentialHelperValue is the value of DockerCredentialHelperKey on the marked profile. + DockerCredentialHelperValue = "true" +) + // Profile holds a subset of the keys in a databrickscfg profile. // It should only be used for prompting and filtering. // Use its name to construct a config.Config. @@ -25,6 +32,8 @@ type Profile struct { Scopes string Resources string AuthType string + + DockerCredentialHelper bool } func (p Profile) Cloud() string { diff --git a/libs/dockercredentials/configure.go b/libs/dockercredentials/configure.go index b567bc02a41..0affc35878e 100644 --- a/libs/dockercredentials/configure.go +++ b/libs/dockercredentials/configure.go @@ -6,7 +6,9 @@ import ( "os/exec" "path/filepath" + "github.com/databricks/cli/libs/databrickscfg/profile" "github.com/databricks/cli/libs/env" + "github.com/databricks/cli/libs/log" ) // Configuration describes the installed helper and the Docker configuration it updated. @@ -37,8 +39,9 @@ func Configure(ctx context.Context, executable, registryHost string) (Configurat return Configuration{ConfigPath: configPath, Shim: shim}, nil } -// Inspect checks the helper selection and executable availability without modifying Docker's configuration. -func Inspect(ctx context.Context, registryHost string) (Status, error) { +// Inspect reports whether Docker uses the credential helper with profileName for registry, without modifying any configuration. +func Inspect(ctx context.Context, profiler profile.Profiler, registry Registry, profileName string) (Status, error) { + registryHost := registry.Host configPath, err := ConfigPath(ctx) if err != nil { return Status{}, err @@ -51,6 +54,14 @@ func Inspect(ctx context.Context, registryHost string) (Status, error) { _, err := exec.LookPath("docker-credential-" + HelperName) configured = err == nil } + if configured { + // The helper cannot authenticate as profileName if it resolves the registry to another profile or to none. + selected, err := ProfileForRegistry(ctx, profiler, registry) + if err != nil { + log.Debugf(ctx, "Docker credential helper cannot resolve %s to a profile: %v", registryHost, err) + } + configured = err == nil && selected.Name == profileName + } return Status{Host: registryHost, Configured: configured}, nil } diff --git a/libs/dockercredentials/configure_test.go b/libs/dockercredentials/configure_test.go index 1f7362c40a3..b6c38e50ba2 100644 --- a/libs/dockercredentials/configure_test.go +++ b/libs/dockercredentials/configure_test.go @@ -5,12 +5,18 @@ import ( "path/filepath" "testing" + "github.com/databricks/cli/libs/databrickscfg/profile" "github.com/databricks/cli/libs/dockercredentials" "github.com/databricks/cli/libs/env" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) +var ( + inspectRegistry = dockercredentials.Registry{WorkspaceID: workspaceID, Host: "registry.test"} + inspectProfiler = profile.InMemoryProfiler{Profiles: profile.Profiles{workspaceProfile("workspace")}} +) + func TestConfigure(t *testing.T) { dir := t.TempDir() t.Setenv("PATH", filepath.Join(dir, "bin")) @@ -21,7 +27,7 @@ func TestConfigure(t *testing.T) { require.NoError(t, err) assert.Equal(t, configPath, result.ConfigPath) assert.FileExists(t, result.Shim.Path) - status, err := dockercredentials.Inspect(ctx, "registry.test") + status, err := dockercredentials.Inspect(ctx, inspectProfiler, inspectRegistry, "workspace") require.NoError(t, err) assert.True(t, status.Configured) assert.Equal(t, "registry.test", status.Host) @@ -55,7 +61,7 @@ func TestInspectRequiresHelperOnPath(t *testing.T) { require.NoError(t, err) } - status, err := dockercredentials.Inspect(ctx, "registry.test") + status, err := dockercredentials.Inspect(ctx, inspectProfiler, inspectRegistry, "workspace") require.NoError(t, err) assert.Equal(t, "registry.test", status.Host) assert.Equal(t, tc.want, status.Configured) @@ -63,6 +69,47 @@ func TestInspectRequiresHelperOnPath(t *testing.T) { } } +func TestInspectRequiresDockerConfigAndProfile(t *testing.T) { + recorded := workspaceProfile("prod") + recorded.DockerCredentialHelper = true + otherWorkspace := workspaceProfile("other") + otherWorkspace.WorkspaceID = "987654321" + tests := []struct { + name string + profiles profile.Profiles + selected string + skipDockerConfig bool + want bool + }{ + {name: "only profile", profiles: profile.Profiles{workspaceProfile("prod")}, selected: "prod", want: true}, + {name: "Docker config does not use the helper", profiles: profile.Profiles{recorded}, selected: "prod", skipDockerConfig: true}, + {name: "recorded profile", profiles: profile.Profiles{recorded, workspaceProfile("dev")}, selected: "prod", want: true}, + {name: "another profile is recorded", profiles: profile.Profiles{recorded, workspaceProfile("dev")}, selected: "dev"}, + {name: "no profile is recorded", profiles: profile.Profiles{workspaceProfile("prod"), workspaceProfile("dev")}, selected: "prod"}, + {name: "profile is for another workspace", profiles: profile.Profiles{workspaceProfile("prod"), otherWorkspace}, selected: "other"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + dir := t.TempDir() + binDir := filepath.Join(dir, "bin") + configPath := filepath.Join(dir, "docker", "config.json") + ctx := env.Set(t.Context(), "DOCKER_CONFIG", filepath.Dir(configPath)) + t.Setenv("PATH", binDir) + t.Setenv("PATHEXT", ".COM;.EXE;.BAT;.CMD") + _, err := dockercredentials.Configure(ctx, filepath.Join(binDir, "databricks"), "registry.test") + require.NoError(t, err) + if tt.skipDockerConfig { + require.NoError(t, os.Remove(configPath)) + } + + status, err := dockercredentials.Inspect(ctx, profile.InMemoryProfiler{Profiles: tt.profiles}, inspectRegistry, tt.selected) + require.NoError(t, err) + assert.Equal(t, tt.want, status.Configured) + }) + } +} + func TestConfigureInstallsShimBeforeUpdatingConfig(t *testing.T) { dir := t.TempDir() blocked := filepath.Join(dir, "file") diff --git a/libs/dockercredentials/profile.go b/libs/dockercredentials/profile.go index 9acdb84edbf..bce6766f0c2 100644 --- a/libs/dockercredentials/profile.go +++ b/libs/dockercredentials/profile.go @@ -6,7 +6,9 @@ import ( "strings" "github.com/databricks/cli/libs/auth" + "github.com/databricks/cli/libs/databrickscfg" "github.com/databricks/cli/libs/databrickscfg/profile" + "github.com/databricks/cli/libs/env" "github.com/databricks/databricks-sdk-go/config" ) @@ -50,35 +52,38 @@ func LoadProfile(ctx context.Context, profiler profile.Profiler, name string) (p return profiles[0], nil } -// EnsureUniqueProfile verifies that workspaceID resolves to only p. -func EnsureUniqueProfile(ctx context.Context, profiler profile.Profiler, p profile.Profile, workspaceID string) error { - matches, err := profiler.LoadProfiles(ctx, func(candidate profile.Profile) bool { - return candidate.WorkspaceID == workspaceID +// RecordProfile marks the named profile as the Docker credential helper profile for registry and returns the names of the profiles it unmarked. +func RecordProfile(ctx context.Context, registry Registry, name string) ([]string, error) { + // Unsupported profiles are included so a stale mark is cleared even while the profile is unusable. + recorded, err := profile.DefaultProfiler.LoadProfiles(ctx, func(p profile.Profile) bool { + return p.DockerCredentialHelper && matchesRegistry(p, registry) }) if err != nil { - return err - } - - if p.WorkspaceID == "" || p.WorkspaceID == profile.WorkspaceIDNone { - matches = append(matches, p) - } - var names []string - for _, candidate := range matches { - if ValidateProfile(candidate) == nil { - names = append(names, candidate.Name) + return nil, err + } + var previous []string + alreadyRecorded := false + for _, p := range recorded { + if p.Name == name { + alreadyRecorded = true + } else { + previous = append(previous, p.Name) } } - if len(names) <= 1 { - return nil + if alreadyRecorded && len(previous) == 0 { + return nil, nil } - - return duplicateProfilesError(workspaceID, names) + if err := databrickscfg.SetDockerCredentialHelperProfile(ctx, name, env.Get(ctx, "DATABRICKS_CONFIG_FILE"), previous); err != nil { + return nil, err + } + return previous, nil } // ProfileForRegistry returns the Docker credential helper profile for registry. +// A profile recorded by RecordProfile takes precedence, even when it is no longer supported. func ProfileForRegistry(ctx context.Context, profiler profile.Profiler, registry Registry) (profile.Profile, error) { workspaceProfiles, err := profiler.LoadProfiles(ctx, func(p profile.Profile) bool { - return p.WorkspaceID == registry.WorkspaceID && registry.ServesWorkspaceHost(p.Host) + return matchesRegistry(p, registry) }) if err != nil { return profile.Profile{}, err @@ -87,24 +92,38 @@ func ProfileForRegistry(ctx context.Context, profiler profile.Profiler, registry return profile.Profile{}, fmt.Errorf("no Databricks profile found for workspace ID %s from registry host %s. Run databricks auth login --host and set workspace_id for that profile", registry.WorkspaceID, registry.Host) } - var matchingProfiles profile.Profiles + var recorded, matchingProfiles profile.Profiles for _, p := range workspaceProfiles { + if p.DockerCredentialHelper { + recorded = append(recorded, p) + } if ValidateProfile(p) == nil { matchingProfiles = append(matchingProfiles, p) } } - switch len(matchingProfiles) { - case 0: + switch { + case len(recorded) == 1: + if err := ValidateProfile(recorded[0]); err != nil { + return profile.Profile{}, err + } + return recorded[0], nil + case len(recorded) > 1: + return profile.Profile{}, duplicateProfilesError(registry.WorkspaceID, recorded.Names()) + case len(matchingProfiles) == 0: return profile.Profile{}, ValidateProfile(workspaceProfiles[0]) - case 1: + case len(matchingProfiles) == 1: return matchingProfiles[0], nil default: return profile.Profile{}, duplicateProfilesError(registry.WorkspaceID, matchingProfiles.Names()) } } +func matchesRegistry(p profile.Profile, registry Registry) bool { + return p.WorkspaceID == registry.WorkspaceID && registry.ServesWorkspaceHost(p.Host) +} + func duplicateProfilesError(workspaceID string, names []string) error { - return fmt.Errorf("multiple Databricks profiles match workspace ID %s: %s. Docker credential helper requires exactly one profile per workspace. Remove the duplicate profile with: databricks auth logout --profile --delete", workspaceID, strings.Join(names, " and ")) + return fmt.Errorf("multiple Databricks profiles match workspace ID %s: %s. Select the profile for Docker with: databricks auth docker configure ", workspaceID, strings.Join(names, ", ")) } // RewriteProfileError adds Docker profile context to authentication errors. diff --git a/libs/dockercredentials/profile_test.go b/libs/dockercredentials/profile_test.go index 0586d7e1e7a..ef3158b2f7d 100644 --- a/libs/dockercredentials/profile_test.go +++ b/libs/dockercredentials/profile_test.go @@ -3,12 +3,15 @@ package dockercredentials_test import ( "errors" "fmt" + "os" + "path/filepath" "testing" "github.com/databricks/cli/libs/auth" "github.com/databricks/cli/libs/auth/u2m" "github.com/databricks/cli/libs/databrickscfg/profile" "github.com/databricks/cli/libs/dockercredentials" + "github.com/databricks/cli/libs/env" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -40,6 +43,20 @@ func workspaceProfile(name string) profile.Profile { } } +func recordedProfile(name string) profile.Profile { + p := workspaceProfile(name) + p.DockerCredentialHelper = true + return p +} + +func configSection(name, id string, recorded bool) string { + section := fmt.Sprintf("[%s]\nhost = %s\nauth_type = databricks-cli\nworkspace_id = %s\n", name, workspaceHost, id) + if recorded { + section += "docker_credential_helper = true\n" + } + return section + "\n" +} + func TestValidateProfile(t *testing.T) { tests := []struct { name string @@ -139,98 +156,146 @@ func TestLoadProfile(t *testing.T) { } } -func TestEnsureUniqueProfile(t *testing.T) { - selected := workspaceProfile("prod") +func TestProfileForRegistry(t *testing.T) { + registry := dockercredentials.Registry{ + WorkspaceID: workspaceID, + Host: "123456789.container.us-west-2.cloud.databricks.test", + } + m2m := profile.Profile{ + Name: "m2m", + Host: workspaceHost, + WorkspaceID: workspaceID, + HasClientCredentials: true, + } + recordedM2M := m2m + recordedM2M.DockerCredentialHelper = true + loopback := profile.Profile{ + Name: "loopback", + Host: "http://127.0.0.1:8080", + WorkspaceID: workspaceID, + AuthType: auth.AuthTypeDatabricksCli, + } tests := []struct { name string profiles profile.Profiles - selected profile.Profile + want string wantErr string }{ - {name: "single profile", profiles: profile.Profiles{selected}, selected: selected}, + {name: "selects workspace profile", profiles: profile.Profiles{workspaceProfile("workspace")}, want: "workspace"}, + {name: "ignores unsupported profile", profiles: profile.Profiles{m2m, workspaceProfile("workspace")}, want: "workspace"}, + {name: "ignores host the registry does not serve", profiles: profile.Profiles{loopback, workspaceProfile("workspace")}, want: "workspace"}, + {name: "selects recorded profile", profiles: profile.Profiles{workspaceProfile("dev"), recordedProfile("prod")}, want: "prod"}, { - name: "duplicate workspace ID", - profiles: profile.Profiles{selected, workspaceProfile("dev")}, - selected: selected, - wantErr: "multiple Databricks profiles match workspace ID 123456789: prod and dev. Docker credential helper requires exactly one profile per workspace. Remove the duplicate profile with: databricks auth logout --profile --delete", + name: "no recorded profile", + profiles: profile.Profiles{workspaceProfile("prod"), workspaceProfile("dev")}, + wantErr: "multiple Databricks profiles match workspace ID 123456789: prod, dev. Select the profile for Docker with: databricks auth docker configure ", }, { - name: "unsupported duplicate", - profiles: profile.Profiles{selected, { - Name: "m2m", - Host: workspaceHost, - WorkspaceID: workspaceID, - HasClientCredentials: true, - }}, - selected: selected, + name: "recorded profile is unsupported", + profiles: profile.Profiles{recordedM2M, workspaceProfile("workspace")}, + wantErr: `profile "m2m" uses client credentials. Docker credential helper requires a profile created by databricks auth login`, }, { - name: "resolved workspace ID appends selected profile", - profiles: profile.Profiles{selected}, - selected: profile.Profile{ - Name: "workspace", - Host: workspaceHost, - AuthType: auth.AuthTypeDatabricksCli, - }, - wantErr: "multiple Databricks profiles match workspace ID 123456789: prod and workspace. Docker credential helper requires exactly one profile per workspace. Remove the duplicate profile with: databricks auth logout --profile --delete", + name: "several recorded profiles", + profiles: profile.Profiles{recordedProfile("prod"), recordedProfile("dev")}, + wantErr: "multiple Databricks profiles match workspace ID 123456789: prod, dev. Select the profile for Docker with: databricks auth docker configure ", + }, + { + name: "only unsupported profiles", + profiles: profile.Profiles{m2m}, + wantErr: `profile "m2m" uses client credentials. Docker credential helper requires a profile created by databricks auth login`, + }, + { + name: "no matching profile", + wantErr: "no Databricks profile found for workspace ID 123456789 from registry host 123456789.container.us-west-2.cloud.databricks.test. Run databricks auth login --host and set workspace_id for that profile", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - err := dockercredentials.EnsureUniqueProfile(t.Context(), profile.InMemoryProfiler{Profiles: tt.profiles}, tt.selected, workspaceID) - if tt.wantErr == "" { - assert.NoError(t, err) + got, err := dockercredentials.ProfileForRegistry(t.Context(), profile.InMemoryProfiler{Profiles: tt.profiles}, registry) + if tt.wantErr != "" { + assert.EqualError(t, err, tt.wantErr) return } - assert.EqualError(t, err, tt.wantErr) + require.NoError(t, err) + assert.Equal(t, tt.want, got.Name) }) } } -func TestProfileForRegistry(t *testing.T) { +func TestRecordProfile(t *testing.T) { registry := dockercredentials.Registry{ WorkspaceID: workspaceID, Host: "123456789.container.us-west-2.cloud.databricks.test", } - workspace := workspaceProfile("workspace") tests := []struct { - name string - profiles profile.Profiles - want string - wantErr string + name string + config string + wantPrevious []string + wantRecorded []string + wantNoWrite bool }{ - {name: "selects workspace profile", profiles: profile.Profiles{workspace}, want: workspace.Name}, { - name: "duplicate workspace ID", - profiles: profile.Profiles{workspaceProfile("prod"), workspaceProfile("dev")}, - wantErr: "multiple Databricks profiles match workspace ID 123456789: prod and dev. Docker credential helper requires exactly one profile per workspace. Remove the duplicate profile with: databricks auth logout --profile --delete", + name: "records only profile", + config: configSection("prod", workspaceID, false), + wantRecorded: []string{"prod"}, }, { - name: "ignores unsupported duplicate", - profiles: profile.Profiles{workspace, { - Name: "m2m", - Host: workspaceHost, - WorkspaceID: workspaceID, - HasClientCredentials: true, - }}, - want: workspace.Name, + name: "switches from previous profiles", + config: configSection("dev", workspaceID, true) + configSection("prod", workspaceID, false) + configSection("stage", workspaceID, true), + wantPrevious: []string{"dev", "stage"}, + wantRecorded: []string{"prod"}, }, { - name: "no matching profile", - wantErr: "no Databricks profile found for workspace ID 123456789 from registry host 123456789.container.us-west-2.cloud.databricks.test. Run databricks auth login --host and set workspace_id for that profile", + name: "unmarks other profiles when already recorded", + config: configSection("dev", workspaceID, true) + configSection("prod", workspaceID, true), + wantPrevious: []string{"dev"}, + wantRecorded: []string{"prod"}, + }, + { + name: "keeps recorded profile", + config: configSection("dev", workspaceID, false) + configSection("prod", workspaceID, true), + wantRecorded: []string{"prod"}, + wantNoWrite: true, + }, + { + name: "ignores other workspaces", + config: configSection("other", "987654321", true) + configSection("prod", workspaceID, false), + wantRecorded: []string{"other", "prod"}, + }, + { + name: "unmarks unsupported profiles", + config: "[pat]\nhost = " + workspaceHost + "\nauth_type = pat\nworkspace_id = " + workspaceID + "\ndocker_credential_helper = true\n\n" + configSection("prod", workspaceID, false), + wantPrevious: []string{"pat"}, + wantRecorded: []string{"prod"}, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := dockercredentials.ProfileForRegistry(t.Context(), profile.InMemoryProfiler{Profiles: tt.profiles}, registry) - if tt.wantErr != "" { - assert.EqualError(t, err, tt.wantErr) - return - } + path := filepath.Join(t.TempDir(), ".databrickscfg") + require.NoError(t, os.WriteFile(path, []byte(tt.config), 0o600)) + ctx := env.Set(t.Context(), "DATABRICKS_CONFIG_FILE", path) + + previous, err := dockercredentials.RecordProfile(ctx, registry, "prod") require.NoError(t, err) - assert.Equal(t, tt.want, got.Name) + assert.Equal(t, tt.wantPrevious, previous) + + selected, err := dockercredentials.ProfileForRegistry(ctx, profile.DefaultProfiler, registry) + require.NoError(t, err) + assert.Equal(t, "prod", selected.Name) + + recorded, err := profile.DefaultProfiler.LoadProfiles(ctx, func(p profile.Profile) bool { + return p.DockerCredentialHelper + }) + require.NoError(t, err) + assert.Equal(t, tt.wantRecorded, recorded.Names()) + if tt.wantNoWrite { + contents, err := os.ReadFile(path) + require.NoError(t, err) + assert.Equal(t, tt.config, string(contents)) + } }) } }