diff --git a/.nextchanges/cli/docker-shared-workspace-profiles.md b/.nextchanges/cli/docker-shared-workspace-profiles.md new file mode 100644 index 00000000000..5a2b71ba9c9 --- /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 for the registry in the `[__settings__]` section of `.databrickscfg`, the Docker credential helper uses it, and `databricks auth docker host` shows 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 { "host": "[WORKSPACE_ID].container.us-west-2.localhost", - "configured": true + "configured": true, + "profile": "docker-test" } >>> print_requests.py --get //api @@ -154,3 +158,86 @@ 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 +Credential helper profile: shared + +>>> databricks auth docker host --profile shared +Registry host: [WORKSPACE_ID].container.us-west-2.localhost +Credential helper configured: YES +Credential helper profile: shared + +>>> 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] + +[__settings__] +docker_profile.[WORKSPACE_ID].container.us-west-2.localhost = docker-test + +[shared] +host = [DATABRICKS_URL] +auth_type = databricks-cli +workspace_id = [WORKSPACE_ID] + +>>> docker-credential-databricks get +{ + "Secret": "oauth-token", + "Username": "oauthtoken" +} + +=== Deleting the recorded profile clears its Docker selection + +>>> databricks auth logout --profile docker-test --delete --auto-approve +Logged out of and deleted profile "docker-test". + +>>> 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] + +[__settings__] + +[shared] +host = [DATABRICKS_URL] +auth_type = databricks-cli +workspace_id = [WORKSPACE_ID] + +>>> databricks auth docker host --profile shared +Registry host: [WORKSPACE_ID].container.us-west-2.localhost +Credential helper configured: YES +Credential helper profile: shared + +=== Recorded profile that no longer exists + +>>> databricks auth docker host --profile shared +Registry host: [WORKSPACE_ID].container.us-west-2.localhost +Credential helper configured: NO + +>>> musterr docker-credential-databricks get +Error: profile "gone" selected for Docker registry [WORKSPACE_ID].container.us-west-2.localhost is missing or no longer targets workspace ID [WORKSPACE_ID]. Select the profile for Docker with: databricks auth docker configure diff --git a/acceptance/cmd/auth/docker/configure/script b/acceptance/cmd/auth/docker/configure/script index 8df314ca720..ed6119878ad 100644 --- a/acceptance/cmd/auth/docker/configure/script +++ b/acceptance/cmd/auth/docker/configure/script @@ -63,3 +63,30 @@ 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 output names the profile the credential helper uses for the registry. The +helper counts as configured only if Docker's config.json selects it for the +registry and it 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..4c06efe2867 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 and 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" +} + +=== Recorded profile that no longer exists + +>>> musterr [CLI] auth docker token +Error: profile "gone" selected for Docker registry [WORKSPACE_ID].container.us-west-2.localhost is missing or no longer targets workspace ID [WORKSPACE_ID]. 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..08fb79a983e 100644 --- a/acceptance/cmd/auth/docker/token/script +++ b/acceptance/cmd/auth/docker/token/script @@ -46,3 +46,16 @@ 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" +cat >> "$HOME/.databrickscfg" < and set workspace_id for that profile", registry.WorkspaceID, registry.Host) } + if recorded != "" { + i := slices.IndexFunc(workspaceProfiles, func(p profile.Profile) bool { return p.Name == recorded }) + if i < 0 { + return profile.Profile{}, fmt.Errorf("profile %q selected for Docker registry %s is missing or no longer targets workspace ID %s. %s", recorded, registry.Host, registry.WorkspaceID, selectProfileHint) + } + if err := ValidateProfile(workspaceProfiles[i]); err != nil { + return profile.Profile{}, err + } + return workspaceProfiles[i], nil + } + var matchingProfiles profile.Profiles for _, p := range workspaceProfiles { if ValidateProfile(p) == nil { @@ -104,7 +95,7 @@ func ProfileForRegistry(ctx context.Context, profiler profile.Profiler, registry } 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. %s", workspaceID, strings.Join(names, " and "), selectProfileHint) } // RewriteProfileError adds Docker profile context to authentication errors. diff --git a/libs/dockercredentials/profile_test.go b/libs/dockercredentials/profile_test.go index 0586d7e1e7a..d6eff111085 100644 --- a/libs/dockercredentials/profile_test.go +++ b/libs/dockercredentials/profile_test.go @@ -139,64 +139,22 @@ func TestLoadProfile(t *testing.T) { } } -func TestEnsureUniqueProfile(t *testing.T) { - selected := workspaceProfile("prod") - tests := []struct { - name string - profiles profile.Profiles - selected profile.Profile - wantErr string - }{ - {name: "single profile", profiles: profile.Profiles{selected}, selected: selected}, - { - 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: "unsupported duplicate", - profiles: profile.Profiles{selected, { - Name: "m2m", - Host: workspaceHost, - WorkspaceID: workspaceID, - HasClientCredentials: true, - }}, - selected: selected, - }, - { - 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", - }, - } - - 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) - return - } - assert.EqualError(t, err, tt.wantErr) - }) - } -} - func TestProfileForRegistry(t *testing.T) { registry := dockercredentials.Registry{ WorkspaceID: workspaceID, Host: "123456789.container.us-west-2.cloud.databricks.test", } workspace := workspaceProfile("workspace") + m2m := profile.Profile{ + Name: "m2m", + Host: workspaceHost, + WorkspaceID: workspaceID, + HasClientCredentials: true, + } tests := []struct { name string profiles profile.Profiles + recorded string want string wantErr string }{ @@ -204,27 +162,40 @@ func TestProfileForRegistry(t *testing.T) { { 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", + wantErr: "multiple Databricks profiles match workspace ID 123456789: prod and dev. Select the profile for Docker with: databricks auth docker configure ", }, { - name: "ignores unsupported duplicate", - profiles: profile.Profiles{workspace, { - Name: "m2m", - Host: workspaceHost, - WorkspaceID: workspaceID, - HasClientCredentials: true, - }}, - want: workspace.Name, + name: "ignores unsupported duplicate", + profiles: profile.Profiles{workspace, m2m}, + want: workspace.Name, }, { 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: "selects recorded profile", + profiles: profile.Profiles{workspaceProfile("prod"), workspaceProfile("dev")}, + recorded: "dev", + want: "dev", + }, + { + name: "recorded profile is missing", + profiles: profile.Profiles{workspace}, + recorded: "gone", + wantErr: `profile "gone" selected for Docker registry 123456789.container.us-west-2.cloud.databricks.test is missing or no longer targets workspace ID 123456789. Select the profile for Docker with: databricks auth docker configure `, + }, + { + name: "recorded profile is unsupported", + profiles: profile.Profiles{workspace, m2m}, + recorded: "m2m", + wantErr: `profile "m2m" uses client credentials. Docker credential helper requires a profile created by databricks auth login`, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := dockercredentials.ProfileForRegistry(t.Context(), profile.InMemoryProfiler{Profiles: tt.profiles}, registry) + got, err := dockercredentials.ProfileForRegistry(t.Context(), profile.InMemoryProfiler{Profiles: tt.profiles}, registry, tt.recorded) if tt.wantErr != "" { assert.EqualError(t, err, tt.wantErr) return