-
Notifications
You must be signed in to change notification settings - Fork 1
/
tlsconfig.go
193 lines (160 loc) · 5.17 KB
/
tlsconfig.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
package tlsconfig
import (
"crypto/tls"
"crypto/x509"
"net/http"
"os"
"sync"
"time"
"github.com/effective-security/xlog"
"github.com/pkg/errors"
)
var logger = xlog.NewPackageLogger("github.com/effective-security/porto/pkg", "tlsconfig")
// NewServerTLSFromFiles will build a tls.Config from the supplied certificate, key
// and optional trust roots files, these files are all expected to be PEM encoded.
// The file paths are relative to the working directory if not specified in absolute
// format.
// caBundle is optional.
// rootsFile is optional, if not specified the standard OS CA roots will be used.
func NewServerTLSFromFiles(certFile, keyFile, rootsFile string, clientauthType tls.ClientAuthType) (*tls.Config, error) {
tlscert, err := LoadX509KeyPairWithOCSP(certFile, keyFile)
if err != nil {
return nil, err
}
var roots *x509.CertPool
if rootsFile != "" {
rootsBytes, err := os.ReadFile(rootsFile)
if err != nil {
return nil, errors.WithStack(err)
}
roots = x509.NewCertPool()
roots.AppendCertsFromPEM(rootsBytes)
}
return &tls.Config{
MinVersion: tls.VersionTLS12,
NextProtos: []string{"h2", "http/1.1"},
Certificates: []tls.Certificate{*tlscert},
ClientAuth: clientauthType,
ClientCAs: roots,
RootCAs: roots,
}, nil
}
// NewClientTLSFromFiles will build a tls.Config from the supplied certificate, key
// and optional trust roots files, these files are all expected to be PEM encoded.
// The file paths are relative to the working directory if not specified in absolute
// format.
// caBundle is optional.
// rootsFile is optional, if not specified the standard OS CA roots will be used.
func NewClientTLSFromFiles(certFile, keyFile, rootsFile string) (*tls.Config, error) {
var roots *x509.CertPool
if rootsFile != "" {
rootsBytes, err := os.ReadFile(rootsFile)
if err != nil {
return nil, errors.WithStack(err)
}
roots = x509.NewCertPool()
roots.AppendCertsFromPEM(rootsBytes)
}
cfg := &tls.Config{
MinVersion: tls.VersionTLS12,
NextProtos: []string{"h2", "http/1.1"},
//Certificates: []tls.Certificate{tlscert},
ClientCAs: roots,
RootCAs: roots,
}
if certFile != "" {
tlscert, err := LoadX509KeyPairWithOCSP(certFile, keyFile)
if err != nil {
return nil, err
}
if tlscert.Leaf == nil && len(tlscert.Certificate) > 0 {
tlscert.Leaf, err = x509.ParseCertificate(tlscert.Certificate[0])
if err != nil {
logger.KV(xlog.WARNING, "reason", "ParseCertificate", "err", err)
}
}
cfg.Certificates = []tls.Certificate{*tlscert}
}
return cfg, nil
}
// NewClientTLSWithReloader is a wrapper around NewClientTLSFromFiles with NewKeypairReloader
func NewClientTLSWithReloader(certFile, keyFile, rootsFile string, checkInterval time.Duration) (*tls.Config, *KeypairReloader, error) {
tlsCfg, err := NewClientTLSFromFiles(certFile, keyFile, rootsFile)
if err != nil {
return nil, nil, err
}
tlsloader, err := NewKeypairReloader("", certFile, keyFile, checkInterval)
if err != nil {
return nil, nil, err
}
tlsCfg.GetClientCertificate = tlsloader.GetClientCertificateFunc()
return tlsCfg, tlsloader, nil
}
// NewHTTPTransportWithReloader creates an HTTPTransport based on a
// given Transport (or http.DefaultTransport).
func NewHTTPTransportWithReloader(
certFile, keyFile, rootsFile string,
checkInterval time.Duration,
HTTPUserTransport *http.Transport) (*HTTPTransport, error) {
transport := HTTPUserTransport
if transport == nil {
transport = http.DefaultTransport.(*http.Transport).Clone()
transport.MaxIdleConnsPerHost = 100
transport.MaxConnsPerHost = 100
transport.MaxIdleConns = 100
}
tlsCfg, err := NewClientTLSFromFiles(certFile, keyFile, rootsFile)
if err != nil {
return nil, err
}
tlsloader, err := NewKeypairReloader("", certFile, keyFile, checkInterval)
if err != nil {
return nil, err
}
tripper := &HTTPTransport{
transport: transport,
tlsConfig: tlsCfg,
reloader: tlsloader,
}
tlsloader.OnReload(func(tlscert *tls.Certificate) {
logger.KV(xlog.NOTICE, "reason", "onReload", "cn", tlscert.Leaf.Subject.CommonName, "expires", tlscert.Leaf.NotAfter.Format(time.RFC3339))
tripper.lock.Lock()
tripper.tlsConfig = tripper.tlsConfig.Clone()
tripper.tlsConfig.Certificates = []tls.Certificate{*tlscert}
tripper.transport.CloseIdleConnections()
tripper.lock.Unlock()
})
return tripper, nil
}
// HTTPTransport is an implementation of http.RoundTripper with an
// auto-updating TLSClientConfig.
type HTTPTransport struct {
transport *http.Transport
tlsConfig *tls.Config
reloader *KeypairReloader
lock sync.RWMutex
}
// RoundTrip implements the http.RoundTripper interface.
func (t *HTTPTransport) RoundTrip(r *http.Request) (*http.Response, error) {
t.lock.Lock()
cfg := t.tlsConfig
t.transport.TLSClientConfig = cfg
t.lock.Unlock()
resp, err := t.transport.RoundTrip(r)
if err != nil {
return nil, errors.WithStack(err)
}
return resp, nil
}
// Close will close the reloader and release its resources
func (t *HTTPTransport) Close() error {
t.lock.RLock()
defer t.lock.RUnlock()
if t.reloader == nil {
return errors.New("already closed")
}
err := t.reloader.Close()
t.reloader = nil
return err
}
var _ http.RoundTripper = (*HTTPTransport)(nil)