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