Skip to content

Commit 314766a

Browse files
authored
Detect mismatch between host defined in config and env variable (#2549)
## Changes * Adds a mismatch check when host is defined in config and as an env variable * `https://` prefix is ignored during validation on host mismatch, both in env and profiles ## Why If I defined both DATABRICKS_TOKEN and DATABRICKS_HOST I expect this token to be used only for requests to DATABRICKS_HOST, while now we have it added to `w.CurrentUser.Me` requests to the hosts defined in the config ## Tests Added acceptance tests <!-- 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 15a7c48 commit 314766a

File tree

18 files changed

+259
-146
lines changed

18 files changed

+259
-146
lines changed

NEXT_CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
* Added support for volumes in deployment bind/unbind commands ([#2527](https://github.com/databricks/cli/pull/2527))
1212
* Added support for dashboards in deployment bind/unbind commands ([#2516](https://github.com/databricks/cli/pull/2516))
1313
* Added support for registered models in deployment bind/unbind commands ([#2556](https://github.com/databricks/cli/pull/2556))
14+
* Added a mismatch check when host is defined in config and as an env variable ([#2549](https://github.com/databricks/cli/pull/2549))
1415

1516
### API Changes
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[DEFAULT]
2+
host = https://bar.com
3+
4+
[profile_with_matching_host]
5+
host = $DATABRICKS_HOST
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
bundle:
2+
name: test-auth
3+
4+
targets:
5+
match:
6+
default: true
7+
workspace:
8+
host: $DATABRICKS_HOST
9+
not_match:
10+
workspace:
11+
host: https://foo.com
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
=== Bundle commands load bundle configuration with DATABRICKS_HOST defined, validation not OK (env-defined host doesn't match bundle host)
3+
>>> errcode [CLI] bundle validate -t not_match
4+
Error: cannot resolve bundle auth configuration: config host mismatch: DATABRICKS_HOST is defined as [DATABRICKS_HOST], but CLI configured to use https://foo.com
5+
6+
Name: test-auth
7+
Target: not_match
8+
Workspace:
9+
Host: https://foo.com
10+
11+
Found 1 error
12+
13+
Exit code: 1
14+
15+
=== Bundle commands load bundle configuration with DATABRICKS_HOST defined, validation OK (env-defined host matches bundle host)
16+
>>> errcode [CLI] bundle validate -t match
17+
Name: test-auth
18+
Target: match
19+
Workspace:
20+
Host: [DATABRICKS_HOST]
21+
User: [USERNAME]
22+
Path: /Workspace/Users/[USERNAME]/.bundle/test-auth/match
23+
24+
Validation OK!
25+
26+
=== Bundle commands load bundle configuration with -p flag with DATABRICKS_HOST defined, host in profile matches bundle host but env-defined host doesn't
27+
>>> errcode [CLI] bundle validate -t match -p profile_with_matching_host
28+
Name: test-auth
29+
Target: match
30+
Workspace:
31+
Host: [DATABRICKS_HOST]
32+
User: [USERNAME]
33+
Path: /Workspace/Users/[USERNAME]/.bundle/test-auth/match
34+
35+
Validation OK!
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Replace placeholder with an actual host URL
2+
envsubst < databricks.yml > out.yml && mv out.yml databricks.yml
3+
envsubst < .databrickscfg > out && mv out .databrickscfg
4+
5+
export DATABRICKS_CONFIG_FILE=.databrickscfg
6+
7+
title "Bundle commands load bundle configuration with DATABRICKS_HOST defined, validation not OK (env-defined host doesn't match bundle host)"
8+
trace errcode $CLI bundle validate -t not_match
9+
10+
title "Bundle commands load bundle configuration with DATABRICKS_HOST defined, validation OK (env-defined host matches bundle host)"
11+
trace errcode $CLI bundle validate -t match
12+
13+
export DATABRICKS_HOST="https://baz.com"
14+
title "Bundle commands load bundle configuration with -p flag with DATABRICKS_HOST defined, host in profile matches bundle host but env-defined host doesn't"
15+
trace errcode $CLI bundle validate -t match -p profile_with_matching_host
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Some of the clouds have DATABRICKS_HOST variable setup without https:// prefix
2+
# In the result, output is replaced with DATABRICKS_URL variable instead of DATABRICKS_HOST
3+
[[Repls]]
4+
Old='DATABRICKS_URL'
5+
New='DATABRICKS_HOST'

acceptance/bundle/debug/out.stderr.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
10:07:59 Debug: Apply pid=12345 mutator=<func>
1818
10:07:59 Info: Phase: initialize pid=12345
1919
10:07:59 Debug: Apply pid=12345 mutator=validate:AllResourcesHaveValues
20-
10:07:59 Debug: Apply pid=12345 mutator=validate:interpolation_in_auth_config
2120
10:07:59 Debug: Apply pid=12345 mutator=RewriteSyncPaths
2221
10:07:59 Debug: Apply pid=12345 mutator=SyncDefaultPath
2322
10:07:59 Debug: Apply pid=12345 mutator=SyncInferRoot

acceptance/bundle/variables/host/output.txt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,14 @@ Warning: Variable interpolation is not supported for fields that configure authe
77
Interpolation is not supported for the field workspace.host. Please set
88
the DATABRICKS_HOST environment variable if you wish to configure this field at runtime.
99

10-
Error: failed during request visitor: parse "https://${var.host}": invalid character "{" in host name
10+
Error: cannot resolve bundle auth configuration: config host mismatch: DATABRICKS_HOST is defined as [DATABRICKS_URL], but CLI configured to use ${var.host}
1111

1212
{
1313
"bundle": {
1414
"environment": "default",
1515
"name": "host",
1616
"target": "default"
1717
},
18-
"sync": {
19-
"paths": [
20-
"."
21-
]
22-
},
2318
"targets": null,
2419
"variables": {
2520
"host": {
@@ -41,7 +36,7 @@ Warning: Variable interpolation is not supported for fields that configure authe
4136
Interpolation is not supported for the field workspace.host. Please set
4237
the DATABRICKS_HOST environment variable if you wish to configure this field at runtime.
4338

44-
Error: failed during request visitor: parse "https://${var.host}": invalid character "{" in host name
39+
Error: cannot resolve bundle auth configuration: config host mismatch: DATABRICKS_HOST is defined as [DATABRICKS_URL], but CLI configured to use ${var.host}
4540

4641
Name: host
4742
Target: default

bundle/config/validate/interpolation_in_auth_config.go

Lines changed: 0 additions & 83 deletions
This file was deleted.

bundle/config/workspace.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package config
22

33
import (
4+
"fmt"
45
"os"
56
"path/filepath"
67

78
"github.com/databricks/cli/libs/databrickscfg"
9+
"github.com/databricks/cli/libs/workspace"
810
"github.com/databricks/databricks-sdk-go"
911
"github.com/databricks/databricks-sdk-go/config"
1012
"github.com/databricks/databricks-sdk-go/marshal"
@@ -148,9 +150,34 @@ func (w *Workspace) Client() (*databricks.WorkspaceClient, error) {
148150
}
149151
}
150152

153+
if w.Host != "" && w.Profile == "" {
154+
err := validateConfigAndEnvHost(cfg)
155+
if err != nil {
156+
return nil, err
157+
}
158+
}
159+
151160
return databricks.NewWorkspaceClient((*databricks.Config)(cfg))
152161
}
153162

163+
func validateConfigAndEnvHost(cfg *config.Config) error {
164+
var hostEnvName, hostEnvVal string
165+
for _, attr := range config.ConfigAttributes {
166+
if attr.Name == "host" {
167+
hostEnvVal, hostEnvName = attr.ReadEnv()
168+
break
169+
}
170+
}
171+
if hostEnvName == "" || hostEnvVal == "" {
172+
return nil
173+
}
174+
175+
if !workspace.MatchHost(hostEnvVal, cfg.Host) {
176+
return fmt.Errorf("config host mismatch: %s is defined as %s, but CLI configured to use %s", hostEnvName, hostEnvVal, cfg.Host)
177+
}
178+
return nil
179+
}
180+
154181
func init() {
155182
arg0 := os.Args[0]
156183

0 commit comments

Comments
 (0)