2015-12-17 02:26:13 +01:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2017-08-12 00:31:16 +02:00
|
|
|
"context"
|
2015-12-17 02:26:13 +01:00
|
|
|
"fmt"
|
|
|
|
|
2020-08-24 13:18:39 +02:00
|
|
|
"github.com/distribution/distribution/v3"
|
|
|
|
dcontext "github.com/distribution/distribution/v3/context"
|
|
|
|
"github.com/distribution/distribution/v3/manifest/manifestlist"
|
2016-12-17 01:28:34 +01:00
|
|
|
"github.com/opencontainers/go-digest"
|
2015-12-17 02:26:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// manifestListHandler is a ManifestHandler that covers schema2 manifest lists.
|
|
|
|
type manifestListHandler struct {
|
2016-07-21 00:09:11 +02:00
|
|
|
repository distribution.Repository
|
|
|
|
blobStore distribution.BlobStore
|
2015-12-17 02:26:13 +01:00
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ ManifestHandler = &manifestListHandler{}
|
|
|
|
|
|
|
|
func (ms *manifestListHandler) Unmarshal(ctx context.Context, dgst digest.Digest, content []byte) (distribution.Manifest, error) {
|
2017-08-12 00:31:16 +02:00
|
|
|
dcontext.GetLogger(ms.ctx).Debug("(*manifestListHandler).Unmarshal")
|
2015-12-17 02:26:13 +01:00
|
|
|
|
2018-09-05 22:40:42 +02:00
|
|
|
m := &manifestlist.DeserializedManifestList{}
|
|
|
|
if err := m.UnmarshalJSON(content); err != nil {
|
2015-12-17 02:26:13 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-05 22:40:42 +02:00
|
|
|
return m, nil
|
2015-12-17 02:26:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *manifestListHandler) Put(ctx context.Context, manifestList distribution.Manifest, skipDependencyVerification bool) (digest.Digest, error) {
|
2017-08-12 00:31:16 +02:00
|
|
|
dcontext.GetLogger(ms.ctx).Debug("(*manifestListHandler).Put")
|
2015-12-17 02:26:13 +01:00
|
|
|
|
|
|
|
m, ok := manifestList.(*manifestlist.DeserializedManifestList)
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("wrong type put to manifestListHandler: %T", manifestList)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ms.verifyManifest(ms.ctx, *m, skipDependencyVerification); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
mt, payload, err := m.Payload()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
revision, err := ms.blobStore.Put(ctx, mt, payload)
|
|
|
|
if err != nil {
|
2017-08-12 00:31:16 +02:00
|
|
|
dcontext.GetLogger(ctx).Errorf("error putting payload into blobstore: %v", err)
|
2015-12-17 02:26:13 +01: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.
|
|
|
|
func (ms *manifestListHandler) verifyManifest(ctx context.Context, mnfst manifestlist.DeserializedManifestList, skipDependencyVerification bool) error {
|
|
|
|
var errs distribution.ErrManifestVerification
|
|
|
|
|
2018-07-24 00:03:17 +02:00
|
|
|
if mnfst.SchemaVersion != 2 {
|
|
|
|
return fmt.Errorf("unrecognized manifest list schema version %d", mnfst.SchemaVersion)
|
|
|
|
}
|
|
|
|
|
2015-12-17 02:26:13 +01:00
|
|
|
if !skipDependencyVerification {
|
|
|
|
// This manifest service is different from the blob service
|
|
|
|
// returned by Blob. It uses a linked blob store to ensure that
|
|
|
|
// only manifests are accessible.
|
2016-07-21 00:09:11 +02:00
|
|
|
|
2015-12-17 02:26:13 +01:00
|
|
|
manifestService, err := ms.repository.Manifests(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, manifestDescriptor := range mnfst.References() {
|
|
|
|
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})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(errs) != 0 {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|