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
26 changes: 13 additions & 13 deletions cmd/root/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@ import (
"context"
"errors"
"fmt"
"net/http"
"os"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/databrickscfg"
"github.com/databricks/databricks-sdk-go"
"github.com/databricks/databricks-sdk-go/config"
"github.com/databricks/databricks-sdk-go/service/iam"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
)

// Placeholders to use as unique keys in context.Context.
var workspaceClient int
var accountClient int
var currentUser int

func initProfileFlag(cmd *cobra.Command) {
cmd.PersistentFlags().StringP("profile", "p", "", "~/.databrickscfg profile")
Expand Down Expand Up @@ -94,8 +93,7 @@ TRY_AUTH: // or try picking a config profile dynamically
if err != nil {
return err
}
// get current user identity also to verify validity of configuration
me, err := w.CurrentUser.Me(ctx)
err = w.Config.Authenticate(emptyHttpRequest(ctx))
if cmdio.IsInteractive(ctx) && errors.Is(err, config.ErrCannotConfigureAuth) {
profile, err := askForWorkspaceProfile()
if err != nil {
Expand All @@ -107,7 +105,6 @@ TRY_AUTH: // or try picking a config profile dynamically
if err != nil {
return err
}
ctx = context.WithValue(ctx, &currentUser, me)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there anything that depends on this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is not. The only accessor was the root.Me() function and nothing calls it.

ctx = context.WithValue(ctx, &workspaceClient, w)
cmd.SetContext(ctx)
return nil
Expand Down Expand Up @@ -194,6 +191,17 @@ func askForAccountProfile() (string, error) {
return profiles[i].Name, nil
}

// To verify that a client is configured correctly, we pass an empty HTTP request
// to a client's `config.Authenticate` function. Note: this functionality
// should be supported by the SDK itself.
func emptyHttpRequest(ctx context.Context) *http.Request {
req, err := http.NewRequestWithContext(ctx, "", "", nil)
if err != nil {
panic(err)
}
return req
}

func WorkspaceClient(ctx context.Context) *databricks.WorkspaceClient {
w, ok := ctx.Value(&workspaceClient).(*databricks.WorkspaceClient)
if !ok {
Expand All @@ -209,11 +217,3 @@ func AccountClient(ctx context.Context) *databricks.AccountClient {
}
return a
}

func Me(ctx context.Context) *iam.User {
me, ok := ctx.Value(&currentUser).(*iam.User)
if !ok {
panic("cannot get current user. Please report it as a bug")
}
return me
}
14 changes: 14 additions & 0 deletions cmd/root/auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package root

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
)

func TestEmptyHttpRequest(t *testing.T) {
ctx, _ := context.WithCancel(context.Background())
req := emptyHttpRequest(ctx)
assert.Equal(t, req.Context(), ctx)
}