-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathari_test.go
258 lines (235 loc) · 6.94 KB
/
ari_test.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
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
package acme
import (
"crypto/x509"
"encoding/pem"
"testing"
"time"
)
func TestClient_GetRenewalInfo(t *testing.T) {
if testClient.dir.RenewalInfo == "" {
t.Skip("acme server does not support ari renewals")
return
}
account, order, _ := makeOrderFinalised(t, nil)
if order.Certificate == "" {
t.Fatalf("no certificate: %+v", order)
}
certs, err := testClient.FetchCertificates(account, order.Certificate)
t.Logf("Issued serial %s\n", certs[0].SerialNumber.String())
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
if len(certs) < 2 {
t.Fatalf("no certs")
}
renewalInfo, err := testClient.GetRenewalInfo(certs[0])
t.Logf("Suggested renewal window for new issuance: %v\n", renewalInfo.SuggestedWindow)
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
if renewalInfo.RetryAfter.IsZero() {
t.Fatalf("no retry after provided")
}
if renewalInfo.SuggestedWindow.Start.Before(time.Now()) {
t.Fatalf("suggested window start is in the past?")
}
if renewalInfo.SuggestedWindow.End.Before(time.Now()) {
t.Fatalf("suggested window start is in the past?")
}
if renewalInfo.SuggestedWindow.End.Before(renewalInfo.SuggestedWindow.Start) {
t.Fatalf("suggested window end is before start?")
}
err = testClient.RevokeCertificate(account, certs[0], account.PrivateKey, ReasonUnspecified)
if err != nil {
t.Fatalf("failed to revoke certificate: %v", err)
}
// The renewal window should adjust to allow immediate renewal
renewalInfo, err = testClient.GetRenewalInfo(certs[0])
t.Logf("Suggested renewal window for revoked certificate: %v\n", renewalInfo.SuggestedWindow)
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
if !renewalInfo.SuggestedWindow.Start.Before(time.Now()) {
t.Fatalf("suggested window start is in the past?")
}
if !renewalInfo.SuggestedWindow.End.Before(time.Now()) {
t.Fatalf("suggested window start is in the past?")
}
if renewalInfo.SuggestedWindow.End.Before(renewalInfo.SuggestedWindow.Start) {
t.Fatalf("suggested window end is before start?")
}
}
func TestClient_IssueReplacementCert(t *testing.T) {
if testClient.dir.RenewalInfo == "" {
t.Skip("acme server does not support ari renewals")
return
}
t.Log("Issuing initial order")
account, order, _ := makeOrderFinalised(t, nil)
if order.Certificate == "" {
t.Fatalf("no certificate: %+v", order)
}
// Replacing the original order should work
t.Log("Issuing first replacement order")
replacementOrder, err := makeReplacementOrderFinalized(t, order, account, nil, false)
if err != nil {
t.Fatal(err)
}
// Replacing the replacement should work
t.Log("Issuing second replacement order")
_, err = makeReplacementOrderFinalized(t, replacementOrder, account, nil, false)
if err != nil {
t.Fatal(err)
}
// Attempting to replace a previously replacement order should fail.
t.Log("Should not be able to create a duplicate replacement")
_, err = makeReplacementOrderFinalized(t, replacementOrder, account, nil, false)
if err == nil {
t.Fatal(err)
}
}
func TestClient_FailedReplacementOrderAllowsAnotherReplacement(t *testing.T) {
if testClient.dir.RenewalInfo == "" {
t.Skip("acme server does not support ari renewals")
return
}
t.Log("Issuing initial order")
account, order, _ := makeOrderFinalised(t, nil)
if order.Certificate == "" {
t.Fatalf("no certificate: %+v", order)
}
// Explicitly deactivate the previous authorization so the VA has to
// re-validate the order and encounter a failure. Upon receiving a
// validation failure, Pebble marks an order as invalid which is what we
// need.
auth, err := testClient.DeactivateAuthorization(account, order.Authorizations[0])
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
if auth.Status != "deactivated" {
t.Fatalf("expected deactivated status, got: %s", auth.Status)
}
t.Log("Issuing replacement order which will intentionally fail")
_, err = makeReplacementOrderFinalized(t, order, account, nil, true)
if err == nil {
t.Fatal(err)
}
// Attempting to replace a previously failed replacement order should pass
t.Log("Issuing replacement order for a parent order who previously had a failed replacement order")
_, err = makeReplacementOrderFinalized(t, order, account, nil, false)
if err != nil {
t.Fatal(err)
}
}
func Test_generateCertID(t *testing.T) {
type args struct {
cert *x509.Certificate
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
name: "ari example",
args: args{
// certificate taken from draft-ietf-acme-ari-03 appendix A.1. Example Certificate
cert: pem2cert(t, `-----BEGIN CERTIFICATE-----
MIIBQzCB66ADAgECAgUAh2VDITAKBggqhkjOPQQDAjAVMRMwEQYDVQQDEwpFeGFt
cGxlIENBMCIYDzAwMDEwMTAxMDAwMDAwWhgPMDAwMTAxMDEwMDAwMDBaMBYxFDAS
BgNVBAMTC2V4YW1wbGUuY29tMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEeBZu
7cbpAYNXZLbbh8rNIzuOoqOOtmxA1v7cRm//AwyMwWxyHz4zfwmBhcSrf47NUAFf
qzLQ2PPQxdTXREYEnKMjMCEwHwYDVR0jBBgwFoAUaYhba4dGQEHhs3uEe6CuLN4B
yNQwCgYIKoZIzj0EAwIDRwAwRAIge09+S5TZAlw5tgtiVvuERV6cT4mfutXIlwTb
+FYN/8oCIClDsqBklhB9KAelFiYt9+6FDj3z4KGVelYM5MdsO3pK
-----END CERTIFICATE-----`),
},
want: `aYhba4dGQEHhs3uEe6CuLN4ByNQ.AIdlQyE`,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GenerateARICertID(tt.args.cert)
if (err != nil) != tt.wantErr {
t.Errorf("GenerateARICertID() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("GenerateARICertID() error\n got = %v\n want = %v", got, tt.want)
}
})
}
}
func pem2cert(t *testing.T, s string) *x509.Certificate {
block, _ := pem.Decode([]byte(s))
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
t.Fatalf("error parsing certificate: %v", err)
}
return cert
}
type zeroTimeNow struct{}
func (zeroTimeNow) Now() time.Time {
return time.Time{}
}
func Test_parseRetryAfter(t *testing.T) {
systemTime = zeroTimeNow{}
currentTime := time.Now().Round(time.Second)
currentTimeRFC1123 := currentTime.Format(time.RFC1123)
type args struct {
ra string
}
tests := []struct {
name string
args args
want time.Time
wantErr bool
}{
{
name: "simple",
args: args{
ra: "123",
},
want: time.Time{}.Add(123 * time.Second),
wantErr: false,
},
{
name: "date",
args: args{
ra: "Wed, 21 Oct 2015 07:28:00 GMT",
},
want: time.Date(2015, 10, 21, 7, 28, 0, 0, time.FixedZone("GMT", 0)),
wantErr: false,
},
{
name: "bad",
args: args{
ra: "hello, world",
},
want: time.Time{},
wantErr: true,
},
{
name: "dynamic",
args: args{
ra: currentTimeRFC1123,
},
want: currentTime,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseRetryAfter(tt.args.ra)
if (err != nil) != tt.wantErr {
t.Errorf("parseRetryAfter() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !got.Equal(tt.want) {
t.Errorf("parseRetryAfter() got = %v, want %v", got, tt.want)
}
})
}
}