-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
bearertokenauth.go
214 lines (185 loc) · 5.91 KB
/
bearertokenauth.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package bearertokenauthextension // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/bearertokenauthextension"
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"sync"
"github.com/fsnotify/fsnotify"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/extension/auth"
"go.uber.org/zap"
"google.golang.org/grpc/credentials"
)
var _ credentials.PerRPCCredentials = (*PerRPCAuth)(nil)
// PerRPCAuth is a gRPC credentials.PerRPCCredentials implementation that returns an 'authorization' header.
type PerRPCAuth struct {
metadata map[string]string
}
// GetRequestMetadata returns the request metadata to be used with the RPC.
func (c *PerRPCAuth) GetRequestMetadata(context.Context, ...string) (map[string]string, error) {
return c.metadata, nil
}
// RequireTransportSecurity always returns true for this implementation. Passing bearer tokens in plain-text connections is a bad idea.
func (c *PerRPCAuth) RequireTransportSecurity() bool {
return true
}
var (
_ auth.Server = (*BearerTokenAuth)(nil)
_ auth.Client = (*BearerTokenAuth)(nil)
)
// BearerTokenAuth is an implementation of auth.Client. It embeds a static authorization "bearer" token in every rpc call.
type BearerTokenAuth struct {
muTokenString sync.RWMutex
scheme string
tokenString string
shutdownCH chan struct{}
filename string
logger *zap.Logger
}
var _ auth.Client = (*BearerTokenAuth)(nil)
func newBearerTokenAuth(cfg *Config, logger *zap.Logger) *BearerTokenAuth {
if cfg.Filename != "" && cfg.BearerToken != "" {
logger.Warn("a filename is specified. Configured token is ignored!")
}
return &BearerTokenAuth{
scheme: cfg.Scheme,
tokenString: string(cfg.BearerToken),
filename: cfg.Filename,
logger: logger,
}
}
// Start of BearerTokenAuth does nothing and returns nil if no filename
// is specified. Otherwise a routine is started to monitor the file containing
// the token to be transferred.
func (b *BearerTokenAuth) Start(ctx context.Context, _ component.Host) error {
if b.filename == "" {
return nil
}
if b.shutdownCH != nil {
return fmt.Errorf("bearerToken file monitoring is already running")
}
// Read file once
b.refreshToken()
b.shutdownCH = make(chan struct{})
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
// start file watcher
go b.startWatcher(ctx, watcher)
return watcher.Add(b.filename)
}
func (b *BearerTokenAuth) startWatcher(ctx context.Context, watcher *fsnotify.Watcher) {
defer watcher.Close()
for {
select {
case _, ok := <-b.shutdownCH:
_ = ok
return
case <-ctx.Done():
return
case event, ok := <-watcher.Events:
if !ok {
continue
}
// NOTE: k8s configmaps uses symlinks, we need this workaround.
// original configmap file is removed.
// SEE: https://martensson.io/go-fsnotify-and-kubernetes-configmaps/
if event.Op == fsnotify.Remove || event.Op == fsnotify.Chmod {
// remove the watcher since the file is removed
if err := watcher.Remove(event.Name); err != nil {
b.logger.Error(err.Error())
}
// add a new watcher pointing to the new symlink/file
if err := watcher.Add(b.filename); err != nil {
b.logger.Error(err.Error())
}
b.refreshToken()
}
// also allow normal files to be modified and reloaded.
if event.Op == fsnotify.Write {
b.refreshToken()
}
}
}
}
func (b *BearerTokenAuth) refreshToken() {
b.logger.Info("refresh token", zap.String("filename", b.filename))
token, err := os.ReadFile(b.filename)
if err != nil {
b.logger.Error(err.Error())
return
}
b.muTokenString.Lock()
b.tokenString = string(token)
b.muTokenString.Unlock()
}
// Shutdown of BearerTokenAuth does nothing and returns nil
func (b *BearerTokenAuth) Shutdown(_ context.Context) error {
if b.filename == "" {
return nil
}
if b.shutdownCH == nil {
return fmt.Errorf("bearerToken file monitoring is not running")
}
b.shutdownCH <- struct{}{}
close(b.shutdownCH)
b.shutdownCH = nil
return nil
}
// PerRPCCredentials returns PerRPCAuth an implementation of credentials.PerRPCCredentials that
func (b *BearerTokenAuth) PerRPCCredentials() (credentials.PerRPCCredentials, error) {
return &PerRPCAuth{
metadata: map[string]string{"authorization": b.bearerToken()},
}, nil
}
func (b *BearerTokenAuth) bearerToken() string {
b.muTokenString.RLock()
token := fmt.Sprintf("%s %s", b.scheme, b.tokenString)
b.muTokenString.RUnlock()
return token
}
// RoundTripper is not implemented by BearerTokenAuth
func (b *BearerTokenAuth) RoundTripper(base http.RoundTripper) (http.RoundTripper, error) {
return &BearerAuthRoundTripper{
baseTransport: base,
bearerTokenFunc: b.bearerToken,
}, nil
}
// Authenticate checks whether the given context contains valid auth data.
func (b *BearerTokenAuth) Authenticate(ctx context.Context, headers map[string][]string) (context.Context, error) {
auth, ok := headers["authorization"]
if !ok {
auth, ok = headers["Authorization"]
}
if !ok || len(auth) == 0 {
return ctx, errors.New("authentication didn't succeed")
}
token := auth[0]
expect := b.tokenString
if len(b.scheme) != 0 {
expect = fmt.Sprintf("%s %s", b.scheme, expect)
}
if expect != token {
return ctx, fmt.Errorf("scheme or token does not match: %s", token)
}
return ctx, nil
}
// BearerAuthRoundTripper intercepts and adds Bearer token Authorization headers to each http request.
type BearerAuthRoundTripper struct {
baseTransport http.RoundTripper
bearerTokenFunc func() string
}
// RoundTrip modifies the original request and adds Bearer token Authorization headers.
func (interceptor *BearerAuthRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
req2 := req.Clone(req.Context())
if req2.Header == nil {
req2.Header = make(http.Header)
}
req2.Header.Set("Authorization", interceptor.bearerTokenFunc())
return interceptor.baseTransport.RoundTrip(req2)
}