@@ -11,6 +11,7 @@ import (
1111 v0 "github.com/docker/cagent/pkg/config/v0"
1212 v1 "github.com/docker/cagent/pkg/config/v1"
1313 latest "github.com/docker/cagent/pkg/config/v2"
14+ v2 "github.com/docker/cagent/pkg/config/v2"
1415)
1516
1617// LoadConfigSecure loads the configuration from a file with path validation
@@ -79,17 +80,20 @@ func ValidatePathInDirectory(path, allowedDir string) (string, error) {
7980}
8081
8182func loadConfig (path string ) (* latest.Config , error ) {
83+ dir := filepath .Dir (path )
8284 data , err := os .ReadFile (path )
8385 if err != nil {
8486 return nil , fmt .Errorf ("reading config file: %w" , err )
8587 }
8688
87- var raw map [string ]any
88- if err := yaml .Unmarshal (data , & raw ); err != nil {
89- return nil , fmt .Errorf ("parsing config file %s\n %s" , path , yaml .FormatError (err , true , true ))
89+ var raw struct {
90+ Version any `yaml:"version"`
91+ }
92+ if err := yaml .UnmarshalWithOptions (data , & raw , yaml .ReferenceDirs (dir )); err != nil {
93+ return nil , fmt .Errorf ("looking for version in config file %s\n %s" , path , yaml .FormatError (err , true , true ))
9094 }
9195
92- oldConfig , err := parseCurrentVersion (data , raw [ "version" ] )
96+ oldConfig , err := parseCurrentVersion (dir , data , raw . Version )
9397 if err != nil {
9498 return nil , fmt .Errorf ("parsing config file %s\n %s" , path , yaml .FormatError (err , true , true ))
9599 }
@@ -106,14 +110,22 @@ func loadConfig(path string) (*latest.Config, error) {
106110 return & config , nil
107111}
108112
109- func parseCurrentVersion (data []byte , version any ) (any , error ) {
113+ func parseCurrentVersion (dir string , data []byte , version any ) (any , error ) {
114+ options := []yaml.DecodeOption {yaml .Strict (), yaml .ReferenceDirs (dir )}
115+
110116 switch version {
111117 case nil , "0" , 0 :
112- return v0 .Load (data )
118+ var cfg v0.Config
119+ err := yaml .UnmarshalWithOptions (data , & cfg , options ... )
120+ return cfg , err
113121 case "1" , 1 :
114- return v1 .Load (data )
122+ var cfg v1.Config
123+ err := yaml .UnmarshalWithOptions (data , & cfg , options ... )
124+ return cfg , err
115125 default :
116- return latest .Load (data )
126+ var cfg v2.Config
127+ err := yaml .UnmarshalWithOptions (data , & cfg , options ... )
128+ return cfg , err
117129 }
118130}
119131
0 commit comments