-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconfig.go
More file actions
922 lines (798 loc) · 27.9 KB
/
Copy pathconfig.go
File metadata and controls
922 lines (798 loc) · 27.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
// Package config provides configuration loading and validation for the Minexus application
// It supports environment-specific configurations, command line flags, and strict validation
// to ensure correct application behavior across different environments.
package config
import (
"bufio"
"flag"
"fmt"
"net"
"os"
"strconv"
"strings"
"time"
"go.uber.org/zap"
"github.com/arhuman/minexus/internal/logging"
)
// Environment constants for MINEXUS_ENV
const (
EnvProduction = "prod"
EnvTest = "test"
)
// DetectEnvironment returns the current environment based on MINEXUS_ENV
// Panics on invalid environment values to prevent configuration errors
func DetectEnvironment() string {
env := os.Getenv("MINEXUS_ENV")
if env == "" {
env = EnvTest // Default to test
}
// Strict validation - panic on invalid environment to prevent typos
validEnvs := map[string]bool{
EnvProduction: true,
EnvTest: true,
}
if !validEnvs[env] {
panic(fmt.Sprintf("Invalid MINEXUS_ENV value: '%s'. Valid values are: %s, %s",
env, EnvProduction, EnvTest))
}
return env
}
// GetEnvironmentFileName returns the environment-specific filename for the current environment
func GetEnvironmentFileName() string {
env := DetectEnvironment()
return fmt.Sprintf(".env.%s", env)
}
// ValidationError represents a configuration validation error
type ValidationError struct {
Field string
Value string
Message string
}
func (e ValidationError) Error() string {
return fmt.Sprintf("configuration validation failed for %s=%s: %s", e.Field, e.Value, e.Message)
}
// Loader provides unified configuration loading with priority handling
type Loader struct {
envVars map[string]string
logger *zap.Logger
}
// NewConfigLoader creates a new configuration loader
func NewConfigLoader() *Loader {
return &Loader{
envVars: make(map[string]string),
}
}
// WithLogger sets the logger for the config loader
func (cl *Loader) WithLogger(logger *zap.Logger) *Loader {
cl.logger = logger
return cl
}
// LoadEnvironmentFile loads environment variables from environment-specific file
// Uses strict validation with no fallback - panics if environment file is missing
func (cl *Loader) LoadEnvironmentFile() error {
env := DetectEnvironment() // Panics on invalid environment
filename := GetEnvironmentFileName()
// Strict loading - no fallback, panic if file doesn't exist
file, err := os.Open(filename)
if err != nil {
panic(fmt.Sprintf("Required environment file '%s' not found for environment '%s'. "+
"Create this file from env.sample or set MINEXUS_ENV to a valid environment (prod, test)",
filename, env))
}
defer file.Close()
scanner := bufio.NewScanner(file)
lineNum := 0
for scanner.Scan() {
lineNum++
line := strings.TrimSpace(scanner.Text())
// Skip empty lines and comments
if line == "" || strings.HasPrefix(line, "#") {
continue
}
// Parse KEY=VALUE format
parts := strings.SplitN(line, "=", 2)
if len(parts) != 2 {
if cl.logger != nil {
cl.logger.Warn("Invalid line in env file",
zap.String("file", filename),
zap.Int("line", lineNum),
zap.String("content", line))
}
continue
}
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
// Remove quotes if present
if len(value) >= 2 {
if (strings.HasPrefix(value, `"`) && strings.HasSuffix(value, `"`)) ||
(strings.HasPrefix(value, `'`) && strings.HasSuffix(value, `'`)) {
value = value[1 : len(value)-1]
}
}
cl.envVars[key] = value
}
if err := scanner.Err(); err != nil {
panic(fmt.Sprintf("Error reading environment file '%s': %v", filename, err))
}
if cl.logger != nil {
cl.logger.Info("Loaded environment file",
zap.String("file", filename),
zap.String("environment", env),
zap.Int("variables", len(cl.envVars)))
}
return nil
}
// GetString gets string value with priority: flags → env → file → default
func (cl *Loader) GetString(key, defaultValue string) string {
// Check environment variables first (highest priority after flags)
if value := os.Getenv(key); value != "" {
return value
}
// Check .env file variables
if value, exists := cl.envVars[key]; exists {
return value
}
// Return default
return defaultValue
}
// GetInt gets int value with validation
func (cl *Loader) GetInt(key string, defaultValue int) (int, error) {
value := cl.GetString(key, "")
if value == "" {
return defaultValue, nil
}
intVal, err := strconv.Atoi(value)
if err != nil {
return 0, ValidationError{
Field: key,
Value: value,
Message: "must be a valid integer",
}
}
return intVal, nil
}
// GetIntInRange gets int value with range validation
func (cl *Loader) GetIntInRange(key string, defaultValue, minValue, maxValue int) (int, error) {
value, err := cl.GetInt(key, defaultValue)
if err != nil {
return 0, err
}
if value < minValue || value > maxValue {
return 0, ValidationError{
Field: key,
Value: strconv.Itoa(value),
Message: fmt.Sprintf("must be between %d and %d", minValue, maxValue),
}
}
return value, nil
}
// GetBool gets bool value with validation
func (cl *Loader) GetBool(key string, defaultValue bool) (bool, error) {
value := cl.GetString(key, "")
if value == "" {
return defaultValue, nil
}
boolVal, err := strconv.ParseBool(value)
if err != nil {
return false, ValidationError{
Field: key,
Value: value,
Message: "must be true/false, 1/0, or yes/no",
}
}
return boolVal, nil
}
// GetDuration gets duration value with validation
func (cl *Loader) GetDuration(key string, defaultValue time.Duration) (time.Duration, error) {
value := cl.GetString(key, "")
if value == "" {
return defaultValue, nil
}
// Try parsing as duration first
if duration, err := time.ParseDuration(value); err == nil {
return duration, nil
}
// Try parsing as seconds (for backward compatibility)
if seconds, err := strconv.Atoi(value); err == nil {
return time.Duration(seconds) * time.Second, nil
}
return 0, ValidationError{
Field: key,
Value: value,
Message: "must be a valid duration (e.g., '10s', '5m') or number of seconds",
}
}
// ValidateNetworkAddress validates a network address
func (cl *Loader) ValidateNetworkAddress(key, value string) error {
if value == "" {
return ValidationError{
Field: key,
Value: value,
Message: "network address cannot be empty",
}
}
host, port, err := net.SplitHostPort(value)
if err != nil {
return ValidationError{
Field: key,
Value: value,
Message: "must be in format 'host:port'",
}
}
// Validate port range
if portNum, err := strconv.Atoi(port); err != nil || portNum < 1 || portNum > 65535 {
return ValidationError{
Field: key,
Value: value,
Message: "port must be between 1 and 65535",
}
}
// Validate host (basic check)
if host == "" {
return ValidationError{
Field: key,
Value: value,
Message: "host cannot be empty",
}
}
return nil
}
// ValidateHostname validates a hostname (without port)
func (cl *Loader) ValidateHostname(key, value string) error {
if value == "" {
return ValidationError{
Field: key,
Value: value,
Message: "hostname cannot be empty",
}
}
// Check if it contains a port (which it shouldn't)
if strings.Contains(value, ":") {
return ValidationError{
Field: key,
Value: value,
Message: "should contain only hostname, not host:port format",
}
}
return nil
}
// ValidateRequired ensures a required field is not empty
func (cl *Loader) ValidateRequired(key, value string) error {
if value == "" {
return ValidationError{
Field: key,
Value: value,
Message: "is required and cannot be empty",
}
}
return nil
}
// ValidateDirectory ensures a directory path is valid
func (cl *Loader) ValidateDirectory(key, value string) error {
if value == "" {
return ValidationError{
Field: key,
Value: value,
Message: "directory path cannot be empty",
}
}
info, err := os.Stat(value)
if err != nil {
if os.IsNotExist(err) {
return ValidationError{
Field: key,
Value: value,
Message: "directory does not exist",
}
}
return ValidationError{
Field: key,
Value: value,
Message: fmt.Sprintf("cannot access directory: %v", err),
}
}
if !info.IsDir() {
return ValidationError{
Field: key,
Value: value,
Message: "path exists but is not a directory",
}
}
return nil
}
// ConsoleConfig holds configuration for the console client
type ConsoleConfig struct {
ServerAddr string
ConnectTimeout int // seconds
Debug bool
}
// NexusConfig holds configuration for the Nexus server
type NexusConfig struct {
MinionPort int // Port for minion connections with standard TLS
ConsolePort int // Port for console connections with mTLS
WebPort int // Port for HTTP web server
WebEnabled bool // Enable/disable web server
WebRoot string // Path to webroot directory (for file system assets)
DBHost string
DBPort int
DBUser string
DBPassword string
DBName string
DBSSLMode string
Debug bool
MaxMsgSize int
FileRoot string
}
// MinionConfig holds configuration for Minion clients
type MinionConfig struct {
ServerAddr string
ID string
Debug bool
ConnectTimeout int // seconds
InitialReconnectDelay int // seconds - starting delay for exponential backoff
MaxReconnectDelay int // seconds - maximum delay cap for exponential backoff
HeartbeatInterval int // seconds
DefaultShellTimeout int // seconds - default timeout for shell command execution
StreamTimeout int // seconds - timeout for stream operations
}
// DefaultConsoleConfig returns default configuration for Console
func DefaultConsoleConfig() *ConsoleConfig {
return &ConsoleConfig{
ServerAddr: "localhost:11973", // Will be constructed from NEXUS_SERVER + NEXUS_CONSOLE_PORT
ConnectTimeout: 10,
Debug: false,
}
}
// DefaultNexusConfig returns default configuration for Nexus
func DefaultNexusConfig() *NexusConfig {
return &NexusConfig{
MinionPort: 11972,
ConsolePort: 11973,
WebPort: 8086,
WebEnabled: true,
WebRoot: "./webroot",
DBHost: "localhost",
DBPort: 5432,
DBUser: "postgres",
DBPassword: "postgres",
DBName: "minexus",
DBSSLMode: "disable",
Debug: false,
MaxMsgSize: 1024 * 1024 * 10, // 10MB
FileRoot: "/tmp",
}
}
// DefaultMinionConfig returns default configuration for Minion
func DefaultMinionConfig() *MinionConfig {
return &MinionConfig{
ServerAddr: "localhost:11972", // Will be constructed from NEXUS_SERVER + NEXUS_MINION_PORT
ID: "", // Will be auto-generated if empty
Debug: false,
ConnectTimeout: 3,
InitialReconnectDelay: 1, // 1 second initial delay
MaxReconnectDelay: 300, // 5 minutes maximum delay
HeartbeatInterval: 30,
DefaultShellTimeout: 15, // 15 seconds default shell timeout
StreamTimeout: 30, // 30 seconds stream timeout (reduced from 90s hardcoded)
}
}
// LoadConsoleConfig loads console configuration with validation
func LoadConsoleConfig() (*ConsoleConfig, error) {
loader := NewConfigLoader()
if err := loader.LoadEnvironmentFile(); err != nil {
return nil, fmt.Errorf("failed to load environment file: %w", err)
}
config := DefaultConsoleConfig()
var validationErrors []error
// Load and validate server hostname
nexusServer := loader.GetString("NEXUS_SERVER", "localhost")
if err := loader.ValidateHostname("NEXUS_SERVER", nexusServer); err != nil {
validationErrors = append(validationErrors, err)
}
// Load and validate console port
consolePort, err := loader.GetIntInRange("NEXUS_CONSOLE_PORT", 11973, 1, 65535)
if err != nil {
validationErrors = append(validationErrors, err)
}
// Construct server address from hostname and port
config.ServerAddr = fmt.Sprintf("%s:%d", nexusServer, consolePort)
// Load and validate connect timeout
if timeout, err := loader.GetIntInRange("CONNECT_TIMEOUT", config.ConnectTimeout, 1, 300); err != nil {
validationErrors = append(validationErrors, err)
} else {
config.ConnectTimeout = timeout
}
// Load debug flag
if debug, err := loader.GetBool("DEBUG", config.Debug); err != nil {
validationErrors = append(validationErrors, err)
} else {
config.Debug = debug
}
// Handle manual flag parsing for console (to avoid conflicts with other flag parsers)
if len(os.Args) > 1 {
for i, arg := range os.Args[1:] {
switch arg {
case "-server", "--server":
if i+1 < len(os.Args)-1 {
addr := os.Args[i+2]
// For backward compatibility, still accept host:port format in command line flags
if err := loader.ValidateNetworkAddress("server", addr); err != nil {
validationErrors = append(validationErrors, err)
} else {
config.ServerAddr = addr
}
}
case "-debug", "--debug":
config.Debug = true
case "-timeout", "--timeout":
if i+1 < len(os.Args)-1 {
if t, err := strconv.Atoi(os.Args[i+2]); err == nil {
if t < 1 || t > 300 {
validationErrors = append(validationErrors, ValidationError{
Field: "timeout",
Value: strconv.Itoa(t),
Message: "must be between 1 and 300 seconds",
})
} else {
config.ConnectTimeout = t
}
} else {
validationErrors = append(validationErrors, ValidationError{
Field: "timeout",
Value: os.Args[i+2],
Message: "must be a valid integer",
})
}
}
}
}
}
// Return validation errors if any
if len(validationErrors) > 0 {
var errMsg strings.Builder
errMsg.WriteString("Configuration validation failed:\n")
for _, err := range validationErrors {
errMsg.WriteString(fmt.Sprintf(" - %s\n", err.Error()))
}
return nil, fmt.Errorf("%s", errMsg.String())
}
return config, nil
}
// LoadNexusConfig loads Nexus configuration with validation
func LoadNexusConfig() (*NexusConfig, error) {
// Create a simple logger for configuration loading diagnostics
logger, _ := zap.NewDevelopment()
defer logger.Sync()
logger, start := logging.FuncLogger(logger, "LoadNexusConfig")
defer logging.FuncExit(logger, start)
loader := NewConfigLoader().WithLogger(logger)
if err := loader.LoadEnvironmentFile(); err != nil {
return nil, fmt.Errorf("failed to load environment file: %w", err)
}
config := DefaultNexusConfig()
var validationErrors []error
// Load and validate port (allow 0 for system-assigned port)
if port, err := loader.GetIntInRange("NEXUS_MINION_PORT", config.MinionPort, 0, 65535); err != nil {
validationErrors = append(validationErrors, err)
} else {
config.MinionPort = port
}
// Load and validate console port
if consolePort, err := loader.GetIntInRange("NEXUS_CONSOLE_PORT", config.ConsolePort, 0, 65535); err != nil {
validationErrors = append(validationErrors, err)
} else {
config.ConsolePort = consolePort
}
// Load and validate web port
if webPort, err := loader.GetIntInRange("NEXUS_WEB_PORT", config.WebPort, 0, 65535); err != nil {
validationErrors = append(validationErrors, err)
} else {
config.WebPort = webPort
}
// Load web enabled flag
if webEnabled, err := loader.GetBool("NEXUS_WEB_ENABLED", config.WebEnabled); err != nil {
validationErrors = append(validationErrors, err)
} else {
config.WebEnabled = webEnabled
}
// Load web root directory
config.WebRoot = loader.GetString("NEXUS_WEB_ROOT", config.WebRoot)
// Load database configuration
config.DBHost = loader.GetString("DBHOST", config.DBHost)
if err := loader.ValidateRequired("DBHOST", config.DBHost); err != nil {
validationErrors = append(validationErrors, err)
}
if dbPort, err := loader.GetIntInRange("DBPORT", config.DBPort, 1, 65535); err != nil {
validationErrors = append(validationErrors, err)
} else {
config.DBPort = dbPort
}
config.DBUser = loader.GetString("DBUSER", config.DBUser)
config.DBPassword = loader.GetString("DBPASS", config.DBPassword)
config.DBName = loader.GetString("DBNAME", config.DBName)
config.DBSSLMode = loader.GetString("DBSSLMODE", config.DBSSLMode)
// Load debug flag
if debug, err := loader.GetBool("DEBUG", config.Debug); err != nil {
validationErrors = append(validationErrors, err)
} else {
config.Debug = debug
}
// Load and validate max message size
if maxMsgSize, err := loader.GetIntInRange("MAX_MSG_SIZE", config.MaxMsgSize, 1024, 1024*1024*100); err != nil {
validationErrors = append(validationErrors, err)
} else {
config.MaxMsgSize = maxMsgSize
}
// Load and validate file root
config.FileRoot = loader.GetString("FILEROOT", config.FileRoot)
// Parse command line flags (highest priority)
minionPort := flag.Int("minion-port", config.MinionPort, "Port to listen on for minion connections")
consolePort := flag.Int("console-port", config.ConsolePort, "Console port for mTLS connections")
webPort := flag.Int("web-port", config.WebPort, "Port for HTTP web server")
webEnabled := flag.Bool("web-enabled", config.WebEnabled, "Enable/disable web server")
webRoot := flag.String("web-root", config.WebRoot, "Path to webroot directory")
dbHost := flag.String("db-host", config.DBHost, "Database host")
dbPort := flag.Int("db-port", config.DBPort, "Database port")
dbUser := flag.String("db-user", config.DBUser, "Database user")
dbPassword := flag.String("db-password", config.DBPassword, "Database password")
dbName := flag.String("db-name", config.DBName, "Database name")
dbSSLMode := flag.String("db-sslmode", config.DBSSLMode, "Database SSL mode")
debug := flag.Bool("debug", config.Debug, "Enable debug mode")
maxMsgSize := flag.Int("max-msg-size", config.MaxMsgSize, "Maximum message size in bytes")
fileRoot := flag.String("file-root", config.FileRoot, "File root directory")
flag.Parse()
// Apply and validate command line flags
if *minionPort < 0 || *minionPort > 65535 {
validationErrors = append(validationErrors, ValidationError{
Field: "minion-port",
Value: strconv.Itoa(*minionPort),
Message: "must be between 0 and 65535 (0 for system-assigned)",
})
} else {
config.MinionPort = *minionPort
}
if *consolePort < 0 || *consolePort > 65535 {
validationErrors = append(validationErrors, ValidationError{
Field: "console-port",
Value: strconv.Itoa(*consolePort),
Message: "must be between 0 and 65535 (0 for system-assigned)",
})
} else {
config.ConsolePort = *consolePort
}
// Apply and validate web port
if *webPort < 0 || *webPort > 65535 {
validationErrors = append(validationErrors, ValidationError{
Field: "web-port",
Value: strconv.Itoa(*webPort),
Message: "must be between 0 and 65535 (0 for system-assigned)",
})
} else {
config.WebPort = *webPort
}
// Apply web enabled flag and web root
config.WebEnabled = *webEnabled
config.WebRoot = *webRoot
config.DBHost = *dbHost
config.DBPort = *dbPort
config.DBUser = *dbUser
config.DBPassword = *dbPassword
config.DBName = *dbName
config.DBSSLMode = *dbSSLMode
config.Debug = *debug
if *maxMsgSize < 1024 || *maxMsgSize > 1024*1024*100 {
validationErrors = append(validationErrors, ValidationError{
Field: "max-msg-size",
Value: strconv.Itoa(*maxMsgSize),
Message: "must be between 1KB and 100MB",
})
} else {
config.MaxMsgSize = *maxMsgSize
}
config.FileRoot = *fileRoot
// Return validation errors if any
if len(validationErrors) > 0 {
var errMsg strings.Builder
errMsg.WriteString("Configuration validation failed:\n")
for _, err := range validationErrors {
errMsg.WriteString(fmt.Sprintf(" - %s\n", err.Error()))
}
return nil, fmt.Errorf("%s", errMsg.String())
}
return config, nil
}
// LoadMinionConfig loads Minion configuration with validation
func LoadMinionConfig() (*MinionConfig, error) {
loader := NewConfigLoader()
if err := loader.LoadEnvironmentFile(); err != nil {
return nil, fmt.Errorf("failed to load environment file: %w", err)
}
config := DefaultMinionConfig()
var validationErrors []error
// Load configuration from environment variables
loadMinionEnvConfig(loader, config, &validationErrors)
// Parse and apply command line flags
flagValues := parseMinionFlags(config)
flag.Parse()
applyMinionFlags(loader, config, flagValues, &validationErrors)
// Perform final validations
validateMinionConfigConsistency(config, &validationErrors)
return finalizeMinionConfig(config, validationErrors)
}
// loadMinionEnvConfig loads configuration from environment variables
func loadMinionEnvConfig(loader *Loader, config *MinionConfig, validationErrors *[]error) {
// Load and validate server hostname
nexusServer := loader.GetString("NEXUS_SERVER", "localhost")
if err := loader.ValidateHostname("NEXUS_SERVER", nexusServer); err != nil {
*validationErrors = append(*validationErrors, err)
}
// Load and validate nexus port
nexusPort, err := loader.GetIntInRange("NEXUS_MINION_PORT", 11972, 1, 65535)
if err != nil {
*validationErrors = append(*validationErrors, err)
}
// Construct server address from hostname and port
config.ServerAddr = fmt.Sprintf("%s:%d", nexusServer, nexusPort)
// Load minion ID (optional)
config.ID = loader.GetString("MINION_ID", config.ID)
// Load debug flag
if debug, err := loader.GetBool("DEBUG", config.Debug); err != nil {
*validationErrors = append(*validationErrors, err)
} else {
config.Debug = debug
}
// Load timeout configurations
loadMinionTimeouts(loader, config, validationErrors)
}
// loadMinionTimeouts loads timeout-related configuration from environment variables
func loadMinionTimeouts(loader *Loader, config *MinionConfig, validationErrors *[]error) {
timeoutConfigs := []struct {
envVar string
target *int
min, max int
}{
{"CONNECT_TIMEOUT", &config.ConnectTimeout, 1, 300},
{"INITIAL_RECONNECT_DELAY", &config.InitialReconnectDelay, 1, 3600},
{"MAX_RECONNECT_DELAY", &config.MaxReconnectDelay, 1, 3600},
{"HEARTBEAT_INTERVAL", &config.HeartbeatInterval, 5, 300},
{"DEFAULT_SHELL_TIMEOUT", &config.DefaultShellTimeout, 5, 300},
{"STREAM_TIMEOUT", &config.StreamTimeout, 10, 300},
}
for _, tc := range timeoutConfigs {
if value, err := loader.GetIntInRange(tc.envVar, *tc.target, tc.min, tc.max); err != nil {
*validationErrors = append(*validationErrors, err)
} else {
*tc.target = value
}
}
}
// minionFlagValues holds the parsed command line flag values
type minionFlagValues struct {
serverAddr *string
id *string
debug *bool
connectTimeout *int
initialReconnectDelay *int
maxReconnectDelay *int
heartbeatInterval *int
defaultShellTimeout *int
streamTimeout *int
}
// parseMinionFlags parses command line flags and returns the flag pointers
func parseMinionFlags(config *MinionConfig) *minionFlagValues {
return &minionFlagValues{
serverAddr: flag.String("server", config.ServerAddr, "Nexus server address"),
id: flag.String("id", config.ID, "Minion ID (optional, will be generated if not provided)"),
debug: flag.Bool("debug", config.Debug, "Enable debug mode"),
connectTimeout: flag.Int("connect-timeout", config.ConnectTimeout, "Connection timeout in seconds"),
initialReconnectDelay: flag.Int("initial-reconnect-delay", config.InitialReconnectDelay, "Initial reconnection delay in seconds (exponential backoff starting point)"),
maxReconnectDelay: flag.Int("max-reconnect-delay", config.MaxReconnectDelay, "Maximum reconnection delay in seconds (exponential backoff cap)"),
heartbeatInterval: flag.Int("heartbeat-interval", config.HeartbeatInterval, "Heartbeat interval in seconds"),
defaultShellTimeout: flag.Int("default-shell-timeout", config.DefaultShellTimeout, "Default timeout for shell command execution in seconds"),
streamTimeout: flag.Int("stream-timeout", config.StreamTimeout, "Timeout for stream operations in seconds"),
}
}
// applyMinionFlags applies command line flag values to the configuration
func applyMinionFlags(loader *Loader, config *MinionConfig, flags *minionFlagValues, validationErrors *[]error) {
// Apply and validate server address
if err := loader.ValidateNetworkAddress("server", *flags.serverAddr); err != nil {
*validationErrors = append(*validationErrors, err)
} else {
config.ServerAddr = *flags.serverAddr
}
// Apply simple flags
config.ID = *flags.id
config.Debug = *flags.debug
// Apply and validate timeout flags
applyMinionTimeoutFlags(config, flags, validationErrors)
}
// applyMinionTimeoutFlags applies and validates timeout-related command line flags
func applyMinionTimeoutFlags(config *MinionConfig, flags *minionFlagValues, validationErrors *[]error) {
timeoutValidations := []struct {
name string
value int
target *int
min, max int
}{
{"connect-timeout", *flags.connectTimeout, &config.ConnectTimeout, 1, 300},
{"initial-reconnect-delay", *flags.initialReconnectDelay, &config.InitialReconnectDelay, 1, 3600},
{"max-reconnect-delay", *flags.maxReconnectDelay, &config.MaxReconnectDelay, 1, 3600},
{"heartbeat-interval", *flags.heartbeatInterval, &config.HeartbeatInterval, 5, 300},
{"default-shell-timeout", *flags.defaultShellTimeout, &config.DefaultShellTimeout, 5, 300},
{"stream-timeout", *flags.streamTimeout, &config.StreamTimeout, 10, 300},
}
for _, tv := range timeoutValidations {
if tv.value < tv.min || tv.value > tv.max {
*validationErrors = append(*validationErrors, ValidationError{
Field: tv.name,
Value: strconv.Itoa(tv.value),
Message: fmt.Sprintf("must be between %d and %d seconds", tv.min, tv.max),
})
} else {
*tv.target = tv.value
}
}
}
// validateMinionConfigConsistency performs consistency validations
func validateMinionConfigConsistency(config *MinionConfig, validationErrors *[]error) {
if config.InitialReconnectDelay > config.MaxReconnectDelay {
*validationErrors = append(*validationErrors, ValidationError{
Field: "reconnect-delays",
Value: fmt.Sprintf("initial=%d, max=%d", config.InitialReconnectDelay, config.MaxReconnectDelay),
Message: "initial reconnect delay cannot be greater than max reconnect delay",
})
}
}
// finalizeMinionConfig finalizes the configuration and returns errors if any
func finalizeMinionConfig(config *MinionConfig, validationErrors []error) (*MinionConfig, error) {
if len(validationErrors) > 0 {
var errMsg strings.Builder
errMsg.WriteString("Configuration validation failed:\n")
for _, err := range validationErrors {
errMsg.WriteString(fmt.Sprintf(" - %s\n", err.Error()))
}
return nil, fmt.Errorf("%s", errMsg.String())
}
return config, nil
}
// DBConnectionString builds a PostgreSQL connection string from config
func (c *NexusConfig) DBConnectionString() string {
return fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s",
c.DBUser, c.DBPassword, c.DBHost, c.DBPort, c.DBName, c.DBSSLMode)
}
// LogConfig logs the configuration (masks sensitive data)
func (c *NexusConfig) LogConfig(logger *zap.Logger) {
logger.Info("Configuration loaded",
zap.Int("minion_port", c.MinionPort),
zap.Int("console_port", c.ConsolePort),
zap.Int("web_port", c.WebPort),
zap.Bool("web_enabled", c.WebEnabled),
zap.String("web_root", c.WebRoot),
zap.String("db_host", c.DBHost),
zap.Int("db_port", c.DBPort),
zap.String("db_name", c.DBName),
zap.String("db_user", c.DBUser),
zap.Bool("debug", c.Debug),
zap.Int("max_msg_size", c.MaxMsgSize),
zap.String("file_root", c.FileRoot))
}
// LogConfig logs the minion configuration
func (c *MinionConfig) LogConfig(logger *zap.Logger) {
logger.Info("Configuration loaded",
zap.String("server", c.ServerAddr),
zap.String("id", c.ID),
zap.Bool("debug", c.Debug),
zap.Int("connect_timeout", c.ConnectTimeout),
zap.Int("initial_reconnect_delay", c.InitialReconnectDelay),
zap.Int("max_reconnect_delay", c.MaxReconnectDelay),
zap.Int("heartbeat_interval", c.HeartbeatInterval),
zap.Int("default_shell_timeout", c.DefaultShellTimeout),
zap.Int("stream_timeout", c.StreamTimeout))
}
// LogConfig logs the console configuration
func (c *ConsoleConfig) LogConfig(logger *zap.Logger) {
logger.Info("Configuration loaded",
zap.String("server", c.ServerAddr),
zap.Int("connect_timeout", c.ConnectTimeout),
zap.Bool("debug", c.Debug))
}