-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathmanifestschema2handler.go
145 lines (119 loc) · 3.91 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package server
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"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/apis/image"
imageapiv1 "github.com/openshift/origin/pkg/image/apis/image/v1"
)
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
}
if !reflect.DeepEqual(deserializedManifest.Versioned, schema2.SchemaVersion) {
return nil, fmt.Errorf("unexpected manifest schema version=%d, mediaType=%q",
deserializedManifest.SchemaVersion,
deserializedManifest.MediaType)
}
return &deserializedManifest, nil
}
type manifestSchema2Handler struct {
blobStore distribution.BlobStore
manifest *schema2.DeserializedManifest
cachedConfig []byte
}
var _ ManifestHandler = &manifestSchema2Handler{}
func (h *manifestSchema2Handler) Config(ctx context.Context) ([]byte, error) {
if h.cachedConfig == nil {
blob, err := h.blobStore.Get(ctx, h.manifest.Config.Digest)
if err != nil {
context.GetLogger(ctx).Errorf("failed to get manifest config: %v", err)
return nil, err
}
h.cachedConfig = blob
}
return h.cachedConfig, nil
}
func (h *manifestSchema2Handler) Digest() (digest.Digest, error) {
_, p, err := h.manifest.Payload()
if err != nil {
return "", err
}
return digest.FromBytes(p), nil
}
func (h *manifestSchema2Handler) Manifest() distribution.Manifest {
return h.manifest
}
func (h *manifestSchema2Handler) Layers(ctx context.Context) (string, []imageapiv1.ImageLayer, error) {
layers := make([]imageapiv1.ImageLayer, len(h.manifest.Layers))
for i, layer := range h.manifest.Layers {
layers[i].Name = layer.Digest.String()
layers[i].LayerSize = layer.Size
layers[i].MediaType = layer.MediaType
}
return imageapi.DockerImageLayersOrderAscending, layers, nil
}
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) verifyLayer(ctx context.Context, fsLayer distribution.Descriptor) error {
if fsLayer.MediaType == schema2.MediaTypeForeignLayer {
// Clients download this layer from an external URL, so do not check for
// its presense.
if len(fsLayer.URLs) == 0 {
return errMissingURL
}
return nil
}
if len(fsLayer.URLs) != 0 {
return errUnexpectedURL
}
desc, err := h.blobStore.Stat(ctx, fsLayer.Digest)
if err != nil {
return err
}
if fsLayer.Size != desc.Size {
return ErrManifestBlobBadSize{
Digest: fsLayer.Digest,
ActualSize: desc.Size,
SizeInManifest: fsLayer.Size,
}
}
return nil
}
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.
for _, fsLayer := range h.manifest.References() {
if err := h.verifyLayer(ctx, fsLayer); 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
}