Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(db,server): add GetCveIDs #358

Merged
merged 3 commits into from
Apr 24, 2024
Merged
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
1 change: 1 addition & 0 deletions db/db.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package db

Check failure on line 1 in db/db.go

View workflow job for this annotation

GitHub Actions / Build

should have a package comment https://revive.run/r#package-comments

import (
"fmt"
Expand Down Expand Up @@ -29,6 +29,7 @@

Get(string) (*models.CveDetail, error)
GetMulti([]string) (map[string]models.CveDetail, error)
GetCveIDs() ([]string, error)
GetCveIDsByCpeURI(string) ([]string, []string, []string, error)
GetByCpeURI(string) ([]models.CveDetail, error)
InsertJvn([]string) error
Expand Down
36 changes: 33 additions & 3 deletions db/rdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/knqyf263/go-cpe/common"
"github.com/knqyf263/go-cpe/naming"
"github.com/spf13/viper"
"golang.org/x/exp/maps"
"golang.org/x/xerrors"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
Expand Down Expand Up @@ -188,9 +189,7 @@ func (r *RDBDriver) MigrateDB() error {
}
}
case dialectMysql, dialectPostgreSQL:
if err != nil {
return xerrors.Errorf("Failed to migrate. err: %w", err)
}
return xerrors.Errorf("Failed to migrate. err: %w", err)
default:
return xerrors.Errorf("Not Supported DB dialects. r.name: %s", r.name)
}
Expand Down Expand Up @@ -308,6 +307,37 @@ func (r *RDBDriver) Get(cveID string) (*models.CveDetail, error) {
return &detail, nil
}

// GetCveIDs select all cve ids
func (r *RDBDriver) GetCveIDs() ([]string, error) {
cveIDs := map[string]struct{}{}

var nvds []string
if err := r.conn.Model(&models.Nvd{}).Pluck("cve_id", &nvds).Error; err != nil {
return nil, err
}
for _, cveID := range nvds {
cveIDs[cveID] = struct{}{}
}

var jvns []string
if err := r.conn.Model(&models.Jvn{}).Distinct().Pluck("cve_id", &jvns).Error; err != nil {
return nil, err
}
for _, cveID := range jvns {
cveIDs[cveID] = struct{}{}
}

var fortinets []string
if err := r.conn.Model(&models.Fortinet{}).Distinct().Pluck("cve_id", &fortinets).Error; err != nil {
return nil, err
}
for _, cveID := range fortinets {
cveIDs[cveID] = struct{}{}
}

return maps.Keys(cveIDs), nil
}

func (r *RDBDriver) getCveIDsByPartVendorProduct(uri string) ([]string, error) {
specified, err := naming.UnbindURI(uri)
if err != nil {
Expand Down
31 changes: 30 additions & 1 deletion db/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,35 @@ func (r *RedisDriver) GetMulti(cveIDs []string) (map[string]models.CveDetail, er
return cveDetails, nil
}

// GetCveIDs select all cve ids
func (r *RedisDriver) GetCveIDs() ([]string, error) {
ctx := context.Background()

dbsize, err := r.conn.DBSize(ctx).Result()
if err != nil {
return nil, xerrors.Errorf("Failed to DBSize. err: %w", err)
}

cveIDs := []string{}
var cursor uint64
for {
var keys []string
var err error
keys, cursor, err = r.conn.Scan(ctx, cursor, fmt.Sprintf(cveKeyFormat, "*"), dbsize/5).Result()
if err != nil {
return nil, xerrors.Errorf("Failed to Scan. err: %w", err)
}

cveIDs = append(cveIDs, keys...)

if cursor == 0 {
break
}
}

return cveIDs, nil
}

// GetCveIDsByCpeURI Select Cve Ids by by pseudo-CPE
func (r *RedisDriver) GetCveIDsByCpeURI(uri string) ([]string, []string, []string, error) {
specified, err := naming.UnbindURI(uri)
Expand Down Expand Up @@ -504,7 +533,7 @@ func (r *RedisDriver) InsertNvd(years []string) error {
}
var err error

log.Infof("Fetching CVE information from NVD(recent, modified).")
log.Infof("Fetching CVE information from NVD.")
cacheDir, err := nvd.Fetch()
if err != nil {
return xerrors.Errorf("Failed to Fetch. err: %w", err)
Expand Down
30 changes: 30 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server

Check failure on line 1 in server/server.go

View workflow job for this annotation

GitHub Actions / Build

should have a package comment https://revive.run/r#package-comments

import (
"fmt"
Expand Down Expand Up @@ -38,6 +38,8 @@
// Routes
e.GET("/health", health())
e.GET("/cves/:id", getCve(driver))
e.POST("/cves", getCveMulti(driver))
e.GET("/cves/ids", getCveIDs(driver))
e.POST("/cpes", getCveByCpeName(driver))
e.POST("/cpes/ids", getCveIDsByCpeName(driver))

Expand Down Expand Up @@ -67,6 +69,23 @@
}
}

func getCveMulti(driver db.DB) echo.HandlerFunc {
return func(c echo.Context) error {
var cveIDs []string
if err := c.Bind(&cveIDs); err != nil {
log.Errorf("%s", err)
return err
}

cveDetails, err := driver.GetMulti(cveIDs)
if err != nil {
log.Errorf("%s", err)
return err
}
return c.JSON(http.StatusOK, &cveDetails)
}
}

type cpeName struct {
Name string `form:"name"`
}
Expand All @@ -92,6 +111,17 @@
}
}

func getCveIDs(driver db.DB) echo.HandlerFunc {
return func(c echo.Context) error {
cveIDs, err := driver.GetCveIDs()
if err != nil {
log.Errorf("%s", err)
return err
}
return c.JSON(http.StatusOK, &cveIDs)
}
}

func getCveIDsByCpeName(driver db.DB) echo.HandlerFunc {
return func(c echo.Context) error {
cpe := cpeName{}
Expand Down
Loading