order_test.go 55 KB

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