2019-12-16 07:54:25 +00:00
|
|
|
package errs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2022-03-30 08:22:22 +00:00
|
|
|
"github.com/smallstep/certificates/api/log"
|
|
|
|
"github.com/smallstep/certificates/api/render"
|
|
|
|
)
|
2019-12-16 07:54:25 +00:00
|
|
|
|
|
|
|
// Option modifies the Error type.
|
|
|
|
type Option func(e *Error) error
|
|
|
|
|
2019-12-20 21:30:05 +00:00
|
|
|
// withDefaultMessage returns an Option that modifies the error by overwriting the
|
2019-12-16 07:54:25 +00:00
|
|
|
// message only if it is empty.
|
2019-12-20 21:30:05 +00:00
|
|
|
func withDefaultMessage(format string, args ...interface{}) Option {
|
2019-12-16 07:54:25 +00:00
|
|
|
return func(e *Error) error {
|
2021-11-19 02:17:36 +00:00
|
|
|
if e.Msg != "" {
|
2019-12-16 07:54:25 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
e.Msg = fmt.Sprintf(format, args...)
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 21:30:05 +00:00
|
|
|
// WithMessage returns an Option that modifies the error by overwriting the
|
|
|
|
// message only if it is empty.
|
|
|
|
func WithMessage(format string, args ...interface{}) Option {
|
|
|
|
return func(e *Error) error {
|
|
|
|
e.Msg = fmt.Sprintf(format, args...)
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithKeyVal returns an Option that adds the given key-value pair to the
|
|
|
|
// Error details. This is helpful for debugging errors.
|
|
|
|
func WithKeyVal(key string, val interface{}) Option {
|
|
|
|
return func(e *Error) error {
|
|
|
|
if e.Details == nil {
|
|
|
|
e.Details = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
e.Details[key] = val
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-16 07:54:25 +00:00
|
|
|
// Error represents the CA API errors.
|
|
|
|
type Error struct {
|
2019-12-20 21:30:05 +00:00
|
|
|
Status int
|
|
|
|
Err error
|
|
|
|
Msg string
|
|
|
|
Details map[string]interface{}
|
2019-12-16 07:54:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ErrorResponse represents an error in JSON format.
|
|
|
|
type ErrorResponse struct {
|
|
|
|
Status int `json:"status"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cause implements the errors.Causer interface and returns the original error.
|
|
|
|
func (e *Error) Cause() error {
|
|
|
|
return e.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error implements the error interface and returns the error string.
|
|
|
|
func (e *Error) Error() string {
|
|
|
|
return e.Err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
// StatusCode implements the StatusCoder interface and returns the HTTP response
|
|
|
|
// code.
|
|
|
|
func (e *Error) StatusCode() int {
|
|
|
|
return e.Status
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message returns a user friendly error, if one is set.
|
|
|
|
func (e *Error) Message() string {
|
|
|
|
if len(e.Msg) > 0 {
|
|
|
|
return e.Msg
|
|
|
|
}
|
|
|
|
return e.Err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap returns an error annotating err with a stack trace at the point Wrap is
|
|
|
|
// called, and the supplied message. If err is nil, Wrap returns nil.
|
2020-01-24 06:04:34 +00:00
|
|
|
func Wrap(status int, e error, m string, args ...interface{}) error {
|
2019-12-16 07:54:25 +00:00
|
|
|
if e == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-01-24 06:04:34 +00:00
|
|
|
_, opts := splitOptionArgs(args)
|
2022-08-23 19:43:48 +00:00
|
|
|
var err *Error
|
2022-09-21 04:48:04 +00:00
|
|
|
if errors.As(e, &err) {
|
2019-12-16 07:54:25 +00:00
|
|
|
err.Err = errors.Wrap(err.Err, m)
|
|
|
|
e = err
|
|
|
|
} else {
|
|
|
|
e = errors.Wrap(e, m)
|
|
|
|
}
|
|
|
|
return StatusCodeError(status, e, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapf returns an error annotating err with a stack trace at the point Wrap is
|
|
|
|
// called, and the supplied message. If err is nil, Wrap returns nil.
|
|
|
|
func Wrapf(status int, e error, format string, args ...interface{}) error {
|
|
|
|
if e == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-01-24 06:04:34 +00:00
|
|
|
as, opts := splitOptionArgs(args)
|
2022-08-23 19:43:48 +00:00
|
|
|
var err *Error
|
2022-09-21 04:48:04 +00:00
|
|
|
if errors.As(e, &err) {
|
2019-12-16 07:54:25 +00:00
|
|
|
err.Err = errors.Wrapf(err.Err, format, args...)
|
|
|
|
e = err
|
|
|
|
} else {
|
2020-01-24 06:04:34 +00:00
|
|
|
e = errors.Wrapf(e, format, as...)
|
2019-12-16 07:54:25 +00:00
|
|
|
}
|
|
|
|
return StatusCodeError(status, e, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements json.Marshaller interface for the Error struct.
|
|
|
|
func (e *Error) MarshalJSON() ([]byte, error) {
|
|
|
|
var msg string
|
|
|
|
if len(e.Msg) > 0 {
|
|
|
|
msg = e.Msg
|
|
|
|
} else {
|
|
|
|
msg = http.StatusText(e.Status)
|
|
|
|
}
|
|
|
|
return json.Marshal(&ErrorResponse{Status: e.Status, Message: msg})
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements json.Unmarshaler interface for the Error struct.
|
|
|
|
func (e *Error) UnmarshalJSON(data []byte) error {
|
|
|
|
var er ErrorResponse
|
|
|
|
if err := json.Unmarshal(data, &er); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
e.Status = er.Status
|
2022-02-03 20:43:53 +00:00
|
|
|
e.Err = fmt.Errorf("%s", er.Message)
|
2019-12-16 07:54:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format implements the fmt.Formatter interface.
|
|
|
|
func (e *Error) Format(f fmt.State, c rune) {
|
|
|
|
if err, ok := e.Err.(fmt.Formatter); ok {
|
|
|
|
err.Format(f, c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Fprint(f, e.Err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Messenger is a friendly message interface that errors can implement.
|
|
|
|
type Messenger interface {
|
|
|
|
Message() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// StatusCodeError selects the proper error based on the status code.
|
|
|
|
func StatusCodeError(code int, e error, opts ...Option) error {
|
|
|
|
switch code {
|
|
|
|
case http.StatusBadRequest:
|
2021-11-19 02:17:36 +00:00
|
|
|
opts = append(opts, withDefaultMessage(BadRequestDefaultMsg))
|
|
|
|
return NewErr(http.StatusBadRequest, e, opts...)
|
2019-12-16 07:54:25 +00:00
|
|
|
case http.StatusUnauthorized:
|
2020-01-24 06:04:34 +00:00
|
|
|
return UnauthorizedErr(e, opts...)
|
2019-12-16 07:54:25 +00:00
|
|
|
case http.StatusForbidden:
|
2021-11-23 19:52:55 +00:00
|
|
|
opts = append(opts, withDefaultMessage(ForbiddenDefaultMsg))
|
|
|
|
return NewErr(http.StatusForbidden, e, opts...)
|
2019-12-16 07:54:25 +00:00
|
|
|
case http.StatusInternalServerError:
|
2020-01-24 06:04:34 +00:00
|
|
|
return InternalServerErr(e, opts...)
|
2019-12-16 07:54:25 +00:00
|
|
|
case http.StatusNotImplemented:
|
2020-01-24 06:04:34 +00:00
|
|
|
return NotImplementedErr(e, opts...)
|
2019-12-16 07:54:25 +00:00
|
|
|
default:
|
2020-01-24 06:04:34 +00:00
|
|
|
return UnexpectedErr(code, e, opts...)
|
2019-12-16 07:54:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 21:30:05 +00:00
|
|
|
var (
|
|
|
|
seeLogs = "Please see the certificate authority logs for more info."
|
|
|
|
// BadRequestDefaultMsg 400 default msg
|
2020-09-10 14:45:44 +00:00
|
|
|
BadRequestDefaultMsg = "The request could not be completed; malformed or missing data. " + seeLogs
|
2019-12-20 21:30:05 +00:00
|
|
|
// UnauthorizedDefaultMsg 401 default msg
|
|
|
|
UnauthorizedDefaultMsg = "The request lacked necessary authorization to be completed. " + seeLogs
|
|
|
|
// ForbiddenDefaultMsg 403 default msg
|
|
|
|
ForbiddenDefaultMsg = "The request was forbidden by the certificate authority. " + seeLogs
|
|
|
|
// NotFoundDefaultMsg 404 default msg
|
|
|
|
NotFoundDefaultMsg = "The requested resource could not be found. " + seeLogs
|
|
|
|
// InternalServerErrorDefaultMsg 500 default msg
|
|
|
|
InternalServerErrorDefaultMsg = "The certificate authority encountered an Internal Server Error. " + seeLogs
|
|
|
|
// NotImplementedDefaultMsg 501 default msg
|
|
|
|
NotImplementedDefaultMsg = "The requested method is not implemented by the certificate authority. " + seeLogs
|
|
|
|
)
|
2019-12-16 07:54:25 +00:00
|
|
|
|
2021-11-18 23:12:44 +00:00
|
|
|
var (
|
|
|
|
// BadRequestPrefix is the prefix added to the bad request messages that are
|
|
|
|
// directly sent to the cli.
|
|
|
|
BadRequestPrefix = "The request could not be completed: "
|
2021-11-23 19:52:55 +00:00
|
|
|
|
|
|
|
// ForbiddenPrefix is the prefix added to the forbidden messates that are
|
|
|
|
// sent to the cli.
|
|
|
|
ForbiddenPrefix = "The request was forbidden by the certificate authority: "
|
2021-11-18 23:12:44 +00:00
|
|
|
)
|
|
|
|
|
2021-11-19 02:17:36 +00:00
|
|
|
func formatMessage(status int, msg string) string {
|
|
|
|
switch status {
|
|
|
|
case http.StatusBadRequest:
|
|
|
|
return BadRequestPrefix + msg + "."
|
2021-11-23 19:52:55 +00:00
|
|
|
case http.StatusForbidden:
|
|
|
|
return ForbiddenPrefix + msg + "."
|
2021-11-19 02:17:36 +00:00
|
|
|
default:
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 06:04:34 +00:00
|
|
|
// splitOptionArgs splits the variadic length args into string formatting args
|
|
|
|
// and Option(s) to apply to an Error.
|
|
|
|
func splitOptionArgs(args []interface{}) ([]interface{}, []Option) {
|
|
|
|
indexOptionStart := -1
|
|
|
|
for i, a := range args {
|
|
|
|
if _, ok := a.(Option); ok {
|
|
|
|
indexOptionStart = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if indexOptionStart < 0 {
|
|
|
|
return args, []Option{}
|
|
|
|
}
|
|
|
|
opts := []Option{}
|
|
|
|
// Ignore any non-Option args that come after the first Option.
|
|
|
|
for _, o := range args[indexOptionStart:] {
|
|
|
|
if opt, ok := o.(Option); ok {
|
|
|
|
opts = append(opts, opt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return args[:indexOptionStart], opts
|
|
|
|
}
|
|
|
|
|
2021-11-18 23:12:44 +00:00
|
|
|
// New creates a new http error with the given status and message.
|
|
|
|
func New(status int, format string, args ...interface{}) error {
|
|
|
|
msg := fmt.Sprintf(format, args...)
|
|
|
|
return &Error{
|
|
|
|
Status: status,
|
2021-11-19 02:17:36 +00:00
|
|
|
Msg: formatMessage(status, msg),
|
2021-11-18 23:12:44 +00:00
|
|
|
Err: errors.New(msg),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-19 02:17:36 +00:00
|
|
|
// NewError creates a new http error with the given error and message.
|
|
|
|
func NewError(status int, err error, format string, args ...interface{}) error {
|
2022-08-23 19:43:48 +00:00
|
|
|
var e *Error
|
|
|
|
if errors.As(err, &e) {
|
2021-11-19 03:03:43 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-11-19 02:17:36 +00:00
|
|
|
msg := fmt.Sprintf(format, args...)
|
2022-09-21 04:48:04 +00:00
|
|
|
if _, ok := err.(log.StackTracedError); !ok {
|
2021-11-19 02:17:36 +00:00
|
|
|
err = errors.Wrap(err, msg)
|
|
|
|
}
|
|
|
|
return &Error{
|
|
|
|
Status: status,
|
|
|
|
Msg: formatMessage(status, msg),
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 06:04:34 +00:00
|
|
|
// NewErr returns a new Error. If the given error implements the StatusCoder
|
|
|
|
// interface we will ignore the given status.
|
|
|
|
func NewErr(status int, err error, opts ...Option) error {
|
2022-08-23 19:43:48 +00:00
|
|
|
var e *Error
|
|
|
|
if !errors.As(err, &e) {
|
2022-09-21 04:48:04 +00:00
|
|
|
if sc, ok := err.(render.StatusCodedError); ok {
|
2020-01-24 06:04:34 +00:00
|
|
|
e = &Error{Status: sc.StatusCode(), Err: err}
|
|
|
|
} else {
|
|
|
|
cause := errors.Cause(err)
|
2022-09-21 04:48:04 +00:00
|
|
|
if sc, ok := cause.(render.StatusCodedError); ok {
|
2020-01-24 06:04:34 +00:00
|
|
|
e = &Error{Status: sc.StatusCode(), Err: err}
|
|
|
|
} else {
|
|
|
|
e = &Error{Status: status, Err: err}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(e)
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// Errorf creates a new error using the given format and status code.
|
|
|
|
func Errorf(code int, format string, args ...interface{}) error {
|
|
|
|
as, opts := splitOptionArgs(args)
|
|
|
|
opts = append(opts, withDefaultMessage(NotImplementedDefaultMsg))
|
|
|
|
e := &Error{Status: code, Err: fmt.Errorf(format, as...)}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(e)
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-11-18 23:12:44 +00:00
|
|
|
// ApplyOptions applies the given options to the error if is the type *Error.
|
|
|
|
// TODO(mariano): try to get rid of this.
|
|
|
|
func ApplyOptions(err error, opts ...interface{}) error {
|
2022-08-23 19:43:48 +00:00
|
|
|
var e *Error
|
2022-09-21 04:48:04 +00:00
|
|
|
if errors.As(err, &e) {
|
2021-11-18 23:12:44 +00:00
|
|
|
_, o := splitOptionArgs(opts)
|
|
|
|
for _, fn := range o {
|
|
|
|
fn(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-24 06:04:34 +00:00
|
|
|
// InternalServer creates a 500 error with the given format and arguments.
|
|
|
|
func InternalServer(format string, args ...interface{}) error {
|
|
|
|
args = append(args, withDefaultMessage(InternalServerErrorDefaultMsg))
|
|
|
|
return Errorf(http.StatusInternalServerError, format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InternalServerErr returns a 500 error with the given error.
|
|
|
|
func InternalServerErr(err error, opts ...Option) error {
|
2019-12-20 21:30:05 +00:00
|
|
|
opts = append(opts, withDefaultMessage(InternalServerErrorDefaultMsg))
|
2020-01-24 06:04:34 +00:00
|
|
|
return NewErr(http.StatusInternalServerError, err, opts...)
|
2019-12-16 07:54:25 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 06:04:34 +00:00
|
|
|
// NotImplemented creates a 501 error with the given format and arguments.
|
|
|
|
func NotImplemented(format string, args ...interface{}) error {
|
|
|
|
args = append(args, withDefaultMessage(NotImplementedDefaultMsg))
|
|
|
|
return Errorf(http.StatusNotImplemented, format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotImplementedErr returns a 501 error with the given error.
|
|
|
|
func NotImplementedErr(err error, opts ...Option) error {
|
2019-12-20 21:30:05 +00:00
|
|
|
opts = append(opts, withDefaultMessage(NotImplementedDefaultMsg))
|
2020-01-24 06:04:34 +00:00
|
|
|
return NewErr(http.StatusNotImplemented, err, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BadRequest creates a 400 error with the given format and arguments.
|
|
|
|
func BadRequest(format string, args ...interface{}) error {
|
2021-11-18 23:12:44 +00:00
|
|
|
return New(http.StatusBadRequest, format, args...)
|
2019-12-16 07:54:25 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 06:04:34 +00:00
|
|
|
// BadRequestErr returns an 400 error with the given error.
|
2021-11-19 02:17:36 +00:00
|
|
|
func BadRequestErr(err error, format string, args ...interface{}) error {
|
|
|
|
return NewError(http.StatusBadRequest, err, format, args...)
|
2020-01-24 06:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unauthorized creates a 401 error with the given format and arguments.
|
|
|
|
func Unauthorized(format string, args ...interface{}) error {
|
|
|
|
args = append(args, withDefaultMessage(UnauthorizedDefaultMsg))
|
|
|
|
return Errorf(http.StatusUnauthorized, format, args...)
|
2019-12-16 07:54:25 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 06:04:34 +00:00
|
|
|
// UnauthorizedErr returns an 401 error with the given error.
|
|
|
|
func UnauthorizedErr(err error, opts ...Option) error {
|
2019-12-20 21:30:05 +00:00
|
|
|
opts = append(opts, withDefaultMessage(UnauthorizedDefaultMsg))
|
2020-01-24 06:04:34 +00:00
|
|
|
return NewErr(http.StatusUnauthorized, err, opts...)
|
2019-12-16 07:54:25 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 06:04:34 +00:00
|
|
|
// Forbidden creates a 403 error with the given format and arguments.
|
|
|
|
func Forbidden(format string, args ...interface{}) error {
|
2021-11-23 19:52:55 +00:00
|
|
|
return New(http.StatusForbidden, format, args...)
|
2020-01-24 06:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ForbiddenErr returns an 403 error with the given error.
|
2021-11-23 19:52:55 +00:00
|
|
|
func ForbiddenErr(err error, format string, args ...interface{}) error {
|
|
|
|
return NewError(http.StatusForbidden, err, format, args...)
|
2020-01-24 06:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NotFound creates a 404 error with the given format and arguments.
|
|
|
|
func NotFound(format string, args ...interface{}) error {
|
|
|
|
args = append(args, withDefaultMessage(NotFoundDefaultMsg))
|
|
|
|
return Errorf(http.StatusNotFound, format, args...)
|
2019-12-16 07:54:25 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 06:04:34 +00:00
|
|
|
// NotFoundErr returns an 404 error with the given error.
|
|
|
|
func NotFoundErr(err error, opts ...Option) error {
|
2019-12-20 21:30:05 +00:00
|
|
|
opts = append(opts, withDefaultMessage(NotFoundDefaultMsg))
|
2020-01-24 06:04:34 +00:00
|
|
|
return NewErr(http.StatusNotFound, err, opts...)
|
2019-12-16 07:54:25 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 06:04:34 +00:00
|
|
|
// UnexpectedErr will be used when the certificate authority makes an outgoing
|
2019-12-16 07:54:25 +00:00
|
|
|
// request and receives an unhandled status code.
|
2020-01-24 06:04:34 +00:00
|
|
|
func UnexpectedErr(code int, err error, opts ...Option) error {
|
2019-12-20 21:30:05 +00:00
|
|
|
opts = append(opts, withDefaultMessage("The certificate authority received an "+
|
|
|
|
"unexpected HTTP status code - '%d'. "+seeLogs, code))
|
2020-01-24 06:04:34 +00:00
|
|
|
return NewErr(code, err, opts...)
|
2019-12-16 07:54:25 +00:00
|
|
|
}
|