2014-11-11 00:09:10 +01:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2014-11-20 03:52:09 +01:00
|
|
|
|
|
|
|
"github.com/docker/docker-registry/digest"
|
2014-11-22 04:39:52 +01:00
|
|
|
"github.com/docker/docker-registry/storage"
|
2014-11-11 00:09:10 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// ErrorCode represents the error type. The errors are serialized via strings
|
|
|
|
// and the integer format may change and should *never* be exported.
|
|
|
|
type ErrorCode int
|
|
|
|
|
|
|
|
const (
|
2014-11-18 00:44:07 +01:00
|
|
|
// ErrorCodeUnknown is a catch-all for errors not defined below.
|
2014-11-11 00:09:10 +01:00
|
|
|
ErrorCodeUnknown ErrorCode = iota
|
|
|
|
|
|
|
|
// The following errors can happen during a layer upload.
|
2014-11-18 00:44:07 +01:00
|
|
|
|
2014-11-21 04:15:09 +01:00
|
|
|
// ErrorCodeInvalidDigest is returned when uploading a layer if the
|
|
|
|
// provided digest does not match the layer contents.
|
|
|
|
ErrorCodeInvalidDigest
|
2014-11-18 00:44:07 +01:00
|
|
|
|
|
|
|
// ErrorCodeInvalidLength is returned when uploading a layer if the provided
|
|
|
|
// length does not match the content length.
|
2014-11-11 00:09:10 +01:00
|
|
|
ErrorCodeInvalidLength
|
2014-11-18 00:44:07 +01:00
|
|
|
|
|
|
|
// ErrorCodeInvalidName is returned when the name in the manifest does not
|
|
|
|
// match the provided name.
|
2014-11-11 00:09:10 +01:00
|
|
|
ErrorCodeInvalidName
|
2014-11-18 00:44:07 +01:00
|
|
|
|
|
|
|
// ErrorCodeInvalidTag is returned when the tag in the manifest does not
|
|
|
|
// match the provided tag.
|
2014-11-11 00:09:10 +01:00
|
|
|
ErrorCodeInvalidTag
|
2014-11-18 00:44:07 +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
|
|
|
// ErrorCodeUnknownManifest returned when image manifest name and tag is
|
|
|
|
// unknown, accompanied by a 404 status.
|
|
|
|
ErrorCodeUnknownManifest
|
|
|
|
|
|
|
|
// ErrorCodeInvalidManifest returned when an image manifest is invalid,
|
|
|
|
// typically during a PUT operation.
|
|
|
|
ErrorCodeInvalidManifest
|
|
|
|
|
2014-11-18 00:44:07 +01:00
|
|
|
// ErrorCodeUnverifiedManifest is returned when the manifest fails signature
|
|
|
|
// validation.
|
2014-11-11 00:09:10 +01:00
|
|
|
ErrorCodeUnverifiedManifest
|
2014-11-18 00:44:07 +01:00
|
|
|
|
|
|
|
// ErrorCodeUnknownLayer is returned when the manifest references a
|
|
|
|
// nonexistent layer.
|
2014-11-11 00:09:10 +01:00
|
|
|
ErrorCodeUnknownLayer
|
2014-11-18 00:44:07 +01:00
|
|
|
|
2014-11-21 04:15:09 +01:00
|
|
|
// ErrorCodeUnknownLayerUpload is returned when an upload is accessed.
|
|
|
|
ErrorCodeUnknownLayerUpload
|
|
|
|
|
2014-11-18 00:44:07 +01:00
|
|
|
// ErrorCodeUntrustedSignature is returned when the manifest is signed by an
|
|
|
|
// untrusted source.
|
2014-11-11 00:09:10 +01:00
|
|
|
ErrorCodeUntrustedSignature
|
|
|
|
)
|
|
|
|
|
|
|
|
var errorCodeStrings = map[ErrorCode]string{
|
|
|
|
ErrorCodeUnknown: "UNKNOWN",
|
2014-11-21 04:15:09 +01:00
|
|
|
ErrorCodeInvalidDigest: "INVALID_DIGEST",
|
2014-11-11 00:09:10 +01:00
|
|
|
ErrorCodeInvalidLength: "INVALID_LENGTH",
|
|
|
|
ErrorCodeInvalidName: "INVALID_NAME",
|
|
|
|
ErrorCodeInvalidTag: "INVALID_TAG",
|
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
|
|
|
ErrorCodeUnknownManifest: "UNKNOWN_MANIFEST",
|
|
|
|
ErrorCodeInvalidManifest: "INVALID_MANIFEST",
|
2014-11-11 00:09:10 +01:00
|
|
|
ErrorCodeUnverifiedManifest: "UNVERIFIED_MANIFEST",
|
|
|
|
ErrorCodeUnknownLayer: "UNKNOWN_LAYER",
|
2014-11-21 04:15:09 +01:00
|
|
|
ErrorCodeUnknownLayerUpload: "UNKNOWN_LAYER_UPLOAD",
|
2014-11-11 00:09:10 +01:00
|
|
|
ErrorCodeUntrustedSignature: "UNTRUSTED_SIGNATURE",
|
|
|
|
}
|
|
|
|
|
|
|
|
var errorCodesMessages = map[ErrorCode]string{
|
|
|
|
ErrorCodeUnknown: "unknown error",
|
2014-11-21 04:15:09 +01:00
|
|
|
ErrorCodeInvalidDigest: "provided digest did not match uploaded content",
|
2014-11-11 00:09:10 +01:00
|
|
|
ErrorCodeInvalidLength: "provided length did not match content length",
|
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
|
|
|
ErrorCodeInvalidName: "manifest name did not match URI",
|
|
|
|
ErrorCodeInvalidTag: "manifest tag did not match URI",
|
|
|
|
ErrorCodeUnknownManifest: "manifest not known",
|
|
|
|
ErrorCodeInvalidManifest: "manifest is invalid",
|
|
|
|
ErrorCodeUnverifiedManifest: "manifest failed signature validation",
|
|
|
|
ErrorCodeUnknownLayer: "referenced layer not available",
|
2014-11-21 04:15:09 +01:00
|
|
|
ErrorCodeUnknownLayerUpload: "cannot resume unknown layer upload",
|
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
|
|
|
ErrorCodeUntrustedSignature: "manifest signed by untrusted source",
|
2014-11-11 00:09:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var stringToErrorCode map[string]ErrorCode
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
stringToErrorCode = make(map[string]ErrorCode, len(errorCodeStrings))
|
|
|
|
|
|
|
|
// Build up reverse error code map
|
|
|
|
for k, v := range errorCodeStrings {
|
|
|
|
stringToErrorCode[v] = k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseErrorCode attempts to parse the error code string, returning
|
|
|
|
// ErrorCodeUnknown if the error is not known.
|
|
|
|
func ParseErrorCode(s string) ErrorCode {
|
|
|
|
ec, ok := stringToErrorCode[s]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return ErrorCodeUnknown
|
|
|
|
}
|
|
|
|
|
|
|
|
return ec
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the canonical identifier for this error code.
|
|
|
|
func (ec ErrorCode) String() string {
|
|
|
|
s, ok := errorCodeStrings[ec]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return errorCodeStrings[ErrorCodeUnknown]
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2014-11-18 00:44:07 +01:00
|
|
|
// Message returned the human-readable error message for this error code.
|
2014-11-11 00:09:10 +01:00
|
|
|
func (ec ErrorCode) Message() string {
|
|
|
|
m, ok := errorCodesMessages[ec]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return errorCodesMessages[ErrorCodeUnknown]
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2014-11-18 00:44:07 +01:00
|
|
|
// MarshalText encodes the receiver into UTF-8-encoded text and returns the
|
|
|
|
// result.
|
2014-11-11 00:09:10 +01:00
|
|
|
func (ec ErrorCode) MarshalText() (text []byte, err error) {
|
|
|
|
return []byte(ec.String()), nil
|
|
|
|
}
|
|
|
|
|
2014-11-18 00:44:07 +01:00
|
|
|
// UnmarshalText decodes the form generated by MarshalText.
|
2014-11-11 00:09:10 +01:00
|
|
|
func (ec *ErrorCode) UnmarshalText(text []byte) error {
|
|
|
|
*ec = stringToErrorCode[string(text)]
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-18 00:44:07 +01:00
|
|
|
// Error provides a wrapper around ErrorCode with extra Details provided.
|
2014-11-11 00:09:10 +01:00
|
|
|
type Error struct {
|
2014-11-21 04:15:09 +01:00
|
|
|
Code ErrorCode `json:"code"`
|
2014-11-11 00:09:10 +01:00
|
|
|
Message string `json:"message,omitempty"`
|
|
|
|
Detail interface{} `json:"detail,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error returns a human readable representation of the error.
|
|
|
|
func (e Error) Error() string {
|
|
|
|
return fmt.Sprintf("%s: %s",
|
2014-11-21 04:15:09 +01:00
|
|
|
strings.ToLower(strings.Replace(e.Code.String(), "_", " ", -1)),
|
2014-11-11 00:09:10 +01:00
|
|
|
e.Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Errors provides the envelope for multiple errors and a few sugar methods
|
|
|
|
// for use within the application.
|
|
|
|
type Errors struct {
|
2014-11-11 00:56:23 +01:00
|
|
|
Errors []error `json:"errors,omitempty"`
|
2014-11-11 00:09:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Push pushes an error on to the error stack, with the optional detail
|
|
|
|
// argument. It is a programming error (ie panic) to push more than one
|
|
|
|
// detail at a time.
|
|
|
|
func (errs *Errors) Push(code ErrorCode, details ...interface{}) {
|
|
|
|
if len(details) > 1 {
|
|
|
|
panic("please specify zero or one detail items for this error")
|
|
|
|
}
|
|
|
|
|
|
|
|
var detail interface{}
|
|
|
|
if len(details) > 0 {
|
|
|
|
detail = details[0]
|
|
|
|
}
|
|
|
|
|
2014-11-21 04:15:09 +01:00
|
|
|
if err, ok := detail.(error); ok {
|
|
|
|
detail = err.Error()
|
|
|
|
}
|
|
|
|
|
2014-11-11 00:56:23 +01:00
|
|
|
errs.PushErr(Error{
|
2014-11-11 00:09:10 +01:00
|
|
|
Code: code,
|
|
|
|
Message: code.Message(),
|
|
|
|
Detail: detail,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-11-11 00:56:23 +01:00
|
|
|
// PushErr pushes an error interface onto the error stack.
|
|
|
|
func (errs *Errors) PushErr(err error) {
|
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
|
|
|
switch err.(type) {
|
|
|
|
case Error:
|
|
|
|
errs.Errors = append(errs.Errors, err)
|
|
|
|
default:
|
|
|
|
errs.Errors = append(errs.Errors, Error{Message: err.Error()})
|
|
|
|
}
|
2014-11-11 00:56:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (errs *Errors) Error() string {
|
2014-11-21 04:15:09 +01:00
|
|
|
switch errs.Len() {
|
2014-11-11 00:56:23 +01:00
|
|
|
case 0:
|
|
|
|
return "<nil>"
|
|
|
|
case 1:
|
|
|
|
return errs.Errors[0].Error()
|
|
|
|
default:
|
|
|
|
msg := "errors:\n"
|
|
|
|
for _, err := range errs.Errors {
|
|
|
|
msg += err.Error() + "\n"
|
|
|
|
}
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-21 04:15:09 +01:00
|
|
|
// Clear clears the errors.
|
|
|
|
func (errs *Errors) Clear() {
|
|
|
|
errs.Errors = errs.Errors[:0]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the current number of errors.
|
|
|
|
func (errs *Errors) Len() int {
|
|
|
|
return len(errs.Errors)
|
|
|
|
}
|
|
|
|
|
2014-11-11 04:58:10 +01:00
|
|
|
// DetailUnknownLayer provides detail for unknown layer errors, returned by
|
2014-11-11 00:09:10 +01:00
|
|
|
// image manifest push for layers that are not yet transferred. This intended
|
|
|
|
// to only be used on the backend to return detail for this specific error.
|
|
|
|
type DetailUnknownLayer struct {
|
|
|
|
|
|
|
|
// Unknown should contain the contents of a layer descriptor, which is a
|
2014-11-11 04:58:10 +01:00
|
|
|
// single FSLayer currently.
|
2014-11-22 04:29:08 +01:00
|
|
|
Unknown storage.FSLayer `json:"unknown"`
|
2014-11-11 04:58:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// RepositoryNotFoundError is returned when making an operation against a
|
2014-11-18 00:44:07 +01:00
|
|
|
// repository that does not exist in the registry.
|
2014-11-11 04:58:10 +01:00
|
|
|
type RepositoryNotFoundError struct {
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *RepositoryNotFoundError) Error() string {
|
|
|
|
return fmt.Sprintf("No repository found with Name: %s", e.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ImageManifestNotFoundError is returned when making an operation against a
|
2014-11-18 00:44:07 +01:00
|
|
|
// given image manifest that does not exist in the registry.
|
2014-11-11 04:58:10 +01:00
|
|
|
type ImageManifestNotFoundError struct {
|
|
|
|
Name string
|
|
|
|
Tag string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ImageManifestNotFoundError) Error() string {
|
|
|
|
return fmt.Sprintf("No manifest found with Name: %s, Tag: %s",
|
|
|
|
e.Name, e.Tag)
|
|
|
|
}
|
|
|
|
|
2014-11-20 03:06:54 +01:00
|
|
|
// BlobNotFoundError is returned when making an operation against a given image
|
2014-11-18 00:44:07 +01:00
|
|
|
// layer that does not exist in the registry.
|
2014-11-20 03:06:54 +01:00
|
|
|
type BlobNotFoundError struct {
|
2014-11-11 04:58:10 +01:00
|
|
|
Name string
|
2014-11-20 03:52:09 +01:00
|
|
|
Digest digest.Digest
|
2014-11-11 04:58:10 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 03:06:54 +01:00
|
|
|
func (e *BlobNotFoundError) Error() string {
|
|
|
|
return fmt.Sprintf("No blob found with Name: %s, Digest: %s",
|
|
|
|
e.Name, e.Digest)
|
2014-11-11 04:58:10 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 03:06:54 +01:00
|
|
|
// BlobUploadNotFoundError is returned when making a blob upload operation against an
|
|
|
|
// invalid blob upload location url.
|
2014-11-11 04:58:10 +01:00
|
|
|
// This may be the result of using a cancelled, completed, or stale upload
|
2014-11-18 00:44:07 +01:00
|
|
|
// location.
|
2014-11-20 03:06:54 +01:00
|
|
|
type BlobUploadNotFoundError struct {
|
2014-11-11 04:58:10 +01:00
|
|
|
Location string
|
|
|
|
}
|
|
|
|
|
2014-11-20 03:06:54 +01:00
|
|
|
func (e *BlobUploadNotFoundError) Error() string {
|
|
|
|
return fmt.Sprintf("No blob upload found at Location: %s", e.Location)
|
2014-11-11 04:58:10 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 03:06:54 +01:00
|
|
|
// BlobUploadInvalidRangeError is returned when attempting to upload an image
|
|
|
|
// blob chunk that is out of order.
|
|
|
|
// This provides the known BlobSize and LastValidRange which can be used to
|
2014-11-18 00:44:07 +01:00
|
|
|
// resume the upload.
|
2014-11-20 03:06:54 +01:00
|
|
|
type BlobUploadInvalidRangeError struct {
|
2014-11-11 04:58:10 +01:00
|
|
|
Location string
|
|
|
|
LastValidRange int
|
2014-11-20 03:06:54 +01:00
|
|
|
BlobSize int
|
2014-11-11 04:58:10 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 03:06:54 +01:00
|
|
|
func (e *BlobUploadInvalidRangeError) Error() string {
|
2014-11-11 04:58:10 +01:00
|
|
|
return fmt.Sprintf(
|
2014-11-20 03:06:54 +01:00
|
|
|
"Invalid range provided for upload at Location: %s. Last Valid Range: %d, Blob Size: %d",
|
|
|
|
e.Location, e.LastValidRange, e.BlobSize)
|
2014-11-11 04:58:10 +01:00
|
|
|
}
|
|
|
|
|
2014-11-18 00:44:07 +01:00
|
|
|
// UnexpectedHTTPStatusError is returned when an unexpected HTTP status is
|
|
|
|
// returned when making a registry api call.
|
|
|
|
type UnexpectedHTTPStatusError struct {
|
2014-11-11 04:58:10 +01:00
|
|
|
Status string
|
|
|
|
}
|
2014-11-11 00:09:10 +01:00
|
|
|
|
2014-11-18 00:44:07 +01:00
|
|
|
func (e *UnexpectedHTTPStatusError) Error() string {
|
|
|
|
return fmt.Sprintf("Received unexpected HTTP status: %s", e.Status)
|
2014-11-11 00:09:10 +01:00
|
|
|
}
|