2015-12-15 02:19:34 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2016-05-14 21:49:08 +00:00
|
|
|
"errors"
|
2015-12-15 02:19:34 +00:00
|
|
|
"fmt"
|
2016-05-14 21:49:08 +00:00
|
|
|
"net/url"
|
2015-12-15 02:19:34 +00:00
|
|
|
|
|
|
|
"encoding/json"
|
2016-05-14 21:49:08 +00:00
|
|
|
|
2015-12-15 02:19:34 +00:00
|
|
|
"github.com/docker/distribution"
|
|
|
|
"github.com/docker/distribution/context"
|
|
|
|
"github.com/docker/distribution/digest"
|
|
|
|
"github.com/docker/distribution/manifest/schema2"
|
|
|
|
)
|
|
|
|
|
2016-05-14 21:49:08 +00:00
|
|
|
var (
|
|
|
|
errUnexpectedURL = errors.New("unexpected URL on layer")
|
|
|
|
errMissingURL = errors.New("missing URL on layer")
|
|
|
|
errInvalidURL = errors.New("invalid URL on layer")
|
|
|
|
)
|
|
|
|
|
2015-12-15 02:19:34 +00:00
|
|
|
//schema2ManifestHandler is a ManifestHandler that covers schema2 manifests.
|
|
|
|
type schema2ManifestHandler struct {
|
|
|
|
repository *repository
|
|
|
|
blobStore *linkedBlobStore
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ ManifestHandler = &schema2ManifestHandler{}
|
|
|
|
|
|
|
|
func (ms *schema2ManifestHandler) Unmarshal(ctx context.Context, dgst digest.Digest, content []byte) (distribution.Manifest, error) {
|
|
|
|
context.GetLogger(ms.ctx).Debug("(*schema2ManifestHandler).Unmarshal")
|
|
|
|
|
|
|
|
var m schema2.DeserializedManifest
|
|
|
|
if err := json.Unmarshal(content, &m); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *schema2ManifestHandler) Put(ctx context.Context, manifest distribution.Manifest, skipDependencyVerification bool) (digest.Digest, error) {
|
|
|
|
context.GetLogger(ms.ctx).Debug("(*schema2ManifestHandler).Put")
|
|
|
|
|
|
|
|
m, ok := manifest.(*schema2.DeserializedManifest)
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("non-schema2 manifest put to schema2ManifestHandler: %T", manifest)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
context.GetLogger(ctx).Errorf("error putting payload into blobstore: %v", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Link the revision into the repository.
|
|
|
|
if err := ms.blobStore.linkBlob(ctx, revision); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return revision.Digest, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// verifyManifest ensures that the manifest content is valid from the
|
2015-12-17 01:26:13 +00:00
|
|
|
// perspective of the registry. As a policy, the registry only tries to store
|
|
|
|
// valid content, leaving trust policies of that content up to consumers.
|
2015-12-15 02:19:34 +00:00
|
|
|
func (ms *schema2ManifestHandler) verifyManifest(ctx context.Context, mnfst schema2.DeserializedManifest, skipDependencyVerification bool) error {
|
|
|
|
var errs distribution.ErrManifestVerification
|
|
|
|
|
|
|
|
if !skipDependencyVerification {
|
|
|
|
target := mnfst.Target()
|
|
|
|
_, err := ms.repository.Blobs(ctx).Stat(ctx, target.Digest)
|
|
|
|
if err != nil {
|
|
|
|
if err != distribution.ErrBlobUnknown {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// On error here, we always append unknown blob errors.
|
|
|
|
errs = append(errs, distribution.ErrManifestBlobUnknown{Digest: target.Digest})
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fsLayer := range mnfst.References() {
|
2016-05-14 21:49:08 +00:00
|
|
|
var err error
|
|
|
|
if fsLayer.MediaType != schema2.MediaTypeForeignLayer {
|
|
|
|
if len(fsLayer.URLs) == 0 {
|
|
|
|
_, err = ms.repository.Blobs(ctx).Stat(ctx, fsLayer.Digest)
|
|
|
|
} else {
|
|
|
|
err = errUnexpectedURL
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Clients download this layer from an external URL, so do not check for
|
|
|
|
// its presense.
|
|
|
|
if len(fsLayer.URLs) == 0 {
|
|
|
|
err = errMissingURL
|
|
|
|
}
|
2016-07-08 22:44:52 +00:00
|
|
|
allow := ms.repository.manifestURLs.allow
|
|
|
|
deny := ms.repository.manifestURLs.deny
|
2016-05-14 21:49:08 +00:00
|
|
|
for _, u := range fsLayer.URLs {
|
|
|
|
var pu *url.URL
|
|
|
|
pu, err = url.Parse(u)
|
2016-07-08 22:44:52 +00:00
|
|
|
if err != nil || (pu.Scheme != "http" && pu.Scheme != "https") || pu.Fragment != "" || (allow != nil && !allow.MatchString(u)) || (deny != nil && deny.MatchString(u)) {
|
2016-05-14 21:49:08 +00:00
|
|
|
err = errInvalidURL
|
2016-07-11 19:13:42 +00:00
|
|
|
break
|
2016-05-14 21:49:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-15 02:19:34 +00:00
|
|
|
if err != nil {
|
|
|
|
if err != distribution.ErrBlobUnknown {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|