mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-26 06:37:07 +00:00
feat(security): add an admin-triggered rescan
POST /api/admin/rescan queues a background rescan of the cached artifacts with scope vulnerable or all. It ignores the active window and the age of the last scan, runs on the server context and uses the rescan worker concurrency. A second request while a run is active gets 409. GET /api/admin/rescan/status reports queued, done, failed and running. The System page gets a Rescan section with progress and a confirmation for the full rescan.
This commit is contained in:
@@ -14,8 +14,9 @@ import (
|
||||
|
||||
// rescanStore serves a fixed package list and keeps saved scan results.
|
||||
type rescanStore struct {
|
||||
results map[string]*metadata.ScanResult
|
||||
packages []*metadata.Package
|
||||
results map[string]*metadata.ScanResult
|
||||
packages []*metadata.Package
|
||||
summaries []*metadata.ScanSummary
|
||||
stubMetadataStore
|
||||
rmu sync.Mutex
|
||||
}
|
||||
@@ -24,6 +25,10 @@ func (s *rescanStore) ListPackages(context.Context, *metadata.ListOptions) ([]*m
|
||||
return s.packages, nil
|
||||
}
|
||||
|
||||
func (s *rescanStore) GetScanSummaries(context.Context, []string, bool) ([]*metadata.ScanSummary, error) {
|
||||
return s.summaries, nil
|
||||
}
|
||||
|
||||
func (s *rescanStore) GetScanResult(_ context.Context, registry, name, version string) (*metadata.ScanResult, error) {
|
||||
s.rmu.Lock()
|
||||
defer s.rmu.Unlock()
|
||||
@@ -159,3 +164,102 @@ func TestRescanConcurrencyFloor(t *testing.T) {
|
||||
}
|
||||
assert.Equal(t, 1, NewRescanWorker(nil, nil, time.Hour).concurrency)
|
||||
}
|
||||
|
||||
// forcedFixture builds a worker over pkgs whose file source calls gate before each scan.
|
||||
func forcedFixture(t *testing.T, pkgs []*metadata.Package, summaries []*metadata.ScanSummary, gate func(), opts ...RescanOption) (*RescanWorker, *countingScanner) {
|
||||
t.Helper()
|
||||
store := &rescanStore{results: map[string]*metadata.ScanResult{}, packages: pkgs, summaries: summaries}
|
||||
mgr := newTestManager(t, store)
|
||||
cs := &countingScanner{calls: map[string]int{}}
|
||||
mgr.RegisterScanner(cs)
|
||||
mgr.SetFileSource(func(context.Context, string, string, string) (string, func(), error) {
|
||||
if gate != nil {
|
||||
gate()
|
||||
}
|
||||
return "/tmp/dummy", func() {}, nil
|
||||
})
|
||||
return NewRescanWorker(mgr, store, time.Hour, opts...), cs
|
||||
}
|
||||
|
||||
func waitIdle(t *testing.T, w *RescanWorker) RescanStatus {
|
||||
t.Helper()
|
||||
require.Eventually(t, func() bool { return !w.Status().Running }, 5*time.Second, 5*time.Millisecond)
|
||||
return w.Status()
|
||||
}
|
||||
|
||||
func TestRescanNowIgnoresActiveWindowAndFreshness(t *testing.T) {
|
||||
now := time.Now()
|
||||
pkgs := []*metadata.Package{
|
||||
pkgRow("npm", "cold", "1.0.0", now.Add(-90*24*time.Hour)),
|
||||
pkgRow("go", "example.com/m/@v/v1.0.0.zip", "v1.0.0", now),
|
||||
pkgRow("go", "example.com/m/@v/v1.0.0.mod", "v1.0.0", now),
|
||||
}
|
||||
w, cs := forcedFixture(t, pkgs, nil, nil, WithActiveWindow(time.Hour), WithConcurrency(2))
|
||||
// A fresh result would make the periodic cycle skip this package.
|
||||
w.metadataStore.(*rescanStore).results[scanKey("npm", "cold", "1.0.0")] = &metadata.ScanResult{ScannedAt: now}
|
||||
pkgs[0].SecurityScanned = true
|
||||
|
||||
queued, err := w.RescanNow(context.Background(), RescanAll)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 2, queued)
|
||||
st := waitIdle(t, w)
|
||||
|
||||
assert.Equal(t, map[string]int{
|
||||
scanKey("npm", "cold", "1.0.0"): 1,
|
||||
scanKey("go", "example.com/m/@v/v1.0.0.zip", "v1.0.0"): 1,
|
||||
}, cs.snapshot())
|
||||
assert.Equal(t, RescanStatus{StartedAt: st.StartedAt, FinishedAt: st.FinishedAt, Scope: RescanAll, Queued: 2, Done: 2}, st)
|
||||
assert.False(t, st.FinishedAt.IsZero())
|
||||
}
|
||||
|
||||
func TestRescanNowVulnerableScopeOnlyTakesPackagesWithFindings(t *testing.T) {
|
||||
now := time.Now()
|
||||
a, b, c := pkgRow("npm", "a", "1.0.0", now), pkgRow("npm", "b", "1.0.0", now), pkgRow("npm", "c", "1.0.0", now)
|
||||
a.ID, b.ID, c.ID = "id-a", "id-b", "id-c"
|
||||
summaries := []*metadata.ScanSummary{{PackageID: "id-a", Total: 3}, {PackageID: "id-b", Total: 0}}
|
||||
w, cs := forcedFixture(t, []*metadata.Package{a, b, c}, summaries, nil)
|
||||
|
||||
queued, err := w.RescanNow(context.Background(), RescanVulnerable)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 1, queued)
|
||||
waitIdle(t, w)
|
||||
assert.Equal(t, map[string]int{scanKey("npm", "a", "1.0.0"): 1}, cs.snapshot())
|
||||
}
|
||||
|
||||
func TestRescanNowRefusesASecondRunWhileActive(t *testing.T) {
|
||||
release := make(chan struct{})
|
||||
entered := make(chan struct{}, 1)
|
||||
w, _ := forcedFixture(t, []*metadata.Package{pkgRow("npm", "a", "1.0.0", time.Now())}, nil, func() {
|
||||
select {
|
||||
case entered <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
<-release
|
||||
})
|
||||
|
||||
_, err := w.RescanNow(context.Background(), RescanAll)
|
||||
require.NoError(t, err)
|
||||
<-entered
|
||||
assert.True(t, w.Status().Running)
|
||||
_, err = w.RescanNow(context.Background(), RescanAll)
|
||||
assert.ErrorIs(t, err, ErrRescanRunning)
|
||||
|
||||
close(release)
|
||||
waitIdle(t, w)
|
||||
_, err = w.RescanNow(context.Background(), RescanAll)
|
||||
assert.NoError(t, err, "a finished run frees the slot")
|
||||
waitIdle(t, w)
|
||||
}
|
||||
|
||||
func TestRescanNowCountsFailures(t *testing.T) {
|
||||
pkgs := []*metadata.Package{pkgRow("npm", "ok", "1.0.0", time.Now()), pkgRow("npm", "nokey", "1.0.0", time.Now())}
|
||||
pkgs[1].StorageKey = ""
|
||||
w, _ := forcedFixture(t, pkgs, nil, nil)
|
||||
|
||||
_, err := w.RescanNow(context.Background(), RescanAll)
|
||||
require.NoError(t, err)
|
||||
st := waitIdle(t, w)
|
||||
assert.Equal(t, int64(2), st.Queued)
|
||||
assert.Equal(t, int64(1), st.Done)
|
||||
assert.Equal(t, int64(1), st.Failed)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user