Skip to content

Commit 7fccd48

Browse files
authored
auth: add DATABRICKS_DISCOVERY_HOST env var to override login.databricks.com (#5283)
## Why The discovery login flow (`databricks auth login` without `--host`) opens `https://login.databricks.com`. That host is hardcoded in the CLI, so there is no way to point the flow at a non-production login instance during testing or development. The SDK already exposes `u2m.WithDiscoveryHost` (added in databricks-sdk-go #1640, on the CLI's pinned v0.132.0). This PR wires it up. ## Changes **Before:** No way to override the discovery host. `databricks auth login` always opens `https://login.databricks.com`. **Now:** If `DATABRICKS_DISCOVERY_HOST` is set, the CLI passes it through to `u2m.WithDiscoveryHost(...)`. When unset, behavior is identical to before. The "Opening ... in your browser..." log line reflects the override host so it's clear which host is being opened. Intended for testing and development against non-production login instances; unset for normal use. ## Test plan - [x] New unit test `TestDiscoveryLogin_OverridesHostFromEnv` confirms the env var is read and the log message reflects the override host - [x] `go test ./cmd/auth/...` passes - [x] `./task checks` passes - [x] `./task lint-q` passes
1 parent 3204e67 commit 7fccd48

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

cmd/auth/login.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ const (
4646
defaultTimeout = 1 * time.Hour
4747
authTypeDatabricksCLI = "databricks-cli"
4848
discoveryFallbackTip = "\n\nTip: you can specify a workspace directly with: databricks auth login --host <url>"
49+
// discoveryHostEnvVar overrides the default https://login.databricks.com
50+
// host used by the discovery login flow. Intended for testing and
51+
// development against non-production environments. See WithDiscoveryHost
52+
// in github.com/databricks/databricks-sdk-go/credentials/u2m.
53+
discoveryHostEnvVar = "DATABRICKS_DISCOVERY_HOST"
4954
)
5055

5156
// discoveryErr wraps an error (or creates a new one) and appends the
@@ -624,6 +629,10 @@ func discoveryLogin(ctx context.Context, in discoveryLoginInputs) error {
624629
if len(scopesList) > 0 {
625630
opts = append(opts, u2m.WithScopes(scopesList))
626631
}
632+
discoveryHost := env.Get(ctx, discoveryHostEnvVar)
633+
if discoveryHost != "" {
634+
opts = append(opts, u2m.WithDiscoveryHost(discoveryHost))
635+
}
627636

628637
// Apply timeout before creating PersistentAuth so Challenge() respects it.
629638
ctx, cancel := context.WithTimeout(ctx, in.timeout)
@@ -635,7 +644,11 @@ func discoveryLogin(ctx context.Context, in discoveryLoginInputs) error {
635644
}
636645
defer persistentAuth.Close()
637646

638-
cmdio.LogString(ctx, "Opening login.databricks.com in your browser...")
647+
displayHost := "login.databricks.com"
648+
if discoveryHost != "" {
649+
displayHost = discoveryHost
650+
}
651+
cmdio.LogString(ctx, fmt.Sprintf("Opening %s in your browser...", displayHost))
639652
if err := persistentAuth.Challenge(); err != nil {
640653
return discoveryErr("login via login.databricks.com failed", err)
641654
}

cmd/auth/login_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,40 @@ auth_type = databricks-cli
10821082
assert.Equal(t, "222222", savedProfile.WorkspaceID, "workspace_id should be updated to fresh introspection value")
10831083
}
10841084

1085+
func TestDiscoveryLogin_OverridesHostFromEnv(t *testing.T) {
1086+
tmpDir := t.TempDir()
1087+
configPath := filepath.Join(tmpDir, ".databrickscfg")
1088+
err := os.WriteFile(configPath, []byte(""), 0o600)
1089+
require.NoError(t, err)
1090+
t.Setenv("DATABRICKS_CONFIG_FILE", configPath)
1091+
1092+
oauthArg, err := u2m.NewBasicDiscoveryOAuthArgument("DISCOVERY")
1093+
require.NoError(t, err)
1094+
oauthArg.SetDiscoveredHost("https://workspace.example.com")
1095+
1096+
dc := &fakeDiscoveryClient{
1097+
oauthArg: oauthArg,
1098+
persistentAuth: &fakeDiscoveryPersistentAuth{
1099+
token: &oauth2.Token{AccessToken: "test-token"},
1100+
},
1101+
introspectionErr: errors.New("introspection failed"),
1102+
}
1103+
1104+
ctx, stderr := cmdio.NewTestContextWithStderr(t.Context())
1105+
ctx = env.Set(ctx, "DATABRICKS_DISCOVERY_HOST", "https://login.staging.test")
1106+
err = discoveryLogin(ctx, discoveryLoginInputs{
1107+
dc: dc,
1108+
profileName: "DISCOVERY",
1109+
timeout: time.Second,
1110+
browserFunc: func(string) error { return nil },
1111+
tokenCache: newTestTokenCache(),
1112+
})
1113+
require.NoError(t, err)
1114+
1115+
assert.Contains(t, stderr.String(), "Opening https://login.staging.test in your browser...")
1116+
assert.NotContains(t, stderr.String(), "Opening login.databricks.com in your browser...")
1117+
}
1118+
10851119
func TestLoginRejectsPositionalArgWithHostFlag(t *testing.T) {
10861120
ctx := cmdio.MockDiscard(t.Context())
10871121
authArgs := &auth.AuthArguments{Host: "https://example.com"}

0 commit comments

Comments
 (0)