Skip to content
Open
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
16 changes: 5 additions & 11 deletions cmd/oci-image-tool/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package main

import (
"fmt"
"log"
"os"
"strings"

Expand All @@ -37,9 +36,8 @@ var validateTypes = []string{
}

type validateCmd struct {
stdout *log.Logger
typ string // the type to validate, can be empty string
refs []string
typ string // the type to validate, can be empty string
refs []string
}

var v validateCmd
Expand Down Expand Up @@ -94,17 +92,13 @@ func validatePath(name string) error {
}
}

if v.stdout == nil {
v.stdout = log.New(os.Stdout, "oci-image-tool: ", 0)
}

switch typ {
case image.TypeImageLayout:
return image.ValidateLayout(name, v.refs, v.stdout)
return image.ValidateLayout(name, v.refs)
case image.TypeImageZip:
return image.ValidateZip(name, v.refs, v.stdout)
return image.ValidateZip(name, v.refs)
case image.TypeImage:
return image.ValidateFile(name, v.refs, v.stdout)
return image.ValidateFile(name, v.refs)
}

if len(v.refs) != 0 {
Expand Down
25 changes: 11 additions & 14 deletions image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
Expand All @@ -29,42 +28,42 @@ import (

// ValidateLayout walks through the given file tree and validates the manifest
// pointed to by the given refs or returns an error if the validation failed.
func ValidateLayout(src string, refs []string, out *log.Logger) error {
return validate(newPathWalker(src), refs, out)
func ValidateLayout(src string, refs []string) error {
return validate(newPathWalker(src), refs)
}

// ValidateZip walks through the given file tree and validates the manifest
// pointed to by the given refs or returns an error if the validation failed.
func ValidateZip(src string, refs []string, out *log.Logger) error {
return validate(newZipWalker(src), refs, out)
func ValidateZip(src string, refs []string) error {
return validate(newZipWalker(src), refs)
}

// ValidateFile opens the tar file given by the filename, then calls ValidateReader
func ValidateFile(tarFile string, refs []string, out *log.Logger) error {
func ValidateFile(tarFile string, refs []string) error {
f, err := os.Open(tarFile)
if err != nil {
return errors.Wrap(err, "unable to open file")
}
defer f.Close()

return Validate(f, refs, out)
return Validate(f, refs)
}

// Validate walks through a tar stream and validates the manifest.
// * Check that all refs point to extant blobs
// * Checks that all referred blobs are valid
// * Checks that mime-types are correct
// returns error on validation failure
func Validate(r io.ReadSeeker, refs []string, out *log.Logger) error {
return validate(newTarWalker(r), refs, out)
func Validate(r io.ReadSeeker, refs []string) error {
return validate(newTarWalker(r), refs)
}

var validRefMediaTypes = []string{
v1.MediaTypeImageManifest,
v1.MediaTypeImageIndex,
}

func validate(w walker, refs []string, out *log.Logger) error {
func validate(w walker, refs []string) error {
if err := layoutValidate(w); err != nil {
return err
}
Expand All @@ -77,7 +76,7 @@ func validate(w walker, refs []string, out *log.Logger) error {
// 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")
fmt.Println("WARNING: no descriptors found")
}

if len(refs) == 0 {
Expand Down Expand Up @@ -137,9 +136,7 @@ func validate(w walker, refs []string, out *log.Logger) error {
}
}

if out != nil {
out.Printf("reference %q: OK", ref)
}
fmt.Printf("reference %q: OK\n", ref)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion image/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func TestImageLayout(t *testing.T) {
t.Fatal(err)
}

err = ValidateLayout(root, refTag, nil)
err = ValidateLayout(root, refTag)
if err != nil {
t.Fatal(err)
}
Expand Down