|
1 | 1 | package httputil
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net" |
4 | 8 | "net/http"
|
5 | 9 | "net/http/cookiejar"
|
6 |
| - "time" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + "strings" |
| 13 | + "syscall" |
7 | 14 |
|
8 |
| - "github.com/quay/clair/config" |
| 15 | + "github.com/quay/clair/v4/cmd" |
9 | 16 | "golang.org/x/net/publicsuffix"
|
10 |
| - "gopkg.in/square/go-jose.v2" |
11 |
| - "gopkg.in/square/go-jose.v2/jwt" |
12 | 17 | )
|
13 | 18 |
|
14 |
| -// Client returns an http.Client configured according to the supplied |
15 |
| -// configuration. |
| 19 | +// NewClient constructs an [http.Client] that disallows access to public |
| 20 | +// networks, controlled by the localOnly flag. |
16 | 21 | //
|
17 |
| -// If nil is passed for a claim, the returned client does no signing. |
18 |
| -// |
19 |
| -// It returns an *http.Client and a boolean indicating whether the client is |
20 |
| -// configured for authentication, or an error that occurred during construction. |
21 |
| -func Client(next http.RoundTripper, cl *jwt.Claims, cfg *config.Config) (c *http.Client, authed bool, err error) { |
22 |
| - if next == nil { |
23 |
| - next = http.DefaultTransport.(*http.Transport).Clone() |
| 22 | +// If disallowed, the reported error will be a [*net.AddrError] with the "Err" |
| 23 | +// value of "disallowed by policy". |
| 24 | +func NewClient(ctx context.Context, localOnly bool) (*http.Client, error) { |
| 25 | + tr := http.DefaultTransport.(*http.Transport).Clone() |
| 26 | + dialer := &net.Dialer{} |
| 27 | + // Set a control function if we're restricting subnets. |
| 28 | + if localOnly { |
| 29 | + dialer.Control = ctlLocalOnly |
24 | 30 | }
|
25 |
| - authed = false |
| 31 | + tr.DialContext = dialer.DialContext |
| 32 | + |
26 | 33 | jar, err := cookiejar.New(&cookiejar.Options{
|
27 | 34 | PublicSuffixList: publicsuffix.List,
|
28 | 35 | })
|
29 | 36 | if err != nil {
|
30 |
| - return nil, false, err |
31 |
| - } |
32 |
| - c = &http.Client{ |
33 |
| - Jar: jar, |
| 37 | + return nil, err |
34 | 38 | }
|
| 39 | + return &http.Client{ |
| 40 | + Transport: tr, |
| 41 | + Jar: jar, |
| 42 | + }, nil |
| 43 | +} |
35 | 44 |
|
36 |
| - sk := jose.SigningKey{Algorithm: jose.HS256} |
37 |
| - // Keep this organized from "best" to "worst". That way, we can add methods |
38 |
| - // and keep everything working with some careful cluster rolling. |
39 |
| - switch { |
40 |
| - case cl == nil: // Skip signing |
41 |
| - case cfg.Auth.Keyserver != nil: |
42 |
| - sk.Key = []byte(cfg.Auth.Keyserver.Intraservice) |
43 |
| - case cfg.Auth.PSK != nil: |
44 |
| - sk.Key = []byte(cfg.Auth.PSK.Key) |
45 |
| - default: |
46 |
| - } |
47 |
| - rt := &transport{ |
48 |
| - next: next, |
| 45 | +func ctlLocalOnly(network, address string, _ syscall.RawConn) error { |
| 46 | + // Future-proof for QUIC by allowing UDP here. |
| 47 | + if !strings.HasPrefix(network, "tcp") && !strings.HasPrefix(network, "udp") { |
| 48 | + return &net.AddrError{ |
| 49 | + Addr: network + "!" + address, |
| 50 | + Err: "disallowed by policy", |
| 51 | + } |
49 | 52 | }
|
50 |
| - // If we have a claim, make a copy into the transport. |
51 |
| - if cl != nil { |
52 |
| - rt.base = *cl |
| 53 | + addr := net.ParseIP(address) |
| 54 | + if addr == nil { |
| 55 | + return &net.AddrError{ |
| 56 | + Addr: network + "!" + address, |
| 57 | + Err: "martian address", |
| 58 | + } |
53 | 59 | }
|
54 |
| - c.Transport = rt |
55 |
| - |
56 |
| - // Both of the JWT-based methods set the signing key. |
57 |
| - if sk.Key != nil { |
58 |
| - signer, err := jose.NewSigner(sk, nil) |
59 |
| - if err != nil { |
60 |
| - return nil, false, err |
| 60 | + if !addr.IsPrivate() { |
| 61 | + return &net.AddrError{ |
| 62 | + Addr: network + "!" + address, |
| 63 | + Err: "disallowed by policy", |
61 | 64 | }
|
62 |
| - rt.Signer = signer |
63 |
| - authed = true |
64 | 65 | }
|
65 |
| - return c, authed, nil |
| 66 | + return nil |
66 | 67 | }
|
67 | 68 |
|
68 |
| -var _ http.RoundTripper = (*transport)(nil) |
69 |
| - |
70 |
| -// Transport does request modification common to all requests. |
71 |
| -type transport struct { |
72 |
| - jose.Signer |
73 |
| - next http.RoundTripper |
74 |
| - base jwt.Claims |
75 |
| -} |
76 |
| - |
77 |
| -func (cs *transport) RoundTrip(r *http.Request) (*http.Response, error) { |
78 |
| - const ( |
79 |
| - userAgent = `clair/v4` |
80 |
| - ) |
81 |
| - r.Header.Set("user-agent", userAgent) |
82 |
| - if cs.Signer != nil { |
83 |
| - // TODO(hank) Make this mint longer-lived tokens and re-use them, only |
84 |
| - // refreshing when needed. Like a resettable sync.Once. |
85 |
| - now := time.Now() |
86 |
| - cl := cs.base |
87 |
| - cl.IssuedAt = jwt.NewNumericDate(now) |
88 |
| - cl.NotBefore = jwt.NewNumericDate(now.Add(-jwt.DefaultLeeway)) |
89 |
| - cl.Expiry = jwt.NewNumericDate(now.Add(jwt.DefaultLeeway)) |
90 |
| - h, err := jwt.Signed(cs).Claims(&cl).CompactSerialize() |
91 |
| - if err != nil { |
92 |
| - return nil, err |
93 |
| - } |
94 |
| - r.Header.Add("authorization", "Bearer "+h) |
| 69 | +// NewRequestWithContext is a wrapper around [http.NewRequestWithContext] that |
| 70 | +// sets some defaults in the returned request. |
| 71 | +func NewRequestWithContext(ctx context.Context, method, url string, body io.Reader) (*http.Request, error) { |
| 72 | + // The one OK use of the normal function. |
| 73 | + req, err := http.NewRequestWithContext(ctx, method, url, body) |
| 74 | + if err != nil { |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + p, err := os.Executable() |
| 78 | + if err != nil { |
| 79 | + p = `clair?` |
| 80 | + } else { |
| 81 | + p = filepath.Base(p) |
95 | 82 | }
|
96 |
| - return cs.next.RoundTrip(r) |
| 83 | + req.Header.Set("user-agent", fmt.Sprintf("%s/%s", p, cmd.Version)) |
| 84 | + return req, nil |
97 | 85 | }
|
0 commit comments