Skip to content

Commit 719b903

Browse files
authored
Merge pull request #341 from xiekeyang/med-val
validate media type of manifest descendants
2 parents 5dbe4a3 + dd4cbf1 commit 719b903

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

schema/validator.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ package schema
1616

1717
import (
1818
"bytes"
19+
"encoding/json"
1920
"fmt"
2021
"io"
2122
"io/ioutil"
2223

24+
"github.com/opencontainers/image-spec/specs-go/v1"
2325
"github.com/pkg/errors"
2426
"github.com/xeipuuv/gojsonschema"
2527
)
@@ -28,6 +30,12 @@ import (
2830
// and implements validation against a JSON schema.
2931
type Validator string
3032

33+
type validateDescendantsFunc func(r io.Reader) error
34+
35+
var mapValidateDescendants = map[Validator]validateDescendantsFunc{
36+
MediaTypeManifest: validateManifestDescendants,
37+
}
38+
3139
// ValidationError contains all the errors that happened during validation.
3240
type ValidationError struct {
3341
Errs []error
@@ -44,6 +52,16 @@ func (v Validator) Validate(src io.Reader) error {
4452
return errors.Wrap(err, "unable to read the document file")
4553
}
4654

55+
if f, ok := mapValidateDescendants[v]; ok {
56+
if f == nil {
57+
return fmt.Errorf("internal error: mapValidateDescendents[%q] is nil", v)
58+
}
59+
err = f(bytes.NewReader(buf))
60+
if err != nil {
61+
return err
62+
}
63+
}
64+
4765
sl := gojsonschema.NewReferenceLoaderFileSystem("file:///"+specs[v], fs)
4866
ml := gojsonschema.NewStringLoader(string(buf))
4967

@@ -73,3 +91,29 @@ type unimplemented string
7391
func (v unimplemented) Validate(src io.Reader) error {
7492
return fmt.Errorf("%s: unimplemented", v)
7593
}
94+
95+
func validateManifestDescendants(r io.Reader) error {
96+
header := v1.Manifest{}
97+
98+
buf, err := ioutil.ReadAll(r)
99+
if err != nil {
100+
return errors.Wrapf(err, "error reading the io stream")
101+
}
102+
103+
err = json.Unmarshal(buf, &header)
104+
if err != nil {
105+
return errors.Wrap(err, "manifest format mismatch")
106+
}
107+
108+
if header.Config.MediaType != string(v1.MediaTypeImageConfig) {
109+
fmt.Printf("warning: config %s has an unknown media type: %s\n", header.Config.Digest, header.Config.MediaType)
110+
}
111+
112+
for _, layer := range header.Layers {
113+
if layer.MediaType != string(v1.MediaTypeImageLayer) &&
114+
layer.MediaType != string(v1.MediaTypeImageLayerNonDistributable) {
115+
fmt.Printf("warning: layer %s has an unknown media type: %s\n", layer.Digest, layer.MediaType)
116+
}
117+
}
118+
return nil
119+
}

0 commit comments

Comments
 (0)