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/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 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))
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
89 changes: 88 additions & 1 deletion acceptance/cmd/auth/docker/configure/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,14 @@ host = [DATABRICKS_URL]
auth_type = databricks-cli
workspace_id = [WORKSPACE_ID]

[__settings__]
docker_profile.[WORKSPACE_ID].container.us-west-2.localhost = docker-test

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

title "Deleting the recorded profile clears its Docker selection"
trace databricks auth logout --profile docker-test --delete --auto-approve
trace cat "$HOME/.databrickscfg"
trace databricks auth docker host --profile shared

title "Recorded profile that no longer exists"
update_file.py "$HOME/.databrickscfg" "[__settings__]" $'[__settings__]\ndocker_profile.'"$registry_host"' = gone'
trace databricks auth docker host --profile shared
printf '%s\n' "$registry_host" | trace musterr docker-credential-databricks get
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 records the profile for the registry host in the [__settings__] section of
the configuration file, 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 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]
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 and 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"
}

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

[__settings__]
docker_profile.$registry_host = duplicate
ENDCFG
printf '%s\n' "$registry_host" | trace $CLI auth docker token --no-force-refresh | jq -S .

title "Recorded profile that no longer exists"
update_file.py "$HOME/.databrickscfg" "docker_profile.$registry_host = duplicate" "docker_profile.$registry_host = gone"
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"
}
}
}
32 changes: 22 additions & 10 deletions cmd/auth/docker/docker_configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,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 records the profile for the registry host in the [__settings__] section of
the configuration file, 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,24 +69,33 @@ 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)
}

func (t *dockerTarget) configure(ctx context.Context) (dockercredentials.Configuration, error) {
configFile := env.Get(ctx, "DATABRICKS_CONFIG_FILE")
if t.profile.WorkspaceID == "" || t.profile.WorkspaceID == profile.WorkspaceIDNone {
err := databrickscfg.SaveToProfile(ctx, &config.Config{
ConfigFile: env.Get(ctx, "DATABRICKS_CONFIG_FILE"),
ConfigFile: configFile,
Profile: t.profile.Name,
WorkspaceID: t.workspaceID,
})
if err != nil {
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 := databrickscfg.SetDockerProfile(ctx, t.registryHost, t.profile.Name, configFile)
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 previous != "" && previous != t.profile.Name {
cmdio.LogString(ctx, fmt.Sprintf("Warning: Docker credential helper for %s switched from profile %s to %s", t.registryHost, previous, t.profile.Name))
}
return result, nil
}

func (t *dockerTarget) logConfigured(ctx context.Context, result dockercredentials.Configuration) {
Expand Down
19 changes: 15 additions & 4 deletions cmd/auth/docker/docker_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import (
"strings"

"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/databrickscfg"
"github.com/databricks/cli/libs/databrickscfg/profile"
"github.com/databricks/cli/libs/dockercredentials"
"github.com/databricks/cli/libs/env"
"github.com/spf13/cobra"
)

Expand All @@ -15,26 +18,34 @@ 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 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.`,
Example: " databricks auth docker host --profile DEFAULT",
Args: cobra.NoArgs,
Annotations: map[string]string{
"template": "Registry host: {{.Host}}\nCredential helper configured: {{bool .Configured}}\n",
"template": "Registry host: {{.Host}}\nCredential helper configured: {{bool .Configured}}\n{{if .Profile}}Credential helper profile: {{.Profile}}\n{{end}}",
},
PreRunE: func(cmd *cobra.Command, _ []string) error {
return errorOnUnsupportedDockerFlags(cmd, "--profile")
},
}
cmd.RunE = func(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
target, err := dockerHostTarget(cmd)
if err != nil {
return err
}
status, err := dockercredentials.Inspect(cmd.Context(), target.registryHost)
recorded, err := databrickscfg.GetDockerProfile(ctx, target.registryHost, env.Get(ctx, "DATABRICKS_CONFIG_FILE"))
if err != nil {
return err
}
return cmdio.Render(cmd.Context(), status)
registry := dockercredentials.Registry{WorkspaceID: target.workspaceID, Host: target.registryHost}
status, err := dockercredentials.Inspect(ctx, profile.DefaultProfiler, registry, recorded, target.profile.Name)
if err != nil {
return err
}
return cmdio.Render(ctx, status)
}
return cmd
}
Expand Down
Loading
Loading