/
updater.go
434 lines (404 loc) · 11.3 KB
/
updater.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
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
package updater
import (
"archive/zip"
"context"
"errors"
"fmt"
"io"
"io/fs"
"net/http"
"os"
"runtime"
"runtime/pprof"
"sort"
"strings"
"sync"
"github.com/google/uuid"
"github.com/quay/zlog"
"golang.org/x/sync/errgroup"
driver "github.com/quay/claircore/updater/driver/v1"
)
// Updater coordinates running Updaters and saving the results.
//
// Close must be called, or the program may panic.
type Updater struct {
store Store
locker Locker
client *http.Client
configs driver.Configs
factories []driver.UpdaterFactory
}
// New returns an Updater ready to use.
//
// None of the resources passed in the Options struct have any of their cleanup
// methods called, and need to be safe for use by multiple goroutines.
func New(ctx context.Context, opts *Options) (*Updater, error) {
if opts.Store == nil {
return nil, errors.New("updater: no Store implementation provided")
}
if opts.Client == nil {
return nil, errors.New("updater: no http.Client provided")
}
u := &Updater{
store: opts.Store,
locker: opts.Locker,
client: opts.Client,
configs: opts.Configs,
factories: opts.Factories,
}
if opts.Locker == nil {
zlog.Warn(ctx).Msg("no locker passed, using process-local locking")
u.locker = newLocalLocker()
}
if opts.Configs == nil {
zlog.Info(ctx).Msg("no updater configuration passed")
u.configs = make(driver.Configs)
}
if opts.Factories == nil {
zlog.Warn(ctx).Msg("no updater factories provided, this may be a misconfiguration")
}
_, file, line, _ := runtime.Caller(1)
runtime.SetFinalizer(u, func(u *Updater) {
panic(fmt.Sprintf("%s:%d: Updater not closed", file, line))
})
return u, nil
}
// Close releases any resources held by the Updater.
func (u *Updater) Close() error {
runtime.SetFinalizer(u, nil)
u.store = nil
u.locker = nil
u.client = nil
u.configs = nil
u.factories = nil
return nil
}
// Options contains the needed options for an Updater.
//
// The Store and Client members are required. The others are optional, but
// should only be omitted in specific circumstances.
type Options struct {
// This should disallow an unkeyed literal and means that additions to the
// struct shouldn't cause compilation errors.
_forceKeys struct{}
// Store is the interface used to persist parsed data.
Store Store
// Client is the http.Client all the Updaters will use.
Client *http.Client
// Locker provides system-wide locks. If multiple Updater processes are
// running, this should be backed by a distributed lock manager.
Locker Locker
// Configs holds configuration functions for Updaters.
Configs driver.Configs
// Factories is a slice of UpdaterFactories that are used to construct
// Updaters on demand.
Factories []driver.UpdaterFactory
}
// All the internal machinery deals with this taggedUpdater type, so that we
// only have to call the Name method once.
//
// This is to avoid having lots of labeled calls, as *all* calls to updaters
// should be labeled to help in debugging stray goroutines.
type taggedUpdater struct {
Name string
Updater driver.Updater
}
// Run constructs new updaters, runs them, and stores the results.
//
// Errors reported from the Updater itself will return the error immediately,
// but errors reported from updaters are collected and reported once all
// updaters have run.
//
// Run should be preferred to explicit Fetch and Parse calls, because knowing
// that both methods will be running in the same process allows for better
// resource usage.
func (u *Updater) Run(ctx context.Context, strict bool) error {
var (
us []taggedUpdater
ops []driver.UpdateOperation
)
sg, sctx := errgroup.WithContext(ctx)
sg.Go(func() (err error) {
us, err = u.updaters(sctx, u.configs)
return err
})
sg.Go(func() (err error) {
ops, err = u.store.GetLatestUpdateOperations(sctx)
return err
})
if err := sg.Wait(); err != nil {
return err
}
pfps := make(map[string]driver.Fingerprint, len(ops))
for _, op := range ops {
pfps[op.Updater] = op.Fingerprint
}
var wg sync.WaitGroup
lim := runtime.GOMAXPROCS(0)
wg.Add(lim)
feed, errCh := make(chan taggedUpdater), make(chan error)
var errs []error
eg, ctx := errgroup.WithContext(ctx)
eg.Go(feeder(ctx, feed, us))
eg.Go(func() error {
wg.Wait()
close(errCh)
return nil
})
eg.Go(func() error {
for err := range errCh {
errs = append(errs, err)
}
return nil
})
for i := 0; i < lim; i++ {
eg.Go(func() error {
defer wg.Done()
spool, err := os.CreateTemp(tmpDir, tmpPattern)
if err != nil {
zlog.Warn(ctx).Err(err).Msg("unable to create spool file")
return err
}
spoolname := spool.Name()
defer func() {
if err := os.Remove(spoolname); err != nil {
zlog.Warn(ctx).Str("filename", spoolname).Err(err).Msg("unable to remove spool file")
}
if err := spool.Close(); err != nil {
zlog.Warn(ctx).Str("filename", spoolname).Err(err).Msg("error closing spool file")
}
}()
var updErr *updaterError
for upd := range feed {
err := u.fetchAndParse(ctx, spool, pfps, upd)
switch {
case errors.Is(err, nil):
case errors.As(err, &updErr):
zlog.Debug(ctx).Err(updErr).Msg("updater error")
errCh <- updErr.Unwrap()
default:
return err
}
}
return nil
})
}
if err := eg.Wait(); err != nil {
return err
}
// Print or return errors.
if len(errs) != 0 {
if strict {
return errors.Join(errs...)
}
zlog.Info(ctx).Errs("errors", errs).Msg("updater errors")
}
return nil
}
// In all cases, calls into Updaters should be done with the goroutine labels
// set. This allows an execution trace to help narrow down any orphaned
// goroutines.
func (u *Updater) updaters(ctx context.Context, cfg driver.Configs) ([]taggedUpdater, error) {
var r []taggedUpdater
dedup := make(map[string]struct{})
for _, fac := range u.factories {
var key string
pprof.Do(ctx, pprof.Labels("task", "factory_name"), func(_ context.Context) {
key = fac.Name()
})
var set []driver.Updater
var err error
pprof.Do(ctx, pprof.Labels("task", "factory_create", "factory", key), func(ctx context.Context) {
set, err = fac.Create(ctx, cfg[key])
})
if err != nil {
zlog.Info(ctx).Err(err).Msg("factory errored")
continue
}
for _, upd := range set {
var name string
pprof.Do(ctx, pprof.Labels("task", "updater_name"), func(_ context.Context) {
name = upd.Name()
})
if strings.Contains(name, "/") {
zlog.Info(ctx).Str("updater", name).Msg("name contains invalid character: /")
continue
}
if _, ok := dedup[name]; ok {
zlog.Info(ctx).Str("updater", name).Msg("updater already exists")
continue
}
dedup[name] = struct{}{}
r = append(r, taggedUpdater{
Name: name,
Updater: upd,
})
}
}
sort.Slice(r, func(i, j int) bool { return r[i].Name < r[j].Name })
return r, nil
}
func (u *Updater) fetchOne(ctx context.Context, tu taggedUpdater, pfp driver.Fingerprint, out io.Writer) (fp driver.Fingerprint, err error) {
name := tu.Name
ctx = zlog.ContextWithValues(ctx, "updater", name)
zlog.Info(ctx).Msg("fetch start")
defer zlog.Info(ctx).Msg("fetch done")
lctx, done := u.locker.TryLock(ctx, name)
defer done()
if err := lctx.Err(); err != nil {
if pErr := ctx.Err(); pErr != nil {
zlog.Debug(ctx).Err(err).Msg("parent context canceled")
return fp, nil
}
zlog.Info(ctx).Err(err).Msg("lock acquisition failed, skipping")
return fp, err
}
ctx = lctx
zw := zip.NewWriter(out)
defer func() {
if err := zw.Close(); err != nil {
zlog.Warn(ctx).Err(err).Msg("unable to close zip writer")
}
}()
if len(pfp) != 0 {
zlog.Debug(ctx).Str("fingerprint", string(pfp)).Msg("found previous fingerprint")
}
pprof.Do(ctx, pprof.Labels("task", "updater_fetch", "updater", name), func(ctx context.Context) {
fp, err = tu.Updater.Fetch(ctx, zw, pfp, u.client)
})
return fp, err
}
func (u *Updater) parseOne(ctx context.Context, tu taggedUpdater, in fs.FS) (*parseResult, error) {
var (
any bool
res parseResult
err error
)
name := tu.Name
ctx = zlog.ContextWithValues(ctx, "updater", name)
zlog.Info(ctx).Msg("parse start")
defer zlog.Info(ctx).Msg("parse done")
pprof.Do(ctx, pprof.Labels("task", "updater_parse", "updater", name), func(ctx context.Context) {
ctx = zlog.ContextWithValues(ctx, "updater", name)
upd := tu.Updater
if p, ok := upd.(driver.VulnerabilityParser); ok {
zlog.Debug(ctx).Msg("implements VulnerabilityParser")
any = true
res.Vulnerabilities, err = p.ParseVulnerability(ctx, in)
if err != nil {
return
}
zlog.Debug(ctx).
Err(err).
Int("ct", len(res.Vulnerabilities.Vulnerability)).
Msg("found vulnerabilities")
}
if p, ok := upd.(driver.EnrichmentParser); ok {
zlog.Debug(ctx).Msg("implements EnrichmentParser")
any = true
res.Enrichments, err = p.ParseEnrichment(ctx, in)
if err != nil {
return
}
zlog.Debug(ctx).
Err(err).
Int("ct", len(res.Enrichments)).
Msg("found enrichments")
}
})
if !any {
return nil, errors.New("did nothing")
}
return &res, nil
}
type parseResult struct {
Vulnerabilities *driver.ParsedVulnerabilities
Enrichments []driver.EnrichmentRecord
}
func (u *Updater) fetchAndParse(ctx context.Context, spool *os.File, pfps map[string]driver.Fingerprint, tu taggedUpdater) error {
spoolname := spool.Name()
name := tu.Name
ctx = zlog.ContextWithValues(ctx, "updater", name)
if _, err := spool.Seek(0, io.SeekStart); err != nil {
zlog.Error(ctx).Str("filename", spoolname).Err(err).Msg("unable to seek to start")
return err
}
fp, err := u.fetchOne(ctx, tu, pfps[name], spool)
switch {
case errors.Is(err, nil):
case errors.Is(err, driver.ErrUnchanged):
zlog.Debug(ctx).Msg("unchanged")
return nil
default:
return updaterErr(err)
}
sz, err := spool.Seek(0, io.SeekCurrent)
if err != nil {
zlog.Error(ctx).Str("filename", spoolname).Err(err).Msg("unable to seek spoolfile")
return err
}
z, err := zip.NewReader(spool, sz)
if err != nil {
zlog.Error(ctx).Str("filename", spoolname).Err(err).Msg("unable to create zip reader")
return err
}
res, err := u.parseOne(ctx, tu, z)
if err != nil {
return updaterErr(err)
}
ref := uuid.New()
pprof.Do(ctx, pprof.Labels("updater", name), func(ctx context.Context) {
if len(res.Vulnerabilities.Vulnerability) != 0 {
err = u.store.UpdateVulnerabilities(ctx, ref, name, fp, res.Vulnerabilities)
if err != nil {
return
}
zlog.Info(ctx).Stringer("ref", ref).Msg("updated vulnerabilites")
}
if len(res.Enrichments) != 0 {
err = u.store.UpdateEnrichments(ctx, ref, name, fp, res.Enrichments)
if err != nil {
return
}
zlog.Info(ctx).Stringer("ref", ref).Msg("updated enrichments")
}
})
if err != nil {
return err
}
return nil
}
// UpdaterErr returns an *updaterError wrapping "e".
//
// This is used to signal when an error came from an updater.
func updaterErr(e error) error {
return &updaterError{orig: e}
}
type updaterError struct {
orig error
}
func (u *updaterError) Error() string {
return u.orig.Error()
}
func (u *updaterError) Unwrap() error {
return u.orig
}
// Feeder sends "us" down "ch" and closes it when done, while respecting the
// Context's timeout.
func feeder(ctx context.Context, ch chan<- taggedUpdater, us []taggedUpdater) func() error {
return func() error {
defer close(ch)
for _, u := range us {
select {
case <-ctx.Done():
return ctx.Err()
case ch <- u:
}
}
return nil
}
}
const (
tmpDir = ``
tmpPattern = `updater.spool.*`
)