-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.go
114 lines (100 loc) · 3.02 KB
/
server.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
package server
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
tls2 "github.com/armory/go-yaml-tools/pkg/tls"
"io/ioutil"
"net/http"
)
type Server struct {
config *ServerConfig
server *http.Server
}
func NewServer(config *ServerConfig) *Server {
return &Server{
config: config,
}
}
// Start starts the server on the configured port
func (s *Server) Start(router http.Handler) error {
if !s.config.Ssl.Enabled {
return s.startHttp(router)
}
return s.startTls(router)
}
func (s *Server) Shutdown(ctx context.Context) error {
return s.server.Shutdown(ctx)
}
func (s *Server) startHttp(router http.Handler) error {
s.server = &http.Server{
Addr: s.config.GetAddr(),
Handler: router,
}
return s.server.ListenAndServe()
}
func (s *Server) startTls(router http.Handler) error {
tlsConfig, err := s.tlsConfig()
if err != nil {
return err
}
certMode := s.getClientCertMode()
if certMode != tls.NoClientCert {
// With mTLS, we'll parse our PEM to discover CAs with which to validate client certificates
caFile := s.config.Ssl.CAcertFile
if caFile == "" {
// Fall back to cert file - could be a combined PEM (e.g. self signed)
caFile = s.config.Ssl.CertFile
} else if err := tls2.CheckFileExists(caFile); err != nil {
return fmt.Errorf("error with certificate authority file %s: %w", caFile, err)
}
// Create a CA certificate pool and add our server certificate
caCert, err := ioutil.ReadFile(caFile)
if err != nil {
return err
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
tlsConfig.ClientCAs = caCertPool
tlsConfig.ClientAuth = certMode
}
// Discover the server name based on given certificates
tlsConfig.BuildNameToCertificate()
// Create a Server instance to listen on port 8443 with the TLS config
s.server = &http.Server{
Addr: s.config.GetAddr(),
Handler: router,
TLSConfig: tlsConfig,
}
// Listen to HTTPS connections with the server certificate and wait
return s.server.ListenAndServeTLS("", "")
}
func (s *Server) getClientCertMode() tls.ClientAuthType {
switch s.config.Ssl.ClientAuth {
case ClientAuthWant:
return tls.VerifyClientCertIfGiven
case ClientAuthNeed:
return tls.RequireAndVerifyClientCert
case ClientAuthAny:
return tls.RequireAnyClientCert
case ClientAuthRequest:
return tls.RequestClientCert
default:
return tls.NoClientCert
}
}
// tlsConfig prepares the TLS config of the server
// certFile must contain the certificate of the server. It can also contain the private key (optionally encrypted)
// keyFile is needed if the certFile doesn't contain the private key. It can also be encrypted.
func (s *Server) tlsConfig() (*tls.Config, error) {
c, err := tls2.GetX509KeyPair(s.config.Ssl.CertFile, s.config.Ssl.KeyFile, s.config.Ssl.KeyPassword)
if err != nil {
return nil, fmt.Errorf("error with certificate file %s: %w", s.config.Ssl.CertFile, err)
}
return &tls.Config{
Certificates: []tls.Certificate{c},
PreferServerCipherSuites: true,
MinVersion: tls.VersionTLS12,
}, nil
}