2017-01-04 02:44:32 +00:00
|
|
|
// Copyright 2017 clair authors
|
2015-11-13 19:11:28 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-01-19 18:42:37 +00:00
|
|
|
package clair
|
2015-11-13 19:11:28 +00:00
|
|
|
|
|
|
|
import (
|
2017-07-26 23:22:29 +00:00
|
|
|
"fmt"
|
2015-11-13 19:11:28 +00:00
|
|
|
"math/rand"
|
|
|
|
"strconv"
|
2016-02-01 23:41:40 +00:00
|
|
|
"sync"
|
2015-11-13 19:11:28 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pborman/uuid"
|
2016-01-24 03:02:34 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2017-05-04 17:21:25 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-01-04 02:44:32 +00:00
|
|
|
|
|
|
|
"github.com/coreos/clair/database"
|
|
|
|
"github.com/coreos/clair/ext/vulnmdsrc"
|
|
|
|
"github.com/coreos/clair/ext/vulnsrc"
|
2017-01-18 03:18:03 +00:00
|
|
|
"github.com/coreos/clair/pkg/stopper"
|
2015-11-13 19:11:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-06-28 21:13:27 +00:00
|
|
|
updaterLastFlagName = "updater/last"
|
|
|
|
updaterLockName = "updater"
|
|
|
|
updaterLockDuration = updaterLockRefreshDuration + time.Minute*2
|
|
|
|
updaterLockRefreshDuration = time.Minute * 8
|
|
|
|
updaterSleepBetweenLoopsDuration = time.Minute
|
2015-11-13 19:11:28 +00:00
|
|
|
)
|
|
|
|
|
2016-01-24 03:02:34 +00:00
|
|
|
var (
|
|
|
|
promUpdaterErrorsTotal = prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Name: "clair_updater_errors_total",
|
|
|
|
Help: "Numbers of errors that the updater generated.",
|
|
|
|
})
|
|
|
|
|
|
|
|
promUpdaterDurationSeconds = prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
Name: "clair_updater_duration_seconds",
|
|
|
|
Help: "Time it takes to update the vulnerability database.",
|
|
|
|
})
|
|
|
|
|
|
|
|
promUpdaterNotesTotal = prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
Name: "clair_updater_notes_total",
|
|
|
|
Help: "Number of notes that the vulnerability fetchers generated.",
|
|
|
|
})
|
2017-07-26 23:22:29 +00:00
|
|
|
|
|
|
|
// EnabledUpdaters contains all updaters to be used for update.
|
|
|
|
EnabledUpdaters []string
|
2016-01-24 03:02:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
prometheus.MustRegister(promUpdaterErrorsTotal)
|
|
|
|
prometheus.MustRegister(promUpdaterDurationSeconds)
|
|
|
|
prometheus.MustRegister(promUpdaterNotesTotal)
|
|
|
|
}
|
2015-11-13 19:11:28 +00:00
|
|
|
|
2017-01-27 01:14:44 +00:00
|
|
|
// UpdaterConfig is the configuration for the Updater service.
|
|
|
|
type UpdaterConfig struct {
|
2017-07-26 23:22:29 +00:00
|
|
|
EnabledUpdaters []string
|
|
|
|
Interval time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
type vulnerabilityChange struct {
|
|
|
|
old *database.VulnerabilityWithAffected
|
|
|
|
new *database.VulnerabilityWithAffected
|
2017-01-27 01:14:44 +00:00
|
|
|
}
|
|
|
|
|
2017-01-19 18:42:37 +00:00
|
|
|
// RunUpdater begins a process that updates the vulnerability database at
|
|
|
|
// regular intervals.
|
2017-01-27 01:14:44 +00:00
|
|
|
func RunUpdater(config *UpdaterConfig, datastore database.Datastore, st *stopper.Stopper) {
|
2015-11-13 19:11:28 +00:00
|
|
|
defer st.End()
|
|
|
|
|
2015-12-07 21:38:50 +00:00
|
|
|
// Do not run the updater if there is no config or if the interval is 0.
|
2017-07-26 23:22:29 +00:00
|
|
|
if config == nil || config.Interval == 0 || len(config.EnabledUpdaters) == 0 {
|
2017-05-04 17:21:25 +00:00
|
|
|
log.Info("updater service is disabled.")
|
2015-11-13 19:11:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
whoAmI := uuid.New()
|
2017-05-04 17:21:25 +00:00
|
|
|
log.WithField("lock identifier", whoAmI).Info("updater service started")
|
2015-11-13 19:11:28 +00:00
|
|
|
|
|
|
|
for {
|
2015-12-14 05:48:01 +00:00
|
|
|
var stop bool
|
2016-02-04 22:10:19 +00:00
|
|
|
|
|
|
|
// Determine if this is the first update and define the next update time.
|
|
|
|
// The next update time is (last update time + interval) or now if this is the first update.
|
|
|
|
nextUpdate := time.Now().UTC()
|
2017-07-26 23:22:29 +00:00
|
|
|
lastUpdate, firstUpdate, err := GetLastUpdateTime(datastore)
|
2016-02-04 22:10:19 +00:00
|
|
|
if err != nil {
|
2017-07-26 23:22:29 +00:00
|
|
|
log.WithError(err).Error("an error occurred while getting the last update time")
|
2016-02-04 22:10:19 +00:00
|
|
|
nextUpdate = nextUpdate.Add(config.Interval)
|
2017-07-26 23:22:29 +00:00
|
|
|
} else if !firstUpdate {
|
2015-12-07 21:38:50 +00:00
|
|
|
nextUpdate = lastUpdate.Add(config.Interval)
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the next update timer is in the past, then try to update.
|
|
|
|
if nextUpdate.Before(time.Now().UTC()) {
|
|
|
|
// Attempt to get a lock on the the update.
|
|
|
|
log.Debug("attempting to obtain update lock")
|
2017-07-26 23:22:29 +00:00
|
|
|
hasLock, hasLockUntil := lock(datastore, updaterLockName, whoAmI, updaterLockDuration, false)
|
2015-11-13 19:11:28 +00:00
|
|
|
if hasLock {
|
|
|
|
// Launch update in a new go routine.
|
|
|
|
doneC := make(chan bool, 1)
|
|
|
|
go func() {
|
2017-01-19 18:42:37 +00:00
|
|
|
update(datastore, firstUpdate)
|
2015-11-13 19:11:28 +00:00
|
|
|
doneC <- true
|
|
|
|
}()
|
|
|
|
|
2015-12-14 05:48:01 +00:00
|
|
|
for done := false; !done && !stop; {
|
2015-11-13 19:11:28 +00:00
|
|
|
select {
|
|
|
|
case <-doneC:
|
|
|
|
done = true
|
2017-01-19 18:42:37 +00:00
|
|
|
case <-time.After(updaterLockRefreshDuration):
|
2015-11-20 20:02:47 +00:00
|
|
|
// Refresh the lock until the update is done.
|
2017-07-26 23:22:29 +00:00
|
|
|
lock(datastore, updaterLockName, whoAmI, updaterLockDuration, true)
|
2015-12-14 05:48:01 +00:00
|
|
|
case <-st.Chan():
|
|
|
|
stop = true
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
// Unlock the updater.
|
|
|
|
unlock(datastore, updaterLockName, whoAmI)
|
2015-12-01 19:58:17 +00:00
|
|
|
|
2015-12-14 05:48:01 +00:00
|
|
|
if stop {
|
|
|
|
break
|
|
|
|
}
|
2017-06-28 21:13:27 +00:00
|
|
|
|
|
|
|
// Sleep for a short duration to prevent pinning the CPU on a
|
|
|
|
// consistent failure.
|
|
|
|
if stopped := sleepUpdater(time.Now().Add(updaterSleepBetweenLoopsDuration), st); stopped {
|
|
|
|
break
|
|
|
|
}
|
2015-12-01 19:58:17 +00:00
|
|
|
continue
|
2015-11-13 19:11:28 +00:00
|
|
|
} else {
|
2017-07-26 23:22:29 +00:00
|
|
|
lockOwner, lockExpiration, ok, err := findLock(datastore, updaterLockName)
|
|
|
|
if !ok || err != nil {
|
2015-11-13 19:11:28 +00:00
|
|
|
log.Debug("update lock is already taken")
|
|
|
|
nextUpdate = hasLockUntil
|
|
|
|
} else {
|
2017-05-04 17:21:25 +00:00
|
|
|
log.WithFields(log.Fields{"lock owner": lockOwner, "lock expiration": lockExpiration}).Debug("update lock is already taken")
|
2015-11-13 19:11:28 +00:00
|
|
|
nextUpdate = lockExpiration
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-28 21:13:27 +00:00
|
|
|
if stopped := sleepUpdater(nextUpdate, st); stopped {
|
|
|
|
break
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-29 16:22:54 +00:00
|
|
|
// Clean resources.
|
2017-01-13 07:33:19 +00:00
|
|
|
for _, appenders := range vulnmdsrc.Appenders() {
|
2017-01-04 02:44:32 +00:00
|
|
|
appenders.Clean()
|
2016-02-01 23:41:40 +00:00
|
|
|
}
|
2017-01-13 07:33:19 +00:00
|
|
|
for _, updaters := range vulnsrc.Updaters() {
|
2017-01-04 02:44:32 +00:00
|
|
|
updaters.Clean()
|
2016-01-29 16:22:54 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 19:11:28 +00:00
|
|
|
log.Info("updater service stopped")
|
|
|
|
}
|
|
|
|
|
2017-06-28 21:13:27 +00:00
|
|
|
// sleepUpdater sleeps the updater for an approximate duration, but remains
|
|
|
|
// able to be cancelled by a stopper.
|
|
|
|
func sleepUpdater(approxWakeup time.Time, st *stopper.Stopper) (stopped bool) {
|
|
|
|
waitUntil := approxWakeup.Add(time.Duration(rand.ExpFloat64()/0.5) * time.Second)
|
|
|
|
log.WithField("scheduled time", waitUntil).Debug("updater sleeping")
|
|
|
|
if !waitUntil.Before(time.Now().UTC()) {
|
|
|
|
if !st.Sleep(waitUntil.Sub(time.Now())) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
// update fetches all the vulnerabilities from the registered fetchers, updates
|
|
|
|
// vulnerabilities, and updater flags, and logs notes from updaters.
|
2017-01-19 18:42:37 +00:00
|
|
|
func update(datastore database.Datastore, firstUpdate bool) {
|
2016-01-24 03:02:34 +00:00
|
|
|
defer setUpdaterDuration(time.Now())
|
|
|
|
|
2015-11-13 19:11:28 +00:00
|
|
|
log.Info("updating vulnerabilities")
|
|
|
|
|
2015-12-01 19:58:17 +00:00
|
|
|
// Fetch updates.
|
2017-07-26 23:22:29 +00:00
|
|
|
success, vulnerabilities, flags, notes := fetch(datastore)
|
|
|
|
|
|
|
|
// do vulnerability namespacing again to merge potentially duplicated
|
|
|
|
// vulnerabilities from each updater.
|
|
|
|
vulnerabilities = doVulnerabilitiesNamespacing(vulnerabilities)
|
|
|
|
|
|
|
|
// deduplicate fetched namespaces and store them into database.
|
|
|
|
nsMap := map[database.Namespace]struct{}{}
|
|
|
|
for _, vuln := range vulnerabilities {
|
|
|
|
nsMap[vuln.Namespace] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespaces := make([]database.Namespace, 0, len(nsMap))
|
|
|
|
for ns := range nsMap {
|
|
|
|
namespaces = append(namespaces, ns)
|
|
|
|
}
|
|
|
|
|
2018-10-08 17:12:18 +00:00
|
|
|
if err := database.PersistNamespacesAndCommit(datastore, namespaces); err != nil {
|
2017-07-26 23:22:29 +00:00
|
|
|
log.WithError(err).Error("Unable to insert namespaces")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
changes, err := updateVulnerabilities(datastore, vulnerabilities)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
promUpdaterErrorsTotal.Inc()
|
|
|
|
}
|
|
|
|
}()
|
2015-12-01 19:58:17 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2017-07-26 23:22:29 +00:00
|
|
|
log.WithError(err).Error("Unable to update vulnerabilities")
|
2015-12-01 19:58:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
if !firstUpdate {
|
|
|
|
err = createVulnerabilityNotifications(datastore, changes)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Unable to create notifications")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = updateUpdaterFlags(datastore, flags)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Unable to update updater flags")
|
|
|
|
return
|
2015-12-01 19:58:17 +00:00
|
|
|
}
|
2016-01-20 00:17:08 +00:00
|
|
|
|
2016-01-24 03:02:34 +00:00
|
|
|
for _, note := range notes {
|
2017-05-04 17:21:25 +00:00
|
|
|
log.WithField("note", note).Warning("fetcher note")
|
2016-01-24 03:02:34 +00:00
|
|
|
}
|
|
|
|
promUpdaterNotesTotal.Set(float64(len(notes)))
|
2015-12-01 19:58:17 +00:00
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
if success {
|
|
|
|
err = setLastUpdateTime(datastore)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Unable to set last update time")
|
|
|
|
return
|
|
|
|
}
|
2015-12-01 19:58:17 +00:00
|
|
|
}
|
2016-01-24 03:02:34 +00:00
|
|
|
|
2015-12-01 19:58:17 +00:00
|
|
|
log.Info("update finished")
|
|
|
|
}
|
|
|
|
|
2016-01-24 03:02:34 +00:00
|
|
|
func setUpdaterDuration(start time.Time) {
|
|
|
|
promUpdaterDurationSeconds.Set(time.Since(start).Seconds())
|
|
|
|
}
|
|
|
|
|
2015-12-01 19:58:17 +00:00
|
|
|
// fetch get data from the registered fetchers, in parallel.
|
2017-07-26 23:22:29 +00:00
|
|
|
func fetch(datastore database.Datastore) (bool, []database.VulnerabilityWithAffected, map[string]string, []string) {
|
|
|
|
var vulnerabilities []database.VulnerabilityWithAffected
|
2016-01-20 00:17:08 +00:00
|
|
|
var notes []string
|
|
|
|
status := true
|
|
|
|
flags := make(map[string]string)
|
|
|
|
|
2015-11-13 19:11:28 +00:00
|
|
|
// Fetch updates in parallel.
|
2016-02-01 23:41:40 +00:00
|
|
|
log.Info("fetching vulnerability updates")
|
2017-01-04 02:44:32 +00:00
|
|
|
var responseC = make(chan *vulnsrc.UpdateResponse, 0)
|
2017-07-26 23:22:29 +00:00
|
|
|
numUpdaters := 0
|
2017-01-13 07:33:19 +00:00
|
|
|
for n, u := range vulnsrc.Updaters() {
|
2017-07-26 23:22:29 +00:00
|
|
|
if !updaterEnabled(n) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
numUpdaters++
|
2017-01-04 02:44:32 +00:00
|
|
|
go func(name string, u vulnsrc.Updater) {
|
|
|
|
response, err := u.Update(datastore)
|
2015-11-13 19:11:28 +00:00
|
|
|
if err != nil {
|
2016-01-24 03:02:34 +00:00
|
|
|
promUpdaterErrorsTotal.Inc()
|
2017-07-26 23:22:29 +00:00
|
|
|
log.WithError(err).WithField("updater name", name).Error("an error occurred when fetching update")
|
2015-11-13 19:11:28 +00:00
|
|
|
status = false
|
|
|
|
responseC <- nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
responseC <- &response
|
2017-05-04 17:21:25 +00:00
|
|
|
log.WithField("updater name", name).Info("finished fetching")
|
2017-01-04 02:44:32 +00:00
|
|
|
}(n, u)
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Collect results of updates.
|
2017-07-26 23:22:29 +00:00
|
|
|
for i := 0; i < numUpdaters; i++ {
|
2015-12-01 19:58:17 +00:00
|
|
|
resp := <-responseC
|
|
|
|
if resp != nil {
|
2016-01-25 19:50:48 +00:00
|
|
|
vulnerabilities = append(vulnerabilities, doVulnerabilitiesNamespacing(resp.Vulnerabilities)...)
|
2016-01-20 00:17:08 +00:00
|
|
|
notes = append(notes, resp.Notes...)
|
|
|
|
if resp.FlagName != "" && resp.FlagValue != "" {
|
|
|
|
flags[resp.FlagName] = resp.FlagValue
|
2015-12-01 19:58:17 +00:00
|
|
|
}
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 00:17:08 +00:00
|
|
|
close(responseC)
|
2016-02-01 23:41:40 +00:00
|
|
|
return status, addMetadata(datastore, vulnerabilities), flags, notes
|
|
|
|
}
|
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
// Add metadata to the specified vulnerabilities using the registered
|
|
|
|
// MetadataFetchers, in parallel.
|
|
|
|
func addMetadata(datastore database.Datastore, vulnerabilities []database.VulnerabilityWithAffected) []database.VulnerabilityWithAffected {
|
|
|
|
if len(vulnmdsrc.Appenders()) == 0 || len(vulnerabilities) == 0 {
|
2016-02-01 23:41:40 +00:00
|
|
|
return vulnerabilities
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("adding metadata to vulnerabilities")
|
|
|
|
|
2017-01-04 02:44:32 +00:00
|
|
|
// Add a mutex to each vulnerability to ensure that only one appender at a
|
|
|
|
// time can modify the vulnerability's Metadata map.
|
|
|
|
lockableVulnerabilities := make([]*lockableVulnerability, 0, len(vulnerabilities))
|
2016-02-01 23:41:40 +00:00
|
|
|
for i := 0; i < len(vulnerabilities); i++ {
|
2017-01-04 02:44:32 +00:00
|
|
|
lockableVulnerabilities = append(lockableVulnerabilities, &lockableVulnerability{
|
2017-07-26 23:22:29 +00:00
|
|
|
VulnerabilityWithAffected: &vulnerabilities[i],
|
2016-02-01 23:41:40 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
2017-01-13 07:33:19 +00:00
|
|
|
wg.Add(len(vulnmdsrc.Appenders()))
|
2016-02-01 23:41:40 +00:00
|
|
|
|
2017-01-13 07:33:19 +00:00
|
|
|
for n, a := range vulnmdsrc.Appenders() {
|
2017-01-04 02:44:32 +00:00
|
|
|
go func(name string, appender vulnmdsrc.Appender) {
|
2016-02-01 23:41:40 +00:00
|
|
|
defer wg.Done()
|
|
|
|
|
2017-01-04 02:44:32 +00:00
|
|
|
// Build up a metadata cache.
|
|
|
|
if err := appender.BuildCache(datastore); err != nil {
|
2016-02-01 23:41:40 +00:00
|
|
|
promUpdaterErrorsTotal.Inc()
|
2017-07-26 23:22:29 +00:00
|
|
|
log.WithError(err).WithField("appender name", name).Error("an error occurred when loading metadata fetcher")
|
2016-02-01 23:41:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-04 02:44:32 +00:00
|
|
|
// Append vulnerability metadata to each vulnerability.
|
|
|
|
for _, vulnerability := range lockableVulnerabilities {
|
|
|
|
appender.Append(vulnerability.Name, vulnerability.appendFunc)
|
2016-02-01 23:41:40 +00:00
|
|
|
}
|
|
|
|
|
2017-01-04 02:44:32 +00:00
|
|
|
// Purge the metadata cache.
|
|
|
|
appender.PurgeCache()
|
|
|
|
}(n, a)
|
2016-02-01 23:41:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
return vulnerabilities
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
// GetLastUpdateTime retrieves the latest successful time of update and whether
|
|
|
|
// or not it's the first update.
|
|
|
|
func GetLastUpdateTime(datastore database.Datastore) (time.Time, bool, error) {
|
|
|
|
tx, err := datastore.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, false, err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
lastUpdateTSS, ok, err := tx.FindKeyValue(updaterLastFlagName)
|
2016-02-04 22:10:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, false, err
|
|
|
|
}
|
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
if !ok {
|
2016-02-04 22:10:19 +00:00
|
|
|
// This is the first update.
|
|
|
|
return time.Time{}, true, nil
|
2015-12-01 19:58:17 +00:00
|
|
|
}
|
2016-02-04 22:10:19 +00:00
|
|
|
|
|
|
|
lastUpdateTS, err := strconv.ParseInt(lastUpdateTSS, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return time.Unix(lastUpdateTS, 0).UTC(), false, nil
|
2015-12-01 19:58:17 +00:00
|
|
|
}
|
2016-01-25 19:50:48 +00:00
|
|
|
|
2017-01-04 02:44:32 +00:00
|
|
|
type lockableVulnerability struct {
|
2017-07-26 23:22:29 +00:00
|
|
|
*database.VulnerabilityWithAffected
|
2017-01-04 02:44:32 +00:00
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
2017-01-19 18:42:37 +00:00
|
|
|
func (lv *lockableVulnerability) appendFunc(metadataKey string, metadata interface{}, severity database.Severity) {
|
2017-01-04 02:44:32 +00:00
|
|
|
lv.Lock()
|
|
|
|
defer lv.Unlock()
|
|
|
|
|
|
|
|
// If necessary, initialize the metadata map for the vulnerability.
|
|
|
|
if lv.Metadata == nil {
|
|
|
|
lv.Metadata = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append the metadata.
|
|
|
|
lv.Metadata[metadataKey] = metadata
|
|
|
|
|
|
|
|
// If necessary, provide a severity for the vulnerability.
|
2017-01-19 18:42:37 +00:00
|
|
|
if lv.Severity == database.UnknownSeverity {
|
2017-01-04 02:44:32 +00:00
|
|
|
lv.Severity = severity
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-23 07:05:52 +00:00
|
|
|
// doVulnerabilitiesNamespacing takes Vulnerabilities that don't have a
|
|
|
|
// Namespace and split them into multiple vulnerabilities that have a Namespace
|
2017-07-26 23:22:29 +00:00
|
|
|
// and only contains the Affected Features corresponding to their
|
2017-02-23 07:05:52 +00:00
|
|
|
// Namespace.
|
2016-01-25 19:50:48 +00:00
|
|
|
//
|
2017-02-23 07:05:52 +00:00
|
|
|
// It helps simplifying the fetchers that share the same metadata about a
|
|
|
|
// Vulnerability regardless of their actual namespace (ie. same vulnerability
|
|
|
|
// information for every version of a distro).
|
2017-07-26 23:22:29 +00:00
|
|
|
//
|
|
|
|
// It also validates the vulnerabilities fetched from updaters. If any
|
|
|
|
// vulnerability is mal-formated, the updater process will continue but will log
|
|
|
|
// warning.
|
|
|
|
func doVulnerabilitiesNamespacing(vulnerabilities []database.VulnerabilityWithAffected) []database.VulnerabilityWithAffected {
|
|
|
|
vulnerabilitiesMap := make(map[string]*database.VulnerabilityWithAffected)
|
2016-01-25 19:50:48 +00:00
|
|
|
|
|
|
|
for _, v := range vulnerabilities {
|
2017-07-26 23:22:29 +00:00
|
|
|
namespacedFeatures := v.Affected
|
|
|
|
v.Affected = []database.AffectedFeature{}
|
|
|
|
|
|
|
|
for _, fv := range namespacedFeatures {
|
|
|
|
// validate vulnerabilities, throw out the invalid vulnerabilities
|
2019-02-19 21:36:31 +00:00
|
|
|
if fv.FeatureType == "" || fv.AffectedVersion == "" || fv.FeatureName == "" || fv.Namespace.Name == "" || fv.Namespace.VersionFormat == "" {
|
2017-07-26 23:22:29 +00:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"Name": fv.FeatureName,
|
|
|
|
"Affected Version": fv.AffectedVersion,
|
|
|
|
"Namespace": fv.Namespace.Name + ":" + fv.Namespace.VersionFormat,
|
|
|
|
}).Warn("Mal-formated affected feature (skipped)")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
index := fv.Namespace.Name + ":" + v.Name
|
2016-01-25 19:50:48 +00:00
|
|
|
|
|
|
|
if vulnerability, ok := vulnerabilitiesMap[index]; !ok {
|
|
|
|
newVulnerability := v
|
2017-07-26 23:22:29 +00:00
|
|
|
newVulnerability.Namespace = fv.Namespace
|
|
|
|
newVulnerability.Affected = []database.AffectedFeature{fv}
|
2016-01-25 19:50:48 +00:00
|
|
|
|
|
|
|
vulnerabilitiesMap[index] = &newVulnerability
|
|
|
|
} else {
|
2017-07-26 23:22:29 +00:00
|
|
|
vulnerability.Affected = append(vulnerability.Affected, fv)
|
2016-01-25 19:50:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert map into a slice.
|
2017-07-26 23:22:29 +00:00
|
|
|
var response []database.VulnerabilityWithAffected
|
|
|
|
for _, v := range vulnerabilitiesMap {
|
|
|
|
// throw out invalid vulnerabilities.
|
|
|
|
if v.Name == "" || !v.Severity.Valid() || v.Namespace.Name == "" || v.Namespace.VersionFormat == "" {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"Name": v.Name,
|
|
|
|
"Severity": v.Severity,
|
|
|
|
"Namespace": v.Namespace.Name + ":" + v.Namespace.VersionFormat,
|
|
|
|
}).Warning("Vulnerability is mal-formatted")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
response = append(response, *v)
|
2016-01-25 19:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return response
|
|
|
|
}
|
2017-07-26 23:22:29 +00:00
|
|
|
|
|
|
|
func findLock(datastore database.Datastore, updaterLockName string) (string, time.Time, bool, error) {
|
|
|
|
tx, err := datastore.Begin()
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error()
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
return tx.FindLock(updaterLockName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateUpdaterFlags updates the flags specified by updaters, every transaction
|
|
|
|
// is independent of each other.
|
|
|
|
func updateUpdaterFlags(datastore database.Datastore, flags map[string]string) error {
|
|
|
|
for key, value := range flags {
|
|
|
|
tx, err := datastore.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
err = tx.UpdateKeyValue(key, value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = tx.Commit(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// setLastUpdateTime records the last successful date time in database.
|
|
|
|
func setLastUpdateTime(datastore database.Datastore) error {
|
|
|
|
tx, err := datastore.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
err = tx.UpdateKeyValue(updaterLastFlagName, strconv.FormatInt(time.Now().UTC().Unix(), 10))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
// isVulnerabilityChange compares two vulnerabilities by their severity and
|
|
|
|
// affected features, and return true if they are different.
|
|
|
|
func isVulnerabilityChanged(a *database.VulnerabilityWithAffected, b *database.VulnerabilityWithAffected) bool {
|
|
|
|
if a == b {
|
|
|
|
return false
|
|
|
|
} else if a != nil && b != nil && a.Severity == b.Severity && len(a.Affected) == len(b.Affected) {
|
|
|
|
checked := map[string]bool{}
|
|
|
|
for _, affected := range a.Affected {
|
|
|
|
checked[affected.Namespace.Name+":"+affected.FeatureName] = false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, affected := range b.Affected {
|
|
|
|
key := affected.Namespace.Name + ":" + affected.FeatureName
|
|
|
|
if visited, ok := checked[key]; !ok || visited {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
checked[key] = true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// findVulnerabilityChanges finds vulnerability changes from old
|
|
|
|
// vulnerabilities to new vulnerabilities.
|
|
|
|
// old and new vulnerabilities should be unique.
|
|
|
|
func findVulnerabilityChanges(old []database.VulnerabilityWithAffected, new []database.VulnerabilityWithAffected) ([]vulnerabilityChange, error) {
|
|
|
|
changes := map[database.VulnerabilityID]vulnerabilityChange{}
|
|
|
|
for i, vuln := range old {
|
|
|
|
key := database.VulnerabilityID{
|
|
|
|
Name: vuln.Name,
|
|
|
|
Namespace: vuln.Namespace.Name,
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := changes[key]; ok {
|
|
|
|
return nil, fmt.Errorf("duplicated old vulnerability")
|
|
|
|
}
|
|
|
|
changes[key] = vulnerabilityChange{old: &old[i]}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, vuln := range new {
|
|
|
|
key := database.VulnerabilityID{
|
|
|
|
Name: vuln.Name,
|
|
|
|
Namespace: vuln.Namespace.Name,
|
|
|
|
}
|
|
|
|
|
|
|
|
if change, ok := changes[key]; ok {
|
|
|
|
if isVulnerabilityChanged(change.old, &vuln) {
|
|
|
|
change.new = &new[i]
|
|
|
|
changes[key] = change
|
|
|
|
} else {
|
|
|
|
delete(changes, key)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
changes[key] = vulnerabilityChange{new: &new[i]}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vulnChange := make([]vulnerabilityChange, 0, len(changes))
|
|
|
|
for _, change := range changes {
|
|
|
|
vulnChange = append(vulnChange, change)
|
|
|
|
}
|
|
|
|
return vulnChange, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// createVulnerabilityNotifications makes notifications out of vulnerability
|
|
|
|
// changes and insert them into database.
|
|
|
|
func createVulnerabilityNotifications(datastore database.Datastore, changes []vulnerabilityChange) error {
|
|
|
|
log.WithField("count", len(changes)).Debug("creating vulnerability notifications")
|
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := datastore.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
notifications := make([]database.VulnerabilityNotification, 0, len(changes))
|
|
|
|
for _, change := range changes {
|
|
|
|
var oldVuln, newVuln *database.Vulnerability
|
|
|
|
if change.old != nil {
|
|
|
|
oldVuln = &change.old.Vulnerability
|
|
|
|
}
|
|
|
|
|
|
|
|
if change.new != nil {
|
|
|
|
newVuln = &change.new.Vulnerability
|
|
|
|
}
|
|
|
|
|
|
|
|
notifications = append(notifications, database.VulnerabilityNotification{
|
|
|
|
NotificationHook: database.NotificationHook{
|
|
|
|
Name: uuid.New(),
|
|
|
|
Created: time.Now(),
|
|
|
|
},
|
|
|
|
Old: oldVuln,
|
|
|
|
New: newVuln,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.InsertVulnerabilityNotifications(notifications); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateVulnerabilities upserts unique vulnerabilities into the database and
|
|
|
|
// computes vulnerability changes.
|
|
|
|
func updateVulnerabilities(datastore database.Datastore, vulnerabilities []database.VulnerabilityWithAffected) ([]vulnerabilityChange, error) {
|
|
|
|
log.WithField("count", len(vulnerabilities)).Debug("updating vulnerabilities")
|
|
|
|
if len(vulnerabilities) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ids := make([]database.VulnerabilityID, 0, len(vulnerabilities))
|
|
|
|
for _, vuln := range vulnerabilities {
|
|
|
|
ids = append(ids, database.VulnerabilityID{
|
|
|
|
Name: vuln.Name,
|
|
|
|
Namespace: vuln.Namespace.Name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := datastore.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer tx.Rollback()
|
|
|
|
oldVulnNullable, err := tx.FindVulnerabilities(ids)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
oldVuln := []database.VulnerabilityWithAffected{}
|
|
|
|
for _, vuln := range oldVulnNullable {
|
|
|
|
if vuln.Valid {
|
|
|
|
oldVuln = append(oldVuln, vuln.VulnerabilityWithAffected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
changes, err := findVulnerabilityChanges(oldVuln, vulnerabilities)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
toRemove := []database.VulnerabilityID{}
|
|
|
|
toAdd := []database.VulnerabilityWithAffected{}
|
|
|
|
for _, change := range changes {
|
|
|
|
if change.old != nil {
|
|
|
|
toRemove = append(toRemove, database.VulnerabilityID{
|
|
|
|
Name: change.old.Name,
|
|
|
|
Namespace: change.old.Namespace.Name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if change.new != nil {
|
|
|
|
toAdd = append(toAdd, *change.new)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithField("count", len(toRemove)).Debug("marking vulnerabilities as outdated")
|
|
|
|
if err := tx.DeleteVulnerabilities(toRemove); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithField("count", len(toAdd)).Debug("inserting new vulnerabilities")
|
|
|
|
if err := tx.InsertVulnerabilities(toAdd); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return changes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func updaterEnabled(updaterName string) bool {
|
|
|
|
for _, u := range EnabledUpdaters {
|
|
|
|
if u == updaterName {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|