mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-26 06:37:07 +00:00
Add a read-only /api/system endpoint with the effective scan policy, scanner health, rescan, cache and pre-warming settings. It exposes no credentials or paths. The package list now carries a verdict (cleared, held, bypassed or pending) with the reason and the applied bypass. /api/stats adds bypassed_packages. The vulnerability detail response adds the decision, a per-scanner severity breakdown and detected_by on each finding.
89 lines
2.7 KiB
Go
89 lines
2.7 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/gofiber/fiber/v3"
|
|
"github.com/lukaszraczylo/gohoarder/pkg/metadata"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
// trendPeriods maps the period query value of /api/stats/trend to days of history.
|
|
var trendPeriods = map[string]int{"30day": 30, "90day": 90, "1year": 365}
|
|
|
|
const defaultTrendPeriod = "30day"
|
|
|
|
// handleStatsTrend returns one snapshot per day for the period, oldest first.
|
|
// Vulnerability fields are null for days before vulnerability history starts.
|
|
func (a *App) handleStatsTrend(c fiber.Ctx) error {
|
|
period := c.Query("period", defaultTrendPeriod)
|
|
days, ok := trendPeriods[period]
|
|
if !ok {
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
|
"error": "invalid period, must be one of: 30day, 90day, 1year",
|
|
})
|
|
}
|
|
|
|
today := time.Now().UTC().Truncate(24 * time.Hour)
|
|
from := today.AddDate(0, 0, -(days - 1))
|
|
points, err := a.metadata.GetStatsTrend(c.Context(), from)
|
|
if err != nil && !errors.Is(err, metadata.ErrNotImplemented) {
|
|
log.Error().Err(err).Str("period", period).Msg("Failed to get stats trend")
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "failed to get stats trend"})
|
|
}
|
|
if points == nil {
|
|
points = []*metadata.StatsSnapshot{}
|
|
}
|
|
|
|
var vulnStart *string
|
|
for _, p := range points {
|
|
if p.VulnerablePackages != nil {
|
|
vulnStart = &p.Date
|
|
break
|
|
}
|
|
}
|
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
|
"period": period,
|
|
"from": from.Format(metadata.StatsSnapshotDateLayout),
|
|
"to": today.Format(metadata.StatsSnapshotDateLayout),
|
|
"vulnerability_history_start": vulnStart,
|
|
"points": points,
|
|
})
|
|
}
|
|
|
|
// countBlocked counts scanned artifacts the blocking policy refuses.
|
|
func (a *App) countBlocked(ctx context.Context) (int64, error) {
|
|
blocked, _, err := a.countVerdicts(ctx)
|
|
return blocked, err
|
|
}
|
|
|
|
// countVerdicts counts scanned artifacts that are held and that pass only because an admin bypass applies.
|
|
func (a *App) countVerdicts(ctx context.Context) (blocked, bypassed int64, err error) {
|
|
if !a.config.Security.Enabled || a.scanManager == nil {
|
|
return 0, 0, nil
|
|
}
|
|
decisions, err := a.scanManager.ScanDecisions(ctx, nil)
|
|
if err != nil {
|
|
return 0, 0, err
|
|
}
|
|
for _, d := range decisions {
|
|
if metadata.IsMetadataVersion(d.Summary.Version) {
|
|
continue
|
|
}
|
|
switch verdictOf(d) {
|
|
case VerdictHeld:
|
|
blocked++
|
|
case VerdictBypassed:
|
|
bypassed++
|
|
}
|
|
}
|
|
return blocked, bypassed, nil
|
|
}
|
|
|
|
// blockedCounterSetter is implemented by stores that snapshot blocked packages.
|
|
type blockedCounterSetter interface {
|
|
SetBlockedCounter(func(context.Context) (int64, error))
|
|
}
|