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

cmd/oci-image-tool: validate all refs by default #279

Merged
merged 1 commit into from
Sep 13, 2016
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
2 changes: 1 addition & 1 deletion cmd/oci-image-tool/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func newValidateCmd(stdout, stderr *log.Logger) *cobra.Command {
)

cmd.Flags().StringSliceVar(
&v.refs, "ref", []string{"v1.0"},
&v.refs, "ref", nil,
`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.`,
)

Expand Down
21 changes: 21 additions & 0 deletions image/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,27 @@ func (d *descriptor) hash() string {
return pts[1]
}

func listReferences(w walker) (map[string]*descriptor, error) {
refs := make(map[string]*descriptor)

if err := w.walk(func(path string, info os.FileInfo, r io.Reader) error {
if info.IsDir() || !strings.HasPrefix(path, "refs") {
return nil
}

var d descriptor
if err := json.NewDecoder(r).Decode(&d); err != nil {
return err
}
refs[info.Name()] = &d

return nil
}); err != nil {
return nil, err
}
return refs, nil
}

func findDescriptor(w walker, name string) (*descriptor, error) {
var d descriptor
dpath := filepath.Join("refs", name)
Expand Down
37 changes: 29 additions & 8 deletions image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package image

import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
Expand All @@ -33,7 +34,7 @@ func ValidateLayout(src string, refs []string, out *log.Logger) error {

// Validate walks through the given .tar file and
// validates the manifest pointed to by the given refs
// or returns an error if the validation failed.
//iiii or returns an error if the validation failed.
Copy link
Collaborator

Choose a reason for hiding this comment

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

iiii? ;-)

Copy link
Member Author

Choose a reason for hiding this comment

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

lol

func Validate(tarFile string, refs []string, out *log.Logger) error {
f, err := os.Open(tarFile)
if err != nil {
Expand All @@ -50,17 +51,37 @@ var validRefMediaTypes = []string{
}

func validate(w walker, refs []string, out *log.Logger) error {
for _, r := range refs {
ref, err := findDescriptor(w, r)
if err != nil {
return err
ds, err := listReferences(w)
if err != nil {
return err
}
if len(refs) == 0 && len(ds) == 0 {
// TODO(runcom): ugly, we'll need a better way and library
// to express log levels.
// see https://github.com/opencontainers/image-spec/issues/288
out.Print("WARNING: no descriptors found")
}

if len(refs) == 0 {
for ref := range ds {
refs = append(refs, ref)
}
}

for _, ref := range refs {
d, ok := ds[ref]
if !ok {
// TODO(runcom):
// soften this error to a warning if the user didn't ask for any specific reference
// with --ref but she's just validating the whole image.
return fmt.Errorf("reference %s not found", ref)
}

if err = ref.validate(w, validRefMediaTypes); err != nil {
if err = d.validate(w, validRefMediaTypes); err != nil {
return err
}

m, err := findManifest(w, ref)
m, err := findManifest(w, d)
if err != nil {
return err
}
Expand All @@ -69,7 +90,7 @@ func validate(w walker, refs []string, out *log.Logger) error {
return err
}
if out != nil {
out.Printf("reference %q: OK", r)
out.Printf("reference %q: OK", ref)
}
}
return nil
Expand Down