forked from letsencrypt/boulder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctpolicy_test.go
245 lines (225 loc) · 6.53 KB
/
ctpolicy_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
package ctpolicy
import (
"context"
"errors"
"regexp"
"testing"
"time"
"github.com/letsencrypt/boulder/cmd"
"github.com/letsencrypt/boulder/core"
"github.com/letsencrypt/boulder/ctpolicy/ctconfig"
berrors "github.com/letsencrypt/boulder/errors"
blog "github.com/letsencrypt/boulder/log"
"github.com/letsencrypt/boulder/metrics"
pubpb "github.com/letsencrypt/boulder/publisher/proto"
"github.com/letsencrypt/boulder/test"
"github.com/prometheus/client_golang/prometheus"
"google.golang.org/grpc"
)
type mockPub struct {
}
func (mp *mockPub) SubmitToSingleCTWithResult(_ context.Context, _ *pubpb.Request, _ ...grpc.CallOption) (*pubpb.Result, error) {
return &pubpb.Result{Sct: []byte{0}}, nil
}
type alwaysFail struct {
mockPub
}
func (mp *alwaysFail) SubmitToSingleCTWithResult(_ context.Context, _ *pubpb.Request, _ ...grpc.CallOption) (*pubpb.Result, error) {
return nil, errors.New("BAD")
}
func TestGetSCTs(t *testing.T) {
expired, cancel := context.WithDeadline(context.Background(), time.Now())
defer cancel()
missingSCTErr := berrors.MissingSCTs
testCases := []struct {
name string
mock pubpb.PublisherClient
groups []ctconfig.CTGroup
ctx context.Context
result core.SCTDERs
errRegexp *regexp.Regexp
berrorType *berrors.ErrorType
}{
{
name: "basic success case",
mock: &mockPub{},
groups: []ctconfig.CTGroup{
{
Name: "a",
Logs: []ctconfig.LogDescription{
{URI: "abc", Key: "def"},
{URI: "ghi", Key: "jkl"},
},
},
{
Name: "b",
Logs: []ctconfig.LogDescription{
{URI: "abc", Key: "def"},
{URI: "ghi", Key: "jkl"},
},
},
},
ctx: context.Background(),
result: core.SCTDERs{[]byte{0}, []byte{0}},
},
{
name: "basic failure case",
mock: &alwaysFail{},
groups: []ctconfig.CTGroup{
{
Name: "a",
Logs: []ctconfig.LogDescription{
{URI: "abc", Key: "def"},
{URI: "ghi", Key: "jkl"},
},
},
{
Name: "b",
Logs: []ctconfig.LogDescription{
{URI: "abc", Key: "def"},
{URI: "ghi", Key: "jkl"},
},
},
},
ctx: context.Background(),
errRegexp: regexp.MustCompile("CT log group \".\": all submissions failed"),
berrorType: &missingSCTErr,
},
{
name: "parent context timeout failure case",
mock: &alwaysFail{},
groups: []ctconfig.CTGroup{
{
Name: "a",
Logs: []ctconfig.LogDescription{
{URI: "abc", Key: "def"},
{URI: "ghi", Key: "jkl"},
},
},
{
Name: "b",
Logs: []ctconfig.LogDescription{
{URI: "abc", Key: "def"},
{URI: "ghi", Key: "jkl"},
},
},
},
ctx: expired,
errRegexp: regexp.MustCompile("CT log group \".\": context deadline exceeded"),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ctp := New(tc.mock, tc.groups, nil, blog.NewMock(), metrics.NoopRegisterer)
ret, err := ctp.GetSCTs(tc.ctx, []byte{0}, time.Time{})
if tc.result != nil {
test.AssertDeepEquals(t, ret, tc.result)
} else if tc.errRegexp != nil {
if !tc.errRegexp.MatchString(err.Error()) {
t.Errorf("Error %q did not match expected regexp %q", err, tc.errRegexp)
}
if tc.berrorType != nil {
test.AssertErrorIs(t, err, *tc.berrorType)
}
}
})
}
}
type failOne struct {
mockPub
badURL string
}
func (mp *failOne) SubmitToSingleCTWithResult(_ context.Context, req *pubpb.Request, _ ...grpc.CallOption) (*pubpb.Result, error) {
if req.LogURL == mp.badURL {
return nil, errors.New("BAD")
}
return &pubpb.Result{Sct: []byte{0}}, nil
}
type slowPublisher struct {
mockPub
}
func (sp *slowPublisher) SubmitToSingleCTWithResult(_ context.Context, req *pubpb.Request, _ ...grpc.CallOption) (*pubpb.Result, error) {
time.Sleep(time.Second)
return &pubpb.Result{Sct: []byte{0}}, nil
}
func TestGetSCTsMetrics(t *testing.T) {
ctp := New(&failOne{badURL: "abc"}, []ctconfig.CTGroup{
{
Name: "a",
Logs: []ctconfig.LogDescription{
{URI: "abc", Key: "def"},
{URI: "ghi", Key: "jkl"},
},
},
{
Name: "b",
Logs: []ctconfig.LogDescription{
{URI: "abc", Key: "def"},
{URI: "ghi", Key: "jkl"},
},
},
}, nil, blog.NewMock(), metrics.NoopRegisterer)
_, err := ctp.GetSCTs(context.Background(), []byte{0}, time.Time{})
test.AssertNotError(t, err, "GetSCTs failed")
test.AssertMetricWithLabelsEquals(t, ctp.winnerCounter, prometheus.Labels{"log": "ghi", "group": "a"}, 1)
test.AssertMetricWithLabelsEquals(t, ctp.winnerCounter, prometheus.Labels{"log": "ghi", "group": "b"}, 1)
}
func TestGetSCTsFailMetrics(t *testing.T) {
// When an entire log group fails, we should increment the "winner of SCT
// race" stat for that group under the fictional log "all_failed".
ctp := New(&failOne{badURL: "abc"}, []ctconfig.CTGroup{
{
Name: "a",
Logs: []ctconfig.LogDescription{
{URI: "abc", Key: "def"},
},
},
}, nil, blog.NewMock(), metrics.NoopRegisterer)
_, err := ctp.GetSCTs(context.Background(), []byte{0}, time.Time{})
if err == nil {
t.Fatal("GetSCTs should have failed")
}
test.AssertMetricWithLabelsEquals(t, ctp.winnerCounter, prometheus.Labels{"log": "all_failed", "group": "a"}, 1)
// Same thing, but for when an entire log group times out.
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
ctp = New(&slowPublisher{}, []ctconfig.CTGroup{
{
Name: "a",
Logs: []ctconfig.LogDescription{
{URI: "abc", Key: "def"},
},
},
}, nil, blog.NewMock(), metrics.NoopRegisterer)
_, err = ctp.GetSCTs(ctx, []byte{0}, time.Time{})
if err == nil {
t.Fatal("GetSCTs should have failed")
}
test.AssertMetricWithLabelsEquals(t, ctp.winnerCounter, prometheus.Labels{"log": "timeout", "group": "a"}, 1)
}
// A mock publisher that counts submissions
type countEm struct {
count int
}
func (ce *countEm) SubmitToSingleCTWithResult(_ context.Context, _ *pubpb.Request, _ ...grpc.CallOption) (*pubpb.Result, error) {
ce.count++
return &pubpb.Result{Sct: []byte{0}}, nil
}
func TestStagger(t *testing.T) {
countingPub := &countEm{}
ctp := New(countingPub, []ctconfig.CTGroup{
{
Name: "a",
Stagger: cmd.ConfigDuration{Duration: 500 * time.Millisecond},
Logs: []ctconfig.LogDescription{
{URI: "abc", Key: "def"},
{URI: "ghi", Key: "jkl"},
},
},
}, nil, blog.NewMock(), metrics.NoopRegisterer)
_, err := ctp.GetSCTs(context.Background(), []byte{0}, time.Time{})
test.AssertNotError(t, err, "GetSCTs failed")
if countingPub.count != 1 {
t.Errorf("wrong number of requests to publisher. got %d, expected 1", countingPub.count)
}
}