WIP: DATABRICKS_CA_BUNDLE

This commit is contained in:
Miles Yucht 2024-03-11 21:58:07 +01:00
parent a44c52a399
commit 46f61fad09
No known key found for this signature in database
GPG Key ID: CDA4D62DC9997360
4 changed files with 35 additions and 9 deletions

View File

@ -60,10 +60,10 @@ func newLoginCommand(persistentAuth *auth.PersistentAuth) *cobra.Command {
if err != nil {
return err
}
profileName = profile
persistentAuth.Profile = profile
}
err := setHost(ctx, profileName, persistentAuth, args)
err := setHost(ctx, persistentAuth, args)
if err != nil {
return err
}
@ -127,10 +127,10 @@ func newLoginCommand(persistentAuth *auth.PersistentAuth) *cobra.Command {
return cmd
}
func setHost(ctx context.Context, profileName string, persistentAuth *auth.PersistentAuth, args []string) error {
func setHost(ctx context.Context, persistentAuth *auth.PersistentAuth, args []string) error {
// If the chosen profile has a hostname and the user hasn't specified a host, infer the host from the profile.
_, profiles, err := databrickscfg.LoadProfiles(ctx, func(p databrickscfg.Profile) bool {
return p.Name == profileName
return p.Name == persistentAuth.Profile
})
// Tolerate ErrNoConfiguration here, as we will write out a configuration as part of the login flow.
if err != nil && !errors.Is(err, databrickscfg.ErrNoConfiguration) {

View File

@ -26,14 +26,14 @@ func newTokenCommand(persistentAuth *auth.PersistentAuth) *cobra.Command {
var profileName string
profileFlag := cmd.Flag("profile")
if profileFlag != nil {
profileName = profileFlag.Value.String()
persistentAuth.Profile = profileFlag.Value.String()
// If a profile is provided we read the host from the .databrickscfg file
if profileName != "" && len(args) > 0 {
return errors.New("providing both a profile and a host parameters is not supported")
return errors.New("providing both a profile and a hostname is not supported")
}
}
err := setHost(ctx, profileName, persistentAuth, args)
err := setHost(ctx, persistentAuth, args)
if err != nil {
return err
}

2
go.mod
View File

@ -69,3 +69,5 @@ require (
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
replace github.com/databricks/databricks-sdk-go => /Users/miles/databricks-sdk-go

View File

@ -16,6 +16,8 @@ import (
"time"
"github.com/databricks/cli/libs/auth/cache"
"github.com/databricks/databricks-sdk-go/config"
"github.com/databricks/databricks-sdk-go/httpclient"
"github.com/databricks/databricks-sdk-go/retries"
"github.com/pkg/browser"
"golang.org/x/oauth2"
@ -42,6 +44,7 @@ var ( // Databricks SDK API: `databricks OAuth is not` will be checked for prese
type PersistentAuth struct {
Host string
AccountID string
Profile string
http httpGet
cache tokenCache
@ -82,6 +85,7 @@ func (a *PersistentAuth) Load(ctx context.Context) (*oauth2.Token, error) {
return nil, err
}
// eagerly refresh token
ctx = context.WithValue(ctx, oauth2.HTTPClient, a.http)
refreshed, err := cfg.TokenSource(ctx, t).Token()
if err != nil {
return nil, fmt.Errorf("token refresh: %w", err)
@ -96,7 +100,9 @@ func (a *PersistentAuth) Load(ctx context.Context) (*oauth2.Token, error) {
}
func (a *PersistentAuth) ProfileName() string {
// TODO: get profile name from interactive input
if a.Profile != "" {
return a.Profile
}
if a.AccountID != "" {
return fmt.Sprintf("ACCOUNT-%s", a.AccountID)
}
@ -138,7 +144,25 @@ func (a *PersistentAuth) init(ctx context.Context) error {
return ErrFetchCredentials
}
if a.http == nil {
a.http = http.DefaultClient
c := &config.Config{
Profile: a.Profile,
Host: a.Host,
AccountID: a.AccountID,
}
c.EnsureResolved()
clientConfig := httpclient.ClientConfig{
DebugHeaders: c.DebugHeaders,
DebugTruncateBytes: c.DebugTruncateBytes,
InsecureSkipVerify: c.InsecureSkipVerify,
CABundle: c.CABundle,
RetryTimeout: time.Duration(c.RetryTimeoutSeconds) * time.Second,
HTTPTimeout: time.Duration(c.HTTPTimeoutSeconds) * time.Second,
}
httpClient, err := httpclient.NewHttpClient(clientConfig)
if err != nil {
return err
}
a.http = httpClient
}
if a.cache == nil {
a.cache = &cache.TokenCache{}