order_test.go 41 KB

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