forked from quay/clair
/
httpclient.go
90 lines (82 loc) · 2.25 KB
/
httpclient.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
package config
import (
"net/http"
"net/http/cookiejar"
"time"
"golang.org/x/net/publicsuffix"
"gopkg.in/square/go-jose.v2"
"gopkg.in/square/go-jose.v2/jwt"
)
// Client returns an http.Client configured according to the supplied
// configuration.
//
// It returns an *http.Client and a boolean indicating whether the client is
// configured for authentication, or an error that occurred during construction.
func (cfg *Config) Client(next http.RoundTripper, cl jwt.Claims) (c *http.Client, authed bool, err error) {
if next == nil {
next = http.DefaultTransport.(*http.Transport).Clone()
}
authed = false
sk := jose.SigningKey{Algorithm: jose.HS256}
// Keep this organized from "best" to "worst". That way, we can add methods
// and keep everything working with some careful cluster rolling.
switch {
case cfg.Auth.Keyserver != nil:
sk.Key = cfg.Auth.Keyserver.Intraservice
case cfg.Auth.PSK != nil:
sk.Key = cfg.Auth.PSK.Key
default:
}
jar, err := cookiejar.New(&cookiejar.Options{
PublicSuffixList: publicsuffix.List,
})
if err != nil {
return nil, false, err
}
rt := &transport{
next: next,
base: cl,
}
c = &http.Client{
Jar: jar,
Transport: rt,
}
// Both of the JWT-based methods set the signing key.
if sk.Key != nil {
signer, err := jose.NewSigner(sk, nil)
if err != nil {
return nil, false, err
}
rt.Signer = signer
authed = true
}
return c, authed, nil
}
var _ http.RoundTripper = (*transport)(nil)
// Transport does request modification common to all requests.
type transport struct {
jose.Signer
next http.RoundTripper
base jwt.Claims
}
func (cs *transport) RoundTrip(r *http.Request) (*http.Response, error) {
const (
userAgent = `clair/v4`
)
r.Header.Set("user-agent", userAgent)
if cs.Signer != nil {
// TODO(hank) Make this mint longer-lived tokens and re-use them, only
// refreshing when needed. Like a resettable sync.Once.
now := time.Now()
cl := cs.base
cl.IssuedAt = jwt.NewNumericDate(now)
cl.NotBefore = jwt.NewNumericDate(now.Add(-jwt.DefaultLeeway))
cl.Expiry = jwt.NewNumericDate(now.Add(jwt.DefaultLeeway))
h, err := jwt.Signed(cs).Claims(&cl).CompactSerialize()
if err != nil {
return nil, err
}
r.Header.Add("authorization", "Bearer "+h)
}
return cs.next.RoundTrip(r)
}