forked from openshift/origin
/
manifestschema2handler.go
125 lines (103 loc) · 3.44 KB
/
manifestschema2handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package server
import (
"encoding/json"
"errors"
"github.com/docker/distribution"
"github.com/docker/distribution/context"
"github.com/docker/distribution/digest"
"github.com/docker/distribution/manifest/schema2"
imageapi "github.com/openshift/origin/pkg/image/api"
)
var (
errMissingURL = errors.New("missing URL on layer")
errUnexpectedURL = errors.New("unexpected URL on layer")
)
func unmarshalManifestSchema2(content []byte) (distribution.Manifest, error) {
var deserializedManifest schema2.DeserializedManifest
if err := json.Unmarshal(content, &deserializedManifest); err != nil {
return nil, err
}
return &deserializedManifest, nil
}
type manifestSchema2Handler struct {
repo *repository
manifest *schema2.DeserializedManifest
}
var _ ManifestHandler = &manifestSchema2Handler{}
func (h *manifestSchema2Handler) FillImageMetadata(ctx context.Context, image *imageapi.Image) error {
// The manifest.Config references a configuration object for a container by its digest.
// It needs to be fetched in order to fill an image object metadata below.
configBytes, err := h.repo.Blobs(ctx).Get(ctx, h.manifest.Config.Digest)
if err != nil {
context.GetLogger(ctx).Errorf("failed to get image config %s: %v", h.manifest.Config.Digest.String(), err)
return err
}
image.DockerImageConfig = string(configBytes)
if err := imageapi.ImageWithMetadata(image); err != nil {
return err
}
return nil
}
func (h *manifestSchema2Handler) Manifest() distribution.Manifest {
return h.manifest
}
func (h *manifestSchema2Handler) Payload() (mediaType string, payload []byte, canonical []byte, err error) {
mt, p, err := h.manifest.Payload()
return mt, p, p, err
}
func (h *manifestSchema2Handler) Verify(ctx context.Context, skipDependencyVerification bool) error {
var errs distribution.ErrManifestVerification
if skipDependencyVerification {
return nil
}
// we want to verify that referenced blobs exist locally or accessible over
// pullthroughBlobStore. The base image of this image can be remote repository
// and since we use pullthroughBlobStore all the layer existence checks will be
// successful. This means that the docker client will not attempt to send them
// to us as it will assume that the registry has them.
repo := h.repo
target := h.manifest.Target()
_, err := repo.Blobs(ctx).Stat(ctx, target.Digest)
if err != nil {
if err != distribution.ErrBlobUnknown {
errs = append(errs, err)
}
// On error here, we always append unknown blob errors.
errs = append(errs, distribution.ErrManifestBlobUnknown{Digest: target.Digest})
}
for _, fsLayer := range h.manifest.References() {
var err error
if fsLayer.MediaType != schema2.MediaTypeForeignLayer {
if len(fsLayer.URLs) == 0 {
_, err = repo.Blobs(ctx).Stat(ctx, fsLayer.Digest)
} else {
err = errUnexpectedURL
}
} else {
// Clients download this layer from an external URL, so do not check for
// its presense.
if len(fsLayer.URLs) == 0 {
err = errMissingURL
}
}
if err != nil {
if err != distribution.ErrBlobUnknown {
errs = append(errs, err)
continue
}
// On error here, we always append unknown blob errors.
errs = append(errs, distribution.ErrManifestBlobUnknown{Digest: fsLayer.Digest})
}
}
if len(errs) > 0 {
return errs
}
return nil
}
func (h *manifestSchema2Handler) Digest() (digest.Digest, error) {
_, p, err := h.manifest.Payload()
if err != nil {
return "", err
}
return digest.FromBytes(p), nil
}