Skip to content

Commit 0317d75

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

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-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(string(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: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
"bytes"
19+
"encoding/json"
20+
"fmt"
21+
"io"
22+
"io/ioutil"
23+
"os"
24+
"path/filepath"
25+
"strings"
26+
27+
"github.com/opencontainers/image-spec/schema"
28+
"github.com/opencontainers/image-spec/specs-go/v1"
29+
"github.com/pkg/errors"
30+
)
31+
32+
type manifestlist struct {
33+
Manifests []ManifestDescriptor `json:"manifests"`
34+
}
35+
type ManifestDescriptor struct {
36+
Config descriptor
37+
}
38+
39+
func findManifestList(w walker, d *descriptor) (*manifestlist, error) {
40+
var ml manifestlist
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 manifestlist", path)
51+
}
52+
53+
if err := schema.MediaTypeManifestList.Validate(bytes.NewReader(buf)); err != nil {
54+
return errors.Wrapf(err, "%s: manifestlist validation failed", path)
55+
}
56+
57+
if err := json.Unmarshal(buf, &ml); err != nil {
58+
return err
59+
}
60+
61+
if len(ml.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: manifestlist not found", mlpath)
69+
case errEOW:
70+
return &ml, nil
71+
default:
72+
return nil, err
73+
}
74+
}
75+
76+
func (ml *manifestlist) validate(w walker) error {
77+
for _, d := range ml.Manifests {
78+
if err := d.Config.validate(w, []string{v1.MediaTypeImageConfig}); err != nil {
79+
return errors.Wrap(err, "Descriptor validation failed")
80+
}
81+
}
82+
83+
return nil
84+
}

0 commit comments

Comments
 (0)