Skip to content

Commit e2ff441

Browse files
committed
More comments
1 parent 7f32a67 commit e2ff441

File tree

4 files changed

+32
-39
lines changed

4 files changed

+32
-39
lines changed

config/auth_databricks_oidc.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
// Creates a new Databricks OIDC TokenSource.
16-
func NewDatabricksOIDCTokenSource(cfg *DatabricksOIDCTokenSourceConfig) auth.TokenSource {
16+
func NewDatabricksOIDCTokenSource(cfg DatabricksOIDCTokenSourceConfig) auth.TokenSource {
1717
return &databricksOIDCTokenSource{
1818
cfg: cfg,
1919
}
@@ -27,21 +27,21 @@ type DatabricksOIDCTokenSourceConfig struct {
2727
// [Optional] AccountID is the account ID of the Databricks Account.
2828
// This is only used for Account level tokens.
2929
AccountID string
30-
// Host is the host of the Databricks cluster.
30+
// Host is the host of the Databricks account or workspace.
3131
Host string
32-
// TokenEndpointProvider is a function that returns the token endpoint for the Databricks OIDC application.
32+
// TokenEndpointProvider returns the token endpoint for the Databricks OIDC application.
3333
TokenEndpointProvider func(ctx context.Context) (*u2m.OAuthAuthorizationServer, error)
3434
// Audience is the audience of the Databricks OIDC application.
3535
// This is only used for Workspace level tokens.
3636
Audience string
37-
// IdTokenSource is a function that returns the ID token to be used for the token exchange.
37+
// IdTokenSource returns the IDToken to be used for the token exchange.
3838
IdTokenSource IDTokenSource
3939
}
4040

4141
// databricksOIDCTokenSource is a auth.TokenSource which exchanges a token using
4242
// Workload Identity Federation.
4343
type databricksOIDCTokenSource struct {
44-
cfg *DatabricksOIDCTokenSourceConfig
44+
cfg DatabricksOIDCTokenSourceConfig
4545
}
4646

4747
// Token implements [TokenSource.Token]

config/auth_databricks_oidc_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ func TestDatabricksOidcTokenSource(t *testing.T) {
251251
err: tc.tokenProviderError,
252252
}
253253

254-
cfg := &DatabricksOIDCTokenSourceConfig{
254+
cfg := DatabricksOIDCTokenSourceConfig{
255255
ClientID: tc.clientID,
256256
AccountID: tc.accountID,
257257
Host: tc.host,

config/auth_default.go

+23-30
Original file line numberDiff line numberDiff line change
@@ -10,56 +10,49 @@ import (
1010
)
1111

1212
// Constructs all Databricks OIDC Credentials Strategies
13-
func buildOidcTokenCredentialStrategies(cfg *Config) ([]CredentialsStrategy, error) {
14-
// Maps in Go are unordered, so we need to maintain an order of the strategies.
15-
idTokenSourceOrder := []string{
16-
"github-oidc",
17-
// Add new providers at the end of the list
13+
func buildOidcTokenCredentialStrategies(cfg *Config) []CredentialsStrategy {
14+
type namedIdTokenSource struct {
15+
name string
16+
tokenSource IDTokenSource
1817
}
19-
idTokenSources := map[string]IDTokenSource{
20-
"github-oidc": &githubIDTokenSource{
21-
actionsIDTokenRequestURL: cfg.ActionsIDTokenRequestURL,
22-
actionsIDTokenRequestToken: cfg.ActionsIDTokenRequestToken,
23-
refreshClient: cfg.refreshClient,
18+
idTokenSources := []namedIdTokenSource{
19+
{
20+
name: "github-oidc",
21+
tokenSource: &githubIDTokenSource{
22+
actionsIDTokenRequestURL: cfg.ActionsIDTokenRequestURL,
23+
actionsIDTokenRequestToken: cfg.ActionsIDTokenRequestToken,
24+
refreshClient: cfg.refreshClient,
25+
},
2426
},
2527
// Add new providers at the end of the list
2628
}
27-
2829
strategies := []CredentialsStrategy{}
29-
for _, name := range idTokenSourceOrder {
30-
provider, ok := idTokenSources[name]
31-
if !ok {
32-
return nil, fmt.Errorf("no provider found for %s", name)
33-
}
34-
oidcConfig := &DatabricksOIDCTokenSourceConfig{
30+
for _, idTokenSource := range idTokenSources {
31+
oidcConfig := DatabricksOIDCTokenSourceConfig{
3532
ClientID: cfg.ClientID,
36-
Host: cfg.Host,
33+
Host: cfg.CanonicalHostName(),
3734
TokenEndpointProvider: cfg.getOidcEndpoints,
3835
Audience: cfg.TokenAudience,
39-
IdTokenSource: provider,
36+
IdTokenSource: idTokenSource.tokenSource,
4037
}
4138
if cfg.IsAccountClient() {
4239
oidcConfig.AccountID = cfg.AccountID
4340
}
4441
tokenSource := NewDatabricksOIDCTokenSource(oidcConfig)
45-
strategies = append(strategies, NewTokenSourceStrategy(name, tokenSource))
42+
strategies = append(strategies, NewTokenSourceStrategy(idTokenSource.name, tokenSource))
4643
}
47-
return strategies, nil
44+
return strategies
4845
}
4946

50-
func buildDefaultStrategies(cfg *Config) ([]CredentialsStrategy, error) {
47+
func buildDefaultStrategies(cfg *Config) []CredentialsStrategy {
5148
strategies := []CredentialsStrategy{}
5249
strategies = append(strategies,
5350
PatCredentials{},
5451
BasicCredentials{},
5552
M2mCredentials{},
5653
DatabricksCliCredentials,
5754
MetadataServiceCredentials{})
58-
oidcStrategies, err := buildOidcTokenCredentialStrategies(cfg)
59-
if err != nil {
60-
return nil, err
61-
}
62-
strategies = append(strategies, oidcStrategies...)
55+
strategies = append(strategies, buildOidcTokenCredentialStrategies(cfg)...)
6356
strategies = append(strategies,
6457
// Attempt to configure auth from most specific to most generic (the Azure CLI).
6558
AzureGithubOIDCCredentials{},
@@ -69,7 +62,7 @@ func buildDefaultStrategies(cfg *Config) ([]CredentialsStrategy, error) {
6962
// Attempt to configure auth from most specific to most generic (Google Application Default Credentials).
7063
GoogleCredentials{},
7164
GoogleDefaultCredentials{})
72-
return strategies, nil
65+
return strategies
7366
}
7467

7568
type DefaultCredentials struct {
@@ -90,11 +83,11 @@ var errorMessage = fmt.Sprintf("cannot configure default credentials, please che
9083
var ErrCannotConfigureAuth = errors.New(errorMessage)
9184

9285
func (c *DefaultCredentials) Configure(ctx context.Context, cfg *Config) (credentials.CredentialsProvider, error) {
93-
strategies, err := buildDefaultStrategies(cfg)
86+
err := cfg.EnsureResolved()
9487
if err != nil {
9588
return nil, err
9689
}
97-
for _, p := range strategies {
90+
for _, p := range buildDefaultStrategies(cfg) {
9891
if cfg.AuthType != "" && p.Name() != cfg.AuthType {
9992
// ignore other auth types if one is explicitly enforced
10093
logger.Infof(ctx, "Ignoring %s auth, because %s is preferred", p.Name(), cfg.AuthType)

config/oauth_visitors.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ func serviceToServiceVisitor(primary, secondary oauth2.TokenSource, secondaryHea
3535

3636
// The same as serviceToServiceVisitor, but without a secondary token source.
3737
func refreshableVisitor(inner oauth2.TokenSource) func(r *http.Request) error {
38-
return refreshableAuthVisitor(authconv.AuthTokenSource(inner), context.Background())
38+
return refreshableAuthVisitor(authconv.AuthTokenSource(inner))
3939
}
4040

4141
// The same as serviceToServiceVisitor, but without a secondary token source.
42-
func refreshableAuthVisitor(inner auth.TokenSource, ctx context.Context) func(r *http.Request) error {
42+
func refreshableAuthVisitor(inner auth.TokenSource) func(r *http.Request) error {
4343
cts := auth.NewCachedTokenSource(inner)
4444
return func(r *http.Request) error {
45-
inner, err := cts.Token(ctx)
45+
inner, err := cts.Token(context.Background())
4646
if err != nil {
4747
return fmt.Errorf("inner token: %w", err)
4848
}

0 commit comments

Comments
 (0)