Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate: remove autodetect for type and add require limit #184

Merged
merged 2 commits into from
Jan 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions cmd/oci-create-runtime-bundle/oci-create-runtime-bundle.1.md
Original file line number Diff line number Diff line change
@@ -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)
44 changes: 23 additions & 21 deletions cmd/oci-image-tool/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ import (

// supported validation types
var validateTypes = []string{
image.TypeImageLayout,
image.TypeImage,
image.TypeImageZip,
image.TypeManifest,
image.TypeImageIndex,
image.TypeConfig,
Expand All @@ -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] {
Expand All @@ -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))
}

}
Expand All @@ -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")
Expand All @@ -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",
},
},
}
42 changes: 42 additions & 0 deletions cmd/oci-image-validate/oci-image-validate.1.md
Original file line number Diff line number Diff line change
@@ -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)
53 changes: 53 additions & 0 deletions cmd/oci-unpack/oci-unpack.1.md
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 0 additions & 2 deletions completions/bash/oci-image-tool
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ __oci-image-tool_complete_validate_types() {
config
image
imageIndex
imageLayout
imageZip
manifest
" -- "$cur" ) )
}
Expand Down
45 changes: 1 addition & 44 deletions image/autodetect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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")
}
6 changes: 3 additions & 3 deletions man/oci-image-tool-validate.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An image isn't the layout. The layout is the collection of resources that can make up an image. For example, I can still have an image and not have a layout.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, agree. And as you said, layout makes up an image. If we have a layout then we have an image. The point is image may be stored in different file types.
But if you don't have a layout, then it will not be a valid OCI image.
So, how do you think we should change the help info?


# 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
```

Expand Down