Skip to content

Commit 28852fd

Browse files
committed
Support anchors in other files (same parent dir)
Signed-off-by: David Gageot <david.gageot@docker.com>
1 parent bfc7908 commit 28852fd

4 files changed

Lines changed: 20 additions & 35 deletions

File tree

pkg/config/config.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

8182
func 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

pkg/config/v0/load.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

pkg/config/v1/load.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

pkg/config/v2/load.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)