Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion commands/fetch-awesomepoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func fetchAwesomePoc(_ *cobra.Command, _ []string) (err error) {
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{})
driver, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{BatchSize: viper.GetInt("batch-size")})
if err != nil {
if xerrors.Is(err, db.ErrDBLocked) {
return xerrors.Errorf("Failed to open DB. Close DB connection before fetching. err: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion commands/fetch-exploitdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func fetchExploitDB(_ *cobra.Command, _ []string) (err error) {
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{})
driver, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{BatchSize: viper.GetInt("batch-size")})
if err != nil {
if xerrors.Is(err, db.ErrDBLocked) {
return xerrors.Errorf("Failed to open DB. Close DB connection before fetching. err: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion commands/fetch-githubrepos.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func fetchGitHubRepos(_ *cobra.Command, _ []string) (err error) {
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{})
driver, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{BatchSize: viper.GetInt("batch-size")})
if err != nil {
if xerrors.Is(err, db.ErrDBLocked) {
return xerrors.Errorf("Failed to open DB. Close DB connection before fetching. err: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion commands/fetch-inthewild.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func fetchInTheWild(_ *cobra.Command, _ []string) (err error) {
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{})
driver, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{BatchSize: viper.GetInt("batch-size")})
if err != nil {
if xerrors.Is(err, db.ErrDBLocked) {
return xerrors.Errorf("Failed to open DB. Close DB connection before fetching. err: %w", err)
Expand Down
1 change: 1 addition & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type DB interface {
// Option :
type Option struct {
RedisTimeout time.Duration
BatchSize int
}

// NewDB :
Expand Down
17 changes: 9 additions & 8 deletions db/rdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/cheggaaa/pb/v3"
"github.com/glebarez/sqlite"
"github.com/inconshreveable/log15"
"github.com/spf13/viper"
"golang.org/x/xerrors"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
Expand All @@ -32,8 +31,9 @@ const (

// RDBDriver :
type RDBDriver struct {
name string
conn *gorm.DB
name string
conn *gorm.DB
batchSize int
}

// https://github.com/mattn/go-sqlite3/blob/edc3bb69551dcfff02651f083b21f3366ea2f5ab/error.go#L18-L66
Expand All @@ -58,7 +58,9 @@ func (r *RDBDriver) Name() string {
}

// OpenDB opens Database
func (r *RDBDriver) OpenDB(dbType, dbPath string, debugSQL bool, _ Option) (err error) {
func (r *RDBDriver) OpenDB(dbType, dbPath string, debugSQL bool, option Option) (err error) {
r.batchSize = option.BatchSize

gormConfig := gorm.Config{
DisableForeignKeyConstraintWhenMigrating: true,
Logger: logger.New(
Expand Down Expand Up @@ -197,8 +199,7 @@ func (r *RDBDriver) deleteAndInsertExploit(exploitType models.ExploitType, explo
tx.Commit()
}()

batchSize := viper.GetInt("batch-size")
if batchSize < 1 {
if r.batchSize < 1 {
return xerrors.New("Failed to set batch-size. err: batch-size option is not set properly")
}

Expand All @@ -210,7 +211,7 @@ func (r *RDBDriver) deleteAndInsertExploit(exploitType models.ExploitType, explo
if len(oldIDs) > 0 {
log15.Info("Deleting old Exploits")
bar := pb.StartNew(len(oldIDs))
for idx := range chunkSlice(len(oldIDs), batchSize) {
for idx := range chunkSlice(len(oldIDs), r.batchSize) {
osIDs := []models.OffensiveSecurity{}
if err := tx.Model(&models.OffensiveSecurity{}).Select("id").Where("exploit_id IN ?", oldIDs[idx.From:idx.To]).Find(&osIDs).Error; err != nil {
return xerrors.Errorf("Failed to select old OffensiveSecurity: %w", err)
Expand Down Expand Up @@ -252,7 +253,7 @@ func (r *RDBDriver) deleteAndInsertExploit(exploitType models.ExploitType, explo

log15.Info("Inserting new Exploits")
bar := pb.StartNew(len(exploits))
for idx := range chunkSlice(len(exploits), batchSize) {
for idx := range chunkSlice(len(exploits), r.batchSize) {
if err = tx.Create(exploits[idx.From:idx.To]).Error; err != nil {
return xerrors.Errorf("Failed to insert. err: %w", err)
}
Expand Down
12 changes: 6 additions & 6 deletions db/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/cheggaaa/pb/v3"
"github.com/go-redis/redis/v8"
"github.com/inconshreveable/log15"
"github.com/spf13/viper"
"golang.org/x/xerrors"

"github.com/vulsio/go-exploitdb/config"
Expand Down Expand Up @@ -55,8 +54,9 @@ const (

// RedisDriver is Driver for Redis
type RedisDriver struct {
name string
conn *redis.Client
name string
conn *redis.Client
batchSize int
}

// Name return db name
Expand All @@ -66,6 +66,7 @@ func (r *RedisDriver) Name() string {

// OpenDB opens Database
func (r *RedisDriver) OpenDB(_, dbPath string, _ bool, option Option) error {
r.batchSize = option.BatchSize
if err := r.connectRedis(dbPath, option); err != nil {
return xerrors.Errorf("Failed to open DB. dbtype: %s, dbpath: %s, err: %w", dialectRedis, dbPath, err)
}
Expand Down Expand Up @@ -305,8 +306,7 @@ func (r *RedisDriver) GetExploitMultiByCveID(cveIDs []string) (map[string][]mode
// InsertExploit :
func (r *RedisDriver) InsertExploit(exploitType models.ExploitType, exploits []models.Exploit) (err error) {
ctx := context.Background()
batchSize := viper.GetInt("batch-size")
if batchSize < 1 {
if r.batchSize < 1 {
return xerrors.Errorf("Failed to set batch-size. err: batch-size option is not set properly")
}

Expand All @@ -326,7 +326,7 @@ func (r *RedisDriver) InsertExploit(exploitType models.ExploitType, exploits []m

bar := pb.StartNew(len(exploits))
var noCveIDExploitCount, cveIDExploitCount int
for idx := range chunkSlice(len(exploits), batchSize) {
for idx := range chunkSlice(len(exploits), r.batchSize) {
pipe := r.conn.Pipeline()
for _, exploit := range exploits[idx.From:idx.To] {
j, err := json.Marshal(exploit)
Expand Down