Skip to content

Commit cd48670

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

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

image/image.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"log"
2121
"os"
2222
"path/filepath"
23+
"strings"
2324

2425
"github.com/opencontainers/image-spec/specs-go/v1"
2526
"github.com/pkg/errors"
@@ -87,6 +88,18 @@ func validate(w walker, refs []string, out *log.Logger) error {
8788
if err := m.validate(w); err != nil {
8889
return err
8990
}
91+
92+
ml, err := findManifestList(w, d)
93+
if err == nil {
94+
if err := ml.validate(w); err != nil {
95+
return err
96+
}
97+
} else {
98+
if !strings.Contains(err, "manifestlist not found") {
99+
return err
100+
}
101+
}
102+
90103
if out != nil {
91104
out.Printf("reference %q: OK", ref)
92105
}

image/manifest_list.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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 manifestlist struct {
29+
Manifests []ManifestDescriptor `json:"manifests"`
30+
}
31+
type ManifestDescriptor v1.ManifestDescriptor
32+
33+
func findManifestList(w walker, d *descriptor) (*manifestlist, error) {
34+
var ml manifestlist
35+
mlpath := filepath.Join("blobs", d.algo(), d.hash())
36+
37+
switch err := w.walk(func(path string, info os.FileInfo, r io.Reader) error {
38+
if info.IsDir() || filepath.Clean(path) != mlpath {
39+
return nil
40+
}
41+
42+
buf, err := ioutil.ReadAll(r)
43+
if err != nil {
44+
return errors.Wrapf(err, "%s: error reading manifestlist", path)
45+
}
46+
47+
if err := schema.MediaTypeManifestList.Validate(bytes.NewReader(buf)); err != nil {
48+
return errors.Wrapf(err, "%s: manifestlist validation failed", path)
49+
}
50+
51+
if err := json.Unmarshal(buf, &ml); err != nil {
52+
return err
53+
}
54+
55+
if len(ml.Manifests) == 0 {
56+
return fmt.Errorf("%s: no manifests found", path)
57+
}
58+
59+
return errEOW
60+
}); err {
61+
case nil:
62+
return nil, fmt.Errorf("%s: manifestlist not found", mlpath)
63+
case errEOW:
64+
return &ml, nil
65+
default:
66+
return nil, err
67+
}
68+
}
69+
70+
func (ml *manifestlist) validate(w walker) error {
71+
for _, d := range ml.Manifests {
72+
if err := d.Descriptor.validate(w, []string{v1.MediaTypeImageConfig}); err != nil {
73+
return errors.Wrap(err, "Descriptor validation failed")
74+
}
75+
if err := checkPlatform(d); err != nil {
76+
return errors.Wrap(err, "platform validation failed")
77+
}
78+
}
79+
80+
return nil
81+
}
82+
83+
func checkPlatform(m ManifestDescriptor) error {
84+
validCombins := map[string][]string{
85+
"darwin": {"386", "amd64", "arm", "arm64"},
86+
"dragonfly": {"amd64"},
87+
"freebsd": {"386", "amd64", "arm"},
88+
"linux": {"386", "amd64", "arm", "arm64", "ppc64", "ppc64le", "mips64", "mips64le"},
89+
"netbsd": {"386", "amd64", "arm"},
90+
"openbsd": {"386", "amd64", "arm"},
91+
"plan9": {"386", "amd64"},
92+
"solaris": {"amd64"},
93+
"windows": {"386", "amd64"}}
94+
for os, archs := range validCombins {
95+
if os == m.Platform.OS {
96+
for _, arch := range archs {
97+
if arch == m.Platform.Architecture {
98+
return nil
99+
}
100+
}
101+
return fmt.Errorf("Combination of %q and %q is invalid.", m.Platform.OS, m.Platform.Architecture)
102+
}
103+
}
104+
return fmt.Errorf("Operation system %q of the bundle is not supported yet.", m.Platform.OS)
105+
}

0 commit comments

Comments
 (0)