Skip to content

Commit bf107ce

Browse files
author
zhouhao
committed
Add manifestlist validation
Signed-off-by: zhouhao <[email protected]>
1 parent f24d27b commit bf107ce

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

image/image.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ func validate(w walker, refs []string, out *log.Logger) error {
8787
if err := m.validate(w); err != nil {
8888
return err
8989
}
90+
91+
ml, err := findManifest_list(w, d)
92+
if err == nil {
93+
if err := ml.validate(w); err != nil {
94+
return err
95+
}
96+
}
97+
9098
if out != nil {
9199
out.Printf("reference %q: OK", ref)
92100
}

image/manifest_list.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Copyright 2016 The Linux Foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package image
16+
17+
import (
18+
"encoding/json"
19+
"fmt"
20+
"os"
21+
"path/filepath"
22+
23+
"github.com/opencontainers/image-spec/schema"
24+
"github.com/opencontainers/image-spec/specs-go/v1"
25+
"github.com/pkg/errors"
26+
)
27+
28+
type manifest_list struct {
29+
Manifests []ManifestDescriptor `json:"manifests"`
30+
}
31+
32+
type ManifestDescriptor struct {
33+
Descriptor v1.Descriptor
34+
35+
// Platform describes the platform which the image in the manifest runs on.
36+
Platform Platform `json:"platform"`
37+
}
38+
39+
func findManifest_list(w walker, d *descriptor) (*manifest_list, error) {
40+
var ml manifest_list
41+
mlpath := filepath.Join("blobs", d.algo(), d.hash())
42+
43+
switch err := w.walk(func(path string, info os.FileInfo, r io.Reader) error {
44+
if info.IsDir() || filepath.Clean(path) != mlpath {
45+
return nil
46+
}
47+
48+
buf, err := ioutil.ReadAll(r)
49+
if err != nil {
50+
return errors.Wrapf(err, "%s: error reading manifest_list", path)
51+
}
52+
53+
if err := schema.MediaTypeManifestList.Validate(bytes.NewReader(buf)); err != nil {
54+
return errors.Wrapf(err, "%s: manifest_list validation failed", path)
55+
}
56+
57+
if err := json.Unmarshal(buf, &ml); err != nil {
58+
return err
59+
}
60+
61+
if len(m.Manifests) == 0 {
62+
return fmt.Errorf("%s: no manifests found", path)
63+
}
64+
65+
return errEOW
66+
}); err {
67+
case nil:
68+
return nil, fmt.Errorf("%s: manifest_list not found", mpath)
69+
case errEOW:
70+
return &m, nil
71+
default:
72+
return nil, err
73+
}
74+
}
75+
76+
func (ml *manifest_list) validate(w walker) error {
77+
for _, d := range ml.Manifests {
78+
if err := d.Descriptor.validate(w, []string{v1.MediaTypeImageConfig}); err != nil {
79+
return errors.Wrap(err, "Descriptor validation failed")
80+
}
81+
if err := checkPlatform(d); err != nil {
82+
return errors.Wrap(err, "platform validation failed")
83+
}
84+
}
85+
86+
return nil
87+
}
88+
89+
func checkPlatform(m Manifests) error {
90+
validCombins := map[string][]string{
91+
"darwin": {"386", "amd64", "arm", "arm64"},
92+
"dragonfly": {"amd64"},
93+
"freebsd": {"386", "amd64", "arm"},
94+
"linux": {"386", "amd64", "arm", "arm64", "ppc64", "ppc64le", "mips64", "mips64le"},
95+
"netbsd": {"386", "amd64", "arm"},
96+
"openbsd": {"386", "amd64", "arm"},
97+
"plan9": {"386", "amd64"},
98+
"solaris": {"amd64"},
99+
"windows": {"386", "amd64"}}
100+
for os, archs := range validCombins {
101+
if os == m.Platform.OS {
102+
for _, arch := range archs {
103+
if arch == m.Platform.Architecture {
104+
return nil
105+
}
106+
}
107+
return fmt.Errorf("Combination of %q and %q is invalid.", m.Platform.OS, m.Platform.Architecture)
108+
}
109+
}
110+
return fmt.Errorf("Operation system %q of the bundle is not supported yet.", m.Platform.OS)
111+
}

0 commit comments

Comments
 (0)