-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
lib.go
318 lines (292 loc) · 10.4 KB
/
lib.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
// Copyright 2022 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package lib
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/httputil"
"net/http/pprof"
"net/url"
"time"
"github.com/cockroachdb/cmux"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/errors"
)
// ReverseHTTPProxyConfig groups the configuration for ReverseHTTPProxy.
//
// See RunAsync().
type ReverseHTTPProxyConfig struct {
// HttpAddr represents the address on which the proxy will listen.
HTTPAddr string
// TargetURL represents the URL to which requests will be forwarded.
TargetURL string
// CACertPath represents the path to a file containing a certificate authority
// cert. This CA will be the only one trusted to sign CRDB's certificates. If
// not specified, the system's CAs are trusted.
CACertPath string
// UICertPath and UICertKeyPath represent paths to the certificate the proxy
// will use for autheticating itself to clients. If not specified, the proxy
// will just speak HTTP. If specified, the proxy will redirect any HTTP
// requests to HTTPS.
UICertPath, UICertKeyPath string
}
// ReverseHTTPProxy proxies HTTP requests to a given target (assumed to be
// CRDB).
type ReverseHTTPProxy struct {
listenAddr string
proxy *httputil.ReverseProxy
certs certificates
}
// NewReverseHTTPProxy creates a ReverseHTTPProxy.
func NewReverseHTTPProxy(ctx context.Context, cfg ReverseHTTPProxyConfig) *ReverseHTTPProxy {
certs, err := loadCerts(cfg.UICertPath, cfg.UICertKeyPath, cfg.CACertPath)
if err != nil {
log.Fatal(ctx, err.Error())
}
url, err := url.Parse(cfg.TargetURL)
if err != nil {
log.Fatalf(ctx, "Invalid CRDB UI target: %s", cfg.TargetURL)
}
if certs.CAPool != nil && url.Scheme != "https" {
log.Fatalf(ctx, "HTTPS is required for CRDB target when --ca-cert is specified.")
}
if url.Path != "" {
// Supporting a path would require extra code in the proxy to join a
// particular request's path with it.
log.Fatalf(ctx, "Specifying a path in --crdb-http-url is not supported.")
}
// If the proxy is not configured for HTTP, remember to refuse redirects to
// HTTPS from Cockroach.
var HTTPToHTTPSErr error
if certs.UICert == nil {
HTTPToHTTPSErr = errors.New(`The CockroachDB cluster is configured to only serve HTTPS,
but the Observability Service has not been configured for HTTPS.
Set the --ui-cert and --ui-cert-key flags to configure the Observability Service to serve HTTPS,
set the scheme in the --crdb-http-url URL to "https://", and perhaps set --ca-cert
to trust the certificate presented by CockroachDB.`)
}
return &ReverseHTTPProxy{
listenAddr: cfg.HTTPAddr,
proxy: newProxy(url, certs.CAPool, HTTPToHTTPSErr),
certs: certs,
}
}
// RunAsync runs an HTTP proxy server in a goroutine. The returned channel is
// closed when the server terminates.
//
// TODO(andrei): Currently the server never terminates. Figure out a closing
// signal.
func (p *ReverseHTTPProxy) RunAsync(ctx context.Context) <-chan struct{} {
ch := make(chan struct{})
go func() {
defer close(ch)
var err error
listener, err := net.Listen("tcp", p.listenAddr)
if err != nil {
log.Fatal(ctx, err.Error())
}
defer listener.Close()
fmt.Printf("Listening for HTTP requests on %s.\n", p.listenAddr)
// Create the HTTP mux. Requests will generally be forwarded to p.proxy,
// except the /debug/pprof ones which will be served locally.
mux := http.NewServeMux()
mux.Handle("/", p.proxy)
// This seems to be the minimal set of handlers that we need to register in
// order to get all the pprof functionality. The pprof.Index handler handles
// some types of profiles itself.
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
if p.certs.UICert == nil {
// The Observability Service is not configured with certs, so it can only
// serve HTTP.
err = (&http.Server{Handler: mux}).Serve(listener)
} else {
// We're configured to serve HTTPS. We'll also listen for HTTP requests, and redirect them
// to HTTPS.
// Separate HTTP traffic from HTTPS traffic.
protocolMux := cmux.New(listener)
clearL := protocolMux.Match(cmux.HTTP1())
tlsL := protocolMux.Match(cmux.Any())
// Redirect HTTP to HTTPS.
redirectHandler := http.NewServeMux()
redirectHandler.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// TODO(andrei): Consider dealing with HSTS headers. Probably drop HSTS
// headers coming from CRDB, and set our own headers.
http.Redirect(w, r, "https://"+r.Host+r.RequestURI, http.StatusTemporaryRedirect)
})
redirectServer := http.Server{Handler: redirectHandler}
go func() {
_ = redirectServer.Serve(clearL)
}()
// Serve HTTPS traffic by delegating it to the proxy.
tlsServer := &http.Server{Handler: mux}
go func() {
_ = tlsServer.ServeTLS(tlsL, p.certs.UICertPath, p.certs.UICertKeyPath)
}()
err = protocolMux.Serve()
}
if err != http.ErrServerClosed {
fmt.Println(err.Error())
}
}()
return ch
}
// certificates groups together all the certificates relevant to the proxy
// server.
type certificates struct {
UICertPath, UICertKeyPath string
UICert *tls.Certificate
CAPool *x509.CertPool
}
func loadCerts(uiCert, uiKey, caCert string) (certificates, error) {
var certs certificates
certs.UICertPath = uiCert
certs.UICertKeyPath = uiKey
if uiCert != "" {
if uiKey == "" {
return certificates{}, errors.New("--ui-cert-key needs to be specified if --ui-cert is specified")
}
cert, err := tls.LoadX509KeyPair(uiCert, uiKey)
if err != nil {
return certificates{}, errors.Wrap(err, "error parsing UI certificate")
}
certs.UICert = &cert
}
if caCert != "" {
data, err := ioutil.ReadFile(caCert)
if err != nil {
return certificates{}, errors.Wrap(err, "error reading CA cert")
}
block, rest := pem.Decode(data)
if len(rest) != 0 {
return certificates{}, errors.Newf("More than one certificate present in %s. Not sure how to deal with that.", caCert)
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return certificates{}, errors.Wrap(err, "error parsing CA cert")
}
certs.CAPool = x509.NewCertPool()
certs.CAPool.AddCert(cert)
}
return certs, nil
}
// atomicURL is a thread-safe URL.
type atomicURL struct {
mu struct {
syncutil.Mutex // this could be a RWMutex, but it's not worth it as the critical sections are small
url *url.URL
}
}
func newAtomicURL(url *url.URL) *atomicURL {
u := &atomicURL{}
u.mu.url = url
return u
}
func (u *atomicURL) Get() *url.URL {
u.mu.Lock()
defer u.mu.Unlock()
return u.mu.url
}
func (u *atomicURL) ReplaceScheme(newScheme string) {
u.mu.Lock()
defer u.mu.Unlock()
u.mu.url.Scheme = newScheme
}
// newProxy creates a proxy that can forward requests to a CRDB cluster
// identified by url. If CRDB ever returns a redirect, then the redirect target
// will be used by subsequent requests.
//
// caCerts, if not nil, specifies what CA is trusted to sign CRDB's certs. If
// nil, the system defaults are used.
//
// HTTPTohTTPSErr, if set, will cause the proxy to detect when CRDB performs a
// HTTP to HTTPS redirection and return this error instead of proceeding to talk
// HTTPS to CRDB. The idea is that, if the Obs Service is not prepared to talk
// HTTPS to its clients, but CRDB insists on talking HTTPS to its clients, we'd
// rather return errors and ask people to configure the Obs Service for HTTPS
// than downgrade the security that CRDB insists on.
func newProxy(url *url.URL, caCerts *x509.CertPool, HTTPToHTTPSErr error) *httputil.ReverseProxy {
atomicTarget := newAtomicURL(url)
director := func(req *http.Request) {
target := atomicTarget.Get()
req.URL.Scheme = target.Scheme
req.URL.Host = target.Host
targetQuery := target.RawQuery
if targetQuery == "" || req.URL.RawQuery == "" {
req.URL.RawQuery = targetQuery + req.URL.RawQuery
} else {
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
}
if _, ok := req.Header["User-Agent"]; !ok {
// explicitly disable User-Agent so it's not set to default value
req.Header.Set("User-Agent", "")
}
}
modifyResponse := func(r *http.Response) error {
// We deal with redirects specifically: we detect when CRDB wants us to
// switch from HTTP to HTTPS and remember that.
if r.StatusCode != http.StatusTemporaryRedirect &&
r.StatusCode != http.StatusPermanentRedirect &&
r.StatusCode != http.StatusMovedPermanently {
return nil
}
// Check if CRDB is asking to switch from HTTP to HTTPS. If it is, switch
// future requests to use HTTPS.
redirectTarget := r.Header.Get("Location")
newURL, err := url.Parse(redirectTarget)
if err != nil {
return errors.Wrap(err, "invalid redirection")
}
if r.Request.URL.Scheme != newURL.Scheme {
if r.Request.URL.Scheme == "http" && newURL.Scheme == "https" {
// If we're not prepared to server HTTPS, error out.
if HTTPToHTTPSErr != nil {
return HTTPToHTTPSErr
}
}
atomicTarget.ReplaceScheme(newURL.Scheme)
// We'll continue returning this redirect to the client. It will appear to
// the client as a redirection to the same URL that it already requested;
// that's fine. On the retry, we'll forward to the updated CRDB url.
}
return nil
}
proxy := &httputil.ReverseProxy{
Director: director,
ModifyResponse: modifyResponse,
// Overwrite the default error handler so that we render errors produced by
// ModifyResponse. The default handler only logs them on the server and
// doesn't return them to the client.
ErrorHandler: func(rw http.ResponseWriter, req *http.Request, err error) {
rw.WriteHeader(http.StatusInternalServerError)
rw.Write([]byte(err.Error()))
},
}
if caCerts != nil {
// Accept only the specified roots.
proxy.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: caCerts,
},
TLSHandshakeTimeout: 10 * time.Second,
}
}
return proxy
}