store

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package store keeps configured scans, scan runs and their findings, and the history of the cluster's MalwareReports in SQLite (pure Go driver, no CGO).

Index

Constants

View Source
const (
	StatusQueued   = "queued"
	StatusRunning  = "running"
	StatusClean    = "clean"
	StatusInfected = "infected"
	StatusError    = "error"
)

Run statuses. Only clean means the image was fully scanned without findings.

View Source
const (
	TriggerManual   = "manual"
	TriggerSchedule = "schedule"
	TriggerAPI      = "api"
)

Run triggers.

View Source
const MaxDays = 366

MaxDays bounds the days of Daily and ClusterDaily: each day is one entry allocated up front.

Variables

View Source
var ErrNotFound = errors.New("not found")

ErrNotFound is returned when a scan or run does not exist.

Functions

This section is empty.

Types

type ClusterDay

type ClusterDay struct {
	Date       time.Time `json:"date"`
	Clean      int       `json:"clean"`
	Infected   int       `json:"infected"`
	Incomplete int       `json:"incomplete"`
	Errors     int       `json:"errors"`
}

ClusterDay counts the cluster's reports by status at the end of a day (for today: now).

func (ClusterDay) Total

func (d ClusterDay) Total() int

Total returns the number of reports of the day.

type ClusterEvent

type ClusterEvent struct {
	ID int64 `json:"id"`
	// Cluster is the name of the cluster (empty for a single cluster).
	Cluster   string `json:"cluster,omitempty"`
	Namespace string `json:"namespace"`
	Report    string `json:"report"`
	// Group identifies the workload and container across rollouts:
	// "Deployment/web/app" for the ReplicaSets of a Deployment, otherwise
	// "<kind>/<name>/<container>".
	Group        string `json:"group"`
	WorkloadKind string `json:"workloadKind"`
	WorkloadName string `json:"workloadName"`
	Container    string `json:"container"`
	Repository   string `json:"repository"`
	Reference    string `json:"reference,omitempty"`
	Digest       string `json:"digest"`
	// Status of the report (Clean, Infected, Incomplete, Error); empty for a
	// deletion.
	Status     string    `json:"status,omitempty"`
	Deleted    bool      `json:"deleted,omitempty"`
	Findings   int       `json:"findings"`
	Skipped    int       `json:"skipped"`
	Files      int       `json:"files"`
	Engine     string    `json:"engine,omitempty"`
	Signatures string    `json:"signatures,omitempty"`
	Error      string    `json:"error,omitempty"`
	ScannedAt  time.Time `json:"scannedAt"`
	RecordedAt time.Time `json:"recordedAt"`
	// StatusSince is when the group got its current status.
	StatusSince time.Time `json:"statusSince"`
}

ClusterEvent is a change of a MalwareReport of the meles operator: a new scan result, or the deletion of the report.

type ClusterKey

type ClusterKey struct{ Namespace, Group string }

ClusterKey identifies a workload group in a namespace.

type ClusterReportKey

type ClusterReportKey struct{ Namespace, Report string }

ClusterReportKey identifies a report.

type Day

type Day struct {
	Date     time.Time
	Clean    int
	Infected int
	Errors   int
}

Day counts finished runs of one day.

func (Day) Total

func (d Day) Total() int

Total returns the number of runs of the day.

type FindingRow

type FindingRow struct {
	RunID      int64
	FinishedAt time.Time
	ImageRef   string
	Digest     string
	Platform   string
	Layer      int
	Path       string
	Signature  string
	Status     string
}

FindingRow is a finding with its run, for exports.

type Run

type Run struct {
	ID         int64      `json:"id"`
	ScanID     *int64     `json:"scanId,omitempty"`
	ScanName   string     `json:"scanName,omitempty"`
	ImageRef   string     `json:"imageRef"`
	Platforms  string     `json:"platforms"`
	Trigger    string     `json:"trigger"`
	Status     string     `json:"status"`
	QueuedAt   time.Time  `json:"queuedAt"`
	StartedAt  *time.Time `json:"startedAt,omitempty"`
	FinishedAt *time.Time `json:"finishedAt,omitempty"`
	Digest     string     `json:"digest,omitempty"`
	Engine     string     `json:"engine,omitempty"`
	Signatures string     `json:"signatures,omitempty"`
	Files      int        `json:"files"`
	// Findings and Skipped count what is not ignored by a rule.
	Findings int    `json:"findings"`
	Skipped  int    `json:"skipped"`
	Progress string `json:"progress,omitempty"`
	Error    string `json:"error,omitempty"`
}

Run is one execution of a scan or an ad-hoc scan.

func (*Run) Done

func (r *Run) Done() bool

Done reports whether the run has finished.

func (*Run) Duration

func (r *Run) Duration() time.Duration

Duration returns how long the run took or has been running.

type RunFilter

type RunFilter struct {
	ScanID   int64
	Status   string
	ImageRef string
	Since    time.Time
	Limit    int
	Offset   int
}

RunFilter selects runs. Zero values mean no restriction.

type Scan

type Scan struct {
	ID        int64  `json:"id"`
	Name      string `json:"name"`
	ImageRef  string `json:"imageRef"`
	Platforms string `json:"platforms"`
	// Schedule is a cron expression; empty means manual runs only.
	Schedule  string    `json:"schedule"`
	Enabled   bool      `json:"enabled"`
	CreatedAt time.Time `json:"createdAt"`
	UpdatedAt time.Time `json:"updatedAt"`
	// LastRun is the most recent run, if any (not stored with the scan).
	LastRun *Run `json:"lastRun,omitempty"`
}

Scan is a configured scan of an image reference.

type SignatureCount

type SignatureCount struct {
	Signature string
	Findings  int
	Images    int
	LastSeen  time.Time
}

SignatureCount aggregates findings of one signature.

type Stats

type Stats struct {
	Scans          int
	ScheduledScans int
	Runs           int
	Clean          int
	Infected       int
	Errors         int
	// InfectedImages counts images whose latest finished run found malware.
	InfectedImages int
	// Images counts distinct image references with a finished run.
	Images int
}

Stats summarises the database for the dashboard.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store is a SQLite database.

func Open

func Open(path string) (*Store, error)

Open opens or creates the database at path.

func (*Store) Close

func (s *Store) Close() error

Close closes the database.

func (*Store) ClusterDaily

func (s *Store) ClusterDaily(ctx context.Context, cluster string, days int, loc *time.Location) ([]ClusterDay, error)

ClusterDaily replays the history to the state at the end of each of the last days days (oldest first) in loc. Days before the history began count nothing.

func (*Store) ClusterHistory

func (s *Store) ClusterHistory(ctx context.Context, cluster, namespace, group string, limit int) ([]ClusterEvent, error)

ClusterHistory returns the newest events of a workload group, newest first.

func (*Store) ClusterStatusSince

func (s *Store) ClusterStatusSince(ctx context.Context, cluster string) (map[ClusterKey]time.Time, error)

ClusterStatusSince returns, per workload group, since when its current status holds.

func (*Store) CountRuns

func (s *Store) CountRuns(ctx context.Context, f RunFilter) (int, error)

CountRuns counts runs matching f (Limit and Offset are ignored).

func (*Store) CreateRun

func (s *Store) CreateRun(ctx context.Context, r *Run) error

CreateRun queues a run and sets its ID.

func (*Store) CreateScan

func (s *Store) CreateScan(ctx context.Context, sc *Scan) error

CreateScan stores a new scan and sets its ID.

func (*Store) Daily

func (s *Store) Daily(ctx context.Context, days int, loc *time.Location) ([]Day, error)

Daily returns one entry per day for the last days days (oldest first), grouped by the day the run finished in loc.

func (*Store) DeleteScan

func (s *Store) DeleteScan(ctx context.Context, id int64) error

DeleteScan deletes a scan. Its runs are kept as ad-hoc runs.

func (*Store) FailUnfinished

func (s *Store) FailUnfinished(ctx context.Context) (int64, error)

FailUnfinished marks runs left queued or running by a previous process as failed. Call it once at startup.

func (*Store) Findings

func (s *Store) Findings(ctx context.Context, since time.Time) ([]FindingRow, error)

Findings returns all findings of runs finished since since, newest first.

func (*Store) FinishRun

func (s *Store) FinishRun(ctx context.Context, id int64, rep *scan.Report, runErr error) error

FinishRun stores the result of a run: the report, or the error that stopped it. A run with an error is never stored as clean.

func (*Store) GetReport

func (s *Store) GetReport(ctx context.Context, id int64) (*scan.Report, error)

GetReport returns the scan report of a finished run, or nil if the run has none (still running or failed).

func (*Store) GetRun

func (s *Store) GetRun(ctx context.Context, id int64) (*Run, error)

GetRun returns a run without its report.

func (*Store) GetScan

func (s *Store) GetScan(ctx context.Context, id int64) (*Scan, error)

GetScan returns a scan with its last run.

func (*Store) HasActiveRun

func (s *Store) HasActiveRun(ctx context.Context, scanID int64) (bool, error)

HasActiveRun reports whether a scan has a queued or running run.

func (*Store) ImageStatus

func (s *Store) ImageStatus(ctx context.Context) ([]Run, error)

ImageStatus returns the latest finished run of every image reference, infected images first.

func (*Store) ListRuns

func (s *Store) ListRuns(ctx context.Context, f RunFilter) ([]Run, error)

ListRuns returns runs, newest first.

func (*Store) ListScans

func (s *Store) ListScans(ctx context.Context) ([]Scan, error)

ListScans returns all scans by name, each with its last run.

func (*Store) LiveClusterReports

func (s *Store) LiveClusterReports(ctx context.Context, cluster string) (map[ClusterReportKey]ClusterEvent, error)

LiveClusterReports returns the reports whose last event is not a deletion, with that event, to record deletions missed while the server was down.

func (*Store) Ping

func (s *Store) Ping(ctx context.Context) error

Ping checks the database connection.

func (*Store) PruneClusterEvents

func (s *Store) PruneClusterEvents(ctx context.Context, before time.Time) (int64, error)

PruneClusterEvents deletes events recorded before t, except the last event of each report: it holds the current state and its StatusSince.

func (*Store) RecordClusterEvent

func (s *Store) RecordClusterEvent(ctx context.Context, e *ClusterEvent) (bool, error)

RecordClusterEvent stores e unless it repeats the last event of the report: the same scan (status, digest, scan time), or a second deletion. Reports seen again after a restart therefore add nothing. If the group of the report changed (e.g. its Deployment became known), its earlier events move to the new group. It sets e.ID and e.StatusSince and reports whether e was stored.

func (*Store) SetProgress

func (s *Store) SetProgress(ctx context.Context, id int64, progress string) error

SetProgress updates the progress text of a running run.

func (*Store) StartRun

func (s *Store) StartRun(ctx context.Context, id int64) error

StartRun marks a queued run as running.

func (*Store) Stats

func (s *Store) Stats(ctx context.Context, since time.Time) (Stats, error)

Stats returns counts; run counts cover runs queued since since.

func (*Store) TopSignatures

func (s *Store) TopSignatures(ctx context.Context, since time.Time, limit int) ([]SignatureCount, error)

TopSignatures returns the most frequent signatures of runs finished since since.

func (*Store) UpdateScan

func (s *Store) UpdateScan(ctx context.Context, sc *Scan) error

UpdateScan saves the configuration of an existing scan.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL