-
-
Notifications
You must be signed in to change notification settings - Fork 357
Expand file tree
/
Copy pathlocalcache_test.go
More file actions
418 lines (373 loc) · 14.8 KB
/
Copy pathlocalcache_test.go
File metadata and controls
418 lines (373 loc) · 14.8 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// 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 (
"bytes"
"context"
"crypto/x509"
"crypto/x509/pkix"
"math/big"
"testing"
"time"
"github.com/mholt/acmez/v3/acme"
"golang.org/x/crypto/ocsp"
)
func testLocalCacheConfig(t *testing.T) (*Config, *ACMEIssuer, *recordingStorage, *FileStorage) {
t.Helper()
am := &ACMEIssuer{CA: "https://example.com/acme/directory"}
storage := &recordingStorage{Storage: &FileStorage{Path: t.TempDir()}}
localCache := &FileStorage{Path: t.TempDir()}
cfg := &Config{
Issuers: []Issuer{am},
Storage: storage,
Logger: defaultTestLogger,
certCache: &Cache{
cache: make(map[string]Certificate),
cacheIndex: make(map[string][]string),
logger: defaultTestLogger,
},
}
am.config = cfg
return cfg, am, storage, localCache
}
func testCertResource(issuer Issuer, domain string) CertificateResource {
return CertificateResource{
SANs: []string{domain},
PrivateKeyPEM: []byte("private key"),
CertificatePEM: []byte("certificate"),
IssuerData: mustJSON(acme.Certificate{URL: "https://example.com/cert"}),
issuerKey: issuer.IssuerKey(),
}
}
// testIssuedCertResource is like testCertResource, but with a real
// self-signed certificate and its matching private key, for tests that
// need the two to actually pair up.
func testIssuedCertResource(t *testing.T, issuer Issuer, domain string, notBefore time.Time) CertificateResource {
t.Helper()
_, key, certPEM := mustIssueTestCertificate(t, &x509.Certificate{
SerialNumber: big.NewInt(notBefore.Unix()),
Subject: pkix.Name{CommonName: domain},
DNSNames: []string{domain},
NotBefore: notBefore,
NotAfter: notBefore.Add(90 * 24 * time.Hour),
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
IsCA: true,
}, nil, nil)
keyPEM, err := PEMEncodePrivateKey(key)
if err != nil {
t.Fatalf("Expected no error encoding private key, got: %v", err)
}
res := testCertResource(issuer, domain)
res.CertificatePEM = certPEM
res.PrivateKeyPEM = keyPEM
return res
}
// simulateTornWrite leaves cfg's local cache the way a save that failed
// partway through would: storage has a renewed certificate and its key, while
// the local cache has the renewed certificate next to the key it replaced. It
// returns the renewed resource.
func simulateTornWrite(t *testing.T, cfg *Config, issuer Issuer, localCache Storage, domain string) CertificateResource {
t.Helper()
ctx := context.Background()
now := time.Now()
// the certificate this instance has been serving, in both tiers
err := cfg.saveCertResource(ctx, issuer, testIssuedCertResource(t, issuer, domain, now.Add(-60*24*time.Hour)))
if err != nil {
t.Fatalf("Expected no error saving cert resource, got: %v", err)
}
// as if another instance renewed it, which only reaches storage
renewed := testIssuedCertResource(t, issuer, domain, now)
localCacheOnly := cfg.LocalCache
cfg.LocalCache = nil
err = cfg.saveCertResource(ctx, issuer, renewed)
cfg.LocalCache = localCacheOnly
if err != nil {
t.Fatalf("Expected no error saving renewed cert resource, got: %v", err)
}
// and this instance picked up the renewed certificate, but not its key
err = localCache.Store(ctx, StorageKeys.SiteCert(issuer.IssuerKey(), domain), renewed.CertificatePEM)
if err != nil {
t.Fatalf("Expected no error storing renewed certificate locally, got: %v", err)
}
return renewed
}
func TestLocalCache(t *testing.T) {
ctx := context.Background()
const domain = "example.com"
cfg, am, storage, localCache := testLocalCacheConfig(t)
cfg.LocalCache = localCache
certKey := StorageKeys.SiteCert(am.IssuerKey(), domain)
saved := testIssuedCertResource(t, am, domain, time.Now())
err := cfg.saveCertResource(ctx, am, saved)
if err != nil {
t.Fatalf("Expected no error saving cert resource, got: %v", err)
}
// saving writes through to the local cache, so serving the
// certificate afterwards should not need storage at all
storage.calls = nil
certRes, err := cfg.loadCertResource(ctx, am, domain, cfg.cachedStorage())
if err != nil {
t.Fatalf("Expected no error loading cert resource, got: %v", err)
}
if !bytes.Equal(certRes.CertificatePEM, saved.CertificatePEM) {
t.Error("Expected the saved certificate to be loaded")
}
if len(storage.calls) > 0 {
t.Errorf("Expected no storage calls, got: %v", storage.calls)
}
// as if another instance renewed the certificate
err = storage.Store(ctx, certKey, []byte("renewed certificate"))
if err != nil {
t.Fatalf("Expected no error storing renewed certificate, got: %v", err)
}
// cluster-sensitive loads see storage, and refresh the local cache
certRes, err = cfg.loadCertResource(ctx, am, domain, cfg.groundTruthStorage())
if err != nil {
t.Fatalf("Expected no error loading cert resource, got: %v", err)
}
if string(certRes.CertificatePEM) != "renewed certificate" {
t.Errorf("Expected 'renewed certificate', got: %s", certRes.CertificatePEM)
}
cached, err := localCache.Load(ctx, certKey)
if err != nil {
t.Fatalf("Expected no error loading from local cache, got: %v", err)
}
if string(cached) != "renewed certificate" {
t.Errorf("Expected local cache to be refreshed, got: %s", cached)
}
// deleting the site assets evicts them from the local cache too
err = cfg.deleteSiteAssets(ctx, am.IssuerKey(), domain)
if err != nil {
t.Fatalf("Expected no error deleting site assets, got: %v", err)
}
if localCache.Exists(ctx, certKey) {
t.Errorf("Expected %s to be gone from the local cache", certKey)
}
}
func TestLocalCacheTornWrite(t *testing.T) {
ctx := context.Background()
const domain = "example.com"
cfg, am, storage, localCache := testLocalCacheConfig(t)
cfg.LocalCache = localCache
cfg.OCSP = OCSPConfig{DisableStapling: true}
renewed := simulateTornWrite(t, cfg, am, localCache, domain)
// the local certificate and key don't match, so loading them has to
// fall back to ground truth storage instead of failing the handshake
cert, err := cfg.CacheManagedCertificate(ctx, domain)
if err != nil {
t.Fatalf("Expected no error caching managed certificate, got: %v", err)
}
expected, err := makeCertificate(renewed.CertificatePEM, renewed.PrivateKeyPEM)
if err != nil {
t.Fatalf("Expected no error making the renewed certificate, got: %v", err)
}
if !bytes.Equal(cert.Leaf.Raw, expected.Leaf.Raw) {
t.Error("Expected the renewed certificate to be loaded")
}
// and the local cache is now the renewed certificate and its own key,
// so the next load doesn't need storage at all
for key, want := range map[string][]byte{
StorageKeys.SiteCert(am.IssuerKey(), domain): renewed.CertificatePEM,
StorageKeys.SitePrivateKey(am.IssuerKey(), domain): renewed.PrivateKeyPEM,
} {
cached, err := localCache.Load(ctx, key)
if err != nil {
t.Fatalf("Expected no error loading %s from local cache, got: %v", key, err)
}
if !bytes.Equal(cached, want) {
t.Errorf("Expected %s in the local cache to be the renewed one", key)
}
}
storage.calls = nil
if _, err = cfg.CacheManagedCertificate(ctx, domain); err != nil {
t.Fatalf("Expected no error caching managed certificate, got: %v", err)
}
if len(storage.calls) > 0 {
t.Errorf("Expected no storage calls after recovery, got: %v", storage.calls)
}
}
func TestLocalCacheStaleCertRenewedByPeer(t *testing.T) {
ctx := context.Background()
const domain = "example.com"
now := time.Now()
cfg, preferred, storage, localCache := testLocalCacheConfig(t)
fallback := &ACMEIssuer{CA: "https://fallback.example.com/acme/directory"}
fallback.config = cfg
cfg.Issuers = []Issuer{preferred, fallback}
cfg.LocalCache = localCache
cfg.OCSP = OCSPConfig{DisableStapling: true}
// this instance has served the preferred issuer's certificate, now
// expired, and has a fallback certificate that is still fine
for issuer, issued := range map[Issuer]time.Time{preferred: now.Add(-100 * 24 * time.Hour), fallback: now.Add(-30 * 24 * time.Hour)} {
if err := cfg.saveCertResource(ctx, issuer, testIssuedCertResource(t, issuer, domain, issued)); err != nil {
t.Fatalf("Expected no error saving cert resource, got: %v", err)
}
}
// another instance renewed the preferred certificate, which only
// reaches storage; the fallback would mask that forever, since it
// never needs renewal itself and so never triggers a storage read
renewed := testIssuedCertResource(t, preferred, domain, now)
cfg.LocalCache = nil
if err := cfg.saveCertResource(ctx, preferred, renewed); err != nil {
t.Fatalf("Expected no error saving renewed cert resource, got: %v", err)
}
cfg.LocalCache = localCache
cert, err := cfg.CacheManagedCertificate(ctx, domain)
if err != nil {
t.Fatalf("Expected no error caching managed certificate, got: %v", err)
}
expected, err := makeCertificate(renewed.CertificatePEM, renewed.PrivateKeyPEM)
if err != nil {
t.Fatalf("Expected no error making the renewed certificate, got: %v", err)
}
if !bytes.Equal(cert.Leaf.Raw, expected.Leaf.Raw) {
t.Error("Expected the renewed certificate from the preferred issuer to be loaded")
}
// the local cache has caught up, so the next load stays local
storage.calls = nil
if _, err = cfg.CacheManagedCertificate(ctx, domain); err != nil {
t.Fatalf("Expected no error caching managed certificate, got: %v", err)
}
if len(storage.calls) > 0 {
t.Errorf("Expected no storage calls once the local cache caught up, got: %v", storage.calls)
}
}
func TestLocalCacheUnknownDomainLoadedOnce(t *testing.T) {
ctx := context.Background()
cfg, _, storage, localCache := testLocalCacheConfig(t)
cfg.LocalCache = localCache
// a certificate the local cache doesn't have is looked up in storage
// anyway, so a certificate that isn't there either is not torn and
// must not be looked up a second time; on-demand TLS asks about
// domains with no certificate all the time
if _, err := cfg.CacheManagedCertificate(ctx, "not-in-storage.example.com"); err == nil {
t.Fatal("Expected an error caching an unknown certificate, got none")
}
loads := 0
for _, call := range storage.calls {
if call.name == "Load" {
loads++
}
}
if loads != 1 {
t.Errorf("Expected a single load, got %d: %v", loads, storage.calls)
}
}
func TestLocalCacheOCSPStaple(t *testing.T) {
ctx := context.Background()
now := time.Now().Add(-1 * time.Hour)
issuerCert, issuerKey, issuerPEM := mustIssueTestCertificate(t, &x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{CommonName: "test issuer"},
NotBefore: now,
NotAfter: now.Add(48 * time.Hour),
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
BasicConstraintsValid: true,
IsCA: true,
}, nil, nil)
issuedCert, issuedKey, issuedPEM := mustIssueTestCertificate(t, &x509.Certificate{
SerialNumber: big.NewInt(2),
Subject: pkix.Name{CommonName: "cached.example"},
DNSNames: []string{"cached.example"},
NotBefore: now,
NotAfter: now.Add(30 * 24 * time.Hour),
OCSPServer: []string{"http://ocsp.example.test"},
KeyUsage: x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
}, issuerCert, issuerKey)
issuedKeyPEM, err := PEMEncodePrivateKey(issuedKey)
if err != nil {
t.Fatalf("Expected no error encoding private key, got: %v", err)
}
bundle := append(append([]byte{}, issuedPEM...), issuerPEM...)
response, err := ocsp.CreateResponse(issuerCert, issuerCert, ocsp.Response{
Status: ocsp.Good,
SerialNumber: issuedCert.SerialNumber,
ThisUpdate: now.UTC(),
NextUpdate: now.Add(24 * time.Hour).UTC(),
}, issuerKey)
if err != nil {
t.Fatalf("Expected no error creating OCSP response, got: %v", err)
}
responder := startOCSPResponder(t, map[string][]byte{
issuedCert.SerialNumber.String(): response,
})
t.Cleanup(responder.Close)
cfg, _, storage, localCache := testLocalCacheConfig(t)
cfg.LocalCache = localCache
cfg.OCSP = OCSPConfig{
ResponderOverrides: map[string]string{"http://ocsp.example.test": responder.URL},
}
// the first staple comes from the responder, and is written to both
cert, err := makeCertificate(issuedPEM, issuedKeyPEM)
if err != nil {
t.Fatalf("Expected no error making certificate, got: %v", err)
}
err = stapleOCSP(ctx, cfg.OCSP, cfg.cachedStorage(), &cert, bundle)
if err != nil {
t.Fatalf("Expected no error stapling OCSP, got: %v", err)
}
if !bytes.Equal(cert.Certificate.OCSPStaple, response) {
t.Fatal("Expected OCSP response to be stapled to certificate")
}
// stapling the same certificate again is served from the local cache
storage.calls = nil
cert, err = makeCertificate(issuedPEM, issuedKeyPEM)
if err != nil {
t.Fatalf("Expected no error making certificate, got: %v", err)
}
err = stapleOCSP(ctx, cfg.OCSP, cfg.cachedStorage(), &cert, bundle)
if err != nil {
t.Fatalf("Expected no error stapling OCSP, got: %v", err)
}
if !bytes.Equal(cert.Certificate.OCSPStaple, response) {
t.Error("Expected OCSP response to be stapled from the local cache")
}
if len(storage.calls) > 0 {
t.Errorf("Expected no storage calls, got: %v", storage.calls)
}
}
func TestWarmLocalCache(t *testing.T) {
ctx := context.Background()
const domain = "example.com"
cfg, am, storage, localCache := testLocalCacheConfig(t)
// no local cache yet, so this only writes to storage
err := cfg.saveCertResource(ctx, am, testIssuedCertResource(t, am, domain, time.Now()))
if err != nil {
t.Fatalf("Expected no error saving cert resource, got: %v", err)
}
if err = cfg.WarmLocalCache(ctx, domain); err == nil {
t.Error("Expected an error warming without a local cache, got none")
}
cfg.LocalCache = localCache
err = cfg.WarmLocalCache(ctx, domain)
if err != nil {
t.Fatalf("Expected no error warming local cache, got: %v", err)
}
storage.calls = nil
_, err = cfg.loadCertResource(ctx, am, domain, cfg.cachedStorage())
if err != nil {
t.Fatalf("Expected no error loading cert resource, got: %v", err)
}
if len(storage.calls) > 0 {
t.Errorf("Expected no storage calls after warming, got: %v", storage.calls)
}
// warming a certificate that isn't in storage must not be silently ignored
err = cfg.WarmLocalCache(ctx, "not-in-storage.example.com")
if err == nil {
t.Error("Expected an error warming an unknown domain, got none")
}
}