From 003e33bdefb29d8f35ddf9da6c28b5e7cf050904 Mon Sep 17 00:00:00 2001 From: zhouhao Date: Tue, 6 Dec 2016 13:22:10 +0800 Subject: [PATCH 1/2] Fix information Signed-off-by: zhouhao --- .../oci-create-runtime-bundle.1.md | 46 ++++++++++++++++ .../oci-image-validate.1.md | 42 +++++++++++++++ cmd/oci-unpack/oci-unpack.1.md | 53 +++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 cmd/oci-create-runtime-bundle/oci-create-runtime-bundle.1.md create mode 100644 cmd/oci-image-validate/oci-image-validate.1.md create mode 100644 cmd/oci-unpack/oci-unpack.1.md diff --git a/cmd/oci-create-runtime-bundle/oci-create-runtime-bundle.1.md b/cmd/oci-create-runtime-bundle/oci-create-runtime-bundle.1.md new file mode 100644 index 0000000..0c25fd9 --- /dev/null +++ b/cmd/oci-create-runtime-bundle/oci-create-runtime-bundle.1.md @@ -0,0 +1,46 @@ +% OCI(1) OCI-CREATE-RUNTIME-BUNDLE User Manuals +% OCI Community +% JULY 2016 +# NAME +oci-create-runtime-bundle \- Create an OCI runtime bundle + +# SYNOPSIS +**oci-create-runtime-bundle** [src] [dest] [flags] +**oci-create-runtime-bundle** [--help|-v|--version] + +# DESCRIPTION +`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. + +Also translates the referenced config from application/vnd.oci.image.config.v1+json to a +runtime-spec-compatible `dest/config.json`. + +# FLAGS +**--help** + Print usage statement + +**--ref**="" + The ref pointing to the manifest of the OCI image. This must be present in the "refs" subdirectory of the image. (default "v1.0") + +**--rootfs**="" + 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") + +**--type**="" + Type of the file to unpack. If unset, oci-create-runtime-bundle will try to auto-detect the type. One of "imageLayout,image" + +**-v**, **--version** + Print version information and exit. + +# EXAMPLES +``` +$ skopeo copy docker://busybox oci:busybox-oci +$ mkdir busybox-bundle +$ oci-create-runtime-bundle --ref latest busybox-oci busybox-bundle +$ cd busybox-bundle && sudo runc run busybox +[...] +``` + +# SEE ALSO +**runc**(1), **skopeo**(1) + +# HISTORY +Sept 2016, Originally compiled by Antonio Murdaca (runcom at redhat dot com) diff --git a/cmd/oci-image-validate/oci-image-validate.1.md b/cmd/oci-image-validate/oci-image-validate.1.md new file mode 100644 index 0000000..d9c93b6 --- /dev/null +++ b/cmd/oci-image-validate/oci-image-validate.1.md @@ -0,0 +1,42 @@ +% OCI(1) OCI-IMAGE-VALIDATE User Manuals +% OCI Community +% JULY 2016 +# NAME +oci-image-validate \- Validate one or more image files + +# SYNOPSIS +**oci-image-validate** FILE... [flags] +**oci-image-validate** [--help|-v|--version] + +# DESCRIPTION +`oci-image-validate` validates the given file(s) against the OCI image specification. + + +# FLAGS +**--help** + Print usage statement + +**--ref**=[] + The reference to validate (should point to a manifest). + Can be specified multiple times to validate multiple references. + `NAME` must be present in the `refs` subdirectory of the image. + Only applicable if type is image or imageLayout. + +**--type**="" + 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" + +**-v**, **--version** + Print version information and exit. + +# EXAMPLES +``` +$ skopeo copy docker://busybox oci:busybox-oci +$ oci-image-validate --type imageLayout --ref latest busybox-oci +busybox-oci: OK +``` + +# SEE ALSO +**skopeo**(1) + +# HISTORY +Sept 2016, Originally compiled by Antonio Murdaca (runcom at redhat dot com) diff --git a/cmd/oci-unpack/oci-unpack.1.md b/cmd/oci-unpack/oci-unpack.1.md new file mode 100644 index 0000000..fcbbd92 --- /dev/null +++ b/cmd/oci-unpack/oci-unpack.1.md @@ -0,0 +1,53 @@ +% OCI(1) OCI-UNPACK User Manuals +% OCI Community +% JULY 2016 +# NAME +oci-unpack \- Unpack an image or image source layout + +# SYNOPSIS +**oci-unpack** [src] [dest] [flags] +**oci-unpack** [--help|-v|--version] + +# DESCRIPTION +`oci-unpack` validates an application/vnd.oci.image.manifest.v1+json and unpacks its layered filesystem to `dest`. + +# FLAGS +**--help** + Print usage statement + +**--ref**="" + The ref pointing to the manifest to be unpacked. This must be present in the "refs" subdirectory of the image. (default "v1.0") + +**--type**="" + Type of the file to unpack. If unset, oci-unpack will try to auto-detect the type. One of "imageLayout,image" + +**-v**, **--version** + Print version information and exit. + +# EXAMPLES +``` +$ skopeo copy docker://busybox oci:busybox-oci +$ mkdir busybox-bundle +$ oci-unpack --ref latest busybox-oci busybox-bundle +$ tree busybox-bundle +busybox-bundle +├── bin +│   ├── [ +│   ├── [[ +│   ├── acpid +│   ├── addgroup +│   ├── add-shell +│   ├── adduser +│   ├── adjtimex +│   ├── ar +│   ├── arp +│   ├── arping +│   ├── ash +[...] +``` + +# SEE ALSO +**skopeo**(1) + +# HISTORY +Sept 2016, Originally compiled by Antonio Murdaca (runcom at redhat dot com) From c02c3c78a27515679793713cf03a5321fdff5013 Mon Sep 17 00:00:00 2001 From: Ma Shimiao Date: Sat, 9 Sep 2017 11:22:35 +0800 Subject: [PATCH 2/2] 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 --- cmd/oci-image-tool/validate.go | 44 ++++++++++++++++--------------- completions/bash/oci-image-tool | 2 -- image/autodetect.go | 45 +------------------------------- man/oci-image-tool-validate.1.md | 6 ++--- 4 files changed, 27 insertions(+), 70 deletions(-) diff --git a/cmd/oci-image-tool/validate.go b/cmd/oci-image-tool/validate.go index 64a8958..2873aef 100644 --- a/cmd/oci-image-tool/validate.go +++ b/cmd/oci-image-tool/validate.go @@ -28,9 +28,7 @@ import ( // supported validation types var validateTypes = []string{ - image.TypeImageLayout, image.TypeImage, - image.TypeImageZip, image.TypeManifest, image.TypeImageIndex, image.TypeConfig, @@ -54,6 +52,10 @@ func validateAction(context *cli.Context) error { refs: context.StringSlice("ref"), } + if v.typ == "" { + return fmt.Errorf("--type must be set") + } + for index, ref := range v.refs { for i := index + 1; i < len(v.refs); i++ { if ref == v.refs[i] { @@ -74,9 +76,9 @@ func validateAction(context *cli.Context) error { if verr, ok := errors.Cause(err).(schema.ValidationError); ok { errs = append(errs, fmt.Sprintf("%v", verr.Errs)) } else if serr, ok := errors.Cause(err).(*schema.SyntaxError); ok { - errs = append(errs, fmt.Sprintf("%s:%d:%d: validation failed: %v", arg, serr.Line, serr.Col, err)) + errs = append(errs, fmt.Sprintf("%s:%d:%d: %v", arg, serr.Line, serr.Col, err)) } else { - errs = append(errs, fmt.Sprintf("%s: validation failed: %v", arg, err)) + errs = append(errs, fmt.Sprintf("%s: %v", arg, err)) } } @@ -95,29 +97,29 @@ func validatePath(name string) error { typ = v.typ ) - if typ == "" { - if typ, err = image.Autodetect(name); err != nil { - return errors.Wrap(err, "unable to determine type") - } - } - if v.stdout == nil { v.stdout = log.New(os.Stdout, "oci-image-tool: ", 0) } - switch typ { - case image.TypeImageLayout: - return image.ValidateLayout(name, v.refs, v.stdout) - case image.TypeImageZip: - return image.ValidateZip(name, v.refs, v.stdout) - case image.TypeImage: - return image.ValidateFile(name, v.refs, v.stdout) + if typ == image.TypeImage { + imageType, err := image.Autodetect(name) + if err != nil { + return errors.Wrap(err, "unable to determine image type") + } + fmt.Println("autodetected image file type is:", imageType) + switch imageType { + case image.TypeImageLayout: + return image.ValidateLayout(name, v.refs, v.stdout) + case image.TypeImageZip: + return image.ValidateZip(name, v.refs, v.stdout) + case image.TypeImage: + return image.ValidateFile(name, v.refs, v.stdout) + } } if len(v.refs) != 0 { - fmt.Printf("WARNING: type %q does not support ref, which are only appropriate if type is image or imageLayout.\n", typ) + fmt.Println("WARNING: refs are only appropriate if type is image") } - f, err := os.Open(name) if err != nil { return errors.Wrap(err, "unable to open file") @@ -144,13 +146,13 @@ var validateCommand = cli.Command{ cli.StringFlag{ Name: "type", Usage: fmt.Sprintf( - `Type of the file to validate. If unset, oci-image-tool will try to auto-detect the type. One of "%s".`, + `Type of the file to validate. One of "%s".`, strings.Join(validateTypes, ","), ), }, cli.StringSliceFlag{ Name: "ref", - 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.", + 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", }, }, } diff --git a/completions/bash/oci-image-tool b/completions/bash/oci-image-tool index a6a2157..a0c7c57 100644 --- a/completions/bash/oci-image-tool +++ b/completions/bash/oci-image-tool @@ -113,8 +113,6 @@ __oci-image-tool_complete_validate_types() { config image imageIndex - imageLayout - imageZip manifest " -- "$cur" ) ) } diff --git a/image/autodetect.go b/image/autodetect.go index c7f6363..54abbcc 100644 --- a/image/autodetect.go +++ b/image/autodetect.go @@ -15,13 +15,11 @@ package image import ( - "encoding/json" "io" "io/ioutil" "net/http" "os" - "github.com/opencontainers/image-spec/schema" "github.com/pkg/errors" ) @@ -65,48 +63,7 @@ func Autodetect(path string) (string, error) { return TypeImage, nil case "application/zip": return TypeImageZip, nil - - case "text/plain; charset=utf-8": - // might be a JSON file, will be handled below - - default: - return "", errors.New("unknown file type") - } - - if _, err := f.Seek(0, io.SeekStart); err != nil { - return "", errors.Wrap(err, "unable to seek") - } - - header := struct { - SchemaVersion int `json:"schemaVersion"` - MediaType string `json:"mediaType"` - Config interface{} `json:"config"` - }{} - - if err := json.NewDecoder(f).Decode(&header); err != nil { - if _, errSeek := f.Seek(0, io.SeekStart); errSeek != nil { - return "", errors.Wrap(err, "unable to seek") - } - - e := errors.Wrap( - schema.WrapSyntaxError(f, err), - "unable to parse JSON", - ) - - return "", e - } - - switch { - case header.MediaType == string(schema.ValidatorMediaTypeManifest): - return TypeManifest, nil - - case header.MediaType == string(schema.ValidatorMediaTypeImageIndex): - return TypeImageIndex, nil - - case header.MediaType == "" && header.SchemaVersion == 0 && header.Config != nil: - // config files don't have mediaType/schemaVersion header - return TypeConfig, nil } - return "", errors.New("unknown media type") + return "", errors.New("unknown file type") } diff --git a/man/oci-image-tool-validate.1.md b/man/oci-image-tool-validate.1.md index cf2b4c5..6ae22ed 100644 --- a/man/oci-image-tool-validate.1.md +++ b/man/oci-image-tool-validate.1.md @@ -20,15 +20,15 @@ oci-image-tool validate \- Validate one or more image files Reference should point to a manifest or index. e.g. --ref name=v1.0 --ref platform.os=latest Only support `name`, `platform.os` and `digest` three cases. - Only applicable if type is image or imageLayout. + Only applicable if type is image. **--type**="" - 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" + Type of the file to validate. One of "image,manifest,imageIndex,config" # EXAMPLES ``` $ skopeo copy docker://busybox oci:busybox-oci:latest -$ oci-image-tool validate --type imageLayout --ref name=latest busybox-oci +$ oci-image-tool validate --type image --ref name=latest busybox-oci busybox-oci: OK ```