Files
at-container-registry/scanner/internal/config/config.go
T

84 lines
2.0 KiB
Go

// Package config provides environment-based configuration for the scanner service.
package config
import (
"fmt"
"os"
"strconv"
)
// Config holds all scanner configuration
type Config struct {
// Addr is the HTTP address for the health endpoint
Addr string
// HoldURL is the WebSocket URL of the hold service
HoldURL string
// SharedSecret is the shared secret for scanner authentication
SharedSecret string
// Workers is the number of concurrent scan workers
Workers int
// QueueSize is the maximum priority queue depth
QueueSize int
// VulnEnabled enables Grype vulnerability scanning
VulnEnabled bool
// VulnDBPath is the directory for the Grype vulnerability database
VulnDBPath string
// TmpDir is the directory for temporary layer extraction
TmpDir string
}
// Load reads configuration from environment variables with SCANNER_ prefix
func Load() (*Config, error) {
cfg := &Config{
Addr: envOr("SCANNER_ADDR", ":9090"),
HoldURL: os.Getenv("SCANNER_HOLD_URL"),
SharedSecret: os.Getenv("SCANNER_SHARED_SECRET"),
Workers: envIntOr("SCANNER_WORKERS", 2),
QueueSize: envIntOr("SCANNER_QUEUE_SIZE", 100),
VulnEnabled: envBoolOr("SCANNER_VULN_ENABLED", true),
VulnDBPath: envOr("SCANNER_VULN_DB_PATH", "/var/lib/atcr-scanner/vulndb"),
TmpDir: envOr("SCANNER_TMP_DIR", "/var/lib/atcr-scanner/tmp"),
}
if cfg.HoldURL == "" {
return nil, fmt.Errorf("SCANNER_HOLD_URL is required")
}
if cfg.SharedSecret == "" {
return nil, fmt.Errorf("SCANNER_SHARED_SECRET is required")
}
return cfg, nil
}
func envOr(key, fallback string) string {
if v := os.Getenv(key); v != "" {
return v
}
return fallback
}
func envIntOr(key string, fallback int) int {
if v := os.Getenv(key); v != "" {
if n, err := strconv.Atoi(v); err == nil {
return n
}
}
return fallback
}
func envBoolOr(key string, fallback bool) bool {
if v := os.Getenv(key); v != "" {
if b, err := strconv.ParseBool(v); err == nil {
return b
}
}
return fallback
}