errors.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // Error represents an ACME
  2. package acme
  3. import (
  4. "fmt"
  5. "github.com/pkg/errors"
  6. )
  7. // ProblemType is the type of the ACME problem.
  8. type ProblemType int
  9. const (
  10. // The request specified an account that does not exist
  11. ErrorAccountDoesNotExistType ProblemType = iota
  12. // The request specified a certificate to be revoked that has already been revoked
  13. ErrorAlreadyRevokedType
  14. // The CSR is unacceptable (e.g., due to a short key)
  15. ErrorBadCSRType
  16. // The client sent an unacceptable anti-replay nonce
  17. ErrorBadNonceType
  18. // The JWS was signed by a public key the server does not support
  19. ErrorBadPublicKeyType
  20. // The revocation reason provided is not allowed by the server
  21. ErrorBadRevocationReasonType
  22. // The JWS was signed with an algorithm the server does not support
  23. ErrorBadSignatureAlgorithmType
  24. // Certification Authority Authorization (CAA) records forbid the CA from issuing a certificate
  25. ErrorCaaType
  26. // Specific error conditions are indicated in the “subproblems” array.
  27. ErrorCompoundType
  28. // The server could not connect to validation target
  29. ErrorConnectionType
  30. // There was a problem with a DNS query during identifier validation
  31. ErrorDNSType
  32. // The request must include a value for the “externalAccountBinding” field
  33. ErrorExternalAccountRequiredType
  34. // Response received didn’t match the challenge’s requirements
  35. ErrorIncorrectResponseType
  36. // A contact URL for an account was invalid
  37. ErrorInvalidContactType
  38. // The request message was malformed
  39. ErrorMalformedType
  40. // The request attempted to finalize an order that is not ready to be finalized
  41. ErrorOrderNotReadyType
  42. // The request exceeds a rate limit
  43. ErrorRateLimitedType
  44. // The server will not issue certificates for the identifier
  45. ErrorRejectedIdentifierType
  46. // The server experienced an internal error
  47. ErrorServerInternalType
  48. // The server received a TLS error during validation
  49. ErrorTLSType
  50. // The client lacks sufficient authorization
  51. ErrorUnauthorizedType
  52. // A contact URL for an account used an unsupported protocol scheme
  53. ErrorUnsupportedContactType
  54. // An identifier is of an unsupported type
  55. ErrorUnsupportedIdentifierType
  56. // Visit the “instance” URL and take actions specified there
  57. ErrorUserActionRequiredType
  58. // The operation is not implemented
  59. ErrorNotImplementedType
  60. )
  61. // String returns the string representation of the acme problem type,
  62. // fulfilling the Stringer interface.
  63. func (ap ProblemType) String() string {
  64. switch ap {
  65. case ErrorAccountDoesNotExistType:
  66. return "accountDoesNotExist"
  67. case ErrorAlreadyRevokedType:
  68. return "alreadyRevoked"
  69. case ErrorBadCSRType:
  70. return "badCSR"
  71. case ErrorBadNonceType:
  72. return "badNonce"
  73. case ErrorBadPublicKeyType:
  74. return "badPublicKey"
  75. case ErrorBadRevocationReasonType:
  76. return "badRevocationReason"
  77. case ErrorBadSignatureAlgorithmType:
  78. return "badSignatureAlgorithm"
  79. case ErrorCaaType:
  80. return "caa"
  81. case ErrorCompoundType:
  82. return "compound"
  83. case ErrorConnectionType:
  84. return "connection"
  85. case ErrorDNSType:
  86. return "dns"
  87. case ErrorExternalAccountRequiredType:
  88. return "externalAccountRequired"
  89. case ErrorInvalidContactType:
  90. return "incorrectResponse"
  91. case ErrorInvalidContactType:
  92. return "invalidContact"
  93. case ErrorMalformedType:
  94. return "malformed"
  95. case ErrorOrderNotReadyType:
  96. return "orderNotReady"
  97. case ErrorRateLimitedType:
  98. return "rateLimited"
  99. case ErrorRejectedIdentifierType:
  100. return "rejectedIdentifier"
  101. case ErrorServerInternalType:
  102. return "serverInternal"
  103. case ErrorTLSType:
  104. return "tls"
  105. case ErrorUnauthorizedType:
  106. return "unauthorized"
  107. case ErrorUnsupportedContactType:
  108. return "unsupportedContact"
  109. case ErrorUnsupportedIdentifierType:
  110. return "unsupportedIdentifier"
  111. case ErrorUserActionRequiredType:
  112. return "userActionRequired"
  113. case ErrorNotImplementedType:
  114. return "notImplemented"
  115. default:
  116. return fmt.Sprintf("unsupported type ACME error type %v", ap)
  117. }
  118. }
  119. type errorMetadata struct {
  120. details string
  121. status int
  122. typ string
  123. String string
  124. }
  125. var (
  126. officialACMEPrefix = "urn:ietf:params:acme:error:"
  127. stepACMEPrefix = "urn:step:acme:error:"
  128. errorServerInternalMetadata = errorMetadata{
  129. ErrorAccountDoesNotExistType: {
  130. typ: officialACMEPrefix + ErrorServerInternalType.String(),
  131. details: "The server experienced an internal error",
  132. status: 500,
  133. },
  134. }
  135. errorMap = [ProblemType]errorMetadata{
  136. ErrorAccountDoesNotExistType: {
  137. typ: officialACMEPrefix + ErrorAccountDoesNotExistType.String(),
  138. details: "Account does not exist",
  139. status: 400,
  140. },
  141. ErrorAlreadyRevokedType: {
  142. typ: officialACMEPrefix + ErrorAlreadyRevokedType.String(),
  143. details: "Certificate already Revoked",
  144. status: 400,
  145. },
  146. ErrorBadCSRType: {
  147. typ: officialACMEPrefix + ErrorBadCSRType.String(),
  148. details: "The CSR is unacceptable",
  149. status: 400,
  150. },
  151. ErrorBadNonceType: {
  152. typ: officialACMEPrefix + ErrorBadNonceType.String(),
  153. details: "Unacceptable anti-replay nonce",
  154. status: 400,
  155. },
  156. ErrorBadPublicKeyType: {
  157. typ: officialACMEPrefix + ErrorBadPublicKeyType.String(),
  158. details: "The jws was signed by a public key the server does not support",
  159. status: 400,
  160. },
  161. ErrorBadRevocationReasonType: {
  162. typ: officialACMEPrefix + ErrorBadRevocationReasonType.String(),
  163. details: "The revocation reason provided is not allowed by the server",
  164. status: 400,
  165. },
  166. ErrorBadSignatureAlgorithmType: {
  167. typ: officialACMEPrefix + ErrorBadSignatureAlgorithmType.String(),
  168. details: "The JWS was signed with an algorithm the server does not support",
  169. status: 400,
  170. },
  171. ErrorCaaType: {
  172. typ: officialACMEPrefix + ErrorCaaType.String(),
  173. details: "Certification Authority Authorization (CAA) records forbid the CA from issuing a certificate",
  174. status: 400,
  175. },
  176. ErrorCompoundType: {
  177. typ: officialACMEPrefix + ErrorCompoundType.String(),
  178. details: "Specific error conditions are indicated in the “subproblems” array",
  179. status: 400,
  180. },
  181. ErrorConnectionType: {
  182. typ: officialACMEPrefix + ErrorConnectionType.String(),
  183. details: "The server could not connect to validation target",
  184. status: 400,
  185. },
  186. ErrorDNSType: {
  187. typ: officialACMEPrefix + ErrorDNSType.String(),
  188. details: "There was a problem with a DNS query during identifier validation",
  189. status: 400,
  190. },
  191. ErrorExternalAccountRequiredType: {
  192. typ: officialACMEPrefix + ErrorExternalAccountRequiredType.String(),
  193. details: "The request must include a value for the \"externalAccountBinding\" field",
  194. status: 400,
  195. },
  196. ErrorIncorrectResponseType: {
  197. typ: officialACMEPrefix + ErrorIncorrectResponseType.String(),
  198. details: "Response received didn't match the challenge's requirements",
  199. status: 400,
  200. },
  201. ErrorInvalidContactType: {
  202. typ: officialACMEPrefix + ErrorInvalidContactType.String(),
  203. details: "A contact URL for an account was invalid",
  204. status: 400,
  205. },
  206. ErrorMalformedType: {
  207. typ: officialACMEPrefix + ErrorMalformedType.String(),
  208. details: "The request message was malformed",
  209. status: 400,
  210. },
  211. ErrorOrderNotReadyType: {
  212. typ: officialACMEPrefix + ErrorOrderNotReadyType.String(),
  213. details: "The request attempted to finalize an order that is not ready to be finalized",
  214. status: 400,
  215. },
  216. ErrorRateLimitedType: {
  217. typ: officialACMEPrefix + ErrorRateLimitedType.String(),
  218. details: "The request exceeds a rate limit",
  219. status: 400,
  220. },
  221. ErrorRejectedIdentifierType: {
  222. typ: officialACMEPrefix + ErrorRejectedIdentifierType.String(),
  223. details: "The server will not issue certificates for the identifier",
  224. status: 400,
  225. },
  226. ErrorNotImplementedType: {
  227. typ: officialACMEPrefix + ErrorRejectedIdentifierType.String(),
  228. details: "The requested operation is not implemented",
  229. status: 501,
  230. },
  231. ErrorTLSType: {
  232. typ: officialACMEPrefix + ErrorTLSType.String(),
  233. details: "The server received a TLS error during validation",
  234. status: 400,
  235. },
  236. ErrorUnauthorizedType: {
  237. typ: officialACMEPrefix + ErrorUnauthorizedType.String(),
  238. details: "The client lacks sufficient authorization",
  239. status: 401,
  240. },
  241. ErrorUnsupportedContactType: {
  242. typ: officialACMEPrefix + ErrorUnsupportedContactType.String(),
  243. details: "A contact URL for an account used an unsupported protocol scheme",
  244. status: 400,
  245. },
  246. ErrorUnsupportedIdentifierType: {
  247. typ: officialACMEPrefix + ErrorUnsupportedIdentifierType.String(),
  248. details: "An identifier is of an unsupported type",
  249. status: 400,
  250. },
  251. ErrorUserActionRequiredType: {
  252. typ: officialACMEPrefix + ErrorUserActionRequiredType.String(),
  253. details: "Visit the “instance” URL and take actions specified there",
  254. status: 400,
  255. },
  256. ErrorServerInternalType: errorServerInternalMetadata,
  257. }
  258. )
  259. // Error represents an ACME
  260. type Error struct {
  261. Type string `json:"type"`
  262. Detail string `json:"detail"`
  263. Subproblems []interface{} `json:"subproblems,omitempty"`
  264. Identifier interface{} `json:"identifier,omitempty"`
  265. Err error `json:"-"`
  266. Status int `json:"-"`
  267. }
  268. func NewError(pt ProblemType, msg string, args ...interface{}) *Error {
  269. meta, ok := errorMetadata[typ]
  270. if !ok {
  271. meta = errorServerInternalMetadata
  272. return &Error{
  273. Type: meta.typ,
  274. Details: meta.details,
  275. Status: meta.Status,
  276. Err: errors.Errorf("unrecognized problemType %v", pt),
  277. }
  278. }
  279. return &Error{
  280. Type: meta.typ,
  281. Details: meta.details,
  282. Status: meta.status,
  283. Err: errors.Errorf(msg, args...),
  284. }
  285. }
  286. // ErrorWrap attempts to wrap the internal error.
  287. func ErrorWrap(typ ProblemType, err error, msg string, args ...interface{}) *Error {
  288. switch e := err.(type) {
  289. case nil:
  290. return nil
  291. case *Error:
  292. if e.Err == nil {
  293. e.Err = errors.Errorf(msg+"; "+e.Detail, args...)
  294. } else {
  295. e.Err = errors.Wrapf(e.Err, msg, args...)
  296. }
  297. return e
  298. default:
  299. return NewError(ErrorServerInternalType, msg, args...)
  300. }
  301. }
  302. // StatusCode returns the status code and implements the StatusCoder interface.
  303. func (e *Error) StatusCode() int {
  304. return e.Status
  305. }
  306. // Error allows AError to implement the error interface.
  307. func (e *Error) Error() string {
  308. return e.Detail
  309. }
  310. // Cause returns the internal error and implements the Causer interface.
  311. func (e *Error) Cause() error {
  312. if e.Err == nil {
  313. return errors.New(e.Detail)
  314. }
  315. return e.Err
  316. }