@@ -32,11 +32,13 @@ import (
3232// and implements validation against a JSON schema.
3333type Validator string
3434
35- type validateDescendantsFunc func (r io.Reader ) error
35+ type validateFunc func (r io.Reader ) error
3636
37- var mapValidateDescendants = map [Validator ]validateDescendantsFunc {
38- ValidatorMediaTypeManifest : validateManifestDescendants ,
39- ValidatorMediaTypeDescriptor : validateDescriptorDescendants ,
37+ var mapValidate = map [Validator ]validateFunc {
38+ ValidatorMediaTypeImageConfig : validateConfig ,
39+ ValidatorMediaTypeDescriptor : validateDescriptor ,
40+ ValidatorMediaTypeImageIndex : validateIndex ,
41+ ValidatorMediaTypeManifest : validateManifest ,
4042}
4143
4244// ValidationError contains all the errors that happened during validation.
@@ -55,9 +57,9 @@ func (v Validator) Validate(src io.Reader) error {
5557 return errors .Wrap (err , "unable to read the document file" )
5658 }
5759
58- if f , ok := mapValidateDescendants [v ]; ok {
60+ if f , ok := mapValidate [v ]; ok {
5961 if f == nil {
60- return fmt .Errorf ("internal error: mapValidateDescendents [%q] is nil" , v )
62+ return fmt .Errorf ("internal error: mapValidate [%q] is nil" , v )
6163 }
6264 err = f (bytes .NewReader (buf ))
6365 if err != nil {
@@ -95,7 +97,7 @@ func (v unimplemented) Validate(src io.Reader) error {
9597 return fmt .Errorf ("%s: unimplemented" , v )
9698}
9799
98- func validateManifestDescendants (r io.Reader ) error {
100+ func validateManifest (r io.Reader ) error {
99101 header := v1.Manifest {}
100102
101103 buf , err := ioutil .ReadAll (r )
@@ -128,7 +130,7 @@ var (
128130 sha512EncodedRegexp = regexp .MustCompile (`^[a-f0-9]{128}$` )
129131)
130132
131- func validateDescriptorDescendants (r io.Reader ) error {
133+ func validateDescriptor (r io.Reader ) error {
132134 header := v1.Descriptor {}
133135
134136 buf , err := ioutil .ReadAll (r )
@@ -158,3 +160,70 @@ func validateDescriptorDescendants(r io.Reader) error {
158160 }
159161 return nil
160162}
163+
164+ func validateIndex (r io.Reader ) error {
165+ header := v1.Index {}
166+
167+ buf , err := ioutil .ReadAll (r )
168+ if err != nil {
169+ return errors .Wrapf (err , "error reading the io stream" )
170+ }
171+
172+ err = json .Unmarshal (buf , & header )
173+ if err != nil {
174+ return errors .Wrap (err , "manifestlist format mismatch" )
175+ }
176+
177+ for _ , manifest := range header .Manifests {
178+ if manifest .MediaType != string (v1 .MediaTypeImageIndex ) {
179+ fmt .Printf ("warning: manifest %s has an unknown media type: %s\n " , manifest .Digest , manifest .MediaType )
180+ }
181+
182+ checkPlatform (manifest .Platform .OS , manifest .Platform .Architecture )
183+ }
184+
185+ return nil
186+ }
187+
188+ func validateConfig (r io.Reader ) error {
189+ header := v1.Image {}
190+
191+ buf , err := ioutil .ReadAll (r )
192+ if err != nil {
193+ return errors .Wrapf (err , "error reading the io stream" )
194+ }
195+
196+ err = json .Unmarshal (buf , & header )
197+ if err != nil {
198+ return errors .Wrap (err , "config format mismatch" )
199+ }
200+
201+ checkPlatform (header .OS , header .Architecture )
202+
203+ return nil
204+ }
205+
206+ func checkPlatform (OS string , Architecture string ) {
207+ validCombins := map [string ][]string {
208+ "android" : {"arm" },
209+ "darwin" : {"386" , "amd64" , "arm" , "arm64" },
210+ "dragonfly" : {"amd64" },
211+ "freebsd" : {"386" , "amd64" , "arm" },
212+ "linux" : {"386" , "amd64" , "arm" , "arm64" , "ppc64" , "ppc64le" , "mips64" , "mips64le" , "s390x" },
213+ "netbsd" : {"386" , "amd64" , "arm" },
214+ "openbsd" : {"386" , "amd64" , "arm" },
215+ "plan9" : {"386" , "amd64" },
216+ "solaris" : {"amd64" },
217+ "windows" : {"386" , "amd64" }}
218+ for os , archs := range validCombins {
219+ if os == OS {
220+ for _ , arch := range archs {
221+ if arch == Architecture {
222+ break
223+ }
224+ }
225+ fmt .Printf ("warning: combination of %q and %q is invalid." , OS , Architecture )
226+ }
227+ }
228+ fmt .Printf ("warning: operating system %q of the bundle is not supported yet." , OS )
229+ }
0 commit comments