Skip to content
This repository was archived by the owner on Jan 15, 2026. It is now read-only.

Commit fe62cfe

Browse files
author
zhouhao
committed
image: add image layout validation
Signed-off-by: zhouhao <zhouhao@cn.fujitsu.com>
1 parent 83850e8 commit fe62cfe

2 files changed

Lines changed: 112 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)