2015-02-11 02:25:40 +01:00
|
|
|
package handlers
|
2014-11-11 03:57:38 +01:00
|
|
|
|
2014-12-10 06:25:54 +01:00
|
|
|
import (
|
2018-01-18 01:01:41 +01:00
|
|
|
"context"
|
2015-02-07 01:19:19 +01:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
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/registry/api/errcode"
|
|
|
|
v2 "github.com/distribution/distribution/v3/registry/api/v2"
|
|
|
|
"github.com/distribution/distribution/v3/registry/auth"
|
2016-12-17 01:28:34 +01:00
|
|
|
"github.com/opencontainers/go-digest"
|
2014-12-10 06:25:54 +01:00
|
|
|
)
|
2014-11-11 03:57:38 +01:00
|
|
|
|
|
|
|
// Context should contain the request specific context for use in across
|
|
|
|
// handlers. Resources that don't need to be shared across handlers should not
|
|
|
|
// be on this object.
|
|
|
|
type Context struct {
|
|
|
|
// App points to the application structure that created this context.
|
|
|
|
*App
|
2015-02-07 01:19:19 +01:00
|
|
|
context.Context
|
2015-01-29 00:55:18 +01:00
|
|
|
|
2015-01-17 03:32:27 +01:00
|
|
|
// Repository is the repository for the current request. All requests
|
|
|
|
// should be scoped to a single repository. This field may be nil.
|
2015-02-12 01:49:49 +01:00
|
|
|
Repository distribution.Repository
|
2014-11-11 03:57:38 +01:00
|
|
|
|
2018-08-14 22:10:56 +02:00
|
|
|
// RepositoryRemover provides method to delete a repository
|
|
|
|
RepositoryRemover distribution.RepositoryRemover
|
|
|
|
|
2014-11-11 03:57:38 +01:00
|
|
|
// Errors is a collection of errors encountered during the request to be
|
|
|
|
// returned to the client API. If errors are added to the collection, the
|
|
|
|
// handler *must not* start the response via http.ResponseWriter.
|
2015-05-15 03:21:39 +02:00
|
|
|
Errors errcode.Errors
|
2014-11-11 03:57:38 +01:00
|
|
|
|
2015-02-07 01:19:19 +01:00
|
|
|
urlBuilder *v2.URLBuilder
|
|
|
|
|
|
|
|
// TODO(stevvooe): The goal is too completely factor this context and
|
|
|
|
// dispatching out of the web application. Ideally, we should lean on
|
|
|
|
// context.Context for injection of these resources.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value overrides context.Context.Value to ensure that calls are routed to
|
|
|
|
// correct context.
|
|
|
|
func (ctx *Context) Value(key interface{}) interface{} {
|
|
|
|
return ctx.Context.Value(key)
|
|
|
|
}
|
2015-02-04 02:59:24 +01:00
|
|
|
|
2015-02-07 01:19:19 +01:00
|
|
|
func getName(ctx context.Context) (name string) {
|
2017-08-12 00:31:16 +02:00
|
|
|
return dcontext.GetStringValue(ctx, "vars.name")
|
2015-02-07 01:19:19 +01:00
|
|
|
}
|
2014-11-11 03:57:38 +01:00
|
|
|
|
2015-02-27 00:47:04 +01:00
|
|
|
func getReference(ctx context.Context) (reference string) {
|
2017-08-12 00:31:16 +02:00
|
|
|
return dcontext.GetStringValue(ctx, "vars.reference")
|
2015-02-07 01:19:19 +01: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 21:16:58 +01:00
|
|
|
|
2015-02-07 01:19:19 +01:00
|
|
|
var errDigestNotAvailable = fmt.Errorf("digest not available in context")
|
|
|
|
|
|
|
|
func getDigest(ctx context.Context) (dgst digest.Digest, err error) {
|
2017-08-12 00:31:16 +02:00
|
|
|
dgstStr := dcontext.GetStringValue(ctx, "vars.digest")
|
2015-02-07 01:19:19 +01:00
|
|
|
|
|
|
|
if dgstStr == "" {
|
2017-08-12 00:31:16 +02:00
|
|
|
dcontext.GetLogger(ctx).Errorf("digest not available")
|
2015-02-07 01:19:19 +01:00
|
|
|
return "", errDigestNotAvailable
|
|
|
|
}
|
|
|
|
|
2016-12-16 00:07:42 +01:00
|
|
|
d, err := digest.Parse(dgstStr)
|
2015-02-07 01:19:19 +01:00
|
|
|
if err != nil {
|
2017-08-12 00:31:16 +02:00
|
|
|
dcontext.GetLogger(ctx).Errorf("error parsing digest=%q: %v", dgstStr, err)
|
2015-02-07 01:19:19 +01:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUploadUUID(ctx context.Context) (uuid string) {
|
2017-08-12 00:31:16 +02:00
|
|
|
return dcontext.GetStringValue(ctx, "vars.uuid")
|
2015-02-07 01:19:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// getUserName attempts to resolve a username from the context and request. If
|
|
|
|
// a username cannot be resolved, the empty string is returned.
|
|
|
|
func getUserName(ctx context.Context, r *http.Request) string {
|
2017-08-12 00:31:16 +02:00
|
|
|
username := dcontext.GetStringValue(ctx, auth.UserNameKey)
|
2015-02-07 01:19:19 +01:00
|
|
|
|
|
|
|
// Fallback to request user with basic auth
|
|
|
|
if username == "" {
|
|
|
|
var ok bool
|
|
|
|
uname, _, ok := basicAuth(r)
|
|
|
|
if ok {
|
|
|
|
username = uname
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return username
|
2014-11-11 03:57:38 +01:00
|
|
|
}
|