| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package image |
| 16 |
|
| 17 |
import ( |
| 18 |
"encoding/json" |
| 19 |
"fmt" |
| 20 |
"io" |
| 21 |
"os" |
| 22 |
"path/filepath" |
| 23 |
"strings" |
| 24 |
|
| 25 |
"github.com/opencontainers/image-spec/specs-go/v1" |
| 26 |
"github.com/pkg/errors" |
| 27 |
) |
| 28 |
|
| 29 |
const indexPath = "index.json" |
| 30 |
|
| 31 |
func listReferences(w walker) ([]v1.Descriptor, error) { |
| 32 |
var descs []v1.Descriptor |
| 33 |
var index v1.Index |
| 34 |
|
| 35 |
if err := w.walk(func(path string, info os.FileInfo, r io.Reader) error { |
| 36 |
if info.IsDir() || filepath.Clean(path) != indexPath { |
| 37 |
return nil |
| 38 |
} |
| 39 |
|
| 40 |
if err := json.NewDecoder(r).Decode(&index); err != nil { |
| 41 |
return err |
| 42 |
} |
| 43 |
descs = index.Manifests |
| 44 |
|
| 45 |
return nil |
| 46 |
}); err != nil { |
| 47 |
return nil, err |
| 48 |
} |
| 49 |
|
| 50 |
return descs, nil |
| 51 |
} |
| 52 |
|
| 53 |
func findDescriptor(w walker, names []string) ([]v1.Descriptor, error) { |
| 54 |
var descs []v1.Descriptor |
| 55 |
var index v1.Index |
| 56 |
dpath := "index.json" |
| 57 |
|
| 58 |
if err := w.find(dpath, func(path string, r io.Reader) error { |
| 59 |
if err := json.NewDecoder(r).Decode(&index); err != nil { |
| 60 |
return err |
| 61 |
} |
| 62 |
|
| 63 |
descs = index.Manifests |
| 64 |
for _, name := range names { |
| 65 |
argsParts := strings.Split(name, "=") |
| 66 |
if len(argsParts) != 2 { |
| 67 |
return fmt.Errorf("each ref must contain two parts") |
| 68 |
} |
| 69 |
|
| 70 |
switch argsParts[0] { |
| 71 |
case "name": |
| 72 |
for i := 0; i < len(descs); i++ { |
| 73 |
if descs[i].Annotations[v1.AnnotationRefName] != argsParts[1] { |
| 74 |
descs = append(descs[:i], descs[i+1:]...) |
| 75 |
} |
| 76 |
} |
| 77 |
case "platform.os": |
| 78 |
for i := 0; i < len(descs); i++ { |
| 79 |
if descs[i].Platform != nil && index.Manifests[i].Platform.OS != argsParts[1] { |
| 80 |
descs = append(descs[:i], descs[i+1:]...) |
| 81 |
} |
| 82 |
} |
| 83 |
case "digest": |
| 84 |
for i := 0; i < len(descs); i++ { |
| 85 |
if string(descs[i].Digest) != argsParts[1] { |
| 86 |
descs = append(descs[:i], descs[i+1:]...) |
| 87 |
} |
| 88 |
} |
| 89 |
default: |
| 90 |
return fmt.Errorf("criteria %q unimplemented", argsParts[0]) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
return nil |
| 95 |
}); err != nil { |
| 96 |
return nil, err |
| 97 |
} |
| 98 |
|
| 99 |
if len(descs) == 0 { |
| 100 |
return nil, fmt.Errorf("index.json: descriptor retrieved by refs %v is not match", names) |
| 101 |
} else if len(descs) > 1 { |
| 102 |
return nil, fmt.Errorf("index.json: descriptor retrieved by refs %v is not unique", names) |
| 103 |
} |
| 104 |
|
| 105 |
return descs, nil |
| 106 |
} |
| 107 |
|
| 108 |
func validateDescriptor(d *v1.Descriptor, w walker, mts []string) error { |
| 109 |
var found bool |
| 110 |
for _, mt := range mts { |
| 111 |
if d.MediaType == mt { |
| 112 |
found = true |
| 113 |
break |
| 114 |
} |
| 115 |
} |
| 116 |
if !found { |
| 117 |
return fmt.Errorf("invalid descriptor MediaType %q", d.MediaType) |
| 118 |
} |
| 119 |
|
| 120 |
if err := d.Digest.Validate(); err != nil { |
| 121 |
return err |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
verifier := d.Digest.Verifier() |
| 126 |
numBytes, err := w.get(*d, verifier) |
| 127 |
if err != nil { |
| 128 |
return err |
| 129 |
} |
| 130 |
|
| 131 |
if err != nil { |
| 132 |
return errors.Wrap(err, "error generating hash") |
| 133 |
} |
| 134 |
|
| 135 |
if numBytes != d.Size { |
| 136 |
return errors.New("size mismatch") |
| 137 |
} |
| 138 |
|
| 139 |
if !verifier.Verified() { |
| 140 |
return errors.New("digest mismatch") |
| 141 |
} |
| 142 |
|
| 143 |
return nil |
| 144 |
} |