Skip to content

Commit 8b6919f

Browse files
authored
Pass through OIDC token env variable to Terraform (#3113)
## Changes Pass through OIDC token env variable to Terraform ## Why This allows OIDC auth to correctly work in DABs, otherwise it fails with ``` Error: cannot create job: failed during request visitor: env-oidc auth: missing env var "DATABRICKS_OIDC_TOKEN". Config: host=*** client_id=***. Env: DATABRICKS_HOST, DATABRICKS_CLIENT_ID ``` ## Tests Added unit test <!-- If your PR needs to be included in the release notes for next release, add a separate entry in NEXT_CHANGELOG.md as part of your PR. -->
1 parent af10ba3 commit 8b6919f

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

NEXT_CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* Improve error message for host mismatch between bundle and profile used ([#3100](https://github.com/databricks/cli/pull/3100))
1313
* Remove support for deprecated `experimental/pydabs` config, use `experimental/python` instead. See [Configuration in Python
1414
](https://docs.databricks.com/dev-tools/bundles/python). ([#3102](https://github.com/databricks/cli/pull/3102))
15+
* Pass through OIDC token env variable to Terraform ([#3113](https://github.com/databricks/cli/pull/3113))
1516

1617
* The `default-python` template now prompts if you want to use serverless compute (default to `yes`) ([#3051](https://github.com/databricks/cli/pull/3051)).
1718

bundle/deploy/terraform/init.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,20 @@ func inheritEnvVars(ctx context.Context, environ map[string]string) error {
158158
}
159159
}
160160

161+
// If there's a DATABRICKS_OIDC_TOKEN_ENV set, we need to pass the value of the environment variable defined in DATABRICKS_OIDC_TOKEN_ENV to Terraform.
162+
// This is necessary to ensure that Terraform can use the same OIDC token as the CLI.
163+
oidcTokenEnv, ok := env.Lookup(ctx, "DATABRICKS_OIDC_TOKEN_ENV")
164+
if ok {
165+
environ["DATABRICKS_OIDC_TOKEN_ENV"] = oidcTokenEnv
166+
} else {
167+
oidcTokenEnv = "DATABRICKS_OIDC_TOKEN"
168+
}
169+
170+
oidcToken, ok := env.Lookup(ctx, oidcTokenEnv)
171+
if ok {
172+
environ[oidcTokenEnv] = oidcToken
173+
}
174+
161175
// Map $DATABRICKS_TF_CLI_CONFIG_FILE to $TF_CLI_CONFIG_FILE
162176
// VSCode extension provides a file with the "provider_installation.filesystem_mirror" configuration.
163177
// We only use it if the provider version matches the currently used version,

bundle/deploy/terraform/init_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,29 @@ func TestInheritEnvVars(t *testing.T) {
287287
}
288288
}
289289

290+
func TestInheritOIDCTokenEnvCustom(t *testing.T) {
291+
t.Setenv("DATABRICKS_OIDC_TOKEN_ENV", "custom_DATABRICKS_OIDC_TOKEN")
292+
t.Setenv("custom_DATABRICKS_OIDC_TOKEN", "foobar")
293+
294+
ctx := context.Background()
295+
env := map[string]string{}
296+
err := inheritEnvVars(ctx, env)
297+
require.NoError(t, err)
298+
assert.Equal(t, "foobar", env["custom_DATABRICKS_OIDC_TOKEN"])
299+
assert.Equal(t, "custom_DATABRICKS_OIDC_TOKEN", env["DATABRICKS_OIDC_TOKEN_ENV"])
300+
}
301+
302+
func TestInheritOIDCTokenEnv(t *testing.T) {
303+
t.Setenv("DATABRICKS_OIDC_TOKEN", "foobar")
304+
305+
ctx := context.Background()
306+
env := map[string]string{}
307+
err := inheritEnvVars(ctx, env)
308+
require.NoError(t, err)
309+
assert.Equal(t, "foobar", env["DATABRICKS_OIDC_TOKEN"])
310+
assert.Equal(t, "", env["DATABRICKS_OIDC_TOKEN_ENV"])
311+
}
312+
290313
func TestSetUserProfileFromInheritEnvVars(t *testing.T) {
291314
t.Setenv("USERPROFILE", "c:\\foo\\c")
292315

0 commit comments

Comments
 (0)