Skip to content

Commit

Permalink
try to clean up cache if updating grypeDB fails
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Bertschy <matthias.bertschy@gmail.com>
  • Loading branch information
matthyx committed Jul 13, 2023
1 parent 3c1acf1 commit 85811ed
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
12 changes: 11 additions & 1 deletion adapters/v1/grype.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,21 @@ func (g *GrypeAdapter) Ready(ctx context.Context) bool {
logger.L().Info("updating grype DB",
helpers.String("listingURL", g.dbConfig.ListingURL))
var err error
g.store, g.dbStatus, g.dbCloser, err = grype.LoadVulnerabilityDB(g.dbConfig, true)
newStore, newDbStatus, newDbCloser, err := grype.LoadVulnerabilityDB(g.dbConfig, true)
if err != nil {
logger.L().Ctx(ctx).Error("failed to update grype DB", helpers.Error(err))
if g.dbCloser != nil {
g.dbCloser.Close()
logger.L().Debug("closed DB")
}
err := tools.DeleteContents(g.dbConfig.DBRootDir)
logger.L().Debug("cleaned up cache", helpers.Error(err),
helpers.String("DBRootDir", g.dbConfig.DBRootDir))
return false
}
g.store = newStore
g.dbStatus = newDbStatus
g.dbCloser = newDbCloser
g.lastDbUpdate = now
logger.L().Info("grype DB updated")
return true
Expand Down
15 changes: 15 additions & 0 deletions internal/tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tools
import (
"encoding/json"
"os"
"path"
"regexp"
"runtime/debug"
"testing"
Expand Down Expand Up @@ -83,3 +84,17 @@ func FileToCVEManifest(path string) domain.CVEManifest {
}
return cve
}

func DeleteContents(dir string) error {
d, err := os.ReadDir(dir)
if err != nil {
return err
}
for _, c := range d {
err := os.RemoveAll(path.Join([]string{dir, c.Name()}...))
if err != nil {
return err
}
}
return nil
}

0 comments on commit 85811ed

Please sign in to comment.