Skip to content

Modern Docker Management, Designed for Everyone

fix: store raw trivy reports in the database and fix patch-target pagination

Kyle Mendell53e7e82715bf
diff --git a/backend/internal/bootstrap/bootstrap.go b/backend/internal/bootstrap/bootstrap.go
index bcfb9f033..fdb4c1a4e 100644
--- a/backend/internal/bootstrap/bootstrap.go
+++ b/backend/internal/bootstrap/bootstrap.go
@@ -30,6 +30,7 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/internal/user"
"github.com/getarcaneapp/arcane/backend/v2/internal/variable"
"github.com/getarcaneapp/arcane/backend/v2/internal/volume"
+ "github.com/getarcaneapp/arcane/backend/v2/internal/vulnerability"
"github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/edge"
"github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/startup"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils"
@@ -163,17 +164,18 @@ type initializeStartupStateParams struct {
Config *config.Config
HTTPClient *http.Client
- Volume *volume.Module
- Settings *settings.SettingsService
- Environment *environment.EnvironmentService
- GitOpsSync *gitops.GitOpsSyncService
- Project *project.ProjectService
- Variable *variable.VariableService
- Docker *docker.DockerClientService
- Swarm *swarm.SwarmService
- Role *role.RoleService
- User *user.UserService
- ApiKey *apikey.ApiKeyService
+ Volume *volume.Module
+ Settings *settings.SettingsService
+ Environment *environment.EnvironmentService
+ GitOpsSync *gitops.GitOpsSyncService
+ Project *project.ProjectService
+ Variable *variable.VariableService
+ Docker *docker.DockerClientService
+ Swarm *swarm.SwarmService
+ Role *role.RoleService
+ User *user.UserService
+ ApiKey *apikey.ApiKeyService
+ Vulnerability *vulnerability.VulnerabilityService
}
func initializeStartupState(p initializeStartupStateParams) {
@@ -222,6 +224,7 @@ func initializeStartupState(p initializeStartupStateParams) {
slog.WarnContext(appCtx, "Failed to ensure local environment", "error", err)
}
initializeGitOpsStartupStateInternal(appCtx, p.GitOpsSync)
+ p.Vulnerability.ImportLegacyReportFiles(appCtx)
if p.Project != nil {
if err := p.Project.RecoverProjectRenameJournals(appCtx); err != nil {
slog.WarnContext(appCtx, "Failed to recover interrupted project rename operations on startup", "error", err)
diff --git a/backend/internal/imagepatch/handler.go b/backend/internal/imagepatch/handler.go
index 1c8d11911..fcbe7063e 100644
--- a/backend/internal/imagepatch/handler.go
+++ b/backend/internal/imagepatch/handler.go
@@ -34,8 +34,10 @@ type PatchImageOutput struct {
type ListPatchTargetsInput struct {
EnvironmentID string `path:"id" doc:"Environment ID"`
Search string `query:"search" doc:"Search query"`
- Start int `query:"start" doc:"Start offset"`
- Limit int `query:"limit" doc:"Limit"`
+ Sort string `query:"sort" doc:"Column to sort by"`
+ Order string `query:"order" default:"desc" doc:"Sort direction"`
+ Start int `query:"start" default:"0" doc:"Start offset"`
+ Limit int `query:"limit" default:"20" doc:"Limit"`
}
type ListPatchTargetsOutput struct {
@@ -124,7 +126,7 @@ func (h *ImagePatchHandler) PatchImage(ctx context.Context, input *PatchImageInp
// ListPatchTargets returns scanned images with fixable counts and latest patch runs.
func (h *ImagePatchHandler) ListPatchTargets(ctx context.Context, input *ListPatchTargetsInput) (*ListPatchTargetsOutput, error) {
- params := handlerutil.PaginationParams(input.Start, input.Limit, "", "", input.Search)
+ params := handlerutil.PaginationParams(input.Start, input.Limit, input.Sort, input.Order, input.Search)
targets, paginationResp, err := h.imagePatchService.ListPatchTargets(ctx, input.EnvironmentID, params)
if err != nil {
diff --git a/backend/internal/imagepatch/imagepatch.go b/backend/internal/imagepatch/imagepatch.go
index cb73cf13e..e31650d8d 100644
--- a/backend/internal/imagepatch/imagepatch.go
+++ b/backend/internal/imagepatch/imagepatch.go
@@ -38,7 +38,6 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/logging"
activitytypes "github.com/getarcaneapp/arcane/types/v2/activity"
"github.com/getarcaneapp/arcane/types/v2/imagepatch"
- vulntypes "github.com/getarcaneapp/arcane/types/v2/vulnerability"
)
// ImagePatchService patches image OS packages in place using the Copacetic
@@ -110,24 +109,18 @@ func (s *ImagePatchService) PatchImage(ctx context.Context, envID, imageID strin
// Resolve the stored scan report when patching from a scan.
mode := imagepatch.PatchModeUpdateAll
- reportPath := ""
+ var reportData []byte
if strings.TrimSpace(opts.ScanID) != "" {
// Scan records are keyed by image ID; refuse a scan belonging to a
// different image than the one selected by the route.
if opts.ScanID != imageInspect.ID {
return nil, common.ErrPatchScanImageMismatch
}
- var scan vulnerability.VulnerabilityScanRecord
- if err := s.db.WithContext(runCtx).First(&scan, "id = ?", opts.ScanID).Error; err != nil {
- return nil, common.ErrPatchScanReportUnavailable
- }
- if scan.ReportPath == nil || strings.TrimSpace(*scan.ReportPath) == "" {
- return nil, common.ErrPatchScanReportUnavailable
- }
- if exists, err := acfs.Exists(runCtx, filepath.Dir(*scan.ReportPath), "/"+filepath.Base(*scan.ReportPath)); err != nil || !exists {
+ var report vulnerability.VulnerabilityReportRecord
+ if err := s.db.WithContext(runCtx).First(&report, "image_id = ?", opts.ScanID).Error; err != nil || len(report.Data) == 0 {
return nil, common.ErrPatchScanReportUnavailable
}
- reportPath = *scan.ReportPath
+ reportData = []byte(report.Data)
mode = imagepatch.PatchModeReport
}
@@ -187,13 +180,13 @@ func (s *ImagePatchService) PatchImage(ctx context.Context, envID, imageID strin
"patchTarget", copaImageRef,
)
- go s.patchInBackgroundInternal(runCtx, record, opts, reportPath, copaImageRef, activityID)
+ go s.patchInBackgroundInternal(runCtx, record, opts, reportData, copaImageRef, activityID)
dto := record.ToDto()
return &dto, nil
}
-func (s *ImagePatchService) patchInBackgroundInternal(ctx context.Context, record *ImagePatchRecord, opts imagepatch.PatchOptions, reportPath, copaImageRef, activityID string) {
+func (s *ImagePatchService) patchInBackgroundInternal(ctx context.Context, record *ImagePatchRecord, opts imagepatch.PatchOptions, reportData []byte, copaImageRef, activityID string) {
select {
case s.patchSlot <- struct{}{}:
defer func() { <-s.patchSlot }()
@@ -203,6 +196,23 @@ func (s *ImagePatchService) patchInBackgroundInternal(ctx context.Context, recor
return
}
+ // Copa consumes the scan report as a file; materialize the stored report
+ // into a temp file for the duration of the run.
+ reportPath := ""
+ if len(reportData) > 0 {
+ reportDir, err := os.MkdirTemp("", "arcane-patch-report")
+ if err == nil {
+ reportPath = filepath.Join(reportDir, "report.json")
+ err = os.WriteFile(reportPath, reportData, 0o600)
+ defer os.RemoveAll(reportDir)
+ }
+ if err != nil {
+ s.finishPatchRecordInternal(ctx, record, imagepatch.PatchStatusFailed, err.Error(), nil, 0)
+ s.completePatchActivityInternal(ctx, activityID, false, err.Error())
+ return
+ }
+ }
+
startTime := time.Now()
timeoutSeconds := opts.TimeoutSeconds
@@ -503,37 +513,29 @@ func (s *ImagePatchService) ListPatchTargets(ctx context.Context, envID string,
}
// Only list images that are actionable (fixable vulnerabilities) or carry
- // patch history. NULL fixable_count means a pre-column scan record; keep it
- // visible until the next scan fills the count in.
+ // patch history.
patchedImageIDs := s.db.Model(&ImagePatchRecord{}).
Distinct().
Select("original_image_id").
Where("environment_id = ?", envID)
+ reportImageIDs := s.db.Model(&vulnerability.VulnerabilityReportRecord{}).Select("image_id")
q := s.db.WithContext(ctx).
Model(&vulnerability.VulnerabilityScanRecord{}).
- Where("status = ? AND report_path IS NOT NULL", vulnerability.ScanStatusCompleted).
- Where("(fixable_count > 0 OR fixable_count IS NULL OR id IN (?))", patchedImageIDs).
+ Where("status = ?", vulnerability.ScanStatusCompleted).
+ Where("id IN (?)", reportImageIDs).
+ Where("(fixable_count > 0 OR id IN (?))", patchedImageIDs).
Where("image_name NOT LIKE 'sha256:%' AND image_name NOT LIKE '%<none>%' AND image_name <> id")
if len(excludedNames) > 0 {
q = q.Where("image_name NOT IN ?", excludedNames)
}
- if term := strings.TrimSpace(params.Search); term != "" {
- q = q.Where("image_name LIKE ?", "%"+term+"%")
- }
+ q = pagination.ApplyLikeSearch(q, params.Search, "image_name LIKE ?")
- var totalItems int64
- if err := q.Count(&totalItems).Error; err != nil {
- return nil, pagination.Response{}, errors.WrapIf(err, "failed to count patch targets")
- }
-
- limit := params.Limit
- if limit <= 0 {
- limit = 20
- params.Limit = limit
+ if params.Sort == "" {
+ params.Sort = "scanTime"
}
-
var scans []vulnerability.VulnerabilityScanRecord
- if err := q.Order("scan_time DESC").Offset(params.Start).Limit(limit).Find(&scans).Error; err != nil {
+ paginationResp, err := pagination.PaginateAndSortDB(params, q, &scans)
+ if err != nil {
return nil, pagination.Response{}, errors.WrapIf(err, "failed to list patch targets")
}
@@ -547,7 +549,7 @@ func (s *ImagePatchService) ListPatchTargets(ctx context.Context, envID string,
target := imagepatch.PatchTarget{
ImageID: scan.ID,
ImageRef: scan.ImageName,
- FixableCount: fixableVulnerabilityCountInternal(ctx, scan),
+ FixableCount: mo.PointerToOption(scan.FixableCount).OrEmpty(),
TotalCount: scan.TotalCount,
ScanTime: scan.ScanTime,
}
@@ -570,7 +572,7 @@ func (s *ImagePatchService) ListPatchTargets(ctx context.Context, envID string,
targets = append(targets, target)
}
- return targets, pagination.BuildResponse(totalItems, totalItems, params), nil
+ return targets, paginationResp, nil
}
// patchedImageScanSummaryInternal looks up the scan of a completed patch's
@@ -593,42 +595,12 @@ func (s *ImagePatchService) patchedImageScanSummaryInternal(ctx context.Context,
}
return &imagepatch.PatchScanSummary{
Status: scan.Status,
- FixableCount: fixableVulnerabilityCountInternal(ctx, &scan),
+ FixableCount: mo.PointerToOption(scan.FixableCount).OrEmpty(),
TotalCount: scan.TotalCount,
ScanTime: scan.ScanTime,
}
}
-func fixableVulnerabilityCountInternal(ctx context.Context, scan *vulnerability.VulnerabilityScanRecord) int {
- if scan.FixableCount != nil {
- return *scan.FixableCount
- }
- if scan.ReportPath == nil || strings.TrimSpace(*scan.ReportPath) == "" {
- return 0
- }
- data, err := acfs.ReadFile(ctx, filepath.Dir(*scan.ReportPath), "/"+filepath.Base(*scan.ReportPath))
- if err != nil {
- return 0
- }
- var report vulntypes.TrivyReport
- if err := json.Unmarshal(data, &report); err != nil {
- return 0
- }
- count := 0
- for i := range report.Results {
- res := &report.Results[i]
- if res.Class != vulntypes.TrivyClassOSPackages {
- continue
- }
- for j := range res.Vulnerabilities {
- if strings.TrimSpace(res.Vulnerabilities[j].FixedVersion) != "" {
- count++
- }
- }
- }
- return count
-}
-
// PatchFlaggedImages patches every image whose latest completed vulnerability
// scan found fixable vulnerabilities and stored a raw report. Images that are
// themselves patch outputs are skipped (never patch a patched tag), as are
@@ -637,7 +609,8 @@ func fixableVulnerabilityCountInternal(ctx context.Context, scan *vulnerability.
func (s *ImagePatchService) PatchFlaggedImages(ctx context.Context, envID string, user common.User) (patched, skipped int, err error) {
var scans []vulnerability.VulnerabilityScanRecord
if err := s.db.WithContext(ctx).
- Where("status = ? AND total_count > 0 AND report_path IS NOT NULL", vulnerability.ScanStatusCompleted).
+ Where("status = ? AND fixable_count > 0", vulnerability.ScanStatusCompleted).
+ Where("id IN (?)", s.db.Model(&vulnerability.VulnerabilityReportRecord{}).Select("image_id")).
Where("image_name NOT LIKE 'sha256:%' AND image_name NOT LIKE '%<none>%' AND image_name <> id").
Find(&scans).Error; err != nil {
return 0, 0, errors.WrapIf(err, "failed to list vulnerability scans")
@@ -651,12 +624,6 @@ func (s *ImagePatchService) PatchFlaggedImages(ctx context.Context, envID string
for i := range scans {
scan := &scans[i]
- // Only patch images whose scan found at least one fixable vulnerability.
- if fixableVulnerabilityCountInternal(ctx, scan) == 0 {
- skipped++
- continue
- }
-
if _, isPatchOutput := patchedRefs[scan.ImageName]; isPatchOutput {
skipped++
continue
diff --git a/backend/internal/imagepatch/imagepatch_test.go b/backend/internal/imagepatch/imagepatch_test.go
index 974daa231..defeb1307 100644
--- a/backend/internal/imagepatch/imagepatch_test.go
+++ b/backend/internal/imagepatch/imagepatch_test.go
@@ -60,7 +60,7 @@ func TestListPatchTargets_ExcludesUntaggedImages(t *testing.T) {
dsn := fmt.Sprintf("file:image-patch-test-%d?mode=memory&cache=shared", time.Now().UnixNano())
gdb, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{})
require.NoError(t, err)
- require.NoError(t, gdb.AutoMigrate(&vulnerability.VulnerabilityScanRecord{}, &ImagePatchRecord{}))
+ require.NoError(t, gdb.AutoMigrate(&vulnerability.VulnerabilityScanRecord{}, &vulnerability.VulnerabilityReportRecord{}, &ImagePatchRecord{}))
db := &database.DB{DB: gdb}
svc := &ImagePatchService{
@@ -68,15 +68,15 @@ func TestListPatchTargets_ExcludesUntaggedImages(t *testing.T) {
dockerService: docker.NewDockerClientService(ctx, nil, &config.Config{DockerHost: "unix:///nonexistent-arcane-test.sock"}, nil),
}
- reportPath := "/data/vulnerability-reports/test.json"
fixable := 3
records := []vulnerability.VulnerabilityScanRecord{
- {ID: "sha256:aaa", ImageName: "nginx:latest", Status: vulnerability.ScanStatusCompleted, ScanTime: time.Now(), ReportPath: &reportPath, FixableCount: &fixable},
- {ID: "sha256:bbb", ImageName: "sha256:bbb", Status: vulnerability.ScanStatusCompleted, ScanTime: time.Now(), ReportPath: &reportPath, FixableCount: &fixable},
- {ID: "sha256:ccc", ImageName: "<none>:<none>", Status: vulnerability.ScanStatusCompleted, ScanTime: time.Now(), ReportPath: &reportPath, FixableCount: &fixable},
+ {ID: "sha256:aaa", ImageName: "nginx:latest", Status: vulnerability.ScanStatusCompleted, ScanTime: time.Now(), FixableCount: &fixable},
+ {ID: "sha256:bbb", ImageName: "sha256:bbb", Status: vulnerability.ScanStatusCompleted, ScanTime: time.Now(), FixableCount: &fixable},
+ {ID: "sha256:ccc", ImageName: "<none>:<none>", Status: vulnerability.ScanStatusCompleted, ScanTime: time.Now(), FixableCount: &fixable},
}
for i := range records {
require.NoError(t, db.Create(&records[i]).Error)
+ require.NoError(t, db.Create(&vulnerability.VulnerabilityReportRecord{ImageID: records[i].ID, Data: "{}"}).Error)
}
targets, _, err := svc.ListPatchTargets(ctx, "0", pagination.QueryParams{Limit: 20})
diff --git a/backend/internal/vulnerability/model.go b/backend/internal/vulnerability/model.go
index 7f2bb056d..6cb83752c 100644
--- a/backend/internal/vulnerability/model.go
+++ b/backend/internal/vulnerability/model.go
@@ -16,13 +16,13 @@ type VulnerabilityScanRecord struct {
ID string `json:"id" gorm:"primaryKey;type:text"`
// ImageName is the image name with tag (e.g., nginx:latest)
- ImageName string `json:"imageName" gorm:"column:image_name"`
+ ImageName string `json:"imageName" gorm:"column:image_name" sortable:"true"`
// Status is the status of the scan (pending, scanning, completed, failed)
Status string `json:"status" gorm:"column:status"`
// ScanTime is the timestamp when the scan was performed
- ScanTime time.Time `json:"scanTime" gorm:"column:scan_time"`
+ ScanTime time.Time `json:"scanTime" gorm:"column:scan_time" sortable:"true"`
// Duration is the duration of the scan in milliseconds
Duration int64 `json:"duration" gorm:"column:duration"`
@@ -44,19 +44,29 @@ type VulnerabilityScanRecord struct {
// ScannerVersion is the version of the scanner used
ScannerVersion string `json:"scannerVersion" gorm:"column:scanner_version"`
- // ReportPath is the stored raw Trivy JSON report used for image patching
- ReportPath *string `json:"reportPath,omitempty" gorm:"column:report_path"`
-
// FixableCount is the number of vulnerabilities with a known fix, stored at
// scan time so image patching can query it without parsing the JSON blob.
// NULL on records written before the column existed.
- FixableCount *int `json:"fixableCount,omitempty" gorm:"column:fixable_count"`
+ FixableCount *int `json:"fixableCount,omitempty" gorm:"column:fixable_count" sortable:"true"`
}
func (v *VulnerabilityScanRecord) TableName() string {
return "vulnerability_scans"
}
+// VulnerabilityReportRecord stores the raw Trivy JSON report used for
+// report-based image patching, one row per image, replaced on rescan.
+type VulnerabilityReportRecord struct {
+ ImageID string `json:"imageId" gorm:"column:image_id;primaryKey;type:text"`
+ Data string `json:"-" gorm:"column:data;type:text"`
+ CreatedAt time.Time `json:"createdAt" gorm:"column:created_at"`
+ UpdatedAt *time.Time `json:"updatedAt,omitempty" gorm:"column:updated_at"`
+}
+
+func (VulnerabilityReportRecord) TableName() string {
+ return "vulnerability_reports"
+}
+
// Scan status constants
const (
ScanStatusPending = "pending"
diff --git a/backend/internal/vulnerability/vulnerability_query.go b/backend/internal/vulnerability/vulnerability_query.go
index 34171d1b5..f03b6dfcb 100644
--- a/backend/internal/vulnerability/vulnerability_query.go
+++ b/backend/internal/vulnerability/vulnerability_query.go
@@ -35,7 +35,12 @@ func (s *VulnerabilityService) GetScanResult(ctx context.Context, imageID string
s.markStaleScanIfNeeded(ctx, &record)
- return s.convertRecordToResult(&record)
+ result := s.convertRecordToResult(&record)
+ var reportCount int64
+ if err := s.db.WithContext(ctx).Model(&VulnerabilityReportRecord{}).Where("image_id = ?", record.ID).Count(&reportCount).Error; err == nil {
+ result.HasReport = reportCount > 0
+ }
+ return result, nil
}
// GetScanSummary retrieves just the summary for an image (for list views)
diff --git a/backend/internal/vulnerability/vulnerability_store.go b/backend/internal/vulnerability/vulnerability_store.go
index cc60186f2..bb235cb35 100644
--- a/backend/internal/vulnerability/vulnerability_store.go
+++ b/backend/internal/vulnerability/vulnerability_store.go
@@ -8,7 +8,7 @@ import (
"fmt"
"io/fs"
"log/slog"
- "path/filepath"
+ "os"
"strings"
"time"
@@ -19,25 +19,98 @@ import (
"gorm.io/gorm"
"gorm.io/gorm/clause"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/vuln"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/dbutil"
"github.com/getarcaneapp/arcane/types/v2/vulnerability"
)
-// deleteStoredReportsInternal best-effort removes the raw report files of the
-// scan records matched by the given query, so report files never outlive their
+// deleteStoredReportsInternal best-effort removes the raw report rows of the
+// scan records matched by the given query, so reports never outlive their
// scan records. Failures are logged and never block record deletion.
func (s *VulnerabilityService) deleteStoredReportsInternal(ctx context.Context, query *gorm.DB) {
- var reportPaths []string
- if err := query.Where("report_path IS NOT NULL").Pluck("report_path", &reportPaths).Error; err != nil {
+ var imageIDs []string
+ if err := query.Pluck("id", &imageIDs).Error; err != nil {
slog.WarnContext(ctx, "failed to list stored vulnerability reports for cleanup", "error", err)
return
}
- for _, reportPath := range reportPaths {
- if strings.TrimSpace(reportPath) == "" {
- continue
+ if len(imageIDs) == 0 {
+ return
+ }
+ if err := s.db.WithContext(ctx).Where("image_id IN ?", imageIDs).Delete(&VulnerabilityReportRecord{}).Error; err != nil {
+ slog.WarnContext(ctx, "failed to delete stored vulnerability reports", "error", err)
+ }
+}
+
+// ImportLegacyReportFiles moves raw Trivy report files written by older
+// versions into the vulnerability_reports table, backfilling missing fixable
+// counts, then removes the on-disk report directory. No-op once imported;
+// failures are logged and never block startup.
+func (s *VulnerabilityService) ImportLegacyReportFiles(ctx context.Context) {
+ if s.db == nil {
+ return
+ }
+
+ rootDir, logical := ".", "/data/vulnerability-reports"
+ if _, err := os.Stat("/app/data"); err == nil {
+ rootDir, logical = "/app/data", "/vulnerability-reports"
+ }
+ exists, err := acfs.Exists(ctx, rootDir, logical)
+ if err != nil || !exists {
+ return
+ }
+
+ var scans []VulnerabilityScanRecord
+ if err := s.db.WithContext(ctx).Select("id", "fixable_count").Find(&scans).Error; err != nil {
+ slog.WarnContext(ctx, "failed to list scan records for report import", "error", err)
+ return
+ }
+
+ for i := range scans {
+ s.importLegacyReportInternal(ctx, &scans[i], rootDir, logical)
+ }
+
+ if err := acfs.RemoveAll(ctx, rootDir, logical); err != nil {
+ slog.WarnContext(ctx, "failed to remove legacy vulnerability report directory", "error", err)
+ }
+}
+
+func (s *VulnerabilityService) importLegacyReportInternal(ctx context.Context, scan *VulnerabilityScanRecord, rootDir, logical string) {
+ fileName := strings.ReplaceAll(scan.ID, ":", "_") + ".json"
+ data, err := acfs.ReadFile(ctx, rootDir, logical+"/"+fileName)
+ if err != nil {
+ if !errors.Is(err, fs.ErrNotExist) {
+ slog.WarnContext(ctx, "failed to read legacy vulnerability report", "imageId", scan.ID, "error", err)
+ }
+ return
+ }
+ report, err := vuln.DecodeReportFromBytes(data)
+ if err != nil || report == nil {
+ slog.WarnContext(ctx, "skipping unparseable legacy vulnerability report", "imageId", scan.ID, "error", err)
+ return
+ }
+ record := VulnerabilityReportRecord{ImageID: scan.ID, Data: string(data)}
+ if err := s.db.WithContext(ctx).Clauses(clause.OnConflict{
+ Columns: []clause.Column{{Name: "image_id"}},
+ UpdateAll: true,
+ }).Create(&record).Error; err != nil {
+ slog.WarnContext(ctx, "failed to import legacy vulnerability report", "imageId", scan.ID, "error", err)
+ return
+ }
+ if scan.FixableCount == nil {
+ count := 0
+ for r := range report.Results {
+ res := &report.Results[r]
+ if res.Class != vulnerability.TrivyClassOSPackages {
+ continue
+ }
+ for v := range res.Vulnerabilities {
+ if strings.TrimSpace(res.Vulnerabilities[v].FixedVersion) != "" {
+ count++
+ }
+ }
}
- if err := acfs.Remove(ctx, filepath.Dir(reportPath), "/"+filepath.Base(reportPath)); err != nil && !errors.Is(err, fs.ErrNotExist) {
- slog.WarnContext(ctx, "failed to delete stored vulnerability report", "path", reportPath, "error", err)
+ if err := s.db.WithContext(ctx).Model(&VulnerabilityScanRecord{}).Where("id = ?", scan.ID).Update("fixable_count", count).Error; err != nil {
+ slog.WarnContext(ctx, "failed to backfill fixable vulnerability count", "imageId", scan.ID, "error", err)
}
}
}
@@ -265,10 +338,6 @@ func (s *VulnerabilityService) saveScanResult(ctx context.Context, result *vulne
Error: errPtr,
ScannerVersion: result.ScannerVersion,
}
- if result.ReportPath != "" {
- record.ReportPath = &result.ReportPath
- }
-
fixable := 0
for i := range result.Vulnerabilities {
v := &result.Vulnerabilities[i]
@@ -286,7 +355,7 @@ func (s *VulnerabilityService) saveScanResult(ctx context.Context, result *vulne
}
// convertRecordToResult converts a database record to a ScanResult
-func (s *VulnerabilityService) convertRecordToResult(record *VulnerabilityScanRecord) (*vulnerability.ScanResult, error) {
+func (s *VulnerabilityService) convertRecordToResult(record *VulnerabilityScanRecord) *vulnerability.ScanResult {
result := &vulnerability.ScanResult{
ImageID: record.ID,
ImageName: record.ImageName,
@@ -308,11 +377,6 @@ func (s *VulnerabilityService) convertRecordToResult(record *VulnerabilityScanRe
result.Error = *record.Error
}
- if record.ReportPath != nil && *record.ReportPath != "" {
- result.ReportPath = *record.ReportPath
- result.HasReport = true
- }
-
// Parse vulnerabilities from JSON
if len(record.Vulnerabilities) > 0 && record.Vulnerabilities[0] != "" {
var vulns []vulnerability.Vulnerability
@@ -326,7 +390,7 @@ func (s *VulnerabilityService) convertRecordToResult(record *VulnerabilityScanRe
s.ensureSummary(result)
s.applyScanPhaseToResultInternal(result)
- return result, nil
+ return result
}
func (s *VulnerabilityService) ensureSummary(result *vulnerability.ScanResult) {
diff --git a/backend/internal/vulnerability/vulnerability_trivy.go b/backend/internal/vulnerability/vulnerability_trivy.go
index 6702c13fd..4073d4bc6 100644
--- a/backend/internal/vulnerability/vulnerability_trivy.go
+++ b/backend/internal/vulnerability/vulnerability_trivy.go
@@ -8,7 +8,6 @@ import (
"io"
"log/slog"
"os"
- "path/filepath"
"strings"
"sync"
"time"
@@ -28,8 +27,8 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/vuln"
"github.com/getarcaneapp/arcane/backend/v2/pkg/projects"
"github.com/getarcaneapp/arcane/types/v2/vulnerability"
- "go.getarcane.app/acfs"
"go.getarcane.app/sys/cgroup"
+ "gorm.io/gorm/clause"
)
type trivyRuntimeSecurityInternal struct {
@@ -364,43 +363,35 @@ func (s *VulnerabilityService) execTrivyScanInContainer(ctx context.Context, con
result := vulnerability.ConvertTrivyReportToScanResult(trivyReport, imageID, time.Now(), 0)
result.ScannerVersion = scannerVersion
- result.ReportPath = persistRawTrivyReportInternal(ctx, imageID, trivyOutput, stdoutFile)
+ result.HasReport = s.persistRawTrivyReportInternal(ctx, imageID, trivyOutput, stdoutFile)
return result, nil
}
-// persistRawTrivyReportInternal stores the raw Trivy JSON report on disk so it
-// can later drive report-based image patching. Returns the stored path, or ""
-// when nothing was persisted. Reports are keyed by image ID, so a re-scan
-// overwrites the previous report in place. When the output-file read produced
-// nothing, the stdout temp file the report was decoded from is used instead.
-func persistRawTrivyReportInternal(ctx context.Context, imageID string, raw []byte, stdoutFallback *os.File) string {
+// persistRawTrivyReportInternal stores the raw Trivy JSON report in the
+// database so it can later drive report-based image patching. Reports are
+// keyed by image ID, so a re-scan replaces the previous report. When the
+// output-file read produced nothing, the stdout temp file the report was
+// decoded from is used instead.
+func (s *VulnerabilityService) persistRawTrivyReportInternal(ctx context.Context, imageID string, raw []byte, stdoutFallback *os.File) bool {
if len(raw) == 0 && stdoutFallback != nil {
if _, err := stdoutFallback.Seek(0, io.SeekStart); err == nil {
raw, _ = io.ReadAll(stdoutFallback)
}
}
- if len(raw) == 0 {
- return ""
- }
-
- rootDir, logical := ".", "/data/vulnerability-reports"
- // Probes the fixed system path /app/data, which is not under any acfs root.
- if _, err := os.Stat("/app/data"); err == nil {
- rootDir, logical = "/app/data", "/vulnerability-reports"
- }
- if err := acfs.MkdirAll(ctx, rootDir, logical, 0o700); err != nil {
- slog.WarnContext(ctx, "failed to create vulnerability report directory", "dir", filepath.Join(rootDir, logical), "error", err)
- return ""
+ if len(raw) == 0 || s.db == nil {
+ return false
}
- reportDir := filepath.Join(rootDir, logical)
- fileName := strings.ReplaceAll(imageID, ":", "_") + ".json"
- if err := acfs.WriteFile(ctx, reportDir, "/"+fileName, raw, 0o600); err != nil {
- slog.WarnContext(ctx, "failed to persist raw trivy report", "path", filepath.Join(reportDir, fileName), "error", err)
- return ""
+ record := VulnerabilityReportRecord{ImageID: imageID, Data: string(raw)}
+ if err := s.db.WithContext(ctx).Clauses(clause.OnConflict{
+ Columns: []clause.Column{{Name: "image_id"}},
+ UpdateAll: true,
+ }).Create(&record).Error; err != nil {
+ slog.WarnContext(ctx, "failed to persist raw trivy report", "imageId", imageID, "error", err)
+ return false
}
- return filepath.Join(reportDir, fileName)
+ return true
}
// GetTrivyVersion returns the Trivy version from the configured scanner image.
@@ -1248,7 +1239,7 @@ func (s *VulnerabilityService) runTrivyScan(ctx context.Context, trivyImage stri
// Convert Trivy report to our ScanResult format
result := vulnerability.ConvertTrivyReportToScanResult(trivyReport, imageID, time.Now(), duration)
result.ScannerVersion = s.GetTrivyVersion(ctx)
- result.ReportPath = persistRawTrivyReportInternal(ctx, imageID, trivyOutput, stdoutFile)
+ result.HasReport = s.persistRawTrivyReportInternal(ctx, imageID, trivyOutput, stdoutFile)
totalVulnerabilities := 0
if result.Summary != nil {
totalVulnerabilities = result.Summary.Total
diff --git a/backend/resources/migrations/postgres/076_move_vulnerability_reports_to_db.sql b/backend/resources/migrations/postgres/076_move_vulnerability_reports_to_db.sql
new file mode 100644
index 000000000..0ce6ffcac
--- /dev/null
+++ b/backend/resources/migrations/postgres/076_move_vulnerability_reports_to_db.sql
@@ -0,0 +1,14 @@
+-- +goose Up
+-- Store raw Trivy reports in the database instead of files on disk.
+CREATE TABLE IF NOT EXISTS vulnerability_reports (
+ image_id TEXT PRIMARY KEY,
+ data TEXT NOT NULL,
+ created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ updated_at TIMESTAMP
+);
+
+ALTER TABLE vulnerability_scans DROP COLUMN IF EXISTS report_path;
+
+-- +goose Down
+ALTER TABLE vulnerability_scans ADD COLUMN report_path TEXT;
+DROP TABLE IF EXISTS vulnerability_reports;
diff --git a/backend/resources/migrations/sqlite/076_move_vulnerability_reports_to_db.sql b/backend/resources/migrations/sqlite/076_move_vulnerability_reports_to_db.sql
new file mode 100644
index 000000000..0cb143409
--- /dev/null
+++ b/backend/resources/migrations/sqlite/076_move_vulnerability_reports_to_db.sql
@@ -0,0 +1,14 @@
+-- +goose Up
+-- Store raw Trivy reports in the database instead of files on disk.
+CREATE TABLE IF NOT EXISTS vulnerability_reports (
+ image_id TEXT PRIMARY KEY,
+ data TEXT NOT NULL,
+ created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ updated_at DATETIME
+);
+
+ALTER TABLE vulnerability_scans DROP COLUMN report_path;
+
+-- +goose Down
+ALTER TABLE vulnerability_scans ADD COLUMN report_path TEXT;
+DROP TABLE IF EXISTS vulnerability_reports;
diff --git a/frontend/src/routes/(app)/security/+page.svelte b/frontend/src/routes/(app)/security/+page.svelte
index ba77d2010..79c97c3bd 100644
--- a/frontend/src/routes/(app)/security/+page.svelte
+++ b/frontend/src/routes/(app)/security/+page.svelte
@@ -31,7 +31,7 @@
let showIgnored = $state(false);
function withIgnoredFilter(options: SearchPaginationSortRequest, show: boolean): SearchPaginationSortRequest {
- const filters = { ...(options.filters ?? {}) };
+ const filters = { ...options.filters };
if (show) {
filters['ignored'] = 'true';
} else {
@@ -68,9 +68,7 @@
data: [],
pagination: { totalPages: 0, totalItems: 0, currentPage: 1, itemsPerPage: 20 }
});
- let patchRequestOptions = $state<SearchPaginationSortRequest>({
- pagination: { page: 1, limit: 20 }
- });
+ let patchRequestOptions = $state<SearchPaginationSortRequest>(untrack(() => data.patchRequestOptions));
async function loadPatches() {
try {
const response = await imageService.listPatchTargets(patchRequestOptions);
diff --git a/frontend/src/routes/(app)/security/+page.ts b/frontend/src/routes/(app)/security/+page.ts
index 28f46585f..bee5cd715 100644
--- a/frontend/src/routes/(app)/security/+page.ts
+++ b/frontend/src/routes/(app)/security/+page.ts
@@ -22,6 +22,17 @@ export const load: PageLoad = async ({ parent }) => {
}
} satisfies SearchPaginationSortRequest);
+ const patchRequestOptions = resolveInitialTableRequest('arcane-security-patch-table', {
+ pagination: {
+ page: 1,
+ limit: 20
+ },
+ sort: {
+ column: 'scanTime',
+ direction: 'desc'
+ }
+ } satisfies SearchPaginationSortRequest);
+
const requestForApi = mapVulnerabilityRequest(vulnerabilityRequestOptions);
let summary;
@@ -44,6 +55,7 @@ export const load: PageLoad = async ({ parent }) => {
return {
summary,
vulnerabilities: mapVulnerabilityPage(vulnerabilities, vulnerabilityRequestOptions),
- vulnerabilityRequestOptions
+ vulnerabilityRequestOptions,
+ patchRequestOptions
};
};
diff --git a/frontend/src/routes/(app)/security/security-patch-table.svelte b/frontend/src/routes/(app)/security/security-patch-table.svelte
index ecae30454..28d2158f2 100644
--- a/frontend/src/routes/(app)/security/security-patch-table.svelte
+++ b/frontend/src/routes/(app)/security/security-patch-table.svelte
@@ -81,9 +81,9 @@
}
const columns = $derived([
- { accessorKey: 'imageRef', title: m.common_image(), cell: ImageCell },
- { accessorKey: 'fixableCount', title: m.security_fixable(), cell: FixableCell },
- { accessorKey: 'scanTime', title: m.security_last_scanned(), cell: ScannedCell },
+ { id: 'imageName', accessorKey: 'imageRef', title: m.common_image(), sortable: true, cell: ImageCell },
+ { accessorKey: 'fixableCount', title: m.security_fixable(), sortable: true, cell: FixableCell },
+ { accessorKey: 'scanTime', title: m.security_last_scanned(), sortable: true, cell: ScannedCell },
{ id: 'lastPatch', title: m.security_last_patch(), cell: LastPatchCell },
{ id: 'patchedRef', title: m.security_patched_ref(), cell: PatchedRefCell }
] satisfies ColumnSpec<PatchTargetRow>[]);
diff --git a/types/vulnerability/vulnerability.go b/types/vulnerability/vulnerability.go
index a6470993f..a0a6b8c53 100644
--- a/types/vulnerability/vulnerability.go
+++ b/types/vulnerability/vulnerability.go
@@ -243,10 +243,6 @@ type ScanResult struct {
//
// Required: false
HasReport bool `json:"hasReport,omitempty"`
-
- // ReportPath is the server-side path of the stored raw scanner report.
- // Never serialized; only used internally to persist and resolve reports.
- ReportPath string `json:"-"`
}
// ScanStatus represents the status of a vulnerability scan