2015-12-17 01:26:13 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2017-08-11 22:31:16 +00:00
|
|
|
"context"
|
2015-12-17 01:26:13 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-08-24 11:18:39 +00:00
|
|
|
"github.com/distribution/distribution/v3"
|
2023-10-24 17:16:58 +00:00
|
|
|
"github.com/distribution/distribution/v3/internal/dcontext"
|
2020-08-24 11:18:39 +00:00
|
|
|
"github.com/distribution/distribution/v3/manifest/manifestlist"
|
2023-03-31 10:35:30 +00:00
|
|
|
"github.com/distribution/distribution/v3/manifest/ocischema"
|
2016-12-17 00:28:34 +00:00
|
|
|
"github.com/opencontainers/go-digest"
|
2015-12-17 01:26:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// manifestListHandler is a ManifestHandler that covers schema2 manifest lists.
|
|
|
|
type manifestListHandler struct {
|
2023-08-15 13:37:43 +00:00
|
|
|
repository distribution.Repository
|
|
|
|
blobStore distribution.BlobStore
|
|
|
|
ctx context.Context
|
|
|
|
validateImageIndexes validateImageIndexes
|
2015-12-17 01:26:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ ManifestHandler = &manifestListHandler{}
|
|
|
|
|
|
|
|
func (ms *manifestListHandler) Unmarshal(ctx context.Context, dgst digest.Digest, content []byte) (distribution.Manifest, error) {
|
2017-08-11 22:31:16 +00:00
|
|
|
dcontext.GetLogger(ms.ctx).Debug("(*manifestListHandler).Unmarshal")
|
2015-12-17 01:26:13 +00:00
|
|
|
|
2018-09-05 20:40:42 +00:00
|
|
|
m := &manifestlist.DeserializedManifestList{}
|
|
|
|
if err := m.UnmarshalJSON(content); err != nil {
|
2015-12-17 01:26:13 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-05 20:40:42 +00:00
|
|
|
return m, nil
|
2015-12-17 01:26:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *manifestListHandler) Put(ctx context.Context, manifestList distribution.Manifest, skipDependencyVerification bool) (digest.Digest, error) {
|
2017-08-11 22:31:16 +00:00
|
|
|
dcontext.GetLogger(ms.ctx).Debug("(*manifestListHandler).Put")
|
2015-12-17 01:26:13 +00:00
|
|
|
|
2023-04-30 16:16:51 +00:00
|
|
|
var schemaVersion int
|
2023-03-31 10:35:30 +00:00
|
|
|
switch m := manifestList.(type) {
|
|
|
|
case *manifestlist.DeserializedManifestList:
|
|
|
|
schemaVersion = m.SchemaVersion
|
|
|
|
case *ocischema.DeserializedImageIndex:
|
|
|
|
schemaVersion = m.SchemaVersion
|
|
|
|
default:
|
2015-12-17 01:26:13 +00:00
|
|
|
return "", fmt.Errorf("wrong type put to manifestListHandler: %T", manifestList)
|
|
|
|
}
|
2023-04-30 16:16:51 +00:00
|
|
|
|
|
|
|
const expectedSchemaVersion = 2
|
2023-04-21 13:43:21 +00:00
|
|
|
if schemaVersion != expectedSchemaVersion {
|
|
|
|
return "", fmt.Errorf("unrecognized manifest list schema version %d, expected %d", schemaVersion, expectedSchemaVersion)
|
2023-03-31 10:35:30 +00:00
|
|
|
}
|
2015-12-17 01:26:13 +00:00
|
|
|
|
2023-03-31 10:35:30 +00:00
|
|
|
if err := ms.verifyManifest(ms.ctx, manifestList, skipDependencyVerification); err != nil {
|
2015-12-17 01:26:13 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-03-31 10:35:30 +00:00
|
|
|
mt, payload, err := manifestList.Payload()
|
2015-12-17 01:26:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
revision, err := ms.blobStore.Put(ctx, mt, payload)
|
|
|
|
if err != nil {
|
2017-08-11 22:31:16 +00:00
|
|
|
dcontext.GetLogger(ctx).Errorf("error putting payload into blobstore: %v", err)
|
2015-12-17 01:26:13 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return revision.Digest, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// verifyManifest ensures that the manifest content is valid from the
|
|
|
|
// perspective of the registry. As a policy, the registry only tries to
|
|
|
|
// store valid content, leaving trust policies of that content up to
|
|
|
|
// consumers.
|
2023-03-31 10:35:30 +00:00
|
|
|
func (ms *manifestListHandler) verifyManifest(ctx context.Context, mnfst distribution.Manifest, skipDependencyVerification bool) error {
|
2015-12-17 01:26:13 +00:00
|
|
|
var errs distribution.ErrManifestVerification
|
|
|
|
|
2023-08-15 13:37:43 +00:00
|
|
|
// Check if we should be validating the existence of any child images in images indexes
|
|
|
|
if ms.validateImageIndexes.imagesExist && !skipDependencyVerification {
|
|
|
|
// Get the manifest service we can use to check for the existence of child images
|
2015-12-17 01:26:13 +00:00
|
|
|
manifestService, err := ms.repository.Manifests(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, manifestDescriptor := range mnfst.References() {
|
2023-08-15 13:37:43 +00:00
|
|
|
if ms.platformMustExist(manifestDescriptor) {
|
|
|
|
exists, err := manifestService.Exists(ctx, manifestDescriptor.Digest)
|
|
|
|
if err != nil && err != distribution.ErrBlobUnknown {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
if err != nil || !exists {
|
|
|
|
// On error here, we always append unknown blob errors.
|
|
|
|
errs = append(errs, distribution.ErrManifestBlobUnknown{Digest: manifestDescriptor.Digest})
|
|
|
|
}
|
2015-12-17 01:26:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(errs) != 0 {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-08-15 13:37:43 +00:00
|
|
|
|
|
|
|
// platformMustExist checks if a descriptor within an index should be validated as existing before accepting the manifest into the registry.
|
|
|
|
func (ms *manifestListHandler) platformMustExist(descriptor distribution.Descriptor) bool {
|
|
|
|
// If there are no image platforms configured to validate, we must check the existence of all child images.
|
|
|
|
if len(ms.validateImageIndexes.imagePlatforms) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
imagePlatform := descriptor.Platform
|
|
|
|
|
|
|
|
// If the platform matches a platform that is configured to validate, we must check the existence.
|
|
|
|
for _, platform := range ms.validateImageIndexes.imagePlatforms {
|
|
|
|
if imagePlatform.Architecture == platform.architecture &&
|
|
|
|
imagePlatform.OS == platform.os {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the platform doesn't match a platform configured to validate, we don't need to check the existence.
|
|
|
|
return false
|
|
|
|
}
|