Skip to content

Commit b6afaa5

Browse files
committed
v3_5_experimental: add validation, unit tests and error
1 parent dfcba34 commit b6afaa5

File tree

4 files changed

+42
-42
lines changed

4 files changed

+42
-42
lines changed

config/shared/errors/errors.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ var (
8585
ErrInvalidProxy = errors.New("proxies must be http(s)")
8686
ErrInsecureProxy = errors.New("insecure plaintext HTTP proxy specified for HTTPS resources")
8787
ErrPathConflictsSystemd = errors.New("path conflicts with systemd unit or dropin")
88-
ErrPathConflictsParentDir = errors.New("path conflicts with parent directory of another file, link, or directory")
88+
ErrPathAlreadyExists = errors.New("path already exists")
89+
ErrMissLabeledDir = errors.New("parent directory path matches configured file, check path, and ensure parent directory is configured")
8990

9091
// Systemd section errors
9192
ErrInvalidSystemdExt = errors.New("invalid systemd unit extension")

config/v3_5_experimental/types/config.go

+34-33
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ var (
3535
}
3636
)
3737

38+
var paths = map[string]struct{}{}
39+
3840
func (cfg Config) Validate(c path.ContextPath) (r report.Report) {
3941
systemdPath := "/etc/systemd/system/"
4042
unitPaths := map[string]struct{}{}
@@ -76,43 +78,21 @@ func (cfg Config) validateParents(c path.ContextPath) report.Report {
7678
Path string
7779
Field string
7880
}
79-
paths := map[string]struct{}{}
8081
r := report.Report{}
8182

8283
for i, f := range cfg.Storage.Files {
83-
if _, exists := paths[f.Path]; exists {
84-
r.AddOnError(c.Append("storage", "files", i, "path"), errors.ErrPathConflictsParentDir) //TODO: should add different error?
85-
return r
86-
}
87-
paths[f.Path] = struct{}{}
88-
entries = append(entries, struct {
89-
Path string
90-
Field string
91-
}{Path: f.Path, Field: "files"})
84+
r = handlePathConflict(f.Path, "files", i, c, r, errors.ErrPathAlreadyExists)
85+
addPathAndEntry(f.Path, "files", &entries)
9286
}
9387

9488
for i, d := range cfg.Storage.Directories {
95-
if _, exists := paths[d.Path]; exists {
96-
r.AddOnError(c.Append("storage", "directories", i, "path"), errors.ErrPathConflictsParentDir) //TODO: should add different error?
97-
return r
98-
}
99-
paths[d.Path] = struct{}{}
100-
entries = append(entries, struct {
101-
Path string
102-
Field string
103-
}{Path: d.Path, Field: "directories"})
89+
r = handlePathConflict(d.Path, "directories", i, c, r, errors.ErrPathAlreadyExists)
90+
addPathAndEntry(d.Path, "directories", &entries)
10491
}
10592

10693
for i, l := range cfg.Storage.Links {
107-
if _, exists := paths[l.Path]; exists {
108-
r.AddOnError(c.Append("storage", "links", i, "path"), errors.ErrPathConflictsParentDir) //TODO: error to already exist path
109-
return r
110-
}
111-
paths[l.Path] = struct{}{}
112-
entries = append(entries, struct {
113-
Path string
114-
Field string
115-
}{Path: l.Path, Field: "links"})
94+
r = handlePathConflict(l.Path, "links", i, c, r, errors.ErrPathAlreadyExists)
95+
addPathAndEntry(l.Path, "links", &entries)
11696
}
11797

11898
sort.Slice(entries, func(i, j int) bool {
@@ -122,7 +102,7 @@ func (cfg Config) validateParents(c path.ContextPath) report.Report {
122102
for i, entry := range entries {
123103
if i > 0 && isWithin(entry.Path, entries[i-1].Path) {
124104
if entries[i-1].Field != "directories" {
125-
r.AddOnError(c.Append("storage", entry.Field, i, "path"), errors.ErrPathConflictsParentDir) //TODO: conflict parent directories error
105+
r.AddOnError(c.Append("storage", entry.Field, i, "path"), errors.ErrMissLabeledDir)
126106
return r
127107
}
128108
}
@@ -131,16 +111,37 @@ func (cfg Config) validateParents(c path.ContextPath) report.Report {
131111
return r
132112
}
133113

134-
// check the depth
114+
func handlePathConflict(path, fieldName string, index int, c path.ContextPath, r report.Report, err error) report.Report {
115+
if _, exists := paths[path]; exists {
116+
r.AddOnError(c.Append("storage", fieldName, index, "path"), err)
117+
}
118+
return r
119+
}
120+
121+
func addPathAndEntry(path, fieldName string, entries *[]struct{ Path, Field string }) {
122+
*entries = append(*entries, struct {
123+
Path string
124+
Field string
125+
}{Path: path, Field: fieldName})
126+
}
127+
135128
func depth(path string) uint {
136129
var count uint
137-
for p := filepath.Clean(path); p != "/" && p != "."; count++ {
138-
p = filepath.Dir(p)
130+
cleanedPath := filepath.FromSlash(filepath.Clean(path))
131+
sep := string(filepath.Separator)
132+
133+
volume := filepath.VolumeName(cleanedPath)
134+
if volume != "" {
135+
cleanedPath = cleanedPath[len(volume):]
136+
}
137+
138+
for cleanedPath != sep && cleanedPath != "." {
139+
cleanedPath = filepath.Dir(cleanedPath)
140+
count++
139141
}
140142
return count
141143
}
142144

143-
// isWithin checks if newPath is within prevPath.
144145
func isWithin(newPath, prevPath string) bool {
145146
return strings.HasPrefix(newPath, prevPath) && newPath != prevPath
146147
}

config/v3_5_experimental/types/config_test.go

+5-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020

2121
"github.com/coreos/ignition/v2/config/shared/errors"
2222
"github.com/coreos/ignition/v2/config/util"
23-
2423
"github.com/coreos/vcontext/path"
2524
"github.com/coreos/vcontext/report"
2625
)
@@ -194,7 +193,7 @@ func TestConfigValidation(t *testing.T) {
194193
},
195194
},
196195
},
197-
out: errors.ErrPathConflictsParentDir,
196+
out: errors.ErrMissLabeledDir,
198197
at: path.New("json", "storage", "files", 1, "path"),
199198
},
200199

@@ -210,8 +209,8 @@ func TestConfigValidation(t *testing.T) {
210209
},
211210
},
212211
},
213-
out: errors.ErrPathConflictsParentDir,
214-
at: path.New("json", "storage", "links", 0, "path"),
212+
out: errors.ErrMissLabeledDir,
213+
at: path.New("json", "storage", "links", 1, "path"),
215214
},
216215

217216
// test 8: file path conflicts with directory path, should error
@@ -220,15 +219,14 @@ func TestConfigValidation(t *testing.T) {
220219
Storage: Storage{
221220
Files: []File{
222221
{Node: Node{Path: "/foo/bar"}},
223-
{Node: Node{Path: "/foo/bar"}},
224222
},
225223
Directories: []Directory{
226224
{Node: Node{Path: "/foo/bar/baz"}},
227225
},
228226
},
229227
},
230-
out: errors.ErrPathConflictsParentDir,
231-
at: path.New("json", "storage", "directories", 0, "path"),
228+
out: errors.ErrMissLabeledDir,
229+
at: path.New("json", "storage", "directories", 1, "path"),
232230
},
233231

234232
// test 9: non-conflicting scenarios with systemd unit and systemd dropin file, should not error
@@ -302,7 +300,6 @@ func TestConfigValidation(t *testing.T) {
302300
in: Config{
303301
Storage: Storage{
304302
Files: []File{
305-
{Node: Node{Path: "/foo/bar"}},
306303
{Node: Node{Path: "/foo/bar/baz"}},
307304
},
308305
Directories: []Directory{

docs/release-notes.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ nav_order: 9
2424

2525
- Fix failure when config only disables units already disabled
2626
- Retry HTTP requests on Azure on status codes 404, 410, and 429
27+
- Fix validation to catch conflicts with the parent directory of another file, link or directories
2728

2829

2930
## Ignition 2.17.0 (2023-11-20)

0 commit comments

Comments
 (0)