Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
package vulnerability
import (
"time"
"uuid"
"github.com/getarcaneapp/arcane/types/v2/vulnerability"
"go.getarcane.app/kit/pkg"
"gorm.io/gorm"
"github.com/getarcaneapp/arcane/backend/v2/internal/database"
)
// VulnerabilityScanRecord stores vulnerability scan results for images
type VulnerabilityScanRecord struct {
database.BaseModel
// ImageID is the Docker image ID (primary key)
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" 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" sortable:"true"`
// Duration is the duration of the scan in milliseconds
Duration int64 `json:"duration" gorm:"column:duration"`
// Total vulnerability counts by severity
CriticalCount int `json:"criticalCount" gorm:"column:critical_count"`
HighCount int `json:"highCount" gorm:"column:high_count"`
MediumCount int `json:"mediumCount" gorm:"column:medium_count"`
LowCount int `json:"lowCount" gorm:"column:low_count"`
UnknownCount int `json:"unknownCount" gorm:"column:unknown_count"`
TotalCount int `json:"totalCount" gorm:"column:total_count"`
// Vulnerabilities stores the JSON encoded vulnerabilities list
Vulnerabilities database.StringSlice `json:"vulnerabilities" gorm:"column:vulnerabilities;type:text"`
// Error contains the error message if the scan failed
Error *string `json:"error,omitempty" gorm:"column:error"`
// ScannerVersion is the version of the scanner used
ScannerVersion string `json:"scannerVersion" gorm:"column:scanner_version"`
// 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" sortable:"true"`
ScanPolicy string `json:"-" gorm:"column:scan_policy"`
}
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"
}
// VulnerabilityScanItem is one CVE finding from a completed scan, normalized for list queries.
type VulnerabilityScanItem struct {
ID int64 `json:"id" gorm:"primaryKey;autoIncrement;column:id"`
ImageID string `json:"imageId" gorm:"column:image_id;index"`
VulnerabilityID string `json:"vulnerabilityId" gorm:"column:vulnerability_id;index"`
PkgName string `json:"pkgName" gorm:"column:pkg_name"`
InstalledVersion string `json:"installedVersion" gorm:"column:installed_version"`
FixedVersion string `json:"fixedVersion" gorm:"column:fixed_version"`
Severity string `json:"severity" gorm:"column:severity;index"`
SeverityRank int `json:"severityRank" gorm:"column:severity_rank"`
PkgClass string `json:"pkgClass" gorm:"column:pkg_class"`
PkgType string `json:"pkgType" gorm:"column:pkg_type"`
Title string `json:"title" gorm:"column:title"`
CVSSScore *float64 `json:"cvssScore,omitempty" gorm:"column:cvss_score"`
Details *string `json:"-" gorm:"column:details"`
}
func (VulnerabilityScanItem) TableName() string {
return "vulnerability_scan_items"
}
// VulnerabilityThreatIntel caches CISA KEV and FIRST EPSS data per CVE.
type VulnerabilityThreatIntel struct {
CVEID string `gorm:"column:cve_id;primaryKey"`
KnownExploited bool `gorm:"column:known_exploited"`
KEVDateAdded string `gorm:"column:kev_date_added"`
KEVDueDate string `gorm:"column:kev_due_date"`
KEVRansomware bool `gorm:"column:kev_ransomware"`
EPSSScore *float64 `gorm:"column:epss_score"`
EPSSPercentile *float64 `gorm:"column:epss_percentile"`
EPSSCheckedAt *time.Time `gorm:"column:epss_checked_at"`
UpdatedAt *time.Time `gorm:"column:updated_at"`
}
func (VulnerabilityThreatIntel) TableName() string {
return "vulnerability_threat_intel"
}
type vulnerabilityScanItemDetails struct {
Description string `json:"description,omitempty"`
References []string `json:"references,omitempty"`
CVSS *vulnerability.CVSSInfo `json:"cvss,omitempty"`
PublishedDate *time.Time `json:"publishedDate,omitempty"`
LastModifiedDate *time.Time `json:"lastModifiedDate,omitempty"`
}
// VulnerabilityIgnore stores ignored vulnerability records
type VulnerabilityIgnore struct {
// ID is the unique identifier for this ignore record
ID string `json:"id" gorm:"primaryKey;type:text"`
// EnvironmentID is the environment where this ignore applies
EnvironmentID string `json:"environmentId" gorm:"column:environment_id;index"`
// ImageID is the Docker image ID
ImageID string `json:"imageId" gorm:"column:image_id;index"`
// VulnerabilityID is the CVE or vulnerability identifier (e.g., CVE-2023-1234)
VulnerabilityID string `json:"vulnerabilityId" gorm:"column:vulnerability_id;index"`
// PkgName is the package name containing the vulnerability
PkgName string `json:"pkgName" gorm:"column:pkg_name"`
// InstalledVersion is the version of the package with the vulnerability
InstalledVersion string `json:"installedVersion" gorm:"column:installed_version"`
// Reason is an optional reason for ignoring this vulnerability
Reason *string `json:"reason,omitempty" gorm:"column:reason"`
// CreatedBy is the user ID who created this ignore record
CreatedBy string `json:"createdBy" gorm:"column:created_by"`
// CreatedAt is when this ignore record was created
CreatedAt time.Time `json:"createdAt" gorm:"column:created_at"`
}
func (v *VulnerabilityIgnore) TableName() string {
return "vulnerability_ignores"
}
// BeforeCreate sets the ID and CreatedAt before inserting
func (v *VulnerabilityIgnore) BeforeCreate(db *gorm.DB) error {
if v.ID == "" {
v.ID = uuid.New().String()
}
if v.CreatedAt.IsZero() {
v.CreatedAt = time.Now()
}
return nil
}
// CompositeKey generates a composite key for deduplication.
// This helps prevent duplicate ignore records for the same vulnerability.
func (v *VulnerabilityIgnore) CompositeKey() string {
return v.EnvironmentID + ":" + v.ImageID + ":" + v.VulnerabilityID + ":" + v.PkgName + ":" + v.InstalledVersion
}
// Scan status constants
const (
ScanStatusPending = "pending"
ScanStatusScanning = "scanning"
ScanStatusCompleted = "completed"
ScanStatusFailed = "failed"
)
// IsCompleted returns true if the scan has completed
func (v *VulnerabilityScanRecord) IsCompleted() bool {
return v.Status == ScanStatusCompleted
}
// IsFailed returns true if the scan has failed
func (v *VulnerabilityScanRecord) IsFailed() bool {
return v.Status == ScanStatusFailed
}
// IsScanning returns true if the scan is in progress
func (v *VulnerabilityScanRecord) IsScanning() bool {
return v.Status == ScanStatusScanning
}
// GetTotalVulnerabilities returns the total count of vulnerabilities
func (v *VulnerabilityScanRecord) GetTotalVulnerabilities() int {
return v.TotalCount
}
// GetHighestSeverity returns the highest severity level found
func (v *VulnerabilityScanRecord) GetHighestSeverity() string {
if v.CriticalCount > 0 {
return "CRITICAL"
}
if v.HighCount > 0 {
return "HIGH"
}
if v.MediumCount > 0 {
return "MEDIUM"
}
if v.LowCount > 0 {
return "LOW"
}
return kit.Ternary(v.UnknownCount > 0, "UNKNOWN", "NONE")
}