From bc138e563cdb4c3a2f474f5f7d9376ffb100bc71 Mon Sep 17 00:00:00 2001 From: michel-thebeau-WR Date: Thu, 7 Aug 2025 21:04:52 +0000 Subject: [PATCH] openbao-monitor: align the log levels with helm chart The helm chart uses numeric values representing the following log levels: DEBUG, INFO, WARNING, ERROR and FATAL Update the validation to accept numeric levels, and interpet 5 (FATAL) as ERROR. Test Plan: PASS test valid/invalid loglevels, numeric bounds Change-Id: Ifbebe30e487f30ba11ada398056252e80a07ba94 Signed-off-by: Michel Thebeau --- .../openbao-monitor/source/commands/root.go | 6 +-- .../openbao-monitor/source/config/config.go | 24 ++++++++++++ .../openbao-monitor/source/config/validate.go | 38 ++++++++++++++++--- 3 files changed, 58 insertions(+), 10 deletions(-) diff --git a/security/openbao-monitor/source/commands/root.go b/security/openbao-monitor/source/commands/root.go index 3bb46176..1417249f 100644 --- a/security/openbao-monitor/source/commands/root.go +++ b/security/openbao-monitor/source/commands/root.go @@ -60,11 +60,7 @@ func setupCmd(cmd *cobra.Command, args []string) error { // Set default configuration for logs if no custum configs are given logFile := globalConfig.LogPath - logLevel := globalConfig.LogLevel - if logLevel == "" { - // Default log level if no log level was set - logLevel = "INFO" - } + logLevel := globalConfig.InterpretLogLevel() // Set default to stderr if no log file was specified. logWriter = os.Stderr diff --git a/security/openbao-monitor/source/config/config.go b/security/openbao-monitor/source/config/config.go index 163c61c3..977d3dbe 100644 --- a/security/openbao-monitor/source/config/config.go +++ b/security/openbao-monitor/source/config/config.go @@ -65,6 +65,7 @@ type MonitorConfig struct { // The default log level // Available log levels: DEBUG, INFO, WARN and ERROR + // Accepts numeric values matching helm chart log levels (4-5 are interpreted as ERROR) LogLevel string `yaml:"logLevel"` // The time in seconds waited between each unseal check in the run command. @@ -295,3 +296,26 @@ func (configInstance *MonitorConfig) ParseInitResponse(dnshost string, responce slog.Debug("Parsing init response complete") return nil } + +// Interpret numeric or text log level +// Always returns a log level in string format +func (configInstance *MonitorConfig) InterpretLogLevel() string { + // Check if the log level is a number + if converted, err := strconv.Atoi(configInstance.LogLevel); err == nil { + if level, exists := availableLogLevels[converted]; exists { + return level + } + + // error, but this code should not be reached if validateLogConfig works + fmt.Errorf("the numeric LogLevel %v is not a valid log level", configInstance.LogLevel) + return "INFO" // Default to INFO if the numeric level is invalid + } + + // Default to INFO if no log level was set + if configInstance.LogLevel == "" { + return "INFO" + } + + // validateLogConfig already validated the LogLevel + return configInstance.LogLevel +} \ No newline at end of file diff --git a/security/openbao-monitor/source/config/validate.go b/security/openbao-monitor/source/config/validate.go index c2bbca78..634d1eb1 100644 --- a/security/openbao-monitor/source/config/validate.go +++ b/security/openbao-monitor/source/config/validate.go @@ -6,9 +6,18 @@ import ( "os" "path" "regexp" - "slices" + "strconv" ) +// Available log levels; these should match the levels available in helm chart +var availableLogLevels = map[int]string{ + 1: "DEBUG", + 2: "INFO", + 3: "WARN", + 4: "ERROR", + 5: "ERROR", +} + func (configInstance MonitorConfig) validateDNS() error { for domain_name, url := range configInstance.ServerAddresses { // If Host is empty, then the domain entry is invalid @@ -63,6 +72,8 @@ func (configInstance MonitorConfig) validateKeyShards() error { } func (configInstance MonitorConfig) validateLogConfig() error { + var found bool = false + if configInstance.LogPath != "" { _, err := os.Stat(path.Dir(configInstance.LogPath)) if err != nil { @@ -71,10 +82,27 @@ func (configInstance MonitorConfig) validateLogConfig() error { } } if configInstance.LogLevel != "" { - availableLogLevels := []string{"DEBUG", "INFO", "WARN", "ERROR"} - if !slices.Contains(availableLogLevels, configInstance.LogLevel) { - return fmt.Errorf( - "the listed LogLevel %v is not a valid log level", configInstance.LogLevel) + if converted, err := strconv.Atoi(configInstance.LogLevel); err == nil { + // convert the numeric log level to string + if _, exists := availableLogLevels[converted]; exists { + // pass, Accept the numeric LogLevel + } else { + return fmt.Errorf( + "the numeric LogLevel %v is not a valid log level", configInstance.LogLevel) + } + } else { + // Check if the LogLevel is one of the available log levels + for _, value := range availableLogLevels { + if value == configInstance.LogLevel { + // Accept LogLevel + found = true + break + } + } + if !found { + return fmt.Errorf( + "the listed LogLevel %v is not a valid log level", configInstance.LogLevel) + } } }