order_test.go 47 KB

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