From 899afeaea657ddc6855d19d6da62bed04e1f4671 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Tue, 28 Mar 2017 14:52:45 -0700 Subject: [PATCH] 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]: https://github.com/opencontainers/runtime-spec/pull/469 Signed-off-by: W. Trevor King --- validate/validate.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/validate/validate.go b/validate/validate.go index 2889895be..4fc19180e 100644 --- a/validate/validate.go +++ b/validate/validate.go @@ -115,11 +115,23 @@ func (v *Validator) CheckAll() (msgs []string) { func (v *Validator) CheckRootfsPath() (msgs []string) { logrus.Debugf("check rootfs path") + absBundlePath, err := filepath.Abs(v.bundlePath) + if err != nil { + msgs = append(msgs, fmt.Sprintf("unable to convert %q to an absolute path", v.bundlePath)) + } + var rootfsPath string + var absRootPath string if filepath.IsAbs(v.spec.Root.Path) { rootfsPath = v.spec.Root.Path + absRootPath = filepath.Clean(rootfsPath) } else { + var err error rootfsPath = filepath.Join(v.bundlePath, v.spec.Root.Path) + absRootPath, err = filepath.Abs(rootfsPath) + if err != nil { + msgs = append(msgs, fmt.Sprintf("unable to convert %q to an absolute path", rootfsPath)) + } } if fi, err := os.Stat(rootfsPath); err != nil { @@ -128,6 +140,11 @@ func (v *Validator) CheckRootfsPath() (msgs []string) { msgs = append(msgs, fmt.Sprintf("The root path %q is not a directory.", rootfsPath)) } + rootParent := filepath.Dir(absRootPath) + if absRootPath == string(filepath.Separator) || rootParent != absBundlePath { + msgs = append(msgs, fmt.Sprintf("root.path is %q, but it MUST be a child of %q", v.spec.Root.Path, absBundlePath)) + } + return }