-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathmanifestschema1handler.go
177 lines (147 loc) · 5.05 KB
/
manifestschema1handler.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package server
import (
"encoding/json"
"fmt"
"path"
"github.com/docker/distribution"
"github.com/docker/distribution/context"
"github.com/docker/distribution/digest"
"github.com/docker/distribution/manifest/schema1"
"github.com/docker/distribution/reference"
"github.com/docker/libtrust"
imageapi "github.com/openshift/origin/pkg/image/apis/image"
imageapiv1 "github.com/openshift/origin/pkg/image/apis/image/v1"
)
func unmarshalManifestSchema1(content []byte, signatures [][]byte) (distribution.Manifest, error) {
// prefer signatures from the manifest
if _, err := libtrust.ParsePrettySignature(content, "signatures"); err == nil {
sm := schema1.SignedManifest{Canonical: content}
if err = json.Unmarshal(content, &sm); err == nil {
return &sm, nil
}
}
jsig, err := libtrust.NewJSONSignature(content, signatures...)
if err != nil {
return nil, err
}
// Extract the pretty JWS
content, err = jsig.PrettySignature("signatures")
if err != nil {
return nil, err
}
var sm schema1.SignedManifest
if err = json.Unmarshal(content, &sm); err != nil {
return nil, err
}
return &sm, nil
}
type manifestSchema1Handler struct {
serverAddr string
blobStore distribution.BlobStore
manifest *schema1.SignedManifest
blobsCache map[digest.Digest]distribution.Descriptor
}
var _ ManifestHandler = &manifestSchema1Handler{}
func (h *manifestSchema1Handler) Config(ctx context.Context) ([]byte, error) {
return nil, nil
}
func (h *manifestSchema1Handler) Digest() (digest.Digest, error) {
return digest.FromBytes(h.manifest.Canonical), nil
}
func (h *manifestSchema1Handler) Manifest() distribution.Manifest {
return h.manifest
}
func (h *manifestSchema1Handler) statBlob(ctx context.Context, dgst digest.Digest) (distribution.Descriptor, error) {
desc, ok := h.blobsCache[dgst]
if ok {
return desc, nil
}
desc, err := h.blobStore.Stat(ctx, dgst)
if err != nil {
return desc, err
}
if h.blobsCache == nil {
h.blobsCache = make(map[digest.Digest]distribution.Descriptor)
}
h.blobsCache[dgst] = desc
return desc, nil
}
func (h *manifestSchema1Handler) Layers(ctx context.Context) (string, []imageapiv1.ImageLayer, error) {
layers := make([]imageapiv1.ImageLayer, len(h.manifest.FSLayers))
for i, fslayer := range h.manifest.FSLayers {
desc, err := h.statBlob(ctx, fslayer.BlobSum)
if err != nil {
return "", nil, err
}
// In a schema1 manifest the layers are ordered from the youngest to
// the oldest. But we want to have layers in different order.
revidx := (len(h.manifest.FSLayers) - 1) - i // n-1, n-2, ..., 1, 0
layers[revidx].Name = fslayer.BlobSum.String()
layers[revidx].LayerSize = desc.Size
layers[revidx].MediaType = schema1.MediaTypeManifestLayer
}
return imageapi.DockerImageLayersOrderAscending, layers, nil
}
func (h *manifestSchema1Handler) Payload() (mediaType string, payload []byte, canonical []byte, err error) {
mt, payload, err := h.manifest.Payload()
return mt, payload, h.manifest.Canonical, err
}
func (h *manifestSchema1Handler) Verify(ctx context.Context, skipDependencyVerification bool) error {
var errs distribution.ErrManifestVerification
// 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.
if len(path.Join(h.serverAddr, h.manifest.Name)) > reference.NameTotalLengthMax {
errs = append(errs,
distribution.ErrManifestNameInvalid{
Name: h.manifest.Name,
Reason: fmt.Errorf("<registry-host>/<manifest-name> must not be more than %d characters", reference.NameTotalLengthMax),
})
}
if !reference.NameRegexp.MatchString(h.manifest.Name) {
errs = append(errs,
distribution.ErrManifestNameInvalid{
Name: h.manifest.Name,
Reason: fmt.Errorf("invalid manifest name format"),
})
}
if len(h.manifest.History) != len(h.manifest.FSLayers) {
errs = append(errs, fmt.Errorf("mismatched history and fslayer cardinality %d != %d",
len(h.manifest.History), len(h.manifest.FSLayers)))
}
if _, err := schema1.Verify(h.manifest); err != nil {
switch err {
case libtrust.ErrMissingSignatureKey, libtrust.ErrInvalidJSONContent, libtrust.ErrMissingSignatureKey:
errs = append(errs, distribution.ErrManifestUnverified{})
default:
if err.Error() == "invalid signature" {
errs = append(errs, distribution.ErrManifestUnverified{})
} else {
errs = append(errs, err)
}
}
}
if skipDependencyVerification {
if len(errs) > 0 {
return errs
}
return nil
}
for _, fsLayer := range h.manifest.References() {
_, err := h.statBlob(ctx, fsLayer.Digest)
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
}