2016-01-19 22:26:15 +00:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-08-24 11:18:39 +00:00
|
|
|
"github.com/distribution/distribution/v3"
|
|
|
|
"github.com/distribution/distribution/v3/context"
|
|
|
|
"github.com/distribution/distribution/v3/manifest"
|
|
|
|
"github.com/distribution/distribution/v3/manifest/manifestlist"
|
|
|
|
"github.com/distribution/distribution/v3/manifest/schema1"
|
|
|
|
"github.com/distribution/distribution/v3/manifest/schema2"
|
2016-01-19 22:26:15 +00:00
|
|
|
"github.com/docker/libtrust"
|
2016-12-17 00:28:34 +00:00
|
|
|
"github.com/opencontainers/go-digest"
|
2016-01-19 22:26:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// MakeManifestList constructs a manifest list out of a list of manifest digests
|
|
|
|
func MakeManifestList(blobstatter distribution.BlobStatter, manifestDigests []digest.Digest) (*manifestlist.DeserializedManifestList, error) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
var manifestDescriptors []manifestlist.ManifestDescriptor
|
|
|
|
for _, manifestDigest := range manifestDigests {
|
|
|
|
descriptor, err := blobstatter.Stat(ctx, manifestDigest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
platformSpec := manifestlist.PlatformSpec{
|
|
|
|
Architecture: "atari2600",
|
|
|
|
OS: "CP/M",
|
|
|
|
Variant: "ternary",
|
|
|
|
Features: []string{"VLIW", "superscalaroutoforderdevnull"},
|
|
|
|
}
|
|
|
|
manifestDescriptor := manifestlist.ManifestDescriptor{
|
|
|
|
Descriptor: descriptor,
|
|
|
|
Platform: platformSpec,
|
|
|
|
}
|
|
|
|
manifestDescriptors = append(manifestDescriptors, manifestDescriptor)
|
|
|
|
}
|
|
|
|
|
|
|
|
return manifestlist.FromDescriptors(manifestDescriptors)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakeSchema1Manifest constructs a schema 1 manifest from a given list of digests and returns
|
2022-11-25 15:54:21 +00:00
|
|
|
// the digest of the manifest.
|
|
|
|
//
|
|
|
|
// Deprecated: Docker Image Manifest v2, Schema 1 is deprecated since 2015.
|
|
|
|
// Use Docker Image Manifest v2, Schema 2, or the OCI Image Specification.
|
2023-05-03 16:54:34 +00:00
|
|
|
func MakeSchema1Manifest(digests []digest.Digest) (*schema1.SignedManifest, error) {
|
2022-11-26 12:34:11 +00:00
|
|
|
mfst := schema1.Manifest{
|
2016-01-19 22:26:15 +00:00
|
|
|
Versioned: manifest.Versioned{
|
|
|
|
SchemaVersion: 1,
|
|
|
|
},
|
|
|
|
Name: "who",
|
|
|
|
Tag: "cares",
|
|
|
|
}
|
|
|
|
|
2022-11-26 12:34:11 +00:00
|
|
|
for _, d := range digests {
|
|
|
|
mfst.FSLayers = append(mfst.FSLayers, schema1.FSLayer{BlobSum: d})
|
|
|
|
mfst.History = append(mfst.History, schema1.History{V1Compatibility: ""})
|
2016-01-19 22:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pk, err := libtrust.GenerateECP256PrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unexpected error generating private key: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-11-26 12:34:11 +00:00
|
|
|
signedManifest, err := schema1.Sign(&mfst, pk)
|
2016-01-19 22:26:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error signing manifest: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return signedManifest, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakeSchema2Manifest constructs a schema 2 manifest from a given list of digests and returns
|
|
|
|
// the digest of the manifest
|
|
|
|
func MakeSchema2Manifest(repository distribution.Repository, digests []digest.Digest) (distribution.Manifest, error) {
|
|
|
|
ctx := context.Background()
|
|
|
|
blobStore := repository.Blobs(ctx)
|
2023-05-03 16:54:34 +00:00
|
|
|
|
|
|
|
var configJSON []byte
|
|
|
|
|
|
|
|
d, err := blobStore.Put(ctx, schema2.MediaTypeImageConfig, configJSON)
|
|
|
|
if err != nil {
|
2023-05-19 13:38:09 +00:00
|
|
|
return nil, fmt.Errorf("unexpected error storing content in blobstore: %v", err)
|
2023-05-03 16:54:34 +00:00
|
|
|
}
|
|
|
|
builder := schema2.NewManifestBuilder(d, configJSON)
|
|
|
|
for _, digest := range digests {
|
|
|
|
builder.AppendReference(distribution.Descriptor{Digest: digest})
|
2016-01-19 22:26:15 +00:00
|
|
|
}
|
|
|
|
|
2022-11-26 12:34:11 +00:00
|
|
|
mfst, err := builder.Build(ctx)
|
2016-01-19 22:26:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unexpected error generating manifest: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-11-26 12:34:11 +00:00
|
|
|
return mfst, nil
|
2016-01-19 22:26:15 +00:00
|
|
|
}
|