-
Notifications
You must be signed in to change notification settings - Fork 681
/
http.go
150 lines (129 loc) · 4.13 KB
/
http.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
// Copyright Project Contour Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package httpsvc provides a HTTP/1.x Service which is compatible with the
// workgroup.Group API.
package httpsvc
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"net"
"net/http"
"os"
"strconv"
"time"
"github.com/sirupsen/logrus"
)
// Service is a HTTP/1.x endpoint which is compatible with the workgroup.Group API.
type Service struct {
Addr string
Port int
// TLS parameters
CABundle string
Cert string
Key string
logrus.FieldLogger
http.ServeMux
}
func (svc *Service) NeedLeaderElection() bool {
return false
}
// Implements controller-runtime Runnable interface.
// When context is done, http server will shutdown.
func (svc *Service) Start(ctx context.Context) (err error) {
defer func() {
if err != nil {
svc.WithError(err).Error("terminated HTTP server with error")
} else {
svc.Info("stopped HTTP server")
}
}()
// Create TLSConfig if both certificate and key are provided.
var tlsConfig *tls.Config
if svc.Cert != "" && svc.Key != "" {
tlsConfig, err = svc.tlsConfig()
if err != nil {
return err
}
}
// If one of the TLS parameters are defined, at least server certificate
// and key must be defined.
if (svc.Cert != "" || svc.Key != "" || svc.CABundle != "") &&
(svc.Cert == "" || svc.Key == "") {
svc.Fatal("you must supply at least server certificate and key TLS parameters or none of them")
}
s := http.Server{
Addr: net.JoinHostPort(svc.Addr, strconv.Itoa(svc.Port)),
Handler: &svc.ServeMux,
ReadTimeout: 10 * time.Second,
ReadHeaderTimeout: 10 * time.Second, // To mitigate Slowloris attacks: https://www.cloudflare.com/learning/ddos/ddos-attack-tools/slowloris/
WriteTimeout: 5 * time.Minute, // allow for long trace requests
MaxHeaderBytes: 1 << 11, // 8kb should be enough for anyone
TLSConfig: tlsConfig,
}
go func() {
// wait for stop signal from group.
<-ctx.Done()
// shutdown the server with 5 seconds grace.
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
_ = s.Shutdown(ctx) // ignored, will always be a cancellation error
}()
if s.TLSConfig != nil {
svc.WithField("address", s.Addr).Info("started HTTPS server")
return s.ListenAndServeTLS(svc.Cert, svc.Key)
}
svc.WithField("address", s.Addr).Info("started HTTP server")
return s.ListenAndServe()
}
func (svc *Service) tlsConfig() (*tls.Config, error) {
// Define a closure that lazily loads certificates and key at TLS handshake
// to ensure that latest certificates are used in case they have been rotated.
loadConfig := func() (*tls.Config, error) {
cert, err := tls.LoadX509KeyPair(svc.Cert, svc.Key)
if err != nil {
return nil, err
}
clientAuth := tls.NoClientCert
var certPool *x509.CertPool
if svc.CABundle != "" {
clientAuth = tls.RequireAndVerifyClientCert
ca, err := os.ReadFile(svc.CABundle)
if err != nil {
return nil, err
}
certPool = x509.NewCertPool()
if ok := certPool.AppendCertsFromPEM(ca); !ok {
return nil, fmt.Errorf("unable to append certificate in %s to CA pool", svc.CABundle)
}
}
return &tls.Config{
Certificates: []tls.Certificate{cert},
ClientAuth: clientAuth,
ClientCAs: certPool,
MinVersion: tls.VersionTLS13,
}, nil
}
// Attempt to load certificates and key to catch configuration errors early.
if _, err := loadConfig(); err != nil {
return nil, err
}
return &tls.Config{
MinVersion: tls.VersionTLS12,
GetConfigForClient: func(*tls.ClientHelloInfo) (*tls.Config, error) {
return loadConfig()
},
}, nil
}