-
Notifications
You must be signed in to change notification settings - Fork 50
[Feature] Implement U2M Authentication in the Go SDK #1108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
4a538e3
70ef496
3550329
05bd576
0688c3b
35b8365
47f2fb2
e883616
181f88b
74592c3
9b5913c
73c73af
63bf521
2622989
359a5d0
44af89f
bd7303e
323fb61
3905c5d
f53fb84
ef8c3f3
0ff447d
6c2ed96
e9f3732
19a34ce
c7b5155
37b44b1
36ea3dc
fc87393
945151f
356a7c9
7441bd1
3550bba
fcb031f
83e4141
51a5b08
62acd68
f506510
d70dac4
373daf3
2f0ebbb
21df9ec
6ff1f3d
f08c2b9
de60fcf
11380f0
44c2ca7
45b7e55
51da35a
e37d457
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package config | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/databricks/databricks-sdk-go/credentials" | ||
"github.com/databricks/databricks-sdk-go/credentials/oauth" | ||
"github.com/databricks/databricks-sdk-go/logger" | ||
) | ||
|
||
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) { | ||
a := u.Auth | ||
if a == nil { | ||
var err error | ||
a, err = oauth.NewPersistentAuth(ctx) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we update |
||
if err != nil { | ||
logger.Debugf(ctx, "failed to create persistent auth: %v, continuing", err) | ||
return nil, nil | ||
} | ||
} | ||
f := func(r *http.Request) error { | ||
arg := oauth.BasicOAuthArgument{ | ||
Host: cfg.Host, | ||
AccountID: cfg.AccountID, | ||
} | ||
token, err := a.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) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nit] Move this on top of the |
||
// 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{} |
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) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is moved to credentials/oauth/oidc.go: https://github.com/databricks/databricks-sdk-go/pull/1108/files#diff-dbbf458b1ab6c594fd438f9281715b863447375ede0366e94a7159052c240defR1. It has been separated into two methods.