Skip to content

Commit

Permalink
work
Browse files Browse the repository at this point in the history
  • Loading branch information
mgyucht committed Jan 3, 2025
1 parent 2399d72 commit 4a538e3
Show file tree
Hide file tree
Showing 23 changed files with 1,175 additions and 279 deletions.
123 changes: 0 additions & 123 deletions config/auth_databricks_cli.go

This file was deleted.

108 changes: 0 additions & 108 deletions config/auth_databricks_cli_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion config/auth_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var authProviders = []CredentialsStrategy{
PatCredentials{},
BasicCredentials{},
M2mCredentials{},
DatabricksCliCredentials{},
U2MCredentials{},
MetadataServiceCredentials{},

// Attempt to configure auth from most specific to most generic (the Azure CLI).
Expand Down
30 changes: 1 addition & 29 deletions config/auth_m2m.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"golang.org/x/oauth2/clientcredentials"

"github.com/databricks/databricks-sdk-go/credentials"
"github.com/databricks/databricks-sdk-go/httpclient"
"github.com/databricks/databricks-sdk-go/logger"
)

Expand All @@ -26,7 +25,7 @@ func (c M2mCredentials) Configure(ctx context.Context, cfg *Config) (credentials
if cfg.ClientID == "" || cfg.ClientSecret == "" {
return nil, nil
}
endpoints, err := oidcEndpoints(ctx, cfg)
endpoints, err := cfg.refreshClient.GetOidcEndpoints(ctx, cfg.Host, cfg.AccountID)
if err != nil {
return nil, fmt.Errorf("oidc: %w", err)
}
Expand All @@ -41,30 +40,3 @@ func (c M2mCredentials) Configure(ctx context.Context, cfg *Config) (credentials
visitor := refreshableVisitor(ts)
return credentials.NewOAuthCredentialsProvider(visitor, ts.Token), nil
}

func oidcEndpoints(ctx context.Context, cfg *Config) (*oauthAuthorizationServer, error) {
prefix := cfg.Host
if cfg.IsAccountClient() && cfg.AccountID != "" {
// TODO: technically, we could use the same config profile for both workspace
// and account, but we have to add logic for determining accounts host from
// workspace host.
prefix := fmt.Sprintf("%s/oidc/accounts/%s", cfg.Host, cfg.AccountID)
return &oauthAuthorizationServer{
AuthorizationEndpoint: fmt.Sprintf("%s/v1/authorize", prefix),
TokenEndpoint: fmt.Sprintf("%s/v1/token", prefix),
}, nil
}
oidc := fmt.Sprintf("%s/oidc/.well-known/oauth-authorization-server", prefix)
var oauthEndpoints oauthAuthorizationServer
err := cfg.refreshClient.Do(ctx, "GET", oidc,
httpclient.WithResponseUnmarshal(&oauthEndpoints))
if err != nil {
return nil, errOAuthNotSupported
}
return &oauthEndpoints, nil
}

type oauthAuthorizationServer struct {
AuthorizationEndpoint string `json:"authorization_endpoint"` // ../v1/authorize
TokenEndpoint string `json:"token_endpoint"` // ../v1/token
}
3 changes: 2 additions & 1 deletion config/auth_m2m_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/url"
"testing"

"github.com/databricks/databricks-sdk-go/httpclient"
"github.com/databricks/databricks-sdk-go/httpclient/fixtures"
"github.com/stretchr/testify/require"
"golang.org/x/oauth2"
Expand All @@ -16,7 +17,7 @@ func TestM2mHappyFlow(t *testing.T) {
ClientSecret: "c",
HTTPTransport: fixtures.MappingTransport{
"GET /oidc/.well-known/oauth-authorization-server": {
Response: oauthAuthorizationServer{
Response: httpclient.OAuthAuthorizationServer{
AuthorizationEndpoint: "https://localhost:1234/dummy/auth",
TokenEndpoint: "https://localhost:1234/dummy/token",
},
Expand Down
49 changes: 49 additions & 0 deletions config/auth_u2m.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package config

import (
"context"
"fmt"
"net/http"

"github.com/databricks/databricks-sdk-go/credentials"
"github.com/databricks/databricks-sdk-go/credentials/oauth"
)

type U2MCredentials struct {
Auth oauth.PersistentAuth
}

// Name implements CredentialsStrategy.
func (u U2MCredentials) Name() string {
return "oauth-u2m"
}

// Configure implements CredentialsStrategy.
func (u U2MCredentials) Configure(ctx context.Context, cfg *Config) (credentials.CredentialsProvider, error) {
f := func(r *http.Request) error {
arg := oauth.BasicOAuthArgument{
Host: cfg.Host,
AccountID: cfg.AccountID,
}
token, err := u.Auth.Load(r.Context(), arg)
if err != nil {
return fmt.Errorf("oidc: %w", err)
}
r.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token.AccessToken))
return nil
}

r, err := http.NewRequestWithContext(ctx, http.MethodGet, "", nil)
if err != nil {
return nil, fmt.Errorf("http request: %w", err)
}
// Try to load the credential from the token cache. If absent, fall back
// to the next credentials strategy.
if err := f(r); err != nil {
return nil, nil
}

return credentials.NewCredentialsProvider(f), nil
}

var _ CredentialsStrategy = U2MCredentials{}
10 changes: 10 additions & 0 deletions credentials/cache/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cache

import (
"golang.org/x/oauth2"
)

type TokenCache interface {
Store(key string, t *oauth2.Token) error
Lookup(key string) (*oauth2.Token, error)
}
Loading

0 comments on commit 4a538e3

Please sign in to comment.