-
-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fortinet): new support for fortinet data feed (#336)
- Loading branch information
Showing
13 changed files
with
1,118 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package commands | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
"golang.org/x/xerrors" | ||
|
||
db "github.com/vulsio/go-cve-dictionary/db" | ||
"github.com/vulsio/go-cve-dictionary/fetcher/fortinet" | ||
log "github.com/vulsio/go-cve-dictionary/log" | ||
"github.com/vulsio/go-cve-dictionary/models" | ||
) | ||
|
||
var fetchFortinetCmd = &cobra.Command{ | ||
Use: "fortinet", | ||
Short: "Fetch Vulnerability dictionary from Fortinet Advisories", | ||
Long: "Fetch Vulnerability dictionary from Fortinet Advisories", | ||
RunE: fetchFortinet, | ||
} | ||
|
||
func init() { | ||
fetchCmd.AddCommand(fetchFortinetCmd) | ||
} | ||
|
||
func fetchFortinet(_ *cobra.Command, _ []string) (err error) { | ||
if err := log.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) | ||
} | ||
|
||
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) | ||
} | ||
|
||
advs, err := fortinet.FetchConvert() | ||
if err != nil { | ||
return xerrors.Errorf("Failed to fetch from fortinet. err: %w", err) | ||
} | ||
|
||
log.Infof("Inserting Fortinet into DB (%s).", driver.Name()) | ||
if err := driver.InsertFortinet(advs); 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) | ||
} | ||
|
||
log.Infof("Finished fetching Fortinet.") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.