-
Notifications
You must be signed in to change notification settings - Fork 48
/
http_mtls_server.go
76 lines (65 loc) · 1.75 KB
/
http_mtls_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
package test_mtls_server
import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
)
type MTLSServer struct {
Server *httptest.Server
CAPath string
ClientKeyPath string
ClientCrtPath string
}
func NewMTLSServer(handler http.Handler) (*MTLSServer, error) {
certsPath, err := filepath.Abs("test_mtls_server")
if err != nil {
return nil, err
}
caPath := filepath.Join(certsPath, "ca.crt")
serverKeyPath := filepath.Join(certsPath, "server.key")
serverCrtPath := filepath.Join(certsPath, "server.crt")
clientKeyPath := filepath.Join(certsPath, "client.key")
clientCrtPath := filepath.Join(certsPath, "client.crt")
caCertPem, err := os.ReadFile(caPath)
if err != nil {
return nil, err
}
caPool := x509.NewCertPool()
ok := caPool.AppendCertsFromPEM(caCertPem)
if !ok {
return nil, fmt.Errorf("Unable to append ca cert (%s) to ca pool", caPath)
}
serverCrt, err := tls.LoadX509KeyPair(serverCrtPath, serverKeyPath)
if err != nil {
return nil, err
}
server := httptest.NewUnstartedServer(handler)
server.TLS = &tls.Config{
Certificates: []tls.Certificate{serverCrt},
ClientAuth: tls.RequireAndVerifyClientCert,
ClientCAs: caPool,
MinVersion: tls.VersionTLS12,
VerifyPeerCertificate: func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
for _, chain := range verifiedChains {
for _, domain := range []string{"127.0.0.1", "localhost", "osbuild.org"} {
if chain[0].VerifyHostname(domain) == nil {
return nil
}
}
}
return errors.New("domain not in allowlist")
},
}
server.StartTLS()
return &MTLSServer{
Server: server,
CAPath: caPath,
ClientKeyPath: clientKeyPath,
ClientCrtPath: clientCrtPath,
}, nil
}