Skip to content

Commit df5907a

Browse files
author
zhouhao
committed
image: add image layout validation
Signed-off-by: zhouhao <[email protected]>
1 parent 83850e8 commit df5907a

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

image/image.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ var validRefMediaTypes = []string{
5858
}
5959

6060
func validate(w walker, refs []string, out *log.Logger) error {
61+
if err := layoutValidate(w); err != nil {
62+
return err
63+
}
64+
6165
ds, err := listReferences(w)
6266
if err != nil {
6367
return err
@@ -130,6 +134,10 @@ func Unpack(r io.ReadSeeker, dest, refName string) error {
130134
}
131135

132136
func unpack(w walker, dest, refName string) error {
137+
if err := layoutValidate(w); err != nil {
138+
return err
139+
}
140+
133141
ref, err := findDescriptor(w, refName)
134142
if err != nil {
135143
return err
@@ -178,6 +186,10 @@ func CreateRuntimeBundle(r io.ReadSeeker, dest, ref, root string) error {
178186
}
179187

180188
func createRuntimeBundle(w walker, dest, refName, rootfs string) error {
189+
if err := layoutValidate(w); err != nil {
190+
return err
191+
}
192+
181193
ref, err := findDescriptor(w, refName)
182194
if err != nil {
183195
return err

image/layout.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
"strings"
25+
26+
"github.com/opencontainers/image-spec/schema"
27+
"github.com/opencontainers/image-spec/specs-go/v1"
28+
"github.com/pkg/errors"
29+
)
30+
31+
func layoutValidate(w walker) error {
32+
var blobsExist, indexExist, layoutExist bool
33+
34+
if err := w.walk(func(path string, info os.FileInfo, r io.Reader) error {
35+
if strings.EqualFold(path, "blobs") {
36+
blobsExist = true
37+
if !info.IsDir() {
38+
return fmt.Errorf("blobs is not a directory")
39+
}
40+
41+
return nil
42+
}
43+
44+
if strings.EqualFold(path, "index.json") {
45+
indexExist = true
46+
if info.IsDir() {
47+
return fmt.Errorf("index.json is a directory")
48+
}
49+
50+
var index v1.Index
51+
buf, err := ioutil.ReadAll(r)
52+
if err != nil {
53+
return errors.Wrap(err, "error reading index.json")
54+
}
55+
56+
if err := json.Unmarshal(buf, &index); err != nil {
57+
return errors.Wrap(err, "index.json format mismatch")
58+
}
59+
60+
return nil
61+
}
62+
63+
if strings.EqualFold(path, "oci-layout") {
64+
layoutExist = true
65+
if info.IsDir() {
66+
return fmt.Errorf("oci-layout is a directory")
67+
}
68+
69+
var imageLayout v1.ImageLayout
70+
buf, err := ioutil.ReadAll(r)
71+
if err != nil {
72+
return errors.Wrap(err, "error reading oci-layout")
73+
}
74+
75+
if err := schema.ValidatorMediaTypeLayoutHeader.Validate(bytes.NewReader(buf)); err != nil {
76+
return errors.Wrap(err, "oci-layout: imageLayout validation failed")
77+
}
78+
79+
if err := json.Unmarshal(buf, &imageLayout); err != nil {
80+
return errors.Wrap(err, "oci-layout format mismatch")
81+
}
82+
83+
return nil
84+
}
85+
86+
return nil
87+
}); err != nil {
88+
return err
89+
}
90+
91+
if !blobsExist {
92+
return fmt.Errorf("image layout must contain blobs directory")
93+
}
94+
95+
if !indexExist {
96+
return fmt.Errorf("image layout must contain index.json file")
97+
}
98+
99+
if !layoutExist {
100+
return fmt.Errorf("image layout must contain oci-layout file")
101+
}
102+
103+
return nil
104+
}

0 commit comments

Comments
 (0)