Skip to content

Commit

Permalink
refactor: remove redundant logging (#65)
Browse files Browse the repository at this point in the history
* refactor: remove redundant logging

* fix
  • Loading branch information
kotakanbe authored Oct 1, 2021
1 parent c10d271 commit 3c58fdc
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 223 deletions.
17 changes: 6 additions & 11 deletions commands/fetch-awesomepoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,33 @@ func fetchAwesomePoc(cmd *cobra.Command, args []string) (err error) {
)
if err != nil {
if locked {
log15.Error("Failed to initialize DB. Close DB connection before fetching", "err", err)
return xerrors.Errorf("Failed to initialize DB. Close DB connection before fetching. err: %w", err)
}
return err
}

fetchMeta, err := driver.GetFetchMeta()
if err != nil {
log15.Error("Failed to get FetchMeta from DB.", "err", err)
return err
return xerrors.Errorf("Failed to get FetchMeta from DB. err: %w", err)
}
if fetchMeta.OutDated() {
log15.Error("Failed to Insert CVEs into DB. SchemaVersion is old", "SchemaVersion", map[string]uint{"latest": models.LatestSchemaVersion, "DB": fetchMeta.SchemaVersion})
return xerrors.New("Failed to Insert CVEs into DB. SchemaVersion is old")
return xerrors.Errorf("Failed to Insert CVEs into DB. SchemaVersion is old. SchemaVersion: %+v", map[string]uint{"latest": models.LatestSchemaVersion, "DB": fetchMeta.SchemaVersion})
}

if err := driver.UpsertFetchMeta(fetchMeta); err != nil {
log15.Error("Failed to upsert FetchMeta to DB.", "dbpath", viper.GetString("dbpath"), "err", err)
return err
return xerrors.Errorf("Failed to upsert FetchMeta to DB. dbpath: %s, err: %w", viper.GetString("dbpath"), err)
}

log15.Info("Fetching Awesome Poc Exploit")
var exploits []models.Exploit
if exploits, err = fetcher.FetchAwesomePoc(); err != nil {
log15.Error("Failed to fetch AwesomePoc Exploit", "err", err)
return err
return xerrors.Errorf("Failed to fetch AwesomePoc Exploit. err: %w", err)
}
log15.Info("Awesome Poc Exploit", "count", len(exploits))

log15.Info("Insert Exploit into go-exploitdb.", "db", driver.Name())
if err := driver.InsertExploit(models.AwesomePocType, exploits); err != nil {
log15.Error("Failed to insert.", "dbpath", viper.GetString("dbpath"), "err", err)
return err
return xerrors.Errorf("Failed to insert. dbpath: %s, err: %w", viper.GetString("dbpath"), err)
}

return nil
Expand Down
17 changes: 6 additions & 11 deletions commands/fetch-exploitdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,33 @@ func fetchExploitDB(cmd *cobra.Command, args []string) (err error) {
)
if err != nil {
if locked {
log15.Error("Failed to initialize DB. Close DB connection before fetching", "err", err)
return xerrors.Errorf("Failed to initialize DB. Close DB connection before fetching. err: %w", err)
}
return err
}

fetchMeta, err := driver.GetFetchMeta()
if err != nil {
log15.Error("Failed to get FetchMeta from DB.", "err", err)
return err
return xerrors.Errorf("Failed to get FetchMeta from DB. err: %w", err)
}
if fetchMeta.OutDated() {
log15.Error("Failed to Insert CVEs into DB. SchemaVersion is old", "SchemaVersion", map[string]uint{"latest": models.LatestSchemaVersion, "DB": fetchMeta.SchemaVersion})
return xerrors.New("Failed to Insert CVEs into DB. SchemaVersion is old")
return xerrors.Errorf("Failed to Insert CVEs into DB. SchemaVersion is old. SchemaVersion: %+v", map[string]uint{"latest": models.LatestSchemaVersion, "DB": fetchMeta.SchemaVersion})
}

if err := driver.UpsertFetchMeta(fetchMeta); err != nil {
log15.Error("Failed to upsert FetchMeta to DB.", "dbpath", viper.GetString("dbpath"), "err", err)
return err
return xerrors.Errorf("Failed to upsert FetchMeta to DB. dbpath: %s, err: %w", viper.GetString("dbpath"), err)
}

log15.Info("Fetching Offensive Security Exploit")
var exploits []models.Exploit
if exploits, err = fetcher.FetchExploitDB(); err != nil {
log15.Error("Failed to fetch Exploit", "err", err)
return err
return xerrors.Errorf("Failed to fetch Exploit. err: %w", err)
}
log15.Info("Offensive Security Exploit", "count", len(exploits))

log15.Info("Insert Exploit into go-exploitdb.", "db", driver.Name())
if err := driver.InsertExploit(models.OffensiveSecurityType, exploits); err != nil {
log15.Error("Failed to insert.", "dbpath", viper.GetString("dbpath"), "err", err)
return err
return xerrors.Errorf("Failed to insert. dbpath: %s, err: %w", viper.GetString("dbpath"), err)
}

return nil
Expand Down
17 changes: 6 additions & 11 deletions commands/fetch-githubrepos.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,38 +40,33 @@ func fetchGitHubRepos(cmd *cobra.Command, args []string) (err error) {
)
if err != nil {
if locked {
log15.Error("Failed to initialize DB. Close DB connection before fetching", "err", err)
return xerrors.Errorf("Failed to initialize DB. Close DB connection before fetching. err: %w", err)
}
return err
}

fetchMeta, err := driver.GetFetchMeta()
if err != nil {
log15.Error("Failed to get FetchMeta from DB.", "err", err)
return err
return xerrors.Errorf("Failed to get FetchMeta from DB. err: %w", err)
}
if fetchMeta.OutDated() {
log15.Error("Failed to Insert CVEs into DB. SchemaVersion is old", "SchemaVersion", map[string]uint{"latest": models.LatestSchemaVersion, "DB": fetchMeta.SchemaVersion})
return xerrors.New("Failed to Insert CVEs into DB. SchemaVersion is old")
return xerrors.Errorf("Failed to Insert CVEs into DB. SchemaVersion is old. SchemaVersion: %+v", map[string]uint{"latest": models.LatestSchemaVersion, "DB": fetchMeta.SchemaVersion})
}

if err := driver.UpsertFetchMeta(fetchMeta); err != nil {
log15.Error("Failed to upsert FetchMeta to DB.", "dbpath", viper.GetString("dbpath"), "err", err)
return err
return xerrors.Errorf("Failed to upsert FetchMeta to DB. dbpath: %s, err: %w", viper.GetString("dbpath"), err)
}

log15.Info("Fetching GitHub Repos Exploit")
var exploits []models.Exploit
if exploits, err = fetcher.FetchGitHubRepos(viper.GetInt("threshold-stars"), viper.GetInt("threshold-forks")); err != nil {
log15.Error("Failed to fetch GitHubRepo Exploit", "err", err)
return err
return xerrors.Errorf("Failed to fetch GitHubRepo Exploit. err: %w", err)
}
log15.Info("GitHub Repos Exploit", "count", len(exploits))

log15.Info("Insert Exploit into go-exploitdb.", "db", driver.Name())
if err := driver.InsertExploit(models.GitHubRepositoryType, exploits); err != nil {
log15.Error("Failed to insert.", "dbpath", viper.GetString("dbpath"), "err", err)
return err
return xerrors.Errorf("Failed to insert. dbpath: %s, err: %w", viper.GetString("dbpath"), err)
}

return nil
Expand Down
13 changes: 4 additions & 9 deletions commands/search.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package commands

import (
"errors"
"fmt"
"regexp"

"github.com/inconshreveable/log15"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/vulsio/go-exploitdb/db"
Expand Down Expand Up @@ -51,7 +49,7 @@ func searchExploit(cmd *cobra.Command, args []string) error {
)
if err != nil {
if locked {
log15.Error("Failed to initialize DB. Close DB connection before fetching", "err", err)
return xerrors.Errorf("Failed to initialize DB. Close DB connection before fetching. err: %w", err)
}
return err
}
Expand All @@ -63,25 +61,22 @@ func searchExploit(cmd *cobra.Command, args []string) error {
switch searchType {
case "CVE":
if !cveIDRegexp.MatchString(param) {
log15.Error("Specify the search type [CVE] parameters like `--param CVE-xxxx-xxxx`")
return errors.New("Invalid CVE Param")
return xerrors.Errorf("Specify the search type [CVE] parameters like `--param CVE-xxxx-xxxx`")
}
results, err = driver.GetExploitByCveID(param)
if err != nil {
return err
}
case "ID":
if !exploitDBIDRegexp.MatchString(param) {
log15.Error("Specify the search type [ID] parameters like `--param 10000`")
return errors.New("Invalid ID Param")
return xerrors.Errorf("Specify the search type [ID] parameters like `--param 10000`")
}
results, err = driver.GetExploitByID(param)
if err != nil {
return err
}
default:
log15.Error("Specify the search type [ CVE / ID].")
return errors.New("Invalid Type")
return xerrors.Errorf("Specify the search type [ CVE / ID].")
}
fmt.Println("")
fmt.Println("Results: ")
Expand Down
5 changes: 2 additions & 3 deletions commands/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ func executeServer(cmd *cobra.Command, args []string) (err error) {
driver, locked, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"))
if err != nil {
if locked {
log15.Error("Failed to initialize DB. Close DB connection before fetching", "err", err)
return xerrors.Errorf("Failed to initialize DB. Close DB connection before fetching. err: %w", err)
}
return err
}

log15.Info("Starting HTTP Server...")
if err = server.Start(viper.GetBool("log-to-file"), viper.GetString("log-dir"), driver); err != nil {
log15.Error("Failed to start server.", "err", err)
return err
return xerrors.Errorf("Failed to start server. err: %w", err)
}

return nil
Expand Down
53 changes: 0 additions & 53 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,58 +1,5 @@
package config

import (
valid "github.com/asaskevich/govalidator"
"github.com/inconshreveable/log15"
)

// CommonConfig :
type CommonConfig struct {
Debug bool
DebugSQL bool
Quiet bool
DBPath string
DBType string
HTTPProxy string
}

// SearchConfig :
type SearchConfig struct {
SearchType string
SearchParam string
}

// ServerConfig :
type ServerConfig struct {
Bind string
Port string
}

// CommonConf :
var CommonConf CommonConfig

// SearchConf :
var SearchConf SearchConfig

// ServerConf :
var ServerConf ServerConfig

// Validate :
func (p *CommonConfig) Validate() bool {
if p.DBType == "sqlite3" {
if ok, _ := valid.IsFilePath(p.DBPath); !ok {
log15.Error("SQLite3 DB path must be a *Absolute* file path.", "dbpath", p.DBPath)
return false
}
}

_, err := valid.ValidateStruct(p)
if err != nil {
log15.Error("Invalid Struct", "err", err)
return false
}
return true
}

// Version of go-exploitdb
var Version = "`make build` or `make install` will show the version"

Expand Down
11 changes: 3 additions & 8 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package db
import (
"fmt"

"github.com/inconshreveable/log15"
"github.com/vulsio/go-exploitdb/models"
"golang.org/x/xerrors"
)
Expand All @@ -29,8 +28,7 @@ type DB interface {
// NewDB :
func NewDB(dbType string, dbPath string, debugSQL bool) (driver DB, locked bool, err error) {
if driver, err = newDB(dbType); err != nil {
log15.Error("Failed to new db.", "err", err)
return driver, false, err
return driver, false, xerrors.Errorf("Failed to new db. err: %w", err)
}

if locked, err := driver.OpenDB(dbType, dbPath, debugSQL); err != nil {
Expand All @@ -42,17 +40,14 @@ func NewDB(dbType string, dbPath string, debugSQL bool) (driver DB, locked bool,

isV1, err := driver.IsExploitModelV1()
if err != nil {
log15.Error("Failed to IsExploitModelV1.", "err", err)
return nil, false, err
return nil, false, xerrors.Errorf("Failed to IsExploitModelV1. err: %w", err)
}
if isV1 {
log15.Error("Failed to NewDB. Since SchemaVersion is incompatible, delete Database and fetch again")
return nil, false, xerrors.New("Failed to NewDB. Since SchemaVersion is incompatible, delete Database and fetch again.")
}

if err := driver.MigrateDB(); err != nil {
log15.Error("Failed to migrate db.", "err", err)
return driver, false, err
return driver, false, xerrors.Errorf("Failed to migrate db. err: %w", err)
}

return driver, false, nil
Expand Down
Loading

0 comments on commit 3c58fdc

Please sign in to comment.