|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/spf13/viper" |
| 8 | +) |
| 9 | + |
| 10 | +// Config represents the complete application configuration |
| 11 | +type Config struct { |
| 12 | + Central CentralConfig `mapstructure:"central"` |
| 13 | + Global GlobalConfig `mapstructure:"global"` |
| 14 | + Tools ToolsConfig `mapstructure:"tools"` |
| 15 | +} |
| 16 | + |
| 17 | +// CentralConfig contains StackRox Central connection configuration |
| 18 | +type CentralConfig struct { |
| 19 | + URL string `mapstructure:"url"` |
| 20 | + Insecure bool `mapstructure:"insecure"` |
| 21 | + ForceHTTP1 bool `mapstructure:"force_http1"` |
| 22 | +} |
| 23 | + |
| 24 | +// GlobalConfig contains global MCP server configuration |
| 25 | +type GlobalConfig struct { |
| 26 | + ReadOnlyTools bool `mapstructure:"read_only_tools"` |
| 27 | +} |
| 28 | + |
| 29 | +// ToolsConfig contains configuration for individual MCP tools |
| 30 | +type ToolsConfig struct { |
| 31 | + Vulnerability VulnerabilityConfig `mapstructure:"vulnerability"` |
| 32 | + ConfigManager ConfigManagerConfig `mapstructure:"config_manager"` |
| 33 | +} |
| 34 | + |
| 35 | +// VulnerabilityConfig contains configuration for vulnerability management tools |
| 36 | +type VulnerabilityConfig struct { |
| 37 | + Enabled bool `mapstructure:"enabled"` |
| 38 | +} |
| 39 | + |
| 40 | +// ConfigManagerConfig contains configuration for config management tools |
| 41 | +type ConfigManagerConfig struct { |
| 42 | + Enabled bool `mapstructure:"enabled"` |
| 43 | +} |
| 44 | + |
| 45 | +// LoadConfig loads configuration from YAML file and environment variables. |
| 46 | +// Environment variables take precedence over YAML configuration. |
| 47 | +// Env var naming convention: STACKROX_MCP__SECTION__KEY (double underscore as separator) |
| 48 | +// configPath: optional path to YAML configuration file (can be empty) |
| 49 | +func LoadConfig(configPath string) (*Config, error) { |
| 50 | + v := viper.New() |
| 51 | + |
| 52 | + setDefaults(v) |
| 53 | + |
| 54 | + // Set up environment variable support |
| 55 | + // Note: SetEnvPrefix adds a single underscore, so "STACKROX_MCP_" becomes the prefix |
| 56 | + // We want double underscores between sections, so we use "__" in the replacer |
| 57 | + v.SetEnvPrefix("STACKROX_MCP_") |
| 58 | + v.SetEnvKeyReplacer(strings.NewReplacer(".", "__")) |
| 59 | + v.AutomaticEnv() |
| 60 | + |
| 61 | + if configPath != "" { |
| 62 | + v.SetConfigFile(configPath) |
| 63 | + if err := v.ReadInConfig(); err != nil { |
| 64 | + return nil, fmt.Errorf("failed to read config file: %w", err) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + var cfg Config |
| 69 | + if err := v.Unmarshal(&cfg); err != nil { |
| 70 | + return nil, fmt.Errorf("failed to unmarshal config: %w", err) |
| 71 | + } |
| 72 | + |
| 73 | + if err := cfg.Validate(); err != nil { |
| 74 | + return nil, fmt.Errorf("invalid configuration: %w", err) |
| 75 | + } |
| 76 | + |
| 77 | + return &cfg, nil |
| 78 | +} |
| 79 | + |
| 80 | +// setDefaults sets default values for configuration |
| 81 | +func setDefaults(v *viper.Viper) { |
| 82 | + v.SetDefault("central.url", "central.stackrox:8443") |
| 83 | + v.SetDefault("central.insecure", false) |
| 84 | + v.SetDefault("central.force_http1", false) |
| 85 | + |
| 86 | + v.SetDefault("global.read_only_tools", true) |
| 87 | + |
| 88 | + v.SetDefault("tools.vulnerability.enabled", false) |
| 89 | + v.SetDefault("tools.config_manager.enabled", false) |
| 90 | +} |
| 91 | + |
| 92 | +// Validate validates the configuration |
| 93 | +func (c *Config) Validate() error { |
| 94 | + if c.Central.URL == "" { |
| 95 | + return fmt.Errorf("central.url is required") |
| 96 | + } |
| 97 | + |
| 98 | + if !c.Tools.Vulnerability.Enabled && !c.Tools.ConfigManager.Enabled { |
| 99 | + return fmt.Errorf("at least one tool has to be enabled") |
| 100 | + } |
| 101 | + |
| 102 | + return nil |
| 103 | +} |
0 commit comments