/
auth.go
542 lines (497 loc) · 16.4 KB
/
auth.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package auth implements common functionality to authenticate reclient against GCP.
package auth
import (
"bytes"
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"os"
"os/exec"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/bazelbuild/remote-apis-sdks/go/pkg/digest"
"github.com/bazelbuild/reclient/internal/pkg/features"
"github.com/bazelbuild/reclient/internal/pkg/pathtranslator"
log "github.com/golang/glog"
"golang.org/x/oauth2"
googleOauth "golang.org/x/oauth2/google"
grpcOauth "google.golang.org/grpc/credentials/oauth"
)
// Exit codes to indicate various causes of authentication failure.
const (
// ExitCodeNoAuth is the exit code when no auth option is specified.
ExitCodeNoAuth = 14
// ExitCodeCredsFileAuth is the exit code when there is a failure to authenticate using a credentials file.
ExitCodeCredsFileAuth = 13
// ExitCodeGCECredsAuth is the exit code when there is a failure in GCE credentials.
ExitCodeGCECredsAuth = 12
// ExitCodeExternalTokenAuth is the exit code when there is a failure to authenticate with an external token.
ExitCodeExternalTokenAuth = 11
// ExitCodeAppDefCredsAuth is the exit code when there is a failure to authenticate with ADC.
ExitCodeAppDefCredsAuth = 10
// ExitCodeUnknown is the exit code when there is an unknown auth issue.
ExitCodeUnknown = 19
)
// Mechanism is a mechanism of authentication to the remote execution service.
type Mechanism int
const (
// Unknown is an unknown auth mechanism.
Unknown Mechanism = iota
// CredentialsHelper is using an externally provided binary to get credentials.
CredentialsHelper
// ADC is GCP's application default credentials authentication mechanism.
ADC
// GCE is authentication using GCE VM service accounts.
GCE
// CredentialFile is using service account credentials from a proviced file
CredentialFile
// None implies that the user will not use authentication
None
)
// String returns the string representation of the auth mechanism.
func (m Mechanism) String() string {
switch m {
case Unknown:
return "Unknown"
case CredentialsHelper:
return "CredentialsHelper"
case ADC:
return "ADC"
case GCE:
return "GCE"
case CredentialFile:
return "CredentialFile"
case None:
return "None"
default:
return "Incorrect Value"
}
}
const (
// CredshelperPathFlag is the path to the credentials helper binary.
CredshelperPathFlag = "experimental_credentials_helper"
// CredshelperArgsFlag is the flag used to pass in the arguments to the credentials helper binary.
CredshelperArgsFlag = "experimental_credentials_helper_args"
// TODO(b/261172745): define these flags in reproxy rather than in the SDK.
// UseAppDefaultCredsFlag is used to authenticate with application default credentials.
UseAppDefaultCredsFlag = "use_application_default_credentials"
// UseExternalTokenFlag indicates the user will authenticate with a provided token.
UseExternalTokenFlag = "use_external_auth_token"
// UseGCECredsFlag indicates the user will authenticate with GCE VM credentials.
UseGCECredsFlag = "use_gce_credentials"
// ServiceNoAuthFlag indicates the user will not use authentication
ServiceNoAuthFlag = "service_no_auth"
// CredentialFileFlag indicates the user authenticate with a credential file
CredentialFileFlag = "credential_file"
)
var boolAuthFlags = []string{
UseAppDefaultCredsFlag,
UseGCECredsFlag,
UseExternalTokenFlag,
ServiceNoAuthFlag,
}
var stringAuthFlags = []string{
CredentialFileFlag,
}
var nowFn = time.Now
// Error is an error occured during authenticating or initializing credentials.
type Error struct {
error
// ExitCode is the exit code for the error.
ExitCode int
}
type reusableCmd struct {
path string
args []string
digestOnce sync.Once
digest digest.Digest
}
func newResubaleCmd(binary string, args []string) *reusableCmd {
cmd := exec.Command(binary, args...)
return &reusableCmd{
path: cmd.Path,
args: args,
}
}
func (r *reusableCmd) String() string {
return fmt.Sprintf("%s %v", r.path, strings.Join(r.args, " "))
}
func (r *reusableCmd) Cmd() *exec.Cmd {
return exec.Command(r.path, r.args...)
}
func (r *reusableCmd) Digest() digest.Digest {
r.digestOnce.Do(func() {
chCmd := append(r.args, r.path)
sort.Strings(chCmd)
cmdStr := strings.Join(chCmd, ",")
r.digest = digest.NewFromBlob([]byte(cmdStr))
})
return r.digest
}
// Credentials provides auth functionalities with a specific auth mechanism.
type Credentials struct {
m Mechanism
refreshExp time.Time
tokenSource *grpcOauth.TokenSource
credsHelperCmd *reusableCmd
credsFile string
}
// MechanismFromFlags returns an auth Mechanism based on flags currently set.
func MechanismFromFlags() (Mechanism, error) {
vals := make(map[string]bool, len(boolAuthFlags)+len(stringAuthFlags))
var errs []string
for _, name := range boolAuthFlags {
b, err := boolFlagVal(name)
if err != nil {
errs = append(errs, err.Error())
}
vals[name] = b
}
if len(errs) > 0 {
return Unknown, fmt.Errorf("encountered error(s) parsing auth flags:\n%v", strings.Join(errs, "\n"))
}
for _, name := range stringAuthFlags {
f := flag.Lookup(name)
vals[name] = f != nil && f.Value.String() != ""
}
if vals[ServiceNoAuthFlag] {
return None, nil
}
if vals[CredentialFileFlag] {
return CredentialFile, nil
}
if vals[UseAppDefaultCredsFlag] {
return ADC, nil
}
if vals[UseGCECredsFlag] {
return GCE, nil
}
return Unknown, &Error{fmt.Errorf("couldn't determine auth mechanism from flags %v", vals), ExitCodeNoAuth}
}
// Cacheable returns true if this mechanism should be cached to disk
func (m Mechanism) Cacheable() bool {
if !features.GetConfig().EnableCredentialCache {
return false
}
if m == CredentialsHelper {
return true
}
return false
}
func boolFlagVal(flagName string) (bool, error) {
if f := flag.Lookup(flagName); f != nil && f.Value.String() != "" {
b, err := strconv.ParseBool(f.Value.String())
if err != nil {
return false, fmt.Errorf("unable to parse boolean flag --%s: %w", flagName, err)
}
return b, nil
}
return false, nil
}
// NewCredentials initializes a credentials object.
func NewCredentials(m Mechanism, credsFile string) (*Credentials, error) {
cc, err := loadFromDisk(credsFile)
if err != nil {
log.Warningf("Failed to load credentials cache file from %v: %v", credsFile, err)
return buildCredentials(cachedCredentials{m: m}, credsFile)
}
if cc.m != m {
log.Warningf("Cached mechanism (%v) is not the same as requested mechanism (%v). Will attempt to authenticate using the requested mechanism.", cc.m, m)
return buildCredentials(cachedCredentials{m: m}, credsFile)
}
return buildCredentials(cc, credsFile)
}
func buildCredentials(baseCreds cachedCredentials, credsFile string) (*Credentials, error) {
if baseCreds.m == Unknown {
return nil, errors.New("cannot initialize credentials with unknown mechanism")
}
c := &Credentials{
m: baseCreds.m,
refreshExp: baseCreds.refreshExp,
credsFile: credsFile,
}
return c, nil
}
// build credentials obtained from the credentials helper.
func buildExternalCredentials(baseCreds cachedCredentials, credsFile string, credsHelperCmd *reusableCmd) *Credentials {
c := &Credentials{
m: CredentialsHelper,
credsFile: credsFile,
credsHelperCmd: credsHelperCmd,
}
baseTs := &externalTokenSource{
credsHelperCmd: credsHelperCmd,
}
c.tokenSource = &grpcOauth.TokenSource{
// Wrap the base token source with a ReuseTokenSource so that we only
// generate new credentials when the current one is about to expire.
// This is needed because retrieving the token is expensive and some
// token providers have per hour rate limits.
TokenSource: oauth2.ReuseTokenSourceWithExpiry(
baseCreds.token,
baseTs,
// Refresh tokens 5 mins early to be safe
5*time.Minute,
),
}
return c
}
func loadCredsFromDisk(credsFile string, credsHelperCmd *reusableCmd) (*Credentials, error) {
cc, err := loadFromDisk(credsFile)
if err != nil {
return nil, err
}
cmdDigest := credsHelperCmd.Digest()
if cc.credsHelperCmdDigest != cmdDigest.String() {
return nil, fmt.Errorf("cached credshelper command digest: %s is not the same as requested credshelper command digest: %s",
cc.credsHelperCmdDigest, cmdDigest.String())
}
isExpired := cc.token != nil && cc.token.Expiry.Before(nowFn())
if isExpired {
return nil, fmt.Errorf("cached token is expired at %v", cc.token.Expiry)
}
return buildExternalCredentials(cc, credsFile, credsHelperCmd), nil
}
// SaveToDisk saves credentials to disk.
func (c *Credentials) SaveToDisk() {
if c == nil {
return
}
if !c.m.Cacheable() {
return
}
cc := cachedCredentials{m: c.m, refreshExp: c.refreshExp}
// Since c.tokenSource is always wrapped in a oauth2.ReuseTokenSourceWithExpiry
// this will return a cached credential if one exists.
t, err := c.tokenSource.Token()
if err != nil {
log.Errorf("Failed to get token to persist to disk: %v", err)
return
}
cc.token = t
if c.credsHelperCmd != nil {
cc.credsHelperCmdDigest = c.credsHelperCmd.Digest().String()
}
if err := saveToDisk(cc, c.credsFile); err != nil {
log.Errorf("Failed to save credentials to disk: %v", err)
}
}
// RemoveFromDisk deletes the credentials cache on disk.
func (c *Credentials) RemoveFromDisk() {
if c == nil {
return
}
if err := os.Remove(c.credsFile); err != nil {
log.Errorf("Failed to remove credentials from disk: %v", err)
}
}
// UpdateStatus updates the refresh expiry time if it is expired
func (c *Credentials) UpdateStatus() (int, error) {
if !nowFn().Before(c.refreshExp) && c.m == ADC {
exp, err := checkADCStatus()
if err != nil {
return ExitCodeAppDefCredsAuth, fmt.Errorf("application default credentials were invalid: %v", err)
}
c.refreshExp = exp
}
return 0, nil
}
// ReproxyAuthenticationFlags retrieves the auth flags to use to start reproxy.
func (m Mechanism) ReproxyAuthenticationFlags() []string {
bm := make(map[string]bool, len(boolAuthFlags))
sm := make(map[string]string, len(stringAuthFlags))
for _, f := range boolAuthFlags {
bm[f] = false
}
for _, f := range stringAuthFlags {
sm[f] = ""
}
switch m {
case GCE:
bm[UseGCECredsFlag] = true
case ADC:
bm[UseAppDefaultCredsFlag] = true
case CredentialFile:
if f := flag.Lookup(CredentialFileFlag); f != nil {
sm[CredentialFileFlag] = f.Value.String()
}
case None:
bm[ServiceNoAuthFlag] = true
}
vals := make([]string, 0, len(boolAuthFlags)+len(stringAuthFlags))
for _, f := range boolAuthFlags {
vals = append(vals, fmt.Sprintf("--%v=%v", f, bm[f]))
}
for _, f := range stringAuthFlags {
vals = append(vals, fmt.Sprintf("--%v=%v", f, sm[f]))
}
return vals
}
// Mechanism returns the authentication mechanism of the credentials object.
func (c *Credentials) Mechanism() Mechanism {
if c == nil {
return None
}
return c.m
}
// TokenSource returns a token source for this credentials instance.
// If this credential type does not produce credentials nil will be returned.
func (c *Credentials) TokenSource() *grpcOauth.TokenSource {
if c == nil {
return nil
}
return c.tokenSource
}
func checkADCStatus() (time.Time, error) {
ts, err := googleOauth.FindDefaultCredentialsWithParams(context.Background(), googleOauth.CredentialsParams{
Scopes: []string{"https://www.googleapis.com/auth/cloud-platform"},
EarlyTokenRefresh: 5 * time.Minute,
})
if err != nil {
return time.Time{}, fmt.Errorf("could not find Application Default Credentials: %w", err)
}
token, err := ts.TokenSource.Token()
if err != nil {
aerr, ok := err.(*googleOauth.AuthenticationError)
if !ok {
return time.Time{}, fmt.Errorf("could not get valid Application Default Credentials token: %w", err)
}
if aerr.Temporary() {
log.Errorf("Ignoring temporary ADC error: %v", err)
return time.Time{}, nil
}
rerr, ok := aerr.Unwrap().(*oauth2.RetrieveError)
if !ok {
return time.Time{}, fmt.Errorf("could not get valid Application Default Credentials token: %w", err)
}
var resp struct {
Error string `json:"error"`
ErrorSubtype string `json:"error_subtype"`
}
if err := json.Unmarshal(rerr.Body, &resp); err == nil &&
resp.Error == "invalid_grant" &&
resp.ErrorSubtype == "invalid_rapt" {
return time.Time{}, fmt.Errorf("reauth required, run `gcloud auth application-default login` and try again")
}
return time.Time{}, fmt.Errorf("could not get valid Application Default Credentials token: %w", err)
}
if !token.Valid() {
log.Errorf("Could not get valid Application Default Credentials token: %v", err)
return time.Time{}, fmt.Errorf("could not get valid Application Default Credentials token: %w", err)
}
return token.Expiry, nil
}
// externaltokenSource uses a credentialsHelper to obtain gcp oauth tokens.
// This should be wrapped in a "golang.org/x/oauth2".ReuseTokenSource
// to avoid obtaining new tokens each time.
type externalTokenSource struct {
credsHelperCmd *reusableCmd
}
// Token retrieves an oauth2 token from the external tokensource.
func (ts *externalTokenSource) Token() (*oauth2.Token, error) {
if ts == nil {
return nil, fmt.Errorf("empty tokensource")
}
tk, _, err := runCredsHelperCmd(ts.credsHelperCmd)
if err == nil {
log.Infof("'%s' credentials refreshed at %v, expires at %v", ts.credsHelperCmd, time.Now(), tk.Expiry)
}
return tk, err
}
// NewExternalCredentials creates credentials obtained from a credshelper.
func NewExternalCredentials(credshelper string, credshelperArgs []string, credsFile string) (*Credentials, error) {
if credshelper == "execrel://" {
credshelperPath, err := pathtranslator.BinaryRelToAbs("credshelper")
if err != nil {
log.Fatalf("Specified %s=execrel:// but `credshelper` was not found in the same directory as `bootstrap` or `reproxy`: %v", CredshelperPathFlag, err)
}
credshelper = credshelperPath
}
credsHelperCmd := newResubaleCmd(credshelper, credshelperArgs)
creds, err := loadCredsFromDisk(credsFile, credsHelperCmd)
if err != nil {
log.Warningf("Failed to use cached credentials: %v", err)
tk, rexp, err := runCredsHelperCmd(credsHelperCmd)
if err != nil {
return nil, err
}
return buildExternalCredentials(cachedCredentials{token: tk, refreshExp: rexp}, credsFile, credsHelperCmd), nil
}
return creds, err
}
func runCredsHelperCmd(credsHelperCmd *reusableCmd) (*oauth2.Token, time.Time, error) {
log.V(2).Infof("Running %v", credsHelperCmd)
var stdout, stderr bytes.Buffer
cmd := credsHelperCmd.Cmd()
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
out := stdout.String()
if stderr.String() != "" {
log.Errorf("Credentials helper warnings and errors: %v", stderr.String())
}
if err != nil {
return nil, time.Time{}, err
}
token, expiry, refreshExpiry, err := parseTokenExpiryFromOutput(out)
return &oauth2.Token{
AccessToken: token,
Expiry: expiry,
}, refreshExpiry, err
}
// CredsHelperOut is the struct to record the json output from the credshelper.
type CredsHelperOut struct {
Token string `json:"token"`
Expiry string `json:"expiry"`
RefreshExpiry string `json:"refresh_expiry"`
}
func parseTokenExpiryFromOutput(out string) (string, time.Time, time.Time, error) {
var (
tk string
exp, rexp time.Time
chOut CredsHelperOut
)
if err := json.Unmarshal([]byte(out), &chOut); err != nil {
return tk, exp, rexp,
fmt.Errorf("error while decoding credshelper output:%v", err)
}
tk = chOut.Token
if tk == "" {
return tk, exp, rexp,
fmt.Errorf("no token was printed by the credentials helper")
}
if chOut.Expiry != "" {
expiry, err := time.Parse(time.UnixDate, chOut.Expiry)
if err != nil {
return tk, exp, rexp, fmt.Errorf("invalid expiry format: %v (Expected time.UnixDate format)", chOut.Expiry)
}
exp = expiry
rexp = expiry
}
if chOut.RefreshExpiry != "" {
rexpiry, err := time.Parse(time.UnixDate, chOut.RefreshExpiry)
if err != nil {
return tk, exp, rexp, fmt.Errorf("invalid refresh expiry format: %v (Expected time.UnixDate format)", chOut.RefreshExpiry)
}
rexp = rexpiry
}
return tk, exp, rexp, nil
}