Skip to content

Commit

Permalink
Merge pull request #279 from runcom/validate-all-refs
Browse files Browse the repository at this point in the history
cmd/oci-image-tool: validate all refs by default
  • Loading branch information
philips authored Sep 13, 2016
2 parents 2903142 + a480306 commit c2ed5aa
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
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.
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

0 comments on commit c2ed5aa

Please sign in to comment.