/
redhat.go
75 lines (63 loc) · 2.54 KB
/
redhat.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
package cmd
import (
"time"
"github.com/inconshreveable/log15"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/xerrors"
"github.com/vulsio/gost/db"
"github.com/vulsio/gost/fetcher"
"github.com/vulsio/gost/models"
"github.com/vulsio/gost/util"
)
// redhatCmd represents the redhat command
var redHatCmd = &cobra.Command{
Use: "redhat",
Short: "Fetch the CVE information from aquasecurity/vuln-list",
Long: `Fetch the CVE information from aquasecurity/vuln-list`,
RunE: fetchRedHat,
}
func init() {
fetchCmd.AddCommand(redHatCmd)
}
func fetchRedHat(_ *cobra.Command, _ []string) (err error) {
if err := util.SetLogger(viper.GetBool("log-to-file"), viper.GetString("log-dir"), viper.GetBool("debug"), viper.GetBool("log-json")); err != nil {
return xerrors.Errorf("Failed to SetLogger. err: %w", err)
}
cveJSONs, err := fetcher.FetchRedHatVulnList()
if err != nil {
return xerrors.Errorf("Failed to initialize vulnerability DB . err: %w", err)
}
cves, err := models.ConvertRedhat(cveJSONs)
if err != nil {
return xerrors.Errorf("Failed to convert RedhatCVE. err: %w", err)
}
log15.Info("Initialize Database")
driver, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{})
if err != nil {
if xerrors.Is(err, db.ErrDBLocked) {
return xerrors.Errorf("Failed to open DB. Close DB connection before fetching. err: %w", err)
}
return xerrors.Errorf("Failed to open DB. err: %w", err)
}
fetchMeta, err := driver.GetFetchMeta()
if err != nil {
return xerrors.Errorf("Failed to get FetchMeta from DB. err: %w", err)
}
if fetchMeta.OutDated() {
return xerrors.Errorf("Failed to Insert CVEs into DB. err: SchemaVersion is old. SchemaVersion: %+v", map[string]uint{"latest": models.LatestSchemaVersion, "DB": fetchMeta.SchemaVersion})
}
// If the fetch fails the first time (without SchemaVersion), the DB needs to be cleaned every time, so insert SchemaVersion.
if err := driver.UpsertFetchMeta(fetchMeta); err != nil {
return xerrors.Errorf("Failed to upsert FetchMeta to DB. dbpath: %s, err: %w", viper.GetString("dbpath"), err)
}
log15.Info("Insert RedHat into DB", "db", driver.Name())
if err := driver.InsertRedhat(cves); err != nil {
return xerrors.Errorf("Failed to insert. dbpath: %s, err: %w", viper.GetString("dbpath"), err)
}
fetchMeta.LastFetchedAt = time.Now()
if err := driver.UpsertFetchMeta(fetchMeta); err != nil {
return xerrors.Errorf("Failed to upsert FetchMeta to DB. dbpath: %s, err: %w", viper.GetString("dbpath"), err)
}
return nil
}