Documentation
¶
Overview ¶
Package scan defines the Scan domain entity and types. A Scan binds an Asset Group with a Scanner/Workflow and Schedule.
Index ¶
- Constants
- type AgentPreference
- type Filter
- type OverviewStats
- type Repository
- type Scan
- func (s *Scan) Activate() error
- func (s *Scan) CalculateNextRunAt() *time.Time
- func (s *Scan) CalculateRetryDelay(attempt int) time.Duration
- func (s *Scan) CanTrigger() bool
- func (s *Scan) Clone(newName string) *Scan
- func (s *Scan) Disable() error
- func (s *Scan) GetAllAssetGroupIDs() []shared.ID
- func (s *Scan) HasAssetGroup() bool
- func (s *Scan) HasTargets() bool
- func (s *Scan) IsDueForExecution(now time.Time) bool
- func (s *Scan) Pause() error
- func (s *Scan) RecordRun(runID shared.ID, status string)
- func (s *Scan) SetAgentPreference(pref AgentPreference)
- func (s *Scan) SetAssetGroupIDs(ids []shared.ID)
- func (s *Scan) SetCreatedBy(userID shared.ID)
- func (s *Scan) SetProfileID(profileID *shared.ID)
- func (s *Scan) SetRetryConfig(maxRetries, backoffSeconds int)
- func (s *Scan) SetRunOnTenantRunner(value bool)
- func (s *Scan) SetSchedule(scheduleType ScheduleType, cron string, day *int, t *time.Time, ...) error
- func (s *Scan) SetSingleScanner(scannerName string, config map[string]any, targetsPerJob int) error
- func (s *Scan) SetTags(tags []string)
- func (s *Scan) SetTargets(targets []string)
- func (s *Scan) SetTimeoutSeconds(seconds int)
- func (s *Scan) SetWorkflow(pipelineID shared.ID) error
- func (s *Scan) ShouldRetry(currentAttempt int) bool
- func (s *Scan) Update(name, description string) error
- func (s *Scan) Validate() error
- type ScanType
- type ScheduleType
- type Stats
- type Status
- type StatusCounts
Constants ¶
const ( // MinScanTimeoutSeconds is the minimum allowed scan timeout (30 seconds). // Lower values would create DoS pressure on the timeout sweeper. MinScanTimeoutSeconds = 30 // DefaultScanTimeoutSeconds is the default scan timeout (1 hour). DefaultScanTimeoutSeconds = 3600 // MaxScanTimeoutSeconds is the maximum allowed scan timeout (24 hours). MaxScanTimeoutSeconds = 86400 )
Timeout constants for scan execution.
const ( // MaxRetryCount is the absolute maximum retries allowed per scan. MaxRetryCount = 10 // DefaultRetryBackoffSeconds is the default initial backoff between retries. DefaultRetryBackoffSeconds = 60 // MinRetryBackoffSeconds is the minimum allowed initial backoff. MinRetryBackoffSeconds = 10 // MaxRetryBackoffSeconds is the maximum allowed initial backoff (24 hours). MaxRetryBackoffSeconds = 86400 )
Retry constants for scan execution.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AgentPreference ¶
type AgentPreference string
AgentPreference determines which agents can execute the scan.
const ( // AgentPreferenceAuto tries tenant agents first, falls back to platform. AgentPreferenceAuto AgentPreference = "auto" // AgentPreferenceTenant only uses tenant's own agents. AgentPreferenceTenant AgentPreference = "tenant" // AgentPreferencePlatform only uses platform agents. AgentPreferencePlatform AgentPreference = "platform" )
type Filter ¶
type Filter struct {
TenantID *shared.ID
AssetGroupID *shared.ID
PipelineID *shared.ID
ScanType *ScanType
ScheduleType *ScheduleType
Status *Status
Tags []string
Search string
}
Filter represents filter options for listing scans.
type OverviewStats ¶
type OverviewStats struct {
Pipelines StatusCounts `json:"pipelines"`
Scans StatusCounts `json:"scans"`
Jobs StatusCounts `json:"jobs"`
}
OverviewStats represents the scan management overview statistics.
type Repository ¶
type Repository interface {
// Create creates a new scan.
Create(ctx context.Context, scan *Scan) error
// GetByID retrieves a scan by ID.
GetByID(ctx context.Context, id shared.ID) (*Scan, error)
// GetByTenantAndID retrieves a scan by tenant and ID.
GetByTenantAndID(ctx context.Context, tenantID, id shared.ID) (*Scan, error)
// GetByName retrieves a scan by tenant and name.
GetByName(ctx context.Context, tenantID shared.ID, name string) (*Scan, error)
// List lists scans with filters and pagination.
List(ctx context.Context, filter Filter, page pagination.Pagination) (pagination.Result[*Scan], error)
// Update updates a scan.
Update(ctx context.Context, scan *Scan) error
// Delete deletes a scan.
Delete(ctx context.Context, id shared.ID) error
// ListDueForExecution lists scans that are due for scheduled execution.
ListDueForExecution(ctx context.Context, now time.Time) ([]*Scan, error)
// UpdateNextRunAt updates the next run time for a scan.
UpdateNextRunAt(ctx context.Context, id shared.ID, nextRunAt *time.Time) error
// RecordRun records a run result for a scan.
RecordRun(ctx context.Context, id shared.ID, runID shared.ID, status string) error
// GetStats returns aggregated statistics for scans.
GetStats(ctx context.Context, tenantID shared.ID) (*Stats, error)
// Count counts scans matching the filter.
Count(ctx context.Context, filter Filter) (int64, error)
// ListByAssetGroupID lists all scans for an asset group.
ListByAssetGroupID(ctx context.Context, assetGroupID shared.ID) ([]*Scan, error)
// ListByPipelineID lists all scans using a pipeline.
ListByPipelineID(ctx context.Context, pipelineID shared.ID) ([]*Scan, error)
// UpdateStatusByAssetGroupID updates status for all scans in an asset group.
UpdateStatusByAssetGroupID(ctx context.Context, assetGroupID shared.ID, status Status) error
// TryLockScanForScheduler attempts to acquire a session-level advisory lock
// for the given scan ID. Returns true if the lock was acquired, false if
// another instance already holds it. The lock must be released with
// UnlockScanForScheduler when the trigger completes.
TryLockScanForScheduler(ctx context.Context, id shared.ID) (bool, error)
// UnlockScanForScheduler releases a previously acquired scheduler lock for the given scan ID.
UnlockScanForScheduler(ctx context.Context, id shared.ID) error
}
Repository defines the interface for scan persistence.
type Scan ¶
type Scan struct {
ID shared.ID
TenantID shared.ID
Name string
Description string
// Target - can use AssetGroupID/AssetGroupIDs, Targets, or both
AssetGroupID shared.ID // Optional: primary asset group (legacy, for single asset group)
AssetGroupIDs []shared.ID // Optional: multiple asset groups (NEW)
Targets []string // Optional: direct target list (domains, IPs, URLs)
// Scan Type
ScanType ScanType
PipelineID *shared.ID // For workflow type
ScannerName string // For single type
ScannerConfig map[string]any // Scanner-specific configuration
TargetsPerJob int // Number of targets per job batch
// Schedule
ScheduleType ScheduleType
ScheduleCron string // Cron expression (for crontab type)
ScheduleDay *int // Day of week (0-6) or month (1-31)
ScheduleTime *time.Time // Time of day to run
ScheduleTimezone string
NextRunAt *time.Time // Pre-computed next run time
// Routing
Tags []string // Route to agents with matching tags
RunOnTenantRunner bool // Restrict to tenant's own runners
AgentPreference AgentPreference // Agent selection mode: auto, tenant, platform
// Profile - links to ScanProfile for tool configs, intensity, quality gates
ProfileID *shared.ID
// Timeout - max execution time in seconds (default 3600 = 1h, max 86400 = 24h)
TimeoutSeconds int
// Retry config - automatic retry of failed runs with exponential backoff
MaxRetries int // 0 = no retry, max 10
RetryBackoffSeconds int // Initial backoff (default 60s), actual delay is backoff * 2^attempt
// Status
Status Status
// Execution Statistics
LastRunID *shared.ID
LastRunAt *time.Time
LastRunStatus string
TotalRuns int
SuccessfulRuns int
FailedRuns int
// Audit
CreatedBy *shared.ID
CreatedAt time.Time
UpdatedAt time.Time
}
Scan represents a scan definition that binds an asset group with a scanner/workflow and schedule.
func NewScan ¶
func NewScan(tenantID shared.ID, name string, assetGroupID shared.ID, scanType ScanType) (*Scan, error)
NewScan creates a new scan definition. assetGroupID is optional if targets are provided later via SetTargets.
func NewScanWithTargets ¶
func NewScanWithTargets(tenantID shared.ID, name string, targets []string, scanType ScanType) (*Scan, error)
NewScanWithTargets creates a new scan definition with direct targets. This is used when creating scans without a pre-existing asset group.
func (*Scan) CalculateNextRunAt ¶
CalculateNextRunAt returns the next scheduled run time. This is used by the scheduler to update next_run_at after triggering.
func (*Scan) CalculateRetryDelay ¶ added in v0.1.5
CalculateRetryDelay returns the exponential backoff delay for the given attempt number. attempt 0 = first retry, attempt 1 = second retry, etc. Capped at MaxRetryBackoffSeconds.
func (*Scan) CanTrigger ¶
CanTrigger returns true if the scan can be triggered.
func (*Scan) GetAllAssetGroupIDs ¶
GetAllAssetGroupIDs returns all asset group IDs (both singular and multiple).
func (*Scan) HasAssetGroup ¶
HasAssetGroup returns true if the scan is linked to an asset group.
func (*Scan) HasTargets ¶
HasTargets returns true if the scan has direct targets.
func (*Scan) IsDueForExecution ¶
IsDueForExecution returns true if the scan is due for scheduled execution.
func (*Scan) SetAgentPreference ¶
func (s *Scan) SetAgentPreference(pref AgentPreference)
SetAgentPreference sets the agent selection preference.
func (*Scan) SetAssetGroupIDs ¶
SetAssetGroupIDs sets multiple asset group IDs for the scan.
func (*Scan) SetCreatedBy ¶
SetCreatedBy sets the user who created the scan.
func (*Scan) SetProfileID ¶ added in v0.1.5
SetProfileID links the scan to a scan profile (for tool configs and quality gates). Pass nil to unlink.
func (*Scan) SetRetryConfig ¶ added in v0.1.5
SetRetryConfig configures the retry behavior for failed runs. maxRetries is capped at MaxRetryCount; backoff is bounded to [Min,Max]RetryBackoffSeconds.
func (*Scan) SetRunOnTenantRunner ¶
SetRunOnTenantRunner sets whether to restrict to tenant runners only.
func (*Scan) SetSchedule ¶
func (s *Scan) SetSchedule(scheduleType ScheduleType, cron string, day *int, t *time.Time, timezone string) error
SetSchedule configures the schedule for the scan.
func (*Scan) SetSingleScanner ¶
SetSingleScanner configures the scan to use a single scanner.
func (*Scan) SetTargets ¶
SetTargets sets the direct target list for the scan.
func (*Scan) SetTimeoutSeconds ¶ added in v0.1.5
SetTimeoutSeconds sets the maximum execution time in seconds. Enforces [MinScanTimeoutSeconds, MaxScanTimeoutSeconds] bounds at the domain layer (defense-in-depth — even if HTTP validation is bypassed, the domain enforces the floor). If <= 0, defaults to DefaultScanTimeoutSeconds.
func (*Scan) SetWorkflow ¶
SetWorkflow configures the scan to use a workflow pipeline.
func (*Scan) ShouldRetry ¶ added in v0.1.5
ShouldRetry returns true if a failed run with the given retry attempt should be retried.
type ScheduleType ¶
type ScheduleType string
ScheduleType represents when the scan should run.
const ( ScheduleManual ScheduleType = "manual" ScheduleDaily ScheduleType = "daily" ScheduleWeekly ScheduleType = "weekly" ScheduleMonthly ScheduleType = "monthly" ScheduleCrontab ScheduleType = "crontab" )
type Stats ¶
type Stats struct {
Total int64 `json:"total"`
Active int64 `json:"active"`
Paused int64 `json:"paused"`
Disabled int64 `json:"disabled"`
ByScheduleType map[ScheduleType]int64 `json:"by_schedule_type"`
ByScanType map[ScanType]int64 `json:"by_scan_type"`
}
Stats represents aggregated statistics for scans.