4
0

order_test.go 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711
  1. package api
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/x509"
  6. "encoding/base64"
  7. "encoding/json"
  8. "fmt"
  9. "io/ioutil"
  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. url := 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", url, 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 := ioutil.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{url})
  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. url := fmt.Sprintf("%s/acme/%s/order/ordID",
  651. baseURL.String(), escProvName)
  652. type test struct {
  653. db acme.DB
  654. ctx context.Context
  655. nor *NewOrderRequest
  656. statusCode int
  657. vr func(t *testing.T, o *acme.Order)
  658. err *acme.Error
  659. }
  660. var tests = map[string]func(t *testing.T) test{
  661. "fail/no-account": func(t *testing.T) test {
  662. return test{
  663. ctx: context.WithValue(context.Background(), provisionerContextKey, prov),
  664. statusCode: 400,
  665. err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
  666. }
  667. },
  668. "fail/nil-account": func(t *testing.T) test {
  669. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  670. ctx = context.WithValue(ctx, accContextKey, nil)
  671. return test{
  672. ctx: ctx,
  673. statusCode: 400,
  674. err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
  675. }
  676. },
  677. "fail/no-provisioner": func(t *testing.T) test {
  678. acc := &acme.Account{ID: "accountID"}
  679. ctx := context.WithValue(context.Background(), accContextKey, acc)
  680. return test{
  681. ctx: ctx,
  682. statusCode: 500,
  683. err: acme.NewErrorISE("provisioner does not exist"),
  684. }
  685. },
  686. "fail/nil-provisioner": func(t *testing.T) test {
  687. acc := &acme.Account{ID: "accountID"}
  688. ctx := context.WithValue(context.Background(), provisionerContextKey, nil)
  689. ctx = context.WithValue(ctx, accContextKey, acc)
  690. return test{
  691. ctx: ctx,
  692. statusCode: 500,
  693. err: acme.NewErrorISE("provisioner does not exist"),
  694. }
  695. },
  696. "fail/no-payload": func(t *testing.T) test {
  697. acc := &acme.Account{ID: "accountID"}
  698. ctx := context.WithValue(context.Background(), accContextKey, acc)
  699. ctx = context.WithValue(ctx, provisionerContextKey, prov)
  700. return test{
  701. ctx: ctx,
  702. statusCode: 500,
  703. err: acme.NewErrorISE("payload does not exist"),
  704. }
  705. },
  706. "fail/nil-payload": func(t *testing.T) test {
  707. acc := &acme.Account{ID: "accountID"}
  708. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  709. ctx = context.WithValue(ctx, accContextKey, acc)
  710. ctx = context.WithValue(ctx, payloadContextKey, nil)
  711. return test{
  712. ctx: ctx,
  713. statusCode: 500,
  714. err: acme.NewErrorISE("paylod does not exist"),
  715. }
  716. },
  717. "fail/unmarshal-payload-error": func(t *testing.T) test {
  718. acc := &acme.Account{ID: "accID"}
  719. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  720. ctx = context.WithValue(ctx, accContextKey, acc)
  721. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{})
  722. return test{
  723. ctx: ctx,
  724. statusCode: 400,
  725. err: acme.NewError(acme.ErrorMalformedType, "failed to unmarshal new-order request payload: unexpected end of JSON input"),
  726. }
  727. },
  728. "fail/malformed-payload-error": func(t *testing.T) test {
  729. acc := &acme.Account{ID: "accID"}
  730. fr := &NewOrderRequest{}
  731. b, err := json.Marshal(fr)
  732. assert.FatalError(t, err)
  733. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  734. ctx = context.WithValue(ctx, accContextKey, acc)
  735. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  736. return test{
  737. ctx: ctx,
  738. statusCode: 400,
  739. err: acme.NewError(acme.ErrorMalformedType, "identifiers list cannot be empty"),
  740. }
  741. },
  742. "fail/error-h.newAuthorization": func(t *testing.T) test {
  743. acc := &acme.Account{ID: "accID"}
  744. fr := &NewOrderRequest{
  745. Identifiers: []acme.Identifier{
  746. {Type: "dns", Value: "zap.internal"},
  747. },
  748. }
  749. b, err := json.Marshal(fr)
  750. assert.FatalError(t, err)
  751. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  752. ctx = context.WithValue(ctx, accContextKey, acc)
  753. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  754. return test{
  755. ctx: ctx,
  756. statusCode: 500,
  757. db: &acme.MockDB{
  758. MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
  759. assert.Equals(t, ch.AccountID, "accID")
  760. assert.Equals(t, ch.Type, acme.DNS01)
  761. assert.NotEquals(t, ch.Token, "")
  762. assert.Equals(t, ch.Status, acme.StatusPending)
  763. assert.Equals(t, ch.Value, "zap.internal")
  764. return errors.New("force")
  765. },
  766. },
  767. err: acme.NewErrorISE("error creating challenge: force"),
  768. }
  769. },
  770. "fail/error-db.CreateOrder": func(t *testing.T) test {
  771. acc := &acme.Account{ID: "accID"}
  772. fr := &NewOrderRequest{
  773. Identifiers: []acme.Identifier{
  774. {Type: "dns", Value: "zap.internal"},
  775. },
  776. }
  777. b, err := json.Marshal(fr)
  778. assert.FatalError(t, err)
  779. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  780. ctx = context.WithValue(ctx, accContextKey, acc)
  781. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  782. var (
  783. ch1, ch2, ch3 **acme.Challenge
  784. az1ID *string
  785. count = 0
  786. )
  787. return test{
  788. ctx: ctx,
  789. statusCode: 500,
  790. db: &acme.MockDB{
  791. MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
  792. switch count {
  793. case 0:
  794. ch.ID = "dns"
  795. assert.Equals(t, ch.Type, acme.DNS01)
  796. ch1 = &ch
  797. case 1:
  798. ch.ID = "http"
  799. assert.Equals(t, ch.Type, acme.HTTP01)
  800. ch2 = &ch
  801. case 2:
  802. ch.ID = "tls"
  803. assert.Equals(t, ch.Type, acme.TLSALPN01)
  804. ch3 = &ch
  805. default:
  806. assert.FatalError(t, errors.New("test logic error"))
  807. return errors.New("force")
  808. }
  809. count++
  810. assert.Equals(t, ch.AccountID, "accID")
  811. assert.NotEquals(t, ch.Token, "")
  812. assert.Equals(t, ch.Status, acme.StatusPending)
  813. assert.Equals(t, ch.Value, "zap.internal")
  814. return nil
  815. },
  816. MockCreateAuthorization: func(ctx context.Context, az *acme.Authorization) error {
  817. az.ID = "az1ID"
  818. az1ID = &az.ID
  819. assert.Equals(t, az.AccountID, "accID")
  820. assert.NotEquals(t, az.Token, "")
  821. assert.Equals(t, az.Status, acme.StatusPending)
  822. assert.Equals(t, az.Identifier, fr.Identifiers[0])
  823. assert.Equals(t, az.Challenges, []*acme.Challenge{*ch1, *ch2, *ch3})
  824. assert.Equals(t, az.Wildcard, false)
  825. return nil
  826. },
  827. MockCreateOrder: func(ctx context.Context, o *acme.Order) error {
  828. assert.Equals(t, o.AccountID, "accID")
  829. assert.Equals(t, o.ProvisionerID, prov.GetID())
  830. assert.Equals(t, o.Status, acme.StatusPending)
  831. assert.Equals(t, o.Identifiers, fr.Identifiers)
  832. assert.Equals(t, o.AuthorizationIDs, []string{*az1ID})
  833. return errors.New("force")
  834. },
  835. },
  836. err: acme.NewErrorISE("error creating order: force"),
  837. }
  838. },
  839. "ok/multiple-authz": func(t *testing.T) test {
  840. acc := &acme.Account{ID: "accID"}
  841. nor := &NewOrderRequest{
  842. Identifiers: []acme.Identifier{
  843. {Type: "dns", Value: "zap.internal"},
  844. {Type: "dns", Value: "*.zar.internal"},
  845. },
  846. }
  847. b, err := json.Marshal(nor)
  848. assert.FatalError(t, err)
  849. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  850. ctx = context.WithValue(ctx, accContextKey, acc)
  851. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  852. ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
  853. var (
  854. ch1, ch2, ch3, ch4 **acme.Challenge
  855. az1ID, az2ID *string
  856. chCount, azCount = 0, 0
  857. )
  858. return test{
  859. ctx: ctx,
  860. statusCode: 201,
  861. nor: nor,
  862. db: &acme.MockDB{
  863. MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
  864. switch chCount {
  865. case 0:
  866. ch.ID = "dns"
  867. assert.Equals(t, ch.Type, acme.DNS01)
  868. assert.Equals(t, ch.Value, "zap.internal")
  869. ch1 = &ch
  870. case 1:
  871. ch.ID = "http"
  872. assert.Equals(t, ch.Type, acme.HTTP01)
  873. assert.Equals(t, ch.Value, "zap.internal")
  874. ch2 = &ch
  875. case 2:
  876. ch.ID = "tls"
  877. assert.Equals(t, ch.Type, acme.TLSALPN01)
  878. assert.Equals(t, ch.Value, "zap.internal")
  879. ch3 = &ch
  880. case 3:
  881. ch.ID = "dns"
  882. assert.Equals(t, ch.Type, acme.DNS01)
  883. assert.Equals(t, ch.Value, "zar.internal")
  884. ch4 = &ch
  885. default:
  886. assert.FatalError(t, errors.New("test logic error"))
  887. return errors.New("force")
  888. }
  889. chCount++
  890. assert.Equals(t, ch.AccountID, "accID")
  891. assert.NotEquals(t, ch.Token, "")
  892. assert.Equals(t, ch.Status, acme.StatusPending)
  893. return nil
  894. },
  895. MockCreateAuthorization: func(ctx context.Context, az *acme.Authorization) error {
  896. switch azCount {
  897. case 0:
  898. az.ID = "az1ID"
  899. az1ID = &az.ID
  900. assert.Equals(t, az.Identifier, nor.Identifiers[0])
  901. assert.Equals(t, az.Wildcard, false)
  902. assert.Equals(t, az.Challenges, []*acme.Challenge{*ch1, *ch2, *ch3})
  903. case 1:
  904. az.ID = "az2ID"
  905. az2ID = &az.ID
  906. assert.Equals(t, az.Identifier, acme.Identifier{
  907. Type: acme.DNS,
  908. Value: "zar.internal",
  909. })
  910. assert.Equals(t, az.Wildcard, true)
  911. assert.Equals(t, az.Challenges, []*acme.Challenge{*ch4})
  912. default:
  913. assert.FatalError(t, errors.New("test logic error"))
  914. return errors.New("force")
  915. }
  916. azCount++
  917. assert.Equals(t, az.AccountID, "accID")
  918. assert.NotEquals(t, az.Token, "")
  919. assert.Equals(t, az.Status, acme.StatusPending)
  920. return nil
  921. },
  922. MockCreateOrder: func(ctx context.Context, o *acme.Order) error {
  923. o.ID = "ordID"
  924. assert.Equals(t, o.AccountID, "accID")
  925. assert.Equals(t, o.ProvisionerID, prov.GetID())
  926. assert.Equals(t, o.Status, acme.StatusPending)
  927. assert.Equals(t, o.Identifiers, nor.Identifiers)
  928. assert.Equals(t, o.AuthorizationIDs, []string{*az1ID, *az2ID})
  929. return nil
  930. },
  931. },
  932. vr: func(t *testing.T, o *acme.Order) {
  933. now := clock.Now()
  934. testBufferDur := 5 * time.Second
  935. orderExpiry := now.Add(defaultOrderExpiry)
  936. expNbf := now.Add(-defaultOrderBackdate)
  937. expNaf := now.Add(prov.DefaultTLSCertDuration())
  938. assert.Equals(t, o.ID, "ordID")
  939. assert.Equals(t, o.Status, acme.StatusPending)
  940. assert.Equals(t, o.Identifiers, nor.Identifiers)
  941. assert.Equals(t, o.AuthorizationURLs, []string{
  942. fmt.Sprintf("%s/acme/%s/authz/az1ID", baseURL.String(), escProvName),
  943. fmt.Sprintf("%s/acme/%s/authz/az2ID", baseURL.String(), escProvName),
  944. })
  945. assert.True(t, o.NotBefore.Add(-testBufferDur).Before(expNbf))
  946. assert.True(t, o.NotBefore.Add(testBufferDur).After(expNbf))
  947. assert.True(t, o.NotAfter.Add(-testBufferDur).Before(expNaf))
  948. assert.True(t, o.NotAfter.Add(testBufferDur).After(expNaf))
  949. assert.True(t, o.ExpiresAt.Add(-testBufferDur).Before(orderExpiry))
  950. assert.True(t, o.ExpiresAt.Add(testBufferDur).After(orderExpiry))
  951. },
  952. }
  953. },
  954. "ok/default-naf-nbf": func(t *testing.T) test {
  955. acc := &acme.Account{ID: "accID"}
  956. nor := &NewOrderRequest{
  957. Identifiers: []acme.Identifier{
  958. {Type: "dns", Value: "zap.internal"},
  959. },
  960. }
  961. b, err := json.Marshal(nor)
  962. assert.FatalError(t, err)
  963. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  964. ctx = context.WithValue(ctx, accContextKey, acc)
  965. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  966. ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
  967. var (
  968. ch1, ch2, ch3 **acme.Challenge
  969. az1ID *string
  970. count = 0
  971. )
  972. return test{
  973. ctx: ctx,
  974. statusCode: 201,
  975. nor: nor,
  976. db: &acme.MockDB{
  977. MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
  978. switch count {
  979. case 0:
  980. ch.ID = "dns"
  981. assert.Equals(t, ch.Type, acme.DNS01)
  982. ch1 = &ch
  983. case 1:
  984. ch.ID = "http"
  985. assert.Equals(t, ch.Type, acme.HTTP01)
  986. ch2 = &ch
  987. case 2:
  988. ch.ID = "tls"
  989. assert.Equals(t, ch.Type, acme.TLSALPN01)
  990. ch3 = &ch
  991. default:
  992. assert.FatalError(t, errors.New("test logic error"))
  993. return errors.New("force")
  994. }
  995. count++
  996. assert.Equals(t, ch.AccountID, "accID")
  997. assert.NotEquals(t, ch.Token, "")
  998. assert.Equals(t, ch.Status, acme.StatusPending)
  999. assert.Equals(t, ch.Value, "zap.internal")
  1000. return nil
  1001. },
  1002. MockCreateAuthorization: func(ctx context.Context, az *acme.Authorization) error {
  1003. az.ID = "az1ID"
  1004. az1ID = &az.ID
  1005. assert.Equals(t, az.AccountID, "accID")
  1006. assert.NotEquals(t, az.Token, "")
  1007. assert.Equals(t, az.Status, acme.StatusPending)
  1008. assert.Equals(t, az.Identifier, nor.Identifiers[0])
  1009. assert.Equals(t, az.Challenges, []*acme.Challenge{*ch1, *ch2, *ch3})
  1010. assert.Equals(t, az.Wildcard, false)
  1011. return nil
  1012. },
  1013. MockCreateOrder: func(ctx context.Context, o *acme.Order) error {
  1014. o.ID = "ordID"
  1015. assert.Equals(t, o.AccountID, "accID")
  1016. assert.Equals(t, o.ProvisionerID, prov.GetID())
  1017. assert.Equals(t, o.Status, acme.StatusPending)
  1018. assert.Equals(t, o.Identifiers, nor.Identifiers)
  1019. assert.Equals(t, o.AuthorizationIDs, []string{*az1ID})
  1020. return nil
  1021. },
  1022. },
  1023. vr: func(t *testing.T, o *acme.Order) {
  1024. now := clock.Now()
  1025. testBufferDur := 5 * time.Second
  1026. orderExpiry := now.Add(defaultOrderExpiry)
  1027. expNbf := now.Add(-defaultOrderBackdate)
  1028. expNaf := now.Add(prov.DefaultTLSCertDuration())
  1029. assert.Equals(t, o.ID, "ordID")
  1030. assert.Equals(t, o.Status, acme.StatusPending)
  1031. assert.Equals(t, o.Identifiers, nor.Identifiers)
  1032. assert.Equals(t, o.AuthorizationURLs, []string{fmt.Sprintf("%s/acme/%s/authz/az1ID", baseURL.String(), escProvName)})
  1033. assert.True(t, o.NotBefore.Add(-testBufferDur).Before(expNbf))
  1034. assert.True(t, o.NotBefore.Add(testBufferDur).After(expNbf))
  1035. assert.True(t, o.NotAfter.Add(-testBufferDur).Before(expNaf))
  1036. assert.True(t, o.NotAfter.Add(testBufferDur).After(expNaf))
  1037. assert.True(t, o.ExpiresAt.Add(-testBufferDur).Before(orderExpiry))
  1038. assert.True(t, o.ExpiresAt.Add(testBufferDur).After(orderExpiry))
  1039. },
  1040. }
  1041. },
  1042. "ok/nbf-no-naf": func(t *testing.T) test {
  1043. now := clock.Now()
  1044. expNbf := now.Add(10 * time.Minute)
  1045. acc := &acme.Account{ID: "accID"}
  1046. nor := &NewOrderRequest{
  1047. Identifiers: []acme.Identifier{
  1048. {Type: "dns", Value: "zap.internal"},
  1049. },
  1050. NotBefore: expNbf,
  1051. }
  1052. b, err := json.Marshal(nor)
  1053. assert.FatalError(t, err)
  1054. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1055. ctx = context.WithValue(ctx, accContextKey, acc)
  1056. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  1057. ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
  1058. var (
  1059. ch1, ch2, ch3 **acme.Challenge
  1060. az1ID *string
  1061. count = 0
  1062. )
  1063. return test{
  1064. ctx: ctx,
  1065. statusCode: 201,
  1066. nor: nor,
  1067. db: &acme.MockDB{
  1068. MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
  1069. switch count {
  1070. case 0:
  1071. ch.ID = "dns"
  1072. assert.Equals(t, ch.Type, acme.DNS01)
  1073. ch1 = &ch
  1074. case 1:
  1075. ch.ID = "http"
  1076. assert.Equals(t, ch.Type, acme.HTTP01)
  1077. ch2 = &ch
  1078. case 2:
  1079. ch.ID = "tls"
  1080. assert.Equals(t, ch.Type, acme.TLSALPN01)
  1081. ch3 = &ch
  1082. default:
  1083. assert.FatalError(t, errors.New("test logic error"))
  1084. return errors.New("force")
  1085. }
  1086. count++
  1087. assert.Equals(t, ch.AccountID, "accID")
  1088. assert.NotEquals(t, ch.Token, "")
  1089. assert.Equals(t, ch.Status, acme.StatusPending)
  1090. assert.Equals(t, ch.Value, "zap.internal")
  1091. return nil
  1092. },
  1093. MockCreateAuthorization: func(ctx context.Context, az *acme.Authorization) error {
  1094. az.ID = "az1ID"
  1095. az1ID = &az.ID
  1096. assert.Equals(t, az.AccountID, "accID")
  1097. assert.NotEquals(t, az.Token, "")
  1098. assert.Equals(t, az.Status, acme.StatusPending)
  1099. assert.Equals(t, az.Identifier, nor.Identifiers[0])
  1100. assert.Equals(t, az.Challenges, []*acme.Challenge{*ch1, *ch2, *ch3})
  1101. assert.Equals(t, az.Wildcard, false)
  1102. return nil
  1103. },
  1104. MockCreateOrder: func(ctx context.Context, o *acme.Order) error {
  1105. o.ID = "ordID"
  1106. assert.Equals(t, o.AccountID, "accID")
  1107. assert.Equals(t, o.ProvisionerID, prov.GetID())
  1108. assert.Equals(t, o.Status, acme.StatusPending)
  1109. assert.Equals(t, o.Identifiers, nor.Identifiers)
  1110. assert.Equals(t, o.AuthorizationIDs, []string{*az1ID})
  1111. return nil
  1112. },
  1113. },
  1114. vr: func(t *testing.T, o *acme.Order) {
  1115. now := clock.Now()
  1116. testBufferDur := 5 * time.Second
  1117. orderExpiry := now.Add(defaultOrderExpiry)
  1118. expNaf := expNbf.Add(prov.DefaultTLSCertDuration())
  1119. assert.Equals(t, o.ID, "ordID")
  1120. assert.Equals(t, o.Status, acme.StatusPending)
  1121. assert.Equals(t, o.Identifiers, nor.Identifiers)
  1122. assert.Equals(t, o.AuthorizationURLs, []string{fmt.Sprintf("%s/acme/%s/authz/az1ID", baseURL.String(), escProvName)})
  1123. assert.True(t, o.NotBefore.Add(-testBufferDur).Before(expNbf))
  1124. assert.True(t, o.NotBefore.Add(testBufferDur).After(expNbf))
  1125. assert.True(t, o.NotAfter.Add(-testBufferDur).Before(expNaf))
  1126. assert.True(t, o.NotAfter.Add(testBufferDur).After(expNaf))
  1127. assert.True(t, o.ExpiresAt.Add(-testBufferDur).Before(orderExpiry))
  1128. assert.True(t, o.ExpiresAt.Add(testBufferDur).After(orderExpiry))
  1129. },
  1130. }
  1131. },
  1132. "ok/naf-no-nbf": func(t *testing.T) test {
  1133. now := clock.Now()
  1134. expNaf := now.Add(15 * time.Minute)
  1135. acc := &acme.Account{ID: "accID"}
  1136. nor := &NewOrderRequest{
  1137. Identifiers: []acme.Identifier{
  1138. {Type: "dns", Value: "zap.internal"},
  1139. },
  1140. NotAfter: expNaf,
  1141. }
  1142. b, err := json.Marshal(nor)
  1143. assert.FatalError(t, err)
  1144. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1145. ctx = context.WithValue(ctx, accContextKey, acc)
  1146. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  1147. ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
  1148. var (
  1149. ch1, ch2, ch3 **acme.Challenge
  1150. az1ID *string
  1151. count = 0
  1152. )
  1153. return test{
  1154. ctx: ctx,
  1155. statusCode: 201,
  1156. nor: nor,
  1157. db: &acme.MockDB{
  1158. MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
  1159. switch count {
  1160. case 0:
  1161. ch.ID = "dns"
  1162. assert.Equals(t, ch.Type, acme.DNS01)
  1163. ch1 = &ch
  1164. case 1:
  1165. ch.ID = "http"
  1166. assert.Equals(t, ch.Type, acme.HTTP01)
  1167. ch2 = &ch
  1168. case 2:
  1169. ch.ID = "tls"
  1170. assert.Equals(t, ch.Type, acme.TLSALPN01)
  1171. ch3 = &ch
  1172. default:
  1173. assert.FatalError(t, errors.New("test logic error"))
  1174. return errors.New("force")
  1175. }
  1176. count++
  1177. assert.Equals(t, ch.AccountID, "accID")
  1178. assert.NotEquals(t, ch.Token, "")
  1179. assert.Equals(t, ch.Status, acme.StatusPending)
  1180. assert.Equals(t, ch.Value, "zap.internal")
  1181. return nil
  1182. },
  1183. MockCreateAuthorization: func(ctx context.Context, az *acme.Authorization) error {
  1184. az.ID = "az1ID"
  1185. az1ID = &az.ID
  1186. assert.Equals(t, az.AccountID, "accID")
  1187. assert.NotEquals(t, az.Token, "")
  1188. assert.Equals(t, az.Status, acme.StatusPending)
  1189. assert.Equals(t, az.Identifier, nor.Identifiers[0])
  1190. assert.Equals(t, az.Challenges, []*acme.Challenge{*ch1, *ch2, *ch3})
  1191. assert.Equals(t, az.Wildcard, false)
  1192. return nil
  1193. },
  1194. MockCreateOrder: func(ctx context.Context, o *acme.Order) error {
  1195. o.ID = "ordID"
  1196. assert.Equals(t, o.AccountID, "accID")
  1197. assert.Equals(t, o.ProvisionerID, prov.GetID())
  1198. assert.Equals(t, o.Status, acme.StatusPending)
  1199. assert.Equals(t, o.Identifiers, nor.Identifiers)
  1200. assert.Equals(t, o.AuthorizationIDs, []string{*az1ID})
  1201. return nil
  1202. },
  1203. },
  1204. vr: func(t *testing.T, o *acme.Order) {
  1205. testBufferDur := 5 * time.Second
  1206. orderExpiry := now.Add(defaultOrderExpiry)
  1207. expNbf := now.Add(-defaultOrderBackdate)
  1208. assert.Equals(t, o.ID, "ordID")
  1209. assert.Equals(t, o.Status, acme.StatusPending)
  1210. assert.Equals(t, o.Identifiers, nor.Identifiers)
  1211. assert.Equals(t, o.AuthorizationURLs, []string{fmt.Sprintf("%s/acme/%s/authz/az1ID", baseURL.String(), escProvName)})
  1212. assert.True(t, o.NotBefore.Add(-testBufferDur).Before(expNbf))
  1213. assert.True(t, o.NotBefore.Add(testBufferDur).After(expNbf))
  1214. assert.True(t, o.NotAfter.Add(-testBufferDur).Before(expNaf))
  1215. assert.True(t, o.NotAfter.Add(testBufferDur).After(expNaf))
  1216. assert.True(t, o.ExpiresAt.Add(-testBufferDur).Before(orderExpiry))
  1217. assert.True(t, o.ExpiresAt.Add(testBufferDur).After(orderExpiry))
  1218. },
  1219. }
  1220. },
  1221. "ok/naf-nbf": func(t *testing.T) test {
  1222. now := clock.Now()
  1223. expNbf := now.Add(5 * time.Minute)
  1224. expNaf := now.Add(15 * time.Minute)
  1225. acc := &acme.Account{ID: "accID"}
  1226. nor := &NewOrderRequest{
  1227. Identifiers: []acme.Identifier{
  1228. {Type: "dns", Value: "zap.internal"},
  1229. },
  1230. NotBefore: expNbf,
  1231. NotAfter: expNaf,
  1232. }
  1233. b, err := json.Marshal(nor)
  1234. assert.FatalError(t, err)
  1235. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1236. ctx = context.WithValue(ctx, accContextKey, acc)
  1237. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  1238. ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
  1239. var (
  1240. ch1, ch2, ch3 **acme.Challenge
  1241. az1ID *string
  1242. count = 0
  1243. )
  1244. return test{
  1245. ctx: ctx,
  1246. statusCode: 201,
  1247. nor: nor,
  1248. db: &acme.MockDB{
  1249. MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
  1250. switch count {
  1251. case 0:
  1252. ch.ID = "dns"
  1253. assert.Equals(t, ch.Type, acme.DNS01)
  1254. ch1 = &ch
  1255. case 1:
  1256. ch.ID = "http"
  1257. assert.Equals(t, ch.Type, acme.HTTP01)
  1258. ch2 = &ch
  1259. case 2:
  1260. ch.ID = "tls"
  1261. assert.Equals(t, ch.Type, acme.TLSALPN01)
  1262. ch3 = &ch
  1263. default:
  1264. assert.FatalError(t, errors.New("test logic error"))
  1265. return errors.New("force")
  1266. }
  1267. count++
  1268. assert.Equals(t, ch.AccountID, "accID")
  1269. assert.NotEquals(t, ch.Token, "")
  1270. assert.Equals(t, ch.Status, acme.StatusPending)
  1271. assert.Equals(t, ch.Value, "zap.internal")
  1272. return nil
  1273. },
  1274. MockCreateAuthorization: func(ctx context.Context, az *acme.Authorization) error {
  1275. az.ID = "az1ID"
  1276. az1ID = &az.ID
  1277. assert.Equals(t, az.AccountID, "accID")
  1278. assert.NotEquals(t, az.Token, "")
  1279. assert.Equals(t, az.Status, acme.StatusPending)
  1280. assert.Equals(t, az.Identifier, nor.Identifiers[0])
  1281. assert.Equals(t, az.Challenges, []*acme.Challenge{*ch1, *ch2, *ch3})
  1282. assert.Equals(t, az.Wildcard, false)
  1283. return nil
  1284. },
  1285. MockCreateOrder: func(ctx context.Context, o *acme.Order) error {
  1286. o.ID = "ordID"
  1287. assert.Equals(t, o.AccountID, "accID")
  1288. assert.Equals(t, o.ProvisionerID, prov.GetID())
  1289. assert.Equals(t, o.Status, acme.StatusPending)
  1290. assert.Equals(t, o.Identifiers, nor.Identifiers)
  1291. assert.Equals(t, o.AuthorizationIDs, []string{*az1ID})
  1292. return nil
  1293. },
  1294. },
  1295. vr: func(t *testing.T, o *acme.Order) {
  1296. testBufferDur := 5 * time.Second
  1297. orderExpiry := now.Add(defaultOrderExpiry)
  1298. assert.Equals(t, o.ID, "ordID")
  1299. assert.Equals(t, o.Status, acme.StatusPending)
  1300. assert.Equals(t, o.Identifiers, nor.Identifiers)
  1301. assert.Equals(t, o.AuthorizationURLs, []string{fmt.Sprintf("%s/acme/%s/authz/az1ID", baseURL.String(), escProvName)})
  1302. assert.True(t, o.NotBefore.Add(-testBufferDur).Before(expNbf))
  1303. assert.True(t, o.NotBefore.Add(testBufferDur).After(expNbf))
  1304. assert.True(t, o.NotAfter.Add(-testBufferDur).Before(expNaf))
  1305. assert.True(t, o.NotAfter.Add(testBufferDur).After(expNaf))
  1306. assert.True(t, o.ExpiresAt.Add(-testBufferDur).Before(orderExpiry))
  1307. assert.True(t, o.ExpiresAt.Add(testBufferDur).After(orderExpiry))
  1308. },
  1309. }
  1310. },
  1311. }
  1312. for name, run := range tests {
  1313. tc := run(t)
  1314. t.Run(name, func(t *testing.T) {
  1315. h := &Handler{linker: NewLinker("dns", "acme"), db: tc.db}
  1316. req := httptest.NewRequest("GET", url, nil)
  1317. req = req.WithContext(tc.ctx)
  1318. w := httptest.NewRecorder()
  1319. h.NewOrder(w, req)
  1320. res := w.Result()
  1321. assert.Equals(t, res.StatusCode, tc.statusCode)
  1322. body, err := ioutil.ReadAll(res.Body)
  1323. res.Body.Close()
  1324. assert.FatalError(t, err)
  1325. if res.StatusCode >= 400 && assert.NotNil(t, tc.err) {
  1326. var ae acme.Error
  1327. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  1328. assert.Equals(t, ae.Type, tc.err.Type)
  1329. assert.Equals(t, ae.Detail, tc.err.Detail)
  1330. assert.Equals(t, ae.Identifier, tc.err.Identifier)
  1331. assert.Equals(t, ae.Subproblems, tc.err.Subproblems)
  1332. assert.Equals(t, res.Header["Content-Type"], []string{"application/problem+json"})
  1333. } else {
  1334. ro := new(acme.Order)
  1335. assert.FatalError(t, json.Unmarshal(body, ro))
  1336. if tc.vr != nil {
  1337. tc.vr(t, ro)
  1338. }
  1339. assert.Equals(t, res.Header["Location"], []string{url})
  1340. assert.Equals(t, res.Header["Content-Type"], []string{"application/json"})
  1341. }
  1342. })
  1343. }
  1344. }
  1345. func TestHandler_FinalizeOrder(t *testing.T) {
  1346. prov := newProv()
  1347. escProvName := url.PathEscape(prov.GetName())
  1348. baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
  1349. now := clock.Now()
  1350. nbf := now
  1351. naf := now.Add(24 * time.Hour)
  1352. o := acme.Order{
  1353. ID: "orderID",
  1354. NotBefore: nbf,
  1355. NotAfter: naf,
  1356. Identifiers: []acme.Identifier{
  1357. {
  1358. Type: "dns",
  1359. Value: "example.com",
  1360. },
  1361. {
  1362. Type: "dns",
  1363. Value: "*.smallstep.com",
  1364. },
  1365. },
  1366. ExpiresAt: naf,
  1367. Status: acme.StatusValid,
  1368. AuthorizationURLs: []string{
  1369. fmt.Sprintf("%s/acme/%s/authz/foo", baseURL.String(), escProvName),
  1370. fmt.Sprintf("%s/acme/%s/authz/bar", baseURL.String(), escProvName),
  1371. fmt.Sprintf("%s/acme/%s/authz/baz", baseURL.String(), escProvName),
  1372. },
  1373. FinalizeURL: fmt.Sprintf("%s/acme/%s/order/orderID/finalize", baseURL.String(), escProvName),
  1374. CertificateURL: fmt.Sprintf("%s/acme/%s/certificate/certID", baseURL.String(), escProvName),
  1375. }
  1376. // Request with chi context
  1377. chiCtx := chi.NewRouteContext()
  1378. chiCtx.URLParams.Add("ordID", o.ID)
  1379. url := fmt.Sprintf("%s/acme/%s/order/%s",
  1380. baseURL.String(), escProvName, o.ID)
  1381. _csr, err := pemutil.Read("../../authority/testdata/certs/foo.csr")
  1382. assert.FatalError(t, err)
  1383. csr, ok := _csr.(*x509.CertificateRequest)
  1384. assert.Fatal(t, ok)
  1385. nor := &FinalizeRequest{
  1386. CSR: base64.RawURLEncoding.EncodeToString(csr.Raw),
  1387. }
  1388. payloadBytes, err := json.Marshal(nor)
  1389. assert.FatalError(t, err)
  1390. type test struct {
  1391. db acme.DB
  1392. ctx context.Context
  1393. statusCode int
  1394. err *acme.Error
  1395. }
  1396. var tests = map[string]func(t *testing.T) test{
  1397. "fail/no-account": func(t *testing.T) test {
  1398. return test{
  1399. ctx: context.WithValue(context.Background(), provisionerContextKey, prov),
  1400. statusCode: 400,
  1401. err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
  1402. }
  1403. },
  1404. "fail/nil-account": func(t *testing.T) test {
  1405. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1406. ctx = context.WithValue(ctx, accContextKey, nil)
  1407. return test{
  1408. ctx: ctx,
  1409. statusCode: 400,
  1410. err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
  1411. }
  1412. },
  1413. "fail/no-provisioner": func(t *testing.T) test {
  1414. acc := &acme.Account{ID: "accountID"}
  1415. ctx := context.WithValue(context.Background(), accContextKey, acc)
  1416. return test{
  1417. ctx: ctx,
  1418. statusCode: 500,
  1419. err: acme.NewErrorISE("provisioner does not exist"),
  1420. }
  1421. },
  1422. "fail/nil-provisioner": func(t *testing.T) test {
  1423. acc := &acme.Account{ID: "accountID"}
  1424. ctx := context.WithValue(context.Background(), provisionerContextKey, nil)
  1425. ctx = context.WithValue(ctx, accContextKey, acc)
  1426. return test{
  1427. ctx: ctx,
  1428. statusCode: 500,
  1429. err: acme.NewErrorISE("provisioner does not exist"),
  1430. }
  1431. },
  1432. "fail/no-payload": func(t *testing.T) test {
  1433. acc := &acme.Account{ID: "accountID"}
  1434. ctx := context.WithValue(context.Background(), accContextKey, acc)
  1435. ctx = context.WithValue(ctx, provisionerContextKey, prov)
  1436. return test{
  1437. ctx: ctx,
  1438. statusCode: 500,
  1439. err: acme.NewErrorISE("payload does not exist"),
  1440. }
  1441. },
  1442. "fail/nil-payload": func(t *testing.T) test {
  1443. acc := &acme.Account{ID: "accountID"}
  1444. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1445. ctx = context.WithValue(ctx, accContextKey, acc)
  1446. ctx = context.WithValue(ctx, payloadContextKey, nil)
  1447. return test{
  1448. ctx: ctx,
  1449. statusCode: 500,
  1450. err: acme.NewErrorISE("paylod does not exist"),
  1451. }
  1452. },
  1453. "fail/unmarshal-payload-error": func(t *testing.T) test {
  1454. acc := &acme.Account{ID: "accID"}
  1455. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1456. ctx = context.WithValue(ctx, accContextKey, acc)
  1457. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{})
  1458. return test{
  1459. ctx: ctx,
  1460. statusCode: 400,
  1461. err: acme.NewError(acme.ErrorMalformedType, "failed to unmarshal finalize-order request payload: unexpected end of JSON input"),
  1462. }
  1463. },
  1464. "fail/malformed-payload-error": func(t *testing.T) test {
  1465. acc := &acme.Account{ID: "accID"}
  1466. fr := &FinalizeRequest{}
  1467. b, err := json.Marshal(fr)
  1468. assert.FatalError(t, err)
  1469. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1470. ctx = context.WithValue(ctx, accContextKey, acc)
  1471. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  1472. return test{
  1473. ctx: ctx,
  1474. statusCode: 400,
  1475. err: acme.NewError(acme.ErrorMalformedType, "unable to parse csr: asn1: syntax error: sequence truncated"),
  1476. }
  1477. },
  1478. "fail/db.GetOrder-error": func(t *testing.T) test {
  1479. acc := &acme.Account{ID: "accountID"}
  1480. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1481. ctx = context.WithValue(ctx, accContextKey, acc)
  1482. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: payloadBytes})
  1483. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  1484. return test{
  1485. db: &acme.MockDB{
  1486. MockError: acme.NewErrorISE("force"),
  1487. },
  1488. ctx: ctx,
  1489. statusCode: 500,
  1490. err: acme.NewErrorISE("force"),
  1491. }
  1492. },
  1493. "fail/account-id-mismatch": func(t *testing.T) test {
  1494. acc := &acme.Account{ID: "accountID"}
  1495. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1496. ctx = context.WithValue(ctx, accContextKey, acc)
  1497. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: payloadBytes})
  1498. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  1499. return test{
  1500. db: &acme.MockDB{
  1501. MockGetOrder: func(ctx context.Context, id string) (*acme.Order, error) {
  1502. return &acme.Order{AccountID: "foo"}, nil
  1503. },
  1504. },
  1505. ctx: ctx,
  1506. statusCode: 401,
  1507. err: acme.NewError(acme.ErrorUnauthorizedType, "account id mismatch"),
  1508. }
  1509. },
  1510. "fail/provisioner-id-mismatch": func(t *testing.T) test {
  1511. acc := &acme.Account{ID: "accountID"}
  1512. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1513. ctx = context.WithValue(ctx, accContextKey, acc)
  1514. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: payloadBytes})
  1515. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  1516. return test{
  1517. db: &acme.MockDB{
  1518. MockGetOrder: func(ctx context.Context, id string) (*acme.Order, error) {
  1519. return &acme.Order{AccountID: "accountID", ProvisionerID: "bar"}, nil
  1520. },
  1521. },
  1522. ctx: ctx,
  1523. statusCode: 401,
  1524. err: acme.NewError(acme.ErrorUnauthorizedType, "provisioner id mismatch"),
  1525. }
  1526. },
  1527. "fail/order-finalize-error": func(t *testing.T) test {
  1528. acc := &acme.Account{ID: "accountID"}
  1529. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1530. ctx = context.WithValue(ctx, accContextKey, acc)
  1531. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: payloadBytes})
  1532. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  1533. return test{
  1534. db: &acme.MockDB{
  1535. MockGetOrder: func(ctx context.Context, id string) (*acme.Order, error) {
  1536. return &acme.Order{
  1537. AccountID: "accountID",
  1538. ProvisionerID: fmt.Sprintf("acme/%s", prov.GetName()),
  1539. ExpiresAt: clock.Now().Add(-time.Hour),
  1540. Status: acme.StatusReady,
  1541. }, nil
  1542. },
  1543. MockUpdateOrder: func(ctx context.Context, o *acme.Order) error {
  1544. return acme.NewErrorISE("force")
  1545. },
  1546. },
  1547. ctx: ctx,
  1548. statusCode: 500,
  1549. err: acme.NewErrorISE("force"),
  1550. }
  1551. },
  1552. "ok": func(t *testing.T) test {
  1553. acc := &acme.Account{ID: "accountID"}
  1554. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1555. ctx = context.WithValue(ctx, accContextKey, acc)
  1556. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: payloadBytes})
  1557. ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
  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{
  1563. ID: "orderID",
  1564. AccountID: "accountID",
  1565. ProvisionerID: fmt.Sprintf("acme/%s", prov.GetName()),
  1566. ExpiresAt: naf,
  1567. Status: acme.StatusValid,
  1568. AuthorizationIDs: []string{"foo", "bar", "baz"},
  1569. NotBefore: nbf,
  1570. NotAfter: naf,
  1571. Identifiers: []acme.Identifier{
  1572. {
  1573. Type: "dns",
  1574. Value: "example.com",
  1575. },
  1576. {
  1577. Type: "dns",
  1578. Value: "*.smallstep.com",
  1579. },
  1580. },
  1581. CertificateID: "certID",
  1582. }, nil
  1583. },
  1584. },
  1585. ctx: ctx,
  1586. statusCode: 200,
  1587. }
  1588. },
  1589. }
  1590. for name, run := range tests {
  1591. tc := run(t)
  1592. t.Run(name, func(t *testing.T) {
  1593. h := &Handler{linker: NewLinker("dns", "acme"), db: tc.db}
  1594. req := httptest.NewRequest("GET", url, nil)
  1595. req = req.WithContext(tc.ctx)
  1596. w := httptest.NewRecorder()
  1597. h.FinalizeOrder(w, req)
  1598. res := w.Result()
  1599. assert.Equals(t, res.StatusCode, tc.statusCode)
  1600. body, err := ioutil.ReadAll(res.Body)
  1601. res.Body.Close()
  1602. assert.FatalError(t, err)
  1603. if res.StatusCode >= 400 && assert.NotNil(t, tc.err) {
  1604. var ae acme.Error
  1605. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  1606. assert.Equals(t, ae.Type, tc.err.Type)
  1607. assert.Equals(t, ae.Detail, tc.err.Detail)
  1608. assert.Equals(t, ae.Identifier, tc.err.Identifier)
  1609. assert.Equals(t, ae.Subproblems, tc.err.Subproblems)
  1610. assert.Equals(t, res.Header["Content-Type"], []string{"application/problem+json"})
  1611. } else {
  1612. expB, err := json.Marshal(o)
  1613. assert.FatalError(t, err)
  1614. ro := new(acme.Order)
  1615. assert.FatalError(t, json.Unmarshal(body, ro))
  1616. assert.Equals(t, bytes.TrimSpace(body), expB)
  1617. assert.Equals(t, res.Header["Location"], []string{url})
  1618. assert.Equals(t, res.Header["Content-Type"], []string{"application/json"})
  1619. }
  1620. })
  1621. }
  1622. }
  1623. func TestHandler_challengeTypes(t *testing.T) {
  1624. type args struct {
  1625. az *acme.Authorization
  1626. }
  1627. tests := []struct {
  1628. name string
  1629. args args
  1630. want []acme.ChallengeType
  1631. }{
  1632. {
  1633. name: "ok/dns",
  1634. args: args{
  1635. az: &acme.Authorization{
  1636. Identifier: acme.Identifier{Type: "dns", Value: "example.com"},
  1637. Wildcard: false,
  1638. },
  1639. },
  1640. want: []acme.ChallengeType{acme.DNS01, acme.HTTP01, acme.TLSALPN01},
  1641. },
  1642. {
  1643. name: "ok/wildcard",
  1644. args: args{
  1645. az: &acme.Authorization{
  1646. Identifier: acme.Identifier{Type: "dns", Value: "*.example.com"},
  1647. Wildcard: true,
  1648. },
  1649. },
  1650. want: []acme.ChallengeType{acme.DNS01},
  1651. },
  1652. {
  1653. name: "ok/ip",
  1654. args: args{
  1655. az: &acme.Authorization{
  1656. Identifier: acme.Identifier{Type: "ip", Value: "192.168.42.42"},
  1657. Wildcard: false,
  1658. },
  1659. },
  1660. want: []acme.ChallengeType{acme.HTTP01, acme.TLSALPN01},
  1661. },
  1662. }
  1663. for _, tt := range tests {
  1664. t.Run(tt.name, func(t *testing.T) {
  1665. if got := challengeTypes(tt.args.az); !reflect.DeepEqual(got, tt.want) {
  1666. t.Errorf("Handler.challengeTypes() = %v, want %v", got, tt.want)
  1667. }
  1668. })
  1669. }
  1670. }