Skip to content
Open
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/docker-shared-workspace-profiles.md
Original file line number Diff line number Diff line change
@@ -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))
5 changes: 0 additions & 5 deletions acceptance/cmd/auth/docker/configure-errors/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <profile-name> --delete

=== Nothing changed
Docker config not created; profiles unchanged
8 changes: 0 additions & 8 deletions acceptance/cmd/auth/docker/configure-errors/script
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ cat >> "$HOME/.databrickscfg" <<ENDCFG
host = https://workspace.example.test
auth_type = databricks-cli

[duplicate]
host = $DATABRICKS_HOST_ORIG
auth_type = databricks-cli
workspace_id = 900800700600

[pat]
host = $DATABRICKS_HOST_ORIG
auth_type = pat
Expand Down Expand Up @@ -54,9 +49,6 @@ trace musterr $CLI auth docker configure pat
trace musterr $CLI auth docker configure m2m
trace musterr $CLI auth docker configure blank-auth

title "Another profile already maps to the workspace"
trace musterr $CLI auth docker configure docker-test

title "Nothing changed"
assert_not_exists.py "$DOCKER_CONFIG/config.json"
cmp "$HOME/databrickscfg.before" "$HOME/.databrickscfg"
Expand Down
56 changes: 53 additions & 3 deletions acceptance/cmd/auth/docker/configure/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ Installed Docker credential helper: [TEST_TMP_DIR]/home/bin/docker-credential-da
[DEFAULT]

[docker-test]
host = [DATABRICKS_URL]
auth_type = databricks-cli
workspace_id = [WORKSPACE_ID]
host = [DATABRICKS_URL]
auth_type = databricks-cli
workspace_id = [WORKSPACE_ID]
docker_credential_helper = true

>>> databricks auth docker host --profile docker-test --output json
{
Expand Down Expand Up @@ -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"
}
17 changes: 17 additions & 0 deletions acceptance/cmd/auth/docker/configure/script
Original file line number Diff line number Diff line change
Expand Up @@ -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" <<ENDCFG

[shared]
host = $DATABRICKS_HOST_ORIG
auth_type = databricks-cli
workspace_id = 900800700600
ENDCFG
cp token-cache.json "$HOME/.databricks/token-cache.json"
trace databricks auth docker configure shared
trace databricks auth docker host --profile docker-test
trace databricks auth docker host --profile shared
trace databricks auth docker configure docker-test
trace print_requests.py --get --oneline //api
trace cat "$HOME/.databrickscfg"
printf '%s\n' "$registry_host" | trace docker-credential-databricks get | jq -S .
17 changes: 17 additions & 0 deletions acceptance/cmd/auth/docker/configure/token-cache.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"version": 1,
"tokens": {
"docker-test": {
"access_token": "cached-access-token",
"token_type": "Bearer",
"refresh_token": "test-refresh-token",
"expiry": "2099-01-01T00:00:00Z"
},
"shared": {
"access_token": "shared-access-token",
"token_type": "Bearer",
"refresh_token": "shared-refresh-token",
"expiry": "2099-01-01T00:00:00Z"
}
}
}
17 changes: 11 additions & 6 deletions acceptance/cmd/auth/docker/help/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@ Use "databricks auth docker [command] --help" for more information about a comma
This command installs docker-credential-databricks and configures Docker to use
it for the selected workspace's Artifact Registry host. If the selected profile
does not already include a workspace_id, the command resolves and saves it so
the Docker helper can map the registry host back to the profile. The registry
region is inferred from the workspace's metastore. Select the workspace with
[PROFILE] or --profile; --host, --account-id, and --workspace-id are not
supported. The deprecated --region flag is retained for compatibility; omit it
because it will be fully removed in the next release.
the Docker helper can map the registry host back to the profile. The command
also marks the profile with docker_credential_helper = true, so the helper uses
it when other profiles match the same workspace; run the command with another
profile to switch. The registry region is inferred from the workspace's
metastore. Select the workspace with [PROFILE] or --profile; --host,
--account-id, and --workspace-id are not supported. The deprecated --region
flag is retained for compatibility; omit it because it will be fully removed in
the next release.

Usage:
databricks auth docker configure [PROFILE] [flags]
Expand All @@ -90,7 +93,9 @@ Global Flags:
>>> [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]
Expand Down
15 changes: 14 additions & 1 deletion acceptance/cmd/auth/docker/token/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <profile-name> --delete
Error: multiple Databricks profiles match workspace ID [WORKSPACE_ID]: docker-test, duplicate. Select the profile for Docker with: databricks auth docker configure <profile-name>

=== 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 <profile-name>
16 changes: 16 additions & 0 deletions acceptance/cmd/auth/docker/token/script
Original file line number Diff line number Diff line change
Expand Up @@ -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" <<ENDCFG

[also-recorded]
host = $DATABRICKS_HOST_ORIG
auth_type = databricks-cli
workspace_id = 900800700600
docker_credential_helper = true
ENDCFG
printf '%s\n' "$registry_host" | trace musterr $CLI auth docker token
17 changes: 17 additions & 0 deletions acceptance/cmd/auth/docker/token/token-cache.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"version": 1,
"tokens": {
"docker-test": {
"access_token": "docker-test-access-token",
"token_type": "Bearer",
"refresh_token": "docker-test-refresh-token",
"expiry": "2099-01-01T00:00:00Z"
},
"duplicate": {
"access_token": "duplicate-access-token",
"token_type": "Bearer",
"refresh_token": "duplicate-refresh-token",
"expiry": "2099-01-01T00:00:00Z"
}
}
}
34 changes: 25 additions & 9 deletions cmd/auth/docker/docker_configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"path/filepath"
"strings"

"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/databrickscfg"
Expand All @@ -24,11 +25,14 @@ func newDockerConfigureCommand() *cobra.Command {
This command installs docker-credential-databricks and configures Docker to use
it for the selected workspace's Artifact Registry host. If the selected profile
does not already include a workspace_id, the command resolves and saves it so
the Docker helper can map the registry host back to the profile. The registry
region is inferred from the workspace's metastore. Select the workspace with
[PROFILE] or --profile; --host, --account-id, and --workspace-id are not
supported. The deprecated --region flag is retained for compatibility; omit it
because it will be fully removed in the next release.`,
the Docker helper can map the registry host back to the profile. The command
also marks the profile with docker_credential_helper = true, so the helper uses
it when other profiles match the same workspace; run the command with another
profile to switch. The registry region is inferred from the workspace's
metastore. Select the workspace with [PROFILE] or --profile; --host,
--account-id, and --workspace-id are not supported. The deprecated --region
flag is retained for compatibility; omit it because it will be fully removed in
the next release.`,
Args: cobra.MaximumNArgs(1),
PreRunE: func(cmd *cobra.Command, _ []string) error {
return errorOnUnsupportedDockerFlags(cmd, "[PROFILE] or --profile")
Expand Down Expand Up @@ -66,9 +70,6 @@ func configureDockerTarget(cmd *cobra.Command, args []string, region string) (*d
if err != nil {
return nil, err
}
if err := dockercredentials.EnsureUniqueProfile(ctx, profile.DefaultProfiler, w.profile, w.id); err != nil {
return nil, err
}
return w.target(ctx, explicitRegion)
}

Expand All @@ -83,7 +84,22 @@ func (t *dockerTarget) configure(ctx context.Context) (dockercredentials.Configu
return dockercredentials.Configuration{}, fmt.Errorf("save workspace ID to profile %q: %w", t.profile.Name, err)
}
}
return dockercredentials.Configure(ctx, t.executable, t.registryHost)
result, err := dockercredentials.Configure(ctx, t.executable, t.registryHost)
if err != nil {
return dockercredentials.Configuration{}, err
}
previous, err := dockercredentials.RecordProfile(ctx, t.registry(), t.profile.Name)
if err != nil {
return dockercredentials.Configuration{}, fmt.Errorf("configured Docker for %s, but could not save profile %q as its credential helper profile: %w", t.registryHost, t.profile.Name, err)
}
if len(previous) > 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) {
Expand Down
7 changes: 5 additions & 2 deletions cmd/auth/docker/docker_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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{
Expand All @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/auth/docker/docker_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
34 changes: 34 additions & 0 deletions libs/databrickscfg/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 = ""
Expand All @@ -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)
Expand Down
Loading
Loading