-
Notifications
You must be signed in to change notification settings - Fork 2
/
cache_batch.go
379 lines (325 loc) · 10.5 KB
/
cache_batch.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
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
package dyocsp
import (
"context"
"errors"
"fmt"
"math/big"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/yuxki/dyocsp/pkg/cache"
"github.com/yuxki/dyocsp/pkg/date"
"github.com/yuxki/dyocsp/pkg/db"
)
type expirationLogger struct {
Logger zerolog.Logger
}
func (e *expirationLogger) InvalidMsg(serial string, msg string) {
e.Logger.Warn().Msg(fmt.Sprintf("%s: %s", msg, serial))
}
func (e *expirationLogger) WarnMsg(serial *big.Int, msg string) {
e.Logger.Warn().Msg(fmt.Sprintf("%s: %s", msg, serial.Text(db.SerialBase)))
}
// CADBClient is an interface that represents a client for scanning a database
// and creating IntermediateEntries.
type CADBClient interface {
Scan(ctx context.Context) ([]db.IntermidiateEntry, error)
}
func createExpirationLogger(expiration expBehavior, logger zerolog.Logger) *db.ExpirationControl {
expLogger := expirationLogger{Logger: logger}
var expCtl *db.ExpirationControl
switch expiration {
case Warn:
expCtl = db.NewExpirationControl(db.WithWarnOnExpiration(), db.WithLogger(&expLogger))
case Invalid:
expCtl = db.NewExpirationControl()
}
return expCtl
}
// The CacheBatch function scans the CA database for certificates with revocation
// information and generates response caches. It then updates the dyocsp.ResponseCacheStore
// with these caches. The job is repeated infinitely with an interval between each job. The
// interval refers to the interval of the OCSP Response Next Update. This Loop delays processing
// until the specified interval is reached, taking into account the duration of the batch job.
type CacheBatch struct {
ca string
cacheStore *cache.ResponseCacheStore
caDBClient CADBClient
responder *Responder
now date.Now
nextUpdate time.Time
batchSerial int
interval time.Duration
// Options
intervalSec int
delay time.Duration
strict bool
expiration expBehavior
quite chan string
updatedNotify chan struct{}
logger *zerolog.Logger
}
// Default values.
const (
DefaultInterval = 60
)
type expBehavior int
const (
Ignore expBehavior = iota
Warn
Invalid
)
var ErrDelayExceedsInterval = errors.New("delay must be less than interval or equal")
// CacheBatchOption is type of an functional option for dyocsp.CacheBatch.
type CacheBatchOption func(*CacheBatch)
// WithIntervalSec sets interval seconds option. Interval is the duration specification
// between the Next Update and the Next Update. If 0 or less than 0 is set, DefaultInterval
// is used.
func WithIntervalSec(sec int) func(*CacheBatch) {
return func(c *CacheBatch) {
c.intervalSec = sec
}
}
// WithDelay sets delay option. Delay is a duration specification that pauses
// the execution of the program for a specified CacheBatchSpec.Interval before
// continuing to process further. Default value is 0 and. If value is less than 0,
// default value is used.
func WithDelay(delay time.Duration) func(*CacheBatch) {
return func(c *CacheBatch) {
c.delay = delay
}
}
// WithDelay sets strict option.// The strict specification of dyocsp.CacheBatch
// means that it is in 'strict mode',which calls panic() when a CADBClient error
// occurs during the scanning of the database. Default value is false.
func WithStrict(strict bool) func(*CacheBatch) {
return func(c *CacheBatch) {
c.strict = strict
}
}
// WithExpiration sets expiration. This expiration determines the behavior when the
// Expiration Date is exceeded. Default value is Ignore.
func WithExpiration(exp expBehavior) func(*CacheBatch) {
return func(c *CacheBatch) {
c.expiration = exp
}
}
// WithLogger sets logger. If not set, global logger is used.
func WithLogger(logger *zerolog.Logger) func(*CacheBatch) {
return func(c *CacheBatch) {
c.logger = logger
}
}
// WithQuietChan sets a quiet message channel, which
// stops the loop of dyocsp.CacheBatch.Run(). It also sends a message immediately
// before quieting the loop.
func WithQuiteChan(ch chan string) func(*CacheBatch) {
return func(c *CacheBatch) {
c.quite = ch
}
}
// WithUpdatedNotifyChan sets a channel to notify when the cache store is updated.
// To ensure that the batch waits until a notify is received, the user should
// create a receiver.
func WithUpdatedNotifyChan(ch chan struct{}) func(*CacheBatch) {
return func(c *CacheBatch) {
c.updatedNotify = ch
}
}
// NewCacheBatch creates a new instance of dyocsp.CacheBatch and returns it.
func NewCacheBatch(
ca string,
cacheStore *cache.ResponseCacheStore,
caDBClient CADBClient,
responder *Responder,
nextUpdate time.Time,
opts ...CacheBatchOption,
) (*CacheBatch, error) {
batch := &CacheBatch{
ca: ca,
cacheStore: cacheStore,
caDBClient: caDBClient,
responder: responder,
now: date.NowGMT,
nextUpdate: nextUpdate,
batchSerial: 0,
}
for _, opt := range opts {
opt(batch)
}
if batch.intervalSec <= 0 {
batch.interval = time.Second * DefaultInterval
} else {
batch.interval = time.Second * time.Duration(batch.intervalSec)
}
if batch.delay < 0 {
batch.delay = 0
}
if batch.delay > batch.interval {
return nil, ErrDelayExceedsInterval
}
if batch.logger == nil {
batch.logger = &log.Logger
}
return batch, nil
}
func (c *CacheBatch) logEntryErrors(ce db.CertificateEntry, logger *zerolog.Logger) (noError bool) {
noError = true
for i := db.MalformSerial; i <= db.UndefinedCRLReason; i++ {
err, ok := ce.Errors[i]
if !ok {
continue
}
logger.Error().Err(err).Msg("")
noError = false
}
return noError
}
// RunOnce returns a slice of cache.ResponseCache through the following process.
// - Scan the CA database to identify entries related to certificate revocation.
// - Verify and parse entries for pre-signed response caches.
// - Sign the pre-signed response caches using the dyocsp.Responder.
//
// This function is the main job of dyocsp.CacheBatch.Run().
func (c *CacheBatch) RunOnce(ctx context.Context) []cache.ResponseCache {
logger := zerolog.Ctx(ctx)
// DB --> IntermidiateEntry
var itmds []db.IntermidiateEntry
logger.Info().Msg("Database scan started by client.")
itmds, err := c.caDBClient.Scan(ctx)
if err != nil {
logger.Error().Err(err).Msg("")
if c.strict {
panic(err)
}
}
logger.Info().Msg("Database scan completed.")
logger.Debug().Msgf("List of scanned entries from the database: %v", itmds)
// IntermidiateEntry --> CertificateEntry
expCtl := createExpirationLogger(c.expiration, *logger)
exch := db.NewEntryExchange()
entries := make([]db.CertificateEntry, 0, len(itmds))
for idx := range itmds {
// When certificate is expired, response cache is not created.
if itmds[idx].RevType == "E" {
continue
}
ce := exch.ParseCertificateEntry(itmds[idx])
if noerr := c.logEntryErrors(ce, logger); noerr {
entries = append(entries, ce)
}
if expCtl != nil {
// When certificate after date is past, response cache is not created.
entries = expCtl.Do(c.now(), entries)
}
}
logger.Debug().Msgf("List of exchange entries from scanned entries: %v", entries)
// CertificateEntry --> cache.ResponseCache(Pre-Signed)
resCaches := make([]cache.ResponseCache, 0, len(entries))
for idx := range entries {
resCache, err := cache.CreatePreSignedResponseCache(entries[idx], c.nextUpdate, c.interval)
if err != nil {
logger.Error().Err(err).Msg("")
}
if c.responder.AuthType == Delegation {
resCache.SetCertToTemplate(c.responder.rCert)
}
resCaches = append(resCaches, resCache)
}
logger.Debug().Msgf("Number of pre-signed-caches: %d", len(resCaches))
// cache.ResponseCache(Pre-Signed) --> cache.ResponseCache(Signed)
signedCaches := make([]cache.ResponseCache, 0, len(resCaches))
for idx := range resCaches {
signedCache, err := c.responder.SignCacheResponse(resCaches[idx])
if err != nil {
logger.Error().Msg(fmt.Sprintf("Failed to sign :%v", resCaches[idx]))
} else {
signedCaches = append(signedCaches, signedCache)
}
}
logger.Debug().Msgf("Number of signed-caches: %d", len(signedCaches))
return signedCaches
}
func (c *CacheBatch) syncWithWaitDuration(now time.Time) time.Duration {
waitDur := c.interval
switch r := now.Compare(c.nextUpdate); r {
case -1:
waitDur = (waitDur + c.nextUpdate.Sub(now)) - c.delay
case 1:
waitDur = (waitDur - now.Sub(c.nextUpdate)) - c.delay
if waitDur < 0 {
waitDur = 0
}
default:
waitDur -= c.delay
}
return waitDur
}
func (c *CacheBatch) logBatchSummary(ctx context.Context, start time.Time) {
logger := zerolog.Ctx(ctx)
dur := fmt.Sprintf("%v", time.Since(start))
logger.Info().
Str("duration", dur).
Msg("Cache generation batch completed.")
}
func (c *CacheBatch) waitForNextUpdate(ctx context.Context, waitDur time.Duration) {
logger := zerolog.Ctx(ctx)
logger.Info().Dur("wait", waitDur).
Time("next-update", c.nextUpdate).
Msg("Waiting for the next update.")
if c.quite != nil {
outer:
for {
select {
case <-time.After(waitDur):
break outer
case msg := <-c.quite:
// Stop when it received quite message
logger.Info().Msgf("Quite message received, stop loop: %s", msg)
c.quite <- "Loop stopped."
return
}
}
} else {
time.Sleep(waitDur)
}
}
// Run starts a loop that processes the batch and caches signed response
// caches in the interval specification.
// The batch execute following jobs in order.
// - Scan the CA database with db.CADBClient.Scan().
// - Verify revocation information entries and create, sign OCSP response.
// - Verify the revocation information entries.
// - Create and sign an OCSP response.
// - Compute the wait time needed to adjust for any out-of-sync between the
// actual time and the next update time. This can occur due to delays in processing
// or the duration of batch processing.
// - Update Next Update.
// - Wait for next update.
func (c *CacheBatch) Run(ctx context.Context) {
for {
startTime := c.now()
logger := c.logger.With().Int("batch_serial", c.batchSerial).Logger()
logger.Info().Msg("Starting cache generation batch.")
ctx := logger.WithContext(ctx)
// Create response caches
caches := c.RunOnce(ctx)
// Update cache store
invs := c.cacheStore.Update(caches)
for i := range invs {
logger.Error().Msgf("Invalid response cache: %s", invs[i].Entry().Serial)
}
logger.Info().Msg("Response cache updated.")
if c.updatedNotify != nil {
c.updatedNotify <- struct{}{}
}
// Summury of this loop batch
c.logBatchSummary(ctx, startTime)
waitDur := c.syncWithWaitDuration(c.now())
// Update nextUpdate
c.nextUpdate = c.nextUpdate.Add(c.interval)
// Wait for next update
c.waitForNextUpdate(ctx, waitDur)
c.batchSerial++
}
}