order_test.go 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754
  1. package api
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/x509"
  6. "encoding/base64"
  7. "encoding/json"
  8. "fmt"
  9. "io"
  10. "net/http/httptest"
  11. "net/url"
  12. "reflect"
  13. "testing"
  14. "time"
  15. "github.com/go-chi/chi"
  16. "github.com/pkg/errors"
  17. "github.com/smallstep/assert"
  18. "github.com/smallstep/certificates/acme"
  19. "go.step.sm/crypto/pemutil"
  20. )
  21. func TestNewOrderRequest_Validate(t *testing.T) {
  22. type test struct {
  23. nor *NewOrderRequest
  24. nbf, naf time.Time
  25. err *acme.Error
  26. }
  27. var tests = map[string]func(t *testing.T) test{
  28. "fail/no-identifiers": func(t *testing.T) test {
  29. return test{
  30. nor: &NewOrderRequest{},
  31. err: acme.NewError(acme.ErrorMalformedType, "identifiers list cannot be empty"),
  32. }
  33. },
  34. "fail/bad-identifier": func(t *testing.T) test {
  35. return test{
  36. nor: &NewOrderRequest{
  37. Identifiers: []acme.Identifier{
  38. {Type: "dns", Value: "example.com"},
  39. {Type: "foo", Value: "bar.com"},
  40. },
  41. },
  42. err: acme.NewError(acme.ErrorMalformedType, "identifier type unsupported: foo"),
  43. }
  44. },
  45. "fail/bad-ip": func(t *testing.T) test {
  46. nbf := time.Now().UTC().Add(time.Minute)
  47. naf := time.Now().UTC().Add(5 * time.Minute)
  48. return test{
  49. nor: &NewOrderRequest{
  50. Identifiers: []acme.Identifier{
  51. {Type: "ip", Value: "192.168.42.1000"},
  52. },
  53. NotAfter: naf,
  54. NotBefore: nbf,
  55. },
  56. nbf: nbf,
  57. naf: naf,
  58. err: acme.NewError(acme.ErrorMalformedType, "invalid IP address: %s", "192.168.42.1000"),
  59. }
  60. },
  61. "ok": func(t *testing.T) test {
  62. nbf := time.Now().UTC().Add(time.Minute)
  63. naf := time.Now().UTC().Add(5 * time.Minute)
  64. return test{
  65. nor: &NewOrderRequest{
  66. Identifiers: []acme.Identifier{
  67. {Type: "dns", Value: "example.com"},
  68. {Type: "dns", Value: "bar.com"},
  69. },
  70. NotAfter: naf,
  71. NotBefore: nbf,
  72. },
  73. nbf: nbf,
  74. naf: naf,
  75. }
  76. },
  77. "ok/ipv4": func(t *testing.T) test {
  78. nbf := time.Now().UTC().Add(time.Minute)
  79. naf := time.Now().UTC().Add(5 * time.Minute)
  80. return test{
  81. nor: &NewOrderRequest{
  82. Identifiers: []acme.Identifier{
  83. {Type: "ip", Value: "192.168.42.42"},
  84. },
  85. NotAfter: naf,
  86. NotBefore: nbf,
  87. },
  88. nbf: nbf,
  89. naf: naf,
  90. }
  91. },
  92. "ok/ipv6": func(t *testing.T) test {
  93. nbf := time.Now().UTC().Add(time.Minute)
  94. naf := time.Now().UTC().Add(5 * time.Minute)
  95. return test{
  96. nor: &NewOrderRequest{
  97. Identifiers: []acme.Identifier{
  98. {Type: "ip", Value: "2001:db8::1"},
  99. },
  100. NotAfter: naf,
  101. NotBefore: nbf,
  102. },
  103. nbf: nbf,
  104. naf: naf,
  105. }
  106. },
  107. "ok/mixed-dns-and-ipv4": func(t *testing.T) test {
  108. nbf := time.Now().UTC().Add(time.Minute)
  109. naf := time.Now().UTC().Add(5 * time.Minute)
  110. return test{
  111. nor: &NewOrderRequest{
  112. Identifiers: []acme.Identifier{
  113. {Type: "dns", Value: "example.com"},
  114. {Type: "ip", Value: "192.168.42.42"},
  115. },
  116. NotAfter: naf,
  117. NotBefore: nbf,
  118. },
  119. nbf: nbf,
  120. naf: naf,
  121. }
  122. },
  123. "ok/mixed-ipv4-and-ipv6": func(t *testing.T) test {
  124. nbf := time.Now().UTC().Add(time.Minute)
  125. naf := time.Now().UTC().Add(5 * time.Minute)
  126. return test{
  127. nor: &NewOrderRequest{
  128. Identifiers: []acme.Identifier{
  129. {Type: "ip", Value: "192.168.42.42"},
  130. {Type: "ip", Value: "2001:db8::1"},
  131. },
  132. NotAfter: naf,
  133. NotBefore: nbf,
  134. },
  135. nbf: nbf,
  136. naf: naf,
  137. }
  138. },
  139. }
  140. for name, run := range tests {
  141. tc := run(t)
  142. t.Run(name, func(t *testing.T) {
  143. if err := tc.nor.Validate(); err != nil {
  144. if assert.NotNil(t, err) {
  145. ae, ok := err.(*acme.Error)
  146. assert.True(t, ok)
  147. assert.HasPrefix(t, ae.Error(), tc.err.Error())
  148. assert.Equals(t, ae.StatusCode(), tc.err.StatusCode())
  149. assert.Equals(t, ae.Type, tc.err.Type)
  150. }
  151. } else {
  152. if assert.Nil(t, tc.err) {
  153. if tc.nbf.IsZero() {
  154. assert.True(t, tc.nor.NotBefore.Before(time.Now().Add(time.Minute)))
  155. assert.True(t, tc.nor.NotBefore.After(time.Now().Add(-time.Minute)))
  156. } else {
  157. assert.Equals(t, tc.nor.NotBefore, tc.nbf)
  158. }
  159. if tc.naf.IsZero() {
  160. assert.True(t, tc.nor.NotAfter.Before(time.Now().Add(24*time.Hour)))
  161. assert.True(t, tc.nor.NotAfter.After(time.Now().Add(24*time.Hour-time.Minute)))
  162. } else {
  163. assert.Equals(t, tc.nor.NotAfter, tc.naf)
  164. }
  165. }
  166. }
  167. })
  168. }
  169. }
  170. func TestFinalizeRequestValidate(t *testing.T) {
  171. _csr, err := pemutil.Read("../../authority/testdata/certs/foo.csr")
  172. assert.FatalError(t, err)
  173. csr, ok := _csr.(*x509.CertificateRequest)
  174. assert.Fatal(t, ok)
  175. type test struct {
  176. fr *FinalizeRequest
  177. err *acme.Error
  178. }
  179. var tests = map[string]func(t *testing.T) test{
  180. "fail/parse-csr-error": func(t *testing.T) test {
  181. return test{
  182. fr: &FinalizeRequest{},
  183. err: acme.NewError(acme.ErrorMalformedType, "unable to parse csr: asn1: syntax error: sequence truncated"),
  184. }
  185. },
  186. "fail/invalid-csr-signature": func(t *testing.T) test {
  187. b, err := pemutil.Read("../../authority/testdata/certs/badsig.csr")
  188. assert.FatalError(t, err)
  189. c, ok := b.(*x509.CertificateRequest)
  190. assert.Fatal(t, ok)
  191. return test{
  192. fr: &FinalizeRequest{
  193. CSR: base64.RawURLEncoding.EncodeToString(c.Raw),
  194. },
  195. err: acme.NewError(acme.ErrorMalformedType, "csr failed signature check: x509: ECDSA verification failure"),
  196. }
  197. },
  198. "ok": func(t *testing.T) test {
  199. return test{
  200. fr: &FinalizeRequest{
  201. CSR: base64.RawURLEncoding.EncodeToString(csr.Raw),
  202. },
  203. }
  204. },
  205. }
  206. for name, run := range tests {
  207. tc := run(t)
  208. t.Run(name, func(t *testing.T) {
  209. if err := tc.fr.Validate(); err != nil {
  210. if assert.NotNil(t, err) {
  211. ae, ok := err.(*acme.Error)
  212. assert.True(t, ok)
  213. assert.HasPrefix(t, ae.Error(), tc.err.Error())
  214. assert.Equals(t, ae.StatusCode(), tc.err.StatusCode())
  215. assert.Equals(t, ae.Type, tc.err.Type)
  216. }
  217. } else {
  218. if assert.Nil(t, tc.err) {
  219. assert.Equals(t, tc.fr.csr.Raw, csr.Raw)
  220. }
  221. }
  222. })
  223. }
  224. }
  225. func TestHandler_GetOrder(t *testing.T) {
  226. prov := newProv()
  227. escProvName := url.PathEscape(prov.GetName())
  228. baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
  229. now := clock.Now()
  230. nbf := now
  231. naf := now.Add(24 * time.Hour)
  232. expiry := now.Add(-time.Hour)
  233. o := acme.Order{
  234. ID: "orderID",
  235. NotBefore: nbf,
  236. NotAfter: naf,
  237. Identifiers: []acme.Identifier{
  238. {
  239. Type: "dns",
  240. Value: "example.com",
  241. },
  242. {
  243. Type: "dns",
  244. Value: "*.smallstep.com",
  245. },
  246. },
  247. ExpiresAt: expiry,
  248. Status: acme.StatusInvalid,
  249. Error: acme.NewError(acme.ErrorMalformedType, "order has expired"),
  250. AuthorizationURLs: []string{
  251. fmt.Sprintf("%s/acme/%s/authz/foo", baseURL.String(), escProvName),
  252. fmt.Sprintf("%s/acme/%s/authz/bar", baseURL.String(), escProvName),
  253. fmt.Sprintf("%s/acme/%s/authz/baz", baseURL.String(), escProvName),
  254. },
  255. FinalizeURL: fmt.Sprintf("%s/acme/%s/order/orderID/finalize", baseURL.String(), escProvName),
  256. }
  257. // Request with chi context
  258. chiCtx := chi.NewRouteContext()
  259. chiCtx.URLParams.Add("ordID", o.ID)
  260. u := fmt.Sprintf("%s/acme/%s/order/%s",
  261. baseURL.String(), escProvName, o.ID)
  262. type test struct {
  263. db acme.DB
  264. ctx context.Context
  265. statusCode int
  266. err *acme.Error
  267. }
  268. var tests = map[string]func(t *testing.T) test{
  269. "fail/no-account": func(t *testing.T) test {
  270. return test{
  271. ctx: context.WithValue(context.Background(), provisionerContextKey, prov),
  272. statusCode: 400,
  273. err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
  274. }
  275. },
  276. "fail/nil-account": func(t *testing.T) test {
  277. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  278. ctx = context.WithValue(ctx, accContextKey, nil)
  279. return test{
  280. ctx: ctx,
  281. statusCode: 400,
  282. err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
  283. }
  284. },
  285. "fail/no-provisioner": func(t *testing.T) test {
  286. acc := &acme.Account{ID: "accountID"}
  287. ctx := context.WithValue(context.Background(), accContextKey, acc)
  288. return test{
  289. ctx: ctx,
  290. statusCode: 500,
  291. err: acme.NewErrorISE("provisioner does not exist"),
  292. }
  293. },
  294. "fail/nil-provisioner": func(t *testing.T) test {
  295. acc := &acme.Account{ID: "accountID"}
  296. ctx := context.WithValue(context.Background(), provisionerContextKey, nil)
  297. ctx = context.WithValue(ctx, accContextKey, acc)
  298. return test{
  299. ctx: ctx,
  300. statusCode: 500,
  301. err: acme.NewErrorISE("provisioner does not exist"),
  302. }
  303. },
  304. "fail/db.GetOrder-error": func(t *testing.T) test {
  305. acc := &acme.Account{ID: "accountID"}
  306. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  307. ctx = context.WithValue(ctx, accContextKey, acc)
  308. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  309. return test{
  310. db: &acme.MockDB{
  311. MockError: acme.NewErrorISE("force"),
  312. },
  313. ctx: ctx,
  314. statusCode: 500,
  315. err: acme.NewErrorISE("force"),
  316. }
  317. },
  318. "fail/account-id-mismatch": func(t *testing.T) test {
  319. acc := &acme.Account{ID: "accountID"}
  320. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  321. ctx = context.WithValue(ctx, accContextKey, acc)
  322. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  323. return test{
  324. db: &acme.MockDB{
  325. MockGetOrder: func(ctx context.Context, id string) (*acme.Order, error) {
  326. return &acme.Order{AccountID: "foo"}, nil
  327. },
  328. },
  329. ctx: ctx,
  330. statusCode: 401,
  331. err: acme.NewError(acme.ErrorUnauthorizedType, "account id mismatch"),
  332. }
  333. },
  334. "fail/provisioner-id-mismatch": func(t *testing.T) test {
  335. acc := &acme.Account{ID: "accountID"}
  336. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  337. ctx = context.WithValue(ctx, accContextKey, acc)
  338. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  339. return test{
  340. db: &acme.MockDB{
  341. MockGetOrder: func(ctx context.Context, id string) (*acme.Order, error) {
  342. return &acme.Order{AccountID: "accountID", ProvisionerID: "bar"}, nil
  343. },
  344. },
  345. ctx: ctx,
  346. statusCode: 401,
  347. err: acme.NewError(acme.ErrorUnauthorizedType, "provisioner id mismatch"),
  348. }
  349. },
  350. "fail/order-update-error": func(t *testing.T) test {
  351. acc := &acme.Account{ID: "accountID"}
  352. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  353. ctx = context.WithValue(ctx, accContextKey, acc)
  354. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  355. return test{
  356. db: &acme.MockDB{
  357. MockGetOrder: func(ctx context.Context, id string) (*acme.Order, error) {
  358. return &acme.Order{
  359. AccountID: "accountID",
  360. ProvisionerID: fmt.Sprintf("acme/%s", prov.GetName()),
  361. ExpiresAt: clock.Now().Add(-time.Hour),
  362. Status: acme.StatusReady,
  363. }, nil
  364. },
  365. MockUpdateOrder: func(ctx context.Context, o *acme.Order) error {
  366. return acme.NewErrorISE("force")
  367. },
  368. },
  369. ctx: ctx,
  370. statusCode: 500,
  371. err: acme.NewErrorISE("force"),
  372. }
  373. },
  374. "ok": func(t *testing.T) test {
  375. acc := &acme.Account{ID: "accountID"}
  376. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  377. ctx = context.WithValue(ctx, accContextKey, acc)
  378. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  379. ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
  380. return test{
  381. db: &acme.MockDB{
  382. MockGetOrder: func(ctx context.Context, id string) (*acme.Order, error) {
  383. return &acme.Order{
  384. ID: "orderID",
  385. AccountID: "accountID",
  386. ProvisionerID: fmt.Sprintf("acme/%s", prov.GetName()),
  387. ExpiresAt: expiry,
  388. Status: acme.StatusReady,
  389. AuthorizationIDs: []string{"foo", "bar", "baz"},
  390. NotBefore: nbf,
  391. NotAfter: naf,
  392. Identifiers: []acme.Identifier{
  393. {
  394. Type: "dns",
  395. Value: "example.com",
  396. },
  397. {
  398. Type: "dns",
  399. Value: "*.smallstep.com",
  400. },
  401. },
  402. }, nil
  403. },
  404. MockUpdateOrder: func(ctx context.Context, o *acme.Order) error {
  405. return nil
  406. },
  407. },
  408. ctx: ctx,
  409. statusCode: 200,
  410. }
  411. },
  412. }
  413. for name, run := range tests {
  414. tc := run(t)
  415. t.Run(name, func(t *testing.T) {
  416. h := &Handler{linker: NewLinker("dns", "acme"), db: tc.db}
  417. req := httptest.NewRequest("GET", u, nil)
  418. req = req.WithContext(tc.ctx)
  419. w := httptest.NewRecorder()
  420. h.GetOrder(w, req)
  421. res := w.Result()
  422. assert.Equals(t, res.StatusCode, tc.statusCode)
  423. body, err := io.ReadAll(res.Body)
  424. res.Body.Close()
  425. assert.FatalError(t, err)
  426. if res.StatusCode >= 400 && assert.NotNil(t, tc.err) {
  427. var ae acme.Error
  428. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  429. assert.Equals(t, ae.Type, tc.err.Type)
  430. assert.Equals(t, ae.Detail, tc.err.Detail)
  431. assert.Equals(t, ae.Identifier, tc.err.Identifier)
  432. assert.Equals(t, ae.Subproblems, tc.err.Subproblems)
  433. assert.Equals(t, res.Header["Content-Type"], []string{"application/problem+json"})
  434. } else {
  435. expB, err := json.Marshal(o)
  436. assert.FatalError(t, err)
  437. assert.Equals(t, bytes.TrimSpace(body), expB)
  438. assert.Equals(t, res.Header["Location"], []string{u})
  439. assert.Equals(t, res.Header["Content-Type"], []string{"application/json"})
  440. }
  441. })
  442. }
  443. }
  444. func TestHandler_newAuthorization(t *testing.T) {
  445. type test struct {
  446. az *acme.Authorization
  447. db acme.DB
  448. err *acme.Error
  449. }
  450. var tests = map[string]func(t *testing.T) test{
  451. "fail/error-db.CreateChallenge": func(t *testing.T) test {
  452. az := &acme.Authorization{
  453. AccountID: "accID",
  454. Identifier: acme.Identifier{
  455. Type: "dns",
  456. Value: "zap.internal",
  457. },
  458. }
  459. return test{
  460. db: &acme.MockDB{
  461. MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
  462. assert.Equals(t, ch.AccountID, az.AccountID)
  463. assert.Equals(t, ch.Type, acme.DNS01)
  464. assert.Equals(t, ch.Token, az.Token)
  465. assert.Equals(t, ch.Status, acme.StatusPending)
  466. assert.Equals(t, ch.Value, az.Identifier.Value)
  467. return errors.New("force")
  468. },
  469. },
  470. az: az,
  471. err: acme.NewErrorISE("error creating challenge: force"),
  472. }
  473. },
  474. "fail/error-db.CreateAuthorization": func(t *testing.T) test {
  475. az := &acme.Authorization{
  476. AccountID: "accID",
  477. Identifier: acme.Identifier{
  478. Type: "dns",
  479. Value: "zap.internal",
  480. },
  481. Status: acme.StatusPending,
  482. ExpiresAt: clock.Now(),
  483. }
  484. count := 0
  485. var ch1, ch2, ch3 **acme.Challenge
  486. return test{
  487. db: &acme.MockDB{
  488. MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
  489. switch count {
  490. case 0:
  491. ch.ID = "dns"
  492. assert.Equals(t, ch.Type, acme.DNS01)
  493. ch1 = &ch
  494. case 1:
  495. ch.ID = "http"
  496. assert.Equals(t, ch.Type, acme.HTTP01)
  497. ch2 = &ch
  498. case 2:
  499. ch.ID = "tls"
  500. assert.Equals(t, ch.Type, acme.TLSALPN01)
  501. ch3 = &ch
  502. default:
  503. assert.FatalError(t, errors.New("test logic error"))
  504. return errors.New("force")
  505. }
  506. count++
  507. assert.Equals(t, ch.AccountID, az.AccountID)
  508. assert.Equals(t, ch.Token, az.Token)
  509. assert.Equals(t, ch.Status, acme.StatusPending)
  510. assert.Equals(t, ch.Value, az.Identifier.Value)
  511. return nil
  512. },
  513. MockCreateAuthorization: func(ctx context.Context, _az *acme.Authorization) error {
  514. assert.Equals(t, _az.AccountID, az.AccountID)
  515. assert.Equals(t, _az.Token, az.Token)
  516. assert.Equals(t, _az.Status, acme.StatusPending)
  517. assert.Equals(t, _az.Identifier, az.Identifier)
  518. assert.Equals(t, _az.ExpiresAt, az.ExpiresAt)
  519. assert.Equals(t, _az.Challenges, []*acme.Challenge{*ch1, *ch2, *ch3})
  520. assert.Equals(t, _az.Wildcard, false)
  521. return errors.New("force")
  522. },
  523. },
  524. az: az,
  525. err: acme.NewErrorISE("error creating authorization: force"),
  526. }
  527. },
  528. "ok/no-wildcard": func(t *testing.T) test {
  529. az := &acme.Authorization{
  530. AccountID: "accID",
  531. Identifier: acme.Identifier{
  532. Type: "dns",
  533. Value: "zap.internal",
  534. },
  535. Status: acme.StatusPending,
  536. ExpiresAt: clock.Now(),
  537. }
  538. count := 0
  539. var ch1, ch2, ch3 **acme.Challenge
  540. return test{
  541. db: &acme.MockDB{
  542. MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
  543. switch count {
  544. case 0:
  545. ch.ID = "dns"
  546. assert.Equals(t, ch.Type, acme.DNS01)
  547. ch1 = &ch
  548. case 1:
  549. ch.ID = "http"
  550. assert.Equals(t, ch.Type, acme.HTTP01)
  551. ch2 = &ch
  552. case 2:
  553. ch.ID = "tls"
  554. assert.Equals(t, ch.Type, acme.TLSALPN01)
  555. ch3 = &ch
  556. default:
  557. assert.FatalError(t, errors.New("test logic error"))
  558. return errors.New("force")
  559. }
  560. count++
  561. assert.Equals(t, ch.AccountID, az.AccountID)
  562. assert.Equals(t, ch.Token, az.Token)
  563. assert.Equals(t, ch.Status, acme.StatusPending)
  564. assert.Equals(t, ch.Value, az.Identifier.Value)
  565. return nil
  566. },
  567. MockCreateAuthorization: func(ctx context.Context, _az *acme.Authorization) error {
  568. assert.Equals(t, _az.AccountID, az.AccountID)
  569. assert.Equals(t, _az.Token, az.Token)
  570. assert.Equals(t, _az.Status, acme.StatusPending)
  571. assert.Equals(t, _az.Identifier, az.Identifier)
  572. assert.Equals(t, _az.ExpiresAt, az.ExpiresAt)
  573. assert.Equals(t, _az.Challenges, []*acme.Challenge{*ch1, *ch2, *ch3})
  574. assert.Equals(t, _az.Wildcard, false)
  575. return nil
  576. },
  577. },
  578. az: az,
  579. }
  580. },
  581. "ok/wildcard": func(t *testing.T) test {
  582. az := &acme.Authorization{
  583. AccountID: "accID",
  584. Identifier: acme.Identifier{
  585. Type: "dns",
  586. Value: "*.zap.internal",
  587. },
  588. Status: acme.StatusPending,
  589. ExpiresAt: clock.Now(),
  590. }
  591. var ch1 **acme.Challenge
  592. return test{
  593. db: &acme.MockDB{
  594. MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
  595. ch.ID = "dns"
  596. assert.Equals(t, ch.Type, acme.DNS01)
  597. assert.Equals(t, ch.AccountID, az.AccountID)
  598. assert.Equals(t, ch.Token, az.Token)
  599. assert.Equals(t, ch.Status, acme.StatusPending)
  600. assert.Equals(t, ch.Value, "zap.internal")
  601. ch1 = &ch
  602. return nil
  603. },
  604. MockCreateAuthorization: func(ctx context.Context, _az *acme.Authorization) error {
  605. assert.Equals(t, _az.AccountID, az.AccountID)
  606. assert.Equals(t, _az.Token, az.Token)
  607. assert.Equals(t, _az.Status, acme.StatusPending)
  608. assert.Equals(t, _az.Identifier, acme.Identifier{
  609. Type: "dns",
  610. Value: "zap.internal",
  611. })
  612. assert.Equals(t, _az.ExpiresAt, az.ExpiresAt)
  613. assert.Equals(t, _az.Challenges, []*acme.Challenge{*ch1})
  614. assert.Equals(t, _az.Wildcard, true)
  615. return nil
  616. },
  617. },
  618. az: az,
  619. }
  620. },
  621. }
  622. for name, run := range tests {
  623. t.Run(name, func(t *testing.T) {
  624. tc := run(t)
  625. h := &Handler{db: tc.db}
  626. if err := h.newAuthorization(context.Background(), tc.az); err != nil {
  627. if assert.NotNil(t, tc.err) {
  628. switch k := err.(type) {
  629. case *acme.Error:
  630. assert.Equals(t, k.Type, tc.err.Type)
  631. assert.Equals(t, k.Detail, tc.err.Detail)
  632. assert.Equals(t, k.Status, tc.err.Status)
  633. assert.Equals(t, k.Err.Error(), tc.err.Err.Error())
  634. assert.Equals(t, k.Detail, tc.err.Detail)
  635. default:
  636. assert.FatalError(t, errors.New("unexpected error type"))
  637. }
  638. }
  639. } else {
  640. assert.Nil(t, tc.err)
  641. }
  642. })
  643. }
  644. }
  645. func TestHandler_NewOrder(t *testing.T) {
  646. // Request with chi context
  647. prov := newProv()
  648. escProvName := url.PathEscape(prov.GetName())
  649. baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
  650. u := fmt.Sprintf("%s/acme/%s/order/ordID",
  651. baseURL.String(), escProvName)
  652. type test struct {
  653. ca acme.CertificateAuthority
  654. db acme.DB
  655. ctx context.Context
  656. nor *NewOrderRequest
  657. statusCode int
  658. vr func(t *testing.T, o *acme.Order)
  659. err *acme.Error
  660. }
  661. var tests = map[string]func(t *testing.T) test{
  662. "fail/no-account": func(t *testing.T) test {
  663. return test{
  664. ctx: context.WithValue(context.Background(), provisionerContextKey, prov),
  665. statusCode: 400,
  666. err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
  667. }
  668. },
  669. "fail/nil-account": func(t *testing.T) test {
  670. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  671. ctx = context.WithValue(ctx, accContextKey, nil)
  672. return test{
  673. ctx: ctx,
  674. statusCode: 400,
  675. err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
  676. }
  677. },
  678. "fail/no-provisioner": func(t *testing.T) test {
  679. acc := &acme.Account{ID: "accountID"}
  680. ctx := context.WithValue(context.Background(), accContextKey, acc)
  681. return test{
  682. ctx: ctx,
  683. statusCode: 500,
  684. err: acme.NewErrorISE("provisioner does not exist"),
  685. }
  686. },
  687. "fail/nil-provisioner": func(t *testing.T) test {
  688. acc := &acme.Account{ID: "accountID"}
  689. ctx := context.WithValue(context.Background(), provisionerContextKey, nil)
  690. ctx = context.WithValue(ctx, accContextKey, acc)
  691. return test{
  692. ctx: ctx,
  693. statusCode: 500,
  694. err: acme.NewErrorISE("provisioner does not exist"),
  695. }
  696. },
  697. "fail/no-payload": func(t *testing.T) test {
  698. acc := &acme.Account{ID: "accountID"}
  699. ctx := context.WithValue(context.Background(), accContextKey, acc)
  700. ctx = context.WithValue(ctx, provisionerContextKey, prov)
  701. return test{
  702. ctx: ctx,
  703. statusCode: 500,
  704. err: acme.NewErrorISE("payload does not exist"),
  705. }
  706. },
  707. "fail/nil-payload": func(t *testing.T) test {
  708. acc := &acme.Account{ID: "accountID"}
  709. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  710. ctx = context.WithValue(ctx, accContextKey, acc)
  711. ctx = context.WithValue(ctx, payloadContextKey, nil)
  712. return test{
  713. ctx: ctx,
  714. statusCode: 500,
  715. err: acme.NewErrorISE("paylod does not exist"),
  716. }
  717. },
  718. "fail/unmarshal-payload-error": func(t *testing.T) test {
  719. acc := &acme.Account{ID: "accID"}
  720. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  721. ctx = context.WithValue(ctx, accContextKey, acc)
  722. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{})
  723. return test{
  724. ctx: ctx,
  725. statusCode: 400,
  726. err: acme.NewError(acme.ErrorMalformedType, "failed to unmarshal new-order request payload: unexpected end of JSON input"),
  727. }
  728. },
  729. "fail/malformed-payload-error": func(t *testing.T) test {
  730. acc := &acme.Account{ID: "accID"}
  731. fr := &NewOrderRequest{}
  732. b, err := json.Marshal(fr)
  733. assert.FatalError(t, err)
  734. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  735. ctx = context.WithValue(ctx, accContextKey, acc)
  736. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  737. return test{
  738. ctx: ctx,
  739. statusCode: 400,
  740. err: acme.NewError(acme.ErrorMalformedType, "identifiers list cannot be empty"),
  741. }
  742. },
  743. "fail/error-h.newAuthorization": func(t *testing.T) test {
  744. acc := &acme.Account{ID: "accID"}
  745. fr := &NewOrderRequest{
  746. Identifiers: []acme.Identifier{
  747. {Type: "dns", Value: "zap.internal"},
  748. },
  749. }
  750. b, err := json.Marshal(fr)
  751. assert.FatalError(t, err)
  752. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  753. ctx = context.WithValue(ctx, accContextKey, acc)
  754. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  755. return test{
  756. ctx: ctx,
  757. statusCode: 500,
  758. ca: &mockCA{},
  759. db: &acme.MockDB{
  760. MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
  761. assert.Equals(t, ch.AccountID, "accID")
  762. assert.Equals(t, ch.Type, acme.DNS01)
  763. assert.NotEquals(t, ch.Token, "")
  764. assert.Equals(t, ch.Status, acme.StatusPending)
  765. assert.Equals(t, ch.Value, "zap.internal")
  766. return errors.New("force")
  767. },
  768. MockGetExternalAccountKeyByAccountID: func(ctx context.Context, provisionerID, accountID string) (*acme.ExternalAccountKey, error) {
  769. assert.Equals(t, prov.GetID(), provisionerID)
  770. assert.Equals(t, "accID", accountID)
  771. return nil, nil
  772. },
  773. },
  774. err: acme.NewErrorISE("error creating challenge: force"),
  775. }
  776. },
  777. "fail/error-db.CreateOrder": func(t *testing.T) test {
  778. acc := &acme.Account{ID: "accID"}
  779. fr := &NewOrderRequest{
  780. Identifiers: []acme.Identifier{
  781. {Type: "dns", Value: "zap.internal"},
  782. },
  783. }
  784. b, err := json.Marshal(fr)
  785. assert.FatalError(t, err)
  786. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  787. ctx = context.WithValue(ctx, accContextKey, acc)
  788. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  789. var (
  790. ch1, ch2, ch3 **acme.Challenge
  791. az1ID *string
  792. count = 0
  793. )
  794. return test{
  795. ctx: ctx,
  796. statusCode: 500,
  797. ca: &mockCA{},
  798. db: &acme.MockDB{
  799. MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
  800. switch count {
  801. case 0:
  802. ch.ID = "dns"
  803. assert.Equals(t, ch.Type, acme.DNS01)
  804. ch1 = &ch
  805. case 1:
  806. ch.ID = "http"
  807. assert.Equals(t, ch.Type, acme.HTTP01)
  808. ch2 = &ch
  809. case 2:
  810. ch.ID = "tls"
  811. assert.Equals(t, ch.Type, acme.TLSALPN01)
  812. ch3 = &ch
  813. default:
  814. assert.FatalError(t, errors.New("test logic error"))
  815. return errors.New("force")
  816. }
  817. count++
  818. assert.Equals(t, ch.AccountID, "accID")
  819. assert.NotEquals(t, ch.Token, "")
  820. assert.Equals(t, ch.Status, acme.StatusPending)
  821. assert.Equals(t, ch.Value, "zap.internal")
  822. return nil
  823. },
  824. MockCreateAuthorization: func(ctx context.Context, az *acme.Authorization) error {
  825. az.ID = "az1ID"
  826. az1ID = &az.ID
  827. assert.Equals(t, az.AccountID, "accID")
  828. assert.NotEquals(t, az.Token, "")
  829. assert.Equals(t, az.Status, acme.StatusPending)
  830. assert.Equals(t, az.Identifier, fr.Identifiers[0])
  831. assert.Equals(t, az.Challenges, []*acme.Challenge{*ch1, *ch2, *ch3})
  832. assert.Equals(t, az.Wildcard, false)
  833. return nil
  834. },
  835. MockCreateOrder: func(ctx context.Context, o *acme.Order) error {
  836. assert.Equals(t, o.AccountID, "accID")
  837. assert.Equals(t, o.ProvisionerID, prov.GetID())
  838. assert.Equals(t, o.Status, acme.StatusPending)
  839. assert.Equals(t, o.Identifiers, fr.Identifiers)
  840. assert.Equals(t, o.AuthorizationIDs, []string{*az1ID})
  841. return errors.New("force")
  842. },
  843. MockGetExternalAccountKeyByAccountID: func(ctx context.Context, provisionerID, accountID string) (*acme.ExternalAccountKey, error) {
  844. assert.Equals(t, prov.GetID(), provisionerID)
  845. assert.Equals(t, "accID", accountID)
  846. return nil, nil
  847. },
  848. },
  849. err: acme.NewErrorISE("error creating order: force"),
  850. }
  851. },
  852. "ok/multiple-authz": func(t *testing.T) test {
  853. acc := &acme.Account{ID: "accID"}
  854. nor := &NewOrderRequest{
  855. Identifiers: []acme.Identifier{
  856. {Type: "dns", Value: "zap.internal"},
  857. {Type: "dns", Value: "*.zar.internal"},
  858. },
  859. }
  860. b, err := json.Marshal(nor)
  861. assert.FatalError(t, err)
  862. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  863. ctx = context.WithValue(ctx, accContextKey, acc)
  864. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  865. ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
  866. var (
  867. ch1, ch2, ch3, ch4 **acme.Challenge
  868. az1ID, az2ID *string
  869. chCount, azCount = 0, 0
  870. )
  871. return test{
  872. ctx: ctx,
  873. statusCode: 201,
  874. nor: nor,
  875. ca: &mockCA{},
  876. db: &acme.MockDB{
  877. MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
  878. switch chCount {
  879. case 0:
  880. ch.ID = "dns"
  881. assert.Equals(t, ch.Type, acme.DNS01)
  882. assert.Equals(t, ch.Value, "zap.internal")
  883. ch1 = &ch
  884. case 1:
  885. ch.ID = "http"
  886. assert.Equals(t, ch.Type, acme.HTTP01)
  887. assert.Equals(t, ch.Value, "zap.internal")
  888. ch2 = &ch
  889. case 2:
  890. ch.ID = "tls"
  891. assert.Equals(t, ch.Type, acme.TLSALPN01)
  892. assert.Equals(t, ch.Value, "zap.internal")
  893. ch3 = &ch
  894. case 3:
  895. ch.ID = "dns"
  896. assert.Equals(t, ch.Type, acme.DNS01)
  897. assert.Equals(t, ch.Value, "zar.internal")
  898. ch4 = &ch
  899. default:
  900. assert.FatalError(t, errors.New("test logic error"))
  901. return errors.New("force")
  902. }
  903. chCount++
  904. assert.Equals(t, ch.AccountID, "accID")
  905. assert.NotEquals(t, ch.Token, "")
  906. assert.Equals(t, ch.Status, acme.StatusPending)
  907. return nil
  908. },
  909. MockCreateAuthorization: func(ctx context.Context, az *acme.Authorization) error {
  910. switch azCount {
  911. case 0:
  912. az.ID = "az1ID"
  913. az1ID = &az.ID
  914. assert.Equals(t, az.Identifier, nor.Identifiers[0])
  915. assert.Equals(t, az.Wildcard, false)
  916. assert.Equals(t, az.Challenges, []*acme.Challenge{*ch1, *ch2, *ch3})
  917. case 1:
  918. az.ID = "az2ID"
  919. az2ID = &az.ID
  920. assert.Equals(t, az.Identifier, acme.Identifier{
  921. Type: acme.DNS,
  922. Value: "zar.internal",
  923. })
  924. assert.Equals(t, az.Wildcard, true)
  925. assert.Equals(t, az.Challenges, []*acme.Challenge{*ch4})
  926. default:
  927. assert.FatalError(t, errors.New("test logic error"))
  928. return errors.New("force")
  929. }
  930. azCount++
  931. assert.Equals(t, az.AccountID, "accID")
  932. assert.NotEquals(t, az.Token, "")
  933. assert.Equals(t, az.Status, acme.StatusPending)
  934. return nil
  935. },
  936. MockCreateOrder: func(ctx context.Context, o *acme.Order) error {
  937. o.ID = "ordID"
  938. assert.Equals(t, o.AccountID, "accID")
  939. assert.Equals(t, o.ProvisionerID, prov.GetID())
  940. assert.Equals(t, o.Status, acme.StatusPending)
  941. assert.Equals(t, o.Identifiers, nor.Identifiers)
  942. assert.Equals(t, o.AuthorizationIDs, []string{*az1ID, *az2ID})
  943. return nil
  944. },
  945. MockGetExternalAccountKeyByAccountID: func(ctx context.Context, provisionerID, accountID string) (*acme.ExternalAccountKey, error) {
  946. assert.Equals(t, prov.GetID(), provisionerID)
  947. assert.Equals(t, "accID", accountID)
  948. return nil, nil
  949. },
  950. },
  951. vr: func(t *testing.T, o *acme.Order) {
  952. now := clock.Now()
  953. testBufferDur := 5 * time.Second
  954. orderExpiry := now.Add(defaultOrderExpiry)
  955. expNbf := now.Add(-defaultOrderBackdate)
  956. expNaf := now.Add(prov.DefaultTLSCertDuration())
  957. assert.Equals(t, o.ID, "ordID")
  958. assert.Equals(t, o.Status, acme.StatusPending)
  959. assert.Equals(t, o.Identifiers, nor.Identifiers)
  960. assert.Equals(t, o.AuthorizationURLs, []string{
  961. fmt.Sprintf("%s/acme/%s/authz/az1ID", baseURL.String(), escProvName),
  962. fmt.Sprintf("%s/acme/%s/authz/az2ID", baseURL.String(), escProvName),
  963. })
  964. assert.True(t, o.NotBefore.Add(-testBufferDur).Before(expNbf))
  965. assert.True(t, o.NotBefore.Add(testBufferDur).After(expNbf))
  966. assert.True(t, o.NotAfter.Add(-testBufferDur).Before(expNaf))
  967. assert.True(t, o.NotAfter.Add(testBufferDur).After(expNaf))
  968. assert.True(t, o.ExpiresAt.Add(-testBufferDur).Before(orderExpiry))
  969. assert.True(t, o.ExpiresAt.Add(testBufferDur).After(orderExpiry))
  970. },
  971. }
  972. },
  973. "ok/default-naf-nbf": func(t *testing.T) test {
  974. acc := &acme.Account{ID: "accID"}
  975. nor := &NewOrderRequest{
  976. Identifiers: []acme.Identifier{
  977. {Type: "dns", Value: "zap.internal"},
  978. },
  979. }
  980. b, err := json.Marshal(nor)
  981. assert.FatalError(t, err)
  982. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  983. ctx = context.WithValue(ctx, accContextKey, acc)
  984. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  985. ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
  986. var (
  987. ch1, ch2, ch3 **acme.Challenge
  988. az1ID *string
  989. count = 0
  990. )
  991. return test{
  992. ctx: ctx,
  993. statusCode: 201,
  994. nor: nor,
  995. ca: &mockCA{},
  996. db: &acme.MockDB{
  997. MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
  998. switch count {
  999. case 0:
  1000. ch.ID = "dns"
  1001. assert.Equals(t, ch.Type, acme.DNS01)
  1002. ch1 = &ch
  1003. case 1:
  1004. ch.ID = "http"
  1005. assert.Equals(t, ch.Type, acme.HTTP01)
  1006. ch2 = &ch
  1007. case 2:
  1008. ch.ID = "tls"
  1009. assert.Equals(t, ch.Type, acme.TLSALPN01)
  1010. ch3 = &ch
  1011. default:
  1012. assert.FatalError(t, errors.New("test logic error"))
  1013. return errors.New("force")
  1014. }
  1015. count++
  1016. assert.Equals(t, ch.AccountID, "accID")
  1017. assert.NotEquals(t, ch.Token, "")
  1018. assert.Equals(t, ch.Status, acme.StatusPending)
  1019. assert.Equals(t, ch.Value, "zap.internal")
  1020. return nil
  1021. },
  1022. MockCreateAuthorization: func(ctx context.Context, az *acme.Authorization) error {
  1023. az.ID = "az1ID"
  1024. az1ID = &az.ID
  1025. assert.Equals(t, az.AccountID, "accID")
  1026. assert.NotEquals(t, az.Token, "")
  1027. assert.Equals(t, az.Status, acme.StatusPending)
  1028. assert.Equals(t, az.Identifier, nor.Identifiers[0])
  1029. assert.Equals(t, az.Challenges, []*acme.Challenge{*ch1, *ch2, *ch3})
  1030. assert.Equals(t, az.Wildcard, false)
  1031. return nil
  1032. },
  1033. MockCreateOrder: func(ctx context.Context, o *acme.Order) error {
  1034. o.ID = "ordID"
  1035. assert.Equals(t, o.AccountID, "accID")
  1036. assert.Equals(t, o.ProvisionerID, prov.GetID())
  1037. assert.Equals(t, o.Status, acme.StatusPending)
  1038. assert.Equals(t, o.Identifiers, nor.Identifiers)
  1039. assert.Equals(t, o.AuthorizationIDs, []string{*az1ID})
  1040. return nil
  1041. },
  1042. MockGetExternalAccountKeyByAccountID: func(ctx context.Context, provisionerID, accountID string) (*acme.ExternalAccountKey, error) {
  1043. assert.Equals(t, prov.GetID(), provisionerID)
  1044. assert.Equals(t, "accID", accountID)
  1045. return nil, nil
  1046. },
  1047. },
  1048. vr: func(t *testing.T, o *acme.Order) {
  1049. now := clock.Now()
  1050. testBufferDur := 5 * time.Second
  1051. orderExpiry := now.Add(defaultOrderExpiry)
  1052. expNbf := now.Add(-defaultOrderBackdate)
  1053. expNaf := now.Add(prov.DefaultTLSCertDuration())
  1054. assert.Equals(t, o.ID, "ordID")
  1055. assert.Equals(t, o.Status, acme.StatusPending)
  1056. assert.Equals(t, o.Identifiers, nor.Identifiers)
  1057. assert.Equals(t, o.AuthorizationURLs, []string{fmt.Sprintf("%s/acme/%s/authz/az1ID", baseURL.String(), escProvName)})
  1058. assert.True(t, o.NotBefore.Add(-testBufferDur).Before(expNbf))
  1059. assert.True(t, o.NotBefore.Add(testBufferDur).After(expNbf))
  1060. assert.True(t, o.NotAfter.Add(-testBufferDur).Before(expNaf))
  1061. assert.True(t, o.NotAfter.Add(testBufferDur).After(expNaf))
  1062. assert.True(t, o.ExpiresAt.Add(-testBufferDur).Before(orderExpiry))
  1063. assert.True(t, o.ExpiresAt.Add(testBufferDur).After(orderExpiry))
  1064. },
  1065. }
  1066. },
  1067. "ok/nbf-no-naf": func(t *testing.T) test {
  1068. now := clock.Now()
  1069. expNbf := now.Add(10 * time.Minute)
  1070. acc := &acme.Account{ID: "accID"}
  1071. nor := &NewOrderRequest{
  1072. Identifiers: []acme.Identifier{
  1073. {Type: "dns", Value: "zap.internal"},
  1074. },
  1075. NotBefore: expNbf,
  1076. }
  1077. b, err := json.Marshal(nor)
  1078. assert.FatalError(t, err)
  1079. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1080. ctx = context.WithValue(ctx, accContextKey, acc)
  1081. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  1082. ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
  1083. var (
  1084. ch1, ch2, ch3 **acme.Challenge
  1085. az1ID *string
  1086. count = 0
  1087. )
  1088. return test{
  1089. ctx: ctx,
  1090. statusCode: 201,
  1091. nor: nor,
  1092. ca: &mockCA{},
  1093. db: &acme.MockDB{
  1094. MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
  1095. switch count {
  1096. case 0:
  1097. ch.ID = "dns"
  1098. assert.Equals(t, ch.Type, acme.DNS01)
  1099. ch1 = &ch
  1100. case 1:
  1101. ch.ID = "http"
  1102. assert.Equals(t, ch.Type, acme.HTTP01)
  1103. ch2 = &ch
  1104. case 2:
  1105. ch.ID = "tls"
  1106. assert.Equals(t, ch.Type, acme.TLSALPN01)
  1107. ch3 = &ch
  1108. default:
  1109. assert.FatalError(t, errors.New("test logic error"))
  1110. return errors.New("force")
  1111. }
  1112. count++
  1113. assert.Equals(t, ch.AccountID, "accID")
  1114. assert.NotEquals(t, ch.Token, "")
  1115. assert.Equals(t, ch.Status, acme.StatusPending)
  1116. assert.Equals(t, ch.Value, "zap.internal")
  1117. return nil
  1118. },
  1119. MockCreateAuthorization: func(ctx context.Context, az *acme.Authorization) error {
  1120. az.ID = "az1ID"
  1121. az1ID = &az.ID
  1122. assert.Equals(t, az.AccountID, "accID")
  1123. assert.NotEquals(t, az.Token, "")
  1124. assert.Equals(t, az.Status, acme.StatusPending)
  1125. assert.Equals(t, az.Identifier, nor.Identifiers[0])
  1126. assert.Equals(t, az.Challenges, []*acme.Challenge{*ch1, *ch2, *ch3})
  1127. assert.Equals(t, az.Wildcard, false)
  1128. return nil
  1129. },
  1130. MockCreateOrder: func(ctx context.Context, o *acme.Order) error {
  1131. o.ID = "ordID"
  1132. assert.Equals(t, o.AccountID, "accID")
  1133. assert.Equals(t, o.ProvisionerID, prov.GetID())
  1134. assert.Equals(t, o.Status, acme.StatusPending)
  1135. assert.Equals(t, o.Identifiers, nor.Identifiers)
  1136. assert.Equals(t, o.AuthorizationIDs, []string{*az1ID})
  1137. return nil
  1138. },
  1139. MockGetExternalAccountKeyByAccountID: func(ctx context.Context, provisionerID, accountID string) (*acme.ExternalAccountKey, error) {
  1140. assert.Equals(t, prov.GetID(), provisionerID)
  1141. assert.Equals(t, "accID", accountID)
  1142. return nil, nil
  1143. },
  1144. },
  1145. vr: func(t *testing.T, o *acme.Order) {
  1146. now := clock.Now()
  1147. testBufferDur := 5 * time.Second
  1148. orderExpiry := now.Add(defaultOrderExpiry)
  1149. expNaf := expNbf.Add(prov.DefaultTLSCertDuration())
  1150. assert.Equals(t, o.ID, "ordID")
  1151. assert.Equals(t, o.Status, acme.StatusPending)
  1152. assert.Equals(t, o.Identifiers, nor.Identifiers)
  1153. assert.Equals(t, o.AuthorizationURLs, []string{fmt.Sprintf("%s/acme/%s/authz/az1ID", baseURL.String(), escProvName)})
  1154. assert.True(t, o.NotBefore.Add(-testBufferDur).Before(expNbf))
  1155. assert.True(t, o.NotBefore.Add(testBufferDur).After(expNbf))
  1156. assert.True(t, o.NotAfter.Add(-testBufferDur).Before(expNaf))
  1157. assert.True(t, o.NotAfter.Add(testBufferDur).After(expNaf))
  1158. assert.True(t, o.ExpiresAt.Add(-testBufferDur).Before(orderExpiry))
  1159. assert.True(t, o.ExpiresAt.Add(testBufferDur).After(orderExpiry))
  1160. },
  1161. }
  1162. },
  1163. "ok/naf-no-nbf": func(t *testing.T) test {
  1164. now := clock.Now()
  1165. expNaf := now.Add(15 * time.Minute)
  1166. acc := &acme.Account{ID: "accID"}
  1167. nor := &NewOrderRequest{
  1168. Identifiers: []acme.Identifier{
  1169. {Type: "dns", Value: "zap.internal"},
  1170. },
  1171. NotAfter: expNaf,
  1172. }
  1173. b, err := json.Marshal(nor)
  1174. assert.FatalError(t, err)
  1175. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1176. ctx = context.WithValue(ctx, accContextKey, acc)
  1177. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  1178. ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
  1179. var (
  1180. ch1, ch2, ch3 **acme.Challenge
  1181. az1ID *string
  1182. count = 0
  1183. )
  1184. return test{
  1185. ctx: ctx,
  1186. statusCode: 201,
  1187. nor: nor,
  1188. ca: &mockCA{},
  1189. db: &acme.MockDB{
  1190. MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
  1191. switch count {
  1192. case 0:
  1193. ch.ID = "dns"
  1194. assert.Equals(t, ch.Type, acme.DNS01)
  1195. ch1 = &ch
  1196. case 1:
  1197. ch.ID = "http"
  1198. assert.Equals(t, ch.Type, acme.HTTP01)
  1199. ch2 = &ch
  1200. case 2:
  1201. ch.ID = "tls"
  1202. assert.Equals(t, ch.Type, acme.TLSALPN01)
  1203. ch3 = &ch
  1204. default:
  1205. assert.FatalError(t, errors.New("test logic error"))
  1206. return errors.New("force")
  1207. }
  1208. count++
  1209. assert.Equals(t, ch.AccountID, "accID")
  1210. assert.NotEquals(t, ch.Token, "")
  1211. assert.Equals(t, ch.Status, acme.StatusPending)
  1212. assert.Equals(t, ch.Value, "zap.internal")
  1213. return nil
  1214. },
  1215. MockCreateAuthorization: func(ctx context.Context, az *acme.Authorization) error {
  1216. az.ID = "az1ID"
  1217. az1ID = &az.ID
  1218. assert.Equals(t, az.AccountID, "accID")
  1219. assert.NotEquals(t, az.Token, "")
  1220. assert.Equals(t, az.Status, acme.StatusPending)
  1221. assert.Equals(t, az.Identifier, nor.Identifiers[0])
  1222. assert.Equals(t, az.Challenges, []*acme.Challenge{*ch1, *ch2, *ch3})
  1223. assert.Equals(t, az.Wildcard, false)
  1224. return nil
  1225. },
  1226. MockCreateOrder: func(ctx context.Context, o *acme.Order) error {
  1227. o.ID = "ordID"
  1228. assert.Equals(t, o.AccountID, "accID")
  1229. assert.Equals(t, o.ProvisionerID, prov.GetID())
  1230. assert.Equals(t, o.Status, acme.StatusPending)
  1231. assert.Equals(t, o.Identifiers, nor.Identifiers)
  1232. assert.Equals(t, o.AuthorizationIDs, []string{*az1ID})
  1233. return nil
  1234. },
  1235. MockGetExternalAccountKeyByAccountID: func(ctx context.Context, provisionerID, accountID string) (*acme.ExternalAccountKey, error) {
  1236. assert.Equals(t, prov.GetID(), provisionerID)
  1237. assert.Equals(t, "accID", accountID)
  1238. return nil, nil
  1239. },
  1240. },
  1241. vr: func(t *testing.T, o *acme.Order) {
  1242. testBufferDur := 5 * time.Second
  1243. orderExpiry := now.Add(defaultOrderExpiry)
  1244. expNbf := now.Add(-defaultOrderBackdate)
  1245. assert.Equals(t, o.ID, "ordID")
  1246. assert.Equals(t, o.Status, acme.StatusPending)
  1247. assert.Equals(t, o.Identifiers, nor.Identifiers)
  1248. assert.Equals(t, o.AuthorizationURLs, []string{fmt.Sprintf("%s/acme/%s/authz/az1ID", baseURL.String(), escProvName)})
  1249. assert.True(t, o.NotBefore.Add(-testBufferDur).Before(expNbf))
  1250. assert.True(t, o.NotBefore.Add(testBufferDur).After(expNbf))
  1251. assert.True(t, o.NotAfter.Add(-testBufferDur).Before(expNaf))
  1252. assert.True(t, o.NotAfter.Add(testBufferDur).After(expNaf))
  1253. assert.True(t, o.ExpiresAt.Add(-testBufferDur).Before(orderExpiry))
  1254. assert.True(t, o.ExpiresAt.Add(testBufferDur).After(orderExpiry))
  1255. },
  1256. }
  1257. },
  1258. "ok/naf-nbf": func(t *testing.T) test {
  1259. now := clock.Now()
  1260. expNbf := now.Add(5 * time.Minute)
  1261. expNaf := now.Add(15 * time.Minute)
  1262. acc := &acme.Account{ID: "accID"}
  1263. nor := &NewOrderRequest{
  1264. Identifiers: []acme.Identifier{
  1265. {Type: "dns", Value: "zap.internal"},
  1266. },
  1267. NotBefore: expNbf,
  1268. NotAfter: expNaf,
  1269. }
  1270. b, err := json.Marshal(nor)
  1271. assert.FatalError(t, err)
  1272. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1273. ctx = context.WithValue(ctx, accContextKey, acc)
  1274. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  1275. ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
  1276. var (
  1277. ch1, ch2, ch3 **acme.Challenge
  1278. az1ID *string
  1279. count = 0
  1280. )
  1281. return test{
  1282. ctx: ctx,
  1283. statusCode: 201,
  1284. nor: nor,
  1285. ca: &mockCA{},
  1286. db: &acme.MockDB{
  1287. MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
  1288. switch count {
  1289. case 0:
  1290. ch.ID = "dns"
  1291. assert.Equals(t, ch.Type, acme.DNS01)
  1292. ch1 = &ch
  1293. case 1:
  1294. ch.ID = "http"
  1295. assert.Equals(t, ch.Type, acme.HTTP01)
  1296. ch2 = &ch
  1297. case 2:
  1298. ch.ID = "tls"
  1299. assert.Equals(t, ch.Type, acme.TLSALPN01)
  1300. ch3 = &ch
  1301. default:
  1302. assert.FatalError(t, errors.New("test logic error"))
  1303. return errors.New("force")
  1304. }
  1305. count++
  1306. assert.Equals(t, ch.AccountID, "accID")
  1307. assert.NotEquals(t, ch.Token, "")
  1308. assert.Equals(t, ch.Status, acme.StatusPending)
  1309. assert.Equals(t, ch.Value, "zap.internal")
  1310. return nil
  1311. },
  1312. MockCreateAuthorization: func(ctx context.Context, az *acme.Authorization) error {
  1313. az.ID = "az1ID"
  1314. az1ID = &az.ID
  1315. assert.Equals(t, az.AccountID, "accID")
  1316. assert.NotEquals(t, az.Token, "")
  1317. assert.Equals(t, az.Status, acme.StatusPending)
  1318. assert.Equals(t, az.Identifier, nor.Identifiers[0])
  1319. assert.Equals(t, az.Challenges, []*acme.Challenge{*ch1, *ch2, *ch3})
  1320. assert.Equals(t, az.Wildcard, false)
  1321. return nil
  1322. },
  1323. MockCreateOrder: func(ctx context.Context, o *acme.Order) error {
  1324. o.ID = "ordID"
  1325. assert.Equals(t, o.AccountID, "accID")
  1326. assert.Equals(t, o.ProvisionerID, prov.GetID())
  1327. assert.Equals(t, o.Status, acme.StatusPending)
  1328. assert.Equals(t, o.Identifiers, nor.Identifiers)
  1329. assert.Equals(t, o.AuthorizationIDs, []string{*az1ID})
  1330. return nil
  1331. },
  1332. MockGetExternalAccountKeyByAccountID: func(ctx context.Context, provisionerID, accountID string) (*acme.ExternalAccountKey, error) {
  1333. assert.Equals(t, prov.GetID(), provisionerID)
  1334. assert.Equals(t, "accID", accountID)
  1335. return nil, nil
  1336. },
  1337. },
  1338. vr: func(t *testing.T, o *acme.Order) {
  1339. testBufferDur := 5 * time.Second
  1340. orderExpiry := now.Add(defaultOrderExpiry)
  1341. assert.Equals(t, o.ID, "ordID")
  1342. assert.Equals(t, o.Status, acme.StatusPending)
  1343. assert.Equals(t, o.Identifiers, nor.Identifiers)
  1344. assert.Equals(t, o.AuthorizationURLs, []string{fmt.Sprintf("%s/acme/%s/authz/az1ID", baseURL.String(), escProvName)})
  1345. assert.True(t, o.NotBefore.Add(-testBufferDur).Before(expNbf))
  1346. assert.True(t, o.NotBefore.Add(testBufferDur).After(expNbf))
  1347. assert.True(t, o.NotAfter.Add(-testBufferDur).Before(expNaf))
  1348. assert.True(t, o.NotAfter.Add(testBufferDur).After(expNaf))
  1349. assert.True(t, o.ExpiresAt.Add(-testBufferDur).Before(orderExpiry))
  1350. assert.True(t, o.ExpiresAt.Add(testBufferDur).After(orderExpiry))
  1351. },
  1352. }
  1353. },
  1354. }
  1355. for name, run := range tests {
  1356. tc := run(t)
  1357. t.Run(name, func(t *testing.T) {
  1358. h := &Handler{linker: NewLinker("dns", "acme"), db: tc.db, ca: tc.ca}
  1359. req := httptest.NewRequest("GET", u, nil)
  1360. req = req.WithContext(tc.ctx)
  1361. w := httptest.NewRecorder()
  1362. h.NewOrder(w, req)
  1363. res := w.Result()
  1364. assert.Equals(t, res.StatusCode, tc.statusCode)
  1365. body, err := io.ReadAll(res.Body)
  1366. res.Body.Close()
  1367. assert.FatalError(t, err)
  1368. if res.StatusCode >= 400 && assert.NotNil(t, tc.err) {
  1369. var ae acme.Error
  1370. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  1371. assert.Equals(t, ae.Type, tc.err.Type)
  1372. assert.Equals(t, ae.Detail, tc.err.Detail)
  1373. assert.Equals(t, ae.Identifier, tc.err.Identifier)
  1374. assert.Equals(t, ae.Subproblems, tc.err.Subproblems)
  1375. assert.Equals(t, res.Header["Content-Type"], []string{"application/problem+json"})
  1376. } else {
  1377. ro := new(acme.Order)
  1378. assert.FatalError(t, json.Unmarshal(body, ro))
  1379. if tc.vr != nil {
  1380. tc.vr(t, ro)
  1381. }
  1382. assert.Equals(t, res.Header["Location"], []string{u})
  1383. assert.Equals(t, res.Header["Content-Type"], []string{"application/json"})
  1384. }
  1385. })
  1386. }
  1387. }
  1388. func TestHandler_FinalizeOrder(t *testing.T) {
  1389. prov := newProv()
  1390. escProvName := url.PathEscape(prov.GetName())
  1391. baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
  1392. now := clock.Now()
  1393. nbf := now
  1394. naf := now.Add(24 * time.Hour)
  1395. o := acme.Order{
  1396. ID: "orderID",
  1397. NotBefore: nbf,
  1398. NotAfter: naf,
  1399. Identifiers: []acme.Identifier{
  1400. {
  1401. Type: "dns",
  1402. Value: "example.com",
  1403. },
  1404. {
  1405. Type: "dns",
  1406. Value: "*.smallstep.com",
  1407. },
  1408. },
  1409. ExpiresAt: naf,
  1410. Status: acme.StatusValid,
  1411. AuthorizationURLs: []string{
  1412. fmt.Sprintf("%s/acme/%s/authz/foo", baseURL.String(), escProvName),
  1413. fmt.Sprintf("%s/acme/%s/authz/bar", baseURL.String(), escProvName),
  1414. fmt.Sprintf("%s/acme/%s/authz/baz", baseURL.String(), escProvName),
  1415. },
  1416. FinalizeURL: fmt.Sprintf("%s/acme/%s/order/orderID/finalize", baseURL.String(), escProvName),
  1417. CertificateURL: fmt.Sprintf("%s/acme/%s/certificate/certID", baseURL.String(), escProvName),
  1418. }
  1419. // Request with chi context
  1420. chiCtx := chi.NewRouteContext()
  1421. chiCtx.URLParams.Add("ordID", o.ID)
  1422. u := fmt.Sprintf("%s/acme/%s/order/%s",
  1423. baseURL.String(), escProvName, o.ID)
  1424. _csr, err := pemutil.Read("../../authority/testdata/certs/foo.csr")
  1425. assert.FatalError(t, err)
  1426. csr, ok := _csr.(*x509.CertificateRequest)
  1427. assert.Fatal(t, ok)
  1428. nor := &FinalizeRequest{
  1429. CSR: base64.RawURLEncoding.EncodeToString(csr.Raw),
  1430. }
  1431. payloadBytes, err := json.Marshal(nor)
  1432. assert.FatalError(t, err)
  1433. type test struct {
  1434. db acme.DB
  1435. ctx context.Context
  1436. statusCode int
  1437. err *acme.Error
  1438. }
  1439. var tests = map[string]func(t *testing.T) test{
  1440. "fail/no-account": func(t *testing.T) test {
  1441. return test{
  1442. ctx: context.WithValue(context.Background(), provisionerContextKey, prov),
  1443. statusCode: 400,
  1444. err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
  1445. }
  1446. },
  1447. "fail/nil-account": func(t *testing.T) test {
  1448. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1449. ctx = context.WithValue(ctx, accContextKey, nil)
  1450. return test{
  1451. ctx: ctx,
  1452. statusCode: 400,
  1453. err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
  1454. }
  1455. },
  1456. "fail/no-provisioner": func(t *testing.T) test {
  1457. acc := &acme.Account{ID: "accountID"}
  1458. ctx := context.WithValue(context.Background(), accContextKey, acc)
  1459. return test{
  1460. ctx: ctx,
  1461. statusCode: 500,
  1462. err: acme.NewErrorISE("provisioner does not exist"),
  1463. }
  1464. },
  1465. "fail/nil-provisioner": func(t *testing.T) test {
  1466. acc := &acme.Account{ID: "accountID"}
  1467. ctx := context.WithValue(context.Background(), provisionerContextKey, nil)
  1468. ctx = context.WithValue(ctx, accContextKey, acc)
  1469. return test{
  1470. ctx: ctx,
  1471. statusCode: 500,
  1472. err: acme.NewErrorISE("provisioner does not exist"),
  1473. }
  1474. },
  1475. "fail/no-payload": func(t *testing.T) test {
  1476. acc := &acme.Account{ID: "accountID"}
  1477. ctx := context.WithValue(context.Background(), accContextKey, acc)
  1478. ctx = context.WithValue(ctx, provisionerContextKey, prov)
  1479. return test{
  1480. ctx: ctx,
  1481. statusCode: 500,
  1482. err: acme.NewErrorISE("payload does not exist"),
  1483. }
  1484. },
  1485. "fail/nil-payload": func(t *testing.T) test {
  1486. acc := &acme.Account{ID: "accountID"}
  1487. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1488. ctx = context.WithValue(ctx, accContextKey, acc)
  1489. ctx = context.WithValue(ctx, payloadContextKey, nil)
  1490. return test{
  1491. ctx: ctx,
  1492. statusCode: 500,
  1493. err: acme.NewErrorISE("paylod does not exist"),
  1494. }
  1495. },
  1496. "fail/unmarshal-payload-error": func(t *testing.T) test {
  1497. acc := &acme.Account{ID: "accID"}
  1498. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1499. ctx = context.WithValue(ctx, accContextKey, acc)
  1500. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{})
  1501. return test{
  1502. ctx: ctx,
  1503. statusCode: 400,
  1504. err: acme.NewError(acme.ErrorMalformedType, "failed to unmarshal finalize-order request payload: unexpected end of JSON input"),
  1505. }
  1506. },
  1507. "fail/malformed-payload-error": func(t *testing.T) test {
  1508. acc := &acme.Account{ID: "accID"}
  1509. fr := &FinalizeRequest{}
  1510. b, err := json.Marshal(fr)
  1511. assert.FatalError(t, err)
  1512. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1513. ctx = context.WithValue(ctx, accContextKey, acc)
  1514. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  1515. return test{
  1516. ctx: ctx,
  1517. statusCode: 400,
  1518. err: acme.NewError(acme.ErrorMalformedType, "unable to parse csr: asn1: syntax error: sequence truncated"),
  1519. }
  1520. },
  1521. "fail/db.GetOrder-error": func(t *testing.T) test {
  1522. acc := &acme.Account{ID: "accountID"}
  1523. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1524. ctx = context.WithValue(ctx, accContextKey, acc)
  1525. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: payloadBytes})
  1526. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  1527. return test{
  1528. db: &acme.MockDB{
  1529. MockError: acme.NewErrorISE("force"),
  1530. },
  1531. ctx: ctx,
  1532. statusCode: 500,
  1533. err: acme.NewErrorISE("force"),
  1534. }
  1535. },
  1536. "fail/account-id-mismatch": func(t *testing.T) test {
  1537. acc := &acme.Account{ID: "accountID"}
  1538. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1539. ctx = context.WithValue(ctx, accContextKey, acc)
  1540. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: payloadBytes})
  1541. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  1542. return test{
  1543. db: &acme.MockDB{
  1544. MockGetOrder: func(ctx context.Context, id string) (*acme.Order, error) {
  1545. return &acme.Order{AccountID: "foo"}, nil
  1546. },
  1547. },
  1548. ctx: ctx,
  1549. statusCode: 401,
  1550. err: acme.NewError(acme.ErrorUnauthorizedType, "account id mismatch"),
  1551. }
  1552. },
  1553. "fail/provisioner-id-mismatch": func(t *testing.T) test {
  1554. acc := &acme.Account{ID: "accountID"}
  1555. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1556. ctx = context.WithValue(ctx, accContextKey, acc)
  1557. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: payloadBytes})
  1558. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  1559. return test{
  1560. db: &acme.MockDB{
  1561. MockGetOrder: func(ctx context.Context, id string) (*acme.Order, error) {
  1562. return &acme.Order{AccountID: "accountID", ProvisionerID: "bar"}, nil
  1563. },
  1564. },
  1565. ctx: ctx,
  1566. statusCode: 401,
  1567. err: acme.NewError(acme.ErrorUnauthorizedType, "provisioner id mismatch"),
  1568. }
  1569. },
  1570. "fail/order-finalize-error": func(t *testing.T) test {
  1571. acc := &acme.Account{ID: "accountID"}
  1572. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1573. ctx = context.WithValue(ctx, accContextKey, acc)
  1574. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: payloadBytes})
  1575. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  1576. return test{
  1577. db: &acme.MockDB{
  1578. MockGetOrder: func(ctx context.Context, id string) (*acme.Order, error) {
  1579. return &acme.Order{
  1580. AccountID: "accountID",
  1581. ProvisionerID: fmt.Sprintf("acme/%s", prov.GetName()),
  1582. ExpiresAt: clock.Now().Add(-time.Hour),
  1583. Status: acme.StatusReady,
  1584. }, nil
  1585. },
  1586. MockUpdateOrder: func(ctx context.Context, o *acme.Order) error {
  1587. return acme.NewErrorISE("force")
  1588. },
  1589. },
  1590. ctx: ctx,
  1591. statusCode: 500,
  1592. err: acme.NewErrorISE("force"),
  1593. }
  1594. },
  1595. "ok": func(t *testing.T) test {
  1596. acc := &acme.Account{ID: "accountID"}
  1597. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1598. ctx = context.WithValue(ctx, accContextKey, acc)
  1599. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: payloadBytes})
  1600. ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
  1601. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  1602. return test{
  1603. db: &acme.MockDB{
  1604. MockGetOrder: func(ctx context.Context, id string) (*acme.Order, error) {
  1605. return &acme.Order{
  1606. ID: "orderID",
  1607. AccountID: "accountID",
  1608. ProvisionerID: fmt.Sprintf("acme/%s", prov.GetName()),
  1609. ExpiresAt: naf,
  1610. Status: acme.StatusValid,
  1611. AuthorizationIDs: []string{"foo", "bar", "baz"},
  1612. NotBefore: nbf,
  1613. NotAfter: naf,
  1614. Identifiers: []acme.Identifier{
  1615. {
  1616. Type: "dns",
  1617. Value: "example.com",
  1618. },
  1619. {
  1620. Type: "dns",
  1621. Value: "*.smallstep.com",
  1622. },
  1623. },
  1624. CertificateID: "certID",
  1625. }, nil
  1626. },
  1627. },
  1628. ctx: ctx,
  1629. statusCode: 200,
  1630. }
  1631. },
  1632. }
  1633. for name, run := range tests {
  1634. tc := run(t)
  1635. t.Run(name, func(t *testing.T) {
  1636. h := &Handler{linker: NewLinker("dns", "acme"), db: tc.db}
  1637. req := httptest.NewRequest("GET", u, nil)
  1638. req = req.WithContext(tc.ctx)
  1639. w := httptest.NewRecorder()
  1640. h.FinalizeOrder(w, req)
  1641. res := w.Result()
  1642. assert.Equals(t, res.StatusCode, tc.statusCode)
  1643. body, err := io.ReadAll(res.Body)
  1644. res.Body.Close()
  1645. assert.FatalError(t, err)
  1646. if res.StatusCode >= 400 && assert.NotNil(t, tc.err) {
  1647. var ae acme.Error
  1648. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  1649. assert.Equals(t, ae.Type, tc.err.Type)
  1650. assert.Equals(t, ae.Detail, tc.err.Detail)
  1651. assert.Equals(t, ae.Identifier, tc.err.Identifier)
  1652. assert.Equals(t, ae.Subproblems, tc.err.Subproblems)
  1653. assert.Equals(t, res.Header["Content-Type"], []string{"application/problem+json"})
  1654. } else {
  1655. expB, err := json.Marshal(o)
  1656. assert.FatalError(t, err)
  1657. ro := new(acme.Order)
  1658. assert.FatalError(t, json.Unmarshal(body, ro))
  1659. assert.Equals(t, bytes.TrimSpace(body), expB)
  1660. assert.Equals(t, res.Header["Location"], []string{u})
  1661. assert.Equals(t, res.Header["Content-Type"], []string{"application/json"})
  1662. }
  1663. })
  1664. }
  1665. }
  1666. func TestHandler_challengeTypes(t *testing.T) {
  1667. type args struct {
  1668. az *acme.Authorization
  1669. }
  1670. tests := []struct {
  1671. name string
  1672. args args
  1673. want []acme.ChallengeType
  1674. }{
  1675. {
  1676. name: "ok/dns",
  1677. args: args{
  1678. az: &acme.Authorization{
  1679. Identifier: acme.Identifier{Type: "dns", Value: "example.com"},
  1680. Wildcard: false,
  1681. },
  1682. },
  1683. want: []acme.ChallengeType{acme.DNS01, acme.HTTP01, acme.TLSALPN01},
  1684. },
  1685. {
  1686. name: "ok/wildcard",
  1687. args: args{
  1688. az: &acme.Authorization{
  1689. Identifier: acme.Identifier{Type: "dns", Value: "*.example.com"},
  1690. Wildcard: true,
  1691. },
  1692. },
  1693. want: []acme.ChallengeType{acme.DNS01},
  1694. },
  1695. {
  1696. name: "ok/ip",
  1697. args: args{
  1698. az: &acme.Authorization{
  1699. Identifier: acme.Identifier{Type: "ip", Value: "192.168.42.42"},
  1700. Wildcard: false,
  1701. },
  1702. },
  1703. want: []acme.ChallengeType{acme.HTTP01, acme.TLSALPN01},
  1704. },
  1705. }
  1706. for _, tt := range tests {
  1707. t.Run(tt.name, func(t *testing.T) {
  1708. if got := challengeTypes(tt.args.az); !reflect.DeepEqual(got, tt.want) {
  1709. t.Errorf("Handler.challengeTypes() = %v, want %v", got, tt.want)
  1710. }
  1711. })
  1712. }
  1713. }