-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathmanager.go
377 lines (335 loc) · 9.78 KB
/
manager.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
package updates
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"runtime"
"strings"
"time"
"github.com/google/uuid"
"github.com/quay/zlog"
"golang.org/x/sync/semaphore"
"github.com/quay/claircore"
"github.com/quay/claircore/datastore"
"github.com/quay/claircore/libvuln/driver"
"github.com/quay/claircore/updater"
)
const (
DefaultInterval = time.Duration(6 * time.Hour)
)
var DefaultBatchSize = runtime.GOMAXPROCS(0)
type Configs map[string]driver.ConfigUnmarshaler
// LockSource abstracts over how locks are implemented.
//
// An online system needs distributed locks, offline use cases can use
// process-local locks.
type LockSource interface {
TryLock(context.Context, string) (context.Context, context.CancelFunc)
Lock(context.Context, string) (context.Context, context.CancelFunc)
}
// Manager oversees the configuration and invocation of vulnstore updaters.
//
// The Manager may be used in a one-shot fashion, configured to run background
// jobs, or both.
type Manager struct {
// provides run-time updater construction.
factories map[string]driver.UpdaterSetFactory
// max in-flight updaters.
batchSize int
// update interval used once Manager.Start is invoked, otherwise
// this field is not used.
interval time.Duration
// configs provided to updaters once constructed.
configs Configs
// instructs manager to run gc and provides the number of
// update operations to keep.
updateRetention int
locks LockSource
client *http.Client
store datastore.Updater
}
// NewManager will return a manager ready to have its Start or Run methods called.
func NewManager(ctx context.Context, store datastore.Updater, locks LockSource, client *http.Client, opts ...ManagerOption) (*Manager, error) {
ctx = zlog.ContextWithValues(ctx, "component", "libvuln/updates/NewManager")
// the default Manager
m := &Manager{
store: store,
locks: locks,
factories: updater.Registered(),
batchSize: runtime.GOMAXPROCS(0),
interval: DefaultInterval,
client: client,
}
// these options can be ran order independent.
for _, opt := range opts {
opt(m)
}
if m.updateRetention == 1 {
return nil, errors.New("update retention cannot be 1")
}
err := updater.Configure(ctx, m.factories, m.configs, m.client)
if err != nil {
return nil, fmt.Errorf("failed to configure updater set factory: %w", err)
}
return m, nil
}
// Start will run updaters at the given interval.
//
// Start is designed to be ran as a goroutine. Cancel the provided Context
// to end the updater loop.
//
// Start must only be called once between context cancellations.
func (m *Manager) Start(ctx context.Context) error {
ctx = zlog.ContextWithValues(ctx, "component", "libvuln/updates/Manager.Start")
if m.interval == 0 {
return fmt.Errorf("manager must be configured with an interval to start")
}
// perform the initial run
zlog.Info(ctx).Msg("starting initial updates")
err := m.Run(ctx)
if err != nil {
zlog.Error(ctx).Err(err).Msg("errors encountered during updater run")
}
// perform run on every tick
zlog.Info(ctx).Str("interval", m.interval.String()).Msg("starting background updates")
t := time.NewTicker(m.interval)
defer t.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-t.C:
err := m.Run(ctx)
if err != nil {
zlog.Error(ctx).Err(err).Msg("errors encountered during updater run")
}
}
}
}
// Run constructs updaters from factories, configures them and runs them
// in batches.
//
// Run is safe to call at anytime, regardless of whether background updaters
// are running.
func (m *Manager) Run(ctx context.Context) error {
ctx = zlog.ContextWithValues(ctx, "component", "libvuln/updates/Manager.Run")
updaters := []driver.Updater{}
// Constructing updater sets may require network access
// depending on the factory.
// If construction fails, we will simply ignore those updater
// sets.
for _, factory := range m.factories {
updateTime := time.Now()
set, err := factory.UpdaterSet(ctx)
if err != nil {
zlog.Error(ctx).Err(err).Msg("failed constructing factory, excluding from run")
continue
}
if stubUpdaterInSet(set) {
updaterSetName, err := getFactoryNameFromStubUpdater(set)
if err != nil {
zlog.Error(ctx).
Err(err).
Msg("error getting updater set name")
}
err = m.store.RecordUpdaterSetStatus(ctx, updaterSetName, updateTime)
if err != nil {
zlog.Error(ctx).
Err(err).
Str("updaterSetName", updaterSetName).
Time("updateTime", updateTime).
Msg("error while recording update success for all updaters in updater set")
}
continue
}
updaters = append(updaters, set.Updaters()...)
}
// configure updaters
toRun := make([]driver.Updater, 0, len(updaters))
for _, u := range updaters {
if f, ok := u.(driver.Configurable); ok {
name := u.Name()
cfg := m.configs[name]
if cfg == nil {
cfg = noopConfig
}
if err := f.Configure(ctx, cfg, m.client); err != nil {
zlog.Warn(ctx).
Err(err).
Str("updater", name).
Msg("failed configuring updater, excluding from current run")
continue
}
}
toRun = append(toRun, u)
}
zlog.Info(ctx).
Int("total", len(toRun)).
Int("batchSize", m.batchSize).
Msg("running updaters")
sem := semaphore.NewWeighted(int64(m.batchSize))
errChan := make(chan error, len(toRun)+1) // +1 for a potential ctx error
for i := range toRun {
err := sem.Acquire(ctx, 1)
if err != nil {
zlog.Error(ctx).
Err(err).
Msg("sem acquire failed, ending updater run")
break
}
go func(u driver.Updater) {
defer sem.Release(1)
ctx, done := m.locks.TryLock(ctx, u.Name())
defer done()
if err := ctx.Err(); err != nil {
zlog.Debug(ctx).
Err(err).
Str("updater", u.Name()).
Msg("lock context canceled, excluding from run")
return
}
err = m.driveUpdater(ctx, u)
if err != nil {
errChan <- fmt.Errorf("%v: %w", u.Name(), err)
}
}(toRun[i])
}
// Unconditionally wait for all in-flight go routines to return.
// The use of context.Background and lack of error checking is intentional.
// All in-flight goroutines are guaranteed to release their semaphores.
sem.Acquire(context.Background(), int64(m.batchSize))
if m.updateRetention != 0 {
ctx, done := m.locks.TryLock(ctx, "garbage-collection")
if err := ctx.Err(); err != nil {
zlog.Debug(ctx).
Err(err).
Msg("lock context canceled, garbage collection already running")
} else {
zlog.Info(ctx).Int("retention", m.updateRetention).Msg("GC started")
i, err := m.store.GC(ctx, m.updateRetention)
if err != nil {
zlog.Error(ctx).Err(err).Msg("error while performing GC")
} else {
zlog.Info(ctx).
Int64("remaining_ops", i).
Int("retention", m.updateRetention).
Msg("GC completed")
}
}
done()
}
close(errChan)
if len(errChan) != 0 {
var b strings.Builder
b.WriteString("updating errors:\n")
for err := range errChan {
fmt.Fprintf(&b, "%v\n", err)
}
return errors.New(b.String())
}
return nil
}
// stubUpdaterInSet works out if an updater set contains a stub updater,
// signifying all updaters are up to date for this factory
func stubUpdaterInSet(set driver.UpdaterSet) bool {
if len(set.Updaters()) == 1 {
if set.Updaters()[0].Name() == "rhel-all" {
return true
}
}
return false
}
// getFactoryNameFromStubUpdater retrieves the factory name from an updater set with a stub updater
func getFactoryNameFromStubUpdater(set driver.UpdaterSet) (string, error) {
if set.Updaters()[0].Name() == "rhel-all" {
return "RHEL", nil
}
return "", errors.New("unrecognized stub updater name")
}
// DriveUpdater performs the business logic of fetching, parsing, and loading
// vulnerabilities discovered by an updater into the database.
func (m *Manager) driveUpdater(ctx context.Context, u driver.Updater) (err error) {
var newFP driver.Fingerprint
updateTime := time.Now()
defer func() {
deferErr := m.store.RecordUpdaterStatus(ctx, u.Name(), updateTime, newFP, err)
if deferErr != nil {
zlog.Error(ctx).
Err(deferErr).
Str("updater", u.Name()).
Time("updateTime", updateTime).
Msg("error while recording updater status")
}
}()
name := u.Name()
ctx = zlog.ContextWithValues(ctx,
"component", "libvuln/updates/Manager.driveUpdater",
"updater", name)
zlog.Info(ctx).Msg("starting update")
defer zlog.Info(ctx).Msg("finished update")
uoKind := driver.VulnerabilityKind
eu, euOK := u.(driver.EnrichmentUpdater)
if euOK {
zlog.Info(ctx).
Msg("found EnrichmentUpdater")
uoKind = driver.EnrichmentKind
}
var prevFP driver.Fingerprint
opmap, err := m.store.GetUpdateOperations(ctx, uoKind, name)
if err != nil {
return
}
if s := opmap[name]; len(s) > 0 {
prevFP = s[0].Fingerprint
}
var vulnDB io.ReadCloser
switch {
case euOK:
vulnDB, newFP, err = eu.FetchEnrichment(ctx, prevFP)
default:
vulnDB, newFP, err = u.Fetch(ctx, prevFP)
}
if vulnDB != nil {
defer vulnDB.Close()
}
switch {
case err == nil:
case errors.Is(err, driver.Unchanged):
zlog.Info(ctx).Msg("vulnerability database unchanged")
err = nil
return
default:
return
}
var ref uuid.UUID
switch {
case euOK:
var ers []driver.EnrichmentRecord
ers, err = eu.ParseEnrichment(ctx, vulnDB)
if err != nil {
err = fmt.Errorf("enrichment database parse failed: %v", err)
return
}
ref, err = m.store.UpdateEnrichments(ctx, name, newFP, ers)
default:
var vulns []*claircore.Vulnerability
vulns, err = u.Parse(ctx, vulnDB)
if err != nil {
err = fmt.Errorf("vulnerability database parse failed: %v", err)
return
}
ref, err = m.store.UpdateVulnerabilities(ctx, name, newFP, vulns)
}
if err != nil {
err = fmt.Errorf("failed to update: %v", err)
return
}
zlog.Info(ctx).
Str("ref", ref.String()).
Msg("successful update")
return nil
}
// NoopConfig is used when an explicit config is not provided.
func noopConfig(_ interface{}) error { return nil }