-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(detector): use vuls2 for RedHat, CentOS, Alma and Rocky (#2106)
* feat!(detector): use vuls2 for redhat/alma/rocky (#2075) Co-authored-by: MaineK00n <[email protected]> * chore(detector/vuls2): check downloaded time before updating db (#2077) * chore(detector/vuls2): check downloaded time before updating db Co-authored-by: MaineK00n <[email protected]> * fix(detector/vuls2): fix post convert bugs (#2082) * fix(detector/vuls2): use tag for selection logic (#2086) * fix(models/cvecontents): use cve content type Alma, Rocky (#2087) * fix(detector/vuls2): lower stauts string and compare (#2095) * chore(deps): update vuls2 (#2096) * feat(detector/vuls2): fill title and summary (#2097) * chore(deps): update vuls2 (#2099) * chore(deps): update vuls2 incorporate MaineK00n/vuls2#139 * Vuls2Conf instead of Vuls2DictConf * Update detector/vuls2/db.go Co-authored-by: MaineK00n <[email protected]> * Update detector/vuls2/vendor.go Co-authored-by: MaineK00n <[email protected]> * Refactor * more refactor --------- Co-authored-by: MaineK00n <[email protected]>
- Loading branch information
Showing
13 changed files
with
1,243 additions
and
15 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
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,93 @@ | ||
package vuls2 | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/future-architect/vuls/config" | ||
"github.com/future-architect/vuls/logging" | ||
"github.com/pkg/errors" | ||
"golang.org/x/xerrors" | ||
|
||
db "github.com/MaineK00n/vuls2/pkg/db/common" | ||
"github.com/MaineK00n/vuls2/pkg/db/fetch" | ||
) | ||
|
||
var ( | ||
// DefaultGHCRRepository is GitHub Container Registry for vuls2 db | ||
DefaultGHCRRepository = fmt.Sprintf("%s:%d", "ghcr.io/vulsio/vuls-nightly-db", db.SchemaVersion) | ||
|
||
// DefaultPath is the path for vuls2 db file | ||
DefaultPath = func() string { | ||
wd, _ := os.Getwd() | ||
return filepath.Join(wd, "vuls.db") | ||
}() | ||
) | ||
|
||
func newDBConnection(vuls2Conf config.Vuls2Conf, noProgress bool) (db.DB, error) { | ||
willDownload, err := shouldDownload(vuls2Conf, time.Now()) | ||
if err != nil { | ||
return nil, xerrors.Errorf("Failed to check whether to download vuls2 db. err: %w", err) | ||
} | ||
|
||
if willDownload { | ||
logging.Log.Infof("Fetching vuls2 db. repository: %s", vuls2Conf.Repository) | ||
if err := fetch.Fetch(fetch.WithRepository(vuls2Conf.Repository), fetch.WithDBPath(vuls2Conf.Path), fetch.WithNoProgress(noProgress)); err != nil { | ||
return nil, xerrors.Errorf("Failed to fetch vuls2 db. err: %w", err) | ||
} | ||
} | ||
|
||
dbc, err := (&db.Config{ | ||
Type: "boltdb", | ||
Path: vuls2Conf.Path, | ||
}).New() | ||
if err != nil { | ||
return nil, xerrors.Errorf("Failed to new vuls2 db connection. err: %w", err) | ||
} | ||
|
||
return dbc, nil | ||
} | ||
|
||
func shouldDownload(vuls2Conf config.Vuls2Conf, now time.Time) (bool, error) { | ||
if _, err := os.Stat(vuls2Conf.Path); err != nil { | ||
if errors.Is(err, os.ErrNotExist) { | ||
if vuls2Conf.SkipUpdate { | ||
return false, xerrors.Errorf("%s not found, cannot skip update", vuls2Conf.Path) | ||
} | ||
return true, nil | ||
} | ||
return false, xerrors.Errorf("Failed to stat vuls2 db file. err: %w", err) | ||
} | ||
|
||
if vuls2Conf.SkipUpdate { | ||
return false, nil | ||
} | ||
|
||
dbc, err := (&db.Config{ | ||
Type: "boltdb", | ||
Path: vuls2Conf.Path, | ||
}).New() | ||
if err != nil { | ||
return false, xerrors.Errorf("Failed to new vuls2 db connection. path: %s, err: %w", vuls2Conf.Path, err) | ||
} | ||
|
||
if err := dbc.Open(); err != nil { | ||
return false, xerrors.Errorf("Failed to open vuls2 db. path: %s, err: %w", vuls2Conf.Path, err) | ||
} | ||
defer dbc.Close() | ||
|
||
metadata, err := dbc.GetMetadata() | ||
if err != nil { | ||
return false, xerrors.Errorf("Failed to get vuls2 db metadata. path: %s, err: %w", vuls2Conf.Path, err) | ||
} | ||
if metadata == nil { | ||
return false, xerrors.Errorf("Unexpected Vuls2 db metadata. metadata: nil,. path: %s", vuls2Conf.Path) | ||
} | ||
|
||
if metadata.Downloaded != nil && now.Before((*metadata.Downloaded).Add(1*time.Hour)) { | ||
return false, nil | ||
} | ||
return metadata.LastModified.Add(6 * time.Hour).Before(now), 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package vuls2 | ||
|
||
import ( | ||
"path/filepath" | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
"golang.org/x/xerrors" | ||
|
||
"github.com/MaineK00n/vuls2/pkg/db/common" | ||
"github.com/MaineK00n/vuls2/pkg/db/common/types" | ||
"github.com/future-architect/vuls/config" | ||
) | ||
|
||
func Test_shouldDownload(t *testing.T) { | ||
type args struct { | ||
vuls2Conf config.Vuls2Conf | ||
now time.Time | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
metadata *types.Metadata | ||
want bool | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "no db file", | ||
args: args{ | ||
vuls2Conf: config.Vuls2Conf{}, | ||
now: *parse("2024-01-02T00:00:00Z"), | ||
}, | ||
want: true, | ||
}, | ||
{ | ||
name: "no db file, but skip update", | ||
args: args{ | ||
vuls2Conf: config.Vuls2Conf{ | ||
SkipUpdate: true, | ||
}, | ||
now: *parse("2024-01-02T00:00:00Z"), | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "just created", | ||
args: args{ | ||
vuls2Conf: config.Vuls2Conf{}, | ||
now: *parse("2024-01-02T00:00:00Z"), | ||
}, | ||
metadata: &types.Metadata{ | ||
LastModified: *parse("2024-01-02T00:00:00Z"), | ||
Downloaded: parse("2024-01-02T00:00:00Z"), | ||
SchemaVersion: common.SchemaVersion, | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "8 hours old", | ||
args: args{ | ||
vuls2Conf: config.Vuls2Conf{}, | ||
now: *parse("2024-01-02T08:00:00Z"), | ||
}, | ||
metadata: &types.Metadata{ | ||
LastModified: *parse("2024-01-02T00:00:00Z"), | ||
Downloaded: parse("2024-01-02T00:00:00Z"), | ||
SchemaVersion: common.SchemaVersion, | ||
}, | ||
want: true, | ||
}, | ||
{ | ||
name: "8 hours old, but skip update", | ||
args: args{ | ||
vuls2Conf: config.Vuls2Conf{ | ||
SkipUpdate: true, | ||
}, | ||
now: *parse("2024-01-02T08:00:00Z"), | ||
}, | ||
metadata: &types.Metadata{ | ||
LastModified: *parse("2024-01-02T00:00:00Z"), | ||
Downloaded: parse("2024-01-02T00:00:00Z"), | ||
SchemaVersion: common.SchemaVersion, | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "8 hours old, but download recently", | ||
args: args{ | ||
vuls2Conf: config.Vuls2Conf{}, | ||
now: *parse("2024-01-02T08:00:00Z"), | ||
}, | ||
metadata: &types.Metadata{ | ||
LastModified: *parse("2024-01-02T00:00:00Z"), | ||
Downloaded: parse("2024-01-02T07:30:00Z"), | ||
SchemaVersion: common.SchemaVersion, | ||
}, | ||
want: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
d := t.TempDir() | ||
tt.args.vuls2Conf.Path = filepath.Join(d, "vuls.db") | ||
if tt.metadata != nil { | ||
if err := putMetadata(*tt.metadata, tt.args.vuls2Conf.Path); err != nil { | ||
t.Errorf("putMetadata err = %v", err) | ||
return | ||
} | ||
} | ||
got, err := shouldDownload(tt.args.vuls2Conf, tt.args.now) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("shouldDownload() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("shouldDownload() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
|
||
} | ||
|
||
func putMetadata(metadata types.Metadata, path string) error { | ||
c := common.Config{ | ||
Type: "boltdb", | ||
Path: path, | ||
} | ||
dbc, err := c.New() | ||
if err != nil { | ||
return xerrors.Errorf("c.New(). err: %w", err) | ||
} | ||
if err := dbc.Open(); err != nil { | ||
return xerrors.Errorf("dbc.Open(). err: %w", err) | ||
} | ||
defer dbc.Close() | ||
if err := dbc.Initialize(); err != nil { | ||
return xerrors.Errorf("dbc.Initialize(). err: %w", err) | ||
} | ||
if err := dbc.PutMetadata(metadata); err != nil { | ||
return xerrors.Errorf("dbc.PutMetadata(). err: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func parse(date string) *time.Time { | ||
t, _ := time.Parse(time.RFC3339, date) | ||
return &t | ||
} |
Oops, something went wrong.