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

schema: add checkPlatform #469

Closed
wants to merge 1 commit into from
Closed
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
69 changes: 68 additions & 1 deletion schema/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ type Validator string
type validateDescendantsFunc func(r io.Reader) error

var mapValidateDescendants = map[Validator]validateDescendantsFunc{
MediaTypeManifest: validateManifestDescendants,
MediaTypeImageConfig: validateConfigDescendants,
MediaTypeManifest: validateManifestDescendants,
MediaTypeManifestList: validateManifestListDescendants,
}

// ValidationError contains all the errors that happened during validation.
Expand Down Expand Up @@ -117,3 +119,68 @@ func validateManifestDescendants(r io.Reader) error {
}
return nil
}

func validateManifestListDescendants(r io.Reader) error {
header := v1.ManifestList{}

buf, err := ioutil.ReadAll(r)
if err != nil {
return errors.Wrapf(err, "error reading the io stream")
}

err = json.Unmarshal(buf, &header)
if err != nil {
return errors.Wrap(err, "manifestlist format mismatch")
}

for _, manifest := range header.Manifests {
if err = checkPlatform(manifest.Platform.OS, manifest.Platform.Architecture); err != nil {
return errors.Wrap(err, "check Platform error")
}
}
return nil
}

func validateConfigDescendants(r io.Reader) error {
header := v1.Image{}

buf, err := ioutil.ReadAll(r)
if err != nil {
return errors.Wrapf(err, "error reading the io stream")
}

err = json.Unmarshal(buf, &header)
if err != nil {
return errors.Wrap(err, "config format mismatch")
}

if err = checkPlatform(header.OS, header.Architecture); err != nil {
return errors.Wrap(err, "check Platform error")
}
return nil
}

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