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{ Use: "update", Aliases: []string { "up" }, Short: "Update mirror.", Long: `Update local NVD mirror.`, Run: func(cmd *cobra.Command, args []string) { // custom sync config config := nvdmirror.SyncConfig { Cve11BaseUrl: "https://pmdn.org/cvez-test/20220318/nvd/cve", CpeMatch10BaseUrl: "https://pmdn.org/cvez-test/20220318/nvd/cpematch", Cpe23DictUrl: "https://pmdn.org/cvez-test/20220318/nvd/cpedict/official-cpe-dictionary_v2.3.xml.gz", CisaKevcUrl: "https://pmdn.org/cvez-test/20220318/cisa/known_exploited_vulnerabilities.json", CweListUrl: "https://pmdn.org/cvez-test/20220318/cwe/cwec_latest.xml.zip", } // get cache and cache dir cache, cacheDir := getCache() defer cache.Close() // sync data, get updates updates := nvdmirror.Sync(config, &cache, cacheDir) 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) } } } } }, }