-
Notifications
You must be signed in to change notification settings - Fork 273
Expand file tree
/
Copy pathgcloud_token_provider.go
More file actions
121 lines (107 loc) · 3.68 KB
/
gcloud_token_provider.go
File metadata and controls
121 lines (107 loc) · 3.68 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"encoding/json"
"fmt"
"os"
"time"
"k8s.io/klog/v2"
)
// gcloudConfiguration holds types unmarshaled from gcloud config in json format
type gcloudConfiguration struct {
Credential struct {
AccessToken string `json:"access_token"`
TokenExpiry time.Time `json:"token_expiry"`
} `json:"credential"`
Configuration struct {
Properties struct {
Auth struct {
AuthorizationTokenFile string `json:"authorization_token_file"`
} `json:"auth"`
} `json:"properties"`
} `json:"configuration"`
}
// gcloudTokenProvider provides gcloud OAth 2.0 tokens.
type gcloudTokenProvider struct {
readGcloudConfigRaw func(args []string) ([]byte, error)
readFile func(filename string) ([]byte, error)
account string
project string
impersonateServiceAccount string
configuration string
}
// readGcloudConfig returns an object which represents gcloud config output
func (p *gcloudTokenProvider) readGcloudConfig(extraArgs []string) (*gcloudConfiguration, error) {
gcloudConfigBytes, err := p.readGcloudConfigRaw(extraArgs)
if err != nil {
return nil, err
}
var gc gcloudConfiguration
if err := json.Unmarshal(gcloudConfigBytes, &gc); err != nil {
return nil, fmt.Errorf("error parsing gcloud output: %w", err)
}
return &gc, nil
}
func (p *gcloudTokenProvider) token() (string, *time.Time, error) {
cloudsdkAuthAccessToken := os.Getenv(cloudsdkAuthAccessEnvVar)
if cloudsdkAuthAccessToken != "" {
klog.V(4).Infof("Returning token from Environment Variable CLOUDSDK_AUTH_ACCESS_TOKEN as it is populated")
return cloudsdkAuthAccessToken, &time.Time{}, nil
}
gcloudArgs := p.getGcloudArgs()
gc, err := p.readGcloudConfig(gcloudArgs)
if err != nil {
return "", nil, err
}
if gc.Credential.AccessToken == "" {
return "", nil, fmt.Errorf("gcloud config config-helper returned an empty access token")
}
// Authorization Token File is not commonly used. Currently, this is for specific internal debugging scenarios.
token := gc.Credential.AccessToken
var authzTokenFile string
var authzTokenBytes []byte
if authzTokenFile = gc.Configuration.Properties.Auth.AuthorizationTokenFile; authzTokenFile != "" {
authzTokenBytes, err = p.readFile(authzTokenFile)
if err != nil {
return "", nil, fmt.Errorf("gcloud config sets property auth/authorization_token_file, but can't read file at %s: %w", authzTokenFile, err)
}
token = fmt.Sprintf("iam-%s^%s", token, authzTokenBytes)
}
return token, &gc.Credential.TokenExpiry, nil
}
func (p *gcloudTokenProvider) useCache() bool {
cloudsdkAuthAccessToken := os.Getenv(cloudsdkAuthAccessEnvVar)
if cloudsdkAuthAccessToken != "" {
klog.V(4).Infof("cache is not being used as %s is populated", cloudsdkAuthAccessEnvVar)
return false
}
return true
}
func (p *gcloudTokenProvider) getExtraArgs() []string {
extraArgs := make([]string, 0, 4)
if p.project != "" {
extraArgs = append(extraArgs, fmt.Sprintf("--project=%s", p.project))
}
if p.account != "" {
extraArgs = append(extraArgs, fmt.Sprintf("--account=%s", p.account))
}
if p.impersonateServiceAccount != "" {
extraArgs = append(extraArgs, fmt.Sprintf("--impersonate-service-account=%s", p.impersonateServiceAccount))
}
if p.configuration != "" {
extraArgs = append(extraArgs, fmt.Sprintf("--configuration=%s", p.configuration))
}
return extraArgs
}
func (p *gcloudTokenProvider) getGcloudArgs() []string {
args := []string{
"config",
"config-helper",
"--format=json",
}
args = append(args, p.getExtraArgs()...)
return args
}
func readGcloudConfigRaw(args []string) ([]byte, error) {
klog.V(4).Infof("Executing gcloud command with args: %v", args)
return executeCommand("gcloud", args...)
}