66package config
77
88import (
9+ "bytes"
910 "context"
1011 "encoding/json"
1112 "errors"
@@ -20,6 +21,7 @@ import (
2021
2122 "github.com/nginx/agent/v3/internal/datasource/file"
2223
24+ "github.com/goccy/go-yaml"
2325 uuidLibrary "github.com/nginx/agent/v3/pkg/id"
2426 selfsignedcerts "github.com/nginx/agent/v3/pkg/tls"
2527 "github.com/spf13/cobra"
@@ -121,7 +123,6 @@ func ResolveConfig() (*Config, error) {
121123 checkCollectorConfiguration (collector , config )
122124
123125 slog .Debug ("Agent config" , "config" , config )
124- slog .Info ("Enabled features" , "features" , config .Features )
125126 slog .Info ("Excluded files from being watched for file changes" , "exclude_files" ,
126127 config .Watchers .FileWatcher .ExcludeFiles )
127128
@@ -523,6 +524,11 @@ func getConfigFilePaths() []string {
523524}
524525
525526func loadPropertiesFromFile (cfg string ) error {
527+ validationError := validateYamlFile (cfg )
528+ if validationError != nil {
529+ return validationError
530+ }
531+
526532 viperInstance .SetConfigFile (cfg )
527533 viperInstance .SetConfigType ("yaml" )
528534 err := viperInstance .MergeInConfig ()
@@ -533,6 +539,20 @@ func loadPropertiesFromFile(cfg string) error {
533539 return nil
534540}
535541
542+ func validateYamlFile (filePath string ) error {
543+ fileContents , readError := os .ReadFile (filePath )
544+ if readError != nil {
545+ return fmt .Errorf ("failed to read file %s: %w" , filePath , readError )
546+ }
547+
548+ decoder := yaml .NewDecoder (bytes .NewReader (fileContents ), yaml .DisallowUnknownField ())
549+ if err := decoder .Decode (& Config {}); err != nil {
550+ return errors .New (yaml .FormatError (err , false , false ))
551+ }
552+
553+ return nil
554+ }
555+
536556func normalizeFunc (f * flag.FlagSet , name string ) flag.NormalizedName {
537557 from := []string {"_" , "." }
538558 to := "-"
0 commit comments