forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifests.go
350 lines (297 loc) · 10.3 KB
/
manifests.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package testutil
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"reflect"
"testing"
"github.com/docker/distribution"
"github.com/docker/distribution/context"
"github.com/docker/distribution/digest"
"github.com/docker/distribution/manifest"
"github.com/docker/distribution/manifest/schema1"
"github.com/docker/distribution/manifest/schema2"
"github.com/docker/distribution/reference"
distclient "github.com/docker/distribution/registry/client"
"github.com/docker/distribution/registry/client/auth"
"github.com/docker/distribution/registry/client/transport"
"github.com/docker/libtrust"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/diff"
imageapi "github.com/openshift/origin/pkg/image/apis/image"
)
type ManifestSchemaVersion int
type LayerPayload []byte
type ConfigPayload []byte
type Payload struct {
Config ConfigPayload
Layers []LayerPayload
}
const (
ManifestSchema1 ManifestSchemaVersion = 1
ManifestSchema2 ManifestSchemaVersion = 2
)
// MakeSchema1Manifest constructs a schema 1 manifest from a given list of digests and returns
// the digest of the manifest
// github.com/docker/distribution/testutil
func MakeSchema1Manifest(name, tag string, layers []distribution.Descriptor) (string, distribution.Manifest, error) {
m := schema1.Manifest{
Versioned: manifest.Versioned{
SchemaVersion: 1,
},
FSLayers: make([]schema1.FSLayer, 0, len(layers)),
History: make([]schema1.History, 0, len(layers)),
Name: name,
Tag: tag,
}
for _, layer := range layers {
m.FSLayers = append(m.FSLayers, schema1.FSLayer{BlobSum: layer.Digest})
m.History = append(m.History, schema1.History{V1Compatibility: "{}"})
}
pk, err := libtrust.GenerateECP256PrivateKey()
if err != nil {
return "", nil, fmt.Errorf("unexpected error generating private key: %v", err)
}
signedManifest, err := schema1.Sign(&m, pk)
if err != nil {
return "", nil, fmt.Errorf("error signing manifest: %v", err)
}
return string(signedManifest.Canonical), signedManifest, nil
}
// MakeSchema2Manifest constructs a schema 2 manifest from a given list of digests and returns
// the digest of the manifest
func MakeSchema2Manifest(config distribution.Descriptor, layers []distribution.Descriptor) (string, distribution.Manifest, error) {
m := schema2.Manifest{
Versioned: schema2.SchemaVersion,
Config: config,
Layers: make([]distribution.Descriptor, 0, len(layers)),
}
m.Config.MediaType = schema2.MediaTypeConfig
for _, layer := range layers {
layer.MediaType = schema2.MediaTypeLayer
m.Layers = append(m.Layers, layer)
}
manifest, err := schema2.FromStruct(m)
if err != nil {
return "", nil, err
}
_, payload, err := manifest.Payload()
if err != nil {
return "", nil, err
}
return string(payload), manifest, nil
}
func MakeRandomLayers(layerCount int) ([]distribution.Descriptor, []LayerPayload, error) {
var (
layers []distribution.Descriptor
payloads []LayerPayload
)
for i := 0; i < layerCount; i++ {
content, err := CreateRandomTarFile()
if err != nil {
return layers, payloads, fmt.Errorf("unexpected error generating test layer file: %v", err)
}
layers = append(layers, distribution.Descriptor{
Digest: digest.FromBytes(content),
Size: int64(len(content)),
})
payloads = append(payloads, LayerPayload(content))
}
return layers, payloads, nil
}
func MakeManifestConfig() (ConfigPayload, distribution.Descriptor, error) {
cfg := imageapi.DockerImageConfig{}
cfgDesc := distribution.Descriptor{}
jsonBytes, err := json.Marshal(&cfg)
if err != nil {
return nil, cfgDesc, err
}
cfgDesc.Digest = digest.FromBytes(jsonBytes)
cfgDesc.Size = int64(len(jsonBytes))
return jsonBytes, cfgDesc, nil
}
func CreateRandomManifest(schemaVersion ManifestSchemaVersion, layerCount int) (string, distribution.Manifest, *Payload, error) {
var (
rawManifest string
manifest distribution.Manifest
cfgDesc distribution.Descriptor
err error
)
layersDescs, layerPayloads, err := MakeRandomLayers(layerCount)
if err != nil {
return "", nil, nil, fmt.Errorf("cannot generate layers: %v", err)
}
payload := &Payload{
Layers: layerPayloads,
}
switch schemaVersion {
case ManifestSchema1:
rawManifest, manifest, err = MakeSchema1Manifest("who", "cares", layersDescs)
case ManifestSchema2:
_, cfgDesc, err = MakeManifestConfig()
if err != nil {
return "", nil, nil, err
}
rawManifest, manifest, err = MakeSchema2Manifest(cfgDesc, layersDescs)
default:
return "", nil, nil, fmt.Errorf("unsupported manifest version %d", schemaVersion)
}
return rawManifest, manifest, payload, err
}
// CreateUploadTestManifest generates a random manifest blob and uploads it to the given repository. For this
// purpose, a given number of layers will be created and uploaded.
func CreateAndUploadTestManifest(
schemaVersion ManifestSchemaVersion,
layerCount int,
serverURL *url.URL,
creds auth.CredentialStore,
repoName, tag string,
) (dgst digest.Digest, canonical, manifestConfig string, manifest distribution.Manifest, err error) {
var (
layerDescriptors = make([]distribution.Descriptor, 0, layerCount)
)
for i := 0; i < layerCount; i++ {
ds, _, err := UploadRandomTestBlob(serverURL, creds, repoName)
if err != nil {
return "", "", "", nil, fmt.Errorf("unexpected error generating test blob layer: %v", err)
}
layerDescriptors = append(layerDescriptors, ds)
}
switch schemaVersion {
case ManifestSchema1:
canonical, manifest, err = MakeSchema1Manifest(repoName, tag, layerDescriptors)
if err != nil {
return "", "", "", nil, fmt.Errorf("failed to make manifest of schema 1: %v", err)
}
case ManifestSchema2:
cfgPayload, cfgDesc, err := MakeManifestConfig()
if err != nil {
return "", "", "", nil, err
}
_, err = UploadBlob(cfgPayload, serverURL, creds, repoName)
if err != nil {
return "", "", "", nil, fmt.Errorf("failed to upload manifest config of schema 2: %v", err)
}
canonical, manifest, err = MakeSchema2Manifest(cfgDesc, layerDescriptors)
if err != nil {
return "", "", "", nil, fmt.Errorf("failed to make manifest schema 2: %v", err)
}
manifestConfig = string(cfgPayload)
default:
return "", "", "", nil, fmt.Errorf("unsupported manifest version %d", schemaVersion)
}
expectedDgst := digest.FromBytes([]byte(canonical))
ctx := context.Background()
ref, err := reference.ParseNamed(repoName)
if err != nil {
return "", "", "", nil, err
}
var rt http.RoundTripper
if creds != nil {
challengeManager := auth.NewSimpleChallengeManager()
_, err := ping(challengeManager, serverURL.String()+"/v2/", "")
if err != nil {
return "", "", "", nil, err
}
rt = transport.NewTransport(
nil,
auth.NewAuthorizer(
challengeManager,
auth.NewTokenHandler(nil, creds, repoName, "pull", "push"),
auth.NewBasicHandler(creds)))
}
repo, err := distclient.NewRepository(ctx, ref, serverURL.String(), rt)
if err != nil {
return "", "", "", nil, fmt.Errorf("failed to get repository %q: %v", repoName, err)
}
ms, err := repo.Manifests(ctx)
if err != nil {
return "", "", "", nil, err
}
dgst, err = ms.Put(ctx, manifest)
if err != nil {
return "", "", "", nil, err
}
if expectedDgst != dgst {
return "", "", "", nil, fmt.Errorf("registry server computed different digest for uploaded manifest than expected: %q != %q", dgst, expectedDgst)
}
return dgst, canonical, manifestConfig, manifest, nil
}
// AssertManifestsEqual compares two manifests and returns if they are equal. Signatures of manifest schema 1
// are not taken into account.
func AssertManifestsEqual(t *testing.T, description string, ma distribution.Manifest, mb distribution.Manifest) {
if ma == mb {
return
}
if (ma == nil) != (mb == nil) {
t.Fatalf("[%s] only one of the manifests is nil", description)
}
_, pa, err := ma.Payload()
if err != nil {
t.Fatalf("[%s] failed to get payload for first manifest: %v", description, err)
}
_, pb, err := mb.Payload()
if err != nil {
t.Fatalf("[%s] failed to get payload for second manifest: %v", description, err)
}
var va, vb manifest.Versioned
if err := json.Unmarshal([]byte(pa), &va); err != nil {
t.Fatalf("[%s] failed to unmarshal payload of the first manifest: %v", description, err)
}
if err := json.Unmarshal([]byte(pb), &vb); err != nil {
t.Fatalf("[%s] failed to unmarshal payload of the second manifest: %v", description, err)
}
if !reflect.DeepEqual(va, vb) {
t.Fatalf("[%s] manifests are of different version: %s", description, diff.ObjectGoPrintDiff(va, vb))
}
switch va.SchemaVersion {
case 1:
ms1a, ok := ma.(*schema1.SignedManifest)
if !ok {
t.Fatalf("[%s] failed to convert first manifest (%T) to schema1.SignedManifest", description, ma)
}
ms1b, ok := mb.(*schema1.SignedManifest)
if !ok {
t.Fatalf("[%s] failed to convert first manifest (%T) to schema1.SignedManifest", description, mb)
}
if !reflect.DeepEqual(ms1a.Manifest, ms1b.Manifest) {
t.Fatalf("[%s] manifests don't match: %s", description, diff.ObjectGoPrintDiff(ms1a.Manifest, ms1b.Manifest))
}
case 2:
if !reflect.DeepEqual(ma, mb) {
t.Fatalf("[%s] manifests don't match: %s", description, diff.ObjectGoPrintDiff(ma, mb))
}
default:
t.Fatalf("[%s] unrecognized manifest schema version: %d", description, va.SchemaVersion)
}
}
// NewImageManifest creates a new Image object for the given manifest string. Note that the manifest must
// contain signatures if it is of schema 1.
func NewImageForManifest(repoName string, rawManifest string, manifestConfig string, managedByOpenShift bool) (*imageapi.Image, error) {
var versioned manifest.Versioned
if err := json.Unmarshal([]byte(rawManifest), &versioned); err != nil {
return nil, err
}
_, desc, err := distribution.UnmarshalManifest(versioned.MediaType, []byte(rawManifest))
if err != nil {
return nil, err
}
annotations := make(map[string]string)
if managedByOpenShift {
annotations[imageapi.ManagedByOpenShiftAnnotation] = "true"
}
img := &imageapi.Image{
ObjectMeta: metav1.ObjectMeta{
Name: desc.Digest.String(),
Annotations: annotations,
},
DockerImageReference: fmt.Sprintf("localhost:5000/%s@%s", repoName, desc.Digest.String()),
DockerImageManifest: rawManifest,
DockerImageConfig: manifestConfig,
}
if err := imageapi.ImageWithMetadata(img); err != nil {
return nil, fmt.Errorf("failed to fill image with metadata: %v", err)
}
return img, nil
}