github.com/google/cloudprober@v0.11.3/common/oauth/bearer.go (about) 1 // Copyright 2019 The Cloudprober Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package oauth 16 17 import ( 18 "fmt" 19 "os/exec" 20 "strings" 21 "sync" 22 "time" 23 24 "github.com/google/cloudprober/common/file" 25 configpb "github.com/google/cloudprober/common/oauth/proto" 26 "github.com/google/cloudprober/logger" 27 "golang.org/x/oauth2" 28 "golang.org/x/oauth2/google" 29 ) 30 31 type bearerTokenSource struct { 32 c *configpb.BearerToken 33 getTokenFromBackend func(*configpb.BearerToken) (string, error) 34 cache string 35 mu sync.RWMutex 36 l *logger.Logger 37 } 38 39 var getTokenFromFile = func(c *configpb.BearerToken) (string, error) { 40 b, err := file.ReadFile(c.GetFile()) 41 if err != nil { 42 return "", err 43 } 44 return string(b), nil 45 } 46 47 var getTokenFromCmd = func(c *configpb.BearerToken) (string, error) { 48 var cmd *exec.Cmd 49 50 cmdParts := strings.Split(c.GetCmd(), " ") 51 cmd = exec.Command(cmdParts[0], cmdParts[1:len(cmdParts)]...) 52 53 out, err := cmd.Output() 54 if err != nil { 55 return "", fmt.Errorf("failed to execute command: %v", cmd) 56 } 57 return string(out), nil 58 } 59 60 var getTokenFromGCEMetadata = func(c *configpb.BearerToken) (string, error) { 61 ts := google.ComputeTokenSource(c.GetGceServiceAccount()) 62 tok, err := ts.Token() 63 return tok.AccessToken, err 64 } 65 66 func newBearerTokenSource(c *configpb.BearerToken, l *logger.Logger) (oauth2.TokenSource, error) { 67 ts := &bearerTokenSource{ 68 c: c, 69 l: l, 70 } 71 72 switch ts.c.Source.(type) { 73 case *configpb.BearerToken_File: 74 ts.getTokenFromBackend = getTokenFromFile 75 76 case *configpb.BearerToken_Cmd: 77 ts.getTokenFromBackend = getTokenFromCmd 78 79 case *configpb.BearerToken_GceServiceAccount: 80 ts.getTokenFromBackend = getTokenFromGCEMetadata 81 82 default: 83 ts.getTokenFromBackend = getTokenFromGCEMetadata 84 } 85 86 tok, err := ts.getTokenFromBackend(c) 87 if err != nil { 88 return nil, err 89 } 90 ts.cache = tok 91 92 if ts.c.GetRefreshIntervalSec() == 0 { 93 return ts, nil 94 } 95 96 go func() { 97 interval := time.Duration(ts.c.GetRefreshIntervalSec()) * time.Second 98 99 for range time.Tick(interval) { 100 tok, err := ts.getTokenFromBackend(ts.c) 101 102 if err != nil { 103 ts.l.Warningf("oauth.bearerTokenSource: %s", err) 104 continue 105 } 106 107 ts.mu.Lock() 108 ts.cache = tok 109 ts.mu.Unlock() 110 } 111 }() 112 113 return ts, nil 114 } 115 116 func (ts *bearerTokenSource) Token() (*oauth2.Token, error) { 117 ts.mu.RLock() 118 defer ts.mu.RUnlock() 119 120 if ts.c.GetRefreshIntervalSec() == 0 { 121 tok, err := ts.getTokenFromBackend(ts.c) 122 123 if err != nil { 124 if ts.cache == "" { 125 return nil, err 126 } 127 128 ts.l.Errorf("oauth.bearerTokenSource: failed to get token: %v, using cache", err) 129 return &oauth2.Token{AccessToken: ts.cache}, nil 130 } 131 132 return &oauth2.Token{AccessToken: tok}, nil 133 } 134 135 return &oauth2.Token{AccessToken: ts.cache}, nil 136 }