2014-11-10 23:09:10 +00:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2014-11-20 02:52:09 +00:00
|
|
|
|
|
|
|
"github.com/docker/docker-registry/digest"
|
2014-11-10 23:09:10 +00: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-17 23:44:07 +00:00
|
|
|
// ErrorCodeUnknown is a catch-all for errors not defined below.
|
2014-11-10 23:09:10 +00:00
|
|
|
ErrorCodeUnknown ErrorCode = iota
|
|
|
|
|
|
|
|
// The following errors can happen during a layer upload.
|
2014-11-17 23:44:07 +00:00
|
|
|
|
2014-11-21 03:15:09 +00:00
|
|
|
// ErrorCodeInvalidDigest is returned when uploading a layer if the
|
|
|
|
// provided digest does not match the layer contents.
|
|
|
|
ErrorCodeInvalidDigest
|
2014-11-17 23:44:07 +00:00
|
|
|
|
|
|
|
// ErrorCodeInvalidLength is returned when uploading a layer if the provided
|
|
|
|
// length does not match the content length.
|
2014-11-10 23:09:10 +00:00
|
|
|
ErrorCodeInvalidLength
|
2014-11-17 23:44:07 +00:00
|
|
|
|
|
|
|
// ErrorCodeInvalidName is returned when the name in the manifest does not
|
|
|
|
// match the provided name.
|
2014-11-10 23:09:10 +00:00
|
|
|
ErrorCodeInvalidName
|
2014-11-17 23:44:07 +00:00
|
|
|
|
|
|
|
// ErrorCodeInvalidTag is returned when the tag in the manifest does not
|
|
|
|
// match the provided tag.
|
2014-11-10 23:09:10 +00:00
|
|
|
ErrorCodeInvalidTag
|
2014-11-17 23:44:07 +00:00
|
|
|
|
|
|
|
// ErrorCodeUnverifiedManifest is returned when the manifest fails signature
|
|
|
|
// validation.
|
2014-11-10 23:09:10 +00:00
|
|
|
ErrorCodeUnverifiedManifest
|
2014-11-17 23:44:07 +00:00
|
|
|
|
|
|
|
// ErrorCodeUnknownLayer is returned when the manifest references a
|
|
|
|
// nonexistent layer.
|
2014-11-10 23:09:10 +00:00
|
|
|
ErrorCodeUnknownLayer
|
2014-11-17 23:44:07 +00:00
|
|
|
|
2014-11-21 03:15:09 +00:00
|
|
|
// ErrorCodeUnknownLayerUpload is returned when an upload is accessed.
|
|
|
|
ErrorCodeUnknownLayerUpload
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// ErrorCodeUntrustedSignature is returned when the manifest is signed by an
|
|
|
|
// untrusted source.
|
2014-11-10 23:09:10 +00:00
|
|
|
ErrorCodeUntrustedSignature
|
|
|
|
)
|
|
|
|
|
|
|
|
var errorCodeStrings = map[ErrorCode]string{
|
|
|
|
ErrorCodeUnknown: "UNKNOWN",
|
2014-11-21 03:15:09 +00:00
|
|
|
ErrorCodeInvalidDigest: "INVALID_DIGEST",
|
2014-11-10 23:09:10 +00:00
|
|
|
ErrorCodeInvalidLength: "INVALID_LENGTH",
|
|
|
|
ErrorCodeInvalidName: "INVALID_NAME",
|
|
|
|
ErrorCodeInvalidTag: "INVALID_TAG",
|
|
|
|
ErrorCodeUnverifiedManifest: "UNVERIFIED_MANIFEST",
|
|
|
|
ErrorCodeUnknownLayer: "UNKNOWN_LAYER",
|
2014-11-21 03:15:09 +00:00
|
|
|
ErrorCodeUnknownLayerUpload: "UNKNOWN_LAYER_UPLOAD",
|
2014-11-10 23:09:10 +00:00
|
|
|
ErrorCodeUntrustedSignature: "UNTRUSTED_SIGNATURE",
|
|
|
|
}
|
|
|
|
|
|
|
|
var errorCodesMessages = map[ErrorCode]string{
|
|
|
|
ErrorCodeUnknown: "unknown error",
|
2014-11-21 03:15:09 +00:00
|
|
|
ErrorCodeInvalidDigest: "provided digest did not match uploaded content",
|
2014-11-10 23:09:10 +00:00
|
|
|
ErrorCodeInvalidLength: "provided length did not match content length",
|
|
|
|
ErrorCodeInvalidName: "Manifest name did not match URI",
|
|
|
|
ErrorCodeInvalidTag: "Manifest tag did not match URI",
|
|
|
|
ErrorCodeUnverifiedManifest: "Manifest failed signature validation",
|
|
|
|
ErrorCodeUnknownLayer: "Referenced layer not available",
|
2014-11-21 03:15:09 +00:00
|
|
|
ErrorCodeUnknownLayerUpload: "cannot resume unknown layer upload",
|
2014-11-10 23:09:10 +00:00
|
|
|
ErrorCodeUntrustedSignature: "Manifest signed by untrusted source",
|
|
|
|
}
|
|
|
|
|
|
|
|
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-17 23:44:07 +00:00
|
|
|
// Message returned the human-readable error message for this error code.
|
2014-11-10 23:09:10 +00:00
|
|
|
func (ec ErrorCode) Message() string {
|
|
|
|
m, ok := errorCodesMessages[ec]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return errorCodesMessages[ErrorCodeUnknown]
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// MarshalText encodes the receiver into UTF-8-encoded text and returns the
|
|
|
|
// result.
|
2014-11-10 23:09:10 +00:00
|
|
|
func (ec ErrorCode) MarshalText() (text []byte, err error) {
|
|
|
|
return []byte(ec.String()), nil
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// UnmarshalText decodes the form generated by MarshalText.
|
2014-11-10 23:09:10 +00:00
|
|
|
func (ec *ErrorCode) UnmarshalText(text []byte) error {
|
|
|
|
*ec = stringToErrorCode[string(text)]
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// Error provides a wrapper around ErrorCode with extra Details provided.
|
2014-11-10 23:09:10 +00:00
|
|
|
type Error struct {
|
2014-11-21 03:15:09 +00:00
|
|
|
Code ErrorCode `json:"code"`
|
2014-11-10 23:09:10 +00: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 03:15:09 +00:00
|
|
|
strings.ToLower(strings.Replace(e.Code.String(), "_", " ", -1)),
|
2014-11-10 23:09:10 +00: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-10 23:56:23 +00:00
|
|
|
Errors []error `json:"errors,omitempty"`
|
2014-11-10 23:09:10 +00: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 03:15:09 +00:00
|
|
|
if err, ok := detail.(error); ok {
|
|
|
|
detail = err.Error()
|
|
|
|
}
|
|
|
|
|
2014-11-10 23:56:23 +00:00
|
|
|
errs.PushErr(Error{
|
2014-11-10 23:09:10 +00:00
|
|
|
Code: code,
|
|
|
|
Message: code.Message(),
|
|
|
|
Detail: detail,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-11-10 23:56:23 +00:00
|
|
|
// PushErr pushes an error interface onto the error stack.
|
|
|
|
func (errs *Errors) PushErr(err error) {
|
|
|
|
errs.Errors = append(errs.Errors, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (errs *Errors) Error() string {
|
2014-11-21 03:15:09 +00:00
|
|
|
switch errs.Len() {
|
2014-11-10 23:56:23 +00: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 03:15:09 +00: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 03:58:10 +00:00
|
|
|
// DetailUnknownLayer provides detail for unknown layer errors, returned by
|
2014-11-10 23:09:10 +00: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 03:58:10 +00:00
|
|
|
// single FSLayer currently.
|
2014-11-22 03:29:08 +00:00
|
|
|
Unknown storage.FSLayer `json:"unknown"`
|
2014-11-11 03:58:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RepositoryNotFoundError is returned when making an operation against a
|
2014-11-17 23:44:07 +00:00
|
|
|
// repository that does not exist in the registry.
|
2014-11-11 03:58:10 +00: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-17 23:44:07 +00:00
|
|
|
// given image manifest that does not exist in the registry.
|
2014-11-11 03:58:10 +00: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 02:06:54 +00:00
|
|
|
// BlobNotFoundError is returned when making an operation against a given image
|
2014-11-17 23:44:07 +00:00
|
|
|
// layer that does not exist in the registry.
|
2014-11-20 02:06:54 +00:00
|
|
|
type BlobNotFoundError struct {
|
2014-11-11 03:58:10 +00:00
|
|
|
Name string
|
2014-11-20 02:52:09 +00:00
|
|
|
Digest digest.Digest
|
2014-11-11 03:58:10 +00:00
|
|
|
}
|
|
|
|
|
2014-11-20 02:06:54 +00:00
|
|
|
func (e *BlobNotFoundError) Error() string {
|
|
|
|
return fmt.Sprintf("No blob found with Name: %s, Digest: %s",
|
|
|
|
e.Name, e.Digest)
|
2014-11-11 03:58:10 +00:00
|
|
|
}
|
|
|
|
|
2014-11-20 02:06:54 +00:00
|
|
|
// BlobUploadNotFoundError is returned when making a blob upload operation against an
|
|
|
|
// invalid blob upload location url.
|
2014-11-11 03:58:10 +00:00
|
|
|
// This may be the result of using a cancelled, completed, or stale upload
|
2014-11-17 23:44:07 +00:00
|
|
|
// location.
|
2014-11-20 02:06:54 +00:00
|
|
|
type BlobUploadNotFoundError struct {
|
2014-11-11 03:58:10 +00:00
|
|
|
Location string
|
|
|
|
}
|
|
|
|
|
2014-11-20 02:06:54 +00:00
|
|
|
func (e *BlobUploadNotFoundError) Error() string {
|
|
|
|
return fmt.Sprintf("No blob upload found at Location: %s", e.Location)
|
2014-11-11 03:58:10 +00:00
|
|
|
}
|
|
|
|
|
2014-11-20 02:06:54 +00: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-17 23:44:07 +00:00
|
|
|
// resume the upload.
|
2014-11-20 02:06:54 +00:00
|
|
|
type BlobUploadInvalidRangeError struct {
|
2014-11-11 03:58:10 +00:00
|
|
|
Location string
|
|
|
|
LastValidRange int
|
2014-11-20 02:06:54 +00:00
|
|
|
BlobSize int
|
2014-11-11 03:58:10 +00:00
|
|
|
}
|
|
|
|
|
2014-11-20 02:06:54 +00:00
|
|
|
func (e *BlobUploadInvalidRangeError) Error() string {
|
2014-11-11 03:58:10 +00:00
|
|
|
return fmt.Sprintf(
|
2014-11-20 02:06:54 +00: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 03:58:10 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// UnexpectedHTTPStatusError is returned when an unexpected HTTP status is
|
|
|
|
// returned when making a registry api call.
|
|
|
|
type UnexpectedHTTPStatusError struct {
|
2014-11-11 03:58:10 +00:00
|
|
|
Status string
|
|
|
|
}
|
2014-11-10 23:09:10 +00:00
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
func (e *UnexpectedHTTPStatusError) Error() string {
|
|
|
|
return fmt.Sprintf("Received unexpected HTTP status: %s", e.Status)
|
2014-11-10 23:09:10 +00:00
|
|
|
}
|