forked from databricks/databricks-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_default.go
64 lines (55 loc) · 1.57 KB
/
auth_default.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package config
import (
"context"
"errors"
"fmt"
"net/http"
"github.com/xuxiaoshuo/databricks-sdk-go/logger"
)
var (
authProviders = []CredentialsProvider{
PatCredentials{},
BasicCredentials{},
M2mCredentials{},
DatabricksCliCredentials{},
MetadataServiceCredentials{},
// Attempt to configure auth from most specific to most generic (the Azure CLI).
AzureMsiCredentials{},
AzureClientSecretCredentials{},
AzureCliCredentials{},
// Attempt to configure auth from most specific to most generic (Google Application Default Credentials).
GoogleCredentials{},
GoogleDefaultCredentials{},
}
)
type DefaultCredentials struct {
name string
}
func (c *DefaultCredentials) Name() string {
if c.name == "" {
return "default"
}
return c.name
}
// ErrCannotConfigureAuth (experimental) is returned when no auth is configured
var ErrCannotConfigureAuth = errors.New("cannot configure default credentials")
func (c *DefaultCredentials) Configure(ctx context.Context, cfg *Config) (func(*http.Request) error, error) {
for _, p := range authProviders {
if cfg.AuthType != "" && p.Name() != cfg.AuthType {
// ignore other auth types if one is explicitly enforced
logger.Infof(ctx, "Ignoring %s auth, because %s is preferred", p.Name(), cfg.AuthType)
continue
}
logger.Tracef(ctx, "Attempting to configure auth: %s", p.Name())
visitor, err := p.Configure(ctx, cfg)
if err != nil {
return nil, fmt.Errorf("%s: %w", p.Name(), err)
}
if visitor == nil {
continue
}
c.name = p.Name()
return visitor, nil
}
return nil, ErrCannotConfigureAuth
}