-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpersist_test.go
More file actions
157 lines (141 loc) · 4.62 KB
/
Copy pathpersist_test.go
File metadata and controls
157 lines (141 loc) · 4.62 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
package acme
import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/base64"
"encoding/json"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/go-jose/go-jose/v4"
"github.com/letsencrypt/cactus/ca"
"github.com/letsencrypt/cactus/cert"
cactuslog "github.com/letsencrypt/cactus/log"
"github.com/letsencrypt/cactus/signer"
"github.com/letsencrypt/cactus/storage"
)
// TestRestartResume verifies that, after a server restart, an order
// that finished issuing before the restart can still serve its cert.
func TestRestartResume(t *testing.T) {
dir := t.TempDir()
fs, err := storage.New(dir)
if err != nil {
t.Fatal(err)
}
seed := make([]byte, signer.SeedSize)
for i := range seed {
seed[i] = byte(i)
}
logSigner, err := signer.FromSeed(signer.AlgMLDSA44, seed)
if err != nil {
t.Fatal(err)
}
// First server run.
l1, err := cactuslog.New(context.Background(), cactuslog.Config{
LogID: cert.TrustAnchorID("32473.1"),
CosignerID: cert.TrustAnchorID("32473.1"),
Signer: logSigner,
FS: fs,
FlushPeriod: 25 * time.Millisecond,
})
if err != nil {
t.Fatal(err)
}
issuer1, _ := ca.New(l1, "32473.1", 1)
srv1, _ := New(Config{Issuer: issuer1, ChallengeMode: ChallengeAutoPass})
if err := srv1.AttachStorage(fs); err != nil {
t.Fatal(err)
}
hsrv1 := httptest.NewServer(srv1.Handler())
srv1.SetExternalURL(hsrv1.URL)
certURL, certID, acctKey, kid := finalizeOneCert(t, hsrv1.URL, "example.test")
// Stop everything.
hsrv1.Close()
l1.Stop()
// Re-open storage and bring up a new server.
fs2, _ := storage.New(dir)
l2, err := cactuslog.New(context.Background(), cactuslog.Config{
LogID: cert.TrustAnchorID("32473.1"),
CosignerID: cert.TrustAnchorID("32473.1"),
Signer: logSigner,
FS: fs2,
FlushPeriod: 25 * time.Millisecond,
})
if err != nil {
t.Fatal(err)
}
defer l2.Stop()
issuer2, _ := ca.New(l2, "32473.1", 1)
srv2, _ := New(Config{Issuer: issuer2, ChallengeMode: ChallengeAutoPass})
if err := srv2.AttachStorage(fs2); err != nil {
t.Fatal(err)
}
hsrv2 := httptest.NewServer(srv2.Handler())
defer hsrv2.Close()
srv2.SetExternalURL(hsrv2.URL)
// Cert ID survived; rewrite the URL with the new server's prefix
// and verify the cert is still served. The kid (account URL) also
// shifts to the new server's prefix.
newCertURL := hsrv2.URL + strings.TrimPrefix(certURL, hsrv1.URL)
newKID := hsrv2.URL + strings.TrimPrefix(kid, hsrv1.URL)
resp, body := postAsGet(t, hsrv2.URL, newCertURL, acctKey, newKID, "")
if resp.StatusCode != 200 {
t.Fatalf("post-restart cert fetch status = %d body=%s", resp.StatusCode, body)
}
if !strings.Contains(string(body), "BEGIN CERTIFICATE") {
t.Errorf("not a PEM cert: %q", string(body))
}
_ = certID
}
// finalizeOneCert drives a full ACME flow against base and returns the
// certificate URL, its ID, plus the (acctKey, kid) needed to download
// the cert via POST-as-GET.
func finalizeOneCert(t *testing.T, base, dnsName string) (certURL, certID string, acctKey *ecdsa.PrivateKey, kid string) {
t.Helper()
acctKey, _ = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
acctJWK := &jose.JSONWebKey{Key: acctKey.Public(), Algorithm: "ES256"}
// new-account.
nonce := nonceFor(t, base)
jws := jwsSign(t, acctKey, acctJWK, "", nonce, base+"/new-account",
mustMarshal(NewAccountReq{TermsOfServiceAgreed: true}))
resp, _ := post(t, base, "/new-account", jws)
kid = resp.Header.Get("Location")
// new-order.
nonce = resp.Header.Get("Replay-Nonce")
jws = jwsSign(t, acctKey, nil, kid, nonce, base+"/new-order",
mustMarshal(NewOrderReq{Identifiers: []Identifier{{Type: "dns", Value: dnsName}}}))
resp, body := post(t, base, "/new-order", jws)
var ord OrderResp
if err := json.Unmarshal(body, &ord); err != nil {
t.Fatalf("unmarshal body: %v", err)
}
// CSR.
csrKey, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
csrTmpl := &x509.CertificateRequest{
Subject: pkix.Name{CommonName: dnsName},
DNSNames: []string{dnsName},
}
csrDER, _ := x509.CreateCertificateRequest(rand.Reader, csrTmpl, csrKey)
// finalize.
nonce = resp.Header.Get("Replay-Nonce")
jws = jwsSign(t, acctKey, nil, kid, nonce, ord.Finalize,
mustMarshal(FinalizeReq{CSR: base64.RawURLEncoding.EncodeToString(csrDER)}))
resp, body = post(t, base, strings.TrimPrefix(ord.Finalize, base), jws)
var ord2 OrderResp
if err := json.Unmarshal(body, &ord2); err != nil {
t.Fatalf("unmarshal body: %v", err)
}
return ord2.Certificate, lastSegment(ord2.Certificate), acctKey, kid
}
func mustMarshal(v interface{}) []byte {
b, err := json.Marshal(v)
if err != nil {
panic(err)
}
return b
}