-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.go
359 lines (322 loc) · 11.1 KB
/
main.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
package main
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
stdlog "log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/coreos/go-oidc"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/metalmatze/signal/healthcheck"
"github.com/metalmatze/signal/internalserver"
"github.com/metalmatze/signal/server/signalhttp"
"github.com/oklog/run"
"github.com/prometheus/client_golang/prometheus"
flag "github.com/spf13/pflag"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
)
const (
retryInterval = 5 * time.Second
prefixHeader = "X-Forwarded-Prefix"
)
type config struct {
file string
logLevel level.Option
logFormat string
margin time.Duration
name string
tempFile string
oidc oidcConfig
server serverConfig
upstream upstreamConfig
scope []string
}
type upstreamConfig struct {
url *url.URL
caFile string
readTimeout time.Duration
writeTimeout time.Duration
}
type serverConfig struct {
listen string
listenInternal string
}
type oidcConfig struct {
audience string
clientID string
clientSecret string
issuerURL string
username string
password string
}
func parseFlags() (*config, error) {
cfg := &config{}
flag.StringVar(&cfg.name, "debug.name", "token-refresher", "A name to add as a prefix to log lines.")
logLevelRaw := flag.String("log.level", "info", "The log filtering level. Options: 'error', 'warn', 'info', 'debug'.")
flag.StringVar(&cfg.logFormat, "log.format", "logfmt", "The log format to use. Options: 'logfmt', 'json'.")
flag.StringVar(&cfg.server.listenInternal, "web.internal.listen", ":8081", "The address on which the internal server listens.")
flag.StringVar(&cfg.server.listen, "web.listen", ":8080", "The address on which the proxy server listens.")
flag.StringVar(&cfg.oidc.issuerURL, "oidc.issuer-url", "", "The OIDC issuer URL, see https://openid.net/specs/openid-connect-discovery-1_0.html#IssuerDiscovery.")
flag.StringVar(&cfg.oidc.clientSecret, "oidc.client-secret", "", "The OIDC client secret, see https://tools.ietf.org/html/rfc6749#section-2.3.")
flag.StringVar(&cfg.oidc.clientID, "oidc.client-id", "", "The OIDC client ID, see https://tools.ietf.org/html/rfc6749#section-2.3.")
flag.StringVar(&cfg.oidc.audience, "oidc.audience", "", "The audience for whom the access token is intended, see https://openid.net/specs/openid-connect-core-1_0.html#IDToken.")
flag.StringVar(&cfg.oidc.username, "oidc.username", "", "The username to use for OIDC authentication. If both username and password are set then grant_type is set to password.")
flag.StringVar(&cfg.oidc.password, "oidc.password", "", "The password to use for OIDC authentication. If both username and password are set then grant_type is set to password.")
flag.StringSliceVar(&cfg.scope, "scope", []string{}, "The scope to be included in the payload data of the token. Scopes can either be comma-separated or space-separated.")
flag.StringVar(&cfg.file, "file", "", "The path to the file in which to write the retrieved token.")
flag.StringVar(&cfg.tempFile, "temp-file", "", "The path to a temporary file to use for atomically update the token file. If left empty, \".tmp\" will be suffixed to the token file.")
rawURL := flag.String("url", "", "The target URL to which to proxy requests. All requests will have the acces token in the Authorization HTTP header.(DEPRECATED: Use -upstream.url instead)")
rawUpstreamURL := flag.String("upstream.url", "", "The target URL to which to proxy requests. All requests will have the acces token in the Authorization HTTP header.")
flag.StringVar(&cfg.upstream.caFile, "upstream.ca-file", "", "The path to the CA file to verify upstream server TLS certificates.")
flag.DurationVar(&cfg.upstream.readTimeout, "upstream.read-timeout", 0, "The time from when the connection is accepted to when the request body is fully read.")
flag.DurationVar(&cfg.upstream.writeTimeout, "upstream.write-timeout", 0, "The time from the end of the request header read to the end of the response write .")
flag.DurationVar(&cfg.margin, "margin", 5*time.Minute, "The margin of time before a token expires to try to refresh it.")
flag.Parse()
switch *logLevelRaw {
case "error":
cfg.logLevel = level.AllowError()
case "warn":
cfg.logLevel = level.AllowWarn()
case "info":
cfg.logLevel = level.AllowInfo()
case "debug":
cfg.logLevel = level.AllowDebug()
default:
return nil, fmt.Errorf("unexpected log level: %s", *logLevelRaw)
}
if *rawURL != "" && *rawUpstreamURL != "" {
return nil, errors.New("use only one of --url or --upstream.url")
}
if *rawURL != "" {
u, err := url.Parse(*rawURL)
if err != nil {
return nil, err
}
cfg.upstream.url = u
}
if *rawUpstreamURL != "" {
u, err := url.Parse(*rawUpstreamURL)
if err != nil {
return nil, err
}
cfg.upstream.url = u
}
if cfg.file == "" && cfg.upstream.url == nil {
return nil, errors.New("one of --file or --upstream.url is required")
}
if cfg.tempFile == "" {
cfg.tempFile = cfg.file + ".tmp"
}
return cfg, nil
}
func main() {
cfg, err := parseFlags()
if err != nil {
stdlog.Fatal(err)
}
logger := log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
if cfg.logFormat == "json" {
logger = log.NewJSONLogger(log.NewSyncWriter(os.Stderr))
}
logger = level.NewFilter(logger, cfg.logLevel)
if cfg.name != "" {
logger = log.With(logger, "name", cfg.name)
}
logger = log.With(logger, "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
defer level.Info(logger).Log("msg", "exiting")
reg := prometheus.NewRegistry()
reg.MustRegister(
prometheus.NewGoCollector(),
prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}),
)
level.Info(logger).Log("msg", "token-refresher")
var g run.Group
{
// Signal channels must be buffered.
sig := make(chan os.Signal, 1)
g.Add(func() error {
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
<-sig
level.Info(logger).Log("msg", "caught interrupt")
return nil
}, func(_ error) {
close(sig)
})
}
{
healthchecks := healthcheck.NewMetricsHandler(healthcheck.NewHandler(), reg)
h := internalserver.NewHandler(
internalserver.WithName("Internal - token-refresher API"),
internalserver.WithHealthchecks(healthchecks),
internalserver.WithPrometheusRegistry(reg),
internalserver.WithPProf(),
)
s := http.Server{
Addr: cfg.server.listenInternal,
Handler: h,
}
g.Add(func() error {
level.Info(logger).Log("msg", "starting internal HTTP server", "address", s.Addr)
return s.ListenAndServe()
}, func(err error) {
_ = s.Shutdown(context.Background())
})
}
{
provider, err := oidc.NewProvider(context.Background(), cfg.oidc.issuerURL)
if err != nil {
stdlog.Fatalf("OIDC provider initialization failed: %v", err)
}
ctx := context.WithValue(context.Background(), oauth2.HTTPClient,
&http.Client{
Transport: newRoundTripperInstrumenter(reg).NewRoundTripper("oauth", http.DefaultTransport),
},
)
ccc := clientcredentials.Config{
ClientID: cfg.oidc.clientID,
ClientSecret: cfg.oidc.clientSecret,
TokenURL: provider.Endpoint().TokenURL,
}
if cfg.oidc.audience != "" {
ccc.EndpointParams = url.Values{
"audience": []string{cfg.oidc.audience},
}
}
if cfg.oidc.username != "" && cfg.oidc.password != "" {
ccc.EndpointParams = url.Values{
"username": []string{cfg.oidc.username},
"password": []string{cfg.oidc.password},
"grant_type": []string{"password"},
}
}
if len(cfg.scope) != 0 {
ccc.EndpointParams = url.Values{
"scope": []string{strings.Join(cfg.scope, " ")},
}
}
if cfg.file != "" {
ctx, cancel := context.WithCancel(ctx)
g.Add(func() error {
for {
d := retryInterval
t, err := ccc.Token(ctx)
switch {
case err != nil:
level.Error(logger).Log("msg", "failed to get token", "err", err)
case !t.Valid():
level.Error(logger).Log("msg", "token is invalid", "exp", t.Expiry.String())
default:
if err := ioutil.WriteFile(cfg.tempFile, []byte(t.AccessToken), 0644); err != nil {
level.Error(logger).Log("msg", "failed to write token to temporary file", "err", err)
break
}
if err := os.Rename(cfg.tempFile, cfg.file); err != nil {
level.Error(logger).Log("msg", "failed to write token to file", "err", err)
break
}
d = t.Expiry.Sub(time.Now()) - cfg.margin
}
select {
case <-time.NewTimer(d).C:
case <-ctx.Done():
return nil
}
}
}, func(_ error) {
cancel()
})
}
if cfg.upstream.url != nil {
ctx, cancel := context.WithCancel(ctx)
// Create Reverse Proxy.
p := httputil.ReverseProxy{
Director: func(request *http.Request) {
request.URL.Scheme = cfg.upstream.url.Scheme
// Set the Host at both request and request.URL objects.
request.Host = cfg.upstream.url.Host
request.URL.Host = cfg.upstream.url.Host
// Derive path from the paths of configured URL and request URL.
request.URL.Path, request.URL.RawPath = joinURLPath(cfg.upstream.url, request.URL)
// Add prefix header with value "/", since from a client's perspective
// we are forwarding /<anything> to /<cfg.upstream.url.Path>/<anything>.
request.Header.Add(prefixHeader, "/")
},
}
base := http.DefaultTransport
if cfg.upstream.caFile != "" {
caCert, err := ioutil.ReadFile(cfg.upstream.caFile)
if err != nil {
stdlog.Fatalf("failed to initialize upstream server TLS CA: %v", err)
}
pool := x509.NewCertPool()
pool.AppendCertsFromPEM(caCert)
t := http.DefaultTransport.(*http.Transport).Clone()
t.TLSClientConfig = &tls.Config{RootCAs: pool}
base = t
}
p.Transport = &oauth2.Transport{
Source: ccc.TokenSource(ctx),
Base: base,
}
s := http.Server{
Addr: cfg.server.listen,
Handler: signalhttp.NewHandlerInstrumenter(reg, nil).NewHandler(nil, &p),
}
if cfg.upstream.readTimeout != 0 {
s.ReadTimeout = cfg.upstream.readTimeout
}
if cfg.upstream.writeTimeout != 0 {
s.WriteTimeout = cfg.upstream.writeTimeout
}
g.Add(func() error {
level.Info(logger).Log("msg", "starting proxy server", "address", s.Addr)
return s.ListenAndServe()
}, func(err error) {
_ = s.Shutdown(context.Background())
cancel()
})
}
}
if err := g.Run(); err != nil {
stdlog.Fatal(err)
}
}
func singleJoiningSlash(a, b string) string {
aslash := strings.HasSuffix(a, "/")
bslash := strings.HasPrefix(b, "/")
switch {
case aslash && bslash:
return a + b[1:]
case !aslash && !bslash:
return a + "/" + b
}
return a + b
}
func joinURLPath(a, b *url.URL) (path, rawpath string) {
if a.RawPath == "" && b.RawPath == "" {
return singleJoiningSlash(a.Path, b.Path), ""
}
apath := a.EscapedPath()
bpath := b.EscapedPath()
aslash := strings.HasSuffix(apath, "/")
bslash := strings.HasPrefix(bpath, "/")
switch {
case aslash && bslash:
return a.Path + b.Path[1:], apath + bpath[1:]
case !aslash && !bslash:
return a.Path + "/" + b.Path, apath + "/" + bpath
}
return a.Path + b.Path, apath + bpath
}