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

Clean trivy cache before each cycle #2168

Merged
merged 6 commits into from
May 21, 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
35 changes: 34 additions & 1 deletion internal/vulnerability/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package vulnerability

import (
"context"
"errors"
"time"

"github.com/aquasecurity/trivy/pkg/commands/artifact"
Expand All @@ -33,7 +34,13 @@ type VulnerabilityRunner struct {
}

func NewVulnerabilityRunner(log *logp.Logger) (VulnerabilityRunner, error) {
ctx := context.Background()
log.Debug("NewVulnerabilityRunner: New")

if err := clearTrivyCache(ctx, log); err != nil {
log.Errorf("error during runner cache clearing %s", err.Error())
}

opts := flag.Options{
GlobalOptions: flag.GlobalOptions{
// TODO: Make configurable
Expand All @@ -55,7 +62,7 @@ func NewVulnerabilityRunner(log *logp.Logger) (VulnerabilityRunner, error) {
},
}

runner, err := artifact.NewRunner(context.Background(), opts)
runner, err := artifact.NewRunner(ctx, opts)
if err != nil {
log.Error("NewVulnerabilityRunner: NewRunner error: ", err)
return VulnerabilityRunner{}, err
Expand All @@ -70,3 +77,29 @@ func NewVulnerabilityRunner(log *logp.Logger) (VulnerabilityRunner, error) {
func (f VulnerabilityRunner) GetRunner() artifact.Runner {
return f.runner
}

func clearTrivyCache(ctx context.Context, log *logp.Logger) error {
log.Info("Starting VulnerabilityRunner.ClearCache")
defer log.Info("Ending VulnerabilityRunner.ClearCache")

// These are the three available cli settings for clean/reset translated to flag.Options object.
// {CacheOptions: flag.CacheOptions{ClearCache: true}},
// {DBOptions: flag.DBOptions{Reset: true}},
// {MisconfOptions: flag.MisconfOptions{ResetPolicyBundle: true}},
// In our case we will use only the ClearCache option.

errs := make([]error, 0, 2)
r, err := artifact.NewRunner(ctx, flag.Options{CacheOptions: flag.CacheOptions{ClearCache: true}})
if err != nil {
if !errors.Is(err, artifact.SkipScan) {
errs = append(errs, err)
}
}
romulets marked this conversation as resolved.
Show resolved Hide resolved

// it should never go here (NewRunner should always return artifact.SkipScan and nil runner), but just in case it goes, lets close the runner.
if r != nil {
errs = append(errs, r.Close(ctx))
}

return errors.Join(errs...)
}
6 changes: 5 additions & 1 deletion internal/vulnerability/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ func NewVulnerabilityWorker(log *logp.Logger, c *config.Config, bdp dataprovider

func (f *VulnerabilityWorker) Run(ctx context.Context) {
f.log.Info("Starting VulnerabilityWorker.work")
defer f.runner.GetRunner().Close(ctx)
defer func() {
if err := f.runner.GetRunner().Close(ctx); err != nil {
f.log.Warnf("error during runner closing %s", err.Error())
}
}()

select {
case <-ctx.Done():
Expand Down
Loading