From 5c9400d6138ab3a5e93d6fa4fec9e9257c2a49b6 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 | 24 ++++++++++++++++++++++++ image/image.go | 30 ++++++++++++++++++++++-------- 3 files changed, 47 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..3716f5f44 100644 --- a/image/descriptor.go +++ b/image/descriptor.go @@ -49,6 +49,30 @@ func (d *descriptor) hash() string { return pts[1] } +func findDescriptors(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 + } + if len(refs) == 0 { + return nil, errors.New("no descriptors found") + } + 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..6b8d60c62 100644 --- a/image/image.go +++ b/image/image.go @@ -50,17 +50,31 @@ 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 := findDescriptors(w) + if err != nil { + return err + } + // TODO(runcom): this is O(N) but it could be O(1) if refs have been + // map[string]bool coming from the cli package. + // However, I don't think refs could be large enough to impact performance. + inRefs := func(ref string) bool { + for _, r := range refs { + if r == ref { + return true + } } - - if err = ref.validate(w, validRefMediaTypes); err != nil { + return false + } + hasRefs := len(refs) != 0 + for ref, d := range ds { + if hasRefs && !inRefs(ref) { + continue + } + 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 +83,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