Skip to content

Commit 5216e5c

Browse files
author
Varun Deep Saini
committed
Using auth.ProcessEnv()
Signed-off-by: Varun Deep Saini <varun.23bcs10048@ms.sst.scaler.com>
1 parent bf50877 commit 5216e5c

8 files changed

Lines changed: 90 additions & 3 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
bundle:
2+
name: auth_env_test
3+
4+
sync: {paths: []} # don't need to copy files
5+
6+
python:
7+
resources:
8+
- "resources:load_resources"

acceptance/bundle/python/auth-env-passthrough/out.test.toml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
>>> uv run [UV_ARGS] -q [CLI] bundle validate --output json
3+
{
4+
"job_name": "auth_env_test_job"
5+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import os
2+
3+
from databricks.bundles.core import Bundle, Resources
4+
5+
6+
def load_resources(bundle: Bundle) -> Resources:
7+
host = os.environ.get("DATABRICKS_HOST", "NOT_SET")
8+
9+
if host == "NOT_SET":
10+
raise ValueError("DATABRICKS_HOST was not passed to Python subprocess")
11+
12+
if not host.startswith("http://") and not host.startswith("https://"):
13+
raise ValueError(f"DATABRICKS_HOST has invalid format: {host}")
14+
15+
home = os.environ.get("HOME", os.environ.get("USERPROFILE", "NOT_SET"))
16+
if home == "NOT_SET":
17+
raise ValueError("HOME/USERPROFILE was not passed to Python subprocess")
18+
19+
profile = os.environ.get("DATABRICKS_CONFIG_PROFILE")
20+
if profile is not None:
21+
raise ValueError(
22+
f"DATABRICKS_CONFIG_PROFILE should have been removed but was: {profile}. "
23+
"Conflicting auth env vars must be cleaned to prevent auth issues."
24+
)
25+
26+
resources = Resources()
27+
resources.add_job(
28+
resource_name="test_job",
29+
job={"name": "auth_env_test_job"},
30+
)
31+
32+
return resources
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
echo "$DATABRICKS_BUNDLES_WHEEL" > "requirements-latest.txt"
2+
3+
# Verify that auth env vars (DATABRICKS_HOST) are correctly passed to the
4+
# Python subprocess via auth.ProcessEnv. The Python code validates that
5+
# both DATABRICKS_HOST and HOME are present in the environment.
6+
7+
trace uv run $UV_ARGS -q $CLI bundle validate --output json | \
8+
jq '{job_name: .resources.jobs.test_job.name}'
9+
10+
rm -fr .databricks __pycache__
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# This test verifies that auth env vars from the bundle config are correctly
2+
# passed to the Python subprocess via auth.ProcessEnv, and that conflicting
3+
# auth env vars are REMOVED to prevent auth issues.
4+
5+
Local = true
6+
Cloud = false
7+
8+
Ignore = [
9+
"requirements-latest.txt",
10+
]
11+
12+
[Env]
13+
DATABRICKS_CONFIG_PROFILE = "conflicting_profile"
14+
15+
[EnvMatrix]
16+
UV_ARGS = [
17+
"--with databricks-bundles==0.266.0",
18+
"--with-requirements requirements-latest.txt --no-cache",
19+
]

bundle/config/mutator/python/python_mutator.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ type runPythonMutatorOpts struct {
105105
bundleRootPath string
106106
pythonPath string
107107
loadLocations bool
108-
authEnvs map[string]string
108+
authEnvs []string
109109
}
110110

111111
// getOpts adapts deprecated PyDABs and upcoming Python configuration
@@ -224,7 +224,7 @@ func (m *pythonMutator) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagno
224224
var result applyPythonOutputResult
225225
mutateDiagsHasError := errors.New("unexpected error")
226226

227-
authEnvs := auth.Env(b.Config.Workspace.Config())
227+
authEnvs := auth.ProcessEnv(b.WorkspaceClient().Config)
228228

229229
err = b.Config.Mutate(func(leftRoot dyn.Value) (dyn.Value, error) {
230230
pythonPath, err := detectExecutable(ctx, opts.venvPath)
@@ -369,7 +369,7 @@ func (m *pythonMutator) runPythonMutator(ctx context.Context, root dyn.Value, op
369369
process.WithDir(opts.bundleRootPath),
370370
process.WithStderrWriter(stderrWriter),
371371
process.WithStdoutWriter(stdoutWriter),
372-
process.WithEnvs(opts.authEnvs),
372+
process.WithEnviron(opts.authEnvs),
373373
)
374374
if processErr != nil {
375375
logger.Debugf(ctx, "python mutator process failed: %s", processErr)

libs/process/opts.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ func WithEnvs(envs map[string]string) execOption {
3131
}
3232
}
3333

34+
func WithEnviron(envs []string) execOption {
35+
return func(ctx context.Context, c *exec.Cmd) error {
36+
c.Env = envs
37+
return nil
38+
}
39+
}
40+
3441
func WithDir(dir string) execOption {
3542
return func(_ context.Context, c *exec.Cmd) error {
3643
c.Dir = dir

0 commit comments

Comments
 (0)