Skip to content

Commit dc786a4

Browse files
author
Ma Shimiao
committed
validate: type is requried for validation
1. spec says any extra fields in json file can be ignored, so I think we can't clearly distingish which type the text file is, we'd better remove autodetect for text files. 2. without audotdetect, we must requrie to user to set file type for validation Signed-off-by: Ma Shimiao <[email protected]>
1 parent 386cd33 commit dc786a4

File tree

4 files changed

+27
-70
lines changed

4 files changed

+27
-70
lines changed

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 validateHandler(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
var errs []string
5860
for _, arg := range context.Args() {
5961
err := validatePath(arg)
@@ -66,10 +68,10 @@ func validateHandler(context *cli.Context) error {
6668
if verr, ok := errors.Cause(err).(schema.ValidationError); ok {
6769
errs = append(errs, fmt.Sprintf("%v", verr.Errs))
6870
} else if serr, ok := errors.Cause(err).(*schema.SyntaxError); ok {
69-
errs = append(errs, fmt.Sprintf("%s:%d:%d: validation failed: %v", arg, serr.Line, serr.Col, err))
71+
errs = append(errs, fmt.Sprintf("%s:%d:%d: %v", arg, serr.Line, serr.Col, err))
7072
continue
7173
} else {
72-
errs = append(errs, fmt.Sprintf("%s: validation failed: %v", arg, err))
74+
errs = append(errs, fmt.Sprintf("%s: %v", arg, err))
7375
continue
7476
}
7577

@@ -88,29 +90,29 @@ func validatePath(name string) error {
8890
typ = v.typ
8991
)
9092

91-
if typ == "" {
92-
if typ, err = image.Autodetect(name); err != nil {
93-
return errors.Wrap(err, "unable to determine type")
94-
}
95-
}
96-
9793
if v.stdout == nil {
9894
v.stdout = log.New(os.Stdout, "oci-image-tool: ", 0)
9995
}
10096

101-
switch typ {
102-
case image.TypeImageLayout:
103-
return image.ValidateLayout(name, v.refs, v.stdout)
104-
case image.TypeImageZip:
105-
return image.ValidateZip(name, v.refs, v.stdout)
106-
case image.TypeImage:
107-
return image.ValidateFile(name, v.refs, v.stdout)
97+
if typ == image.TypeImage {
98+
imageType, err := image.Autodetect(name)
99+
if err != nil {
100+
return errors.Wrap(err, "unable to determine image type")
101+
}
102+
fmt.Println("autodetected image file type is:", imageType)
103+
switch imageType {
104+
case image.TypeImageLayout:
105+
return image.ValidateLayout(name, v.refs, v.stdout)
106+
case image.TypeImageZip:
107+
return image.ValidateZip(name, v.refs, v.stdout)
108+
case image.TypeImage:
109+
return image.ValidateFile(name, v.refs, v.stdout)
110+
}
108111
}
109112

110113
if len(v.refs) != 0 {
111-
fmt.Printf("WARNING: type %q does not support refs, which are only appropriate if type is image or imageLayout.\n", typ)
114+
fmt.Println("WARNING: refs are only appropriate if type is image")
112115
}
113-
114116
f, err := os.Open(name)
115117
if err != nil {
116118
return errors.Wrap(err, "unable to open file")
@@ -137,13 +139,13 @@ var validateCommand = cli.Command{
137139
cli.StringFlag{
138140
Name: "type",
139141
Usage: fmt.Sprintf(
140-
`Type of the file to validate. If unset, oci-image-tool will try to auto-detect the type. One of "%s".`,
142+
`Type of the file to validate. One of "%s".`,
141143
strings.Join(validateTypes, ","),
142144
),
143145
},
144146
cli.StringSliceFlag{
145147
Name: "ref",
146-
Usage: "A set of refs pointing to the manifests to be validated. Each reference must be present in the refs subdirectory of the image. Only applicable if type is image or imageLayout.",
148+
Usage: "A set of refs pointing to the manifests to be validated. Each reference must be present in the refs subdirectory of the image. Only applicable if type is image",
147149
},
148150
},
149151
}

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
@@ -19,15 +19,15 @@ oci-image-tool validate \- Validate one or more image files
1919
The reference to validate (should point to a manifest).
2020
Can be specified multiple times to validate multiple references.
2121
`NAME` must be present in the `refs` subdirectory of the image.
22-
Only applicable if type is image or imageLayout.
22+
Only applicable if type is image.
2323

2424
**--type**=""
25-
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"
25+
Type of the file to validate. One of "image,manifest,imageIndex,config"
2626

2727
# EXAMPLES
2828
```
2929
$ skopeo copy docker://busybox oci:busybox-oci
30-
$ oci-image-tool validate --type imageLayout --ref latest busybox-oci
30+
$ oci-image-tool validate --type image --ref latest busybox-oci
3131
busybox-oci: OK
3232
```
3333

0 commit comments

Comments
 (0)