From 080d1bf8b92ef3f80747284f04e0e6ca6daec5f8 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 3 Mar 2025 18:13:56 +0100 Subject: [PATCH] make the profile is passed test work --- .../profile-is-passed/.databrickscfg | 8 +++++ .../profile-is-passed/databricks.yml | 3 ++ .../profile-is-passed/out.requests.txt | 32 +++++++++++++++++++ .../profile-is-passed/output.txt | 13 ++++++-- .../databricks-cli/profile-is-passed/script | 14 ++++++++ cmd/bundle/exec.go | 8 +++++ 6 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 acceptance/bundle/exec/databricks-cli/profile-is-passed/.databrickscfg create mode 100644 acceptance/bundle/exec/databricks-cli/profile-is-passed/out.requests.txt diff --git a/acceptance/bundle/exec/databricks-cli/profile-is-passed/.databrickscfg b/acceptance/bundle/exec/databricks-cli/profile-is-passed/.databrickscfg new file mode 100644 index 000000000..fc39190cb --- /dev/null +++ b/acceptance/bundle/exec/databricks-cli/profile-is-passed/.databrickscfg @@ -0,0 +1,8 @@ +[someprofile] +host = $DATABRICKS_HOST +token = $DATABRICKS_TOKEN + +[myprofile] +host = $DATABRICKS_HOST +client_id = client_id +client_secret = client_secret diff --git a/acceptance/bundle/exec/databricks-cli/profile-is-passed/databricks.yml b/acceptance/bundle/exec/databricks-cli/profile-is-passed/databricks.yml index 432311dab..5de7d1d96 100644 --- a/acceptance/bundle/exec/databricks-cli/profile-is-passed/databricks.yml +++ b/acceptance/bundle/exec/databricks-cli/profile-is-passed/databricks.yml @@ -1,2 +1,5 @@ bundle: name: foobar + +workspace: + profile: someprofile diff --git a/acceptance/bundle/exec/databricks-cli/profile-is-passed/out.requests.txt b/acceptance/bundle/exec/databricks-cli/profile-is-passed/out.requests.txt new file mode 100644 index 000000000..2b214d4fd --- /dev/null +++ b/acceptance/bundle/exec/databricks-cli/profile-is-passed/out.requests.txt @@ -0,0 +1,32 @@ +{ + "headers": { + "Authorization": [ + "Bearer [DATABRICKS_TOKEN]" + ] + }, + "method": "GET", + "path": "/api/2.0/preview/scim/v2/Me" +} +{ + "method": "GET", + "path": "/oidc/.well-known/oauth-authorization-server" +} +{ + "headers": { + "Authorization": [ + "Basic [ENCODED_AUTH]" + ] + }, + "method": "POST", + "path": "/oidc/v1/token", + "raw_body": "grant_type=client_credentials\u0026scope=all-apis" +} +{ + "headers": { + "Authorization": [ + "Bearer oauth-token" + ] + }, + "method": "GET", + "path": "/api/2.0/preview/scim/v2/Me" +} diff --git a/acceptance/bundle/exec/databricks-cli/profile-is-passed/output.txt b/acceptance/bundle/exec/databricks-cli/profile-is-passed/output.txt index a258c7073..d733dd024 100644 --- a/acceptance/bundle/exec/databricks-cli/profile-is-passed/output.txt +++ b/acceptance/bundle/exec/databricks-cli/profile-is-passed/output.txt @@ -1,3 +1,12 @@ -script: line 64: syntax error near unexpected token `)' -Exit code: 2 +>>> [CLI] bundle exec -- databricks current-user me +{ + "id":"[USERID]", + "userName":"[USERNAME]" +} + +>>> [CLI] bundle exec --profile myprofile -- databricks current-user me +{ + "id":"[USERID]", + "userName":"[USERNAME]" +} diff --git a/acceptance/bundle/exec/databricks-cli/profile-is-passed/script b/acceptance/bundle/exec/databricks-cli/profile-is-passed/script index e69de29bb..ad9280c5a 100644 --- a/acceptance/bundle/exec/databricks-cli/profile-is-passed/script +++ b/acceptance/bundle/exec/databricks-cli/profile-is-passed/script @@ -0,0 +1,14 @@ +# Replace placeholder with an actual host URL +envsubst < databricks.yml > out.yml && mv out.yml databricks.yml +envsubst < .databrickscfg > out && mv out .databrickscfg +export DATABRICKS_CONFIG_FILE=.databrickscfg + +# Credentials will be picked up from .databrickscfg. Unset existing credentials. +unset DATABRICKS_HOST +unset DATABRICKS_TOKEN + +# This should use the profile configured in the bundle, i.e. PAT auth +trace $CLI bundle exec -- databricks current-user me + +# This should use myprofile, which uses oauth. +trace $CLI bundle exec --profile myprofile -- databricks current-user me diff --git a/cmd/bundle/exec.go b/cmd/bundle/exec.go index 5ec6a0d28..b5a52f609 100644 --- a/cmd/bundle/exec.go +++ b/cmd/bundle/exec.go @@ -68,6 +68,14 @@ Examples: // TODO: TODO: What happens here if a default target is resolved? When // no targets are defined? env = append(env, "DATABRICKS_BUNDLE_TARGET="+b.Config.Bundle.Target) + + // If the bundle has a profile, explicitly pass it to the child command. + // This is unnecessary for tools that follow the unified authentication spec. + // However, because the CLI can read the profile from the bundle itself, we + // need to pass it explicitly. + if b.Config.Workspace.Profile != "" { + env = append(env, "DATABRICKS_CONFIG_PROFILE="+b.Config.Workspace.Profile) + } childCmd.Env = env // Execute all scripts from the bundle root directory. This behavior can