-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauth_transport.go
84 lines (71 loc) · 2.39 KB
/
auth_transport.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
// Copyright 2021 Outreach Corporation. All Rights Reserved.
//
// Description: Implements a http.Transport for authentication
package vault_client //nolint:revive // Why: We're using - in the name
import (
"context"
"net/http"
"sync"
"time"
"github.com/getoutreach/gobox/pkg/cfg"
"github.com/getoutreach/gobox/pkg/trace"
"github.com/pkg/errors"
)
// transport provides a http.RoundTripper by wrapping an existing
// http.RoundTripper and provides Vault authentication.
type transport struct {
tr http.RoundTripper
am AuthMethod
mu sync.Mutex
token cfg.SecretData
expiresAt time.Time
}
// New returns a Transport that automatically refreshes Vault authentication
// and includes it.
//
// The provided tr http.RoundTripper should be shared between multiple
// clients to ensure reuse of underlying TCP connections.
//
// The returned Transport's RoundTrip method is safe to be used concurrently.
// nolint:gocritic // Why: We want to ensure the credentials aren't modified
func NewTransport(tr http.RoundTripper, am AuthMethod) http.RoundTripper {
return &transport{tr: tr, am: am}
}
// RoundTrip implements http.RoundTripper interface.
func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
token, err := t.Token(req.Context())
if err != nil {
return nil, err
}
if token != "" {
req.Header.Set("Authorization", "Bearer "+string(token))
}
resp, err := t.tr.RoundTrip(req)
return resp, err
}
// Token checks the active token expiration and renews if necessary. Token returns
// a valid client token. If renewal fails an error is returned.
func (t *transport) Token(ctx context.Context) (cfg.SecretData, error) {
ctx = trace.StartCall(ctx, "vault.token_refresh")
defer trace.EndCall(ctx)
t.mu.Lock()
defer t.mu.Unlock()
// if the token is empty, we always want to refresh, otherwise if we have
// an expiresAt, we want to check if it's within 5 minutes of now. if so,
// we want to refresh it
if t.token == "" || (!t.expiresAt.IsZero() && t.expiresAt.Add(-(time.Minute * 5)).Before(time.Now())) {
// Token is not set or expired/nearly expired, so refresh
if err := t.refreshToken(ctx); err != nil {
return "", errors.Wrap(err, "failed to refresh vault approle")
}
}
return t.token, nil
}
func (t *transport) refreshToken(ctx context.Context) error {
if t.am == nil {
return nil
}
var err error
t.token, t.expiresAt, err = t.am.GetToken(ctx)
return err
}