| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/spf13/viper" |
| 9 | ) |
| 10 | |
| 11 | // minimalValidYAML is the smallest config that passes validateConfig. |
| 12 | // It only sets the fields that validateConfig strictly requires, leaving |
| 13 | // everything else at its viper default so we can assert default values. |
| 14 | const minimalValidYAML = ` |
| 15 | webserver: |
| 16 | listen_addr: "0.0.0.0" |
| 17 | listen_port: 8080 |
| 18 | full_url: "/full-stream" |
| 19 | lite_url: "/" |
| 20 | domains_only_url: "/domains-only" |
| 21 | ` |
| 22 | |
| 23 | // writeConfigFile writes content to a temporary YAML file and returns its path. |
| 24 | func writeConfigFile(t *testing.T, content string) string { |
| 25 | t.Helper() |
| 26 | path := filepath.Join(t.TempDir(), "config.yaml") |
| 27 | if err := os.WriteFile(path, []byte(content), 0o644); err != nil { |
| 28 | t.Fatalf("failed to write temp config file: %v", err) |
| 29 | } |
| 30 | return path |
| 31 | } |
| 32 | |
| 33 | // TestWebserverDefaults uses a config file that only sets required |
| 34 | // webserver fields so that optional keys still reflect their defaults. |
| 35 | func TestWebserverDefaults(t *testing.T) { |
| 36 | // Only listen_addr and listen_port are set. Remaining keys should be default. |
| 37 | yaml := ` |
| 38 | webserver: |
| 39 | listen_addr: "0.0.0.0" |
| 40 | listen_port: 8080 |
| 41 | ` |
| 42 | configPath := writeConfigFile(t, yaml) |
| 43 | v := initViper(configPath) |
| 44 | |
| 45 | cases := []struct { |
| 46 | key string |
| 47 | want interface{} |
| 48 | }{ |
| 49 | {"webserver.full_url", "/full-stream"}, |
| 50 | {"webserver.lite_url", "/"}, |
| 51 | {"webserver.domains_only_url", "/domains-only"}, |
| 52 | {"webserver.real_ip", false}, |
| 53 | {"webserver.compression_enabled", false}, |
| 54 | } |
| 55 | |
| 56 | for _, testcase := range cases { |
| 57 | switch want := testcase.want.(type) { |
| 58 | case string: |
| 59 | if got := v.GetString(testcase.key); got != want { |
| 60 | t.Errorf("key %s: want %q, got %q", testcase.key, want, got) |
| 61 | } |
| 62 | case bool: |
| 63 | if got := v.GetBool(testcase.key); got != want { |
| 64 | t.Errorf("key %s: want %v, got %v", testcase.key, want, got) |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // TestPrometheusDefaults uses a config file that only sets required |
| 71 | // webserver fields so that optional keys still reflect their defaults. |
| 72 | func TestPrometheusDefaults(t *testing.T) { |
| 73 | // No prometheus section in the config – all keys should come from defaults. |
| 74 | configPath := writeConfigFile(t, minimalValidYAML) |
| 75 | v := initViper(configPath) |
| 76 | |
| 77 | if got := v.GetBool("prometheus.enabled"); got != false { |
| 78 | t.Errorf("prometheus.enabled: want false, got %v", got) |
| 79 | } |
| 80 | if got := v.GetString("prometheus.listen_addr"); got != "0.0.0.0" { |
| 81 | t.Errorf("prometheus.listen_addr: want '0.0.0.0', got %q", got) |
| 82 | } |
| 83 | if got := v.GetInt("prometheus.listen_port"); got != 9090 { |
| 84 | t.Errorf("prometheus.listen_port: want 9090, got %d", got) |
| 85 | } |
| 86 | if got := v.GetString("prometheus.metrics_url"); got != "/metrics" { |
| 87 | t.Errorf("prometheus.metrics_url: want '/metrics', got %q", got) |
| 88 | } |
| 89 | if got := v.GetBool("prometheus.expose_system_metrics"); got != false { |
| 90 | t.Errorf("prometheus.expose_system_metrics: want false, got %v", got) |
| 91 | } |
| 92 | if got := v.GetBool("prometheus.real_ip"); got != false { |
| 93 | t.Errorf("prometheus.real_ip: want false, got %v", got) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // TestGeneralDefaults uses an empty string as configPath. |
| 98 | // It tests whether the defaults are properly configured. |
| 99 | func TestGeneralDefaults(t *testing.T) { |
| 100 | // No general section in the config – all keys should come from defaults. |
| 101 | |
| 102 | v := initViper("") |
| 103 | |
| 104 | if got := v.GetBool("general.disable_default_logs"); got != false { |
| 105 | t.Errorf("general.disable_default_logs: want false, got %v", got) |
| 106 | } |
| 107 | if got := v.GetInt("general.buffer_sizes.websocket"); got != 300 { |
| 108 | t.Errorf("general.buffer_sizes.websocket: want 300, got %d", got) |
| 109 | } |
| 110 | if got := v.GetInt("general.buffer_sizes.ctlog"); got != 1000 { |
| 111 | t.Errorf("general.buffer_sizes.ctlog: want 1000, got %d", got) |
| 112 | } |
| 113 | if got := v.GetInt("general.buffer_sizes.broadcastmanager"); got != 10000 { |
| 114 | t.Errorf("general.buffer_sizes.broadcastmanager: want 10000, got %d", got) |
| 115 | } |
| 116 | if got := v.GetBool("general.drop_old_logs"); got != true { |
| 117 | t.Errorf("general.drop_old_logs: want true, got %v", got) |
| 118 | } |
| 119 | if got := v.GetBool("general.recovery.enabled"); got != false { |
| 120 | t.Errorf("general.recovery.enabled: want false, got %v", got) |
| 121 | } |
| 122 | if got := v.GetString("general.recovery.ct_index_file"); got != "./ct_index.json" { |
| 123 | t.Errorf("general.recovery.ct_index_file: want './ct_index.json', got %q", got) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // TestConfigFileOverridesDefaults verifies that values from the config file override |
| 128 | // the defaults, while keys absent from the file still return their default values. |
| 129 | func TestConfigFileOverridesDefaults(t *testing.T) { |
| 130 | yaml := ` |
| 131 | webserver: |
| 132 | listen_addr: "127.0.0.1" |
| 133 | listen_port: 9999 |
| 134 | ` |
| 135 | configPath := writeConfigFile(t, yaml) |
| 136 | v := initViper(configPath) |
| 137 | |
| 138 | if got := v.GetString("webserver.listen_addr"); got != "127.0.0.1" { |
| 139 | t.Errorf("listen_addr: want '127.0.0.1', got %q", got) |
| 140 | } |
| 141 | if got := v.GetInt("webserver.listen_port"); got != 9999 { |
| 142 | t.Errorf("listen_port: want 9999, got %d", got) |
| 143 | } |
| 144 | |
| 145 | // Keys absent from the file must still return the registered defaults. |
| 146 | if got := v.GetString("webserver.full_url"); got != "/full-stream" { |
| 147 | t.Errorf("full_url (default): want '/full-stream', got %q", got) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // TestEnvOverridesDefaults verifies that environment variables |
| 152 | // take precedence over defaults when no config file is provided. |
| 153 | func TestEnvOverridesDefaults(t *testing.T) { |
| 154 | t.Setenv("CERTSTREAM_WEBSERVER_LISTEN_PORT", "6543") |
| 155 | |
| 156 | configPath := writeConfigFile(t, minimalValidYAML) |
| 157 | v := initViper(configPath) |
| 158 | |
| 159 | if got := v.GetInt("webserver.listen_port"); got != 6543 { |
| 160 | t.Errorf("listen_port via env: want 6543, got %d", got) |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // TestEnvOverridesConfigFile verifies that environment variables |
| 165 | // take precedence over config file values. |
| 166 | func TestEnvOverridesConfigFile(t *testing.T) { |
| 167 | configPath := writeConfigFile(t, minimalValidYAML) |
| 168 | t.Setenv("CERTSTREAM_WEBSERVER_LISTEN_PORT", "7777") |
| 169 | |
| 170 | v := initViper(configPath) |
| 171 | |
| 172 | if got := v.GetInt("webserver.listen_port"); got != 7777 { |
| 173 | t.Errorf("listen_port via env: want 7777, got %d", got) |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // TestEnvOverridesPrometheusPort verifies that environment variables |
| 178 | // can override config file values for the prometheus.listen_port key. |
| 179 | func TestEnvOverridesPrometheusPort(t *testing.T) { |
| 180 | configPath := writeConfigFile(t, minimalValidYAML) |
| 181 | t.Setenv("CERTSTREAM_PROMETHEUS_LISTEN_PORT", "19090") |
| 182 | |
| 183 | v := initViper(configPath) |
| 184 | |
| 185 | if got := v.GetInt("prometheus.listen_port"); got != 19090 { |
| 186 | t.Errorf("prometheus.listen_port via env: want 19090, got %d", got) |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // viperFromYAML creates a fully initialized *viper.Viper from an in-memory |
| 191 | // YAML string by writing it to a temp file and calling initViper. This |
| 192 | // exercises the same code path as production (defaults + file merge) and |
| 193 | // correctly handles the yaml:",inline" embedded struct tags via mapstructure. |
| 194 | func viperFromYAML(t *testing.T, content string) *viper.Viper { |
| 195 | t.Helper() |
| 196 | return initViper(writeConfigFile(t, content)) |
| 197 | } |
| 198 | |
| 199 | // validViperInstance returns a viper instance built from minimalValidYAML. |
| 200 | func validViperInstance(t *testing.T) *viper.Viper { |
| 201 | t.Helper() |
| 202 | return viperFromYAML(t, minimalValidYAML) |
| 203 | } |
| 204 | |
| 205 | // TestLoadConfigFromViper_UnmarshalsWebserver tests if the nested Webserver |
| 206 | // struct is unmarshalled properly. |
| 207 | func TestLoadConfigFromViper_UnmarshalsWebserver(t *testing.T) { |
| 208 | v := validViperInstance(t) |
| 209 | cfg, err := loadConfigFromViper(v) |
| 210 | if err != nil { |
| 211 | t.Fatalf("unexpected error: %v", err) |
| 212 | } |
| 213 | |
| 214 | if cfg.Webserver.ListenAddr != "0.0.0.0" { |
| 215 | t.Errorf("ListenAddr: want '0.0.0.0', got %q", cfg.Webserver.ListenAddr) |
| 216 | } |
| 217 | if cfg.Webserver.ListenPort != 8080 { |
| 218 | t.Errorf("ListenPort: want 8080, got %d", cfg.Webserver.ListenPort) |
| 219 | } |
| 220 | if cfg.Webserver.FullURL != "/full-stream" { |
| 221 | t.Errorf("FullURL: want '/full-stream', got %q", cfg.Webserver.FullURL) |
| 222 | } |
| 223 | if cfg.Webserver.LiteURL != "/" { |
| 224 | t.Errorf("LiteURL: want '/', got %q", cfg.Webserver.LiteURL) |
| 225 | } |
| 226 | if cfg.Webserver.DomainsOnlyURL != "/domains-only" { |
| 227 | t.Errorf("DomainsOnlyURL: want '/domains-only', got %q", cfg.Webserver.DomainsOnlyURL) |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // TestLoadConfigFromViper_UnmarshalsBufferSizes tests if the nested BufferSizes |
| 232 | // struct is unmarshalled properly. |
| 233 | func TestLoadConfigFromViper_UnmarshalsBufferSizes(t *testing.T) { |
| 234 | v := viperFromYAML(t, ` |
| 235 | webserver: |
| 236 | listen_addr: "0.0.0.0" |
| 237 | listen_port: 8080 |
| 238 | full_url: "/full-stream" |
| 239 | lite_url: "/" |
| 240 | domains_only_url: "/domains-only" |
| 241 | general: |
| 242 | buffer_sizes: |
| 243 | websocket: 512 |
| 244 | ctlog: 2048 |
| 245 | broadcastmanager: 4096 |
| 246 | `) |
| 247 | cfg, err := loadConfigFromViper(v) |
| 248 | if err != nil { |
| 249 | t.Fatalf("unexpected error: %v", err) |
| 250 | } |
| 251 | |
| 252 | if cfg.General.BufferSizes.Websocket != 512 { |
| 253 | t.Errorf("BufferSizes.Websocket: want 512, got %d", cfg.General.BufferSizes.Websocket) |
| 254 | } |
| 255 | if cfg.General.BufferSizes.CTLog != 2048 { |
| 256 | t.Errorf("BufferSizes.CTLog: want 2048, got %d", cfg.General.BufferSizes.CTLog) |
| 257 | } |
| 258 | if cfg.General.BufferSizes.BroadcastManager != 4096 { |
| 259 | t.Errorf("BufferSizes.BroadcastManager: want 4096, got %d", cfg.General.BufferSizes.BroadcastManager) |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | // TestLoadConfigFromViper_SetsAppConfig verifies that loadConfigFromViper updates |
| 264 | // the global AppConfig variable to match the loaded configuration. |
| 265 | func TestLoadConfigFromViper_SetsAppConfig(t *testing.T) { |
| 266 | v := validViperInstance(t) |
| 267 | cfg, err := loadConfigFromViper(v) |
| 268 | if err != nil { |
| 269 | t.Fatalf("unexpected error: %v", err) |
| 270 | } |
| 271 | |
| 272 | if AppConfig.Webserver.ListenPort != cfg.Webserver.ListenPort { |
| 273 | t.Errorf("AppConfig.Webserver.ListenPort not synced: want %d, got %d", |
| 274 | cfg.Webserver.ListenPort, AppConfig.Webserver.ListenPort) |
| 275 | } |
| 276 | if AppConfig.Webserver.ListenAddr != cfg.Webserver.ListenAddr { |
| 277 | t.Errorf("AppConfig.Webserver.ListenAddr not synced: want %q, got %q", |
| 278 | cfg.Webserver.ListenAddr, AppConfig.Webserver.ListenAddr) |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | // TestLoadConfigFromViper_RecoverySettings tests if the nested BufferSizes |
| 283 | // struct is unmarshalled properly. |
| 284 | func TestLoadConfigFromViper_RecoverySettings(t *testing.T) { |
| 285 | v := viperFromYAML(t, ` |
| 286 | webserver: |
| 287 | listen_addr: "0.0.0.0" |
| 288 | listen_port: 8080 |
| 289 | full_url: "/full-stream" |
| 290 | lite_url: "/" |
| 291 | domains_only_url: "/domains-only" |
| 292 | general: |
| 293 | recovery: |
| 294 | enabled: true |
| 295 | ct_index_file: "/tmp/my_index.json" |
| 296 | `) |
| 297 | cfg, err := loadConfigFromViper(v) |
| 298 | if err != nil { |
| 299 | t.Fatalf("unexpected error: %v", err) |
| 300 | } |
| 301 | |
| 302 | if !cfg.General.Recovery.Enabled { |
| 303 | t.Errorf("Recovery.Enabled: want true, got false") |
| 304 | } |
| 305 | if cfg.General.Recovery.CTIndexFile != "/tmp/my_index.json" { |
| 306 | t.Errorf("Recovery.CTIndexFile: want '/tmp/my_index.json', got %q", cfg.General.Recovery.CTIndexFile) |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | func TestReadConfigViper_MinimalValidFile(t *testing.T) { |
| 311 | configPath := writeConfigFile(t, minimalValidYAML) |
| 312 | |
| 313 | cfg, err := ReadConfig(configPath) |
| 314 | if err != nil { |
| 315 | t.Fatalf("unexpected error: %v", err) |
| 316 | } |
| 317 | |
| 318 | if cfg.Webserver.ListenAddr != "0.0.0.0" { |
| 319 | t.Errorf("ListenAddr: want '0.0.0.0', got %q", cfg.Webserver.ListenAddr) |
| 320 | } |
| 321 | if cfg.Webserver.ListenPort != 8080 { |
| 322 | t.Errorf("ListenPort: want 8080, got %d", cfg.Webserver.ListenPort) |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | func TestReadConfigViper_PrometheusSection(t *testing.T) { |
| 327 | yaml := minimalValidYAML + ` |
| 328 | prometheus: |
| 329 | enabled: true |
| 330 | listen_addr: "0.0.0.0" |
| 331 | listen_port: 9090 |
| 332 | metrics_url: "/metrics" |
| 333 | expose_system_metrics: true |
| 334 | ` |
| 335 | configPath := writeConfigFile(t, yaml) |
| 336 | |
| 337 | cfg, err := ReadConfig(configPath) |
| 338 | if err != nil { |
| 339 | t.Fatalf("unexpected error: %v", err) |
| 340 | } |
| 341 | |
| 342 | if !cfg.Prometheus.Enabled { |
| 343 | t.Errorf("Prometheus.Enabled: want true, got false") |
| 344 | } |
| 345 | if cfg.Prometheus.ListenPort != 9090 { |
| 346 | t.Errorf("Prometheus.ListenPort: want 9090, got %d", cfg.Prometheus.ListenPort) |
| 347 | } |
| 348 | if cfg.Prometheus.MetricsURL != "/metrics" { |
| 349 | t.Errorf("Prometheus.MetricsURL: want '/metrics', got %q", cfg.Prometheus.MetricsURL) |
| 350 | } |
| 351 | if !cfg.Prometheus.ExposeSystemMetrics { |
| 352 | t.Errorf("Prometheus.ExposeSystemMetrics: want true, got false") |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | func TestReadConfigViper_CustomBufferSizes(t *testing.T) { |
| 357 | yaml := minimalValidYAML + ` |
| 358 | general: |
| 359 | buffer_sizes: |
| 360 | websocket: 500 |
| 361 | ctlog: 2000 |
| 362 | broadcastmanager: 20000 |
| 363 | ` |
| 364 | configPath := writeConfigFile(t, yaml) |
| 365 | |
| 366 | cfg, err := ReadConfig(configPath) |
| 367 | if err != nil { |
| 368 | t.Fatalf("unexpected error: %v", err) |
| 369 | } |
| 370 | |
| 371 | if cfg.General.BufferSizes.Websocket != 500 { |
| 372 | t.Errorf("BufferSizes.Websocket: want 500, got %d", cfg.General.BufferSizes.Websocket) |
| 373 | } |
| 374 | if cfg.General.BufferSizes.CTLog != 2000 { |
| 375 | t.Errorf("BufferSizes.CTLog: want 2000, got %d", cfg.General.BufferSizes.CTLog) |
| 376 | } |
| 377 | if cfg.General.BufferSizes.BroadcastManager != 20000 { |
| 378 | t.Errorf("BufferSizes.BroadcastManager: want 20000, got %d", cfg.General.BufferSizes.BroadcastManager) |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | func TestReadConfigViper_AdditionalLogs(t *testing.T) { |
| 383 | yaml := minimalValidYAML + ` |
| 384 | general: |
| 385 | additional_logs: |
| 386 | - url: "https://ct.example.com/log" |
| 387 | operator: "Example" |
| 388 | description: "Example CT log" |
| 389 | ` |
| 390 | configPath := writeConfigFile(t, yaml) |
| 391 | |
| 392 | cfg, err := ReadConfig(configPath) |
| 393 | if err != nil { |
| 394 | t.Fatalf("unexpected error: %v", err) |
| 395 | } |
| 396 | |
| 397 | if len(cfg.General.AdditionalLogs) != 1 { |
| 398 | t.Fatalf("AdditionalLogs: want 1 entry, got %d", len(cfg.General.AdditionalLogs)) |
| 399 | } |
| 400 | if cfg.General.AdditionalLogs[0].URL != "https://ct.example.com/log" { |
| 401 | t.Errorf("AdditionalLogs[0].URL: want 'https://ct.example.com/log', got %q", |
| 402 | cfg.General.AdditionalLogs[0].URL) |
| 403 | } |
| 404 | if cfg.General.AdditionalLogs[0].Operator != "Example" { |
| 405 | t.Errorf("AdditionalLogs[0].Operator: want 'Example', got %q", |
| 406 | cfg.General.AdditionalLogs[0].Operator) |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | func TestReadConfigViper_InvalidAdditionalLogURLIgnored(t *testing.T) { |
| 411 | yaml := minimalValidYAML + ` |
| 412 | general: |
| 413 | additional_logs: |
| 414 | - url: "not-a-valid-url" |
| 415 | operator: "Bad" |
| 416 | description: "Invalid log" |
| 417 | - url: "https://ct.example.com/valid-log" |
| 418 | operator: "Good" |
| 419 | description: "Valid log" |
| 420 | ` |
| 421 | configPath := writeConfigFile(t, yaml) |
| 422 | |
| 423 | cfg, err := ReadConfig(configPath) |
| 424 | if err != nil { |
| 425 | t.Fatalf("unexpected error: %v", err) |
| 426 | } |
| 427 | |
| 428 | if len(cfg.General.AdditionalLogs) != 1 { |
| 429 | t.Errorf("AdditionalLogs: want 1 valid entry (invalid filtered), got %d", len(cfg.General.AdditionalLogs)) |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | func TestReadConfigViper_RecoveryConfig(t *testing.T) { |
| 434 | yaml := minimalValidYAML + ` |
| 435 | general: |
| 436 | recovery: |
| 437 | enabled: true |
| 438 | ct_index_file: "./my_index.json" |
| 439 | ` |
| 440 | configPath := writeConfigFile(t, yaml) |
| 441 | |
| 442 | cfg, err := ReadConfig(configPath) |
| 443 | if err != nil { |
| 444 | t.Fatalf("unexpected error: %v", err) |
| 445 | } |
| 446 | |
| 447 | if !cfg.General.Recovery.Enabled { |
| 448 | t.Errorf("Recovery.Enabled: want true, got false") |
| 449 | } |
| 450 | if cfg.General.Recovery.CTIndexFile != "./my_index.json" { |
| 451 | t.Errorf("Recovery.CTIndexFile: want './my_index.json', got %q", cfg.General.Recovery.CTIndexFile) |
| 452 | } |
| 453 | } |