| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package image |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"encoding/json" |
| 20 |
"fmt" |
| 21 |
"io" |
| 22 |
"io/ioutil" |
| 23 |
"os" |
| 24 |
"path/filepath" |
| 25 |
|
| 26 |
"github.com/opencontainers/image-spec/schema" |
| 27 |
"github.com/opencontainers/image-spec/specs-go/v1" |
| 28 |
"github.com/pkg/errors" |
| 29 |
) |
| 30 |
|
| 31 |
func findIndex(w walker, d *v1.Descriptor) (*v1.Index, error) { |
| 32 |
var index v1.Index |
| 33 |
ipath := filepath.Join("blobs", string(d.Digest.Algorithm()), d.Digest.Hex()) |
| 34 |
|
| 35 |
switch err := w.walk(func(path string, info os.FileInfo, r io.Reader) error { |
| 36 |
if info.IsDir() || filepath.Clean(path) != ipath { |
| 37 |
return nil |
| 38 |
} |
| 39 |
|
| 40 |
buf, err := ioutil.ReadAll(r) |
| 41 |
if err != nil { |
| 42 |
return errors.Wrapf(err, "%s: error reading index", path) |
| 43 |
} |
| 44 |
|
| 45 |
if err := schema.ValidatorMediaTypeImageIndex.Validate(bytes.NewReader(buf)); err != nil { |
| 46 |
return errors.Wrapf(err, "%s: index validation failed", path) |
| 47 |
} |
| 48 |
|
| 49 |
if err := json.Unmarshal(buf, &index); err != nil { |
| 50 |
return err |
| 51 |
} |
| 52 |
|
| 53 |
return errEOW |
| 54 |
}); err { |
| 55 |
case errEOW: |
| 56 |
return &index, nil |
| 57 |
case nil: |
| 58 |
return nil, fmt.Errorf("index not found") |
| 59 |
default: |
| 60 |
return nil, err |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
func validateIndex(index *v1.Index, w walker) error { |
| 65 |
for _, manifest := range index.Manifests { |
| 66 |
if err := validateDescriptor(&manifest, w, []string{v1.MediaTypeImageManifest}); err != nil { |
| 67 |
return errors.Wrap(err, "manifest validation failed") |
| 68 |
} |
| 69 |
} |
| 70 |
return nil |
| 71 |
} |