mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-12-06 07:34:00 +00:00
Each trivy scan updated the vulnerability database when the database was stale. Six concurrent rescans then downloaded it at the same time. A download took about 11 seconds alone and 38 seconds under contention. Five of six trivy processes crashed with SIGBUS, because they read a database that another process replaced. The other scans hit their timeout. Scans now run with --skip-db-update. A refresh loop updates the database every 6 hours (trivy.db_update_interval). Scans hold a read lock and an update holds the write lock, so an update never replaces the database under a running scan. A scan that finds no database downloads it once before it scans. The grype refresh loop moves to the dbrefresh package, which both scanners use.
116 lines
3.2 KiB
Go
116 lines
3.2 KiB
Go
package trivy
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/lukaszraczylo/gohoarder/pkg/config"
|
|
"github.com/lukaszraczylo/gohoarder/pkg/metadata"
|
|
"github.com/lukaszraczylo/gohoarder/pkg/scanner/scannertest"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// withDB returns cfg with a cache dir that already holds a database.
|
|
func withDB(t *testing.T, cfg config.TrivyConfig) config.TrivyConfig {
|
|
t.Helper()
|
|
cfg.CacheDB = t.TempDir()
|
|
require.NoError(t, os.MkdirAll(filepath.Join(cfg.CacheDB, "db"), 0o750))
|
|
require.NoError(t, os.WriteFile(filepath.Join(cfg.CacheDB, "db", "trivy.db"), nil, 0o600))
|
|
return cfg
|
|
}
|
|
|
|
// fakeTrivy downloads a database on `trivy image` and scans clean otherwise, logging each step.
|
|
func fakeTrivy(t *testing.T, scanSleep string) (logFile string) {
|
|
t.Helper()
|
|
logFile = filepath.Join(t.TempDir(), "log")
|
|
scannertest.Script(t, "trivy", `
|
|
if [ "$1" = image ]; then
|
|
echo update >> '`+logFile+`'
|
|
mkdir -p "$TRIVY_CACHE_DIR/db" && : > "$TRIVY_CACHE_DIR/db/trivy.db"
|
|
exit 0
|
|
fi
|
|
echo scan-start >> '`+logFile+`'
|
|
sleep `+scanSleep+`
|
|
echo scan-end >> '`+logFile+`'
|
|
echo '{"Results":[]}'`)
|
|
return logFile
|
|
}
|
|
|
|
func readLog(t *testing.T, logFile string) []string {
|
|
t.Helper()
|
|
data, err := os.ReadFile(logFile) // #nosec G304 -- test temp file
|
|
require.NoError(t, err)
|
|
return strings.Fields(string(data))
|
|
}
|
|
|
|
func TestScansDownloadAMissingDatabaseOnce(t *testing.T) {
|
|
logFile := fakeTrivy(t, "0")
|
|
s := New(config.TrivyConfig{CacheDB: t.TempDir()})
|
|
|
|
var wg sync.WaitGroup
|
|
for range 4 {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
res, err := s.Scan(context.Background(), "pypi", "p", "1", "/x/p.whl")
|
|
if assert.NoError(t, err) {
|
|
assert.Equal(t, metadata.ScanStatusClean, res.Status)
|
|
}
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
|
|
lines := readLog(t, logFile)
|
|
assert.Equal(t, "update", lines[0])
|
|
assert.Equal(t, 1, strings.Count(strings.Join(lines, " "), "update"))
|
|
}
|
|
|
|
// Concurrent trivy processes replacing the database under running scans crashed them with SIGBUS.
|
|
func TestUpdateWaitsForRunningScans(t *testing.T) {
|
|
logFile := fakeTrivy(t, "0.3")
|
|
s := New(withDB(t, config.TrivyConfig{}))
|
|
|
|
done := make(chan struct{})
|
|
go func() {
|
|
defer close(done)
|
|
_, err := s.Scan(context.Background(), "pypi", "p", "1", "/x/p.whl")
|
|
assert.NoError(t, err)
|
|
}()
|
|
require.Eventually(t, func() bool {
|
|
data, _ := os.ReadFile(logFile) // #nosec G304 -- test temp file
|
|
return strings.Contains(string(data), "scan-start")
|
|
}, 5*time.Second, 5*time.Millisecond)
|
|
require.NoError(t, s.UpdateDatabase(context.Background()))
|
|
<-done
|
|
|
|
assert.Equal(t, []string{"scan-start", "scan-end", "update"}, readLog(t, logFile))
|
|
}
|
|
|
|
func TestRefreshDatabaseRunsOnInterval(t *testing.T) {
|
|
logFile := fakeTrivy(t, "0")
|
|
stop := make(chan struct{})
|
|
exited := make(chan struct{})
|
|
|
|
go func() {
|
|
New(config.TrivyConfig{CacheDB: t.TempDir()}).RefreshDatabase(stop, 20*time.Millisecond, true)
|
|
close(exited)
|
|
}()
|
|
|
|
require.Eventually(t, func() bool {
|
|
data, _ := os.ReadFile(logFile) // #nosec G304 -- test temp file
|
|
return strings.Count(string(data), "update\n") >= 3
|
|
}, 5*time.Second, 10*time.Millisecond)
|
|
close(stop)
|
|
select {
|
|
case <-exited:
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("refresh loop did not stop")
|
|
}
|
|
}
|