Skip to content

Commit e75c2d1

Browse files
committed
Vuls2Conf instead of Vuls2DictConf
1 parent 26e1ead commit e75c2d1

File tree

7 files changed

+42
-40
lines changed

7 files changed

+42
-40
lines changed

config/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type Config struct {
4747
Metasploit MetasploitConf `json:"metasploit,omitempty"`
4848
KEVuln KEVulnConf `json:"kevuln,omitempty"`
4949
Cti CtiConf `json:"cti,omitempty"`
50-
Vuls2 Vuls2DictConf `json:"vuls2Dict,omitempty"`
50+
Vuls2 Vuls2Conf `json:"vuls2,omitempty"`
5151

5252
Slack SlackConf `json:"-"`
5353
EMail SMTPConf `json:"-"`

config/vulnDictConf.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ func (cnf *CtiConf) Init() {
329329
cnf.DebugSQL = Conf.DebugSQL
330330
}
331331

332-
type Vuls2DictConf struct {
332+
type Vuls2Conf struct {
333333
Repository string
334334
Path string
335335
SkipUpdate bool

detector/detector.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ func Detect(rs []models.ScanResult, dir string) ([]models.ScanResult, error) {
318318

319319
// DetectPkgCves detects OS pkg cves
320320
// pass 2 configs
321-
func DetectPkgCves(r *models.ScanResult, ovalCnf config.GovalDictConf, gostCnf config.GostConf, vuls2Cnf config.Vuls2DictConf, logOpts logging.LogOpts, noProgress bool) error {
321+
func DetectPkgCves(r *models.ScanResult, ovalCnf config.GovalDictConf, gostCnf config.GostConf, vuls2Conf config.Vuls2Conf, logOpts logging.LogOpts, noProgress bool) error {
322322
// Pkg Scan
323323
if isPkgCvesDetactable(r) {
324324
// OVAL, gost(Debian Security Tracker) does not support Package for Raspbian, so skip it.
@@ -327,7 +327,7 @@ func DetectPkgCves(r *models.ScanResult, ovalCnf config.GovalDictConf, gostCnf c
327327
}
328328

329329
// Vuls2
330-
if err := vuls2.Detect(r, vuls2Cnf, noProgress); err != nil {
330+
if err := vuls2.Detect(r, vuls2Conf, noProgress); err != nil {
331331
return xerrors.Errorf("Failed to detect CVE with Vuls2: %w", err)
332332
}
333333

detector/vuls2/db.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,22 @@ var (
2626
}()
2727
)
2828

29-
func newDBConnection(vuls2Cnf config.Vuls2DictConf, noProgress bool) (db.DB, error) {
30-
willDownload, err := shouldDownload(vuls2Cnf, time.Now())
29+
func newDBConnection(vuls2Conf config.Vuls2Conf, noProgress bool) (db.DB, error) {
30+
willDownload, err := shouldDownload(vuls2Conf, time.Now())
3131
if err != nil {
3232
return nil, xerrors.Errorf("Failed to check whether to download vuls2 db. err: %w", err)
3333
}
3434

3535
if willDownload {
36-
logging.Log.Infof("Downloading vuls2 db. repository: %s", vuls2Cnf.Repository)
37-
if err := fetch.Fetch(fetch.WithRepository(vuls2Cnf.Repository), fetch.WithDBPath(vuls2Cnf.Path), fetch.WithNoProgress(noProgress)); err != nil {
36+
logging.Log.Infof("Downloading vuls2 db. repository: %s", vuls2Conf.Repository)
37+
if err := fetch.Fetch(fetch.WithRepository(vuls2Conf.Repository), fetch.WithDBPath(vuls2Conf.Path), fetch.WithNoProgress(noProgress)); err != nil {
3838
return nil, xerrors.Errorf("Failed to fetch vuls2 db. err: %w", err)
3939
}
4040
}
4141

4242
dbc, err := (&db.Config{
4343
Type: "boltdb",
44-
Path: vuls2Cnf.Path,
44+
Path: vuls2Conf.Path,
4545
}).New()
4646
if err != nil {
4747
return nil, xerrors.Errorf("Failed to new vuls2 db connection. err: %w", err)
@@ -50,40 +50,40 @@ func newDBConnection(vuls2Cnf config.Vuls2DictConf, noProgress bool) (db.DB, err
5050
return dbc, nil
5151
}
5252

53-
func shouldDownload(vuls2Cnf config.Vuls2DictConf, now time.Time) (bool, error) {
54-
if _, err := os.Stat(vuls2Cnf.Path); err != nil {
53+
func shouldDownload(vuls2Conf config.Vuls2Conf, now time.Time) (bool, error) {
54+
if _, err := os.Stat(vuls2Conf.Path); err != nil {
5555
if errors.Is(err, os.ErrNotExist) {
56-
if vuls2Cnf.SkipUpdate {
57-
return false, xerrors.Errorf("%s not found, cannot skip update", vuls2Cnf.Path)
56+
if vuls2Conf.SkipUpdate {
57+
return false, xerrors.Errorf("%s not found, cannot skip update", vuls2Conf.Path)
5858
}
5959
return true, nil
6060
}
6161
return false, xerrors.Errorf("Failed to stat vuls2 db file. err: %w", err)
6262
}
6363

64-
if vuls2Cnf.SkipUpdate {
64+
if vuls2Conf.SkipUpdate {
6565
return false, nil
6666
}
6767

6868
dbc, err := (&db.Config{
6969
Type: "boltdb",
70-
Path: vuls2Cnf.Path,
70+
Path: vuls2Conf.Path,
7171
}).New()
7272
if err != nil {
73-
return false, xerrors.Errorf("Failed to new vuls2 db connection. path: %s, err: %w", vuls2Cnf.Path, err)
73+
return false, xerrors.Errorf("Failed to new vuls2 db connection. path: %s, err: %w", vuls2Conf.Path, err)
7474
}
7575

7676
if err := dbc.Open(); err != nil {
77-
return false, xerrors.Errorf("Failed to open vuls2 db. path: %s, err: %w", vuls2Cnf.Path, err)
77+
return false, xerrors.Errorf("Failed to open vuls2 db. path: %s, err: %w", vuls2Conf.Path, err)
7878
}
7979
defer dbc.Close()
8080

8181
metadata, err := dbc.GetMetadata()
8282
if err != nil {
83-
return false, xerrors.Errorf("Failed to get vuls2 db metadata. path: %s, err: %w", vuls2Cnf.Path, err)
83+
return false, xerrors.Errorf("Failed to get vuls2 db metadata. path: %s, err: %w", vuls2Conf.Path, err)
8484
}
8585
if metadata == nil {
86-
return false, xerrors.Errorf("Unexpected Vuls2 db metadata. metadata: nil,. path: %s", vuls2Cnf.Path)
86+
return false, xerrors.Errorf("Unexpected Vuls2 db metadata. metadata: nil,. path: %s", vuls2Conf.Path)
8787
}
8888

8989
if metadata.Downloaded != nil && now.Before((*metadata.Downloaded).Add(1*time.Hour)) {

detector/vuls2/db_test.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import (
1515

1616
func Test_shouldDownload(t *testing.T) {
1717
type args struct {
18-
vuls2Cnf config.Vuls2DictConf
19-
now time.Time
18+
vuls2Conf config.Vuls2Conf
19+
now time.Time
2020
}
2121
tests := []struct {
2222
name string
@@ -28,15 +28,15 @@ func Test_shouldDownload(t *testing.T) {
2828
{
2929
name: "no db file",
3030
args: args{
31-
vuls2Cnf: config.Vuls2DictConf{},
32-
now: *parse("2024-01-02T00:00:00Z"),
31+
vuls2Conf: config.Vuls2Conf{},
32+
now: *parse("2024-01-02T00:00:00Z"),
3333
},
3434
want: true,
3535
},
3636
{
3737
name: "no db file, but skip update",
3838
args: args{
39-
vuls2Cnf: config.Vuls2DictConf{
39+
vuls2Conf: config.Vuls2Conf{
4040
SkipUpdate: true,
4141
},
4242
now: *parse("2024-01-02T00:00:00Z"),
@@ -46,8 +46,8 @@ func Test_shouldDownload(t *testing.T) {
4646
{
4747
name: "just created",
4848
args: args{
49-
vuls2Cnf: config.Vuls2DictConf{},
50-
now: *parse("2024-01-02T00:00:00Z"),
49+
vuls2Conf: config.Vuls2Conf{},
50+
now: *parse("2024-01-02T00:00:00Z"),
5151
},
5252
metadata: &types.Metadata{
5353
LastModified: *parse("2024-01-02T00:00:00Z"),
@@ -59,8 +59,8 @@ func Test_shouldDownload(t *testing.T) {
5959
{
6060
name: "8 hours old",
6161
args: args{
62-
vuls2Cnf: config.Vuls2DictConf{},
63-
now: *parse("2024-01-02T08:00:00Z"),
62+
vuls2Conf: config.Vuls2Conf{},
63+
now: *parse("2024-01-02T08:00:00Z"),
6464
},
6565
metadata: &types.Metadata{
6666
LastModified: *parse("2024-01-02T00:00:00Z"),
@@ -72,7 +72,7 @@ func Test_shouldDownload(t *testing.T) {
7272
{
7373
name: "8 hours old, but skip update",
7474
args: args{
75-
vuls2Cnf: config.Vuls2DictConf{
75+
vuls2Conf: config.Vuls2Conf{
7676
SkipUpdate: true,
7777
},
7878
now: *parse("2024-01-02T08:00:00Z"),
@@ -87,8 +87,8 @@ func Test_shouldDownload(t *testing.T) {
8787
{
8888
name: "8 hours old, but download recently",
8989
args: args{
90-
vuls2Cnf: config.Vuls2DictConf{},
91-
now: *parse("2024-01-02T08:00:00Z"),
90+
vuls2Conf: config.Vuls2Conf{},
91+
now: *parse("2024-01-02T08:00:00Z"),
9292
},
9393
metadata: &types.Metadata{
9494
LastModified: *parse("2024-01-02T00:00:00Z"),
@@ -101,14 +101,14 @@ func Test_shouldDownload(t *testing.T) {
101101
for _, tt := range tests {
102102
t.Run(tt.name, func(t *testing.T) {
103103
d := t.TempDir()
104-
tt.args.vuls2Cnf.Path = filepath.Join(d, "vuls.db")
104+
tt.args.vuls2Conf.Path = filepath.Join(d, "vuls.db")
105105
if tt.metadata != nil {
106-
if err := putMetadata(*tt.metadata, tt.args.vuls2Cnf.Path); err != nil {
106+
if err := putMetadata(*tt.metadata, tt.args.vuls2Conf.Path); err != nil {
107107
t.Errorf("putMetadata err = %v", err)
108108
return
109109
}
110110
}
111-
got, err := shouldDownload(tt.args.vuls2Cnf, tt.args.now)
111+
got, err := shouldDownload(tt.args.vuls2Conf, tt.args.now)
112112
if (err != nil) != tt.wantErr {
113113
t.Errorf("shouldDownload() error = %v, wantErr %v", err, tt.wantErr)
114114
return

detector/vuls2/vendor.go

+1
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ func affectedPackageName(family string, pkg scanTypes.OSPackage) string {
226226
return pkg.SrcName
227227
}
228228
}
229+
229230
func cveContentOptional(family string, rootID dataTypes.RootID, sourceID sourceTypes.SourceID) map[string]string {
230231
switch family {
231232
case constant.RedHat, constant.CentOS:

detector/vuls2/vuls2.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,22 @@ import (
3737

3838
var cveRe = regexp.MustCompile("CVE-[0-9]{4}-[0-9]+")
3939

40-
func Detect(r *models.ScanResult, vuls2Cnf config.Vuls2DictConf, noProgress bool) error {
40+
// Detect detects vulnerabilities and fills ScanResult
41+
func Detect(r *models.ScanResult, vuls2Conf config.Vuls2Conf, noProgress bool) error {
4142
switch r.Family {
4243
case constant.RedHat, constant.CentOS, constant.Alma, constant.Rocky:
4344
default:
4445
return nil
4546
}
4647

47-
if vuls2Cnf.Repository == "" {
48-
vuls2Cnf.Repository = DefaultGHCRRepository
48+
if vuls2Conf.Repository == "" {
49+
vuls2Conf.Repository = DefaultGHCRRepository
4950
}
50-
if vuls2Cnf.Path == "" {
51-
vuls2Cnf.Path = DefaultPath
51+
if vuls2Conf.Path == "" {
52+
vuls2Conf.Path = DefaultPath
5253
}
5354

54-
dbc, err := newDBConnection(vuls2Cnf, noProgress)
55+
dbc, err := newDBConnection(vuls2Conf, noProgress)
5556
if err != nil {
5657
return xerrors.Errorf("Failed to get new db connection. err: %w", err)
5758
}

0 commit comments

Comments
 (0)