Skip to content

Commit 8d697e8

Browse files
committed
validate: Test config.json and rootfs sibling-hood
Test the spec's [1]: While these artifacts MUST all be present in a single directory on the local filesystem, ... which is a condition it imposes on config.json and the directory referenced by root.path. I think we should drop that restriction from the spec, but my attempt to remove the restriction was rejected [2]. If a future spec drops the restriction, we can revert this commit. Using path/filepath for the path manipulation will break when validating cross-platform configs (e.g. trying to validate a Windows bundle on a Linux machine). But that's a bigger issue than this commit, so I've left it alone for now. [1]: https://github.com/opencontainers/runtime-spec/blob/v1.0.0-rc5/bundle.md#container-format [2]: opencontainers/runtime-spec#469 Signed-off-by: W. Trevor King <[email protected]>
1 parent 21af41d commit 8d697e8

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

validate/validate.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,19 +115,38 @@ func (v *Validator) CheckAll() (msgs []string) {
115115
func (v *Validator) CheckRootfsPath() (msgs []string) {
116116
logrus.Debugf("check rootfs path")
117117

118+
absBundlePath, err := filepath.Abs(v.bundlePath)
119+
if err != nil {
120+
msgs = append(msgs, fmt.Sprintf("unable to convert %q to an absolute path", v.bundlePath))
121+
}
122+
absBundlePath = filepath.Clean(absBundlePath)
123+
118124
var rootfsPath string
125+
var absRootPath string
119126
if filepath.IsAbs(v.spec.Root.Path) {
120127
rootfsPath = v.spec.Root.Path
128+
absRootPath = rootfsPath
121129
} else {
130+
var err error
122131
rootfsPath = filepath.Join(v.bundlePath, v.spec.Root.Path)
132+
absRootPath, err = filepath.Abs(rootfsPath)
133+
if err != nil {
134+
msgs = append(msgs, fmt.Sprintf("unable to convert %q to an absolute path", rootfsPath))
135+
}
123136
}
137+
absRootPath = filepath.Clean(absRootPath)
124138

125139
if fi, err := os.Stat(rootfsPath); err != nil {
126140
msgs = append(msgs, fmt.Sprintf("Cannot find the root path %q", rootfsPath))
127141
} else if !fi.IsDir() {
128142
msgs = append(msgs, fmt.Sprintf("The root path %q is not a directory.", rootfsPath))
129143
}
130144

145+
rootParent := filepath.Dir(absRootPath)
146+
if absRootPath == string(filepath.Separator) || rootParent != absBundlePath {
147+
msgs = append(msgs, fmt.Sprintf("root.path is %q, but it MUST be a child of %q", v.spec.Root.Path, absBundlePath))
148+
}
149+
131150
return
132151

133152
}

0 commit comments

Comments
 (0)