order_test.go 40 KB

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