Repository navigation
Expand file tree
/
Copy pathevents_test.go
More file actions
297 lines (267 loc) · 9.16 KB
/
Copy pathevents_test.go
File metadata and controls
297 lines (267 loc) · 9.16 KB
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
// Copyright 2015 Matthew Holt
//
// 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 certmagic
import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"errors"
"math/big"
"net"
"testing"
"time"
"go.uber.org/zap"
)
func TestShouldEmit(t *testing.T) {
noop := func(context.Context, string, map[string]any) error { return nil }
for _, tc := range []struct {
name string
cfg Config
expects bool
}{
{
name: "no handler at all",
cfg: Config{},
expects: false,
},
{
name: "handler, but no way to ask about subscribers",
cfg: Config{OnEvent: noop},
expects: true,
},
{
name: "handler says it has a subscriber",
cfg: Config{OnEvent: noop, ShouldEmitFunc: func(string) bool { return true }},
expects: true,
},
{
name: "handler says it has no subscriber",
cfg: Config{OnEvent: noop, ShouldEmitFunc: func(string) bool { return false }},
expects: false,
},
{
name: "no handler outweighs a subscriber claim",
cfg: Config{ShouldEmitFunc: func(string) bool { return true }},
expects: false,
},
} {
t.Run(tc.name, func(t *testing.T) {
if got := tc.cfg.shouldEmit("tls_get_certificate"); got != tc.expects {
t.Errorf("shouldEmit() = %v, want %v", got, tc.expects)
}
})
}
}
// The event name has to reach ShouldEmitFunc, or a caller cannot
// distinguish the handshake event from the rest.
func TestShouldEmitReceivesEventName(t *testing.T) {
var asked []string
cfg := Config{
OnEvent: func(context.Context, string, map[string]any) error { return nil },
ShouldEmitFunc: func(name string) bool { asked = append(asked, name); return false },
}
cfg.shouldEmit("cert_obtained")
cfg.shouldEmit("tls_get_certificate")
if len(asked) != 2 || asked[0] != "cert_obtained" || asked[1] != "tls_get_certificate" {
t.Errorf("ShouldEmitFunc was asked about %v", asked)
}
}
func testCertificate(tb testing.TB) tls.Certificate {
tb.Helper()
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
tb.Fatal(err)
}
tmpl := &x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{CommonName: "example.com"},
DNSNames: []string{"example.com"},
NotBefore: time.Now().Add(-time.Hour),
NotAfter: time.Now().Add(24 * time.Hour),
}
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
if err != nil {
tb.Fatal(err)
}
leaf, err := x509.ParseCertificate(der)
if err != nil {
tb.Fatal(err)
}
return tls.Certificate{Certificate: [][]byte{der}, PrivateKey: key, Leaf: leaf}
}
type testConn struct{ net.Conn }
func (testConn) RemoteAddr() net.Addr { return &net.TCPAddr{IP: net.IPv4(10, 0, 0, 1), Port: 51234} }
func (testConn) LocalAddr() net.Addr { return &net.TCPAddr{IP: net.IPv4(10, 0, 0, 2), Port: 443} }
func testClientHello() *tls.ClientHelloInfo {
return &tls.ClientHelloInfo{
ServerName: "example.com",
CipherSuites: []uint16{tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, tls.TLS_AES_128_GCM_SHA256},
SupportedCurves: []tls.CurveID{tls.CurveP256, tls.X25519},
SupportedPoints: []uint8{0},
SupportedVersions: []uint16{tls.VersionTLS13, tls.VersionTLS12},
SignatureSchemes: []tls.SignatureScheme{tls.ECDSAWithP256AndSHA256},
Conn: testConn{},
}
}
// handshakeConfig returns a Config serving one cached certificate for
// example.com, with the given event hooks installed.
func handshakeConfig(tb testing.TB, onEvent func(context.Context, string, map[string]any) error, hasSubs func(string) bool) *Config {
tb.Helper()
var cfg *Config
cache := NewCache(CacheOptions{
GetConfigForCert: func(Certificate) (*Config, error) { return cfg, nil },
Logger: zap.NewNop(),
})
tb.Cleanup(cache.Stop)
cfg = New(cache, Config{
Storage: &FileStorage{Path: tb.TempDir()},
Logger: zap.NewNop(),
OnEvent: onEvent,
ShouldEmitFunc: hasSubs,
})
if _, err := cfg.CacheUnmanagedTLSCertificate(context.Background(), testCertificate(tb), nil); err != nil {
tb.Fatal(err)
}
return cfg
}
func TestGetCertificateSkipsUnobservedEvent(t *testing.T) {
var emitted []string
record := func(_ context.Context, name string, _ map[string]any) error {
emitted = append(emitted, name)
return nil
}
t.Run("subscribed", func(t *testing.T) {
cfg := handshakeConfig(t, record, func(string) bool { return true })
emitted = nil // drop what caching the certificate emitted
if _, err := cfg.GetCertificate(testClientHello()); err != nil {
t.Fatal(err)
}
if len(emitted) != 1 || emitted[0] != "tls_get_certificate" {
t.Errorf("emitted %v, want [tls_get_certificate]", emitted)
}
})
t.Run("not subscribed", func(t *testing.T) {
cfg := handshakeConfig(t, record, func(string) bool { return false })
emitted = nil // drop what caching the certificate emitted
if _, err := cfg.GetCertificate(testClientHello()); err != nil {
t.Fatal(err)
}
if len(emitted) != 0 {
t.Errorf("emitted %v, want nothing", emitted)
}
})
t.Run("no way to ask", func(t *testing.T) {
cfg := handshakeConfig(t, record, nil)
emitted = nil // drop what caching the certificate emitted
if _, err := cfg.GetCertificate(testClientHello()); err != nil {
t.Fatal(err)
}
if len(emitted) != 1 {
t.Errorf("emitted %v, want [tls_get_certificate]", emitted)
}
})
}
// A subscribed handler can still abort the handshake.
func TestGetCertificateHonorsEventAbort(t *testing.T) {
abort := errors.New("nope")
cfg := handshakeConfig(t,
func(context.Context, string, map[string]any) error { return abort },
func(string) bool { return true })
if _, err := cfg.GetCertificate(testClientHello()); !errors.Is(err, abort) {
t.Errorf("got error %v, want it to wrap %v", err, abort)
}
}
// What a handshake pays for tls_get_certificate when something is subscribed
// to it: the data is built and dispatched, as before.
func BenchmarkGetCertificateEventSubscribed(b *testing.B) {
cfg := handshakeConfig(b,
func(context.Context, string, map[string]any) error { return nil },
func(string) bool { return true })
benchmarkGetCertificate(b, cfg)
}
// The common case: a handler is installed for other events, but nothing is
// subscribed to this one.
func BenchmarkGetCertificateEventUnsubscribed(b *testing.B) {
cfg := handshakeConfig(b,
func(context.Context, string, map[string]any) error { return nil },
func(string) bool { return false })
benchmarkGetCertificate(b, cfg)
}
func benchmarkGetCertificate(b *testing.B, cfg *Config) {
b.Helper()
hello := testClientHello()
if _, err := cfg.GetCertificate(hello); err != nil {
b.Fatal(err)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := cfg.GetCertificate(hello); err != nil {
b.Fatal(err)
}
}
}
// The predicate is written against a particular OnEvent, so a config that
// brings its own handler must not inherit Default's predicate: it knows
// nothing about that handler and would silence it.
func TestShouldEmitFuncInheritedWithOnEvent(t *testing.T) {
defaultOnEvent := func(context.Context, string, map[string]any) error { return nil }
defaultPredicate := func(string) bool { return false }
oldOnEvent, oldPredicate := Default.OnEvent, Default.ShouldEmitFunc
Default.OnEvent, Default.ShouldEmitFunc = defaultOnEvent, defaultPredicate
t.Cleanup(func() { Default.OnEvent, Default.ShouldEmitFunc = oldOnEvent, oldPredicate })
newConfig := func(tb testing.TB, cfg Config) *Config {
tb.Helper()
var out *Config
cache := NewCache(CacheOptions{
GetConfigForCert: func(Certificate) (*Config, error) { return out, nil },
Logger: zap.NewNop(),
})
tb.Cleanup(cache.Stop)
cfg.Storage = &FileStorage{Path: tb.TempDir()}
cfg.Logger = zap.NewNop()
out = New(cache, cfg)
return out
}
t.Run("own handler does not inherit the predicate", func(t *testing.T) {
cfg := newConfig(t, Config{
OnEvent: func(context.Context, string, map[string]any) error { return nil },
})
if cfg.ShouldEmitFunc != nil {
t.Error("inherited Default's predicate onto a caller's own OnEvent")
}
if !cfg.shouldEmit("tls_get_certificate") {
t.Error("caller's handler is being silenced")
}
})
t.Run("inherited handler brings its predicate", func(t *testing.T) {
cfg := newConfig(t, Config{})
if cfg.ShouldEmitFunc == nil {
t.Fatal("did not inherit Default's predicate alongside Default's OnEvent")
}
if cfg.shouldEmit("tls_get_certificate") {
t.Error("Default's predicate is not being consulted")
}
})
t.Run("own predicate is kept", func(t *testing.T) {
cfg := newConfig(t, Config{ShouldEmitFunc: func(string) bool { return true }})
if !cfg.shouldEmit("tls_get_certificate") {
t.Error("caller's own predicate was replaced")
}
})
}