|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/robfig/cron/v3" |
| 10 | + "github.com/spf13/viper" |
| 11 | +) |
| 12 | + |
| 13 | +// RiskConfig contains configuration for risk-related periodic workers. |
| 14 | +type RiskConfig struct { |
| 15 | + ReviewDeadlineReminderEnabled bool `mapstructure:"review_deadline_reminder_enabled" yaml:"review_deadline_reminder_enabled" json:"reviewDeadlineReminderEnabled"` |
| 16 | + ReviewDeadlineReminderSchedule string `mapstructure:"review_deadline_reminder_schedule" yaml:"review_deadline_reminder_schedule" json:"reviewDeadlineReminderSchedule"` |
| 17 | + |
| 18 | + ReviewOverdueEscalationEnabled bool `mapstructure:"review_overdue_escalation_enabled" yaml:"review_overdue_escalation_enabled" json:"reviewOverdueEscalationEnabled"` |
| 19 | + ReviewOverdueEscalationSchedule string `mapstructure:"review_overdue_escalation_schedule" yaml:"review_overdue_escalation_schedule" json:"reviewOverdueEscalationSchedule"` |
| 20 | + |
| 21 | + StaleRiskScannerEnabled bool `mapstructure:"stale_risk_scanner_enabled" yaml:"stale_risk_scanner_enabled" json:"staleRiskScannerEnabled"` |
| 22 | + StaleRiskScannerSchedule string `mapstructure:"stale_risk_scanner_schedule" yaml:"stale_risk_scanner_schedule" json:"staleRiskScannerSchedule"` |
| 23 | + |
| 24 | + EvidenceReconciliationEnabled bool `mapstructure:"evidence_reconciliation_enabled" yaml:"evidence_reconciliation_enabled" json:"evidenceReconciliationEnabled"` |
| 25 | + EvidenceReconciliationSchedule string `mapstructure:"evidence_reconciliation_schedule" yaml:"evidence_reconciliation_schedule" json:"evidenceReconciliationSchedule"` |
| 26 | + |
| 27 | + AutoReopenEnabled bool `mapstructure:"auto_reopen_enabled" yaml:"auto_reopen_enabled" json:"autoReopenEnabled"` |
| 28 | + AutoReopenThresholdDays int `mapstructure:"auto_reopen_threshold_days" yaml:"auto_reopen_threshold_days" json:"autoReopenThresholdDays"` |
| 29 | +} |
| 30 | + |
| 31 | +func DefaultRiskConfig() *RiskConfig { |
| 32 | + return &RiskConfig{ |
| 33 | + ReviewDeadlineReminderEnabled: false, |
| 34 | + ReviewDeadlineReminderSchedule: "0 0 8 * * *", |
| 35 | + ReviewOverdueEscalationEnabled: false, |
| 36 | + ReviewOverdueEscalationSchedule: "0 0 9 * * *", |
| 37 | + StaleRiskScannerEnabled: false, |
| 38 | + StaleRiskScannerSchedule: "0 0 10 * * 1", |
| 39 | + EvidenceReconciliationEnabled: false, |
| 40 | + EvidenceReconciliationSchedule: "0 30 10 * * *", |
| 41 | + AutoReopenEnabled: false, |
| 42 | + AutoReopenThresholdDays: 30, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func LoadRiskConfig(path string) (*RiskConfig, error) { |
| 47 | + v := viper.NewWithOptions(viper.KeyDelimiter("::")) |
| 48 | + |
| 49 | + def := DefaultRiskConfig() |
| 50 | + v.SetDefault("review_deadline_reminder_enabled", def.ReviewDeadlineReminderEnabled) |
| 51 | + v.SetDefault("review_deadline_reminder_schedule", def.ReviewDeadlineReminderSchedule) |
| 52 | + v.SetDefault("review_overdue_escalation_enabled", def.ReviewOverdueEscalationEnabled) |
| 53 | + v.SetDefault("review_overdue_escalation_schedule", def.ReviewOverdueEscalationSchedule) |
| 54 | + v.SetDefault("stale_risk_scanner_enabled", def.StaleRiskScannerEnabled) |
| 55 | + v.SetDefault("stale_risk_scanner_schedule", def.StaleRiskScannerSchedule) |
| 56 | + v.SetDefault("evidence_reconciliation_enabled", def.EvidenceReconciliationEnabled) |
| 57 | + v.SetDefault("evidence_reconciliation_schedule", def.EvidenceReconciliationSchedule) |
| 58 | + v.SetDefault("auto_reopen_enabled", def.AutoReopenEnabled) |
| 59 | + v.SetDefault("auto_reopen_threshold_days", def.AutoReopenThresholdDays) |
| 60 | + |
| 61 | + v.SetEnvPrefix("CCF_RISK") |
| 62 | + v.SetEnvKeyReplacer(strings.NewReplacer("::", "_", ".", "_", "-", "_")) |
| 63 | + v.AutomaticEnv() |
| 64 | + |
| 65 | + if path != "" { |
| 66 | + v.SetConfigFile(path) |
| 67 | + v.SetConfigType("yaml") |
| 68 | + if err := v.ReadInConfig(); err != nil { |
| 69 | + var notFound viper.ConfigFileNotFoundError |
| 70 | + if !errors.As(err, ¬Found) && !errors.Is(err, os.ErrNotExist) { |
| 71 | + return nil, fmt.Errorf("failed to read risk config file: %w", err) |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + var cfg RiskConfig |
| 77 | + if err := v.Unmarshal(&cfg); err != nil { |
| 78 | + return nil, fmt.Errorf("failed to parse risk config: %w", err) |
| 79 | + } |
| 80 | + if err := cfg.Validate(); err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + |
| 84 | + return &cfg, nil |
| 85 | +} |
| 86 | + |
| 87 | +func (c *RiskConfig) Validate() error { |
| 88 | + parser := cron.NewParser(cron.Second | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor) |
| 89 | + |
| 90 | + if c.ReviewDeadlineReminderEnabled { |
| 91 | + if _, err := parser.Parse(c.ReviewDeadlineReminderSchedule); err != nil { |
| 92 | + return fmt.Errorf("invalid review_deadline_reminder_schedule: %w", err) |
| 93 | + } |
| 94 | + } |
| 95 | + if c.ReviewOverdueEscalationEnabled { |
| 96 | + if _, err := parser.Parse(c.ReviewOverdueEscalationSchedule); err != nil { |
| 97 | + return fmt.Errorf("invalid review_overdue_escalation_schedule: %w", err) |
| 98 | + } |
| 99 | + } |
| 100 | + if c.StaleRiskScannerEnabled { |
| 101 | + if _, err := parser.Parse(c.StaleRiskScannerSchedule); err != nil { |
| 102 | + return fmt.Errorf("invalid stale_risk_scanner_schedule: %w", err) |
| 103 | + } |
| 104 | + } |
| 105 | + if c.EvidenceReconciliationEnabled { |
| 106 | + if _, err := parser.Parse(c.EvidenceReconciliationSchedule); err != nil { |
| 107 | + return fmt.Errorf("invalid evidence_reconciliation_schedule: %w", err) |
| 108 | + } |
| 109 | + } |
| 110 | + if c.AutoReopenThresholdDays < 0 { |
| 111 | + return fmt.Errorf("risk auto reopen threshold days must be non-negative") |
| 112 | + } |
| 113 | + if c.AutoReopenEnabled && c.AutoReopenThresholdDays <= 0 { |
| 114 | + return fmt.Errorf("risk auto reopen threshold days must be greater than zero when auto reopen is enabled") |
| 115 | + } |
| 116 | + |
| 117 | + return nil |
| 118 | +} |
0 commit comments