order_test.go 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945
  1. package acme
  2. import (
  3. "context"
  4. "crypto"
  5. "crypto/x509"
  6. "crypto/x509/pkix"
  7. "encoding/asn1"
  8. "encoding/json"
  9. "fmt"
  10. "net"
  11. "net/url"
  12. "reflect"
  13. "testing"
  14. "time"
  15. "github.com/google/go-cmp/cmp"
  16. "github.com/pkg/errors"
  17. "github.com/smallstep/assert"
  18. "github.com/smallstep/certificates/authority"
  19. "github.com/smallstep/certificates/authority/provisioner"
  20. "go.step.sm/crypto/keyutil"
  21. "go.step.sm/crypto/x509util"
  22. )
  23. func TestOrder_UpdateStatus(t *testing.T) {
  24. type test struct {
  25. o *Order
  26. err *Error
  27. db DB
  28. }
  29. tests := map[string]func(t *testing.T) test{
  30. "ok/already-invalid": func(t *testing.T) test {
  31. o := &Order{
  32. Status: StatusInvalid,
  33. }
  34. return test{
  35. o: o,
  36. }
  37. },
  38. "ok/already-valid": func(t *testing.T) test {
  39. o := &Order{
  40. Status: StatusInvalid,
  41. }
  42. return test{
  43. o: o,
  44. }
  45. },
  46. "fail/error-unexpected-status": func(t *testing.T) test {
  47. o := &Order{
  48. Status: "foo",
  49. }
  50. return test{
  51. o: o,
  52. err: NewErrorISE("unrecognized order status: %s", o.Status),
  53. }
  54. },
  55. "ok/ready-expired": func(t *testing.T) test {
  56. now := clock.Now()
  57. o := &Order{
  58. ID: "oID",
  59. AccountID: "accID",
  60. Status: StatusReady,
  61. ExpiresAt: now.Add(-5 * time.Minute),
  62. }
  63. return test{
  64. o: o,
  65. db: &MockDB{
  66. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  67. assert.Equals(t, updo.ID, o.ID)
  68. assert.Equals(t, updo.AccountID, o.AccountID)
  69. assert.Equals(t, updo.Status, StatusInvalid)
  70. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  71. return nil
  72. },
  73. },
  74. }
  75. },
  76. "fail/ready-expired-db.UpdateOrder-error": func(t *testing.T) test {
  77. now := clock.Now()
  78. o := &Order{
  79. ID: "oID",
  80. AccountID: "accID",
  81. Status: StatusReady,
  82. ExpiresAt: now.Add(-5 * time.Minute),
  83. }
  84. return test{
  85. o: o,
  86. db: &MockDB{
  87. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  88. assert.Equals(t, updo.ID, o.ID)
  89. assert.Equals(t, updo.AccountID, o.AccountID)
  90. assert.Equals(t, updo.Status, StatusInvalid)
  91. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  92. return errors.New("force")
  93. },
  94. },
  95. err: NewErrorISE("error updating order: force"),
  96. }
  97. },
  98. "ok/pending-expired": func(t *testing.T) test {
  99. now := clock.Now()
  100. o := &Order{
  101. ID: "oID",
  102. AccountID: "accID",
  103. Status: StatusPending,
  104. ExpiresAt: now.Add(-5 * time.Minute),
  105. }
  106. return test{
  107. o: o,
  108. db: &MockDB{
  109. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  110. assert.Equals(t, updo.ID, o.ID)
  111. assert.Equals(t, updo.AccountID, o.AccountID)
  112. assert.Equals(t, updo.Status, StatusInvalid)
  113. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  114. err := NewError(ErrorMalformedType, "order has expired")
  115. assert.HasPrefix(t, updo.Error.Err.Error(), err.Err.Error())
  116. assert.Equals(t, updo.Error.Type, err.Type)
  117. assert.Equals(t, updo.Error.Detail, err.Detail)
  118. assert.Equals(t, updo.Error.Status, err.Status)
  119. assert.Equals(t, updo.Error.Detail, err.Detail)
  120. return nil
  121. },
  122. },
  123. }
  124. },
  125. "ok/invalid": func(t *testing.T) test {
  126. now := clock.Now()
  127. o := &Order{
  128. ID: "oID",
  129. AccountID: "accID",
  130. Status: StatusPending,
  131. ExpiresAt: now.Add(5 * time.Minute),
  132. AuthorizationIDs: []string{"a", "b"},
  133. }
  134. az1 := &Authorization{
  135. ID: "a",
  136. Status: StatusValid,
  137. }
  138. az2 := &Authorization{
  139. ID: "b",
  140. Status: StatusInvalid,
  141. }
  142. return test{
  143. o: o,
  144. db: &MockDB{
  145. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  146. assert.Equals(t, updo.ID, o.ID)
  147. assert.Equals(t, updo.AccountID, o.AccountID)
  148. assert.Equals(t, updo.Status, StatusInvalid)
  149. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  150. return nil
  151. },
  152. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  153. switch id {
  154. case az1.ID:
  155. return az1, nil
  156. case az2.ID:
  157. return az2, nil
  158. default:
  159. assert.FatalError(t, errors.Errorf("unexpected authz key %s", id))
  160. return nil, errors.New("force")
  161. }
  162. },
  163. },
  164. }
  165. },
  166. "ok/still-pending": func(t *testing.T) test {
  167. now := clock.Now()
  168. o := &Order{
  169. ID: "oID",
  170. AccountID: "accID",
  171. Status: StatusPending,
  172. ExpiresAt: now.Add(5 * time.Minute),
  173. AuthorizationIDs: []string{"a", "b"},
  174. }
  175. az1 := &Authorization{
  176. ID: "a",
  177. Status: StatusValid,
  178. }
  179. az2 := &Authorization{
  180. ID: "b",
  181. Status: StatusPending,
  182. }
  183. return test{
  184. o: o,
  185. db: &MockDB{
  186. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  187. switch id {
  188. case az1.ID:
  189. return az1, nil
  190. case az2.ID:
  191. return az2, nil
  192. default:
  193. assert.FatalError(t, errors.Errorf("unexpected authz key %s", id))
  194. return nil, errors.New("force")
  195. }
  196. },
  197. },
  198. }
  199. },
  200. "ok/valid": func(t *testing.T) test {
  201. now := clock.Now()
  202. o := &Order{
  203. ID: "oID",
  204. AccountID: "accID",
  205. Status: StatusPending,
  206. ExpiresAt: now.Add(5 * time.Minute),
  207. AuthorizationIDs: []string{"a", "b"},
  208. }
  209. az1 := &Authorization{
  210. ID: "a",
  211. Status: StatusValid,
  212. }
  213. az2 := &Authorization{
  214. ID: "b",
  215. Status: StatusValid,
  216. }
  217. return test{
  218. o: o,
  219. db: &MockDB{
  220. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  221. assert.Equals(t, updo.ID, o.ID)
  222. assert.Equals(t, updo.AccountID, o.AccountID)
  223. assert.Equals(t, updo.Status, StatusReady)
  224. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  225. return nil
  226. },
  227. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  228. switch id {
  229. case az1.ID:
  230. return az1, nil
  231. case az2.ID:
  232. return az2, nil
  233. default:
  234. assert.FatalError(t, errors.Errorf("unexpected authz key %s", id))
  235. return nil, errors.New("force")
  236. }
  237. },
  238. },
  239. }
  240. },
  241. }
  242. for name, run := range tests {
  243. t.Run(name, func(t *testing.T) {
  244. tc := run(t)
  245. if err := tc.o.UpdateStatus(context.Background(), tc.db); err != nil {
  246. if assert.NotNil(t, tc.err) {
  247. var k *Error
  248. if errors.As(err, &k) {
  249. assert.Equals(t, k.Type, tc.err.Type)
  250. assert.Equals(t, k.Detail, tc.err.Detail)
  251. assert.Equals(t, k.Status, tc.err.Status)
  252. assert.Equals(t, k.Err.Error(), tc.err.Err.Error())
  253. assert.Equals(t, k.Detail, tc.err.Detail)
  254. } else {
  255. assert.FatalError(t, errors.New("unexpected error type"))
  256. }
  257. }
  258. } else {
  259. assert.Nil(t, tc.err)
  260. }
  261. })
  262. }
  263. }
  264. type mockSignAuth struct {
  265. sign func(csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error)
  266. areSANsAllowed func(ctx context.Context, sans []string) error
  267. loadProvisionerByName func(string) (provisioner.Interface, error)
  268. ret1, ret2 interface{}
  269. err error
  270. }
  271. func (m *mockSignAuth) Sign(csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  272. if m.sign != nil {
  273. return m.sign(csr, signOpts, extraOpts...)
  274. } else if m.err != nil {
  275. return nil, m.err
  276. }
  277. return []*x509.Certificate{m.ret1.(*x509.Certificate), m.ret2.(*x509.Certificate)}, m.err
  278. }
  279. func (m *mockSignAuth) AreSANsAllowed(ctx context.Context, sans []string) error {
  280. if m.areSANsAllowed != nil {
  281. return m.areSANsAllowed(ctx, sans)
  282. }
  283. return m.err
  284. }
  285. func (m *mockSignAuth) LoadProvisionerByName(name string) (provisioner.Interface, error) {
  286. if m.loadProvisionerByName != nil {
  287. return m.loadProvisionerByName(name)
  288. }
  289. return m.ret1.(provisioner.Interface), m.err
  290. }
  291. func (m *mockSignAuth) IsRevoked(string) (bool, error) {
  292. return false, nil
  293. }
  294. func (m *mockSignAuth) Revoke(context.Context, *authority.RevokeOptions) error {
  295. return nil
  296. }
  297. func TestOrder_Finalize(t *testing.T) {
  298. mustSigner := func(kty, crv string, size int) crypto.Signer {
  299. s, err := keyutil.GenerateSigner(kty, crv, size)
  300. if err != nil {
  301. t.Fatal(err)
  302. }
  303. return s
  304. }
  305. type test struct {
  306. o *Order
  307. err *Error
  308. db DB
  309. ca CertificateAuthority
  310. csr *x509.CertificateRequest
  311. prov Provisioner
  312. }
  313. tests := map[string]func(t *testing.T) test{
  314. "fail/invalid": func(t *testing.T) test {
  315. o := &Order{
  316. ID: "oid",
  317. Status: StatusInvalid,
  318. }
  319. return test{
  320. o: o,
  321. err: NewError(ErrorOrderNotReadyType, "order %s has been abandoned", o.ID),
  322. }
  323. },
  324. "fail/pending": func(t *testing.T) test {
  325. now := clock.Now()
  326. o := &Order{
  327. ID: "oID",
  328. AccountID: "accID",
  329. Status: StatusPending,
  330. ExpiresAt: now.Add(5 * time.Minute),
  331. AuthorizationIDs: []string{"a", "b"},
  332. }
  333. az1 := &Authorization{
  334. ID: "a",
  335. Status: StatusValid,
  336. }
  337. az2 := &Authorization{
  338. ID: "b",
  339. Status: StatusPending,
  340. ExpiresAt: now.Add(5 * time.Minute),
  341. }
  342. return test{
  343. o: o,
  344. db: &MockDB{
  345. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  346. switch id {
  347. case az1.ID:
  348. return az1, nil
  349. case az2.ID:
  350. return az2, nil
  351. default:
  352. assert.FatalError(t, errors.Errorf("unexpected authz key %s", id))
  353. return nil, errors.New("force")
  354. }
  355. },
  356. },
  357. err: NewError(ErrorOrderNotReadyType, "order %s is not ready", o.ID),
  358. }
  359. },
  360. "ok/already-valid": func(t *testing.T) test {
  361. o := &Order{
  362. ID: "oid",
  363. Status: StatusValid,
  364. }
  365. return test{
  366. o: o,
  367. }
  368. },
  369. "fail/error-unexpected-status": func(t *testing.T) test {
  370. now := clock.Now()
  371. o := &Order{
  372. ID: "oID",
  373. AccountID: "accID",
  374. Status: "foo",
  375. ExpiresAt: now.Add(5 * time.Minute),
  376. AuthorizationIDs: []string{"a", "b"},
  377. }
  378. return test{
  379. o: o,
  380. err: NewErrorISE("unrecognized order status: %s", o.Status),
  381. }
  382. },
  383. "fail/non-matching-permanent-identifier-common-name": func(t *testing.T) test {
  384. now := clock.Now()
  385. o := &Order{
  386. ID: "oID",
  387. AccountID: "accID",
  388. Status: StatusReady,
  389. ExpiresAt: now.Add(5 * time.Minute),
  390. AuthorizationIDs: []string{"a", "b"},
  391. Identifiers: []Identifier{
  392. {Type: "permanent-identifier", Value: "a-permanent-identifier"},
  393. },
  394. }
  395. signer := mustSigner("EC", "P-256", 0)
  396. fingerprint, err := keyutil.Fingerprint(signer.Public())
  397. if err != nil {
  398. t.Fatal(err)
  399. }
  400. csr := &x509.CertificateRequest{
  401. Subject: pkix.Name{
  402. CommonName: "a-different-identifier",
  403. },
  404. PublicKey: signer.Public(),
  405. ExtraExtensions: []pkix.Extension{
  406. {
  407. Id: asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 8, 3},
  408. Value: []byte("a-permanent-identifier"),
  409. },
  410. },
  411. }
  412. return test{
  413. o: o,
  414. csr: csr,
  415. db: &MockDB{
  416. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  417. switch id {
  418. case "a":
  419. return &Authorization{
  420. ID: id,
  421. Status: StatusValid,
  422. }, nil
  423. case "b":
  424. return &Authorization{
  425. ID: id,
  426. Fingerprint: fingerprint,
  427. Status: StatusValid,
  428. }, nil
  429. default:
  430. assert.FatalError(t, errors.Errorf("unexpected authorization %s", id))
  431. return nil, errors.New("force")
  432. }
  433. },
  434. MockUpdateOrder: func(ctx context.Context, o *Order) error {
  435. return nil
  436. },
  437. },
  438. err: &Error{
  439. Type: "urn:ietf:params:acme:error:badCSR",
  440. Detail: "The CSR is unacceptable",
  441. Status: 400,
  442. Err: fmt.Errorf("CSR Subject Common Name does not match identifiers exactly: "+
  443. "CSR Subject Common Name = %s, Order Permanent Identifier = %s", csr.Subject.CommonName, "a-permanent-identifier"),
  444. },
  445. }
  446. },
  447. "fail/error-provisioner-auth": func(t *testing.T) test {
  448. now := clock.Now()
  449. o := &Order{
  450. ID: "oID",
  451. AccountID: "accID",
  452. Status: StatusReady,
  453. ExpiresAt: now.Add(5 * time.Minute),
  454. AuthorizationIDs: []string{"a", "b"},
  455. Identifiers: []Identifier{
  456. {Type: "dns", Value: "foo.internal"},
  457. {Type: "dns", Value: "bar.internal"},
  458. },
  459. }
  460. csr := &x509.CertificateRequest{
  461. Subject: pkix.Name{
  462. CommonName: "foo.internal",
  463. },
  464. DNSNames: []string{"bar.internal"},
  465. }
  466. return test{
  467. o: o,
  468. csr: csr,
  469. prov: &MockProvisioner{
  470. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  471. assert.Equals(t, token, "")
  472. return nil, errors.New("force")
  473. },
  474. },
  475. db: &MockDB{
  476. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  477. return &Authorization{ID: id, Status: StatusValid}, nil
  478. },
  479. },
  480. err: NewErrorISE("error retrieving authorization options from ACME provisioner: force"),
  481. }
  482. },
  483. "fail/error-template-options": func(t *testing.T) test {
  484. now := clock.Now()
  485. o := &Order{
  486. ID: "oID",
  487. AccountID: "accID",
  488. Status: StatusReady,
  489. ExpiresAt: now.Add(5 * time.Minute),
  490. AuthorizationIDs: []string{"a", "b"},
  491. Identifiers: []Identifier{
  492. {Type: "dns", Value: "foo.internal"},
  493. {Type: "dns", Value: "bar.internal"},
  494. },
  495. }
  496. csr := &x509.CertificateRequest{
  497. Subject: pkix.Name{
  498. CommonName: "foo.internal",
  499. },
  500. DNSNames: []string{"bar.internal"},
  501. }
  502. return test{
  503. o: o,
  504. csr: csr,
  505. prov: &MockProvisioner{
  506. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  507. assert.Equals(t, token, "")
  508. return nil, nil
  509. },
  510. MgetOptions: func() *provisioner.Options {
  511. return &provisioner.Options{
  512. X509: &provisioner.X509Options{
  513. TemplateData: json.RawMessage([]byte("fo{o")),
  514. },
  515. }
  516. },
  517. },
  518. db: &MockDB{
  519. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  520. return &Authorization{ID: id, Status: StatusValid}, nil
  521. },
  522. },
  523. err: NewErrorISE("error creating template options from ACME provisioner: error unmarshaling template data: invalid character 'o' in literal false (expecting 'a')"),
  524. }
  525. },
  526. "fail/error-ca-sign": func(t *testing.T) test {
  527. now := clock.Now()
  528. o := &Order{
  529. ID: "oID",
  530. AccountID: "accID",
  531. Status: StatusReady,
  532. ExpiresAt: now.Add(5 * time.Minute),
  533. AuthorizationIDs: []string{"a", "b"},
  534. Identifiers: []Identifier{
  535. {Type: "dns", Value: "foo.internal"},
  536. {Type: "dns", Value: "bar.internal"},
  537. },
  538. }
  539. csr := &x509.CertificateRequest{
  540. Subject: pkix.Name{
  541. CommonName: "foo.internal",
  542. },
  543. DNSNames: []string{"bar.internal"},
  544. }
  545. return test{
  546. o: o,
  547. csr: csr,
  548. prov: &MockProvisioner{
  549. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  550. assert.Equals(t, token, "")
  551. return nil, nil
  552. },
  553. MgetOptions: func() *provisioner.Options {
  554. return nil
  555. },
  556. },
  557. ca: &mockSignAuth{
  558. sign: func(_csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  559. assert.Equals(t, _csr, csr)
  560. return nil, errors.New("force")
  561. },
  562. },
  563. db: &MockDB{
  564. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  565. return &Authorization{ID: id, Status: StatusValid}, nil
  566. },
  567. },
  568. err: NewErrorISE("error signing certificate for order oID: force"),
  569. }
  570. },
  571. "fail/error-db.CreateCertificate": func(t *testing.T) test {
  572. now := clock.Now()
  573. o := &Order{
  574. ID: "oID",
  575. AccountID: "accID",
  576. Status: StatusReady,
  577. ExpiresAt: now.Add(5 * time.Minute),
  578. AuthorizationIDs: []string{"a", "b"},
  579. Identifiers: []Identifier{
  580. {Type: "dns", Value: "foo.internal"},
  581. {Type: "dns", Value: "bar.internal"},
  582. },
  583. }
  584. csr := &x509.CertificateRequest{
  585. Subject: pkix.Name{
  586. CommonName: "foo.internal",
  587. },
  588. DNSNames: []string{"bar.internal"},
  589. }
  590. foo := &x509.Certificate{Subject: pkix.Name{CommonName: "foo"}}
  591. bar := &x509.Certificate{Subject: pkix.Name{CommonName: "bar"}}
  592. baz := &x509.Certificate{Subject: pkix.Name{CommonName: "baz"}}
  593. return test{
  594. o: o,
  595. csr: csr,
  596. prov: &MockProvisioner{
  597. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  598. assert.Equals(t, token, "")
  599. return nil, nil
  600. },
  601. MgetOptions: func() *provisioner.Options {
  602. return nil
  603. },
  604. },
  605. ca: &mockSignAuth{
  606. sign: func(_csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  607. assert.Equals(t, _csr, csr)
  608. return []*x509.Certificate{foo, bar, baz}, nil
  609. },
  610. },
  611. db: &MockDB{
  612. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  613. return &Authorization{ID: id, Status: StatusValid}, nil
  614. },
  615. MockCreateCertificate: func(ctx context.Context, cert *Certificate) error {
  616. assert.Equals(t, cert.AccountID, o.AccountID)
  617. assert.Equals(t, cert.OrderID, o.ID)
  618. assert.Equals(t, cert.Leaf, foo)
  619. assert.Equals(t, cert.Intermediates, []*x509.Certificate{bar, baz})
  620. return errors.New("force")
  621. },
  622. },
  623. err: NewErrorISE("error creating certificate for order oID: force"),
  624. }
  625. },
  626. "fail/error-db.UpdateOrder": func(t *testing.T) test {
  627. now := clock.Now()
  628. o := &Order{
  629. ID: "oID",
  630. AccountID: "accID",
  631. Status: StatusReady,
  632. ExpiresAt: now.Add(5 * time.Minute),
  633. AuthorizationIDs: []string{"a", "b"},
  634. Identifiers: []Identifier{
  635. {Type: "dns", Value: "foo.internal"},
  636. {Type: "dns", Value: "bar.internal"},
  637. },
  638. }
  639. csr := &x509.CertificateRequest{
  640. Subject: pkix.Name{
  641. CommonName: "foo.internal",
  642. },
  643. DNSNames: []string{"bar.internal"},
  644. }
  645. foo := &x509.Certificate{Subject: pkix.Name{CommonName: "foo"}}
  646. bar := &x509.Certificate{Subject: pkix.Name{CommonName: "bar"}}
  647. baz := &x509.Certificate{Subject: pkix.Name{CommonName: "baz"}}
  648. return test{
  649. o: o,
  650. csr: csr,
  651. prov: &MockProvisioner{
  652. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  653. assert.Equals(t, token, "")
  654. return nil, nil
  655. },
  656. MgetOptions: func() *provisioner.Options {
  657. return nil
  658. },
  659. },
  660. ca: &mockSignAuth{
  661. sign: func(_csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  662. assert.Equals(t, _csr, csr)
  663. return []*x509.Certificate{foo, bar, baz}, nil
  664. },
  665. },
  666. db: &MockDB{
  667. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  668. return &Authorization{ID: id, Status: StatusValid}, nil
  669. },
  670. MockCreateCertificate: func(ctx context.Context, cert *Certificate) error {
  671. cert.ID = "certID"
  672. assert.Equals(t, cert.AccountID, o.AccountID)
  673. assert.Equals(t, cert.OrderID, o.ID)
  674. assert.Equals(t, cert.Leaf, foo)
  675. assert.Equals(t, cert.Intermediates, []*x509.Certificate{bar, baz})
  676. return nil
  677. },
  678. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  679. assert.Equals(t, updo.CertificateID, "certID")
  680. assert.Equals(t, updo.Status, StatusValid)
  681. assert.Equals(t, updo.ID, o.ID)
  682. assert.Equals(t, updo.AccountID, o.AccountID)
  683. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  684. assert.Equals(t, updo.AuthorizationIDs, o.AuthorizationIDs)
  685. assert.Equals(t, updo.Identifiers, o.Identifiers)
  686. return errors.New("force")
  687. },
  688. },
  689. err: NewErrorISE("error updating order oID: force"),
  690. }
  691. },
  692. "fail/csr-fingerprint": func(t *testing.T) test {
  693. now := clock.Now()
  694. o := &Order{
  695. ID: "oID",
  696. AccountID: "accID",
  697. Status: StatusReady,
  698. ExpiresAt: now.Add(5 * time.Minute),
  699. AuthorizationIDs: []string{"a", "b"},
  700. Identifiers: []Identifier{
  701. {Type: "permanent-identifier", Value: "a-permanent-identifier"},
  702. },
  703. }
  704. signer := mustSigner("EC", "P-256", 0)
  705. csr := &x509.CertificateRequest{
  706. Subject: pkix.Name{
  707. CommonName: "a-permanent-identifier",
  708. },
  709. PublicKey: signer.Public(),
  710. ExtraExtensions: []pkix.Extension{
  711. {
  712. Id: asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 8, 3},
  713. Value: []byte("a-permanent-identifier"),
  714. },
  715. },
  716. }
  717. leaf := &x509.Certificate{
  718. Subject: pkix.Name{CommonName: "a-permanent-identifier"},
  719. PublicKey: signer.Public(),
  720. ExtraExtensions: []pkix.Extension{
  721. {
  722. Id: asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 8, 3},
  723. Value: []byte("a-permanent-identifier"),
  724. },
  725. },
  726. }
  727. inter := &x509.Certificate{Subject: pkix.Name{CommonName: "inter"}}
  728. root := &x509.Certificate{Subject: pkix.Name{CommonName: "root"}}
  729. return test{
  730. o: o,
  731. csr: csr,
  732. prov: &MockProvisioner{
  733. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  734. assert.Equals(t, token, "")
  735. return nil, nil
  736. },
  737. MgetOptions: func() *provisioner.Options {
  738. return nil
  739. },
  740. },
  741. ca: &mockSignAuth{
  742. sign: func(_csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  743. assert.Equals(t, _csr, csr)
  744. return []*x509.Certificate{leaf, inter, root}, nil
  745. },
  746. },
  747. db: &MockDB{
  748. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  749. return &Authorization{
  750. ID: id,
  751. Fingerprint: "other-fingerprint",
  752. Status: StatusValid,
  753. }, nil
  754. },
  755. MockCreateCertificate: func(ctx context.Context, cert *Certificate) error {
  756. cert.ID = "certID"
  757. assert.Equals(t, cert.AccountID, o.AccountID)
  758. assert.Equals(t, cert.OrderID, o.ID)
  759. assert.Equals(t, cert.Leaf, leaf)
  760. assert.Equals(t, cert.Intermediates, []*x509.Certificate{inter, root})
  761. return nil
  762. },
  763. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  764. assert.Equals(t, updo.CertificateID, "certID")
  765. assert.Equals(t, updo.Status, StatusValid)
  766. assert.Equals(t, updo.ID, o.ID)
  767. assert.Equals(t, updo.AccountID, o.AccountID)
  768. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  769. assert.Equals(t, updo.AuthorizationIDs, o.AuthorizationIDs)
  770. assert.Equals(t, updo.Identifiers, o.Identifiers)
  771. return nil
  772. },
  773. },
  774. err: NewError(ErrorUnauthorizedType, "order oID csr does not match the attested key"),
  775. }
  776. },
  777. "ok/permanent-identifier": func(t *testing.T) test {
  778. now := clock.Now()
  779. o := &Order{
  780. ID: "oID",
  781. AccountID: "accID",
  782. Status: StatusReady,
  783. ExpiresAt: now.Add(5 * time.Minute),
  784. AuthorizationIDs: []string{"a", "b"},
  785. Identifiers: []Identifier{
  786. {Type: "permanent-identifier", Value: "a-permanent-identifier"},
  787. },
  788. }
  789. signer := mustSigner("EC", "P-256", 0)
  790. fingerprint, err := keyutil.Fingerprint(signer.Public())
  791. if err != nil {
  792. t.Fatal(err)
  793. }
  794. csr := &x509.CertificateRequest{
  795. Subject: pkix.Name{
  796. CommonName: "a-permanent-identifier",
  797. },
  798. PublicKey: signer.Public(),
  799. ExtraExtensions: []pkix.Extension{
  800. {
  801. Id: asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 8, 3},
  802. Value: []byte("a-permanent-identifier"),
  803. },
  804. },
  805. }
  806. leaf := &x509.Certificate{
  807. Subject: pkix.Name{CommonName: "a-permanent-identifier"},
  808. PublicKey: signer.Public(),
  809. ExtraExtensions: []pkix.Extension{
  810. {
  811. Id: asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 8, 3},
  812. Value: []byte("a-permanent-identifier"),
  813. },
  814. },
  815. }
  816. inter := &x509.Certificate{Subject: pkix.Name{CommonName: "inter"}}
  817. root := &x509.Certificate{Subject: pkix.Name{CommonName: "root"}}
  818. return test{
  819. o: o,
  820. csr: csr,
  821. prov: &MockProvisioner{
  822. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  823. assert.Equals(t, token, "")
  824. return nil, nil
  825. },
  826. MgetOptions: func() *provisioner.Options {
  827. return nil
  828. },
  829. },
  830. ca: &mockSignAuth{
  831. sign: func(_csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  832. assert.Equals(t, _csr, csr)
  833. return []*x509.Certificate{leaf, inter, root}, nil
  834. },
  835. },
  836. db: &MockDB{
  837. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  838. switch id {
  839. case "a":
  840. return &Authorization{
  841. ID: id,
  842. Status: StatusValid,
  843. }, nil
  844. case "b":
  845. return &Authorization{
  846. ID: id,
  847. Fingerprint: fingerprint,
  848. Status: StatusValid,
  849. }, nil
  850. default:
  851. assert.FatalError(t, errors.Errorf("unexpected authorization %s", id))
  852. return nil, errors.New("force")
  853. }
  854. },
  855. MockCreateCertificate: func(ctx context.Context, cert *Certificate) error {
  856. cert.ID = "certID"
  857. assert.Equals(t, cert.AccountID, o.AccountID)
  858. assert.Equals(t, cert.OrderID, o.ID)
  859. assert.Equals(t, cert.Leaf, leaf)
  860. assert.Equals(t, cert.Intermediates, []*x509.Certificate{inter, root})
  861. return nil
  862. },
  863. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  864. assert.Equals(t, updo.CertificateID, "certID")
  865. assert.Equals(t, updo.Status, StatusValid)
  866. assert.Equals(t, updo.ID, o.ID)
  867. assert.Equals(t, updo.AccountID, o.AccountID)
  868. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  869. assert.Equals(t, updo.AuthorizationIDs, o.AuthorizationIDs)
  870. assert.Equals(t, updo.Identifiers, o.Identifiers)
  871. return nil
  872. },
  873. },
  874. }
  875. },
  876. "ok/permanent-identifier-only": func(t *testing.T) test {
  877. now := clock.Now()
  878. o := &Order{
  879. ID: "oID",
  880. AccountID: "accID",
  881. Status: StatusReady,
  882. ExpiresAt: now.Add(5 * time.Minute),
  883. AuthorizationIDs: []string{"a", "b"},
  884. Identifiers: []Identifier{
  885. {Type: "dns", Value: "foo.internal"},
  886. {Type: "permanent-identifier", Value: "a-permanent-identifier"},
  887. },
  888. }
  889. signer := mustSigner("EC", "P-256", 0)
  890. fingerprint, err := keyutil.Fingerprint(signer.Public())
  891. if err != nil {
  892. t.Fatal(err)
  893. }
  894. csr := &x509.CertificateRequest{
  895. Subject: pkix.Name{
  896. CommonName: "a-permanent-identifier",
  897. },
  898. DNSNames: []string{"foo.internal"},
  899. PublicKey: signer.Public(),
  900. ExtraExtensions: []pkix.Extension{
  901. {
  902. Id: asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 8, 3},
  903. Value: []byte("a-permanent-identifier"),
  904. },
  905. },
  906. }
  907. leaf := &x509.Certificate{
  908. Subject: pkix.Name{CommonName: "a-permanent-identifier"},
  909. PublicKey: signer.Public(),
  910. ExtraExtensions: []pkix.Extension{
  911. {
  912. Id: asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 8, 3},
  913. Value: []byte("a-permanent-identifier"),
  914. },
  915. },
  916. }
  917. inter := &x509.Certificate{Subject: pkix.Name{CommonName: "inter"}}
  918. root := &x509.Certificate{Subject: pkix.Name{CommonName: "root"}}
  919. return test{
  920. o: o,
  921. csr: csr,
  922. prov: &MockProvisioner{
  923. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  924. assert.Equals(t, token, "")
  925. return nil, nil
  926. },
  927. MgetOptions: func() *provisioner.Options {
  928. return nil
  929. },
  930. },
  931. // TODO(hs): we should work on making the mocks more realistic. Ideally, we should get rid of
  932. // the mock entirely, relying on an instances of provisioner, authority and DB (possibly hardest), so
  933. // that behavior of the tests is what an actual CA would do. We could gradually phase them out by
  934. // using the mocking functions as a wrapper for actual test helpers generated per test case or per
  935. // function that's tested.
  936. ca: &mockSignAuth{
  937. sign: func(_csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  938. assert.Equals(t, _csr, csr)
  939. return []*x509.Certificate{leaf, inter, root}, nil
  940. },
  941. },
  942. db: &MockDB{
  943. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  944. return &Authorization{
  945. ID: id,
  946. Fingerprint: fingerprint,
  947. Status: StatusValid,
  948. }, nil
  949. },
  950. MockCreateCertificate: func(ctx context.Context, cert *Certificate) error {
  951. cert.ID = "certID"
  952. assert.Equals(t, cert.AccountID, o.AccountID)
  953. assert.Equals(t, cert.OrderID, o.ID)
  954. assert.Equals(t, cert.Leaf, leaf)
  955. assert.Equals(t, cert.Intermediates, []*x509.Certificate{inter, root})
  956. return nil
  957. },
  958. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  959. assert.Equals(t, updo.CertificateID, "certID")
  960. assert.Equals(t, updo.Status, StatusValid)
  961. assert.Equals(t, updo.ID, o.ID)
  962. assert.Equals(t, updo.AccountID, o.AccountID)
  963. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  964. assert.Equals(t, updo.AuthorizationIDs, o.AuthorizationIDs)
  965. assert.Equals(t, updo.Identifiers, o.Identifiers)
  966. return nil
  967. },
  968. },
  969. }
  970. },
  971. "ok/new-cert-dns": func(t *testing.T) test {
  972. now := clock.Now()
  973. o := &Order{
  974. ID: "oID",
  975. AccountID: "accID",
  976. Status: StatusReady,
  977. ExpiresAt: now.Add(5 * time.Minute),
  978. AuthorizationIDs: []string{"a", "b"},
  979. Identifiers: []Identifier{
  980. {Type: "dns", Value: "foo.internal"},
  981. {Type: "dns", Value: "bar.internal"},
  982. },
  983. }
  984. csr := &x509.CertificateRequest{
  985. Subject: pkix.Name{
  986. CommonName: "foo.internal",
  987. },
  988. DNSNames: []string{"bar.internal"},
  989. }
  990. foo := &x509.Certificate{Subject: pkix.Name{CommonName: "foo"}}
  991. bar := &x509.Certificate{Subject: pkix.Name{CommonName: "bar"}}
  992. baz := &x509.Certificate{Subject: pkix.Name{CommonName: "baz"}}
  993. return test{
  994. o: o,
  995. csr: csr,
  996. prov: &MockProvisioner{
  997. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  998. assert.Equals(t, token, "")
  999. return nil, nil
  1000. },
  1001. MgetOptions: func() *provisioner.Options {
  1002. return nil
  1003. },
  1004. },
  1005. ca: &mockSignAuth{
  1006. sign: func(_csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  1007. assert.Equals(t, _csr, csr)
  1008. return []*x509.Certificate{foo, bar, baz}, nil
  1009. },
  1010. },
  1011. db: &MockDB{
  1012. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  1013. return &Authorization{ID: id, Status: StatusValid}, nil
  1014. },
  1015. MockCreateCertificate: func(ctx context.Context, cert *Certificate) error {
  1016. cert.ID = "certID"
  1017. assert.Equals(t, cert.AccountID, o.AccountID)
  1018. assert.Equals(t, cert.OrderID, o.ID)
  1019. assert.Equals(t, cert.Leaf, foo)
  1020. assert.Equals(t, cert.Intermediates, []*x509.Certificate{bar, baz})
  1021. return nil
  1022. },
  1023. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  1024. assert.Equals(t, updo.CertificateID, "certID")
  1025. assert.Equals(t, updo.Status, StatusValid)
  1026. assert.Equals(t, updo.ID, o.ID)
  1027. assert.Equals(t, updo.AccountID, o.AccountID)
  1028. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  1029. assert.Equals(t, updo.AuthorizationIDs, o.AuthorizationIDs)
  1030. assert.Equals(t, updo.Identifiers, o.Identifiers)
  1031. return nil
  1032. },
  1033. },
  1034. }
  1035. },
  1036. "ok/new-cert-ip": func(t *testing.T) test {
  1037. now := clock.Now()
  1038. o := &Order{
  1039. ID: "oID",
  1040. AccountID: "accID",
  1041. Status: StatusReady,
  1042. ExpiresAt: now.Add(5 * time.Minute),
  1043. AuthorizationIDs: []string{"a", "b"},
  1044. Identifiers: []Identifier{
  1045. {Type: "ip", Value: "192.168.42.42"},
  1046. {Type: "ip", Value: "192.168.43.42"},
  1047. },
  1048. }
  1049. csr := &x509.CertificateRequest{
  1050. IPAddresses: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.43.42")}, // in case of IPs, no Common Name
  1051. }
  1052. foo := &x509.Certificate{Subject: pkix.Name{CommonName: "foo"}}
  1053. bar := &x509.Certificate{Subject: pkix.Name{CommonName: "bar"}}
  1054. baz := &x509.Certificate{Subject: pkix.Name{CommonName: "baz"}}
  1055. return test{
  1056. o: o,
  1057. csr: csr,
  1058. prov: &MockProvisioner{
  1059. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  1060. assert.Equals(t, token, "")
  1061. return nil, nil
  1062. },
  1063. MgetOptions: func() *provisioner.Options {
  1064. return nil
  1065. },
  1066. },
  1067. ca: &mockSignAuth{
  1068. sign: func(_csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  1069. assert.Equals(t, _csr, csr)
  1070. return []*x509.Certificate{foo, bar, baz}, nil
  1071. },
  1072. },
  1073. db: &MockDB{
  1074. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  1075. return &Authorization{ID: id, Status: StatusValid}, nil
  1076. },
  1077. MockCreateCertificate: func(ctx context.Context, cert *Certificate) error {
  1078. cert.ID = "certID"
  1079. assert.Equals(t, cert.AccountID, o.AccountID)
  1080. assert.Equals(t, cert.OrderID, o.ID)
  1081. assert.Equals(t, cert.Leaf, foo)
  1082. assert.Equals(t, cert.Intermediates, []*x509.Certificate{bar, baz})
  1083. return nil
  1084. },
  1085. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  1086. assert.Equals(t, updo.CertificateID, "certID")
  1087. assert.Equals(t, updo.Status, StatusValid)
  1088. assert.Equals(t, updo.ID, o.ID)
  1089. assert.Equals(t, updo.AccountID, o.AccountID)
  1090. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  1091. assert.Equals(t, updo.AuthorizationIDs, o.AuthorizationIDs)
  1092. assert.Equals(t, updo.Identifiers, o.Identifiers)
  1093. return nil
  1094. },
  1095. },
  1096. }
  1097. },
  1098. "ok/new-cert-dns-and-ip": func(t *testing.T) test {
  1099. now := clock.Now()
  1100. o := &Order{
  1101. ID: "oID",
  1102. AccountID: "accID",
  1103. Status: StatusReady,
  1104. ExpiresAt: now.Add(5 * time.Minute),
  1105. AuthorizationIDs: []string{"a", "b"},
  1106. Identifiers: []Identifier{
  1107. {Type: "dns", Value: "foo.internal"},
  1108. {Type: "ip", Value: "192.168.42.42"},
  1109. },
  1110. }
  1111. csr := &x509.CertificateRequest{
  1112. Subject: pkix.Name{
  1113. CommonName: "foo.internal",
  1114. },
  1115. IPAddresses: []net.IP{net.ParseIP("192.168.42.42")},
  1116. }
  1117. foo := &x509.Certificate{Subject: pkix.Name{CommonName: "foo"}}
  1118. bar := &x509.Certificate{Subject: pkix.Name{CommonName: "bar"}}
  1119. baz := &x509.Certificate{Subject: pkix.Name{CommonName: "baz"}}
  1120. return test{
  1121. o: o,
  1122. csr: csr,
  1123. prov: &MockProvisioner{
  1124. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  1125. assert.Equals(t, token, "")
  1126. return nil, nil
  1127. },
  1128. MgetOptions: func() *provisioner.Options {
  1129. return nil
  1130. },
  1131. },
  1132. ca: &mockSignAuth{
  1133. sign: func(_csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  1134. assert.Equals(t, _csr, csr)
  1135. return []*x509.Certificate{foo, bar, baz}, nil
  1136. },
  1137. },
  1138. db: &MockDB{
  1139. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  1140. return &Authorization{ID: id, Status: StatusValid}, nil
  1141. },
  1142. MockCreateCertificate: func(ctx context.Context, cert *Certificate) error {
  1143. cert.ID = "certID"
  1144. assert.Equals(t, cert.AccountID, o.AccountID)
  1145. assert.Equals(t, cert.OrderID, o.ID)
  1146. assert.Equals(t, cert.Leaf, foo)
  1147. assert.Equals(t, cert.Intermediates, []*x509.Certificate{bar, baz})
  1148. return nil
  1149. },
  1150. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  1151. assert.Equals(t, updo.CertificateID, "certID")
  1152. assert.Equals(t, updo.Status, StatusValid)
  1153. assert.Equals(t, updo.ID, o.ID)
  1154. assert.Equals(t, updo.AccountID, o.AccountID)
  1155. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  1156. assert.Equals(t, updo.AuthorizationIDs, o.AuthorizationIDs)
  1157. assert.Equals(t, updo.Identifiers, o.Identifiers)
  1158. return nil
  1159. },
  1160. },
  1161. }
  1162. },
  1163. }
  1164. for name, run := range tests {
  1165. t.Run(name, func(t *testing.T) {
  1166. tc := run(t)
  1167. if err := tc.o.Finalize(context.Background(), tc.db, tc.csr, tc.ca, tc.prov); err != nil {
  1168. if assert.NotNil(t, tc.err) {
  1169. var k *Error
  1170. if errors.As(err, &k) {
  1171. assert.Equals(t, k.Type, tc.err.Type)
  1172. assert.Equals(t, k.Detail, tc.err.Detail)
  1173. assert.Equals(t, k.Status, tc.err.Status)
  1174. assert.Equals(t, k.Err.Error(), tc.err.Err.Error())
  1175. assert.Equals(t, k.Detail, tc.err.Detail)
  1176. } else {
  1177. assert.FatalError(t, errors.New("unexpected error type"))
  1178. }
  1179. }
  1180. } else {
  1181. assert.Nil(t, tc.err)
  1182. }
  1183. })
  1184. }
  1185. }
  1186. func Test_uniqueSortedIPs(t *testing.T) {
  1187. type args struct {
  1188. ips []net.IP
  1189. }
  1190. tests := []struct {
  1191. name string
  1192. args args
  1193. want []net.IP
  1194. }{
  1195. {
  1196. name: "ok/empty",
  1197. args: args{
  1198. ips: []net.IP{},
  1199. },
  1200. want: []net.IP{},
  1201. },
  1202. {
  1203. name: "ok/single-ipv4",
  1204. args: args{
  1205. ips: []net.IP{net.ParseIP("192.168.42.42")},
  1206. },
  1207. want: []net.IP{net.ParseIP("192.168.42.42")},
  1208. },
  1209. {
  1210. name: "ok/multiple-ipv4",
  1211. args: args{
  1212. ips: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.42.10"), net.ParseIP("192.168.42.1"), net.ParseIP("127.0.0.1")},
  1213. },
  1214. want: []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("192.168.42.1"), net.ParseIP("192.168.42.10"), net.ParseIP("192.168.42.42")},
  1215. }, {
  1216. name: "ok/multiple-ipv4-with-varying-byte-representations",
  1217. args: args{
  1218. ips: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.42.10"), net.ParseIP("192.168.42.1"), []byte{0x7f, 0x0, 0x0, 0x1}},
  1219. },
  1220. want: []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("192.168.42.1"), net.ParseIP("192.168.42.10"), net.ParseIP("192.168.42.42")},
  1221. },
  1222. {
  1223. name: "ok/unique-ipv4",
  1224. args: args{
  1225. ips: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.42.42")},
  1226. },
  1227. want: []net.IP{net.ParseIP("192.168.42.42")},
  1228. },
  1229. {
  1230. name: "ok/single-ipv6",
  1231. args: args{
  1232. ips: []net.IP{net.ParseIP("2001:db8::30")},
  1233. },
  1234. want: []net.IP{net.ParseIP("2001:db8::30")},
  1235. },
  1236. {
  1237. name: "ok/multiple-ipv6",
  1238. args: args{
  1239. ips: []net.IP{net.ParseIP("2001:db8::30"), net.ParseIP("2001:db8::20"), net.ParseIP("2001:db8::10")},
  1240. },
  1241. want: []net.IP{net.ParseIP("2001:db8::10"), net.ParseIP("2001:db8::20"), net.ParseIP("2001:db8::30")},
  1242. },
  1243. {
  1244. name: "ok/unique-ipv6",
  1245. args: args{
  1246. ips: []net.IP{net.ParseIP("2001:db8::1"), net.ParseIP("2001:db8::1")},
  1247. },
  1248. want: []net.IP{net.ParseIP("2001:db8::1")},
  1249. },
  1250. {
  1251. name: "ok/mixed-ipv4-and-ipv6",
  1252. args: args{
  1253. ips: []net.IP{net.ParseIP("2001:db8::1"), net.ParseIP("2001:db8::1"), net.ParseIP("192.168.42.42"), net.ParseIP("192.168.42.42")},
  1254. },
  1255. want: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("2001:db8::1")},
  1256. },
  1257. {
  1258. name: "ok/mixed-ipv4-and-ipv6-and-varying-byte-representations",
  1259. args: args{
  1260. ips: []net.IP{net.ParseIP("2001:db8::1"), net.ParseIP("2001:db8::1"), net.ParseIP("192.168.42.42"), net.ParseIP("192.168.42.42"), []byte{0x7f, 0x0, 0x0, 0x1}},
  1261. },
  1262. want: []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("192.168.42.42"), net.ParseIP("2001:db8::1")},
  1263. },
  1264. {
  1265. name: "ok/mixed-ipv4-and-ipv6-and-more-varying-byte-representations",
  1266. args: args{
  1267. ips: []net.IP{net.ParseIP("2001:db8::1"), net.ParseIP("2001:db8::1"), net.ParseIP("192.168.42.42"), net.ParseIP("2001:db8::2"), net.ParseIP("192.168.42.42"), []byte{0x7f, 0x0, 0x0, 0x1}, []byte{0x7f, 0x0, 0x0, 0x1}, []byte{0x7f, 0x0, 0x0, 0x2}},
  1268. },
  1269. want: []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("127.0.0.2"), net.ParseIP("192.168.42.42"), net.ParseIP("2001:db8::1"), net.ParseIP("2001:db8::2")},
  1270. },
  1271. }
  1272. for _, tt := range tests {
  1273. t.Run(tt.name, func(t *testing.T) {
  1274. got := uniqueSortedIPs(tt.args.ips)
  1275. if !cmp.Equal(tt.want, got) {
  1276. t.Errorf("uniqueSortedIPs() diff =\n%s", cmp.Diff(tt.want, got))
  1277. }
  1278. })
  1279. }
  1280. }
  1281. func Test_numberOfIdentifierType(t *testing.T) {
  1282. type args struct {
  1283. typ IdentifierType
  1284. ids []Identifier
  1285. }
  1286. tests := []struct {
  1287. name string
  1288. args args
  1289. want int
  1290. }{
  1291. {
  1292. name: "ok/no-identifiers",
  1293. args: args{
  1294. typ: DNS,
  1295. ids: []Identifier{},
  1296. },
  1297. want: 0,
  1298. },
  1299. {
  1300. name: "ok/no-dns",
  1301. args: args{
  1302. typ: DNS,
  1303. ids: []Identifier{
  1304. {
  1305. Type: IP,
  1306. Value: "192.168.42.42",
  1307. },
  1308. },
  1309. },
  1310. want: 0,
  1311. },
  1312. {
  1313. name: "ok/no-ips",
  1314. args: args{
  1315. typ: IP,
  1316. ids: []Identifier{
  1317. {
  1318. Type: DNS,
  1319. Value: "example.com",
  1320. },
  1321. },
  1322. },
  1323. want: 0,
  1324. },
  1325. {
  1326. name: "ok/one-dns",
  1327. args: args{
  1328. typ: DNS,
  1329. ids: []Identifier{
  1330. {
  1331. Type: DNS,
  1332. Value: "example.com",
  1333. },
  1334. {
  1335. Type: IP,
  1336. Value: "192.168.42.42",
  1337. },
  1338. },
  1339. },
  1340. want: 1,
  1341. },
  1342. {
  1343. name: "ok/one-ip",
  1344. args: args{
  1345. typ: IP,
  1346. ids: []Identifier{
  1347. {
  1348. Type: DNS,
  1349. Value: "example.com",
  1350. },
  1351. {
  1352. Type: IP,
  1353. Value: "192.168.42.42",
  1354. },
  1355. },
  1356. },
  1357. want: 1,
  1358. },
  1359. {
  1360. name: "ok/more-dns",
  1361. args: args{
  1362. typ: DNS,
  1363. ids: []Identifier{
  1364. {
  1365. Type: DNS,
  1366. Value: "example.com",
  1367. },
  1368. {
  1369. Type: DNS,
  1370. Value: "*.example.com",
  1371. },
  1372. {
  1373. Type: IP,
  1374. Value: "192.168.42.42",
  1375. },
  1376. },
  1377. },
  1378. want: 2,
  1379. },
  1380. {
  1381. name: "ok/more-ips",
  1382. args: args{
  1383. typ: IP,
  1384. ids: []Identifier{
  1385. {
  1386. Type: DNS,
  1387. Value: "example.com",
  1388. },
  1389. {
  1390. Type: IP,
  1391. Value: "192.168.42.42",
  1392. },
  1393. {
  1394. Type: IP,
  1395. Value: "192.168.42.43",
  1396. },
  1397. },
  1398. },
  1399. want: 2,
  1400. },
  1401. }
  1402. for _, tt := range tests {
  1403. t.Run(tt.name, func(t *testing.T) {
  1404. if got := numberOfIdentifierType(tt.args.typ, tt.args.ids); got != tt.want {
  1405. t.Errorf("numberOfIdentifierType() = %v, want %v", got, tt.want)
  1406. }
  1407. })
  1408. }
  1409. }
  1410. func Test_ipsAreEqual(t *testing.T) {
  1411. type args struct {
  1412. x net.IP
  1413. y net.IP
  1414. }
  1415. tests := []struct {
  1416. name string
  1417. args args
  1418. want bool
  1419. }{
  1420. {
  1421. name: "ok/ipv4",
  1422. args: args{
  1423. x: net.ParseIP("192.168.42.42"),
  1424. y: net.ParseIP("192.168.42.42"),
  1425. },
  1426. want: true,
  1427. },
  1428. {
  1429. name: "fail/ipv4",
  1430. args: args{
  1431. x: net.ParseIP("192.168.42.42"),
  1432. y: net.ParseIP("192.168.42.43"),
  1433. },
  1434. want: false,
  1435. },
  1436. {
  1437. name: "ok/ipv6",
  1438. args: args{
  1439. x: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  1440. y: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  1441. },
  1442. want: true,
  1443. },
  1444. {
  1445. name: "fail/ipv6",
  1446. args: args{
  1447. x: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  1448. y: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7335"),
  1449. },
  1450. want: false,
  1451. },
  1452. {
  1453. name: "fail/ipv4-and-ipv6",
  1454. args: args{
  1455. x: net.ParseIP("192.168.42.42"),
  1456. y: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  1457. },
  1458. want: false,
  1459. },
  1460. {
  1461. name: "ok/ipv4-mapped-to-ipv6",
  1462. args: args{
  1463. x: net.ParseIP("192.168.42.42"),
  1464. y: net.ParseIP("::ffff:192.168.42.42"), // parsed to the same IPv4 by Go
  1465. },
  1466. want: true, // we expect this to happen; a known issue in which ipv4 mapped ipv6 addresses are considered the same as their ipv4 counterpart
  1467. },
  1468. {
  1469. name: "fail/invalid-ipv4-and-valid-ipv6",
  1470. args: args{
  1471. x: net.ParseIP("192.168.42.1000"),
  1472. y: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  1473. },
  1474. want: false,
  1475. },
  1476. {
  1477. name: "fail/valid-ipv4-and-invalid-ipv6",
  1478. args: args{
  1479. x: net.ParseIP("192.168.42.42"),
  1480. y: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:733400"),
  1481. },
  1482. want: false,
  1483. },
  1484. {
  1485. name: "fail/invalid-ipv4-and-invalid-ipv6",
  1486. args: args{
  1487. x: net.ParseIP("192.168.42.1000"),
  1488. y: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:1000000"),
  1489. },
  1490. want: false,
  1491. },
  1492. }
  1493. for _, tt := range tests {
  1494. t.Run(tt.name, func(t *testing.T) {
  1495. if got := ipsAreEqual(tt.args.x, tt.args.y); got != tt.want {
  1496. t.Errorf("ipsAreEqual() = %v, want %v", got, tt.want)
  1497. }
  1498. })
  1499. }
  1500. }
  1501. func Test_canonicalize(t *testing.T) {
  1502. type args struct {
  1503. csr *x509.CertificateRequest
  1504. }
  1505. tests := []struct {
  1506. name string
  1507. args args
  1508. want *x509.CertificateRequest
  1509. }{
  1510. {
  1511. name: "ok/dns",
  1512. args: args{
  1513. csr: &x509.CertificateRequest{
  1514. DNSNames: []string{"www.example.com", "example.com"},
  1515. },
  1516. },
  1517. want: &x509.CertificateRequest{
  1518. DNSNames: []string{"example.com", "www.example.com"},
  1519. IPAddresses: []net.IP{},
  1520. },
  1521. },
  1522. {
  1523. name: "ok/common-name",
  1524. args: args{
  1525. csr: &x509.CertificateRequest{
  1526. Subject: pkix.Name{
  1527. CommonName: "example.com",
  1528. },
  1529. DNSNames: []string{"www.example.com"},
  1530. },
  1531. },
  1532. want: &x509.CertificateRequest{
  1533. Subject: pkix.Name{
  1534. CommonName: "example.com",
  1535. },
  1536. DNSNames: []string{"example.com", "www.example.com"},
  1537. IPAddresses: []net.IP{},
  1538. },
  1539. },
  1540. {
  1541. name: "ok/ipv4",
  1542. args: args{
  1543. csr: &x509.CertificateRequest{
  1544. IPAddresses: []net.IP{net.ParseIP("192.168.43.42"), net.ParseIP("192.168.42.42")},
  1545. },
  1546. },
  1547. want: &x509.CertificateRequest{
  1548. DNSNames: []string{},
  1549. IPAddresses: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.43.42")},
  1550. },
  1551. },
  1552. {
  1553. name: "ok/mixed",
  1554. args: args{
  1555. csr: &x509.CertificateRequest{
  1556. DNSNames: []string{"www.example.com", "example.com"},
  1557. IPAddresses: []net.IP{net.ParseIP("192.168.43.42"), net.ParseIP("192.168.42.42")},
  1558. },
  1559. },
  1560. want: &x509.CertificateRequest{
  1561. DNSNames: []string{"example.com", "www.example.com"},
  1562. IPAddresses: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.43.42")},
  1563. },
  1564. },
  1565. {
  1566. name: "ok/mixed-common-name",
  1567. args: args{
  1568. csr: &x509.CertificateRequest{
  1569. Subject: pkix.Name{
  1570. CommonName: "example.com",
  1571. },
  1572. DNSNames: []string{"www.example.com"},
  1573. IPAddresses: []net.IP{net.ParseIP("192.168.43.42"), net.ParseIP("192.168.42.42")},
  1574. },
  1575. },
  1576. want: &x509.CertificateRequest{
  1577. Subject: pkix.Name{
  1578. CommonName: "example.com",
  1579. },
  1580. DNSNames: []string{"example.com", "www.example.com"},
  1581. IPAddresses: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.43.42")},
  1582. },
  1583. },
  1584. {
  1585. name: "ok/ip-common-name",
  1586. args: args{
  1587. csr: &x509.CertificateRequest{
  1588. Subject: pkix.Name{
  1589. CommonName: "127.0.0.1",
  1590. },
  1591. DNSNames: []string{"example.com"},
  1592. IPAddresses: []net.IP{net.ParseIP("192.168.43.42"), net.ParseIP("192.168.42.42")},
  1593. },
  1594. },
  1595. want: &x509.CertificateRequest{
  1596. Subject: pkix.Name{
  1597. CommonName: "127.0.0.1",
  1598. },
  1599. DNSNames: []string{"example.com"},
  1600. IPAddresses: []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("192.168.42.42"), net.ParseIP("192.168.43.42")},
  1601. },
  1602. },
  1603. }
  1604. for _, tt := range tests {
  1605. t.Run(tt.name, func(t *testing.T) {
  1606. got := canonicalize(tt.args.csr)
  1607. if !cmp.Equal(tt.want, got) {
  1608. t.Errorf("canonicalize() diff =\n%s", cmp.Diff(tt.want, got))
  1609. }
  1610. })
  1611. }
  1612. }
  1613. func TestOrder_sans(t *testing.T) {
  1614. type fields struct {
  1615. Identifiers []Identifier
  1616. }
  1617. tests := []struct {
  1618. name string
  1619. fields fields
  1620. csr *x509.CertificateRequest
  1621. want []x509util.SubjectAlternativeName
  1622. err *Error
  1623. }{
  1624. {
  1625. name: "ok/dns",
  1626. fields: fields{
  1627. Identifiers: []Identifier{
  1628. {Type: "dns", Value: "example.com"},
  1629. },
  1630. },
  1631. csr: &x509.CertificateRequest{
  1632. Subject: pkix.Name{
  1633. CommonName: "example.com",
  1634. },
  1635. },
  1636. want: []x509util.SubjectAlternativeName{
  1637. {Type: "dns", Value: "example.com"},
  1638. },
  1639. err: nil,
  1640. },
  1641. {
  1642. name: "fail/invalid-alternative-name-email",
  1643. fields: fields{
  1644. Identifiers: []Identifier{},
  1645. },
  1646. csr: &x509.CertificateRequest{
  1647. Subject: pkix.Name{
  1648. CommonName: "foo.internal",
  1649. },
  1650. EmailAddresses: []string{"test@example.com"},
  1651. },
  1652. want: []x509util.SubjectAlternativeName{},
  1653. err: NewError(ErrorBadCSRType, "Only DNS names and IP addresses are allowed"),
  1654. },
  1655. {
  1656. name: "fail/invalid-alternative-name-uri",
  1657. fields: fields{
  1658. Identifiers: []Identifier{},
  1659. },
  1660. csr: &x509.CertificateRequest{
  1661. Subject: pkix.Name{
  1662. CommonName: "foo.internal",
  1663. },
  1664. URIs: []*url.URL{
  1665. {
  1666. Scheme: "https://",
  1667. Host: "smallstep.com",
  1668. },
  1669. },
  1670. },
  1671. want: []x509util.SubjectAlternativeName{},
  1672. err: NewError(ErrorBadCSRType, "Only DNS names and IP addresses are allowed"),
  1673. },
  1674. {
  1675. name: "fail/error-names-length-mismatch",
  1676. fields: fields{
  1677. Identifiers: []Identifier{
  1678. {Type: "dns", Value: "foo.internal"},
  1679. {Type: "dns", Value: "bar.internal"},
  1680. },
  1681. },
  1682. csr: &x509.CertificateRequest{
  1683. Subject: pkix.Name{
  1684. CommonName: "foo.internal",
  1685. },
  1686. },
  1687. want: []x509util.SubjectAlternativeName{},
  1688. err: NewError(ErrorBadCSRType, "CSR names do not match identifiers exactly: "+
  1689. "CSR names = %v, Order names = %v", []string{"foo.internal"}, []string{"bar.internal", "foo.internal"}),
  1690. },
  1691. {
  1692. name: "fail/error-names-mismatch",
  1693. fields: fields{
  1694. Identifiers: []Identifier{
  1695. {Type: "dns", Value: "foo.internal"},
  1696. {Type: "dns", Value: "bar.internal"},
  1697. },
  1698. },
  1699. csr: &x509.CertificateRequest{
  1700. Subject: pkix.Name{
  1701. CommonName: "foo.internal",
  1702. },
  1703. DNSNames: []string{"zap.internal"},
  1704. },
  1705. want: []x509util.SubjectAlternativeName{},
  1706. err: NewError(ErrorBadCSRType, "CSR names do not match identifiers exactly: "+
  1707. "CSR names = %v, Order names = %v", []string{"foo.internal", "zap.internal"}, []string{"bar.internal", "foo.internal"}),
  1708. },
  1709. {
  1710. name: "ok/ipv4",
  1711. fields: fields{
  1712. Identifiers: []Identifier{
  1713. {Type: "ip", Value: "192.168.43.42"},
  1714. {Type: "ip", Value: "192.168.42.42"},
  1715. },
  1716. },
  1717. csr: &x509.CertificateRequest{
  1718. IPAddresses: []net.IP{net.ParseIP("192.168.43.42"), net.ParseIP("192.168.42.42")},
  1719. },
  1720. want: []x509util.SubjectAlternativeName{
  1721. {Type: "ip", Value: "192.168.42.42"},
  1722. {Type: "ip", Value: "192.168.43.42"},
  1723. },
  1724. err: nil,
  1725. },
  1726. {
  1727. name: "ok/ipv6",
  1728. fields: fields{
  1729. Identifiers: []Identifier{
  1730. {Type: "ip", Value: "2001:0db8:85a3::8a2e:0370:7335"},
  1731. {Type: "ip", Value: "2001:0db8:85a3::8a2e:0370:7334"},
  1732. },
  1733. },
  1734. csr: &x509.CertificateRequest{
  1735. IPAddresses: []net.IP{net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7335"), net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334")},
  1736. },
  1737. want: []x509util.SubjectAlternativeName{
  1738. {Type: "ip", Value: "2001:db8:85a3::8a2e:370:7334"},
  1739. {Type: "ip", Value: "2001:db8:85a3::8a2e:370:7335"},
  1740. },
  1741. err: nil,
  1742. },
  1743. {
  1744. name: "fail/error-ips-length-mismatch",
  1745. fields: fields{
  1746. Identifiers: []Identifier{
  1747. {Type: "ip", Value: "192.168.42.42"},
  1748. {Type: "ip", Value: "192.168.43.42"},
  1749. },
  1750. },
  1751. csr: &x509.CertificateRequest{
  1752. IPAddresses: []net.IP{net.ParseIP("192.168.42.42")},
  1753. },
  1754. want: []x509util.SubjectAlternativeName{},
  1755. err: NewError(ErrorBadCSRType, "CSR IPs do not match identifiers exactly: "+
  1756. "CSR IPs = %v, Order IPs = %v", []net.IP{net.ParseIP("192.168.42.42")}, []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.43.42")}),
  1757. },
  1758. {
  1759. name: "fail/error-ips-mismatch",
  1760. fields: fields{
  1761. Identifiers: []Identifier{
  1762. {Type: "ip", Value: "192.168.42.42"},
  1763. {Type: "ip", Value: "192.168.43.42"},
  1764. },
  1765. },
  1766. csr: &x509.CertificateRequest{
  1767. IPAddresses: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.42.32")},
  1768. },
  1769. want: []x509util.SubjectAlternativeName{},
  1770. err: NewError(ErrorBadCSRType, "CSR IPs do not match identifiers exactly: "+
  1771. "CSR IPs = %v, Order IPs = %v", []net.IP{net.ParseIP("192.168.42.32"), net.ParseIP("192.168.42.42")}, []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.43.42")}),
  1772. },
  1773. {
  1774. name: "ok/mixed",
  1775. fields: fields{
  1776. Identifiers: []Identifier{
  1777. {Type: "dns", Value: "foo.internal"},
  1778. {Type: "dns", Value: "bar.internal"},
  1779. {Type: "ip", Value: "192.168.43.42"},
  1780. {Type: "ip", Value: "192.168.42.42"},
  1781. {Type: "ip", Value: "2001:0db8:85a3:0000:0000:8a2e:0370:7334"},
  1782. },
  1783. },
  1784. csr: &x509.CertificateRequest{
  1785. Subject: pkix.Name{
  1786. CommonName: "bar.internal",
  1787. },
  1788. DNSNames: []string{"foo.internal"},
  1789. IPAddresses: []net.IP{net.ParseIP("192.168.43.42"), net.ParseIP("192.168.42.42"), net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334")},
  1790. },
  1791. want: []x509util.SubjectAlternativeName{
  1792. {Type: "dns", Value: "bar.internal"},
  1793. {Type: "dns", Value: "foo.internal"},
  1794. {Type: "ip", Value: "192.168.42.42"},
  1795. {Type: "ip", Value: "192.168.43.42"},
  1796. {Type: "ip", Value: "2001:db8:85a3::8a2e:370:7334"},
  1797. },
  1798. err: nil,
  1799. },
  1800. {
  1801. name: "fail/unsupported-identifier-type",
  1802. fields: fields{
  1803. Identifiers: []Identifier{
  1804. {Type: "ipv4", Value: "192.168.42.42"},
  1805. },
  1806. },
  1807. csr: &x509.CertificateRequest{
  1808. IPAddresses: []net.IP{net.ParseIP("192.168.42.42")},
  1809. },
  1810. want: []x509util.SubjectAlternativeName{},
  1811. err: NewError(ErrorServerInternalType, "unsupported identifier type in order: ipv4"),
  1812. },
  1813. }
  1814. for _, tt := range tests {
  1815. t.Run(tt.name, func(t *testing.T) {
  1816. o := &Order{
  1817. Identifiers: tt.fields.Identifiers,
  1818. }
  1819. canonicalizedCSR := canonicalize(tt.csr)
  1820. got, err := o.sans(canonicalizedCSR)
  1821. if tt.err != nil {
  1822. if err == nil {
  1823. t.Errorf("Order.sans() = %v, want error; got none", got)
  1824. return
  1825. }
  1826. var k *Error
  1827. if errors.As(err, &k) {
  1828. assert.Equals(t, k.Type, tt.err.Type)
  1829. assert.Equals(t, k.Detail, tt.err.Detail)
  1830. assert.Equals(t, k.Status, tt.err.Status)
  1831. assert.Equals(t, k.Err.Error(), tt.err.Err.Error())
  1832. assert.Equals(t, k.Detail, tt.err.Detail)
  1833. } else {
  1834. assert.FatalError(t, errors.New("unexpected error type"))
  1835. }
  1836. return
  1837. }
  1838. if !reflect.DeepEqual(got, tt.want) {
  1839. t.Errorf("Order.sans() = %v, want %v", got, tt.want)
  1840. }
  1841. })
  1842. }
  1843. }
  1844. func TestOrder_getAuthorizationFingerprint(t *testing.T) {
  1845. ctx := context.Background()
  1846. type fields struct {
  1847. AuthorizationIDs []string
  1848. }
  1849. type args struct {
  1850. ctx context.Context
  1851. db DB
  1852. }
  1853. tests := []struct {
  1854. name string
  1855. fields fields
  1856. args args
  1857. want string
  1858. wantErr bool
  1859. }{
  1860. {"ok", fields{[]string{"az1", "az2"}}, args{ctx, &MockDB{
  1861. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  1862. return &Authorization{ID: id, Status: StatusValid}, nil
  1863. },
  1864. }}, "", false},
  1865. {"ok fingerprint", fields{[]string{"az1", "az2"}}, args{ctx, &MockDB{
  1866. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  1867. if id == "az1" {
  1868. return &Authorization{ID: id, Status: StatusValid}, nil
  1869. }
  1870. return &Authorization{ID: id, Fingerprint: "fingerprint", Status: StatusValid}, nil
  1871. },
  1872. }}, "fingerprint", false},
  1873. {"fail", fields{[]string{"az1", "az2"}}, args{ctx, &MockDB{
  1874. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  1875. return nil, errors.New("force")
  1876. },
  1877. }}, "", true},
  1878. }
  1879. for _, tt := range tests {
  1880. t.Run(tt.name, func(t *testing.T) {
  1881. o := &Order{
  1882. AuthorizationIDs: tt.fields.AuthorizationIDs,
  1883. }
  1884. got, err := o.getAuthorizationFingerprint(tt.args.ctx, tt.args.db)
  1885. if (err != nil) != tt.wantErr {
  1886. t.Errorf("Order.getAuthorizationFingerprint() error = %v, wantErr %v", err, tt.wantErr)
  1887. return
  1888. }
  1889. if got != tt.want {
  1890. t.Errorf("Order.getAuthorizationFingerprint() = %v, want %v", got, tt.want)
  1891. }
  1892. })
  1893. }
  1894. }