order_test.go 57 KB

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