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 #491

Merged
merged 1 commit into from
May 25, 2017
Merged
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
84 changes: 76 additions & 8 deletions schema/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ import (
// and implements validation against a JSON schema.
type Validator string

type validateDescendantsFunc func(r io.Reader) error
type validateFunc func(r io.Reader) error

var mapValidateDescendants = map[Validator]validateDescendantsFunc{
ValidatorMediaTypeManifest: validateManifestDescendants,
ValidatorMediaTypeDescriptor: validateDescriptorDescendants,
var mapValidate = map[Validator]validateFunc{
ValidatorMediaTypeImageConfig: validateConfig,
ValidatorMediaTypeDescriptor: validateDescriptor,
ValidatorMediaTypeImageIndex: validateIndex,
ValidatorMediaTypeManifest: validateManifest,
}

// ValidationError contains all the errors that happened during validation.
Expand All @@ -55,9 +57,9 @@ func (v Validator) Validate(src io.Reader) error {
return errors.Wrap(err, "unable to read the document file")
}

if f, ok := mapValidateDescendants[v]; ok {
if f, ok := mapValidate[v]; ok {
if f == nil {
return fmt.Errorf("internal error: mapValidateDescendents[%q] is nil", v)
return fmt.Errorf("internal error: mapValidate[%q] is nil", v)
}
err = f(bytes.NewReader(buf))
if err != nil {
Expand Down Expand Up @@ -95,7 +97,7 @@ func (v unimplemented) Validate(src io.Reader) error {
return fmt.Errorf("%s: unimplemented", v)
}

func validateManifestDescendants(r io.Reader) error {
func validateManifest(r io.Reader) error {
header := v1.Manifest{}

buf, err := ioutil.ReadAll(r)
Expand Down Expand Up @@ -128,7 +130,7 @@ var (
sha512EncodedRegexp = regexp.MustCompile(`^[a-f0-9]{128}$`)
)

func validateDescriptorDescendants(r io.Reader) error {
func validateDescriptor(r io.Reader) error {
header := v1.Descriptor{}

buf, err := ioutil.ReadAll(r)
Expand Down Expand Up @@ -158,3 +160,69 @@ func validateDescriptorDescendants(r io.Reader) error {
}
return nil
}

func validateIndex(r io.Reader) error {
header := v1.Index{}

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 manifest.MediaType != string(v1.MediaTypeImageManifest) {
fmt.Printf("warning: manifest %s has an unknown media type: %s\n", manifest.Digest, manifest.MediaType)
}

}

return nil
}

func validateConfig(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")
}

checkPlatform(header.OS, header.Architecture)

return nil
}

func checkPlatform(OS string, Architecture string) {
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
}
}
fmt.Printf("warning: combination of %q and %q is invalid.", OS, Architecture)
}
}
fmt.Printf("warning: operating system %q of the bundle is not supported yet.", OS)
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like these should all be errors not printf's.

Copy link
Contributor

@wking wking Jan 25, 2017

Choose a reason for hiding this comment

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

I feel like these should all be errors not printf's.

They aren't errors though, since the spec only SHOULDs a platform/OS supported by runtime-spec's platform.*.

It would be nice to have a more programmatic API for logging warnings, even if it was only “use this Write-supporting object to log errors” instead of hardcoding stdout, but I think that can probably happen in a follow-up commit.

Copy link
Author

Choose a reason for hiding this comment

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

According to the specification, here should be a warning, not an error. (Although I think it should be output errors)

Copy link
Contributor

Choose a reason for hiding this comment

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

Why is a library function writing to stdout?

I am almost ready to propose a moratorium on validation until someone starts doing some engineering work. This is a huge waste of time for the maintainers and this stuff isn't passing muster.

Copy link
Contributor

Choose a reason for hiding this comment

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

I am almost ready to propose a moratorium on validation until someone starts doing some engineering work.

We just need a way to handle warnings for SHOULD violations (they aren't errors, but the caller may want to know about them). I've tried floating strict (#403, opencontainers/image-tools#66) and TAP comments (opencontainers/image-tools#60). And @xiekeyang has been trying to follow your guidance on this in #452. So far nobody has managed to land anything more polished than “print warnings to stdout” (and we have precedence for that approach in master). So until the image-spec maintainers decide how they want SHOULD violations handled, I think we should just roll forward with fmt.Printf("warning:…. It will be easy enough to grep and replace once we have a better approach figured out.

Copy link
Contributor

Choose a reason for hiding this comment

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

@wking Printing to stderr/stdout makes this package completely useless for any case where I care about program output.

So until the image-spec maintainers decide how they want SHOULD violations handled

Propose something that isn't broken.

Copy link
Contributor

Choose a reason for hiding this comment

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

Propose something that isn't broken.

It's not my project ;). I'll let you all sort this out, but it seems unfortunate to block otherwise fine PRs because they follow an established-in-master pattern.

Copy link
Contributor

Choose a reason for hiding this comment

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

@wking These packages are unusable in other commands that control stderr/stdout. I am not sure how they slipped into master, but this is pretty bad.

I am going to take a deeper look into this next week, so I'll be able to provide some better direction.

}