mirror of
https://github.com/aquasecurity/trivy.git
synced 2025-12-21 23:00:42 -08:00
fix(db): introduce db schema version (#221)
This commit is contained in:
24
pkg/db/db.go
24
pkg/db/db.go
@@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/aquasecurity/trivy/pkg/log"
|
"github.com/aquasecurity/trivy/pkg/log"
|
||||||
|
|
||||||
@@ -14,13 +15,17 @@ import (
|
|||||||
bolt "github.com/etcd-io/bbolt"
|
bolt "github.com/etcd-io/bbolt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
SchemaVersion = 1
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
db *bolt.DB
|
db *bolt.DB
|
||||||
dbDir string
|
dbDir string
|
||||||
)
|
)
|
||||||
|
|
||||||
type Operations interface {
|
type Operations interface {
|
||||||
SetVersion(string) error
|
SetVersion(int) error
|
||||||
Update(string, string, string, interface{}) error
|
Update(string, string, string, interface{}) error
|
||||||
BatchUpdate(func(*bolt.Tx) error) error
|
BatchUpdate(func(*bolt.Tx) error) error
|
||||||
PutNestedBucket(*bolt.Tx, string, string, string, interface{}) error
|
PutNestedBucket(*bolt.Tx, string, string, string, interface{}) error
|
||||||
@@ -67,19 +72,22 @@ func Reset() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetVersion() string {
|
func GetVersion() int {
|
||||||
var version string
|
|
||||||
value, err := Get("trivy", "metadata", "version")
|
value, err := Get("trivy", "metadata", "version")
|
||||||
if err != nil {
|
if err != nil || len(value) == 0 {
|
||||||
return ""
|
// initial run
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
if err = json.Unmarshal(value, &version); err != nil {
|
|
||||||
return ""
|
version, err := strconv.Atoi(string(value))
|
||||||
|
if err != nil {
|
||||||
|
// old trivy version
|
||||||
|
return 1
|
||||||
}
|
}
|
||||||
return version
|
return version
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dbc Config) SetVersion(version string) error {
|
func (dbc Config) SetVersion(version int) error {
|
||||||
err := dbc.Update("trivy", "metadata", "version", version)
|
err := dbc.Update("trivy", "metadata", "version", version)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return xerrors.Errorf("failed to save DB version: %w", err)
|
return xerrors.Errorf("failed to save DB version: %w", err)
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ type MockDBConfig struct {
|
|||||||
mock.Mock
|
mock.Mock
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_m *MockDBConfig) SetVersion(version string) error {
|
func (_m *MockDBConfig) SetVersion(version int) error {
|
||||||
ret := _m.Called(version)
|
ret := _m.Called(version)
|
||||||
return ret.Error(0)
|
return ret.Error(0)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ func CloneOrPull(url, repoPath string) (map[string]struct{}, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Need to refresh all vulnerabilities
|
// Need to refresh all vulnerabilities
|
||||||
if db.GetVersion() == "" {
|
if db.GetVersion() == 0 {
|
||||||
err = filepath.Walk(repoPath, func(path string, info os.FileInfo, err error) error {
|
err = filepath.Walk(repoPath, func(path string, info os.FileInfo, err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -21,8 +21,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Run(c *cli.Context) (err error) {
|
func Run(c *cli.Context) (err error) {
|
||||||
cliVersion := c.App.Version
|
|
||||||
|
|
||||||
if c.Bool("quiet") || c.Bool("no-progress") {
|
if c.Bool("quiet") || c.Bool("no-progress") {
|
||||||
utils.Quiet = true
|
utils.Quiet = true
|
||||||
}
|
}
|
||||||
@@ -87,7 +85,7 @@ func Run(c *cli.Context) (err error) {
|
|||||||
|
|
||||||
needRefresh := false
|
needRefresh := false
|
||||||
dbVersion := db.GetVersion()
|
dbVersion := db.GetVersion()
|
||||||
if dbVersion != "" && dbVersion != cliVersion {
|
if 0 < dbVersion && dbVersion < db.SchemaVersion {
|
||||||
if !refresh && !autoRefresh {
|
if !refresh && !autoRefresh {
|
||||||
return xerrors.New("Detected version update of trivy. Please try again with --refresh or --auto-refresh option")
|
return xerrors.New("Detected version update of trivy. Please try again with --refresh or --auto-refresh option")
|
||||||
}
|
}
|
||||||
@@ -114,7 +112,7 @@ func Run(c *cli.Context) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dbc := db.Config{}
|
dbc := db.Config{}
|
||||||
if err = dbc.SetVersion(cliVersion); err != nil {
|
if err = dbc.SetVersion(db.SchemaVersion); err != nil {
|
||||||
return xerrors.Errorf("unexpected error: %w", err)
|
return xerrors.Errorf("unexpected error: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ func BenchmarkUpdate(b *testing.B) {
|
|||||||
b.Run("NVD", func(b *testing.B) {
|
b.Run("NVD", func(b *testing.B) {
|
||||||
dbc := db.Config{}
|
dbc := db.Config{}
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
if err := dbc.SetVersion(""); err != nil {
|
if err := dbc.SetVersion(db.SchemaVersion); err != nil {
|
||||||
b.Fatal(err)
|
b.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := Update([]string{vulnerability.Nvd}); err != nil {
|
if err := Update([]string{vulnerability.Nvd}); err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user