Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config validation #39

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions image/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ func findConfig(w walker, d *descriptor) (*config, error) {
if err := json.Unmarshal(buf, &c); err != nil {
return err
}
// check if the rootfs type is 'layers'
if c.RootFS.Type != "layers" {
return fmt.Errorf("%q is an unknown rootfs type, MUST be 'layers'", c.RootFS.Type)
}
return errEOW
}); err {
case nil:
Expand All @@ -68,6 +64,44 @@ func findConfig(w walker, d *descriptor) (*config, error) {
}
}

func (c *config) validate() error {

// check if the rootfs type is 'layers'
if c.RootFS.Type != "layers" {
return fmt.Errorf("%q is an unknown rootfs type, MUST be 'layers'", c.RootFS.Type)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to check this in Go, because it's checked in JSON Schema since opencontainers/image-spec#358.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok,thanks for your reminder


//check os and architecture
if err := c.checkPlatform(); err != nil {
return err
}
return nil
}

func (c *config) checkPlatform() error {
validCombins := map[string][]string{
"darwin": {"386", "amd64", "arm", "arm64"},
"dragonfly": {"amd64"},
"freebsd": {"386", "amd64", "arm"},
"linux": {"386", "amd64", "arm", "arm64", "ppc64", "ppc64le", "mips64", "mips64le"},
"netbsd": {"386", "amd64", "arm"},
"openbsd": {"386", "amd64", "arm"},
"plan9": {"386", "amd64"},
"solaris": {"amd64"},
"windows": {"386", "amd64"}}
for os, archs := range validCombins {
if os == c.OS {
for _, arch := range archs {
if arch == c.Architecture {
return nil
}
}
return fmt.Errorf("Combination of %q and %q is invalid.", c.OS, c.Architecture)
}
}
return fmt.Errorf("Operation system %q of the bundle is not supported yet.", c.OS)
}

func (c *config) runtimeSpec(rootfs string) (*specs.Spec, error) {
if c.OS != "linux" {
return nil, fmt.Errorf("%s: unsupported OS", c.OS)
Expand Down
16 changes: 15 additions & 1 deletion image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,19 @@ func validate(w walker, refs []string, out *log.Logger) error {
return err
}

if err := m.validate(w); err != nil {
if err = m.validate(w); err != nil {
return err
}

c, err := findConfig(w, &m.Config)
if err != nil {
return err
}

if err = c.validate(); err != nil {
return err
}

if out != nil {
out.Printf("reference %q: OK", ref)
}
Expand Down Expand Up @@ -180,6 +190,10 @@ func createRuntimeBundle(w walker, dest, refName, rootfs string) error {
return err
}

if err = c.validate(); err != nil {
return err
}

err = m.unpack(w, filepath.Join(dest, rootfs))
if err != nil {
return err
Expand Down