66package config
77
88import (
9+ "bytes"
910 "context"
1011 "encoding/json"
1112 "errors"
@@ -22,6 +23,7 @@ import (
2223
2324 "github.com/nginx/agent/v3/internal/datasource/file"
2425
26+ "github.com/goccy/go-yaml"
2527 uuidLibrary "github.com/nginx/agent/v3/pkg/id"
2628 selfsignedcerts "github.com/nginx/agent/v3/pkg/tls"
2729 "github.com/spf13/cobra"
@@ -127,7 +129,6 @@ func ResolveConfig() (*Config, error) {
127129 checkCollectorConfiguration (collector , config )
128130
129131 slog .Debug ("Agent config" , "config" , config )
130- slog .Info ("Enabled features" , "features" , config .Features )
131132 slog .Info ("Excluded files from being watched for file changes" , "exclude_files" ,
132133 config .Watchers .FileWatcher .ExcludeFiles )
133134
@@ -529,6 +530,11 @@ func getConfigFilePaths() []string {
529530}
530531
531532func loadPropertiesFromFile (cfg string ) error {
533+ validationError := validateYamlFile (cfg )
534+ if validationError != nil {
535+ return validationError
536+ }
537+
532538 viperInstance .SetConfigFile (cfg )
533539 viperInstance .SetConfigType ("yaml" )
534540 err := viperInstance .MergeInConfig ()
@@ -539,6 +545,20 @@ func loadPropertiesFromFile(cfg string) error {
539545 return nil
540546}
541547
548+ func validateYamlFile (filePath string ) error {
549+ fileContents , readError := os .ReadFile (filePath )
550+ if readError != nil {
551+ return fmt .Errorf ("failed to read file %s: %w" , filePath , readError )
552+ }
553+
554+ decoder := yaml .NewDecoder (bytes .NewReader (fileContents ), yaml .DisallowUnknownField ())
555+ if err := decoder .Decode (& Config {}); err != nil {
556+ return errors .New (yaml .FormatError (err , false , false ))
557+ }
558+
559+ return nil
560+ }
561+
542562func normalizeFunc (f * flag.FlagSet , name string ) flag.NormalizedName {
543563 from := []string {"_" , "." }
544564 to := "-"
0 commit comments