mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-26 06:37:07 +00:00
feat(security): size the rescan from measured scan times
The confirmation for a full rescan used a fixed guess of 10 seconds per artifact. Scans now record their wall time in scan_results. The new GET /api/admin/rescan/estimate returns the artifact count of each scope and the time at the configured concurrency, from the average of recent scans. While a rescan runs, the status reports elapsed time and an ETA, and the panel shows the live rate.
This commit is contained in:
@@ -329,6 +329,7 @@ type RescanStatus struct {
|
||||
Queued int64
|
||||
Done int64
|
||||
Failed int64
|
||||
Elapsed time.Duration
|
||||
Running bool
|
||||
}
|
||||
|
||||
@@ -436,6 +437,38 @@ dispatch:
|
||||
Msg("Forced rescan completed")
|
||||
}
|
||||
|
||||
// scanDurationSample is how many recent scans the estimate averages.
|
||||
const scanDurationSample = 200
|
||||
|
||||
// RescanEstimate sizes a forced rescan before it starts.
|
||||
type RescanEstimate struct {
|
||||
// AvgScan is the mean wall time of recent scans; zero when none was recorded.
|
||||
AvgScan time.Duration
|
||||
Vulnerable int
|
||||
All int
|
||||
Concurrency int
|
||||
}
|
||||
|
||||
// Estimate counts the artifacts each scope would queue and averages recent scan durations.
|
||||
func (w *RescanWorker) Estimate(ctx context.Context) (RescanEstimate, error) {
|
||||
est := RescanEstimate{Concurrency: w.concurrency}
|
||||
all, err := w.forcedPackages(ctx, RescanAll)
|
||||
if err != nil {
|
||||
return est, err
|
||||
}
|
||||
vulnerable, err := w.forcedPackages(ctx, RescanVulnerable)
|
||||
if err != nil {
|
||||
return est, err
|
||||
}
|
||||
est.All, est.Vulnerable = len(all), len(vulnerable)
|
||||
if src, ok := w.metadataStore.(metadata.ScanDurationSource); ok {
|
||||
if est.AvgScan, err = src.AverageScanDuration(ctx, scanDurationSample); err != nil {
|
||||
return est, fmt.Errorf("average scan duration: %w", err)
|
||||
}
|
||||
}
|
||||
return est, nil
|
||||
}
|
||||
|
||||
// Status reports the latest forced rescan; the zero value means none ran yet.
|
||||
func (w *RescanWorker) Status() RescanStatus {
|
||||
w.runMu.Lock()
|
||||
@@ -444,7 +477,12 @@ func (w *RescanWorker) Status() RescanStatus {
|
||||
if run == nil {
|
||||
return RescanStatus{}
|
||||
}
|
||||
elapsed := run.finished.Sub(run.started)
|
||||
if run.running {
|
||||
elapsed = time.Since(run.started)
|
||||
}
|
||||
return RescanStatus{
|
||||
Elapsed: elapsed,
|
||||
StartedAt: run.started,
|
||||
FinishedAt: run.finished,
|
||||
Scope: run.scope,
|
||||
|
||||
@@ -208,7 +208,7 @@ func TestRescanNowIgnoresActiveWindowAndFreshness(t *testing.T) {
|
||||
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.Equal(t, RescanStatus{StartedAt: st.StartedAt, FinishedAt: st.FinishedAt, Elapsed: st.FinishedAt.Sub(st.StartedAt), Scope: RescanAll, Queued: 2, Done: 2}, st)
|
||||
assert.False(t, st.FinishedAt.IsZero())
|
||||
}
|
||||
|
||||
@@ -263,3 +263,50 @@ func TestRescanNowCountsFailures(t *testing.T) {
|
||||
assert.Equal(t, int64(1), st.Done)
|
||||
assert.Equal(t, int64(1), st.Failed)
|
||||
}
|
||||
|
||||
// durationStore adds the recorded-duration average a gorm store provides.
|
||||
type durationStore struct {
|
||||
*rescanStore
|
||||
avg time.Duration
|
||||
}
|
||||
|
||||
func (d durationStore) AverageScanDuration(context.Context, int) (time.Duration, error) {
|
||||
return d.avg, nil
|
||||
}
|
||||
|
||||
func TestEstimateCountsScopesAndAveragesRecordedScans(t *testing.T) {
|
||||
now := time.Now()
|
||||
a, b := pkgRow("npm", "a", "1.0.0", now), pkgRow("npm", "b", "1.0.0", now)
|
||||
a.ID, b.ID = "id-a", "id-b"
|
||||
meta := pkgRow("npm", "a", "list", now)
|
||||
store := &rescanStore{
|
||||
results: map[string]*metadata.ScanResult{}, packages: []*metadata.Package{a, b, meta},
|
||||
summaries: []*metadata.ScanSummary{{PackageID: "id-a", Total: 1}},
|
||||
}
|
||||
mgr := newTestManager(t, store)
|
||||
w := NewRescanWorker(mgr, durationStore{store, 1500 * time.Millisecond}, time.Hour, WithConcurrency(3))
|
||||
|
||||
est, err := w.Estimate(context.Background())
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, RescanEstimate{AvgScan: 1500 * time.Millisecond, Vulnerable: 1, All: 2, Concurrency: 3}, est)
|
||||
}
|
||||
|
||||
func TestScanRecordsItsDuration(t *testing.T) {
|
||||
store := &rescanStore{results: map[string]*metadata.ScanResult{}}
|
||||
mgr := newTestManager(t, store)
|
||||
mgr.RegisterScanner(&slowScanner{delay: 30 * time.Millisecond})
|
||||
|
||||
require.NoError(t, mgr.ScanPackage(context.Background(), "npm", "a", "1.0.0", "/tmp/dummy"))
|
||||
saved := store.results[scanKey("npm", "a", "1.0.0")]
|
||||
require.NotNil(t, saved)
|
||||
assert.GreaterOrEqual(t, saved.DurationMs, 30)
|
||||
}
|
||||
|
||||
type slowScanner struct{ delay time.Duration }
|
||||
|
||||
func (s *slowScanner) Name() string { return "slow" }
|
||||
func (s *slowScanner) Scan(_ context.Context, registry, name, version, _ string) (*metadata.ScanResult, error) {
|
||||
time.Sleep(s.delay)
|
||||
return &metadata.ScanResult{Registry: registry, PackageName: name, PackageVersion: version, Scanner: "slow", ScannedAt: time.Now(), Status: metadata.ScanStatusClean}, nil
|
||||
}
|
||||
func (s *slowScanner) Health(context.Context) error { return nil }
|
||||
|
||||
@@ -309,6 +309,7 @@ func (m *Manager) scan(ctx context.Context, registry, packageName, version strin
|
||||
key := scanKey(registry, packageName, version)
|
||||
m.markInflight(key, 1)
|
||||
defer m.markInflight(key, -1)
|
||||
started := time.Now()
|
||||
|
||||
log.Info().
|
||||
Str("registry", registry).
|
||||
@@ -429,6 +430,7 @@ func (m *Manager) scan(ctx context.Context, registry, packageName, version strin
|
||||
mergedResult := m.mergeResults(scanResults, scannerNames)
|
||||
// The store finds the package by its cache version, not the release version.
|
||||
mergedResult.PackageVersion = version
|
||||
mergedResult.DurationMs = int(time.Since(started).Milliseconds())
|
||||
|
||||
// Save consolidated result to metadata store
|
||||
if err := m.metadataStore.SaveScanResult(ctx, mergedResult); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user