errors.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 ErrorMalformedType:
  92. return "malformed"
  93. case ErrorOrderNotReadyType:
  94. return "orderNotReady"
  95. case ErrorRateLimitedType:
  96. return "rateLimited"
  97. case ErrorRejectedIdentifierType:
  98. return "rejectedIdentifier"
  99. case ErrorServerInternalType:
  100. return "serverInternal"
  101. case ErrorTLSType:
  102. return "tls"
  103. case ErrorUnauthorizedType:
  104. return "unauthorized"
  105. case ErrorUnsupportedContactType:
  106. return "unsupportedContact"
  107. case ErrorUnsupportedIdentifierType:
  108. return "unsupportedIdentifier"
  109. case ErrorUserActionRequiredType:
  110. return "userActionRequired"
  111. case ErrorNotImplementedType:
  112. return "notImplemented"
  113. default:
  114. return fmt.Sprintf("unsupported type ACME error type %v", ap)
  115. }
  116. }
  117. type errorMetadata struct {
  118. details string
  119. status int
  120. typ string
  121. String string
  122. }
  123. var (
  124. officialACMEPrefix = "urn:ietf:params:acme:error:"
  125. stepACMEPrefix = "urn:step:acme:error:"
  126. errorServerInternalMetadata = errorMetadata{
  127. typ: officialACMEPrefix + ErrorServerInternalType.String(),
  128. details: "The server experienced an internal error",
  129. status: 500,
  130. }
  131. errorMap = map[ProblemType]errorMetadata{
  132. ErrorAccountDoesNotExistType: {
  133. typ: officialACMEPrefix + ErrorAccountDoesNotExistType.String(),
  134. details: "Account does not exist",
  135. status: 400,
  136. },
  137. ErrorAlreadyRevokedType: {
  138. typ: officialACMEPrefix + ErrorAlreadyRevokedType.String(),
  139. details: "Certificate already Revoked",
  140. status: 400,
  141. },
  142. ErrorBadCSRType: {
  143. typ: officialACMEPrefix + ErrorBadCSRType.String(),
  144. details: "The CSR is unacceptable",
  145. status: 400,
  146. },
  147. ErrorBadNonceType: {
  148. typ: officialACMEPrefix + ErrorBadNonceType.String(),
  149. details: "Unacceptable anti-replay nonce",
  150. status: 400,
  151. },
  152. ErrorBadPublicKeyType: {
  153. typ: officialACMEPrefix + ErrorBadPublicKeyType.String(),
  154. details: "The jws was signed by a public key the server does not support",
  155. status: 400,
  156. },
  157. ErrorBadRevocationReasonType: {
  158. typ: officialACMEPrefix + ErrorBadRevocationReasonType.String(),
  159. details: "The revocation reason provided is not allowed by the server",
  160. status: 400,
  161. },
  162. ErrorBadSignatureAlgorithmType: {
  163. typ: officialACMEPrefix + ErrorBadSignatureAlgorithmType.String(),
  164. details: "The JWS was signed with an algorithm the server does not support",
  165. status: 400,
  166. },
  167. ErrorCaaType: {
  168. typ: officialACMEPrefix + ErrorCaaType.String(),
  169. details: "Certification Authority Authorization (CAA) records forbid the CA from issuing a certificate",
  170. status: 400,
  171. },
  172. ErrorCompoundType: {
  173. typ: officialACMEPrefix + ErrorCompoundType.String(),
  174. details: "Specific error conditions are indicated in the “subproblems” array",
  175. status: 400,
  176. },
  177. ErrorConnectionType: {
  178. typ: officialACMEPrefix + ErrorConnectionType.String(),
  179. details: "The server could not connect to validation target",
  180. status: 400,
  181. },
  182. ErrorDNSType: {
  183. typ: officialACMEPrefix + ErrorDNSType.String(),
  184. details: "There was a problem with a DNS query during identifier validation",
  185. status: 400,
  186. },
  187. ErrorExternalAccountRequiredType: {
  188. typ: officialACMEPrefix + ErrorExternalAccountRequiredType.String(),
  189. details: "The request must include a value for the \"externalAccountBinding\" field",
  190. status: 400,
  191. },
  192. ErrorIncorrectResponseType: {
  193. typ: officialACMEPrefix + ErrorIncorrectResponseType.String(),
  194. details: "Response received didn't match the challenge's requirements",
  195. status: 400,
  196. },
  197. ErrorInvalidContactType: {
  198. typ: officialACMEPrefix + ErrorInvalidContactType.String(),
  199. details: "A contact URL for an account was invalid",
  200. status: 400,
  201. },
  202. ErrorMalformedType: {
  203. typ: officialACMEPrefix + ErrorMalformedType.String(),
  204. details: "The request message was malformed",
  205. status: 400,
  206. },
  207. ErrorOrderNotReadyType: {
  208. typ: officialACMEPrefix + ErrorOrderNotReadyType.String(),
  209. details: "The request attempted to finalize an order that is not ready to be finalized",
  210. status: 400,
  211. },
  212. ErrorRateLimitedType: {
  213. typ: officialACMEPrefix + ErrorRateLimitedType.String(),
  214. details: "The request exceeds a rate limit",
  215. status: 400,
  216. },
  217. ErrorRejectedIdentifierType: {
  218. typ: officialACMEPrefix + ErrorRejectedIdentifierType.String(),
  219. details: "The server will not issue certificates for the identifier",
  220. status: 400,
  221. },
  222. ErrorNotImplementedType: {
  223. typ: officialACMEPrefix + ErrorRejectedIdentifierType.String(),
  224. details: "The requested operation is not implemented",
  225. status: 501,
  226. },
  227. ErrorTLSType: {
  228. typ: officialACMEPrefix + ErrorTLSType.String(),
  229. details: "The server received a TLS error during validation",
  230. status: 400,
  231. },
  232. ErrorUnauthorizedType: {
  233. typ: officialACMEPrefix + ErrorUnauthorizedType.String(),
  234. details: "The client lacks sufficient authorization",
  235. status: 401,
  236. },
  237. ErrorUnsupportedContactType: {
  238. typ: officialACMEPrefix + ErrorUnsupportedContactType.String(),
  239. details: "A contact URL for an account used an unsupported protocol scheme",
  240. status: 400,
  241. },
  242. ErrorUnsupportedIdentifierType: {
  243. typ: officialACMEPrefix + ErrorUnsupportedIdentifierType.String(),
  244. details: "An identifier is of an unsupported type",
  245. status: 400,
  246. },
  247. ErrorUserActionRequiredType: {
  248. typ: officialACMEPrefix + ErrorUserActionRequiredType.String(),
  249. details: "Visit the “instance” URL and take actions specified there",
  250. status: 400,
  251. },
  252. ErrorServerInternalType: errorServerInternalMetadata,
  253. }
  254. )
  255. // Error represents an ACME
  256. type Error struct {
  257. Type string `json:"type"`
  258. Details string `json:"detail"`
  259. Subproblems []interface{} `json:"subproblems,omitempty"`
  260. Identifier interface{} `json:"identifier,omitempty"`
  261. Err error `json:"-"`
  262. Status int `json:"-"`
  263. }
  264. func NewError(pt ProblemType, msg string, args ...interface{}) *Error {
  265. meta, ok := errorMap[pt]
  266. if !ok {
  267. meta = errorServerInternalMetadata
  268. return &Error{
  269. Type: meta.typ,
  270. Details: meta.details,
  271. Status: meta.status,
  272. Err: errors.Errorf("unrecognized problemType %v", pt),
  273. }
  274. }
  275. return &Error{
  276. Type: meta.typ,
  277. Details: meta.details,
  278. Status: meta.status,
  279. Err: errors.Errorf(msg, args...),
  280. }
  281. }
  282. // ErrorWrap attempts to wrap the internal error.
  283. func ErrorWrap(typ ProblemType, err error, msg string, args ...interface{}) *Error {
  284. switch e := err.(type) {
  285. case nil:
  286. return nil
  287. case *Error:
  288. if e.Err == nil {
  289. e.Err = errors.Errorf(msg+"; "+e.Details, args...)
  290. } else {
  291. e.Err = errors.Wrapf(e.Err, msg, args...)
  292. }
  293. return e
  294. default:
  295. return NewError(ErrorServerInternalType, msg, args...)
  296. }
  297. }
  298. // ErrorInternalServerWrap shortcut to wrap an internal server error type.
  299. func ErrorInternalServerWrap(err error, msg string, args ...interface{}) *Error {
  300. return ErrorWrap(ErrorServerInternalType, err, msg, args...)
  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.Details
  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.Details)
  314. }
  315. return e.Err
  316. }