Skip to content

Commit

Permalink
cmd/oci-image-tool: validate all refs by default
Browse files Browse the repository at this point in the history
Signed-off-by: Antonio Murdaca <[email protected]>
  • Loading branch information
runcom committed Sep 8, 2016
1 parent 9a93cca commit 5c9400d
Show file tree
Hide file tree
Showing 3 changed files with 47 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
24 changes: 24 additions & 0 deletions image/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 22 additions & 8 deletions image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down

0 comments on commit 5c9400d

Please sign in to comment.