From 0efc2ddbfa66bd6fde38db1c66463717ac4af921 Mon Sep 17 00:00:00 2001 From: Reuben Lifshay Date: Wed, 6 Dec 2023 14:11:54 -0800 Subject: [PATCH 1/2] fix: load config before initializing logger --- cmd/svc_windows.go | 6 +++--- config/internal/config.go | 4 +++- logger/log_windows.go | 5 ----- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/cmd/svc_windows.go b/cmd/svc_windows.go index 8d0a7f7..3831993 100644 --- a/cmd/svc_windows.go +++ b/cmd/svc_windows.go @@ -39,14 +39,14 @@ type azurehoundSvc struct { } func (s *azurehoundSvc) Init(env svc.Environment) error { + config.LoadValues(nil, config.Options()) + config.SetAzureDefaults() + if logr, err := logger.GetLogger(); err != nil { return err } else { log = *logr - config.LoadValues(nil, config.Options()) - config.SetAzureDefaults() - if config.ConfigFileUsed() != "" { log.V(1).Info(fmt.Sprintf("Config File: %v", config.ConfigFileUsed())) } diff --git a/config/internal/config.go b/config/internal/config.go index bf45a9e..46c44e9 100644 --- a/config/internal/config.go +++ b/config/internal/config.go @@ -39,8 +39,10 @@ type Config struct { } func (s Config) Value() interface{} { - if reflect.ValueOf(s.Default).Kind() == reflect.Slice { + if defkind := reflect.ValueOf(s.Default).Kind(); defkind == reflect.Slice { return viper.GetStringSlice(s.Name) + } else if defkind == reflect.Int { + return viper.GetInt(s.Name) } else { return viper.Get(s.Name) } diff --git a/logger/log_windows.go b/logger/log_windows.go index 7bb77f2..7898678 100644 --- a/logger/log_windows.go +++ b/logger/log_windows.go @@ -47,11 +47,6 @@ func setupLogger() (*logr.Logger, error) { options.Writers = append(options.Writers, eventLogWriter) } - // XXX: This is gross, however, reading in the config file when starting the process as a windows service before - // initializing the eventLogWriter causes the program to panic. It doesn't make sense as to why it does that but - // this call will have to remain here until we can figure out what's going on. - config.LoadValues(nil, config.Options()) - // emit logs to file if configured if fileLogWriter := getFileLogLevelWriter(); fileLogWriter != nil { options.Writers = append(options.Writers, fileLogWriter) From 8a60edd5c20786595e9edcf1300ac31cfe41ce5f Mon Sep 17 00:00:00 2001 From: Reuben Lifshay Date: Wed, 6 Dec 2023 15:32:00 -0800 Subject: [PATCH 2/2] chore: use a switch statement for Config.Value() Co-authored-by: Alyx Holms --- config/internal/config.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/config/internal/config.go b/config/internal/config.go index 46c44e9..83517ac 100644 --- a/config/internal/config.go +++ b/config/internal/config.go @@ -39,11 +39,12 @@ type Config struct { } func (s Config) Value() interface{} { - if defkind := reflect.ValueOf(s.Default).Kind(); defkind == reflect.Slice { + switch reflect.ValueOf(s.Default).Kind() { + case reflect.Slice: return viper.GetStringSlice(s.Name) - } else if defkind == reflect.Int { + case reflect.Int: return viper.GetInt(s.Name) - } else { + default: return viper.Get(s.Name) } }