Skip to content

Commit c1c6538

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

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-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: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 struct {
32+
Config descriptor
33+
}
34+
35+
func findManifestList(w walker, d *descriptor) (*manifestlist, error) {
36+
var ml manifestlist
37+
mlpath := filepath.Join("blobs", d.algo(), d.hash())
38+
39+
switch err := w.walk(func(path string, info os.FileInfo, r io.Reader) error {
40+
if info.IsDir() || filepath.Clean(path) != mlpath {
41+
return nil
42+
}
43+
44+
buf, err := ioutil.ReadAll(r)
45+
if err != nil {
46+
return errors.Wrapf(err, "%s: error reading manifestlist", path)
47+
}
48+
49+
if err := schema.MediaTypeManifestList.Validate(bytes.NewReader(buf)); err != nil {
50+
return errors.Wrapf(err, "%s: manifestlist validation failed", path)
51+
}
52+
53+
if err := json.Unmarshal(buf, &ml); err != nil {
54+
return err
55+
}
56+
57+
if len(ml.Manifests) == 0 {
58+
return fmt.Errorf("%s: no manifests found", path)
59+
}
60+
61+
return errEOW
62+
}); err {
63+
case nil:
64+
return nil, fmt.Errorf("%s: manifestlist not found", mlpath)
65+
case errEOW:
66+
return &ml, nil
67+
default:
68+
return nil, err
69+
}
70+
}
71+
72+
func (ml *manifestlist) validate(w walker) error {
73+
for _, d := range ml.Manifests {
74+
if err := d.Config.validate(w, []string{v1.MediaTypeImageConfig}); err != nil {
75+
return errors.Wrap(err, "Descriptor validation failed")
76+
}
77+
}
78+
79+
return nil
80+
}

0 commit comments

Comments
 (0)