2015-02-11 01:25:40 +00:00
|
|
|
package handlers
|
2014-11-11 02:57:38 +00:00
|
|
|
|
|
|
|
import (
|
2015-07-30 01:18:50 +00:00
|
|
|
"bytes"
|
Initial implementation of Manifest HTTP API
Push, pull and delete of manifest files in the registry have been implemented
on top of the storage services. Basic workflows, including reporting of missing
manifests are tested, including various proposed response codes. Common testing
functionality has been collected into shared methods. A test suite may be
emerging but it might better to capture more edge cases (such as resumable
upload, range requests, etc.) before we commit to a full approach.
To support clearer test cases and simpler handler methods, an application aware
urlBuilder has been added. We may want to export the functionality for use in
the client, which could allow us to abstract away from gorilla/mux.
A few error codes have been added to fill in error conditions missing from the
proposal. Some use cases have identified some problems with the approach to
error reporting that requires more work to reconcile. To resolve this, the
mapping of Go errors into error types needs to pulled out of the handlers and
into the application. We also need to move to type-based errors, with rich
information, rather than value-based errors. ErrorHandlers will probably
replace the http.Handlers to make this work correctly.
Unrelated to the above, the "length" parameter has been migrated to "size" for
completing layer uploads. This change should have gone out before but these
diffs ending up being coupled with the parameter name change due to updates to
the layer unit tests.
2014-11-26 20:16:58 +00:00
|
|
|
"fmt"
|
2014-11-11 02:57:38 +00:00
|
|
|
"net/http"
|
2016-06-10 23:34:08 +00:00
|
|
|
"strings"
|
2014-11-11 02:57:38 +00:00
|
|
|
|
2015-02-12 00:49:49 +00:00
|
|
|
"github.com/docker/distribution"
|
2015-02-07 00:19:19 +00:00
|
|
|
ctxu "github.com/docker/distribution/context"
|
2014-12-24 00:01:38 +00:00
|
|
|
"github.com/docker/distribution/digest"
|
2015-12-18 01:32:11 +00:00
|
|
|
"github.com/docker/distribution/manifest/manifestlist"
|
2015-12-15 19:37:28 +00:00
|
|
|
"github.com/docker/distribution/manifest/schema1"
|
|
|
|
"github.com/docker/distribution/manifest/schema2"
|
2015-12-15 22:35:23 +00:00
|
|
|
"github.com/docker/distribution/reference"
|
2015-05-27 00:18:32 +00:00
|
|
|
"github.com/docker/distribution/registry/api/errcode"
|
2015-02-11 02:18:45 +00:00
|
|
|
"github.com/docker/distribution/registry/api/v2"
|
2016-11-22 00:36:36 +00:00
|
|
|
"github.com/docker/distribution/registry/auth"
|
2014-11-11 02:57:38 +00:00
|
|
|
"github.com/gorilla/handlers"
|
|
|
|
)
|
|
|
|
|
2015-12-18 01:32:11 +00:00
|
|
|
// These constants determine which architecture and OS to choose from a
|
|
|
|
// manifest list when downconverting it to a schema1 manifest.
|
|
|
|
const (
|
|
|
|
defaultArch = "amd64"
|
|
|
|
defaultOS = "linux"
|
|
|
|
)
|
|
|
|
|
2014-11-11 02:57:38 +00:00
|
|
|
// imageManifestDispatcher takes the request context and builds the
|
|
|
|
// appropriate handler for handling image manifest requests.
|
|
|
|
func imageManifestDispatcher(ctx *Context, r *http.Request) http.Handler {
|
|
|
|
imageManifestHandler := &imageManifestHandler{
|
|
|
|
Context: ctx,
|
2015-02-26 23:47:04 +00:00
|
|
|
}
|
|
|
|
reference := getReference(ctx)
|
|
|
|
dgst, err := digest.ParseDigest(reference)
|
|
|
|
if err != nil {
|
|
|
|
// We just have a tag
|
|
|
|
imageManifestHandler.Tag = reference
|
|
|
|
} else {
|
|
|
|
imageManifestHandler.Digest = dgst
|
2014-11-11 02:57:38 +00:00
|
|
|
}
|
|
|
|
|
2015-08-07 01:02:43 +00:00
|
|
|
mhandler := handlers.MethodHandler{
|
2015-08-21 04:50:15 +00:00
|
|
|
"GET": http.HandlerFunc(imageManifestHandler.GetImageManifest),
|
|
|
|
"HEAD": http.HandlerFunc(imageManifestHandler.GetImageManifest),
|
2014-11-11 02:57:38 +00:00
|
|
|
}
|
2015-08-07 01:02:43 +00:00
|
|
|
|
|
|
|
if !ctx.readOnly {
|
|
|
|
mhandler["PUT"] = http.HandlerFunc(imageManifestHandler.PutImageManifest)
|
|
|
|
mhandler["DELETE"] = http.HandlerFunc(imageManifestHandler.DeleteImageManifest)
|
|
|
|
}
|
|
|
|
|
|
|
|
return mhandler
|
2014-11-11 02:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// imageManifestHandler handles http operations on image manifests.
|
|
|
|
type imageManifestHandler struct {
|
|
|
|
*Context
|
|
|
|
|
2015-02-26 23:47:04 +00:00
|
|
|
// One of tag or digest gets set, depending on what is present in context.
|
|
|
|
Tag string
|
|
|
|
Digest digest.Digest
|
2014-11-11 02:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetImageManifest fetches the image manifest from the storage backend, if it exists.
|
|
|
|
func (imh *imageManifestHandler) GetImageManifest(w http.ResponseWriter, r *http.Request) {
|
2015-02-07 00:19:19 +00:00
|
|
|
ctxu.GetLogger(imh).Debug("GetImageManifest")
|
2015-06-15 17:39:34 +00:00
|
|
|
manifests, err := imh.Repository.Manifests(imh)
|
|
|
|
if err != nil {
|
|
|
|
imh.Errors = append(imh.Errors, err)
|
|
|
|
return
|
|
|
|
}
|
2015-02-26 23:47:04 +00:00
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
var manifest distribution.Manifest
|
2015-02-26 23:47:04 +00:00
|
|
|
if imh.Tag != "" {
|
2015-08-21 04:50:15 +00:00
|
|
|
tags := imh.Repository.Tags(imh)
|
|
|
|
desc, err := tags.Get(imh, imh.Tag)
|
|
|
|
if err != nil {
|
|
|
|
imh.Errors = append(imh.Errors, v2.ErrorCodeManifestUnknown.WithDetail(err))
|
2015-06-18 23:56:05 +00:00
|
|
|
return
|
|
|
|
}
|
2015-08-21 04:50:15 +00:00
|
|
|
imh.Digest = desc.Digest
|
|
|
|
}
|
|
|
|
|
|
|
|
if etagMatch(r, imh.Digest.String()) {
|
|
|
|
w.WriteHeader(http.StatusNotModified)
|
|
|
|
return
|
2015-02-26 23:47:04 +00:00
|
|
|
}
|
Initial implementation of Manifest HTTP API
Push, pull and delete of manifest files in the registry have been implemented
on top of the storage services. Basic workflows, including reporting of missing
manifests are tested, including various proposed response codes. Common testing
functionality has been collected into shared methods. A test suite may be
emerging but it might better to capture more edge cases (such as resumable
upload, range requests, etc.) before we commit to a full approach.
To support clearer test cases and simpler handler methods, an application aware
urlBuilder has been added. We may want to export the functionality for use in
the client, which could allow us to abstract away from gorilla/mux.
A few error codes have been added to fill in error conditions missing from the
proposal. Some use cases have identified some problems with the approach to
error reporting that requires more work to reconcile. To resolve this, the
mapping of Go errors into error types needs to pulled out of the handlers and
into the application. We also need to move to type-based errors, with rich
information, rather than value-based errors. ErrorHandlers will probably
replace the http.Handlers to make this work correctly.
Unrelated to the above, the "length" parameter has been migrated to "size" for
completing layer uploads. This change should have gone out before but these
diffs ending up being coupled with the parameter name change due to updates to
the layer unit tests.
2014-11-26 20:16:58 +00:00
|
|
|
|
2016-03-18 22:30:47 +00:00
|
|
|
var options []distribution.ManifestServiceOption
|
|
|
|
if imh.Tag != "" {
|
|
|
|
options = append(options, distribution.WithTag(imh.Tag))
|
|
|
|
}
|
|
|
|
manifest, err = manifests.Get(imh, imh.Digest, options...)
|
Initial implementation of Manifest HTTP API
Push, pull and delete of manifest files in the registry have been implemented
on top of the storage services. Basic workflows, including reporting of missing
manifests are tested, including various proposed response codes. Common testing
functionality has been collected into shared methods. A test suite may be
emerging but it might better to capture more edge cases (such as resumable
upload, range requests, etc.) before we commit to a full approach.
To support clearer test cases and simpler handler methods, an application aware
urlBuilder has been added. We may want to export the functionality for use in
the client, which could allow us to abstract away from gorilla/mux.
A few error codes have been added to fill in error conditions missing from the
proposal. Some use cases have identified some problems with the approach to
error reporting that requires more work to reconcile. To resolve this, the
mapping of Go errors into error types needs to pulled out of the handlers and
into the application. We also need to move to type-based errors, with rich
information, rather than value-based errors. ErrorHandlers will probably
replace the http.Handlers to make this work correctly.
Unrelated to the above, the "length" parameter has been migrated to "size" for
completing layer uploads. This change should have gone out before but these
diffs ending up being coupled with the parameter name change due to updates to
the layer unit tests.
2014-11-26 20:16:58 +00:00
|
|
|
if err != nil {
|
2015-06-03 13:52:39 +00:00
|
|
|
imh.Errors = append(imh.Errors, v2.ErrorCodeManifestUnknown.WithDetail(err))
|
Initial implementation of Manifest HTTP API
Push, pull and delete of manifest files in the registry have been implemented
on top of the storage services. Basic workflows, including reporting of missing
manifests are tested, including various proposed response codes. Common testing
functionality has been collected into shared methods. A test suite may be
emerging but it might better to capture more edge cases (such as resumable
upload, range requests, etc.) before we commit to a full approach.
To support clearer test cases and simpler handler methods, an application aware
urlBuilder has been added. We may want to export the functionality for use in
the client, which could allow us to abstract away from gorilla/mux.
A few error codes have been added to fill in error conditions missing from the
proposal. Some use cases have identified some problems with the approach to
error reporting that requires more work to reconcile. To resolve this, the
mapping of Go errors into error types needs to pulled out of the handlers and
into the application. We also need to move to type-based errors, with rich
information, rather than value-based errors. ErrorHandlers will probably
replace the http.Handlers to make this work correctly.
Unrelated to the above, the "length" parameter has been migrated to "size" for
completing layer uploads. This change should have gone out before but these
diffs ending up being coupled with the parameter name change due to updates to
the layer unit tests.
2014-11-26 20:16:58 +00:00
|
|
|
return
|
|
|
|
}
|
2014-11-11 02:57:38 +00:00
|
|
|
|
2015-12-18 01:32:11 +00:00
|
|
|
supportsSchema2 := false
|
|
|
|
supportsManifestList := false
|
2016-06-10 23:34:08 +00:00
|
|
|
// this parsing of Accept headers is not quite as full-featured as godoc.org's parser, but we don't care about "q=" values
|
|
|
|
// https://github.com/golang/gddo/blob/e91d4165076d7474d20abda83f92d15c7ebc3e81/httputil/header/header.go#L165-L202
|
|
|
|
for _, acceptHeader := range r.Header["Accept"] {
|
|
|
|
// r.Header[...] is a slice in case the request contains the same header more than once
|
|
|
|
// if the header isn't set, we'll get the zero value, which "range" will handle gracefully
|
|
|
|
|
|
|
|
// we need to split each header value on "," to get the full list of "Accept" values (per RFC 2616)
|
|
|
|
// https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
|
|
|
|
for _, mediaType := range strings.Split(acceptHeader, ",") {
|
|
|
|
// remove "; q=..." if present
|
|
|
|
if i := strings.Index(mediaType, ";"); i >= 0 {
|
|
|
|
mediaType = mediaType[:i]
|
|
|
|
}
|
|
|
|
|
|
|
|
// it's common (but not required) for Accept values to be space separated ("a/b, c/d, e/f")
|
|
|
|
mediaType = strings.TrimSpace(mediaType)
|
|
|
|
|
2015-12-18 01:32:11 +00:00
|
|
|
if mediaType == schema2.MediaTypeManifest {
|
|
|
|
supportsSchema2 = true
|
|
|
|
}
|
|
|
|
if mediaType == manifestlist.MediaTypeManifestList {
|
|
|
|
supportsManifestList = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
schema2Manifest, isSchema2 := manifest.(*schema2.DeserializedManifest)
|
|
|
|
manifestList, isManifestList := manifest.(*manifestlist.DeserializedManifestList)
|
|
|
|
|
2015-12-15 19:37:28 +00:00
|
|
|
// Only rewrite schema2 manifests when they are being fetched by tag.
|
|
|
|
// If they are being fetched by digest, we can't return something not
|
|
|
|
// matching the digest.
|
2015-12-18 01:32:11 +00:00
|
|
|
if imh.Tag != "" && isSchema2 && !supportsSchema2 {
|
|
|
|
// Rewrite manifest in schema1 format
|
|
|
|
ctxu.GetLogger(imh).Infof("rewriting manifest %s in schema1 format to support old client", imh.Digest.String())
|
|
|
|
|
|
|
|
manifest, err = imh.convertSchema2Manifest(schema2Manifest)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else if imh.Tag != "" && isManifestList && !supportsManifestList {
|
|
|
|
// Rewrite manifest in schema1 format
|
|
|
|
ctxu.GetLogger(imh).Infof("rewriting manifest list %s in schema1 format to support old client", imh.Digest.String())
|
|
|
|
|
|
|
|
// Find the image manifest corresponding to the default
|
|
|
|
// platform
|
|
|
|
var manifestDigest digest.Digest
|
|
|
|
for _, manifestDescriptor := range manifestList.Manifests {
|
|
|
|
if manifestDescriptor.Platform.Architecture == defaultArch && manifestDescriptor.Platform.OS == defaultOS {
|
|
|
|
manifestDigest = manifestDescriptor.Digest
|
|
|
|
break
|
2015-12-15 19:37:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-18 01:32:11 +00:00
|
|
|
if manifestDigest == "" {
|
|
|
|
imh.Errors = append(imh.Errors, v2.ErrorCodeManifestUnknown)
|
|
|
|
return
|
|
|
|
}
|
2015-12-15 19:37:28 +00:00
|
|
|
|
2015-12-18 01:32:11 +00:00
|
|
|
manifest, err = manifests.Get(imh, manifestDigest)
|
|
|
|
if err != nil {
|
|
|
|
imh.Errors = append(imh.Errors, v2.ErrorCodeManifestUnknown.WithDetail(err))
|
|
|
|
return
|
|
|
|
}
|
2015-12-15 19:37:28 +00:00
|
|
|
|
2015-12-18 01:32:11 +00:00
|
|
|
// If necessary, convert the image manifest
|
|
|
|
if schema2Manifest, isSchema2 := manifest.(*schema2.DeserializedManifest); isSchema2 && !supportsSchema2 {
|
|
|
|
manifest, err = imh.convertSchema2Manifest(schema2Manifest)
|
2015-12-15 19:37:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
ct, p, err := manifest.Payload()
|
|
|
|
if err != nil {
|
|
|
|
return
|
2015-02-26 23:47:04 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
w.Header().Set("Content-Type", ct)
|
|
|
|
w.Header().Set("Content-Length", fmt.Sprint(len(p)))
|
2015-02-26 23:47:04 +00:00
|
|
|
w.Header().Set("Docker-Content-Digest", imh.Digest.String())
|
2015-07-24 06:03:13 +00:00
|
|
|
w.Header().Set("Etag", fmt.Sprintf(`"%s"`, imh.Digest))
|
2015-08-21 04:50:15 +00:00
|
|
|
w.Write(p)
|
2014-11-11 02:57:38 +00:00
|
|
|
}
|
|
|
|
|
2015-12-18 01:32:11 +00:00
|
|
|
func (imh *imageManifestHandler) convertSchema2Manifest(schema2Manifest *schema2.DeserializedManifest) (distribution.Manifest, error) {
|
|
|
|
targetDescriptor := schema2Manifest.Target()
|
|
|
|
blobs := imh.Repository.Blobs(imh)
|
|
|
|
configJSON, err := blobs.Get(imh, targetDescriptor.Digest)
|
|
|
|
if err != nil {
|
|
|
|
imh.Errors = append(imh.Errors, v2.ErrorCodeManifestInvalid.WithDetail(err))
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-02-01 21:47:34 +00:00
|
|
|
ref := imh.Repository.Named()
|
2015-12-15 22:35:23 +00:00
|
|
|
|
|
|
|
if imh.Tag != "" {
|
2016-02-01 21:47:34 +00:00
|
|
|
ref, err = reference.WithTag(ref, imh.Tag)
|
2015-12-15 22:35:23 +00:00
|
|
|
if err != nil {
|
|
|
|
imh.Errors = append(imh.Errors, v2.ErrorCodeTagInvalid.WithDetail(err))
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
builder := schema1.NewConfigManifestBuilder(imh.Repository.Blobs(imh), imh.Context.App.trustKey, ref, configJSON)
|
2016-10-13 00:20:27 +00:00
|
|
|
for _, d := range schema2Manifest.Layers {
|
2015-12-18 01:32:11 +00:00
|
|
|
if err := builder.AppendReference(d); err != nil {
|
|
|
|
imh.Errors = append(imh.Errors, v2.ErrorCodeManifestInvalid.WithDetail(err))
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
manifest, err := builder.Build(imh)
|
|
|
|
if err != nil {
|
|
|
|
imh.Errors = append(imh.Errors, v2.ErrorCodeManifestInvalid.WithDetail(err))
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-10 02:28:43 +00:00
|
|
|
imh.Digest = digest.FromBytes(manifest.(*schema1.SignedManifest).Canonical)
|
2015-12-18 01:32:11 +00:00
|
|
|
|
|
|
|
return manifest, nil
|
|
|
|
}
|
|
|
|
|
2015-06-18 23:56:05 +00:00
|
|
|
func etagMatch(r *http.Request, etag string) bool {
|
|
|
|
for _, headerVal := range r.Header["If-None-Match"] {
|
2015-07-24 06:03:13 +00:00
|
|
|
if headerVal == etag || headerVal == fmt.Sprintf(`"%s"`, etag) { // allow quoted or unquoted
|
2015-06-18 23:56:05 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
// PutImageManifest validates and stores an image in the registry.
|
2014-11-11 02:57:38 +00:00
|
|
|
func (imh *imageManifestHandler) PutImageManifest(w http.ResponseWriter, r *http.Request) {
|
2015-02-07 00:19:19 +00:00
|
|
|
ctxu.GetLogger(imh).Debug("PutImageManifest")
|
2015-06-15 17:39:34 +00:00
|
|
|
manifests, err := imh.Repository.Manifests(imh)
|
|
|
|
if err != nil {
|
|
|
|
imh.Errors = append(imh.Errors, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-07-30 01:18:50 +00:00
|
|
|
var jsonBuf bytes.Buffer
|
|
|
|
if err := copyFullPayload(w, r, &jsonBuf, imh, "image manifest PUT", &imh.Errors); err != nil {
|
|
|
|
// copyFullPayload reports the error if necessary
|
2015-07-27 17:00:00 +00:00
|
|
|
return
|
|
|
|
}
|
Initial implementation of Manifest HTTP API
Push, pull and delete of manifest files in the registry have been implemented
on top of the storage services. Basic workflows, including reporting of missing
manifests are tested, including various proposed response codes. Common testing
functionality has been collected into shared methods. A test suite may be
emerging but it might better to capture more edge cases (such as resumable
upload, range requests, etc.) before we commit to a full approach.
To support clearer test cases and simpler handler methods, an application aware
urlBuilder has been added. We may want to export the functionality for use in
the client, which could allow us to abstract away from gorilla/mux.
A few error codes have been added to fill in error conditions missing from the
proposal. Some use cases have identified some problems with the approach to
error reporting that requires more work to reconcile. To resolve this, the
mapping of Go errors into error types needs to pulled out of the handlers and
into the application. We also need to move to type-based errors, with rich
information, rather than value-based errors. ErrorHandlers will probably
replace the http.Handlers to make this work correctly.
Unrelated to the above, the "length" parameter has been migrated to "size" for
completing layer uploads. This change should have gone out before but these
diffs ending up being coupled with the parameter name change due to updates to
the layer unit tests.
2014-11-26 20:16:58 +00:00
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
mediaType := r.Header.Get("Content-Type")
|
|
|
|
manifest, desc, err := distribution.UnmarshalManifest(mediaType, jsonBuf.Bytes())
|
2015-02-26 23:47:04 +00:00
|
|
|
if err != nil {
|
2015-08-21 04:50:15 +00:00
|
|
|
imh.Errors = append(imh.Errors, v2.ErrorCodeManifestInvalid.WithDetail(err))
|
2015-02-26 23:47:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
if imh.Digest != "" {
|
|
|
|
if desc.Digest != imh.Digest {
|
|
|
|
ctxu.GetLogger(imh).Errorf("payload digest does match: %q != %q", desc.Digest, imh.Digest)
|
2015-06-03 13:52:39 +00:00
|
|
|
imh.Errors = append(imh.Errors, v2.ErrorCodeDigestInvalid)
|
2015-02-26 23:47:04 +00:00
|
|
|
return
|
|
|
|
}
|
2015-08-21 04:50:15 +00:00
|
|
|
} else if imh.Tag != "" {
|
|
|
|
imh.Digest = desc.Digest
|
2015-02-26 23:47:04 +00:00
|
|
|
} else {
|
2015-06-03 13:52:39 +00:00
|
|
|
imh.Errors = append(imh.Errors, v2.ErrorCodeTagInvalid.WithDetail("no tag or digest specified"))
|
2015-02-26 23:47:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-18 22:30:47 +00:00
|
|
|
var options []distribution.ManifestServiceOption
|
|
|
|
if imh.Tag != "" {
|
|
|
|
options = append(options, distribution.WithTag(imh.Tag))
|
|
|
|
}
|
2016-11-22 00:36:36 +00:00
|
|
|
|
|
|
|
if err := imh.applyResourcePolicy(manifest); err != nil {
|
|
|
|
imh.Errors = append(imh.Errors, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-18 22:30:47 +00:00
|
|
|
_, err = manifests.Put(imh, manifest, options...)
|
2015-08-21 04:50:15 +00:00
|
|
|
if err != nil {
|
Initial implementation of Manifest HTTP API
Push, pull and delete of manifest files in the registry have been implemented
on top of the storage services. Basic workflows, including reporting of missing
manifests are tested, including various proposed response codes. Common testing
functionality has been collected into shared methods. A test suite may be
emerging but it might better to capture more edge cases (such as resumable
upload, range requests, etc.) before we commit to a full approach.
To support clearer test cases and simpler handler methods, an application aware
urlBuilder has been added. We may want to export the functionality for use in
the client, which could allow us to abstract away from gorilla/mux.
A few error codes have been added to fill in error conditions missing from the
proposal. Some use cases have identified some problems with the approach to
error reporting that requires more work to reconcile. To resolve this, the
mapping of Go errors into error types needs to pulled out of the handlers and
into the application. We also need to move to type-based errors, with rich
information, rather than value-based errors. ErrorHandlers will probably
replace the http.Handlers to make this work correctly.
Unrelated to the above, the "length" parameter has been migrated to "size" for
completing layer uploads. This change should have gone out before but these
diffs ending up being coupled with the parameter name change due to updates to
the layer unit tests.
2014-11-26 20:16:58 +00:00
|
|
|
// TODO(stevvooe): These error handling switches really need to be
|
|
|
|
// handled by an app global mapper.
|
2015-08-11 18:00:30 +00:00
|
|
|
if err == distribution.ErrUnsupported {
|
|
|
|
imh.Errors = append(imh.Errors, errcode.ErrorCodeUnsupported)
|
|
|
|
return
|
|
|
|
}
|
2016-02-18 18:37:03 +00:00
|
|
|
if err == distribution.ErrAccessDenied {
|
|
|
|
imh.Errors = append(imh.Errors, errcode.ErrorCodeDenied)
|
|
|
|
return
|
|
|
|
}
|
Initial implementation of Manifest HTTP API
Push, pull and delete of manifest files in the registry have been implemented
on top of the storage services. Basic workflows, including reporting of missing
manifests are tested, including various proposed response codes. Common testing
functionality has been collected into shared methods. A test suite may be
emerging but it might better to capture more edge cases (such as resumable
upload, range requests, etc.) before we commit to a full approach.
To support clearer test cases and simpler handler methods, an application aware
urlBuilder has been added. We may want to export the functionality for use in
the client, which could allow us to abstract away from gorilla/mux.
A few error codes have been added to fill in error conditions missing from the
proposal. Some use cases have identified some problems with the approach to
error reporting that requires more work to reconcile. To resolve this, the
mapping of Go errors into error types needs to pulled out of the handlers and
into the application. We also need to move to type-based errors, with rich
information, rather than value-based errors. ErrorHandlers will probably
replace the http.Handlers to make this work correctly.
Unrelated to the above, the "length" parameter has been migrated to "size" for
completing layer uploads. This change should have gone out before but these
diffs ending up being coupled with the parameter name change due to updates to
the layer unit tests.
2014-11-26 20:16:58 +00:00
|
|
|
switch err := err.(type) {
|
2015-02-13 21:59:50 +00:00
|
|
|
case distribution.ErrManifestVerification:
|
Initial implementation of Manifest HTTP API
Push, pull and delete of manifest files in the registry have been implemented
on top of the storage services. Basic workflows, including reporting of missing
manifests are tested, including various proposed response codes. Common testing
functionality has been collected into shared methods. A test suite may be
emerging but it might better to capture more edge cases (such as resumable
upload, range requests, etc.) before we commit to a full approach.
To support clearer test cases and simpler handler methods, an application aware
urlBuilder has been added. We may want to export the functionality for use in
the client, which could allow us to abstract away from gorilla/mux.
A few error codes have been added to fill in error conditions missing from the
proposal. Some use cases have identified some problems with the approach to
error reporting that requires more work to reconcile. To resolve this, the
mapping of Go errors into error types needs to pulled out of the handlers and
into the application. We also need to move to type-based errors, with rich
information, rather than value-based errors. ErrorHandlers will probably
replace the http.Handlers to make this work correctly.
Unrelated to the above, the "length" parameter has been migrated to "size" for
completing layer uploads. This change should have gone out before but these
diffs ending up being coupled with the parameter name change due to updates to
the layer unit tests.
2014-11-26 20:16:58 +00:00
|
|
|
for _, verificationError := range err {
|
|
|
|
switch verificationError := verificationError.(type) {
|
Refactor Blob Service API
This PR refactors the blob service API to be oriented around blob descriptors.
Identified by digests, blobs become an abstract entity that can be read and
written using a descriptor as a handle. This allows blobs to take many forms,
such as a ReadSeekCloser or a simple byte buffer, allowing blob oriented
operations to better integrate with blob agnostic APIs (such as the `io`
package). The error definitions are now better organized to reflect conditions
that can only be seen when interacting with the blob API.
The main benefit of this is to separate the much smaller metadata from large
file storage. Many benefits also follow from this. Reading and writing has
been separated into discrete services. Backend implementation is also
simplified, by reducing the amount of metadata that needs to be picked up to
simply serve a read. This also improves cacheability.
"Opening" a blob simply consists of an access check (Stat) and a path
calculation. Caching is greatly simplified and we've made the mapping of
provisional to canonical hashes a first-class concept. BlobDescriptorService
and BlobProvider can be combined in different ways to achieve varying effects.
Recommend Review Approach
-------------------------
This is a very large patch. While apologies are in order, we are getting a
considerable amount of refactoring. Most changes follow from the changes to
the root package (distribution), so start there. From there, the main changes
are in storage. Looking at (*repository).Blobs will help to understand the how
the linkedBlobStore is wired. One can explore the internals within and also
branch out into understanding the changes to the caching layer. Following the
descriptions below will also help to guide you.
To reduce the chances for regressions, it was critical that major changes to
unit tests were avoided. Where possible, they are left untouched and where
not, the spirit is hopefully captured. Pay particular attention to where
behavior may have changed.
Storage
-------
The primary changes to the `storage` package, other than the interface
updates, were to merge the layerstore and blobstore. Blob access is now
layered even further. The first layer, blobStore, exposes a global
`BlobStatter` and `BlobProvider`. Operations here provide a fast path for most
read operations that don't take access control into account. The
`linkedBlobStore` layers on top of the `blobStore`, providing repository-
scoped blob link management in the backend. The `linkedBlobStore` implements
the full `BlobStore` suite, providing access-controlled, repository-local blob
writers. The abstraction between the two is slightly broken in that
`linkedBlobStore` is the only channel under which one can write into the global
blob store. The `linkedBlobStore` also provides flexibility in that it can act
over different link sets depending on configuration. This allows us to use the
same code for signature links, manifest links and blob links. Eventually, we
will fully consolidate this storage.
The improved cache flow comes from the `linkedBlobStatter` component
of `linkedBlobStore`. Using a `cachedBlobStatter`, these combine together to
provide a simple cache hierarchy that should streamline access checks on read
and write operations, or at least provide a single path to optimize. The
metrics have been changed in a slightly incompatible way since the former
operations, Fetch and Exists, are no longer relevant.
The fileWriter and fileReader have been slightly modified to support the rest
of the changes. The most interesting is the removal of the `Stat` call from
`newFileReader`. This was the source of unnecessary round trips that were only
present to look up the size of the resulting reader. Now, one must simply pass
in the size, requiring the caller to decide whether or not the `Stat` call is
appropriate. In several cases, it turned out the caller already had the size
already. The `WriterAt` implementation has been removed from `fileWriter`,
since it is no longer required for `BlobWriter`, reducing the number of paths
which writes may take.
Cache
-----
Unfortunately, the `cache` package required a near full rewrite. It was pretty
mechanical in that the cache is oriented around the `BlobDescriptorService`
slightly modified to include the ability to set the values for individual
digests. While the implementation is oriented towards caching, it can act as a
primary store. Provisions are in place to have repository local metadata, in
addition to global metadata. Fallback is implemented as a part of the storage
package to maintain this flexibility.
One unfortunate side-effect is that caching is now repository-scoped, rather
than global. This should have little effect on performance but may increase
memory usage.
Handlers
--------
The `handlers` package has been updated to leverage the new API. For the most
part, the changes are superficial or mechanical based on the API changes. This
did expose a bug in the handling of provisional vs canonical digests that was
fixed in the unit tests.
Configuration
-------------
One user-facing change has been made to the configuration and is updated in
the associated documentation. The `layerinfo` cache parameter has been
deprecated by the `blobdescriptor` cache parameter. Both are equivalent and
configuration files should be backward compatible.
Notifications
-------------
Changes the `notification` package are simply to support the interface
changes.
Context
-------
A small change has been made to the tracing log-level. Traces have been moved
from "info" to "debug" level to reduce output when not needed.
Signed-off-by: Stephen J Day <stephen.day@docker.com>
2015-05-12 07:10:29 +00:00
|
|
|
case distribution.ErrManifestBlobUnknown:
|
2015-09-15 04:12:33 +00:00
|
|
|
imh.Errors = append(imh.Errors, v2.ErrorCodeManifestBlobUnknown.WithDetail(verificationError.Digest))
|
2015-12-01 21:26:37 +00:00
|
|
|
case distribution.ErrManifestNameInvalid:
|
|
|
|
imh.Errors = append(imh.Errors, v2.ErrorCodeNameInvalid.WithDetail(err))
|
2015-02-13 21:59:50 +00:00
|
|
|
case distribution.ErrManifestUnverified:
|
2015-06-03 13:52:39 +00:00
|
|
|
imh.Errors = append(imh.Errors, v2.ErrorCodeManifestUnverified)
|
Initial implementation of Manifest HTTP API
Push, pull and delete of manifest files in the registry have been implemented
on top of the storage services. Basic workflows, including reporting of missing
manifests are tested, including various proposed response codes. Common testing
functionality has been collected into shared methods. A test suite may be
emerging but it might better to capture more edge cases (such as resumable
upload, range requests, etc.) before we commit to a full approach.
To support clearer test cases and simpler handler methods, an application aware
urlBuilder has been added. We may want to export the functionality for use in
the client, which could allow us to abstract away from gorilla/mux.
A few error codes have been added to fill in error conditions missing from the
proposal. Some use cases have identified some problems with the approach to
error reporting that requires more work to reconcile. To resolve this, the
mapping of Go errors into error types needs to pulled out of the handlers and
into the application. We also need to move to type-based errors, with rich
information, rather than value-based errors. ErrorHandlers will probably
replace the http.Handlers to make this work correctly.
Unrelated to the above, the "length" parameter has been migrated to "size" for
completing layer uploads. This change should have gone out before but these
diffs ending up being coupled with the parameter name change due to updates to
the layer unit tests.
2014-11-26 20:16:58 +00:00
|
|
|
default:
|
|
|
|
if verificationError == digest.ErrDigestInvalidFormat {
|
2015-06-03 13:52:39 +00:00
|
|
|
imh.Errors = append(imh.Errors, v2.ErrorCodeDigestInvalid)
|
Initial implementation of Manifest HTTP API
Push, pull and delete of manifest files in the registry have been implemented
on top of the storage services. Basic workflows, including reporting of missing
manifests are tested, including various proposed response codes. Common testing
functionality has been collected into shared methods. A test suite may be
emerging but it might better to capture more edge cases (such as resumable
upload, range requests, etc.) before we commit to a full approach.
To support clearer test cases and simpler handler methods, an application aware
urlBuilder has been added. We may want to export the functionality for use in
the client, which could allow us to abstract away from gorilla/mux.
A few error codes have been added to fill in error conditions missing from the
proposal. Some use cases have identified some problems with the approach to
error reporting that requires more work to reconcile. To resolve this, the
mapping of Go errors into error types needs to pulled out of the handlers and
into the application. We also need to move to type-based errors, with rich
information, rather than value-based errors. ErrorHandlers will probably
replace the http.Handlers to make this work correctly.
Unrelated to the above, the "length" parameter has been migrated to "size" for
completing layer uploads. This change should have gone out before but these
diffs ending up being coupled with the parameter name change due to updates to
the layer unit tests.
2014-11-26 20:16:58 +00:00
|
|
|
} else {
|
2015-06-03 13:52:39 +00:00
|
|
|
imh.Errors = append(imh.Errors, errcode.ErrorCodeUnknown, verificationError)
|
Initial implementation of Manifest HTTP API
Push, pull and delete of manifest files in the registry have been implemented
on top of the storage services. Basic workflows, including reporting of missing
manifests are tested, including various proposed response codes. Common testing
functionality has been collected into shared methods. A test suite may be
emerging but it might better to capture more edge cases (such as resumable
upload, range requests, etc.) before we commit to a full approach.
To support clearer test cases and simpler handler methods, an application aware
urlBuilder has been added. We may want to export the functionality for use in
the client, which could allow us to abstract away from gorilla/mux.
A few error codes have been added to fill in error conditions missing from the
proposal. Some use cases have identified some problems with the approach to
error reporting that requires more work to reconcile. To resolve this, the
mapping of Go errors into error types needs to pulled out of the handlers and
into the application. We also need to move to type-based errors, with rich
information, rather than value-based errors. ErrorHandlers will probably
replace the http.Handlers to make this work correctly.
Unrelated to the above, the "length" parameter has been migrated to "size" for
completing layer uploads. This change should have gone out before but these
diffs ending up being coupled with the parameter name change due to updates to
the layer unit tests.
2014-11-26 20:16:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-29 21:34:24 +00:00
|
|
|
case errcode.Error:
|
|
|
|
imh.Errors = append(imh.Errors, err)
|
Initial implementation of Manifest HTTP API
Push, pull and delete of manifest files in the registry have been implemented
on top of the storage services. Basic workflows, including reporting of missing
manifests are tested, including various proposed response codes. Common testing
functionality has been collected into shared methods. A test suite may be
emerging but it might better to capture more edge cases (such as resumable
upload, range requests, etc.) before we commit to a full approach.
To support clearer test cases and simpler handler methods, an application aware
urlBuilder has been added. We may want to export the functionality for use in
the client, which could allow us to abstract away from gorilla/mux.
A few error codes have been added to fill in error conditions missing from the
proposal. Some use cases have identified some problems with the approach to
error reporting that requires more work to reconcile. To resolve this, the
mapping of Go errors into error types needs to pulled out of the handlers and
into the application. We also need to move to type-based errors, with rich
information, rather than value-based errors. ErrorHandlers will probably
replace the http.Handlers to make this work correctly.
Unrelated to the above, the "length" parameter has been migrated to "size" for
completing layer uploads. This change should have gone out before but these
diffs ending up being coupled with the parameter name change due to updates to
the layer unit tests.
2014-11-26 20:16:58 +00:00
|
|
|
default:
|
2015-06-03 13:52:39 +00:00
|
|
|
imh.Errors = append(imh.Errors, errcode.ErrorCodeUnknown.WithDetail(err))
|
Initial implementation of Manifest HTTP API
Push, pull and delete of manifest files in the registry have been implemented
on top of the storage services. Basic workflows, including reporting of missing
manifests are tested, including various proposed response codes. Common testing
functionality has been collected into shared methods. A test suite may be
emerging but it might better to capture more edge cases (such as resumable
upload, range requests, etc.) before we commit to a full approach.
To support clearer test cases and simpler handler methods, an application aware
urlBuilder has been added. We may want to export the functionality for use in
the client, which could allow us to abstract away from gorilla/mux.
A few error codes have been added to fill in error conditions missing from the
proposal. Some use cases have identified some problems with the approach to
error reporting that requires more work to reconcile. To resolve this, the
mapping of Go errors into error types needs to pulled out of the handlers and
into the application. We also need to move to type-based errors, with rich
information, rather than value-based errors. ErrorHandlers will probably
replace the http.Handlers to make this work correctly.
Unrelated to the above, the "length" parameter has been migrated to "size" for
completing layer uploads. This change should have gone out before but these
diffs ending up being coupled with the parameter name change due to updates to
the layer unit tests.
2014-11-26 20:16:58 +00:00
|
|
|
}
|
2014-11-11 02:57:38 +00:00
|
|
|
|
Initial implementation of Manifest HTTP API
Push, pull and delete of manifest files in the registry have been implemented
on top of the storage services. Basic workflows, including reporting of missing
manifests are tested, including various proposed response codes. Common testing
functionality has been collected into shared methods. A test suite may be
emerging but it might better to capture more edge cases (such as resumable
upload, range requests, etc.) before we commit to a full approach.
To support clearer test cases and simpler handler methods, an application aware
urlBuilder has been added. We may want to export the functionality for use in
the client, which could allow us to abstract away from gorilla/mux.
A few error codes have been added to fill in error conditions missing from the
proposal. Some use cases have identified some problems with the approach to
error reporting that requires more work to reconcile. To resolve this, the
mapping of Go errors into error types needs to pulled out of the handlers and
into the application. We also need to move to type-based errors, with rich
information, rather than value-based errors. ErrorHandlers will probably
replace the http.Handlers to make this work correctly.
Unrelated to the above, the "length" parameter has been migrated to "size" for
completing layer uploads. This change should have gone out before but these
diffs ending up being coupled with the parameter name change due to updates to
the layer unit tests.
2014-11-26 20:16:58 +00:00
|
|
|
return
|
|
|
|
}
|
2015-02-04 02:27:40 +00:00
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
// Tag this manifest
|
|
|
|
if imh.Tag != "" {
|
|
|
|
tags := imh.Repository.Tags(imh)
|
|
|
|
err = tags.Tag(imh, imh.Tag, desc)
|
|
|
|
if err != nil {
|
|
|
|
imh.Errors = append(imh.Errors, errcode.ErrorCodeUnknown.WithDetail(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-26 23:47:04 +00:00
|
|
|
// Construct a canonical url for the uploaded manifest.
|
2016-02-01 21:47:34 +00:00
|
|
|
ref, err := reference.WithDigest(imh.Repository.Named(), imh.Digest)
|
2015-12-16 00:43:13 +00:00
|
|
|
if err != nil {
|
|
|
|
imh.Errors = append(imh.Errors, errcode.ErrorCodeUnknown.WithDetail(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
location, err := imh.urlBuilder.BuildManifestURL(ref)
|
2015-02-26 23:47:04 +00:00
|
|
|
if err != nil {
|
|
|
|
// NOTE(stevvooe): Given the behavior above, this absurdly unlikely to
|
|
|
|
// happen. We'll log the error here but proceed as if it worked. Worst
|
|
|
|
// case, we set an empty location header.
|
|
|
|
ctxu.GetLogger(imh).Errorf("error building manifest url from digest: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Location", location)
|
|
|
|
w.Header().Set("Docker-Content-Digest", imh.Digest.String())
|
2015-07-24 17:42:02 +00:00
|
|
|
w.WriteHeader(http.StatusCreated)
|
2014-11-11 02:57:38 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 00:36:36 +00:00
|
|
|
// applyResourcePolicy checks whether the resource class matches what has
|
|
|
|
// been authorized and allowed by the policy configuration.
|
|
|
|
func (imh *imageManifestHandler) applyResourcePolicy(manifest distribution.Manifest) error {
|
|
|
|
allowedClasses := imh.App.Config.Policy.Repository.Classes
|
|
|
|
if len(allowedClasses) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var class string
|
|
|
|
switch m := manifest.(type) {
|
|
|
|
case *schema1.SignedManifest:
|
|
|
|
class = "image"
|
|
|
|
case *schema2.DeserializedManifest:
|
|
|
|
switch m.Config.MediaType {
|
|
|
|
case schema2.MediaTypeConfig:
|
|
|
|
class = "image"
|
|
|
|
case schema2.MediaTypePluginConfig:
|
|
|
|
class = "plugin"
|
|
|
|
default:
|
|
|
|
message := fmt.Sprintf("unknown manifest class for %s", m.Config.MediaType)
|
|
|
|
return errcode.ErrorCodeDenied.WithMessage(message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if class == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if class is allowed in registry
|
|
|
|
var allowedClass bool
|
|
|
|
for _, c := range allowedClasses {
|
|
|
|
if class == c {
|
|
|
|
allowedClass = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !allowedClass {
|
|
|
|
message := fmt.Sprintf("registry does not allow %s manifest", class)
|
|
|
|
return errcode.ErrorCodeDenied.WithMessage(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
resources := auth.AuthorizedResources(imh)
|
|
|
|
n := imh.Repository.Named().Name()
|
|
|
|
|
|
|
|
var foundResource bool
|
|
|
|
for _, r := range resources {
|
|
|
|
if r.Name == n {
|
|
|
|
if r.Class == "" {
|
|
|
|
r.Class = "image"
|
|
|
|
}
|
|
|
|
if r.Class == class {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
foundResource = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// resource was found but no matching class was found
|
|
|
|
if foundResource {
|
|
|
|
message := fmt.Sprintf("repository not authorized for %s manifest", class)
|
|
|
|
return errcode.ErrorCodeDenied.WithMessage(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-27 17:52:22 +00:00
|
|
|
// DeleteImageManifest removes the manifest with the given digest from the registry.
|
2014-11-11 02:57:38 +00:00
|
|
|
func (imh *imageManifestHandler) DeleteImageManifest(w http.ResponseWriter, r *http.Request) {
|
2015-02-07 00:19:19 +00:00
|
|
|
ctxu.GetLogger(imh).Debug("DeleteImageManifest")
|
2015-02-26 23:47:04 +00:00
|
|
|
|
2015-05-27 17:52:22 +00:00
|
|
|
manifests, err := imh.Repository.Manifests(imh)
|
|
|
|
if err != nil {
|
|
|
|
imh.Errors = append(imh.Errors, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
err = manifests.Delete(imh, imh.Digest)
|
2015-05-27 17:52:22 +00:00
|
|
|
if err != nil {
|
|
|
|
switch err {
|
|
|
|
case digest.ErrDigestUnsupported:
|
|
|
|
case digest.ErrDigestInvalidFormat:
|
|
|
|
imh.Errors = append(imh.Errors, v2.ErrorCodeDigestInvalid)
|
|
|
|
return
|
|
|
|
case distribution.ErrBlobUnknown:
|
|
|
|
imh.Errors = append(imh.Errors, v2.ErrorCodeManifestUnknown)
|
|
|
|
return
|
|
|
|
case distribution.ErrUnsupported:
|
2015-08-06 23:25:08 +00:00
|
|
|
imh.Errors = append(imh.Errors, errcode.ErrorCodeUnsupported)
|
2015-08-11 18:00:30 +00:00
|
|
|
return
|
2015-05-27 17:52:22 +00:00
|
|
|
default:
|
|
|
|
imh.Errors = append(imh.Errors, errcode.ErrorCodeUnknown)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 19:22:40 +00:00
|
|
|
tagService := imh.Repository.Tags(imh)
|
|
|
|
referencedTags, err := tagService.Lookup(imh, distribution.Descriptor{Digest: imh.Digest})
|
|
|
|
if err != nil {
|
|
|
|
imh.Errors = append(imh.Errors, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tag := range referencedTags {
|
|
|
|
if err := tagService.Untag(imh, tag); err != nil {
|
|
|
|
imh.Errors = append(imh.Errors, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-27 17:52:22 +00:00
|
|
|
w.WriteHeader(http.StatusAccepted)
|
2015-02-26 23:47:04 +00:00
|
|
|
}
|