Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cmd/auth/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ func loadToken(ctx context.Context, args loadTokenArgs) (*oauth2.Token, error) {
return nil, errors.New("providing both a profile and host is not supported")
}

// When no explicit --profile flag is provided, check the env var. This
// handles the case where downstream tools (like the Terraform provider)
// pass --host but not --profile, while DATABRICKS_CONFIG_PROFILE is set.
if args.profileName == "" {
args.profileName = env.Get(ctx, "DATABRICKS_CONFIG_PROFILE")
}

// If no --profile flag, try resolving the positional arg as a profile name.
// If it matches, use it. If not, fall through to host treatment.
if args.profileName == "" && len(args.args) == 1 {
Expand Down
27 changes: 26 additions & 1 deletion cmd/auth/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ func TestToken_loadToken(t *testing.T) {
RefreshToken: "legacy-ws",
Expiry: time.Now().Add(1 * time.Hour),
},
"dup1": {
RefreshToken: "dup1",
Expiry: time.Now().Add(1 * time.Hour),
},
},
}
validateToken := func(resp *oauth2.Token) {
Expand Down Expand Up @@ -613,7 +617,7 @@ func TestToken_loadToken(t *testing.T) {
validateToken: validateToken,
},
{
name: "no args, DATABRICKS_HOST takes precedence over DATABRICKS_CONFIG_PROFILE",
name: "no args, DATABRICKS_CONFIG_PROFILE env takes precedence over DATABRICKS_HOST",
setupCtx: func(ctx context.Context) context.Context {
ctx = env.Set(ctx, "DATABRICKS_HOST", "https://workspace-a.cloud.databricks.com")
ctx = env.Set(ctx, "DATABRICKS_CONFIG_PROFILE", "expired")
Expand All @@ -633,6 +637,27 @@ func TestToken_loadToken(t *testing.T) {
},
validateToken: validateToken,
},
{
name: "host flag with profile env var disambiguates multi-profile",
setupCtx: func(ctx context.Context) context.Context {
return env.Set(ctx, "DATABRICKS_CONFIG_PROFILE", "dup1")
},
args: loadTokenArgs{
authArguments: &auth.AuthArguments{
Host: "https://shared.cloud.databricks.com",
},
profileName: "",
args: []string{},
tokenTimeout: 1 * time.Hour,
profiler: profiler,
persistentAuthOpts: []u2m.PersistentAuthOption{
u2m.WithTokenCache(tokenCache),
u2m.WithOAuthEndpointSupplier(&MockApiClient{}),
u2m.WithHttpClient(&http.Client{Transport: fixtures.SliceTransport{refreshSuccessTokenResponse}}),
},
},
validateToken: validateToken,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
Expand Down
6 changes: 0 additions & 6 deletions libs/auth/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ import (
func Env(cfg *config.Config) map[string]string {
out := make(map[string]string)
for _, attr := range config.ConfigAttributes {
// Ignore profile so that downstream tools don't try and reload
// the profile. We know the current configuration is already valid since
// otherwise the CLI would have thrown an error when loading it.
if attr.Name == "profile" {
continue
}
if len(attr.EnvVars) == 0 {
continue
}
Expand Down
3 changes: 2 additions & 1 deletion libs/auth/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func TestAuthEnv(t *testing.T) {
in := &config.Config{
Profile: "thisshouldbeignored",
Profile: "myprofile",
Host: "https://test.com",
Token: "test-token",
Password: "test-password",
Expand All @@ -24,6 +24,7 @@ func TestAuthEnv(t *testing.T) {
}

expected := map[string]string{
"DATABRICKS_CONFIG_PROFILE": "myprofile",
"DATABRICKS_HOST": "https://test.com",
"DATABRICKS_TOKEN": "test-token",
"DATABRICKS_PASSWORD": "test-password",
Expand Down