4
0

order_test.go 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  1. package acme
  2. import (
  3. "context"
  4. "crypto/x509"
  5. "crypto/x509/pkix"
  6. "encoding/json"
  7. "net"
  8. "reflect"
  9. "testing"
  10. "time"
  11. "github.com/pkg/errors"
  12. "github.com/smallstep/assert"
  13. "github.com/smallstep/certificates/authority/provisioner"
  14. "go.step.sm/crypto/x509util"
  15. )
  16. func TestOrder_UpdateStatus(t *testing.T) {
  17. type test struct {
  18. o *Order
  19. err *Error
  20. db DB
  21. }
  22. tests := map[string]func(t *testing.T) test{
  23. "ok/already-invalid": func(t *testing.T) test {
  24. o := &Order{
  25. Status: StatusInvalid,
  26. }
  27. return test{
  28. o: o,
  29. }
  30. },
  31. "ok/already-valid": func(t *testing.T) test {
  32. o := &Order{
  33. Status: StatusInvalid,
  34. }
  35. return test{
  36. o: o,
  37. }
  38. },
  39. "fail/error-unexpected-status": func(t *testing.T) test {
  40. o := &Order{
  41. Status: "foo",
  42. }
  43. return test{
  44. o: o,
  45. err: NewErrorISE("unrecognized order status: %s", o.Status),
  46. }
  47. },
  48. "ok/ready-expired": func(t *testing.T) test {
  49. now := clock.Now()
  50. o := &Order{
  51. ID: "oID",
  52. AccountID: "accID",
  53. Status: StatusReady,
  54. ExpiresAt: now.Add(-5 * time.Minute),
  55. }
  56. return test{
  57. o: o,
  58. db: &MockDB{
  59. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  60. assert.Equals(t, updo.ID, o.ID)
  61. assert.Equals(t, updo.AccountID, o.AccountID)
  62. assert.Equals(t, updo.Status, StatusInvalid)
  63. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  64. return nil
  65. },
  66. },
  67. }
  68. },
  69. "fail/ready-expired-db.UpdateOrder-error": func(t *testing.T) test {
  70. now := clock.Now()
  71. o := &Order{
  72. ID: "oID",
  73. AccountID: "accID",
  74. Status: StatusReady,
  75. ExpiresAt: now.Add(-5 * time.Minute),
  76. }
  77. return test{
  78. o: o,
  79. db: &MockDB{
  80. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  81. assert.Equals(t, updo.ID, o.ID)
  82. assert.Equals(t, updo.AccountID, o.AccountID)
  83. assert.Equals(t, updo.Status, StatusInvalid)
  84. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  85. return errors.New("force")
  86. },
  87. },
  88. err: NewErrorISE("error updating order: force"),
  89. }
  90. },
  91. "ok/pending-expired": func(t *testing.T) test {
  92. now := clock.Now()
  93. o := &Order{
  94. ID: "oID",
  95. AccountID: "accID",
  96. Status: StatusPending,
  97. ExpiresAt: now.Add(-5 * time.Minute),
  98. }
  99. return test{
  100. o: o,
  101. db: &MockDB{
  102. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  103. assert.Equals(t, updo.ID, o.ID)
  104. assert.Equals(t, updo.AccountID, o.AccountID)
  105. assert.Equals(t, updo.Status, StatusInvalid)
  106. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  107. err := NewError(ErrorMalformedType, "order has expired")
  108. assert.HasPrefix(t, updo.Error.Err.Error(), err.Err.Error())
  109. assert.Equals(t, updo.Error.Type, err.Type)
  110. assert.Equals(t, updo.Error.Detail, err.Detail)
  111. assert.Equals(t, updo.Error.Status, err.Status)
  112. assert.Equals(t, updo.Error.Detail, err.Detail)
  113. return nil
  114. },
  115. },
  116. }
  117. },
  118. "ok/invalid": func(t *testing.T) test {
  119. now := clock.Now()
  120. o := &Order{
  121. ID: "oID",
  122. AccountID: "accID",
  123. Status: StatusPending,
  124. ExpiresAt: now.Add(5 * time.Minute),
  125. AuthorizationIDs: []string{"a", "b"},
  126. }
  127. az1 := &Authorization{
  128. ID: "a",
  129. Status: StatusValid,
  130. }
  131. az2 := &Authorization{
  132. ID: "b",
  133. Status: StatusInvalid,
  134. }
  135. return test{
  136. o: o,
  137. db: &MockDB{
  138. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  139. assert.Equals(t, updo.ID, o.ID)
  140. assert.Equals(t, updo.AccountID, o.AccountID)
  141. assert.Equals(t, updo.Status, StatusInvalid)
  142. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  143. return nil
  144. },
  145. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  146. switch id {
  147. case az1.ID:
  148. return az1, nil
  149. case az2.ID:
  150. return az2, nil
  151. default:
  152. assert.FatalError(t, errors.Errorf("unexpected authz key %s", id))
  153. return nil, errors.New("force")
  154. }
  155. },
  156. },
  157. }
  158. },
  159. "ok/still-pending": func(t *testing.T) test {
  160. now := clock.Now()
  161. o := &Order{
  162. ID: "oID",
  163. AccountID: "accID",
  164. Status: StatusPending,
  165. ExpiresAt: now.Add(5 * time.Minute),
  166. AuthorizationIDs: []string{"a", "b"},
  167. }
  168. az1 := &Authorization{
  169. ID: "a",
  170. Status: StatusValid,
  171. }
  172. az2 := &Authorization{
  173. ID: "b",
  174. Status: StatusPending,
  175. }
  176. return test{
  177. o: o,
  178. db: &MockDB{
  179. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  180. switch id {
  181. case az1.ID:
  182. return az1, nil
  183. case az2.ID:
  184. return az2, nil
  185. default:
  186. assert.FatalError(t, errors.Errorf("unexpected authz key %s", id))
  187. return nil, errors.New("force")
  188. }
  189. },
  190. },
  191. }
  192. },
  193. "ok/valid": func(t *testing.T) test {
  194. now := clock.Now()
  195. o := &Order{
  196. ID: "oID",
  197. AccountID: "accID",
  198. Status: StatusPending,
  199. ExpiresAt: now.Add(5 * time.Minute),
  200. AuthorizationIDs: []string{"a", "b"},
  201. }
  202. az1 := &Authorization{
  203. ID: "a",
  204. Status: StatusValid,
  205. }
  206. az2 := &Authorization{
  207. ID: "b",
  208. Status: StatusValid,
  209. }
  210. return test{
  211. o: o,
  212. db: &MockDB{
  213. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  214. assert.Equals(t, updo.ID, o.ID)
  215. assert.Equals(t, updo.AccountID, o.AccountID)
  216. assert.Equals(t, updo.Status, StatusReady)
  217. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  218. return nil
  219. },
  220. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  221. switch id {
  222. case az1.ID:
  223. return az1, nil
  224. case az2.ID:
  225. return az2, nil
  226. default:
  227. assert.FatalError(t, errors.Errorf("unexpected authz key %s", id))
  228. return nil, errors.New("force")
  229. }
  230. },
  231. },
  232. }
  233. },
  234. }
  235. for name, run := range tests {
  236. t.Run(name, func(t *testing.T) {
  237. tc := run(t)
  238. if err := tc.o.UpdateStatus(context.Background(), tc.db); err != nil {
  239. if assert.NotNil(t, tc.err) {
  240. switch k := err.(type) {
  241. case *Error:
  242. assert.Equals(t, k.Type, tc.err.Type)
  243. assert.Equals(t, k.Detail, tc.err.Detail)
  244. assert.Equals(t, k.Status, tc.err.Status)
  245. assert.Equals(t, k.Err.Error(), tc.err.Err.Error())
  246. assert.Equals(t, k.Detail, tc.err.Detail)
  247. default:
  248. assert.FatalError(t, errors.New("unexpected error type"))
  249. }
  250. }
  251. } else {
  252. assert.Nil(t, tc.err)
  253. }
  254. })
  255. }
  256. }
  257. type mockSignAuth struct {
  258. sign func(csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error)
  259. loadProvisionerByName func(string) (provisioner.Interface, error)
  260. ret1, ret2 interface{}
  261. err error
  262. }
  263. func (m *mockSignAuth) Sign(csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  264. if m.sign != nil {
  265. return m.sign(csr, signOpts, extraOpts...)
  266. } else if m.err != nil {
  267. return nil, m.err
  268. }
  269. return []*x509.Certificate{m.ret1.(*x509.Certificate), m.ret2.(*x509.Certificate)}, m.err
  270. }
  271. func (m *mockSignAuth) LoadProvisionerByName(name string) (provisioner.Interface, error) {
  272. if m.loadProvisionerByName != nil {
  273. return m.loadProvisionerByName(name)
  274. }
  275. return m.ret1.(provisioner.Interface), m.err
  276. }
  277. func TestOrder_Finalize(t *testing.T) {
  278. type test struct {
  279. o *Order
  280. err *Error
  281. db DB
  282. ca CertificateAuthority
  283. csr *x509.CertificateRequest
  284. prov Provisioner
  285. }
  286. tests := map[string]func(t *testing.T) test{
  287. "fail/invalid": func(t *testing.T) test {
  288. o := &Order{
  289. ID: "oid",
  290. Status: StatusInvalid,
  291. }
  292. return test{
  293. o: o,
  294. err: NewError(ErrorOrderNotReadyType, "order %s has been abandoned", o.ID),
  295. }
  296. },
  297. "fail/pending": func(t *testing.T) test {
  298. now := clock.Now()
  299. o := &Order{
  300. ID: "oID",
  301. AccountID: "accID",
  302. Status: StatusPending,
  303. ExpiresAt: now.Add(5 * time.Minute),
  304. AuthorizationIDs: []string{"a", "b"},
  305. }
  306. az1 := &Authorization{
  307. ID: "a",
  308. Status: StatusValid,
  309. }
  310. az2 := &Authorization{
  311. ID: "b",
  312. Status: StatusPending,
  313. ExpiresAt: now.Add(5 * time.Minute),
  314. }
  315. return test{
  316. o: o,
  317. db: &MockDB{
  318. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  319. switch id {
  320. case az1.ID:
  321. return az1, nil
  322. case az2.ID:
  323. return az2, nil
  324. default:
  325. assert.FatalError(t, errors.Errorf("unexpected authz key %s", id))
  326. return nil, errors.New("force")
  327. }
  328. },
  329. },
  330. err: NewError(ErrorOrderNotReadyType, "order %s is not ready", o.ID),
  331. }
  332. },
  333. "ok/already-valid": func(t *testing.T) test {
  334. o := &Order{
  335. ID: "oid",
  336. Status: StatusValid,
  337. }
  338. return test{
  339. o: o,
  340. }
  341. },
  342. "fail/error-unexpected-status": func(t *testing.T) test {
  343. now := clock.Now()
  344. o := &Order{
  345. ID: "oID",
  346. AccountID: "accID",
  347. Status: "foo",
  348. ExpiresAt: now.Add(5 * time.Minute),
  349. AuthorizationIDs: []string{"a", "b"},
  350. }
  351. return test{
  352. o: o,
  353. err: NewErrorISE("unrecognized order status: %s", o.Status),
  354. }
  355. },
  356. "fail/error-provisioner-auth": func(t *testing.T) test {
  357. now := clock.Now()
  358. o := &Order{
  359. ID: "oID",
  360. AccountID: "accID",
  361. Status: StatusReady,
  362. ExpiresAt: now.Add(5 * time.Minute),
  363. AuthorizationIDs: []string{"a", "b"},
  364. Identifiers: []Identifier{
  365. {Type: "dns", Value: "foo.internal"},
  366. {Type: "dns", Value: "bar.internal"},
  367. },
  368. }
  369. csr := &x509.CertificateRequest{
  370. Subject: pkix.Name{
  371. CommonName: "foo.internal",
  372. },
  373. DNSNames: []string{"bar.internal"},
  374. }
  375. return test{
  376. o: o,
  377. csr: csr,
  378. prov: &MockProvisioner{
  379. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  380. assert.Equals(t, token, "")
  381. return nil, errors.New("force")
  382. },
  383. },
  384. err: NewErrorISE("error retrieving authorization options from ACME provisioner: force"),
  385. }
  386. },
  387. "fail/error-template-options": func(t *testing.T) test {
  388. now := clock.Now()
  389. o := &Order{
  390. ID: "oID",
  391. AccountID: "accID",
  392. Status: StatusReady,
  393. ExpiresAt: now.Add(5 * time.Minute),
  394. AuthorizationIDs: []string{"a", "b"},
  395. Identifiers: []Identifier{
  396. {Type: "dns", Value: "foo.internal"},
  397. {Type: "dns", Value: "bar.internal"},
  398. },
  399. }
  400. csr := &x509.CertificateRequest{
  401. Subject: pkix.Name{
  402. CommonName: "foo.internal",
  403. },
  404. DNSNames: []string{"bar.internal"},
  405. }
  406. return test{
  407. o: o,
  408. csr: csr,
  409. prov: &MockProvisioner{
  410. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  411. assert.Equals(t, token, "")
  412. return nil, nil
  413. },
  414. MgetOptions: func() *provisioner.Options {
  415. return &provisioner.Options{
  416. X509: &provisioner.X509Options{
  417. TemplateData: json.RawMessage([]byte("fo{o")),
  418. },
  419. }
  420. },
  421. },
  422. err: NewErrorISE("error creating template options from ACME provisioner: error unmarshaling template data: invalid character 'o' in literal false (expecting 'a')"),
  423. }
  424. },
  425. "fail/error-ca-sign": func(t *testing.T) test {
  426. now := clock.Now()
  427. o := &Order{
  428. ID: "oID",
  429. AccountID: "accID",
  430. Status: StatusReady,
  431. ExpiresAt: now.Add(5 * time.Minute),
  432. AuthorizationIDs: []string{"a", "b"},
  433. Identifiers: []Identifier{
  434. {Type: "dns", Value: "foo.internal"},
  435. {Type: "dns", Value: "bar.internal"},
  436. },
  437. }
  438. csr := &x509.CertificateRequest{
  439. Subject: pkix.Name{
  440. CommonName: "foo.internal",
  441. },
  442. DNSNames: []string{"bar.internal"},
  443. }
  444. return test{
  445. o: o,
  446. csr: csr,
  447. prov: &MockProvisioner{
  448. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  449. assert.Equals(t, token, "")
  450. return nil, nil
  451. },
  452. MgetOptions: func() *provisioner.Options {
  453. return nil
  454. },
  455. },
  456. ca: &mockSignAuth{
  457. sign: func(_csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  458. assert.Equals(t, _csr, csr)
  459. return nil, errors.New("force")
  460. },
  461. },
  462. err: NewErrorISE("error signing certificate for order oID: force"),
  463. }
  464. },
  465. "fail/error-db.CreateCertificate": func(t *testing.T) test {
  466. now := clock.Now()
  467. o := &Order{
  468. ID: "oID",
  469. AccountID: "accID",
  470. Status: StatusReady,
  471. ExpiresAt: now.Add(5 * time.Minute),
  472. AuthorizationIDs: []string{"a", "b"},
  473. Identifiers: []Identifier{
  474. {Type: "dns", Value: "foo.internal"},
  475. {Type: "dns", Value: "bar.internal"},
  476. },
  477. }
  478. csr := &x509.CertificateRequest{
  479. Subject: pkix.Name{
  480. CommonName: "foo.internal",
  481. },
  482. DNSNames: []string{"bar.internal"},
  483. }
  484. foo := &x509.Certificate{Subject: pkix.Name{CommonName: "foo"}}
  485. bar := &x509.Certificate{Subject: pkix.Name{CommonName: "bar"}}
  486. baz := &x509.Certificate{Subject: pkix.Name{CommonName: "baz"}}
  487. return test{
  488. o: o,
  489. csr: csr,
  490. prov: &MockProvisioner{
  491. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  492. assert.Equals(t, token, "")
  493. return nil, nil
  494. },
  495. MgetOptions: func() *provisioner.Options {
  496. return nil
  497. },
  498. },
  499. ca: &mockSignAuth{
  500. sign: func(_csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  501. assert.Equals(t, _csr, csr)
  502. return []*x509.Certificate{foo, bar, baz}, nil
  503. },
  504. },
  505. db: &MockDB{
  506. MockCreateCertificate: func(ctx context.Context, cert *Certificate) error {
  507. assert.Equals(t, cert.AccountID, o.AccountID)
  508. assert.Equals(t, cert.OrderID, o.ID)
  509. assert.Equals(t, cert.Leaf, foo)
  510. assert.Equals(t, cert.Intermediates, []*x509.Certificate{bar, baz})
  511. return errors.New("force")
  512. },
  513. },
  514. err: NewErrorISE("error creating certificate for order oID: force"),
  515. }
  516. },
  517. "fail/error-db.UpdateOrder": func(t *testing.T) test {
  518. now := clock.Now()
  519. o := &Order{
  520. ID: "oID",
  521. AccountID: "accID",
  522. Status: StatusReady,
  523. ExpiresAt: now.Add(5 * time.Minute),
  524. AuthorizationIDs: []string{"a", "b"},
  525. Identifiers: []Identifier{
  526. {Type: "dns", Value: "foo.internal"},
  527. {Type: "dns", Value: "bar.internal"},
  528. },
  529. }
  530. csr := &x509.CertificateRequest{
  531. Subject: pkix.Name{
  532. CommonName: "foo.internal",
  533. },
  534. DNSNames: []string{"bar.internal"},
  535. }
  536. foo := &x509.Certificate{Subject: pkix.Name{CommonName: "foo"}}
  537. bar := &x509.Certificate{Subject: pkix.Name{CommonName: "bar"}}
  538. baz := &x509.Certificate{Subject: pkix.Name{CommonName: "baz"}}
  539. return test{
  540. o: o,
  541. csr: csr,
  542. prov: &MockProvisioner{
  543. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  544. assert.Equals(t, token, "")
  545. return nil, nil
  546. },
  547. MgetOptions: func() *provisioner.Options {
  548. return nil
  549. },
  550. },
  551. ca: &mockSignAuth{
  552. sign: func(_csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  553. assert.Equals(t, _csr, csr)
  554. return []*x509.Certificate{foo, bar, baz}, nil
  555. },
  556. },
  557. db: &MockDB{
  558. MockCreateCertificate: func(ctx context.Context, cert *Certificate) error {
  559. cert.ID = "certID"
  560. assert.Equals(t, cert.AccountID, o.AccountID)
  561. assert.Equals(t, cert.OrderID, o.ID)
  562. assert.Equals(t, cert.Leaf, foo)
  563. assert.Equals(t, cert.Intermediates, []*x509.Certificate{bar, baz})
  564. return nil
  565. },
  566. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  567. assert.Equals(t, updo.CertificateID, "certID")
  568. assert.Equals(t, updo.Status, StatusValid)
  569. assert.Equals(t, updo.ID, o.ID)
  570. assert.Equals(t, updo.AccountID, o.AccountID)
  571. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  572. assert.Equals(t, updo.AuthorizationIDs, o.AuthorizationIDs)
  573. assert.Equals(t, updo.Identifiers, o.Identifiers)
  574. return errors.New("force")
  575. },
  576. },
  577. err: NewErrorISE("error updating order oID: force"),
  578. }
  579. },
  580. "ok/new-cert-dns": func(t *testing.T) test {
  581. now := clock.Now()
  582. o := &Order{
  583. ID: "oID",
  584. AccountID: "accID",
  585. Status: StatusReady,
  586. ExpiresAt: now.Add(5 * time.Minute),
  587. AuthorizationIDs: []string{"a", "b"},
  588. Identifiers: []Identifier{
  589. {Type: "dns", Value: "foo.internal"},
  590. {Type: "dns", Value: "bar.internal"},
  591. },
  592. }
  593. csr := &x509.CertificateRequest{
  594. Subject: pkix.Name{
  595. CommonName: "foo.internal",
  596. },
  597. DNSNames: []string{"bar.internal"},
  598. }
  599. foo := &x509.Certificate{Subject: pkix.Name{CommonName: "foo"}}
  600. bar := &x509.Certificate{Subject: pkix.Name{CommonName: "bar"}}
  601. baz := &x509.Certificate{Subject: pkix.Name{CommonName: "baz"}}
  602. return test{
  603. o: o,
  604. csr: csr,
  605. prov: &MockProvisioner{
  606. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  607. assert.Equals(t, token, "")
  608. return nil, nil
  609. },
  610. MgetOptions: func() *provisioner.Options {
  611. return nil
  612. },
  613. },
  614. ca: &mockSignAuth{
  615. sign: func(_csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  616. assert.Equals(t, _csr, csr)
  617. return []*x509.Certificate{foo, bar, baz}, nil
  618. },
  619. },
  620. db: &MockDB{
  621. MockCreateCertificate: func(ctx context.Context, cert *Certificate) error {
  622. cert.ID = "certID"
  623. assert.Equals(t, cert.AccountID, o.AccountID)
  624. assert.Equals(t, cert.OrderID, o.ID)
  625. assert.Equals(t, cert.Leaf, foo)
  626. assert.Equals(t, cert.Intermediates, []*x509.Certificate{bar, baz})
  627. return nil
  628. },
  629. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  630. assert.Equals(t, updo.CertificateID, "certID")
  631. assert.Equals(t, updo.Status, StatusValid)
  632. assert.Equals(t, updo.ID, o.ID)
  633. assert.Equals(t, updo.AccountID, o.AccountID)
  634. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  635. assert.Equals(t, updo.AuthorizationIDs, o.AuthorizationIDs)
  636. assert.Equals(t, updo.Identifiers, o.Identifiers)
  637. return nil
  638. },
  639. },
  640. }
  641. },
  642. "ok/new-cert-ip": func(t *testing.T) test {
  643. now := clock.Now()
  644. o := &Order{
  645. ID: "oID",
  646. AccountID: "accID",
  647. Status: StatusReady,
  648. ExpiresAt: now.Add(5 * time.Minute),
  649. AuthorizationIDs: []string{"a", "b"},
  650. Identifiers: []Identifier{
  651. {Type: "ip", Value: "192.168.42.42"},
  652. {Type: "ip", Value: "192.168.43.42"},
  653. },
  654. }
  655. csr := &x509.CertificateRequest{
  656. IPAddresses: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.43.42")}, // in case of IPs, no Common Name
  657. }
  658. foo := &x509.Certificate{Subject: pkix.Name{CommonName: "foo"}}
  659. bar := &x509.Certificate{Subject: pkix.Name{CommonName: "bar"}}
  660. baz := &x509.Certificate{Subject: pkix.Name{CommonName: "baz"}}
  661. return test{
  662. o: o,
  663. csr: csr,
  664. prov: &MockProvisioner{
  665. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  666. assert.Equals(t, token, "")
  667. return nil, nil
  668. },
  669. MgetOptions: func() *provisioner.Options {
  670. return nil
  671. },
  672. },
  673. ca: &mockSignAuth{
  674. sign: func(_csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  675. assert.Equals(t, _csr, csr)
  676. return []*x509.Certificate{foo, bar, baz}, nil
  677. },
  678. },
  679. db: &MockDB{
  680. MockCreateCertificate: func(ctx context.Context, cert *Certificate) error {
  681. cert.ID = "certID"
  682. assert.Equals(t, cert.AccountID, o.AccountID)
  683. assert.Equals(t, cert.OrderID, o.ID)
  684. assert.Equals(t, cert.Leaf, foo)
  685. assert.Equals(t, cert.Intermediates, []*x509.Certificate{bar, baz})
  686. return nil
  687. },
  688. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  689. assert.Equals(t, updo.CertificateID, "certID")
  690. assert.Equals(t, updo.Status, StatusValid)
  691. assert.Equals(t, updo.ID, o.ID)
  692. assert.Equals(t, updo.AccountID, o.AccountID)
  693. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  694. assert.Equals(t, updo.AuthorizationIDs, o.AuthorizationIDs)
  695. assert.Equals(t, updo.Identifiers, o.Identifiers)
  696. return nil
  697. },
  698. },
  699. }
  700. },
  701. "ok/new-cert-dns-and-ip": func(t *testing.T) test {
  702. now := clock.Now()
  703. o := &Order{
  704. ID: "oID",
  705. AccountID: "accID",
  706. Status: StatusReady,
  707. ExpiresAt: now.Add(5 * time.Minute),
  708. AuthorizationIDs: []string{"a", "b"},
  709. Identifiers: []Identifier{
  710. {Type: "dns", Value: "foo.internal"},
  711. {Type: "ip", Value: "192.168.42.42"},
  712. },
  713. }
  714. csr := &x509.CertificateRequest{
  715. Subject: pkix.Name{
  716. CommonName: "foo.internal",
  717. },
  718. IPAddresses: []net.IP{net.ParseIP("192.168.42.42")},
  719. }
  720. foo := &x509.Certificate{Subject: pkix.Name{CommonName: "foo"}}
  721. bar := &x509.Certificate{Subject: pkix.Name{CommonName: "bar"}}
  722. baz := &x509.Certificate{Subject: pkix.Name{CommonName: "baz"}}
  723. return test{
  724. o: o,
  725. csr: csr,
  726. prov: &MockProvisioner{
  727. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  728. assert.Equals(t, token, "")
  729. return nil, nil
  730. },
  731. MgetOptions: func() *provisioner.Options {
  732. return nil
  733. },
  734. },
  735. ca: &mockSignAuth{
  736. sign: func(_csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  737. assert.Equals(t, _csr, csr)
  738. return []*x509.Certificate{foo, bar, baz}, nil
  739. },
  740. },
  741. db: &MockDB{
  742. MockCreateCertificate: func(ctx context.Context, cert *Certificate) error {
  743. cert.ID = "certID"
  744. assert.Equals(t, cert.AccountID, o.AccountID)
  745. assert.Equals(t, cert.OrderID, o.ID)
  746. assert.Equals(t, cert.Leaf, foo)
  747. assert.Equals(t, cert.Intermediates, []*x509.Certificate{bar, baz})
  748. return nil
  749. },
  750. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  751. assert.Equals(t, updo.CertificateID, "certID")
  752. assert.Equals(t, updo.Status, StatusValid)
  753. assert.Equals(t, updo.ID, o.ID)
  754. assert.Equals(t, updo.AccountID, o.AccountID)
  755. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  756. assert.Equals(t, updo.AuthorizationIDs, o.AuthorizationIDs)
  757. assert.Equals(t, updo.Identifiers, o.Identifiers)
  758. return nil
  759. },
  760. },
  761. }
  762. },
  763. }
  764. for name, run := range tests {
  765. t.Run(name, func(t *testing.T) {
  766. tc := run(t)
  767. if err := tc.o.Finalize(context.Background(), tc.db, tc.csr, tc.ca, tc.prov); err != nil {
  768. if assert.NotNil(t, tc.err) {
  769. switch k := err.(type) {
  770. case *Error:
  771. assert.Equals(t, k.Type, tc.err.Type)
  772. assert.Equals(t, k.Detail, tc.err.Detail)
  773. assert.Equals(t, k.Status, tc.err.Status)
  774. assert.Equals(t, k.Err.Error(), tc.err.Err.Error())
  775. assert.Equals(t, k.Detail, tc.err.Detail)
  776. default:
  777. assert.FatalError(t, errors.New("unexpected error type"))
  778. }
  779. }
  780. } else {
  781. assert.Nil(t, tc.err)
  782. }
  783. })
  784. }
  785. }
  786. func Test_uniqueSortedIPs(t *testing.T) {
  787. type args struct {
  788. ips []net.IP
  789. }
  790. tests := []struct {
  791. name string
  792. args args
  793. wantUnique []net.IP
  794. }{
  795. {
  796. name: "ok/empty",
  797. args: args{
  798. ips: []net.IP{},
  799. },
  800. wantUnique: []net.IP{},
  801. },
  802. {
  803. name: "ok/single-ipv4",
  804. args: args{
  805. ips: []net.IP{net.ParseIP("192.168.42.42")},
  806. },
  807. wantUnique: []net.IP{net.ParseIP("192.168.42.42")},
  808. },
  809. {
  810. name: "ok/multiple-ipv4",
  811. args: args{
  812. ips: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.42.10"), net.ParseIP("192.168.42.1")},
  813. },
  814. wantUnique: []net.IP{net.ParseIP("192.168.42.1"), net.ParseIP("192.168.42.10"), net.ParseIP("192.168.42.42")},
  815. },
  816. {
  817. name: "ok/unique-ipv4",
  818. args: args{
  819. ips: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.42.42")},
  820. },
  821. wantUnique: []net.IP{net.ParseIP("192.168.42.42")},
  822. },
  823. {
  824. name: "ok/single-ipv6",
  825. args: args{
  826. ips: []net.IP{net.ParseIP("2001:db8::30")},
  827. },
  828. wantUnique: []net.IP{net.ParseIP("2001:db8::30")},
  829. },
  830. {
  831. name: "ok/multiple-ipv6",
  832. args: args{
  833. ips: []net.IP{net.ParseIP("2001:db8::30"), net.ParseIP("2001:db8::20"), net.ParseIP("2001:db8::10")},
  834. },
  835. wantUnique: []net.IP{net.ParseIP("2001:db8::10"), net.ParseIP("2001:db8::20"), net.ParseIP("2001:db8::30")},
  836. },
  837. {
  838. name: "ok/unique-ipv6",
  839. args: args{
  840. ips: []net.IP{net.ParseIP("2001:db8::1"), net.ParseIP("2001:db8::1")},
  841. },
  842. wantUnique: []net.IP{net.ParseIP("2001:db8::1")},
  843. },
  844. {
  845. name: "ok/mixed-ipv4-and-ipv6",
  846. args: args{
  847. 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")},
  848. },
  849. wantUnique: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("2001:db8::1")},
  850. },
  851. }
  852. for _, tt := range tests {
  853. t.Run(tt.name, func(t *testing.T) {
  854. if gotUnique := uniqueSortedIPs(tt.args.ips); !reflect.DeepEqual(gotUnique, tt.wantUnique) {
  855. t.Errorf("uniqueSortedIPs() = %v, want %v", gotUnique, tt.wantUnique)
  856. }
  857. })
  858. }
  859. }
  860. func Test_numberOfIdentifierType(t *testing.T) {
  861. type args struct {
  862. typ IdentifierType
  863. ids []Identifier
  864. }
  865. tests := []struct {
  866. name string
  867. args args
  868. want int
  869. }{
  870. {
  871. name: "ok/no-identifiers",
  872. args: args{
  873. typ: DNS,
  874. ids: []Identifier{},
  875. },
  876. want: 0,
  877. },
  878. {
  879. name: "ok/no-dns",
  880. args: args{
  881. typ: DNS,
  882. ids: []Identifier{
  883. {
  884. Type: IP,
  885. Value: "192.168.42.42",
  886. },
  887. },
  888. },
  889. want: 0,
  890. },
  891. {
  892. name: "ok/no-ips",
  893. args: args{
  894. typ: IP,
  895. ids: []Identifier{
  896. {
  897. Type: DNS,
  898. Value: "example.com",
  899. },
  900. },
  901. },
  902. want: 0,
  903. },
  904. {
  905. name: "ok/one-dns",
  906. args: args{
  907. typ: DNS,
  908. ids: []Identifier{
  909. {
  910. Type: DNS,
  911. Value: "example.com",
  912. },
  913. {
  914. Type: IP,
  915. Value: "192.168.42.42",
  916. },
  917. },
  918. },
  919. want: 1,
  920. },
  921. {
  922. name: "ok/one-ip",
  923. args: args{
  924. typ: IP,
  925. ids: []Identifier{
  926. {
  927. Type: DNS,
  928. Value: "example.com",
  929. },
  930. {
  931. Type: IP,
  932. Value: "192.168.42.42",
  933. },
  934. },
  935. },
  936. want: 1,
  937. },
  938. {
  939. name: "ok/more-dns",
  940. args: args{
  941. typ: DNS,
  942. ids: []Identifier{
  943. {
  944. Type: DNS,
  945. Value: "example.com",
  946. },
  947. {
  948. Type: DNS,
  949. Value: "*.example.com",
  950. },
  951. {
  952. Type: IP,
  953. Value: "192.168.42.42",
  954. },
  955. },
  956. },
  957. want: 2,
  958. },
  959. {
  960. name: "ok/more-ips",
  961. args: args{
  962. typ: IP,
  963. ids: []Identifier{
  964. {
  965. Type: DNS,
  966. Value: "example.com",
  967. },
  968. {
  969. Type: IP,
  970. Value: "192.168.42.42",
  971. },
  972. {
  973. Type: IP,
  974. Value: "192.168.42.43",
  975. },
  976. },
  977. },
  978. want: 2,
  979. },
  980. }
  981. for _, tt := range tests {
  982. t.Run(tt.name, func(t *testing.T) {
  983. if got := numberOfIdentifierType(tt.args.typ, tt.args.ids); got != tt.want {
  984. t.Errorf("numberOfIdentifierType() = %v, want %v", got, tt.want)
  985. }
  986. })
  987. }
  988. }
  989. func Test_ipsAreEqual(t *testing.T) {
  990. type args struct {
  991. x net.IP
  992. y net.IP
  993. }
  994. tests := []struct {
  995. name string
  996. args args
  997. want bool
  998. }{
  999. {
  1000. name: "ok/ipv4",
  1001. args: args{
  1002. x: net.ParseIP("192.168.42.42"),
  1003. y: net.ParseIP("192.168.42.42"),
  1004. },
  1005. want: true,
  1006. },
  1007. {
  1008. name: "fail/ipv4",
  1009. args: args{
  1010. x: net.ParseIP("192.168.42.42"),
  1011. y: net.ParseIP("192.168.42.43"),
  1012. },
  1013. want: false,
  1014. },
  1015. {
  1016. name: "ok/ipv6",
  1017. args: args{
  1018. x: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  1019. y: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  1020. },
  1021. want: true,
  1022. },
  1023. {
  1024. name: "fail/ipv6",
  1025. args: args{
  1026. x: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  1027. y: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7335"),
  1028. },
  1029. want: false,
  1030. },
  1031. {
  1032. name: "fail/ipv4-and-ipv6",
  1033. args: args{
  1034. x: net.ParseIP("192.168.42.42"),
  1035. y: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  1036. },
  1037. want: false,
  1038. },
  1039. {
  1040. name: "ok/ipv4-mapped-to-ipv6",
  1041. args: args{
  1042. x: net.ParseIP("192.168.42.42"),
  1043. y: net.ParseIP("::ffff:192.168.42.42"), // parsed to the same IPv4 by Go
  1044. },
  1045. want: true, // we expect this to happen; a known issue in which ipv4 mapped ipv6 addresses are considered the same as their ipv4 counterpart
  1046. },
  1047. {
  1048. name: "fail/invalid-ipv4-and-valid-ipv6",
  1049. args: args{
  1050. x: net.ParseIP("192.168.42.1000"),
  1051. y: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  1052. },
  1053. want: false,
  1054. },
  1055. {
  1056. name: "fail/valid-ipv4-and-invalid-ipv6",
  1057. args: args{
  1058. x: net.ParseIP("192.168.42.42"),
  1059. y: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:733400"),
  1060. },
  1061. want: false,
  1062. },
  1063. {
  1064. name: "fail/invalid-ipv4-and-invalid-ipv6",
  1065. args: args{
  1066. x: net.ParseIP("192.168.42.1000"),
  1067. y: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:1000000"),
  1068. },
  1069. want: false,
  1070. },
  1071. }
  1072. for _, tt := range tests {
  1073. t.Run(tt.name, func(t *testing.T) {
  1074. if got := ipsAreEqual(tt.args.x, tt.args.y); got != tt.want {
  1075. t.Errorf("ipsAreEqual() = %v, want %v", got, tt.want)
  1076. }
  1077. })
  1078. }
  1079. }
  1080. func Test_canonicalize(t *testing.T) {
  1081. type args struct {
  1082. csr *x509.CertificateRequest
  1083. }
  1084. tests := []struct {
  1085. name string
  1086. args args
  1087. wantCanonicalized *x509.CertificateRequest
  1088. }{
  1089. {
  1090. name: "ok/dns",
  1091. args: args{
  1092. csr: &x509.CertificateRequest{
  1093. DNSNames: []string{"www.example.com", "example.com"},
  1094. },
  1095. },
  1096. wantCanonicalized: &x509.CertificateRequest{
  1097. DNSNames: []string{"example.com", "www.example.com"},
  1098. IPAddresses: []net.IP{},
  1099. },
  1100. },
  1101. {
  1102. name: "ok/common-name",
  1103. args: args{
  1104. csr: &x509.CertificateRequest{
  1105. Subject: pkix.Name{
  1106. CommonName: "example.com",
  1107. },
  1108. DNSNames: []string{"www.example.com"},
  1109. },
  1110. },
  1111. wantCanonicalized: &x509.CertificateRequest{
  1112. Subject: pkix.Name{
  1113. CommonName: "example.com",
  1114. },
  1115. DNSNames: []string{"example.com", "www.example.com"},
  1116. IPAddresses: []net.IP{},
  1117. },
  1118. },
  1119. {
  1120. name: "ok/ipv4",
  1121. args: args{
  1122. csr: &x509.CertificateRequest{
  1123. IPAddresses: []net.IP{net.ParseIP("192.168.43.42"), net.ParseIP("192.168.42.42")},
  1124. },
  1125. },
  1126. wantCanonicalized: &x509.CertificateRequest{
  1127. DNSNames: []string{},
  1128. IPAddresses: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.43.42")},
  1129. },
  1130. },
  1131. {
  1132. name: "ok/mixed",
  1133. args: args{
  1134. csr: &x509.CertificateRequest{
  1135. DNSNames: []string{"www.example.com", "example.com"},
  1136. IPAddresses: []net.IP{net.ParseIP("192.168.43.42"), net.ParseIP("192.168.42.42")},
  1137. },
  1138. },
  1139. wantCanonicalized: &x509.CertificateRequest{
  1140. DNSNames: []string{"example.com", "www.example.com"},
  1141. IPAddresses: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.43.42")},
  1142. },
  1143. },
  1144. {
  1145. name: "ok/mixed-common-name",
  1146. args: args{
  1147. csr: &x509.CertificateRequest{
  1148. Subject: pkix.Name{
  1149. CommonName: "example.com",
  1150. },
  1151. DNSNames: []string{"www.example.com"},
  1152. IPAddresses: []net.IP{net.ParseIP("192.168.43.42"), net.ParseIP("192.168.42.42")},
  1153. },
  1154. },
  1155. wantCanonicalized: &x509.CertificateRequest{
  1156. Subject: pkix.Name{
  1157. CommonName: "example.com",
  1158. },
  1159. DNSNames: []string{"example.com", "www.example.com"},
  1160. IPAddresses: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.43.42")},
  1161. },
  1162. },
  1163. }
  1164. for _, tt := range tests {
  1165. t.Run(tt.name, func(t *testing.T) {
  1166. if gotCanonicalized := canonicalize(tt.args.csr); !reflect.DeepEqual(gotCanonicalized, tt.wantCanonicalized) {
  1167. t.Errorf("canonicalize() = %v, want %v", gotCanonicalized, tt.wantCanonicalized)
  1168. }
  1169. })
  1170. }
  1171. }
  1172. func TestOrder_sans(t *testing.T) {
  1173. type fields struct {
  1174. Identifiers []Identifier
  1175. }
  1176. tests := []struct {
  1177. name string
  1178. fields fields
  1179. csr *x509.CertificateRequest
  1180. want []x509util.SubjectAlternativeName
  1181. err *Error
  1182. }{
  1183. {
  1184. name: "ok/dns",
  1185. fields: fields{
  1186. Identifiers: []Identifier{
  1187. {Type: "dns", Value: "example.com"},
  1188. },
  1189. },
  1190. csr: &x509.CertificateRequest{
  1191. Subject: pkix.Name{
  1192. CommonName: "example.com",
  1193. },
  1194. },
  1195. want: []x509util.SubjectAlternativeName{
  1196. {Type: "dns", Value: "example.com"},
  1197. },
  1198. err: nil,
  1199. },
  1200. {
  1201. name: "fail/error-names-length-mismatch",
  1202. fields: fields{
  1203. Identifiers: []Identifier{
  1204. {Type: "dns", Value: "foo.internal"},
  1205. {Type: "dns", Value: "bar.internal"},
  1206. },
  1207. },
  1208. csr: &x509.CertificateRequest{
  1209. Subject: pkix.Name{
  1210. CommonName: "foo.internal",
  1211. },
  1212. },
  1213. want: []x509util.SubjectAlternativeName{},
  1214. err: NewError(ErrorBadCSRType, "CSR names do not match identifiers exactly: "+
  1215. "CSR names = %v, Order names = %v", []string{"foo.internal"}, []string{"bar.internal", "foo.internal"}),
  1216. },
  1217. {
  1218. name: "fail/error-names-mismatch",
  1219. fields: fields{
  1220. Identifiers: []Identifier{
  1221. {Type: "dns", Value: "foo.internal"},
  1222. {Type: "dns", Value: "bar.internal"},
  1223. },
  1224. },
  1225. csr: &x509.CertificateRequest{
  1226. Subject: pkix.Name{
  1227. CommonName: "foo.internal",
  1228. },
  1229. DNSNames: []string{"zap.internal"},
  1230. },
  1231. want: []x509util.SubjectAlternativeName{},
  1232. err: NewError(ErrorBadCSRType, "CSR names do not match identifiers exactly: "+
  1233. "CSR names = %v, Order names = %v", []string{"foo.internal", "zap.internal"}, []string{"bar.internal", "foo.internal"}),
  1234. },
  1235. {
  1236. name: "ok/ipv4",
  1237. fields: fields{
  1238. Identifiers: []Identifier{
  1239. {Type: "ip", Value: "192.168.43.42"},
  1240. {Type: "ip", Value: "192.168.42.42"},
  1241. },
  1242. },
  1243. csr: &x509.CertificateRequest{
  1244. IPAddresses: []net.IP{net.ParseIP("192.168.43.42"), net.ParseIP("192.168.42.42")},
  1245. },
  1246. want: []x509util.SubjectAlternativeName{
  1247. {Type: "ip", Value: "192.168.42.42"},
  1248. {Type: "ip", Value: "192.168.43.42"},
  1249. },
  1250. err: nil,
  1251. },
  1252. {
  1253. name: "ok/ipv6",
  1254. fields: fields{
  1255. Identifiers: []Identifier{
  1256. {Type: "ip", Value: "2001:0db8:85a3::8a2e:0370:7335"},
  1257. {Type: "ip", Value: "2001:0db8:85a3::8a2e:0370:7334"},
  1258. },
  1259. },
  1260. csr: &x509.CertificateRequest{
  1261. IPAddresses: []net.IP{net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7335"), net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334")},
  1262. },
  1263. want: []x509util.SubjectAlternativeName{
  1264. {Type: "ip", Value: "2001:db8:85a3::8a2e:370:7334"},
  1265. {Type: "ip", Value: "2001:db8:85a3::8a2e:370:7335"},
  1266. },
  1267. err: nil,
  1268. },
  1269. {
  1270. name: "fail/error-ips-length-mismatch",
  1271. fields: fields{
  1272. Identifiers: []Identifier{
  1273. {Type: "ip", Value: "192.168.42.42"},
  1274. {Type: "ip", Value: "192.168.43.42"},
  1275. },
  1276. },
  1277. csr: &x509.CertificateRequest{
  1278. IPAddresses: []net.IP{net.ParseIP("192.168.42.42")},
  1279. },
  1280. want: []x509util.SubjectAlternativeName{},
  1281. err: NewError(ErrorBadCSRType, "CSR IPs do not match identifiers exactly: "+
  1282. "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")}),
  1283. },
  1284. {
  1285. name: "fail/error-ips-mismatch",
  1286. fields: fields{
  1287. Identifiers: []Identifier{
  1288. {Type: "ip", Value: "192.168.42.42"},
  1289. {Type: "ip", Value: "192.168.43.42"},
  1290. },
  1291. },
  1292. csr: &x509.CertificateRequest{
  1293. IPAddresses: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.42.32")},
  1294. },
  1295. want: []x509util.SubjectAlternativeName{},
  1296. err: NewError(ErrorBadCSRType, "CSR IPs do not match identifiers exactly: "+
  1297. "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")}),
  1298. },
  1299. {
  1300. name: "ok/mixed",
  1301. fields: fields{
  1302. Identifiers: []Identifier{
  1303. {Type: "dns", Value: "foo.internal"},
  1304. {Type: "dns", Value: "bar.internal"},
  1305. {Type: "ip", Value: "192.168.43.42"},
  1306. {Type: "ip", Value: "192.168.42.42"},
  1307. {Type: "ip", Value: "2001:0db8:85a3:0000:0000:8a2e:0370:7334"},
  1308. },
  1309. },
  1310. csr: &x509.CertificateRequest{
  1311. Subject: pkix.Name{
  1312. CommonName: "bar.internal",
  1313. },
  1314. DNSNames: []string{"foo.internal"},
  1315. 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")},
  1316. },
  1317. want: []x509util.SubjectAlternativeName{
  1318. {Type: "dns", Value: "bar.internal"},
  1319. {Type: "dns", Value: "foo.internal"},
  1320. {Type: "ip", Value: "192.168.42.42"},
  1321. {Type: "ip", Value: "192.168.43.42"},
  1322. {Type: "ip", Value: "2001:db8:85a3::8a2e:370:7334"},
  1323. },
  1324. err: nil,
  1325. },
  1326. {
  1327. name: "fail/unsupported-identifier-type",
  1328. fields: fields{
  1329. Identifiers: []Identifier{
  1330. {Type: "ipv4", Value: "192.168.42.42"},
  1331. },
  1332. },
  1333. csr: &x509.CertificateRequest{
  1334. IPAddresses: []net.IP{net.ParseIP("192.168.42.42")},
  1335. },
  1336. want: []x509util.SubjectAlternativeName{},
  1337. err: NewError(ErrorServerInternalType, "unsupported identifier type in order: ipv4"),
  1338. },
  1339. }
  1340. for _, tt := range tests {
  1341. t.Run(tt.name, func(t *testing.T) {
  1342. o := &Order{
  1343. Identifiers: tt.fields.Identifiers,
  1344. }
  1345. canonicalizedCSR := canonicalize(tt.csr)
  1346. got, err := o.sans(canonicalizedCSR)
  1347. if tt.err != nil {
  1348. if err == nil {
  1349. t.Errorf("Order.sans() = %v, want error; got none", got)
  1350. return
  1351. }
  1352. switch k := err.(type) {
  1353. case *Error:
  1354. assert.Equals(t, k.Type, tt.err.Type)
  1355. assert.Equals(t, k.Detail, tt.err.Detail)
  1356. assert.Equals(t, k.Status, tt.err.Status)
  1357. assert.Equals(t, k.Err.Error(), tt.err.Err.Error())
  1358. assert.Equals(t, k.Detail, tt.err.Detail)
  1359. default:
  1360. assert.FatalError(t, errors.New("unexpected error type"))
  1361. }
  1362. return
  1363. }
  1364. if !reflect.DeepEqual(got, tt.want) {
  1365. t.Errorf("Order.sans() = %v, want %v", got, tt.want)
  1366. }
  1367. })
  1368. }
  1369. }