-
Notifications
You must be signed in to change notification settings - Fork 14
/
http_utils.go
394 lines (336 loc) · 9.38 KB
/
http_utils.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package grpcinterface
import (
"bytes"
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/improbable-eng/grpc-web/go/grpcweb"
log "github.com/sirupsen/logrus"
"github.com/soheilhy/cmux"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"google.golang.org/grpc"
)
func isValidAddress(addr string) bool {
parts := strings.Split(addr, ":")
if len(parts) != 2 {
return false
}
if parts[0] != "" {
if ip := net.ParseIP(parts[0]); ip == nil {
return false
}
}
port, err := strconv.Atoi(parts[1])
if err != nil {
return false
}
if port <= 1024 {
return false
}
return true
}
func generateOperatorTLSKeyCert(
datadir string, extraIPs, extraDomains []string,
) error {
if err := makeDirectoryIfNotExists(datadir); err != nil {
return err
}
keyPath := filepath.Join(datadir, OperatorTLSKeyFile)
certPath := filepath.Join(datadir, OperatorTLSCertFile)
// if key and cert files already exist nothing to do here.
if pathExists(keyPath) && pathExists(certPath) {
return nil
}
organization := "tdex"
now := time.Now()
validUntil := now.AddDate(1, 0, 0)
// Generate a serial number that's below the serialNumberLimit.
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
return fmt.Errorf("failed to generate serial number: %s", err)
}
// Collect the host's IP addresses, including loopback, in a slice.
ipAddresses := []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("::1")}
if len(extraIPs) > 0 {
for _, ip := range extraIPs {
ipAddresses = append(ipAddresses, net.ParseIP(ip))
}
}
// addIP appends an IP address only if it isn't already in the slice.
addIP := func(ipAddr net.IP) {
for _, ip := range ipAddresses {
if bytes.Equal(ip, ipAddr) {
return
}
}
ipAddresses = append(ipAddresses, ipAddr)
}
// Add all the interface IPs that aren't already in the slice.
addrs, err := net.InterfaceAddrs()
if err != nil {
return err
}
for _, a := range addrs {
ipAddr, _, err := net.ParseCIDR(a.String())
if err == nil {
addIP(ipAddr)
}
}
host, err := os.Hostname()
if err != nil {
return err
}
dnsNames := []string{host}
if host != "localhost" {
dnsNames = append(dnsNames, "localhost")
}
if len(extraDomains) > 0 {
for _, domain := range extraDomains {
dnsNames = append(dnsNames, domain)
}
}
dnsNames = append(dnsNames, "unix", "unixpacket")
priv, err := createOrLoadTLSKey(keyPath)
if err != nil {
return err
}
keybytes, err := x509.MarshalECPrivateKey(priv)
if err != nil {
return err
}
// construct certificate template
template := x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
Organization: []string{organization},
CommonName: host,
},
NotBefore: now.Add(-time.Hour * 24),
NotAfter: validUntil,
KeyUsage: x509.KeyUsageKeyEncipherment |
x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
IsCA: true,
BasicConstraintsValid: true,
DNSNames: dnsNames,
IPAddresses: ipAddresses,
}
derBytes, err := x509.CreateCertificate(
rand.Reader, &template, &template, &priv.PublicKey, priv,
)
if err != nil {
return fmt.Errorf("failed to create certificate: %v", err)
}
certBuf := &bytes.Buffer{}
if err := pem.Encode(
certBuf, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes},
); err != nil {
return fmt.Errorf("failed to encode certificate: %v", err)
}
keyBuf := &bytes.Buffer{}
if err := pem.Encode(
keyBuf, &pem.Block{Type: "EC PRIVATE KEY", Bytes: keybytes},
); err != nil {
return fmt.Errorf("failed to encode private key: %v", err)
}
if err := ioutil.WriteFile(certPath, certBuf.Bytes(), 0644); err != nil {
return err
}
if err := ioutil.WriteFile(keyPath, keyBuf.Bytes(), 0600); err != nil {
os.Remove(certPath)
return err
}
return nil
}
func serveMux(
address, tlsKey, tlsCert string,
grpcServer *grpc.Server, http1Server, http2Server *http.Server,
) (cmux.CMux, error) {
lis, err := net.Listen("tcp", address)
if err != nil {
return nil, err
}
if tlsKey != "" {
tlsConfig, err := getTlsConfig(tlsKey, tlsCert)
if err != nil {
return nil, err
}
lis = tls.NewListener(lis, tlsConfig)
}
mux := cmux.New(lis)
grpcL := mux.MatchWithWriters(cmux.HTTP2MatchHeaderFieldSendSettings("content-type", "application/grpc"))
http2L := mux.Match(cmux.HTTP2())
http1L := mux.Match(cmux.HTTP1())
go grpcServer.Serve(grpcL)
go http2Server.Serve(http2L)
go http1Server.Serve(http1L)
go mux.Serve()
return mux, nil
}
func createOrLoadTLSKey(keyPath string) (*ecdsa.PrivateKey, error) {
if !pathExists(keyPath) {
return ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
}
b, err := ioutil.ReadFile(keyPath)
if err != nil {
return nil, err
}
key, err := privateKeyFromPEM(b)
if err != nil {
return nil, err
}
return key.(*ecdsa.PrivateKey), nil
}
func privateKeyFromPEM(pemBlock []byte) (crypto.PrivateKey, error) {
var derBlock *pem.Block
for {
derBlock, pemBlock = pem.Decode(pemBlock)
if derBlock == nil {
return nil, fmt.Errorf("tls: failed to find any PEM data in key input")
}
if derBlock.Type == "PRIVATE KEY" || strings.HasSuffix(derBlock.Type, " PRIVATE KEY") {
break
}
}
return parsePrivateKey(derBlock.Bytes)
}
func parsePrivateKey(der []byte) (crypto.PrivateKey, error) {
if key, err := x509.ParsePKCS1PrivateKey(der); err == nil {
return key, nil
}
if key, err := x509.ParsePKCS8PrivateKey(der); err == nil {
switch key := key.(type) {
case *rsa.PrivateKey, *ecdsa.PrivateKey, ed25519.PrivateKey:
return key, nil
default:
return nil, fmt.Errorf("tls: found unknown private key type in PKCS#8 wrapping")
}
}
if key, err := x509.ParseECPrivateKey(der); err == nil {
return key, nil
}
return nil, fmt.Errorf("tls: failed to parse private key")
}
/*
gRPC web wrapper
*/
func newGRPCWrappedServer(
addr string,
grpcServer *grpc.Server,
grpcGateway http.Handler,
httpHandlers map[string]func(w http.ResponseWriter, req *http.Request),
) (httpSrv *http.Server, http2Srv *http.Server) {
grpcWebServer := grpcweb.WrapServer(
grpcServer,
grpcweb.WithCorsForRegisteredEndpointsOnly(false),
grpcweb.WithOriginFunc(func(origin string) bool { return true }),
)
handler := func(w http.ResponseWriter, req *http.Request) {
if isOptionRequest(req) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
w.Header().Add("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
return
}
if isGetRequest(req) {
if handler, ok := httpHandlers[req.URL.Path]; ok {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
w.Header().Add("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
handler(w, req)
return
}
}
if isValidRequest(req) {
grpcWebServer.ServeHTTP(w, req)
return
}
if grpcGateway != nil {
if isHttpRequest(req) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
w.Header().Add("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
grpcGateway.ServeHTTP(w, req)
return
}
}
msg := "received a request that could not be matched to grpc or grpc-web"
log.Warn(msg)
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(msg))
}
mux := http.NewServeMux()
mux.HandleFunc("/", handler)
httpSrv = &http.Server{
Addr: addr,
Handler: mux,
}
http2Srv = &http.Server{
Addr: addr,
Handler: h2c.NewHandler(http.HandlerFunc(handler), &http2.Server{}),
}
return
}
func isGetRequest(req *http.Request) bool {
return req.Method == http.MethodGet
}
func isOptionRequest(req *http.Request) bool {
return req.Method == http.MethodOptions
}
func isValidRequest(req *http.Request) bool {
return isValidGrpcWebOptionRequest(req) || isValidGrpcWebRequest(req)
}
func isValidGrpcWebRequest(req *http.Request) bool {
return req.Method == http.MethodPost && isValidGrpcContentTypeHeader(req.Header.Get("content-type"))
}
func isValidGrpcContentTypeHeader(contentType string) bool {
return strings.HasPrefix(contentType, "application/grpc-web-text") ||
strings.HasPrefix(contentType, "application/grpc-web")
}
func isValidGrpcWebOptionRequest(req *http.Request) bool {
accessControlHeader := req.Header.Get("Access-Control-Request-Headers")
return req.Method == http.MethodOptions &&
strings.Contains(accessControlHeader, "x-grpc-web") &&
strings.Contains(accessControlHeader, "content-type")
}
func isHttpRequest(req *http.Request) bool {
return strings.ToLower(req.Method) == "get" ||
strings.Contains(req.Header.Get("Content-Type"), "application/json")
}
func getTlsConfig(tlsKey, tlsCert string) (*tls.Config, error) {
if tlsKey == "" || tlsCert == "" {
return nil, errors.New("tls_key and tls_cert both needs to be provided")
}
certificate, err := tls.LoadX509KeyPair(tlsCert, tlsKey)
if err != nil {
return nil, err
}
config := &tls.Config{
MinVersion: tls.VersionTLS12,
NextProtos: []string{"http/1.1", http2.NextProtoTLS, "h2-14"}, // h2-14 is just for compatibility. will be eventually removed.
Certificates: []tls.Certificate{certificate},
CipherSuites: []uint16{
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
},
}
config.Rand = rand.Reader
return config, nil
}