1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package testutil
17
18 import (
19 "context"
20 "errors"
21 "fmt"
22 "log"
23 "os"
24
25 "golang.org/x/oauth2"
26 "golang.org/x/oauth2/google"
27 "golang.org/x/oauth2/jwt"
28 "google.golang.org/api/impersonate"
29 )
30
31 const (
32 envProjID = "GCLOUD_TESTS_GOLANG_PROJECT_ID"
33 envPrivateKey = "GCLOUD_TESTS_GOLANG_KEY"
34 envImpersonate = "GCLOUD_TESTS_IMPERSONATE_CREDENTIALS"
35 )
36
37
38
39 func ProjID() string {
40 return os.Getenv(envProjID)
41 }
42
43
44
45
46 func Credentials(ctx context.Context, scopes ...string) *google.Credentials {
47 return CredentialsEnv(ctx, envPrivateKey, scopes...)
48 }
49
50
51
52
53
54
55 func CredentialsEnv(ctx context.Context, envVar string, scopes ...string) *google.Credentials {
56 if impKey := os.Getenv(envImpersonate); impKey == "true" {
57 return &google.Credentials{
58 TokenSource: impersonatedTokenSource(ctx, scopes),
59 ProjectID: "dulcet-port-762",
60 }
61 }
62 key := os.Getenv(envVar)
63 if key == "" {
64 creds, err := google.FindDefaultCredentials(ctx, scopes...)
65 if err != nil {
66 log.Println("No 'Application Default Credentials' found.")
67 return nil
68 }
69 return creds
70 }
71
72 data, err := os.ReadFile(key)
73 if err != nil {
74 log.Fatal(err)
75 }
76
77 creds, err := google.CredentialsFromJSON(ctx, data, scopes...)
78 if err != nil {
79 log.Fatal(err)
80 }
81 return creds
82 }
83
84
85
86
87 func TokenSource(ctx context.Context, scopes ...string) oauth2.TokenSource {
88 return TokenSourceEnv(ctx, envPrivateKey, scopes...)
89 }
90
91
92
93
94
95
96
97 func TokenSourceEnv(ctx context.Context, envVar string, scopes ...string) oauth2.TokenSource {
98 if impKey := os.Getenv(envImpersonate); impKey == "true" {
99 return impersonatedTokenSource(ctx, scopes)
100 }
101 key := os.Getenv(envVar)
102 if key == "" {
103 ts, err := google.DefaultTokenSource(ctx, scopes...)
104 if err != nil {
105 log.Println("No 'Application Default Credentials' found.")
106 return nil
107 }
108 return ts
109 }
110 conf, err := jwtConfigFromFile(key, scopes)
111 if err != nil {
112 log.Fatal(err)
113 }
114 return conf.TokenSource(ctx)
115 }
116
117 func impersonatedTokenSource(ctx context.Context, scopes []string) oauth2.TokenSource {
118 ts, err := impersonate.CredentialsTokenSource(ctx, impersonate.CredentialsConfig{
119 TargetPrincipal: "kokoro@dulcet-port-762.iam.gserviceaccount.com",
120 Scopes: scopes,
121 })
122 if err != nil {
123 log.Fatalf("Unable to impersonate credentials, exiting: %v", err)
124 }
125 return ts
126 }
127
128
129
130
131
132 func JWTConfig() (*jwt.Config, error) {
133 return jwtConfigFromFile(os.Getenv(envPrivateKey), nil)
134 }
135
136
137
138
139 func jwtConfigFromFile(filename string, scopes []string) (*jwt.Config, error) {
140 if filename == "" {
141 return nil, nil
142 }
143 jsonKey, err := os.ReadFile(filename)
144 if err != nil {
145 return nil, fmt.Errorf("cannot read the JSON key file, err: %v", err)
146 }
147 conf, err := google.JWTConfigFromJSON(jsonKey, scopes...)
148 if err != nil {
149 return nil, fmt.Errorf("google.JWTConfigFromJSON: %v", err)
150 }
151 return conf, nil
152 }
153
154
155
156
157 func CanReplay(replayFilename string) bool {
158 if os.Getenv("GCLOUD_TESTS_GOLANG_ENABLE_REPLAY") == "" {
159 return false
160 }
161 _, err := os.Stat(replayFilename)
162 return err == nil
163 }
164
165
166
167
168 type ErroringTokenSource struct{}
169
170
171 func (fts ErroringTokenSource) Token() (*oauth2.Token, error) {
172 return nil, errors.New("intentional error")
173 }
174
View as plain text