2017-04-17 18:08:24 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2023-10-13 11:54:59 -04:00
|
|
|
"mime"
|
2017-04-17 18:08:24 -04:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/docker/distribution/registry/api/errcode"
|
|
|
|
"github.com/docker/distribution/registry/client/auth/challenge"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ErrNoErrorsInBody is returned when an HTTP response body parses to an empty
|
|
|
|
// errcode.Errors slice.
|
|
|
|
var ErrNoErrorsInBody = errors.New("no error details found in HTTP response body")
|
|
|
|
|
|
|
|
// UnexpectedHTTPStatusError is returned when an unexpected HTTP status is
|
|
|
|
// returned when making a registry api call.
|
|
|
|
type UnexpectedHTTPStatusError struct {
|
|
|
|
Status string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *UnexpectedHTTPStatusError) Error() string {
|
|
|
|
return fmt.Sprintf("received unexpected HTTP status: %s", e.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnexpectedHTTPResponseError is returned when an expected HTTP status code
|
|
|
|
// is returned, but the content was unexpected and failed to be parsed.
|
|
|
|
type UnexpectedHTTPResponseError struct {
|
|
|
|
ParseErr error
|
|
|
|
StatusCode int
|
|
|
|
Response []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *UnexpectedHTTPResponseError) Error() string {
|
|
|
|
return fmt.Sprintf("error parsing HTTP %d response body: %s: %q", e.StatusCode, e.ParseErr.Error(), string(e.Response))
|
|
|
|
}
|
|
|
|
|
2023-10-13 11:54:59 -04:00
|
|
|
func parseHTTPErrorResponse(resp *http.Response) error {
|
2017-04-17 18:08:24 -04:00
|
|
|
var errors errcode.Errors
|
2023-10-13 11:54:59 -04:00
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
2017-04-17 18:08:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-13 11:54:59 -04:00
|
|
|
statusCode := resp.StatusCode
|
|
|
|
ctHeader := resp.Header.Get("Content-Type")
|
|
|
|
|
|
|
|
if ctHeader == "" {
|
|
|
|
return makeError(statusCode, string(body))
|
|
|
|
}
|
|
|
|
|
|
|
|
contentType, _, err := mime.ParseMediaType(ctHeader)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed parsing content-type: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if contentType != "application/json" && contentType != "application/vnd.api+json" {
|
|
|
|
return makeError(statusCode, string(body))
|
|
|
|
}
|
|
|
|
|
2017-04-17 18:08:24 -04:00
|
|
|
// For backward compatibility, handle irregularly formatted
|
|
|
|
// messages that contain a "details" field.
|
|
|
|
var detailsErr struct {
|
|
|
|
Details string `json:"details"`
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(body, &detailsErr)
|
|
|
|
if err == nil && detailsErr.Details != "" {
|
2023-10-13 11:54:59 -04:00
|
|
|
return makeError(statusCode, detailsErr.Details)
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(body, &errors); err != nil {
|
|
|
|
return &UnexpectedHTTPResponseError{
|
|
|
|
ParseErr: err,
|
|
|
|
StatusCode: statusCode,
|
|
|
|
Response: body,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errors) == 0 {
|
|
|
|
// If there was no error specified in the body, return
|
|
|
|
// UnexpectedHTTPResponseError.
|
|
|
|
return &UnexpectedHTTPResponseError{
|
|
|
|
ParseErr: ErrNoErrorsInBody,
|
|
|
|
StatusCode: statusCode,
|
|
|
|
Response: body,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors
|
|
|
|
}
|
|
|
|
|
2023-10-13 11:54:59 -04:00
|
|
|
func makeError(statusCode int, details string) error {
|
|
|
|
switch statusCode {
|
|
|
|
case http.StatusUnauthorized:
|
|
|
|
return errcode.ErrorCodeUnauthorized.WithMessage(details)
|
|
|
|
case http.StatusForbidden:
|
|
|
|
return errcode.ErrorCodeDenied.WithMessage(details)
|
|
|
|
case http.StatusTooManyRequests:
|
|
|
|
return errcode.ErrorCodeTooManyRequests.WithMessage(details)
|
|
|
|
default:
|
|
|
|
return errcode.ErrorCodeUnknown.WithMessage(details)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 18:08:24 -04:00
|
|
|
func makeErrorList(err error) []error {
|
|
|
|
if errL, ok := err.(errcode.Errors); ok {
|
|
|
|
return []error(errL)
|
|
|
|
}
|
|
|
|
return []error{err}
|
|
|
|
}
|
|
|
|
|
|
|
|
func mergeErrors(err1, err2 error) error {
|
|
|
|
return errcode.Errors(append(makeErrorList(err1), makeErrorList(err2)...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleErrorResponse returns error parsed from HTTP response for an
|
|
|
|
// unsuccessful HTTP response code (in the range 400 - 499 inclusive). An
|
|
|
|
// UnexpectedHTTPStatusError returned for response code outside of expected
|
|
|
|
// range.
|
|
|
|
func HandleErrorResponse(resp *http.Response) error {
|
|
|
|
if resp.StatusCode >= 400 && resp.StatusCode < 500 {
|
|
|
|
// Check for OAuth errors within the `WWW-Authenticate` header first
|
|
|
|
// See https://tools.ietf.org/html/rfc6750#section-3
|
|
|
|
for _, c := range challenge.ResponseChallenges(resp) {
|
|
|
|
if c.Scheme == "bearer" {
|
|
|
|
var err errcode.Error
|
|
|
|
// codes defined at https://tools.ietf.org/html/rfc6750#section-3.1
|
|
|
|
switch c.Parameters["error"] {
|
|
|
|
case "invalid_token":
|
|
|
|
err.Code = errcode.ErrorCodeUnauthorized
|
|
|
|
case "insufficient_scope":
|
|
|
|
err.Code = errcode.ErrorCodeDenied
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if description := c.Parameters["error_description"]; description != "" {
|
|
|
|
err.Message = description
|
|
|
|
} else {
|
|
|
|
err.Message = err.Code.Message()
|
|
|
|
}
|
2023-10-13 11:54:59 -04:00
|
|
|
return mergeErrors(err, parseHTTPErrorResponse(resp))
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
}
|
2023-10-13 11:54:59 -04:00
|
|
|
err := parseHTTPErrorResponse(resp)
|
2017-04-17 18:08:24 -04:00
|
|
|
if uErr, ok := err.(*UnexpectedHTTPResponseError); ok && resp.StatusCode == 401 {
|
|
|
|
return errcode.ErrorCodeUnauthorized.WithDetail(uErr.Response)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return &UnexpectedHTTPStatusError{Status: resp.Status}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SuccessStatus returns true if the argument is a successful HTTP response
|
|
|
|
// code (in the range 200 - 399 inclusive).
|
|
|
|
func SuccessStatus(status int) bool {
|
|
|
|
return status >= 200 && status <= 399
|
|
|
|
}
|