diff options
author | Paul Duncan <pabs@pablotron.org> | 2022-03-19 03:50:06 -0400 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2022-03-19 03:50:06 -0400 |
commit | d94b83df0de95db7ae63726fd11ddd30dff72f39 (patch) | |
tree | ec278ba97ce3ccfad1bc17b9664725ba390bf990 /cmd/update.go | |
parent | 169678198051c4195579bbd011476233983fda66 (diff) | |
download | cvez-d94b83df0de95db7ae63726fd11ddd30dff72f39.tar.bz2 cvez-d94b83df0de95db7ae63726fd11ddd30dff72f39.zip |
cmd: working update cmd
Diffstat (limited to 'cmd/update.go')
-rw-r--r-- | cmd/update.go | 62 |
1 files changed, 61 insertions, 1 deletions
diff --git a/cmd/update.go b/cmd/update.go index b6f2882..86e6c7a 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -2,8 +2,14 @@ package cmd import ( // "encoding/json" + "context" + "github.com/pablotron/cvez/cisa" "github.com/pablotron/cvez/nvdmirror" + "github.com/rs/zerolog/log" "github.com/spf13/cobra" + nvd_feed "github.com/pablotron/cvez/feed" + "os" + "path/filepath" ) var updateCmd = &cobra.Command{ @@ -28,6 +34,60 @@ var updateCmd = &cobra.Command{ // sync data, get updates updates := nvdmirror.Sync(config, &cache, cacheDir) - jsonEncode(updates) + if len(updates) > 0 { + // connect to db + ctx := context.Background() + db := getDb() + + // build list of feeds to add + log.Info().Msg("load feeds") + var feeds []nvd_feed.Feed + for _, row := range(updates) { + if row.Type == nvdmirror.UpdateCveYear { + feeds = append(feeds, getFeed(filepath.Join(cacheDir, row.Path))) + } + } + + if len(feeds) > 0 { + log.Info().Msg("AddCveFeeds") + if _, err := db.AddCveFeeds(ctx, feeds); err != nil { + // FIXME: failing like this leaves an invalid cache + log.Error().Err(err).Msg("AddCveFeeds") + os.Exit(-1) + } + } + + // process cpe dictionary before cpe matches to prevent FK + // constraint violations + for _, row := range(updates) { + if row.Type == nvdmirror.UpdateCpeDict { + log.Info().Msg("AddCpeDictionary") + dict := getCpeDict(filepath.Join(cacheDir, row.Path)) + if err := db.AddCpeDictionary(ctx, dict); err != nil { + log.Error().Err(err).Msg("AddCpeDictionary") + os.Exit(-1) + } + } + } + + for _, row := range(updates) { + switch row.Type { + case nvdmirror.UpdateCpeMatch: + log.Info().Msg("AddCpeMatches") + matches := getCpeMatches(filepath.Join(cacheDir, row.Path)) + if err := db.AddCpeMatches(ctx, matches); err != nil { + log.Error().Err(err).Msg("AddCpeMatches") + os.Exit(-1) + } + case nvdmirror.UpdateCisaKevc: + log.Info().Msg("AddCisaCatalogs") + cat := getCisaCatalog(filepath.Join(cacheDir, row.Path)) + if _, err := db.AddCisaCatalogs(ctx, []cisa.Catalog { cat }); err != nil { + log.Error().Err(err).Msg("AddCisaCatalogs") + os.Exit(-1) + } + } + } + } }, } |