Skip to content
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

[Feature] Implement U2M Authentication in the Go SDK #1108

Open
wants to merge 41 commits into
base: main
Choose a base branch
from

Conversation

mgyucht
Copy link
Contributor

@mgyucht mgyucht commented Jan 3, 2025

What changes are proposed in this pull request?

This PR moves logic about U2M OAuth login from the CLI to the Go SDK. This eliminates a cyclic dependency between the SDK and CLI for interacting with the OAuth token cache and enables U2M support directly in the Go SDK without need for the CLI to be installed.

Most of this code is carried over from the CLI, but I have made specific refactors to generalize it where needed.

Currently, the token cache key follows a specific structure:

  • https://<accounts-host>/oidc/accounts/<account-id> for account-based sessions
  • https://<workspace host> for workspace-based sessions
    This can be generalized to allow callers to cache tokens in other manners. For example, users may want to cache tokens per principal or per set of OAuth scopes for their own OAuth applications.

Additionally, products should be able to use an isolated token cache or a token cache backed by a different storage medium, like a database. This is simple to do by introducing a token cache interface. Implementers can define their own credential strategy and reuse the PersistentAuth type to handle the negotiation.

How is this tested?

Carried over tests from the CLI.

More tests are forthcoming, once a decision is made on this approach.

@mgyucht mgyucht temporarily deployed to test-trigger-is January 3, 2025 10:51 — with GitHub Actions Inactive
@mgyucht mgyucht temporarily deployed to test-trigger-is January 3, 2025 10:52 — with GitHub Actions Inactive
@mgyucht mgyucht temporarily deployed to test-trigger-is January 3, 2025 12:18 — with GitHub Actions Inactive
@mgyucht mgyucht temporarily deployed to test-trigger-is January 3, 2025 12:18 — with GitHub Actions Inactive
Copy link
Contributor

@renaudhartert-db renaudhartert-db left a comment

Choose a reason for hiding this comment

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

Thanks for the PR — always good to get the SDK more self-contained.

tokenCacheVersion = 1
)

var ErrNotConfigured = errors.New("databricks OAuth is not configured for this host")
Copy link
Contributor

Choose a reason for hiding this comment

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

Move in cache.go?

)

const (
// where the token cache is stored
Copy link
Contributor

@renaudhartert-db renaudhartert-db Jan 3, 2025

Choose a reason for hiding this comment

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

I realize you didn't write these comments. Though, let's remain consistent by writing them as full sentences (with majuscule and period). Ref: https://go.dev/wiki/CodeReviewComments#comment-sentences

Comment on lines 24 to 25
// format versioning leaves some room for format improvement
tokenCacheVersion = 1
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe explain a little more what problem this is solving? It's not super clear from looking at this definition.

// https://datatracker.ietf.org/doc/html/rfc6749
type OAuthToken struct {
// The access token issued by the authorization server. This is the token that will be used to authenticate requests.
AccessToken string `json:"access_token" auth:",sensitive"`
Copy link
Contributor

Choose a reason for hiding this comment

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

What is auth:",sensitive"? I don't remember seeing this annotation before.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In some cases (like failed authentication), debug logs will include configuration parameters. Config attributes marked as sensitive are redacted in these logs. I don't think that also applies to this struct though. I'll take a closer look here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good observation: this is only used for attributes of the Config object, to determine whether they are omitted when the config is printed out. I will remove this here.

@@ -22,6 +21,19 @@ type GetOAuthTokenRequest struct {
Assertion string `url:"assertion"`
}

// OAuthToken represents an OAuth token as defined by the OAuth 2.0 Authorization Framework.
// https://datatracker.ietf.org/doc/html/rfc6749
type OAuthToken struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

Use oauth2.Token instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oauth2.Token has an "Expiry" field, which is computed based on the "expires_in" field. This mimics the unexported tokenJSON type from that library.

Comment on lines 85 to 88
loc, err := c.location()
if err != nil {
return err
}
Copy link
Contributor

@renaudhartert-db renaudhartert-db Jan 3, 2025

Choose a reason for hiding this comment

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

[nit] What location was doing wasn't obvious to me reading this line. We could make that clearer with a better name. For example tokenCacheFilepath(). Though, I think it might just be simpler to inline the code since it results in the same number of line minus the location method.

Suggested change
loc, err := c.location()
if err != nil {
return err
}
home, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("could not access home dir: %w")
}
c.fileLocation = filepath.Join(home, tokenCacheFileName)

Comment on lines 30 to 32
// this implementation requires the calling code to do a machine-wide lock,
// otherwise the file might get corrupt.
type FileTokenCache struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we briefly explain what this cache does? Something like:

// FileTokenCache caches tokens in "~/.databricks/token-cache.json". FileTokenCache
// implements the TokenCache interface.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great, will use this description. Sorry, most of this is copied directly from the CLI without carefully reviewing for style or comments, but thanks for pointing these out, and I'm happy to correct them here.

Comment on lines 41 to 49
if errors.Is(err, fs.ErrNotExist) {
dir := filepath.Dir(c.fileLocation)
err = os.MkdirAll(dir, ownerExecReadWrite)
if err != nil {
return fmt.Errorf("mkdir: %w", err)
}
} else if err != nil {
return fmt.Errorf("load: %w", err)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

[optional] This is slightly more idiomatic to test for nil error first.

Suggested change
if errors.Is(err, fs.ErrNotExist) {
dir := filepath.Dir(c.fileLocation)
err = os.MkdirAll(dir, ownerExecReadWrite)
if err != nil {
return fmt.Errorf("mkdir: %w", err)
}
} else if err != nil {
return fmt.Errorf("load: %w", err)
}
if err != nil {
if !errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("load: %w", err)
}
dir := filepath.Dir(c.fileLocation)
if err := os.MkdirAll(dir, ownerExecReadWrite); err != nil {
return fmt.Errorf("mkdir: %w", err)
}
}

var ErrNotConfigured = errors.New("databricks OAuth is not configured for this host")

// this implementation requires the calling code to do a machine-wide lock,
// otherwise the file might get corrupt.
Copy link
Contributor

Choose a reason for hiding this comment

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

Naively, I would have expected the lock to be managed by this cache implementation as an internal detail. Having the lock managed by the client sounds a little bug prone — especially given that the client does not have a programmatic way to know in what file the cache is storing the tokens. Am I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question. It needs to be at least locked in PersistentAuth: the Load() method will read from the cache and then, if the access token is expired, refresh it and update the cache, which replaces it entirely. That requires some form of user-wide mutual exclusion that lasts longer than an individual method call, thus the lock file. I don't see any issues with adding an in-memory mutex within FileTokenCache as well in case an application is directly managing and updating tokens, considering what that would be protecting against.

// Cache is the token cache to store and lookup tokens.
cache cache.TokenCache
// Locker is the lock to synchronize token cache access.
locker sync.Locker
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like locker is never locked. Am I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're not missing anything, I forgot to add it before, but I have it locally and will include it in my next update.

@renaudhartert-db
Copy link
Contributor

Added a few comments, specifically around the cache package.

My remaining concerns are about the new oauth package and I think it would take time to review it properly, taking the whole "auth" library in consideration.

If there's an easy way to provide a first implementation of u2m that does not rely on the oauth package, then I would recommend doing this first, and consider introducing the oauth package as a follow-up.

Discussed offline:

  • I misunderstood the scope of this package as being more general than u2m.
  • We agreed to make it clear that the package is specifically designed for u2m.

fileLocation string

// locker protects the token cache file from concurrent reads and writes.
locker sync.Locker
Copy link
Contributor

Choose a reason for hiding this comment

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

No need for an interface, let's use a mutex directly. The zero value of a mutex is a valid mutex so we do not even need to think about initialization anymore.

Suggested change
locker sync.Locker
locker sync.Mutex

}

// Store implements the TokenCache interface.
func (c *fileTokenCache) Store(key string, t *oauth2.Token) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the intended behavior when storing a nil token? Should we delete it from the cache?

delete(key, f.Tokens)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question. I think that's a reasonable implementation.

Comment on lines 3 to 18
// InvalidRefreshTokenError is returned from PersistentAuth's Load() method
// if the access token has expired and the refresh token in the token cache
// is invalid.
type InvalidRefreshTokenError struct {
err error
}

func (e *InvalidRefreshTokenError) Error() string {
return e.err.Error()
}

func (e *InvalidRefreshTokenError) Unwrap() error {
return e.err
}

var _ error = &InvalidRefreshTokenError{}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this could just be this:

Suggested change
// InvalidRefreshTokenError is returned from PersistentAuth's Load() method
// if the access token has expired and the refresh token in the token cache
// is invalid.
type InvalidRefreshTokenError struct {
err error
}
func (e *InvalidRefreshTokenError) Error() string {
return e.err.Error()
}
func (e *InvalidRefreshTokenError) Unwrap() error {
return e.err
}
var _ error = &InvalidRefreshTokenError{}
// InvalidRefreshTokenError is returned from PersistentAuth's Load() method
// if the access token has expired and the refresh token in the token cache
// is invalid.
type InvalidRefreshTokenError struct {
error
}

// a special error message that instructs the user how to reauthenticate.
type u2mCredentials struct {
// auth is the persistent auth object that manages the token cache.
auth *u2m.PersistentAuth
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this just be a token source?

Suggested change
auth *u2m.PersistentAuth
ts oauth2.TokenSource

// callback server listens for the redirect from the identity provider and
// exchanges the authorization code for an access token. It returns the OAuth2
// token on success.
func (a *PersistentAuth) Challenge() (*oauth2.Token, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This method does not seem to be used. Also, its documentation does not look up to date. Is it necessary to implement some interface?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the core routine that handles the U2M OAuth login. There isn't a specific interface that it implements. It might be reasonable to consider this to be a token source as well

return nil
}

func (a *PersistentAuth) Close() error {
Copy link
Contributor

Choose a reason for hiding this comment

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

This method does not seem to be used publicly, can we make it private?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It will be used by the CLI and by any clients who want to use U2M but want to manage the token cache independently from the CLI.

}

// NewPersistentAuth creates a new PersistentAuth with the provided options.
func NewPersistentAuth(ctx context.Context, opts ...PersistentAuthOption) (*PersistentAuth, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this return a TokenSource so that the implementation is hidden? Ultimately, it does not look like the Challenge and Close methods are used outside of this package.

cfg: &Config{
Host: "https://myworkspace.cloud.databricks.com",
},
auth: must(
Copy link
Contributor

Choose a reason for hiding this comment

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

I would have naturally leaned toward mocking PersistentAuth rather than the OAuthClient — especially if PersitentAuth is treated as a TokenSource (see other comments in persistent_auth.go).

Comment on lines +15 to +16
// GetHttpClient returns an HTTP client for OAuth2 requests.
GetHttpClient(context.Context) *http.Client
Copy link
Contributor

Choose a reason for hiding this comment

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

This method bothers me. I'm wondering if we would not be better off passing the HTTPClient directly to PersistentAuth. I'm also wondering if this interface needs to be public (see comments about mocking PersistentAuth in auth_u2m_test.go).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can split the OAuth endpoints and the HTTP client into separate parameters. However, because oauth2 depends on an http.Client, and to preserve the existing behavior, we do need to pass in the existing ApiClient somehow.

In an ideal world, we would have a pipelined API client specifically for OAuth that would be independent of the ApiClient package. We could actually just duplicate the code in order to break that dependency.

Copy link

If integration tests don't run automatically, an authorized user can run them manually by following the instructions below:

Trigger:
go/deco-tests-run/sdk-go

Inputs:

  • PR number: 1108
  • Commit SHA: 2f0ebbb9153a2c180fe9f5c034e61639f398ce72

Checks will be approved automatically on success.

@mgyucht mgyucht deployed to test-trigger-is February 14, 2025 15:59 — with GitHub Actions Active
Comment on lines 28 to 41
// Auth is the persistent auth object to use. If not specified, a new one will
// be created, using the default cache and locker.
Auth *oauth.PersistentAuth

// GetOAuthArg is a function that returns the OAuth argument to use for
// loading the OAuth session token. If not specified, the OAuth argument is
// determined by the account host and account ID or workspace host in the
// Config.
GetOAuthArg func(context.Context, *Config) (oauth.OAuthArgument, error)

// ErrorHandler controls the behavior of Configure() when loading the OAuth
// token fails. If not specified, any error will cause Configure() to return
// said error.
ErrorHandler func(context.Context, *Config, oauth.OAuthArgument, error) error
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's make these unexported (ditto for other types introduced in this PR).

Comment on lines 61 to 62
// mu protects the token cache file from concurrent reads and writes.
mu *sync.Mutex
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This should be the filemutex

// Cache is the token cache to store and lookup tokens.
cache cache.TokenCache
// Locker is the lock to synchronize token cache access.
locker sync.Locker
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We remove this.

@@ -22,6 +21,19 @@ type GetOAuthTokenRequest struct {
Assertion string `url:"assertion"`
}

// OAuthToken represents an OAuth token as defined by the OAuth 2.0 Authorization Framework.
// https://datatracker.ietf.org/doc/html/rfc6749
type OAuthToken struct {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unexport, explicit reason why this exists


func (a *PersistentAuth) randomString(size int) string {
raw := make([]byte, size)
_, _ = rand.Read(raw)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

comment

// loading the OAuth session token. If not specified, the OAuth argument is
// determined by the account host and account ID or workspace host in the
// Config.
getOAuthArg func(context.Context, *Config) (oauth.OAuthArgument, error)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is how we will support scopes. We'll need a way to determine which key in the token cache the user is intending to use based on the config. It's intentionally unexported now to indicate that we don't really want to support it yet, but it would be the narrow place to make the change later.

}

// Store implements the TokenCache interface.
func (c *fileTokenCache) Store(key string, t *oauth2.Token) error {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question. I think that's a reasonable implementation.

// callback server listens for the redirect from the identity provider and
// exchanges the authorization code for an access token. It returns the OAuth2
// token on success.
func (a *PersistentAuth) Challenge() (*oauth2.Token, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the core routine that handles the U2M OAuth login. There isn't a specific interface that it implements. It might be reasonable to consider this to be a token source as well

return nil
}

func (a *PersistentAuth) Close() error {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It will be used by the CLI and by any clients who want to use U2M but want to manage the token cache independently from the CLI.

Comment on lines +15 to +16
// GetHttpClient returns an HTTP client for OAuth2 requests.
GetHttpClient(context.Context) *http.Client
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can split the OAuth endpoints and the HTTP client into separate parameters. However, because oauth2 depends on an http.Client, and to preserve the existing behavior, we do need to pass in the existing ApiClient somehow.

In an ideal world, we would have a pipelined API client specifically for OAuth that would be independent of the ApiClient package. We could actually just duplicate the code in order to break that dependency.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants