Skip to content

Commit 6539f0e

Browse files
author
Zhou Hao
authored
Merge pull request #184 from Mashimiao/validate-type-req
validate: remove autodetect for type and add require limit
2 parents bb7b937 + c02c3c7 commit 6539f0e

File tree

7 files changed

+168
-70
lines changed

7 files changed

+168
-70
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
% OCI(1) OCI-CREATE-RUNTIME-BUNDLE User Manuals
2+
% OCI Community
3+
% JULY 2016
4+
# NAME
5+
oci-create-runtime-bundle \- Create an OCI runtime bundle
6+
7+
# SYNOPSIS
8+
**oci-create-runtime-bundle** [src] [dest] [flags]
9+
**oci-create-runtime-bundle** [--help|-v|--version]
10+
11+
# DESCRIPTION
12+
`oci-create-runtime-bundle` validates an application/vnd.oci.image.manifest.v1+json and unpacks its layered filesystem to `dest/rootfs`, although the target directory is configurable with `--rootfs`. See **oci-unpack**(1) for more details on this process.
13+
14+
Also translates the referenced config from application/vnd.oci.image.config.v1+json to a
15+
runtime-spec-compatible `dest/config.json`.
16+
17+
# FLAGS
18+
**--help**
19+
Print usage statement
20+
21+
**--ref**=""
22+
The ref pointing to the manifest of the OCI image. This must be present in the "refs" subdirectory of the image. (default "v1.0")
23+
24+
**--rootfs**=""
25+
A directory representing the root filesystem of the container in the OCI runtime bundle. It is strongly recommended to keep the default value. (default "rootfs")
26+
27+
**--type**=""
28+
Type of the file to unpack. If unset, oci-create-runtime-bundle will try to auto-detect the type. One of "imageLayout,image"
29+
30+
**-v**, **--version**
31+
Print version information and exit.
32+
33+
# EXAMPLES
34+
```
35+
$ skopeo copy docker://busybox oci:busybox-oci
36+
$ mkdir busybox-bundle
37+
$ oci-create-runtime-bundle --ref latest busybox-oci busybox-bundle
38+
$ cd busybox-bundle && sudo runc run busybox
39+
[...]
40+
```
41+
42+
# SEE ALSO
43+
**runc**(1), **skopeo**(1)
44+
45+
# HISTORY
46+
Sept 2016, Originally compiled by Antonio Murdaca (runcom at redhat dot com)

cmd/oci-image-tool/validate.go

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ import (
2828

2929
// supported validation types
3030
var validateTypes = []string{
31-
image.TypeImageLayout,
3231
image.TypeImage,
33-
image.TypeImageZip,
3432
image.TypeManifest,
3533
image.TypeImageIndex,
3634
image.TypeConfig,
@@ -54,6 +52,10 @@ func validateAction(context *cli.Context) error {
5452
refs: context.StringSlice("ref"),
5553
}
5654

55+
if v.typ == "" {
56+
return fmt.Errorf("--type must be set")
57+
}
58+
5759
for index, ref := range v.refs {
5860
for i := index + 1; i < len(v.refs); i++ {
5961
if ref == v.refs[i] {
@@ -74,9 +76,9 @@ func validateAction(context *cli.Context) error {
7476
if verr, ok := errors.Cause(err).(schema.ValidationError); ok {
7577
errs = append(errs, fmt.Sprintf("%v", verr.Errs))
7678
} else if serr, ok := errors.Cause(err).(*schema.SyntaxError); ok {
77-
errs = append(errs, fmt.Sprintf("%s:%d:%d: validation failed: %v", arg, serr.Line, serr.Col, err))
79+
errs = append(errs, fmt.Sprintf("%s:%d:%d: %v", arg, serr.Line, serr.Col, err))
7880
} else {
79-
errs = append(errs, fmt.Sprintf("%s: validation failed: %v", arg, err))
81+
errs = append(errs, fmt.Sprintf("%s: %v", arg, err))
8082
}
8183

8284
}
@@ -95,29 +97,29 @@ func validatePath(name string) error {
9597
typ = v.typ
9698
)
9799

98-
if typ == "" {
99-
if typ, err = image.Autodetect(name); err != nil {
100-
return errors.Wrap(err, "unable to determine type")
101-
}
102-
}
103-
104100
if v.stdout == nil {
105101
v.stdout = log.New(os.Stdout, "oci-image-tool: ", 0)
106102
}
107103

108-
switch typ {
109-
case image.TypeImageLayout:
110-
return image.ValidateLayout(name, v.refs, v.stdout)
111-
case image.TypeImageZip:
112-
return image.ValidateZip(name, v.refs, v.stdout)
113-
case image.TypeImage:
114-
return image.ValidateFile(name, v.refs, v.stdout)
104+
if typ == image.TypeImage {
105+
imageType, err := image.Autodetect(name)
106+
if err != nil {
107+
return errors.Wrap(err, "unable to determine image type")
108+
}
109+
fmt.Println("autodetected image file type is:", imageType)
110+
switch imageType {
111+
case image.TypeImageLayout:
112+
return image.ValidateLayout(name, v.refs, v.stdout)
113+
case image.TypeImageZip:
114+
return image.ValidateZip(name, v.refs, v.stdout)
115+
case image.TypeImage:
116+
return image.ValidateFile(name, v.refs, v.stdout)
117+
}
115118
}
116119

117120
if len(v.refs) != 0 {
118-
fmt.Printf("WARNING: type %q does not support ref, which are only appropriate if type is image or imageLayout.\n", typ)
121+
fmt.Println("WARNING: refs are only appropriate if type is image")
119122
}
120-
121123
f, err := os.Open(name)
122124
if err != nil {
123125
return errors.Wrap(err, "unable to open file")
@@ -144,13 +146,13 @@ var validateCommand = cli.Command{
144146
cli.StringFlag{
145147
Name: "type",
146148
Usage: fmt.Sprintf(
147-
`Type of the file to validate. If unset, oci-image-tool will try to auto-detect the type. One of "%s".`,
149+
`Type of the file to validate. One of "%s".`,
148150
strings.Join(validateTypes, ","),
149151
),
150152
},
151153
cli.StringSliceFlag{
152154
Name: "ref",
153-
Usage: "A set of ref specify the search criteria for the validated reference. Format is A=B. Only support 'name', 'platform.os' and 'digest' three cases. Only applicable if type is image or imageLayout.",
155+
Usage: "A set of ref specify the search criteria for the validated reference. Format is A=B. Only support 'name', 'platform.os' and 'digest' three cases. Only applicable if type is image",
154156
},
155157
},
156158
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
% OCI(1) OCI-IMAGE-VALIDATE User Manuals
2+
% OCI Community
3+
% JULY 2016
4+
# NAME
5+
oci-image-validate \- Validate one or more image files
6+
7+
# SYNOPSIS
8+
**oci-image-validate** FILE... [flags]
9+
**oci-image-validate** [--help|-v|--version]
10+
11+
# DESCRIPTION
12+
`oci-image-validate` validates the given file(s) against the OCI image specification.
13+
14+
15+
# FLAGS
16+
**--help**
17+
Print usage statement
18+
19+
**--ref**=[]
20+
The reference to validate (should point to a manifest).
21+
Can be specified multiple times to validate multiple references.
22+
`NAME` must be present in the `refs` subdirectory of the image.
23+
Only applicable if type is image or imageLayout.
24+
25+
**--type**=""
26+
Type of the file to validate. If unset, oci-image-validate will try to auto-detect the type. One of "imageLayout,image,manifest,manifestList,config"
27+
28+
**-v**, **--version**
29+
Print version information and exit.
30+
31+
# EXAMPLES
32+
```
33+
$ skopeo copy docker://busybox oci:busybox-oci
34+
$ oci-image-validate --type imageLayout --ref latest busybox-oci
35+
busybox-oci: OK
36+
```
37+
38+
# SEE ALSO
39+
**skopeo**(1)
40+
41+
# HISTORY
42+
Sept 2016, Originally compiled by Antonio Murdaca (runcom at redhat dot com)

cmd/oci-unpack/oci-unpack.1.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
% OCI(1) OCI-UNPACK User Manuals
2+
% OCI Community
3+
% JULY 2016
4+
# NAME
5+
oci-unpack \- Unpack an image or image source layout
6+
7+
# SYNOPSIS
8+
**oci-unpack** [src] [dest] [flags]
9+
**oci-unpack** [--help|-v|--version]
10+
11+
# DESCRIPTION
12+
`oci-unpack` validates an application/vnd.oci.image.manifest.v1+json and unpacks its layered filesystem to `dest`.
13+
14+
# FLAGS
15+
**--help**
16+
Print usage statement
17+
18+
**--ref**=""
19+
The ref pointing to the manifest to be unpacked. This must be present in the "refs" subdirectory of the image. (default "v1.0")
20+
21+
**--type**=""
22+
Type of the file to unpack. If unset, oci-unpack will try to auto-detect the type. One of "imageLayout,image"
23+
24+
**-v**, **--version**
25+
Print version information and exit.
26+
27+
# EXAMPLES
28+
```
29+
$ skopeo copy docker://busybox oci:busybox-oci
30+
$ mkdir busybox-bundle
31+
$ oci-unpack --ref latest busybox-oci busybox-bundle
32+
$ tree busybox-bundle
33+
busybox-bundle
34+
├── bin
35+
│   ├── [
36+
│   ├── [[
37+
│   ├── acpid
38+
│   ├── addgroup
39+
│   ├── add-shell
40+
│   ├── adduser
41+
│   ├── adjtimex
42+
│   ├── ar
43+
│   ├── arp
44+
│   ├── arping
45+
│   ├── ash
46+
[...]
47+
```
48+
49+
# SEE ALSO
50+
**skopeo**(1)
51+
52+
# HISTORY
53+
Sept 2016, Originally compiled by Antonio Murdaca (runcom at redhat dot com)

completions/bash/oci-image-tool

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,6 @@ __oci-image-tool_complete_validate_types() {
113113
config
114114
image
115115
imageIndex
116-
imageLayout
117-
imageZip
118116
manifest
119117
" -- "$cur" ) )
120118
}

image/autodetect.go

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515
package image
1616

1717
import (
18-
"encoding/json"
1918
"io"
2019
"io/ioutil"
2120
"net/http"
2221
"os"
2322

24-
"github.com/opencontainers/image-spec/schema"
2523
"github.com/pkg/errors"
2624
)
2725

@@ -65,48 +63,7 @@ func Autodetect(path string) (string, error) {
6563
return TypeImage, nil
6664
case "application/zip":
6765
return TypeImageZip, nil
68-
69-
case "text/plain; charset=utf-8":
70-
// might be a JSON file, will be handled below
71-
72-
default:
73-
return "", errors.New("unknown file type")
74-
}
75-
76-
if _, err := f.Seek(0, io.SeekStart); err != nil {
77-
return "", errors.Wrap(err, "unable to seek")
78-
}
79-
80-
header := struct {
81-
SchemaVersion int `json:"schemaVersion"`
82-
MediaType string `json:"mediaType"`
83-
Config interface{} `json:"config"`
84-
}{}
85-
86-
if err := json.NewDecoder(f).Decode(&header); err != nil {
87-
if _, errSeek := f.Seek(0, io.SeekStart); errSeek != nil {
88-
return "", errors.Wrap(err, "unable to seek")
89-
}
90-
91-
e := errors.Wrap(
92-
schema.WrapSyntaxError(f, err),
93-
"unable to parse JSON",
94-
)
95-
96-
return "", e
97-
}
98-
99-
switch {
100-
case header.MediaType == string(schema.ValidatorMediaTypeManifest):
101-
return TypeManifest, nil
102-
103-
case header.MediaType == string(schema.ValidatorMediaTypeImageIndex):
104-
return TypeImageIndex, nil
105-
106-
case header.MediaType == "" && header.SchemaVersion == 0 && header.Config != nil:
107-
// config files don't have mediaType/schemaVersion header
108-
return TypeConfig, nil
10966
}
11067

111-
return "", errors.New("unknown media type")
68+
return "", errors.New("unknown file type")
11269
}

man/oci-image-tool-validate.1.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ oci-image-tool validate \- Validate one or more image files
2020
Reference should point to a manifest or index.
2121
e.g. --ref name=v1.0 --ref platform.os=latest
2222
Only support `name`, `platform.os` and `digest` three cases.
23-
Only applicable if type is image or imageLayout.
23+
Only applicable if type is image.
2424

2525
**--type**=""
26-
Type of the file to validate. If unset, oci-image-tool will try to auto-detect the type. One of "imageLayout,image,imageZip,manifest,imageIndex,config"
26+
Type of the file to validate. One of "image,manifest,imageIndex,config"
2727

2828
# EXAMPLES
2929
```
3030
$ skopeo copy docker://busybox oci:busybox-oci:latest
31-
$ oci-image-tool validate --type imageLayout --ref name=latest busybox-oci
31+
$ oci-image-tool validate --type image --ref name=latest busybox-oci
3232
busybox-oci: OK
3333
```
3434

0 commit comments

Comments
 (0)