-
Notifications
You must be signed in to change notification settings - Fork 41
/
auth_gcp_google_credentials.go
61 lines (55 loc) · 1.8 KB
/
auth_gcp_google_credentials.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
package config
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"os"
"github.com/databricks/databricks-sdk-go/logger"
"golang.org/x/oauth2/google"
"google.golang.org/api/idtoken"
"google.golang.org/api/option"
)
type GoogleCredentials struct {
}
func (c GoogleCredentials) Name() string {
return "google-credentials"
}
func (c GoogleCredentials) Configure(ctx context.Context, cfg *Config) (func(*http.Request) error, error) {
if cfg.GoogleCredentials == "" || !cfg.IsGcp() {
return nil, nil
}
json, err := readCredentials(cfg.GoogleCredentials)
if err != nil {
err = fmt.Errorf("could not read GoogleCredentials. "+
"Make sure the file exists, or the JSON content is valid: %w", err)
return nil, err
}
inner, err := idtoken.NewTokenSource(ctx, cfg.Host, option.WithCredentialsJSON(json))
if err != nil {
return nil, fmt.Errorf("could not obtain OIDC token from JSON: %w", err)
}
// Obtain token source for creating Google Cloud Platform token.
creds, err := google.CredentialsFromJSON(ctx, json,
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/compute")
if err != nil {
return nil, fmt.Errorf("could not obtain OAuth2 token from JSON: %w", err)
}
logger.Infof(ctx, "Using Google Credentials")
return serviceToServiceVisitor(inner, creds.TokenSource, "X-Databricks-GCP-SA-Access-Token"), nil
}
// Reads credentials as JSON. Credentials can be either a path to JSON file,
// or actual JSON string.
func readCredentials(credentials string) ([]byte, error) {
// Try to read credentials as file path.
if _, err := os.Stat(credentials); err == nil {
jsonContents, err := ioutil.ReadFile(credentials)
if err != nil {
return jsonContents, err
}
return jsonContents, nil
}
// Assume that credential is actually JSON string.
return []byte(credentials), nil
}