/
bearer.go
184 lines (156 loc) · 4.9 KB
/
bearer.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
// Copyright 2019-2023 The Cloudprober Authors.
//
// 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 oauth
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"time"
"github.com/cloudprober/cloudprober/internal/file"
configpb "github.com/cloudprober/cloudprober/internal/oauth/proto"
"github.com/cloudprober/cloudprober/logger"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/protobuf/proto"
)
var k8sTokenFile = "/var/run/secrets/kubernetes.io/serviceaccount/token"
type bearerTokenSource struct {
c *configpb.BearerToken
getTokenFromBackend func(*configpb.BearerToken) (*oauth2.Token, error)
cache *tokenCache
l *logger.Logger
}
func bytesToToken(b []byte) *oauth2.Token {
tok := &jsonToken{}
err := json.Unmarshal(b, tok)
if err != nil {
return &oauth2.Token{AccessToken: string(b)}
}
return &oauth2.Token{
AccessToken: tok.AccessToken,
Expiry: time.Now().Add(time.Duration(tok.ExpiresIn) * time.Second),
}
}
var tokenFunctions = struct {
fromFile, fromCmd, fromGCEMetadata, fromK8sTokenFile func(c *configpb.BearerToken) (*oauth2.Token, error)
}{
fromFile: func(c *configpb.BearerToken) (*oauth2.Token, error) {
b, err := file.ReadFile(c.GetFile())
if err != nil {
return nil, err
}
return bytesToToken(b), nil
},
fromCmd: func(c *configpb.BearerToken) (*oauth2.Token, error) {
var cmd *exec.Cmd
cmdParts := strings.Split(c.GetCmd(), " ")
cmd = exec.Command(cmdParts[0], cmdParts[1:]...)
out, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("failed to execute command: %v", cmd)
}
return bytesToToken(out), nil
},
fromGCEMetadata: func(c *configpb.BearerToken) (*oauth2.Token, error) {
ts := google.ComputeTokenSource(c.GetGceServiceAccount())
tok, err := ts.Token()
if err != nil {
return nil, err
}
return tok, nil
},
fromK8sTokenFile: func(c *configpb.BearerToken) (*oauth2.Token, error) {
b, err := os.ReadFile(k8sTokenFile)
if err != nil {
return nil, err
}
return &oauth2.Token{AccessToken: string(b)}, nil
},
}
func newBearerTokenSource(c *configpb.BearerToken, refreshExpiryBuffer time.Duration, l *logger.Logger) (oauth2.TokenSource, error) {
ts := &bearerTokenSource{
c: c,
l: l,
}
var tokenBackendFunc func(*configpb.BearerToken) (*oauth2.Token, error)
switch ts.c.Source.(type) {
case *configpb.BearerToken_File:
tokenBackendFunc = tokenFunctions.fromFile
case *configpb.BearerToken_Cmd:
tokenBackendFunc = tokenFunctions.fromCmd
case *configpb.BearerToken_GceServiceAccount:
tokenBackendFunc = tokenFunctions.fromGCEMetadata
case *configpb.BearerToken_K8SLocalToken:
if !c.GetK8SLocalToken() {
return nil, fmt.Errorf("k8s_local_token cannot be false, config: <%v>", c.String())
}
tokenBackendFunc = tokenFunctions.fromK8sTokenFile
default:
return nil, fmt.Errorf("unknown source: %v", ts.c.GetSource())
}
ts.getTokenFromBackend = func(c *configpb.BearerToken) (*oauth2.Token, error) {
l.Debugf("oauth.bearerTokenSource: Getting a new token using config: %s", c.String())
return tokenBackendFunc(c)
}
tok, err := ts.getTokenFromBackend(c)
if err != nil {
return nil, err
}
ts.cache = &tokenCache{
tok: tok,
refreshExpiryBuffer: refreshExpiryBuffer,
ignoreExpiryIfZero: true,
getToken: func() (*oauth2.Token, error) { return ts.getTokenFromBackend(c) },
l: l,
}
// For JSON tokens return now.
if tok != nil && !tok.Expiry.IsZero() {
return ts, nil
}
// Default refresh interval
if ts.c.RefreshIntervalSec == nil {
ts.c.RefreshIntervalSec = proto.Float32(30)
}
// Explicitly set to zero, so no refreshing.
if ts.c.GetRefreshIntervalSec() == 0 {
return ts, nil
}
go func() {
interval := time.Duration(ts.c.GetRefreshIntervalSec()) * time.Second
ticker := time.NewTicker(interval)
defer ticker.Stop()
for range ticker.C {
tok, err := ts.getTokenFromBackend(ts.c)
if err != nil {
ts.l.Warningf("oauth.bearerTokenSource: %s", err)
continue
}
ts.cache.setToken(tok)
}
}()
return ts, nil
}
func (ts *bearerTokenSource) Token() (*oauth2.Token, error) {
return ts.cache.Token()
}
func K8STokenSource(l *logger.Logger) (oauth2.TokenSource, error) {
return newBearerTokenSource(&configpb.BearerToken{
Source: &configpb.BearerToken_K8SLocalToken{
K8SLocalToken: true,
},
RefreshIntervalSec: proto.Float32(60),
}, time.Minute, l)
}