From a48030640fa2c7a68dde0de252a012cc057143eb Mon Sep 17 00:00:00 2001 From: Antonio Murdaca Date: Thu, 8 Sep 2016 15:18:00 +0200 Subject: [PATCH] cmd/oci-image-tool: validate all refs by default Signed-off-by: Antonio Murdaca --- cmd/oci-image-tool/validate.go | 2 +- image/descriptor.go | 21 +++++++++++++++++++ image/image.go | 37 ++++++++++++++++++++++++++-------- 3 files changed, 51 insertions(+), 9 deletions(-) diff --git a/cmd/oci-image-tool/validate.go b/cmd/oci-image-tool/validate.go index 7c265f47f..617279670 100644 --- a/cmd/oci-image-tool/validate.go +++ b/cmd/oci-image-tool/validate.go @@ -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.`, ) diff --git a/image/descriptor.go b/image/descriptor.go index d00b1bbe5..341b65c47 100644 --- a/image/descriptor.go +++ b/image/descriptor.go @@ -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) diff --git a/image/image.go b/image/image.go index 85d4c7ff6..e0effbe53 100644 --- a/image/image.go +++ b/image/image.go @@ -16,6 +16,7 @@ package image import ( "encoding/json" + "fmt" "log" "os" "path/filepath" @@ -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. func Validate(tarFile string, refs []string, out *log.Logger) error { f, err := os.Open(tarFile) if err != nil { @@ -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 } @@ -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