eab_test.go 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713
  1. package nosql
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "testing"
  7. "github.com/google/go-cmp/cmp"
  8. "github.com/pkg/errors"
  9. "github.com/smallstep/assert"
  10. "github.com/smallstep/certificates/acme"
  11. certdb "github.com/smallstep/certificates/db"
  12. "github.com/smallstep/nosql"
  13. nosqldb "github.com/smallstep/nosql/database"
  14. )
  15. func TestDB_getDBExternalAccountKey(t *testing.T) {
  16. keyID := "keyID"
  17. provID := "provID"
  18. type test struct {
  19. db nosql.DB
  20. err error
  21. acmeErr *acme.Error
  22. dbeak *dbExternalAccountKey
  23. }
  24. var tests = map[string]func(t *testing.T) test{
  25. "ok": func(t *testing.T) test {
  26. now := clock.Now()
  27. dbeak := &dbExternalAccountKey{
  28. ID: keyID,
  29. ProvisionerID: provID,
  30. Reference: "ref",
  31. AccountID: "",
  32. HmacKey: []byte{1, 3, 3, 7},
  33. CreatedAt: now,
  34. }
  35. b, err := json.Marshal(dbeak)
  36. assert.FatalError(t, err)
  37. return test{
  38. db: &certdb.MockNoSQLDB{
  39. MGet: func(bucket, key []byte) ([]byte, error) {
  40. assert.Equals(t, bucket, externalAccountKeyTable)
  41. assert.Equals(t, string(key), keyID)
  42. return b, nil
  43. },
  44. },
  45. err: nil,
  46. dbeak: dbeak,
  47. }
  48. },
  49. "fail/not-found": func(t *testing.T) test {
  50. return test{
  51. db: &certdb.MockNoSQLDB{
  52. MGet: func(bucket, key []byte) ([]byte, error) {
  53. assert.Equals(t, bucket, externalAccountKeyTable)
  54. assert.Equals(t, string(key), keyID)
  55. return nil, nosqldb.ErrNotFound
  56. },
  57. },
  58. err: acme.ErrNotFound,
  59. }
  60. },
  61. "fail/db.Get-error": func(t *testing.T) test {
  62. return test{
  63. db: &certdb.MockNoSQLDB{
  64. MGet: func(bucket, key []byte) ([]byte, error) {
  65. assert.Equals(t, bucket, externalAccountKeyTable)
  66. assert.Equals(t, string(key), keyID)
  67. return nil, errors.New("force")
  68. },
  69. },
  70. err: errors.New("error loading external account key keyID: force"),
  71. }
  72. },
  73. "fail/unmarshal-error": func(t *testing.T) test {
  74. return test{
  75. db: &certdb.MockNoSQLDB{
  76. MGet: func(bucket, key []byte) ([]byte, error) {
  77. assert.Equals(t, bucket, externalAccountKeyTable)
  78. assert.Equals(t, string(key), keyID)
  79. return []byte("foo"), nil
  80. },
  81. },
  82. err: errors.New("error unmarshaling external account key keyID into dbExternalAccountKey"),
  83. }
  84. },
  85. }
  86. for name, run := range tests {
  87. tc := run(t)
  88. t.Run(name, func(t *testing.T) {
  89. d := DB{db: tc.db}
  90. if dbeak, err := d.getDBExternalAccountKey(context.Background(), keyID); err != nil {
  91. var ae *acme.Error
  92. if errors.As(err, &ae) {
  93. if assert.NotNil(t, tc.acmeErr) {
  94. assert.Equals(t, ae.Type, tc.acmeErr.Type)
  95. assert.Equals(t, ae.Detail, tc.acmeErr.Detail)
  96. assert.Equals(t, ae.Status, tc.acmeErr.Status)
  97. assert.Equals(t, ae.Err.Error(), tc.acmeErr.Err.Error())
  98. assert.Equals(t, ae.Detail, tc.acmeErr.Detail)
  99. }
  100. } else {
  101. if assert.NotNil(t, tc.err) {
  102. assert.HasPrefix(t, err.Error(), tc.err.Error())
  103. }
  104. }
  105. } else if assert.Nil(t, tc.err) {
  106. assert.Equals(t, dbeak.ID, tc.dbeak.ID)
  107. assert.Equals(t, dbeak.HmacKey, tc.dbeak.HmacKey)
  108. assert.Equals(t, dbeak.ProvisionerID, tc.dbeak.ProvisionerID)
  109. assert.Equals(t, dbeak.Reference, tc.dbeak.Reference)
  110. assert.Equals(t, dbeak.CreatedAt, tc.dbeak.CreatedAt)
  111. assert.Equals(t, dbeak.AccountID, tc.dbeak.AccountID)
  112. assert.Equals(t, dbeak.BoundAt, tc.dbeak.BoundAt)
  113. }
  114. })
  115. }
  116. }
  117. func TestDB_GetExternalAccountKey(t *testing.T) {
  118. keyID := "keyID"
  119. provID := "provID"
  120. type test struct {
  121. db nosql.DB
  122. err error
  123. acmeErr *acme.Error
  124. eak *acme.ExternalAccountKey
  125. }
  126. var tests = map[string]func(t *testing.T) test{
  127. "ok": func(t *testing.T) test {
  128. now := clock.Now()
  129. dbeak := &dbExternalAccountKey{
  130. ID: keyID,
  131. ProvisionerID: provID,
  132. Reference: "ref",
  133. AccountID: "",
  134. HmacKey: []byte{1, 3, 3, 7},
  135. CreatedAt: now,
  136. }
  137. b, err := json.Marshal(dbeak)
  138. assert.FatalError(t, err)
  139. return test{
  140. db: &certdb.MockNoSQLDB{
  141. MGet: func(bucket, key []byte) ([]byte, error) {
  142. assert.Equals(t, bucket, externalAccountKeyTable)
  143. assert.Equals(t, string(key), keyID)
  144. return b, nil
  145. },
  146. },
  147. eak: &acme.ExternalAccountKey{
  148. ID: keyID,
  149. ProvisionerID: provID,
  150. Reference: "ref",
  151. AccountID: "",
  152. HmacKey: []byte{1, 3, 3, 7},
  153. CreatedAt: now,
  154. },
  155. }
  156. },
  157. "fail/db.Get-error": func(t *testing.T) test {
  158. return test{
  159. db: &certdb.MockNoSQLDB{
  160. MGet: func(bucket, key []byte) ([]byte, error) {
  161. assert.Equals(t, bucket, externalAccountKeyTable)
  162. assert.Equals(t, string(key), keyID)
  163. return nil, errors.New("force")
  164. },
  165. },
  166. err: errors.New("error loading external account key keyID: force"),
  167. }
  168. },
  169. "fail/non-matching-provisioner": func(t *testing.T) test {
  170. now := clock.Now()
  171. dbeak := &dbExternalAccountKey{
  172. ID: keyID,
  173. ProvisionerID: "aDifferentProvID",
  174. Reference: "ref",
  175. AccountID: "",
  176. HmacKey: []byte{1, 3, 3, 7},
  177. CreatedAt: now,
  178. }
  179. b, err := json.Marshal(dbeak)
  180. assert.FatalError(t, err)
  181. return test{
  182. db: &certdb.MockNoSQLDB{
  183. MGet: func(bucket, key []byte) ([]byte, error) {
  184. assert.Equals(t, bucket, externalAccountKeyTable)
  185. assert.Equals(t, string(key), keyID)
  186. return b, nil
  187. },
  188. },
  189. eak: &acme.ExternalAccountKey{
  190. ID: keyID,
  191. ProvisionerID: provID,
  192. Reference: "ref",
  193. AccountID: "",
  194. HmacKey: []byte{1, 3, 3, 7},
  195. CreatedAt: now,
  196. },
  197. acmeErr: acme.NewError(acme.ErrorUnauthorizedType, "provisioner does not match provisioner for which the EAB key was created"),
  198. }
  199. },
  200. }
  201. for name, run := range tests {
  202. tc := run(t)
  203. t.Run(name, func(t *testing.T) {
  204. d := DB{db: tc.db}
  205. if eak, err := d.GetExternalAccountKey(context.Background(), provID, keyID); err != nil {
  206. var ae *acme.Error
  207. if errors.As(err, &ae) {
  208. if assert.NotNil(t, tc.acmeErr) {
  209. assert.Equals(t, ae.Type, tc.acmeErr.Type)
  210. assert.Equals(t, ae.Detail, tc.acmeErr.Detail)
  211. assert.Equals(t, ae.Status, tc.acmeErr.Status)
  212. assert.Equals(t, ae.Err.Error(), tc.acmeErr.Err.Error())
  213. assert.Equals(t, ae.Detail, tc.acmeErr.Detail)
  214. }
  215. } else {
  216. if assert.NotNil(t, tc.err) {
  217. assert.HasPrefix(t, err.Error(), tc.err.Error())
  218. }
  219. }
  220. } else if assert.Nil(t, tc.err) {
  221. assert.Equals(t, eak.ID, tc.eak.ID)
  222. assert.Equals(t, eak.HmacKey, tc.eak.HmacKey)
  223. assert.Equals(t, eak.ProvisionerID, tc.eak.ProvisionerID)
  224. assert.Equals(t, eak.Reference, tc.eak.Reference)
  225. assert.Equals(t, eak.CreatedAt, tc.eak.CreatedAt)
  226. assert.Equals(t, eak.AccountID, tc.eak.AccountID)
  227. assert.Equals(t, eak.BoundAt, tc.eak.BoundAt)
  228. }
  229. })
  230. }
  231. }
  232. func TestDB_GetExternalAccountKeyByReference(t *testing.T) {
  233. keyID := "keyID"
  234. provID := "provID"
  235. ref := "ref"
  236. type test struct {
  237. db nosql.DB
  238. err error
  239. ref string
  240. acmeErr *acme.Error
  241. eak *acme.ExternalAccountKey
  242. }
  243. var tests = map[string]func(t *testing.T) test{
  244. "ok": func(t *testing.T) test {
  245. now := clock.Now()
  246. dbeak := &dbExternalAccountKey{
  247. ID: keyID,
  248. ProvisionerID: provID,
  249. Reference: ref,
  250. AccountID: "",
  251. HmacKey: []byte{1, 3, 3, 7},
  252. CreatedAt: now,
  253. }
  254. dbref := &dbExternalAccountKeyReference{
  255. Reference: ref,
  256. ExternalAccountKeyID: keyID,
  257. }
  258. b, err := json.Marshal(dbeak)
  259. assert.FatalError(t, err)
  260. dbrefBytes, err := json.Marshal(dbref)
  261. assert.FatalError(t, err)
  262. return test{
  263. ref: ref,
  264. db: &certdb.MockNoSQLDB{
  265. MGet: func(bucket, key []byte) ([]byte, error) {
  266. switch string(bucket) {
  267. case string(externalAccountKeyIDsByReferenceTable):
  268. assert.Equals(t, string(key), provID+"."+ref)
  269. return dbrefBytes, nil
  270. case string(externalAccountKeyTable):
  271. assert.Equals(t, string(key), keyID)
  272. return b, nil
  273. default:
  274. assert.FatalError(t, errors.Errorf("unexpected bucket %s", string(bucket)))
  275. return nil, errors.New("force")
  276. }
  277. },
  278. },
  279. eak: &acme.ExternalAccountKey{
  280. ID: keyID,
  281. ProvisionerID: provID,
  282. Reference: ref,
  283. AccountID: "",
  284. HmacKey: []byte{1, 3, 3, 7},
  285. CreatedAt: now,
  286. },
  287. err: nil,
  288. }
  289. },
  290. "ok/no-reference": func(t *testing.T) test {
  291. return test{
  292. ref: "",
  293. eak: nil,
  294. err: nil,
  295. }
  296. },
  297. "fail/reference-not-found": func(t *testing.T) test {
  298. return test{
  299. ref: ref,
  300. db: &certdb.MockNoSQLDB{
  301. MGet: func(bucket, key []byte) ([]byte, error) {
  302. assert.Equals(t, string(bucket), string(externalAccountKeyIDsByReferenceTable))
  303. assert.Equals(t, string(key), provID+"."+ref)
  304. return nil, nosqldb.ErrNotFound
  305. },
  306. },
  307. err: errors.New("not found"),
  308. }
  309. },
  310. "fail/reference-load-error": func(t *testing.T) test {
  311. return test{
  312. ref: ref,
  313. db: &certdb.MockNoSQLDB{
  314. MGet: func(bucket, key []byte) ([]byte, error) {
  315. assert.Equals(t, string(bucket), string(externalAccountKeyIDsByReferenceTable))
  316. assert.Equals(t, string(key), provID+"."+ref)
  317. return nil, errors.New("force")
  318. },
  319. },
  320. err: errors.New("error loading ACME EAB key for reference ref: force"),
  321. }
  322. },
  323. "fail/reference-unmarshal-error": func(t *testing.T) test {
  324. return test{
  325. ref: ref,
  326. db: &certdb.MockNoSQLDB{
  327. MGet: func(bucket, key []byte) ([]byte, error) {
  328. assert.Equals(t, string(bucket), string(externalAccountKeyIDsByReferenceTable))
  329. assert.Equals(t, string(key), provID+"."+ref)
  330. return []byte{0}, nil
  331. },
  332. },
  333. err: errors.New("error unmarshaling ACME EAB key for reference ref"),
  334. }
  335. },
  336. "fail/db.GetExternalAccountKey-error": func(t *testing.T) test {
  337. dbref := &dbExternalAccountKeyReference{
  338. Reference: ref,
  339. ExternalAccountKeyID: keyID,
  340. }
  341. dbrefBytes, err := json.Marshal(dbref)
  342. assert.FatalError(t, err)
  343. return test{
  344. ref: ref,
  345. db: &certdb.MockNoSQLDB{
  346. MGet: func(bucket, key []byte) ([]byte, error) {
  347. switch string(bucket) {
  348. case string(externalAccountKeyIDsByReferenceTable):
  349. assert.Equals(t, string(key), provID+"."+ref)
  350. return dbrefBytes, nil
  351. case string(externalAccountKeyTable):
  352. assert.Equals(t, string(key), keyID)
  353. return nil, errors.New("force")
  354. default:
  355. assert.FatalError(t, errors.Errorf("unexpected bucket %s", string(bucket)))
  356. return nil, errors.New("force")
  357. }
  358. },
  359. },
  360. err: errors.New("error loading external account key keyID: force"),
  361. }
  362. },
  363. }
  364. for name, run := range tests {
  365. tc := run(t)
  366. t.Run(name, func(t *testing.T) {
  367. d := DB{db: tc.db}
  368. if eak, err := d.GetExternalAccountKeyByReference(context.Background(), provID, tc.ref); err != nil {
  369. var ae *acme.Error
  370. if errors.As(err, &ae) {
  371. if assert.NotNil(t, tc.acmeErr) {
  372. assert.Equals(t, ae.Type, tc.acmeErr.Type)
  373. assert.Equals(t, ae.Detail, tc.acmeErr.Detail)
  374. assert.Equals(t, ae.Status, tc.acmeErr.Status)
  375. assert.Equals(t, ae.Err.Error(), tc.acmeErr.Err.Error())
  376. assert.Equals(t, ae.Detail, tc.acmeErr.Detail)
  377. }
  378. } else {
  379. if assert.NotNil(t, tc.err) {
  380. assert.HasPrefix(t, err.Error(), tc.err.Error())
  381. }
  382. }
  383. } else if assert.Nil(t, tc.err) && tc.eak != nil {
  384. assert.Equals(t, eak.ID, tc.eak.ID)
  385. assert.Equals(t, eak.AccountID, tc.eak.AccountID)
  386. assert.Equals(t, eak.BoundAt, tc.eak.BoundAt)
  387. assert.Equals(t, eak.CreatedAt, tc.eak.CreatedAt)
  388. assert.Equals(t, eak.HmacKey, tc.eak.HmacKey)
  389. assert.Equals(t, eak.ProvisionerID, tc.eak.ProvisionerID)
  390. assert.Equals(t, eak.Reference, tc.eak.Reference)
  391. }
  392. })
  393. }
  394. }
  395. func TestDB_GetExternalAccountKeys(t *testing.T) {
  396. keyID1 := "keyID1"
  397. keyID2 := "keyID2"
  398. keyID3 := "keyID3"
  399. provID := "provID"
  400. ref := "ref"
  401. type test struct {
  402. db nosql.DB
  403. err error
  404. acmeErr *acme.Error
  405. eaks []*acme.ExternalAccountKey
  406. }
  407. var tests = map[string]func(t *testing.T) test{
  408. "ok": func(t *testing.T) test {
  409. now := clock.Now()
  410. dbeak1 := &dbExternalAccountKey{
  411. ID: keyID1,
  412. ProvisionerID: provID,
  413. Reference: ref,
  414. AccountID: "",
  415. HmacKey: []byte{1, 3, 3, 7},
  416. CreatedAt: now,
  417. }
  418. b1, err := json.Marshal(dbeak1)
  419. assert.FatalError(t, err)
  420. dbeak2 := &dbExternalAccountKey{
  421. ID: keyID2,
  422. ProvisionerID: provID,
  423. Reference: ref,
  424. AccountID: "",
  425. HmacKey: []byte{1, 3, 3, 7},
  426. CreatedAt: now,
  427. }
  428. b2, err := json.Marshal(dbeak2)
  429. assert.FatalError(t, err)
  430. dbeak3 := &dbExternalAccountKey{
  431. ID: keyID3,
  432. ProvisionerID: "aDifferentProvID",
  433. Reference: ref,
  434. AccountID: "",
  435. HmacKey: []byte{1, 3, 3, 7},
  436. CreatedAt: now,
  437. }
  438. b3, err := json.Marshal(dbeak3)
  439. assert.FatalError(t, err)
  440. return test{
  441. db: &certdb.MockNoSQLDB{
  442. MGet: func(bucket, key []byte) ([]byte, error) {
  443. switch string(bucket) {
  444. case string(externalAccountKeyIDsByProvisionerIDTable):
  445. keys := []string{"", keyID1, keyID2} // includes an empty keyID
  446. b, err := json.Marshal(keys)
  447. assert.FatalError(t, err)
  448. return b, nil
  449. case string(externalAccountKeyTable):
  450. switch string(key) {
  451. case keyID1:
  452. return b1, nil
  453. case keyID2:
  454. return b2, nil
  455. default:
  456. assert.FatalError(t, errors.Errorf("unexpected key %s", string(key)))
  457. return nil, errors.New("force default")
  458. }
  459. default:
  460. assert.FatalError(t, errors.Errorf("unexpected bucket %s", string(bucket)))
  461. return nil, errors.New("force default")
  462. }
  463. },
  464. // TODO: remove the MList
  465. MList: func(bucket []byte) ([]*nosqldb.Entry, error) {
  466. switch string(bucket) {
  467. case string(externalAccountKeyTable):
  468. return []*nosqldb.Entry{
  469. {
  470. Bucket: bucket,
  471. Key: []byte(keyID1),
  472. Value: b1,
  473. },
  474. {
  475. Bucket: bucket,
  476. Key: []byte(keyID2),
  477. Value: b2,
  478. },
  479. {
  480. Bucket: bucket,
  481. Key: []byte(keyID3),
  482. Value: b3,
  483. },
  484. }, nil
  485. case string(externalAccountKeyIDsByProvisionerIDTable):
  486. keys := []string{keyID1, keyID2}
  487. b, err := json.Marshal(keys)
  488. assert.FatalError(t, err)
  489. return []*nosqldb.Entry{
  490. {
  491. Bucket: bucket,
  492. Key: []byte(provID),
  493. Value: b,
  494. },
  495. }, nil
  496. default:
  497. assert.FatalError(t, errors.Errorf("unexpected bucket %s", string(bucket)))
  498. return nil, errors.New("force default")
  499. }
  500. },
  501. },
  502. eaks: []*acme.ExternalAccountKey{
  503. {
  504. ID: keyID1,
  505. ProvisionerID: provID,
  506. Reference: ref,
  507. AccountID: "",
  508. HmacKey: []byte{1, 3, 3, 7},
  509. CreatedAt: now,
  510. },
  511. {
  512. ID: keyID2,
  513. ProvisionerID: provID,
  514. Reference: ref,
  515. AccountID: "",
  516. HmacKey: []byte{1, 3, 3, 7},
  517. CreatedAt: now,
  518. },
  519. },
  520. }
  521. },
  522. "fail/db.Get-externalAccountKeysByProvisionerIDTable": func(t *testing.T) test {
  523. return test{
  524. db: &certdb.MockNoSQLDB{
  525. MGet: func(bucket, key []byte) ([]byte, error) {
  526. assert.Equals(t, string(bucket), string(externalAccountKeyIDsByProvisionerIDTable))
  527. return nil, errors.New("force")
  528. },
  529. },
  530. err: errors.New("error loading ACME EAB Key IDs for provisioner provID: force"),
  531. }
  532. },
  533. "fail/db.Get-externalAccountKeysByProvisionerIDTable-unmarshal": func(t *testing.T) test {
  534. return test{
  535. db: &certdb.MockNoSQLDB{
  536. MGet: func(bucket, key []byte) ([]byte, error) {
  537. assert.Equals(t, string(bucket), string(externalAccountKeyIDsByProvisionerIDTable))
  538. b, _ := json.Marshal(1)
  539. return b, nil
  540. },
  541. },
  542. err: errors.New("error unmarshaling ACME EAB Key IDs for provisioner provID: json: cannot unmarshal number into Go value of type []string"),
  543. }
  544. },
  545. "fail/db.getDBExternalAccountKey": func(t *testing.T) test {
  546. return test{
  547. db: &certdb.MockNoSQLDB{
  548. MGet: func(bucket, key []byte) ([]byte, error) {
  549. switch string(bucket) {
  550. case string(externalAccountKeyIDsByProvisionerIDTable):
  551. keys := []string{keyID1, keyID2}
  552. b, err := json.Marshal(keys)
  553. assert.FatalError(t, err)
  554. return b, nil
  555. case string(externalAccountKeyTable):
  556. return nil, errors.New("force")
  557. default:
  558. assert.FatalError(t, errors.Errorf("unexpected bucket %s", string(bucket)))
  559. return nil, errors.New("force bucket")
  560. }
  561. },
  562. },
  563. err: errors.New("error retrieving ACME EAB Key for provisioner provID and keyID keyID1: error loading external account key keyID1: force"),
  564. }
  565. },
  566. }
  567. for name, run := range tests {
  568. tc := run(t)
  569. t.Run(name, func(t *testing.T) {
  570. d := DB{db: tc.db}
  571. cursor, limit := "", 0
  572. if eaks, nextCursor, err := d.GetExternalAccountKeys(context.Background(), provID, cursor, limit); err != nil {
  573. assert.Equals(t, "", nextCursor)
  574. var ae *acme.Error
  575. if errors.As(err, &ae) {
  576. if assert.NotNil(t, tc.acmeErr) {
  577. assert.Equals(t, ae.Type, tc.acmeErr.Type)
  578. assert.Equals(t, ae.Detail, tc.acmeErr.Detail)
  579. assert.Equals(t, ae.Status, tc.acmeErr.Status)
  580. assert.Equals(t, ae.Err.Error(), tc.acmeErr.Err.Error())
  581. assert.Equals(t, ae.Detail, tc.acmeErr.Detail)
  582. }
  583. } else {
  584. if assert.NotNil(t, tc.err) {
  585. assert.Equals(t, tc.err.Error(), err.Error())
  586. }
  587. }
  588. } else if assert.Nil(t, tc.err) {
  589. assert.Equals(t, len(eaks), len(tc.eaks))
  590. assert.Equals(t, "", nextCursor)
  591. for i, eak := range eaks {
  592. assert.Equals(t, eak.ID, tc.eaks[i].ID)
  593. assert.Equals(t, eak.HmacKey, tc.eaks[i].HmacKey)
  594. assert.Equals(t, eak.ProvisionerID, tc.eaks[i].ProvisionerID)
  595. assert.Equals(t, eak.Reference, tc.eaks[i].Reference)
  596. assert.Equals(t, eak.CreatedAt, tc.eaks[i].CreatedAt)
  597. assert.Equals(t, eak.AccountID, tc.eaks[i].AccountID)
  598. assert.Equals(t, eak.BoundAt, tc.eaks[i].BoundAt)
  599. }
  600. }
  601. })
  602. }
  603. }
  604. func TestDB_DeleteExternalAccountKey(t *testing.T) {
  605. keyID := "keyID"
  606. provID := "provID"
  607. ref := "ref"
  608. type test struct {
  609. db nosql.DB
  610. err error
  611. acmeErr *acme.Error
  612. }
  613. var tests = map[string]func(t *testing.T) test{
  614. "ok": func(t *testing.T) test {
  615. now := clock.Now()
  616. dbeak := &dbExternalAccountKey{
  617. ID: keyID,
  618. ProvisionerID: provID,
  619. Reference: ref,
  620. AccountID: "",
  621. HmacKey: []byte{1, 3, 3, 7},
  622. CreatedAt: now,
  623. }
  624. dbref := &dbExternalAccountKeyReference{
  625. Reference: ref,
  626. ExternalAccountKeyID: keyID,
  627. }
  628. b, err := json.Marshal(dbeak)
  629. assert.FatalError(t, err)
  630. dbrefBytes, err := json.Marshal(dbref)
  631. assert.FatalError(t, err)
  632. return test{
  633. db: &certdb.MockNoSQLDB{
  634. MGet: func(bucket, key []byte) ([]byte, error) {
  635. switch string(bucket) {
  636. case string(externalAccountKeyIDsByReferenceTable):
  637. assert.Equals(t, string(key), provID+"."+ref)
  638. return dbrefBytes, nil
  639. case string(externalAccountKeyTable):
  640. assert.Equals(t, string(key), keyID)
  641. return b, nil
  642. case string(externalAccountKeyIDsByProvisionerIDTable):
  643. assert.Equals(t, provID, string(key))
  644. b, err := json.Marshal([]string{keyID})
  645. assert.FatalError(t, err)
  646. return b, nil
  647. default:
  648. assert.FatalError(t, errors.Errorf("unexpected bucket %s", string(bucket)))
  649. return nil, errors.New("force default")
  650. }
  651. },
  652. MDel: func(bucket, key []byte) error {
  653. switch string(bucket) {
  654. case string(externalAccountKeyIDsByReferenceTable):
  655. assert.Equals(t, string(key), provID+"."+ref)
  656. return nil
  657. case string(externalAccountKeyTable):
  658. assert.Equals(t, string(key), keyID)
  659. return nil
  660. default:
  661. assert.FatalError(t, errors.Errorf("unexpected bucket %s", string(bucket)))
  662. return errors.New("force default")
  663. }
  664. },
  665. MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
  666. fmt.Println(string(bucket))
  667. switch string(bucket) {
  668. case string(externalAccountKeyIDsByReferenceTable):
  669. assert.Equals(t, provID+"."+ref, string(key))
  670. return nil, true, nil
  671. case string(externalAccountKeyIDsByProvisionerIDTable):
  672. assert.Equals(t, provID, string(key))
  673. return nil, true, nil
  674. default:
  675. assert.FatalError(t, errors.Errorf("unexpected bucket %s", string(bucket)))
  676. return nil, false, errors.New("force default")
  677. }
  678. },
  679. },
  680. }
  681. },
  682. "fail/not-found": func(t *testing.T) test {
  683. return test{
  684. db: &certdb.MockNoSQLDB{
  685. MGet: func(bucket, key []byte) ([]byte, error) {
  686. assert.Equals(t, string(bucket), string(externalAccountKeyTable))
  687. assert.Equals(t, string(key), keyID)
  688. return nil, nosqldb.ErrNotFound
  689. },
  690. },
  691. err: errors.New("error loading ACME EAB Key with Key ID keyID: not found"),
  692. }
  693. },
  694. "fail/non-matching-provisioner": func(t *testing.T) test {
  695. now := clock.Now()
  696. dbeak := &dbExternalAccountKey{
  697. ID: keyID,
  698. ProvisionerID: "aDifferentProvID",
  699. Reference: ref,
  700. AccountID: "",
  701. HmacKey: []byte{1, 3, 3, 7},
  702. CreatedAt: now,
  703. }
  704. b, err := json.Marshal(dbeak)
  705. assert.FatalError(t, err)
  706. return test{
  707. db: &certdb.MockNoSQLDB{
  708. MGet: func(bucket, key []byte) ([]byte, error) {
  709. assert.Equals(t, string(bucket), string(externalAccountKeyTable))
  710. assert.Equals(t, string(key), keyID)
  711. return b, nil
  712. },
  713. },
  714. err: errors.New("provisioner does not match provisioner for which the EAB key was created"),
  715. }
  716. },
  717. "fail/delete-reference": func(t *testing.T) test {
  718. now := clock.Now()
  719. dbeak := &dbExternalAccountKey{
  720. ID: keyID,
  721. ProvisionerID: provID,
  722. Reference: ref,
  723. AccountID: "",
  724. HmacKey: []byte{1, 3, 3, 7},
  725. CreatedAt: now,
  726. }
  727. dbref := &dbExternalAccountKeyReference{
  728. Reference: ref,
  729. ExternalAccountKeyID: keyID,
  730. }
  731. b, err := json.Marshal(dbeak)
  732. assert.FatalError(t, err)
  733. dbrefBytes, err := json.Marshal(dbref)
  734. assert.FatalError(t, err)
  735. return test{
  736. db: &certdb.MockNoSQLDB{
  737. MGet: func(bucket, key []byte) ([]byte, error) {
  738. switch string(bucket) {
  739. case string(externalAccountKeyIDsByReferenceTable):
  740. assert.Equals(t, string(key), ref)
  741. return dbrefBytes, nil
  742. case string(externalAccountKeyTable):
  743. assert.Equals(t, string(key), keyID)
  744. return b, nil
  745. default:
  746. assert.FatalError(t, errors.Errorf("unexpected bucket %s", string(bucket)))
  747. return nil, errors.New("force default")
  748. }
  749. },
  750. MDel: func(bucket, key []byte) error {
  751. switch string(bucket) {
  752. case string(externalAccountKeyIDsByReferenceTable):
  753. assert.Equals(t, string(key), provID+"."+ref)
  754. return errors.New("force")
  755. case string(externalAccountKeyTable):
  756. assert.Equals(t, string(key), keyID)
  757. return nil
  758. default:
  759. assert.FatalError(t, errors.Errorf("unexpected bucket %s", string(bucket)))
  760. return errors.New("force default")
  761. }
  762. },
  763. },
  764. err: errors.New("error deleting ACME EAB Key reference with Key ID keyID and reference ref: force"),
  765. }
  766. },
  767. "fail/delete-eak": func(t *testing.T) test {
  768. now := clock.Now()
  769. dbeak := &dbExternalAccountKey{
  770. ID: keyID,
  771. ProvisionerID: provID,
  772. Reference: ref,
  773. AccountID: "",
  774. HmacKey: []byte{1, 3, 3, 7},
  775. CreatedAt: now,
  776. }
  777. dbref := &dbExternalAccountKeyReference{
  778. Reference: ref,
  779. ExternalAccountKeyID: keyID,
  780. }
  781. b, err := json.Marshal(dbeak)
  782. assert.FatalError(t, err)
  783. dbrefBytes, err := json.Marshal(dbref)
  784. assert.FatalError(t, err)
  785. return test{
  786. db: &certdb.MockNoSQLDB{
  787. MGet: func(bucket, key []byte) ([]byte, error) {
  788. switch string(bucket) {
  789. case string(externalAccountKeyIDsByReferenceTable):
  790. assert.Equals(t, string(key), ref)
  791. return dbrefBytes, nil
  792. case string(externalAccountKeyTable):
  793. assert.Equals(t, string(key), keyID)
  794. return b, nil
  795. default:
  796. assert.FatalError(t, errors.Errorf("unexpected bucket %s", string(bucket)))
  797. return nil, errors.New("force default")
  798. }
  799. },
  800. MDel: func(bucket, key []byte) error {
  801. switch string(bucket) {
  802. case string(externalAccountKeyIDsByReferenceTable):
  803. assert.Equals(t, string(key), provID+"."+ref)
  804. return nil
  805. case string(externalAccountKeyTable):
  806. assert.Equals(t, string(key), keyID)
  807. return errors.New("force")
  808. default:
  809. assert.FatalError(t, errors.Errorf("unexpected bucket %s", string(bucket)))
  810. return errors.New("force default")
  811. }
  812. },
  813. },
  814. err: errors.New("error deleting ACME EAB Key with Key ID keyID: force"),
  815. }
  816. },
  817. "fail/delete-eakID": func(t *testing.T) test {
  818. now := clock.Now()
  819. dbeak := &dbExternalAccountKey{
  820. ID: keyID,
  821. ProvisionerID: provID,
  822. Reference: ref,
  823. AccountID: "",
  824. HmacKey: []byte{1, 3, 3, 7},
  825. CreatedAt: now,
  826. }
  827. dbref := &dbExternalAccountKeyReference{
  828. Reference: ref,
  829. ExternalAccountKeyID: keyID,
  830. }
  831. b, err := json.Marshal(dbeak)
  832. assert.FatalError(t, err)
  833. dbrefBytes, err := json.Marshal(dbref)
  834. assert.FatalError(t, err)
  835. return test{
  836. db: &certdb.MockNoSQLDB{
  837. MGet: func(bucket, key []byte) ([]byte, error) {
  838. switch string(bucket) {
  839. case string(externalAccountKeyIDsByReferenceTable):
  840. assert.Equals(t, string(key), ref)
  841. return dbrefBytes, nil
  842. case string(externalAccountKeyTable):
  843. assert.Equals(t, string(key), keyID)
  844. return b, nil
  845. case string(externalAccountKeyIDsByProvisionerIDTable):
  846. return b, errors.New("force")
  847. default:
  848. assert.FatalError(t, errors.Errorf("unexpected bucket %s", string(bucket)))
  849. return nil, errors.New("force default")
  850. }
  851. },
  852. MDel: func(bucket, key []byte) error {
  853. switch string(bucket) {
  854. case string(externalAccountKeyIDsByReferenceTable):
  855. assert.Equals(t, string(key), provID+"."+ref)
  856. return nil
  857. case string(externalAccountKeyTable):
  858. assert.Equals(t, string(key), keyID)
  859. return nil
  860. default:
  861. assert.FatalError(t, errors.Errorf("unexpected bucket %s", string(bucket)))
  862. return errors.New("force default")
  863. }
  864. },
  865. },
  866. err: errors.New("error removing ACME EAB Key ID keyID: error loading eakIDs for provisioner provID: force"),
  867. }
  868. },
  869. }
  870. for name, run := range tests {
  871. tc := run(t)
  872. t.Run(name, func(t *testing.T) {
  873. d := DB{db: tc.db}
  874. if err := d.DeleteExternalAccountKey(context.Background(), provID, keyID); err != nil {
  875. var ae *acme.Error
  876. if errors.As(err, &ae) {
  877. if assert.NotNil(t, tc.acmeErr) {
  878. assert.Equals(t, ae.Type, tc.acmeErr.Type)
  879. assert.Equals(t, ae.Detail, tc.acmeErr.Detail)
  880. assert.Equals(t, ae.Status, tc.acmeErr.Status)
  881. assert.Equals(t, ae.Err.Error(), tc.acmeErr.Err.Error())
  882. assert.Equals(t, ae.Detail, tc.acmeErr.Detail)
  883. }
  884. } else {
  885. if assert.NotNil(t, tc.err) {
  886. assert.Equals(t, err.Error(), tc.err.Error())
  887. }
  888. }
  889. } else {
  890. assert.Nil(t, tc.err)
  891. }
  892. })
  893. }
  894. }
  895. func TestDB_CreateExternalAccountKey(t *testing.T) {
  896. keyID := "keyID"
  897. provID := "provID"
  898. ref := "ref"
  899. type test struct {
  900. db nosql.DB
  901. err error
  902. _id *string
  903. eak *acme.ExternalAccountKey
  904. }
  905. var tests = map[string]func(t *testing.T) test{
  906. "ok": func(t *testing.T) test {
  907. var (
  908. id string
  909. idPtr = &id
  910. )
  911. now := clock.Now()
  912. eak := &acme.ExternalAccountKey{
  913. ID: keyID,
  914. ProvisionerID: provID,
  915. Reference: "ref",
  916. AccountID: "",
  917. CreatedAt: now,
  918. }
  919. return test{
  920. db: &certdb.MockNoSQLDB{
  921. MGet: func(bucket, key []byte) ([]byte, error) {
  922. assert.Equals(t, string(bucket), string(externalAccountKeyIDsByProvisionerIDTable))
  923. assert.Equals(t, provID, string(key))
  924. b, _ := json.Marshal([]string{})
  925. return b, nil
  926. },
  927. MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
  928. switch string(bucket) {
  929. case string(externalAccountKeyIDsByProvisionerIDTable):
  930. assert.Equals(t, provID, string(key))
  931. return nu, true, nil
  932. case string(externalAccountKeyIDsByReferenceTable):
  933. assert.Equals(t, provID+"."+ref, string(key))
  934. assert.Equals(t, nil, old)
  935. return nu, true, nil
  936. case string(externalAccountKeyTable):
  937. assert.Equals(t, nil, old)
  938. id = string(key)
  939. dbeak := new(dbExternalAccountKey)
  940. assert.FatalError(t, json.Unmarshal(nu, dbeak))
  941. assert.Equals(t, string(key), dbeak.ID)
  942. assert.Equals(t, eak.ProvisionerID, dbeak.ProvisionerID)
  943. assert.Equals(t, eak.Reference, dbeak.Reference)
  944. assert.Equals(t, 32, len(dbeak.HmacKey))
  945. assert.False(t, dbeak.CreatedAt.IsZero())
  946. assert.Equals(t, dbeak.AccountID, eak.AccountID)
  947. assert.True(t, dbeak.BoundAt.IsZero())
  948. return nu, true, nil
  949. default:
  950. assert.FatalError(t, errors.Errorf("unexpected bucket %s", string(bucket)))
  951. return nil, false, errors.New("force default")
  952. }
  953. },
  954. },
  955. eak: eak,
  956. _id: idPtr,
  957. }
  958. },
  959. "fail/externalAccountKeyID-cmpAndSwap-error": func(t *testing.T) test {
  960. return test{
  961. db: &certdb.MockNoSQLDB{
  962. MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
  963. switch string(bucket) {
  964. case string(externalAccountKeyIDsByReferenceTable):
  965. assert.Equals(t, string(key), ref)
  966. assert.Equals(t, old, nil)
  967. return nu, true, nil
  968. case string(externalAccountKeyTable):
  969. assert.Equals(t, old, nil)
  970. return nu, true, errors.New("force")
  971. default:
  972. assert.FatalError(t, errors.Errorf("unexpected bucket %s", string(bucket)))
  973. return nil, false, errors.New("force default")
  974. }
  975. },
  976. },
  977. err: errors.New("error saving acme external_account_key: force"),
  978. }
  979. },
  980. "fail/addEAKID-error": func(t *testing.T) test {
  981. return test{
  982. db: &certdb.MockNoSQLDB{
  983. MGet: func(bucket, key []byte) ([]byte, error) {
  984. assert.Equals(t, string(bucket), string(externalAccountKeyIDsByProvisionerIDTable))
  985. assert.Equals(t, provID, string(key))
  986. return nil, errors.New("force")
  987. },
  988. MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
  989. switch string(bucket) {
  990. case string(externalAccountKeyIDsByReferenceTable):
  991. assert.Equals(t, string(key), ref)
  992. assert.Equals(t, old, nil)
  993. return nu, true, nil
  994. case string(externalAccountKeyTable):
  995. assert.Equals(t, old, nil)
  996. return nu, true, nil
  997. default:
  998. assert.FatalError(t, errors.Errorf("unexpected bucket %s", string(bucket)))
  999. return nil, false, errors.New("force default")
  1000. }
  1001. },
  1002. },
  1003. err: errors.New("error loading eakIDs for provisioner provID: force"),
  1004. }
  1005. },
  1006. "fail/externalAccountKeyReference-cmpAndSwap-error": func(t *testing.T) test {
  1007. return test{
  1008. db: &certdb.MockNoSQLDB{
  1009. MGet: func(bucket, key []byte) ([]byte, error) {
  1010. assert.Equals(t, string(bucket), string(externalAccountKeyIDsByProvisionerIDTable))
  1011. assert.Equals(t, provID, string(key))
  1012. b, _ := json.Marshal([]string{})
  1013. return b, nil
  1014. },
  1015. MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
  1016. switch string(bucket) {
  1017. case string(externalAccountKeyIDsByProvisionerIDTable):
  1018. assert.Equals(t, provID, string(key))
  1019. return nu, true, nil
  1020. case string(externalAccountKeyIDsByReferenceTable):
  1021. assert.Equals(t, provID+"."+ref, string(key))
  1022. assert.Equals(t, old, nil)
  1023. return nu, true, errors.New("force")
  1024. case string(externalAccountKeyTable):
  1025. assert.Equals(t, old, nil)
  1026. return nu, true, nil
  1027. default:
  1028. assert.FatalError(t, errors.Errorf("unexpected bucket %s", string(bucket)))
  1029. return nil, false, errors.New("force default")
  1030. }
  1031. },
  1032. },
  1033. err: errors.New("error saving acme external_account_key_reference: force"),
  1034. }
  1035. },
  1036. }
  1037. for name, run := range tests {
  1038. tc := run(t)
  1039. t.Run(name, func(t *testing.T) {
  1040. d := DB{db: tc.db}
  1041. eak, err := d.CreateExternalAccountKey(context.Background(), provID, ref)
  1042. if err != nil {
  1043. if assert.NotNil(t, tc.err) {
  1044. assert.Equals(t, err.Error(), tc.err.Error())
  1045. }
  1046. } else if assert.Nil(t, tc.err) {
  1047. assert.Equals(t, *tc._id, eak.ID)
  1048. assert.Equals(t, provID, eak.ProvisionerID)
  1049. assert.Equals(t, ref, eak.Reference)
  1050. assert.Equals(t, "", eak.AccountID)
  1051. assert.False(t, eak.CreatedAt.IsZero())
  1052. assert.False(t, eak.AlreadyBound())
  1053. assert.True(t, eak.BoundAt.IsZero())
  1054. }
  1055. })
  1056. }
  1057. }
  1058. func TestDB_UpdateExternalAccountKey(t *testing.T) {
  1059. keyID := "keyID"
  1060. provID := "provID"
  1061. ref := "ref"
  1062. now := clock.Now()
  1063. dbeak := &dbExternalAccountKey{
  1064. ID: keyID,
  1065. ProvisionerID: provID,
  1066. Reference: ref,
  1067. AccountID: "",
  1068. HmacKey: []byte{1, 3, 3, 7},
  1069. CreatedAt: now,
  1070. }
  1071. b, err := json.Marshal(dbeak)
  1072. assert.FatalError(t, err)
  1073. type test struct {
  1074. db nosql.DB
  1075. eak *acme.ExternalAccountKey
  1076. err error
  1077. }
  1078. var tests = map[string]func(t *testing.T) test{
  1079. "ok": func(t *testing.T) test {
  1080. eak := &acme.ExternalAccountKey{
  1081. ID: keyID,
  1082. ProvisionerID: provID,
  1083. Reference: ref,
  1084. AccountID: "",
  1085. HmacKey: []byte{1, 3, 3, 7},
  1086. CreatedAt: now,
  1087. }
  1088. return test{
  1089. eak: eak,
  1090. db: &certdb.MockNoSQLDB{
  1091. MGet: func(bucket, key []byte) ([]byte, error) {
  1092. assert.Equals(t, bucket, externalAccountKeyTable)
  1093. assert.Equals(t, string(key), keyID)
  1094. return b, nil
  1095. },
  1096. MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
  1097. assert.Equals(t, bucket, externalAccountKeyTable)
  1098. assert.Equals(t, old, b)
  1099. dbNew := new(dbExternalAccountKey)
  1100. assert.FatalError(t, json.Unmarshal(nu, dbNew))
  1101. assert.Equals(t, dbNew.ID, dbeak.ID)
  1102. assert.Equals(t, dbNew.ProvisionerID, dbeak.ProvisionerID)
  1103. assert.Equals(t, dbNew.Reference, dbeak.Reference)
  1104. assert.Equals(t, dbNew.AccountID, dbeak.AccountID)
  1105. assert.Equals(t, dbNew.CreatedAt, dbeak.CreatedAt)
  1106. assert.Equals(t, dbNew.BoundAt, dbeak.BoundAt)
  1107. assert.Equals(t, dbNew.HmacKey, dbeak.HmacKey)
  1108. return nu, true, nil
  1109. },
  1110. },
  1111. }
  1112. },
  1113. "fail/db.Get-error": func(t *testing.T) test {
  1114. return test{
  1115. eak: &acme.ExternalAccountKey{
  1116. ID: keyID,
  1117. },
  1118. db: &certdb.MockNoSQLDB{
  1119. MGet: func(bucket, key []byte) ([]byte, error) {
  1120. assert.Equals(t, bucket, externalAccountKeyTable)
  1121. assert.Equals(t, string(key), keyID)
  1122. return nil, errors.New("force")
  1123. },
  1124. },
  1125. err: errors.New("error loading external account key keyID: force"),
  1126. }
  1127. },
  1128. "fail/provisioner-mismatch": func(t *testing.T) test {
  1129. newDBEAK := &dbExternalAccountKey{
  1130. ID: keyID,
  1131. ProvisionerID: "aDifferentProvID",
  1132. Reference: ref,
  1133. AccountID: "",
  1134. HmacKey: []byte{1, 3, 3, 7},
  1135. CreatedAt: now,
  1136. }
  1137. b, err := json.Marshal(newDBEAK)
  1138. assert.FatalError(t, err)
  1139. return test{
  1140. eak: &acme.ExternalAccountKey{
  1141. ID: keyID,
  1142. },
  1143. db: &certdb.MockNoSQLDB{
  1144. MGet: func(bucket, key []byte) ([]byte, error) {
  1145. assert.Equals(t, bucket, externalAccountKeyTable)
  1146. assert.Equals(t, string(key), keyID)
  1147. return b, nil
  1148. },
  1149. },
  1150. err: errors.New("provisioner does not match provisioner for which the EAB key was created"),
  1151. }
  1152. },
  1153. "fail/provisioner-change": func(t *testing.T) test {
  1154. newDBEAK := &dbExternalAccountKey{
  1155. ID: keyID,
  1156. ProvisionerID: provID,
  1157. Reference: ref,
  1158. AccountID: "",
  1159. HmacKey: []byte{1, 3, 3, 7},
  1160. CreatedAt: now,
  1161. }
  1162. b, err := json.Marshal(newDBEAK)
  1163. assert.FatalError(t, err)
  1164. return test{
  1165. eak: &acme.ExternalAccountKey{
  1166. ID: keyID,
  1167. ProvisionerID: "aDifferentProvisionerID",
  1168. },
  1169. db: &certdb.MockNoSQLDB{
  1170. MGet: func(bucket, key []byte) ([]byte, error) {
  1171. assert.Equals(t, bucket, externalAccountKeyTable)
  1172. assert.Equals(t, string(key), keyID)
  1173. return b, nil
  1174. },
  1175. },
  1176. err: errors.New("cannot change provisioner for an existing ACME EAB Key"),
  1177. }
  1178. },
  1179. "fail/reference-change": func(t *testing.T) test {
  1180. newDBEAK := &dbExternalAccountKey{
  1181. ID: keyID,
  1182. ProvisionerID: provID,
  1183. Reference: ref,
  1184. AccountID: "",
  1185. HmacKey: []byte{1, 3, 3, 7},
  1186. CreatedAt: now,
  1187. }
  1188. b, err := json.Marshal(newDBEAK)
  1189. assert.FatalError(t, err)
  1190. return test{
  1191. eak: &acme.ExternalAccountKey{
  1192. ID: keyID,
  1193. ProvisionerID: provID,
  1194. Reference: "aDifferentReference",
  1195. },
  1196. db: &certdb.MockNoSQLDB{
  1197. MGet: func(bucket, key []byte) ([]byte, error) {
  1198. assert.Equals(t, bucket, externalAccountKeyTable)
  1199. assert.Equals(t, string(key), keyID)
  1200. return b, nil
  1201. },
  1202. },
  1203. err: errors.New("cannot change reference for an existing ACME EAB Key"),
  1204. }
  1205. },
  1206. }
  1207. for name, run := range tests {
  1208. tc := run(t)
  1209. t.Run(name, func(t *testing.T) {
  1210. d := DB{db: tc.db}
  1211. if err := d.UpdateExternalAccountKey(context.Background(), provID, tc.eak); err != nil {
  1212. if assert.NotNil(t, tc.err) {
  1213. assert.HasPrefix(t, err.Error(), tc.err.Error())
  1214. }
  1215. } else if assert.Nil(t, tc.err) {
  1216. assert.Equals(t, dbeak.ID, tc.eak.ID)
  1217. assert.Equals(t, dbeak.ProvisionerID, tc.eak.ProvisionerID)
  1218. assert.Equals(t, dbeak.Reference, tc.eak.Reference)
  1219. assert.Equals(t, dbeak.AccountID, tc.eak.AccountID)
  1220. assert.Equals(t, dbeak.CreatedAt, tc.eak.CreatedAt)
  1221. assert.Equals(t, dbeak.BoundAt, tc.eak.BoundAt)
  1222. assert.Equals(t, dbeak.HmacKey, tc.eak.HmacKey)
  1223. }
  1224. })
  1225. }
  1226. }
  1227. func TestDB_addEAKID(t *testing.T) {
  1228. provID := "provID"
  1229. eakID := "eakID"
  1230. type test struct {
  1231. ctx context.Context
  1232. provisionerID string
  1233. eakID string
  1234. db nosql.DB
  1235. err error
  1236. }
  1237. var tests = map[string]func(t *testing.T) test{
  1238. "fail/empty-eakID": func(t *testing.T) test {
  1239. return test{
  1240. ctx: context.Background(),
  1241. provisionerID: provID,
  1242. eakID: "",
  1243. err: errors.New("can't add empty eakID for provisioner provID"),
  1244. }
  1245. },
  1246. "fail/db.Get": func(t *testing.T) test {
  1247. return test{
  1248. ctx: context.Background(),
  1249. provisionerID: provID,
  1250. eakID: eakID,
  1251. db: &certdb.MockNoSQLDB{
  1252. MGet: func(bucket, key []byte) ([]byte, error) {
  1253. assert.Equals(t, bucket, externalAccountKeyIDsByProvisionerIDTable)
  1254. assert.Equals(t, string(key), provID)
  1255. return nil, errors.New("force")
  1256. },
  1257. },
  1258. err: errors.New("error loading eakIDs for provisioner provID: force"),
  1259. }
  1260. },
  1261. "fail/unmarshal": func(t *testing.T) test {
  1262. return test{
  1263. ctx: context.Background(),
  1264. provisionerID: provID,
  1265. eakID: eakID,
  1266. db: &certdb.MockNoSQLDB{
  1267. MGet: func(bucket, key []byte) ([]byte, error) {
  1268. assert.Equals(t, bucket, externalAccountKeyIDsByProvisionerIDTable)
  1269. assert.Equals(t, string(key), provID)
  1270. b, _ := json.Marshal(1)
  1271. return b, nil
  1272. },
  1273. },
  1274. err: errors.New("error unmarshaling eakIDs for provisioner provID: json: cannot unmarshal number into Go value of type []string"),
  1275. }
  1276. },
  1277. "fail/eakID-already-exists": func(t *testing.T) test {
  1278. return test{
  1279. ctx: context.Background(),
  1280. provisionerID: provID,
  1281. eakID: eakID,
  1282. db: &certdb.MockNoSQLDB{
  1283. MGet: func(bucket, key []byte) ([]byte, error) {
  1284. assert.Equals(t, bucket, externalAccountKeyIDsByProvisionerIDTable)
  1285. assert.Equals(t, string(key), provID)
  1286. b, _ := json.Marshal([]string{eakID})
  1287. return b, nil
  1288. },
  1289. },
  1290. err: errors.New("eakID eakID already exists for provisioner provID"),
  1291. }
  1292. },
  1293. "fail/db.save": func(t *testing.T) test {
  1294. return test{
  1295. ctx: context.Background(),
  1296. provisionerID: provID,
  1297. eakID: eakID,
  1298. db: &certdb.MockNoSQLDB{
  1299. MGet: func(bucket, key []byte) ([]byte, error) {
  1300. assert.Equals(t, bucket, externalAccountKeyIDsByProvisionerIDTable)
  1301. assert.Equals(t, string(key), provID)
  1302. b, _ := json.Marshal([]string{"id1"})
  1303. return b, nil
  1304. },
  1305. MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
  1306. assert.Equals(t, bucket, externalAccountKeyIDsByProvisionerIDTable)
  1307. assert.Equals(t, string(key), provID)
  1308. oldB, _ := json.Marshal([]string{"id1"})
  1309. assert.Equals(t, old, oldB)
  1310. newB, _ := json.Marshal([]string{"id1", eakID})
  1311. assert.Equals(t, nu, newB)
  1312. return newB, true, errors.New("force")
  1313. },
  1314. },
  1315. err: errors.New("error saving eakIDs index for provisioner provID: error saving acme externalAccountKeyIDsByProvisionerID: force"),
  1316. }
  1317. },
  1318. "ok/db.Get-not-found": func(t *testing.T) test {
  1319. return test{
  1320. ctx: context.Background(),
  1321. provisionerID: provID,
  1322. eakID: eakID,
  1323. db: &certdb.MockNoSQLDB{
  1324. MGet: func(bucket, key []byte) ([]byte, error) {
  1325. assert.Equals(t, bucket, externalAccountKeyIDsByProvisionerIDTable)
  1326. assert.Equals(t, string(key), provID)
  1327. return nil, nosqldb.ErrNotFound
  1328. },
  1329. MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
  1330. assert.Equals(t, bucket, externalAccountKeyIDsByProvisionerIDTable)
  1331. assert.Equals(t, string(key), provID)
  1332. assert.Equals(t, old, nil)
  1333. b, _ := json.Marshal([]string{eakID})
  1334. assert.Equals(t, nu, b)
  1335. return b, true, nil
  1336. },
  1337. },
  1338. err: nil,
  1339. }
  1340. },
  1341. "ok": func(t *testing.T) test {
  1342. return test{
  1343. ctx: context.Background(),
  1344. provisionerID: provID,
  1345. eakID: eakID,
  1346. db: &certdb.MockNoSQLDB{
  1347. MGet: func(bucket, key []byte) ([]byte, error) {
  1348. assert.Equals(t, bucket, externalAccountKeyIDsByProvisionerIDTable)
  1349. assert.Equals(t, string(key), provID)
  1350. b, _ := json.Marshal([]string{"id1", "id2"})
  1351. return b, nil
  1352. },
  1353. MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
  1354. assert.Equals(t, bucket, externalAccountKeyIDsByProvisionerIDTable)
  1355. assert.Equals(t, string(key), provID)
  1356. oldB, _ := json.Marshal([]string{"id1", "id2"})
  1357. assert.Equals(t, old, oldB)
  1358. newB, _ := json.Marshal([]string{"id1", "id2", eakID})
  1359. assert.Equals(t, nu, newB)
  1360. return newB, true, nil
  1361. },
  1362. },
  1363. err: nil,
  1364. }
  1365. },
  1366. }
  1367. for name, run := range tests {
  1368. tc := run(t)
  1369. t.Run(name, func(t *testing.T) {
  1370. db := &DB{
  1371. db: tc.db,
  1372. }
  1373. wantErr := tc.err != nil
  1374. err := db.addEAKID(tc.ctx, tc.provisionerID, tc.eakID)
  1375. if (err != nil) != wantErr {
  1376. t.Errorf("DB.addEAKID() error = %v, wantErr %v", err, wantErr)
  1377. }
  1378. if err != nil {
  1379. assert.Equals(t, tc.err.Error(), err.Error())
  1380. }
  1381. })
  1382. }
  1383. }
  1384. func TestDB_deleteEAKID(t *testing.T) {
  1385. provID := "provID"
  1386. eakID := "eakID"
  1387. type test struct {
  1388. ctx context.Context
  1389. provisionerID string
  1390. eakID string
  1391. db nosql.DB
  1392. err error
  1393. }
  1394. var tests = map[string]func(t *testing.T) test{
  1395. "fail/db.Get": func(t *testing.T) test {
  1396. return test{
  1397. ctx: context.Background(),
  1398. provisionerID: provID,
  1399. eakID: eakID,
  1400. db: &certdb.MockNoSQLDB{
  1401. MGet: func(bucket, key []byte) ([]byte, error) {
  1402. assert.Equals(t, bucket, externalAccountKeyIDsByProvisionerIDTable)
  1403. assert.Equals(t, string(key), provID)
  1404. return nil, errors.New("force")
  1405. },
  1406. },
  1407. err: errors.New("error loading eakIDs for provisioner provID: force"),
  1408. }
  1409. },
  1410. "fail/unmarshal": func(t *testing.T) test {
  1411. return test{
  1412. ctx: context.Background(),
  1413. provisionerID: provID,
  1414. eakID: eakID,
  1415. db: &certdb.MockNoSQLDB{
  1416. MGet: func(bucket, key []byte) ([]byte, error) {
  1417. assert.Equals(t, bucket, externalAccountKeyIDsByProvisionerIDTable)
  1418. assert.Equals(t, string(key), provID)
  1419. b, _ := json.Marshal(1)
  1420. return b, nil
  1421. },
  1422. },
  1423. err: errors.New("error unmarshaling eakIDs for provisioner provID: json: cannot unmarshal number into Go value of type []string"),
  1424. }
  1425. },
  1426. "fail/db.save": func(t *testing.T) test {
  1427. return test{
  1428. ctx: context.Background(),
  1429. provisionerID: provID,
  1430. eakID: eakID,
  1431. db: &certdb.MockNoSQLDB{
  1432. MGet: func(bucket, key []byte) ([]byte, error) {
  1433. assert.Equals(t, bucket, externalAccountKeyIDsByProvisionerIDTable)
  1434. assert.Equals(t, string(key), provID)
  1435. b, _ := json.Marshal([]string{"id1", eakID})
  1436. return b, nil
  1437. },
  1438. MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
  1439. assert.Equals(t, bucket, externalAccountKeyIDsByProvisionerIDTable)
  1440. assert.Equals(t, string(key), provID)
  1441. oldB, _ := json.Marshal([]string{"id1", eakID})
  1442. assert.Equals(t, old, oldB)
  1443. newB, _ := json.Marshal([]string{"id1"})
  1444. assert.Equals(t, nu, newB)
  1445. return newB, true, errors.New("force")
  1446. },
  1447. },
  1448. err: errors.New("error saving eakIDs index for provisioner provID: error saving acme externalAccountKeyIDsByProvisionerID: force"),
  1449. }
  1450. },
  1451. "ok/db.Get-not-found": func(t *testing.T) test {
  1452. return test{
  1453. ctx: context.Background(),
  1454. provisionerID: provID,
  1455. eakID: eakID,
  1456. db: &certdb.MockNoSQLDB{
  1457. MGet: func(bucket, key []byte) ([]byte, error) {
  1458. assert.Equals(t, bucket, externalAccountKeyIDsByProvisionerIDTable)
  1459. assert.Equals(t, string(key), provID)
  1460. return nil, nosqldb.ErrNotFound
  1461. },
  1462. MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
  1463. assert.Equals(t, bucket, externalAccountKeyIDsByProvisionerIDTable)
  1464. assert.Equals(t, string(key), provID)
  1465. assert.Equals(t, old, nil)
  1466. b, _ := json.Marshal([]string{})
  1467. assert.Equals(t, nu, b)
  1468. return b, true, nil
  1469. },
  1470. },
  1471. err: nil,
  1472. }
  1473. },
  1474. "ok": func(t *testing.T) test {
  1475. return test{
  1476. ctx: context.Background(),
  1477. provisionerID: provID,
  1478. eakID: eakID,
  1479. db: &certdb.MockNoSQLDB{
  1480. MGet: func(bucket, key []byte) ([]byte, error) {
  1481. assert.Equals(t, bucket, externalAccountKeyIDsByProvisionerIDTable)
  1482. assert.Equals(t, string(key), provID)
  1483. b, _ := json.Marshal([]string{"id1", eakID, "id2"})
  1484. return b, nil
  1485. },
  1486. MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
  1487. assert.Equals(t, bucket, externalAccountKeyIDsByProvisionerIDTable)
  1488. assert.Equals(t, string(key), provID)
  1489. oldB, _ := json.Marshal([]string{"id1", eakID, "id2"})
  1490. assert.Equals(t, old, oldB)
  1491. newB, _ := json.Marshal([]string{"id1", "id2"})
  1492. assert.Equals(t, nu, newB)
  1493. return newB, true, nil
  1494. },
  1495. },
  1496. err: nil,
  1497. }
  1498. },
  1499. }
  1500. for name, run := range tests {
  1501. tc := run(t)
  1502. t.Run(name, func(t *testing.T) {
  1503. db := &DB{
  1504. db: tc.db,
  1505. }
  1506. wantErr := tc.err != nil
  1507. err := db.deleteEAKID(tc.ctx, tc.provisionerID, tc.eakID)
  1508. if (err != nil) != wantErr {
  1509. t.Errorf("DB.deleteEAKID() error = %v, wantErr %v", err, wantErr)
  1510. }
  1511. if err != nil {
  1512. assert.Equals(t, tc.err.Error(), err.Error())
  1513. }
  1514. })
  1515. }
  1516. }
  1517. func TestDB_addAndDeleteEAKID(t *testing.T) {
  1518. provID := "provID"
  1519. callCounter := 0
  1520. type test struct {
  1521. ctx context.Context
  1522. db nosql.DB
  1523. err error
  1524. }
  1525. var tests = map[string]func(t *testing.T) test{
  1526. "ok/multi": func(t *testing.T) test {
  1527. return test{
  1528. ctx: context.Background(),
  1529. db: &certdb.MockNoSQLDB{
  1530. MGet: func(bucket, key []byte) ([]byte, error) {
  1531. assert.Equals(t, bucket, externalAccountKeyIDsByProvisionerIDTable)
  1532. assert.Equals(t, string(key), provID)
  1533. switch callCounter {
  1534. case 0:
  1535. return nil, nosqldb.ErrNotFound
  1536. case 1:
  1537. b, _ := json.Marshal([]string{"eakID"})
  1538. return b, nil
  1539. case 2:
  1540. b, _ := json.Marshal([]string{})
  1541. return b, nil
  1542. case 3:
  1543. b, _ := json.Marshal([]string{"eakID1"})
  1544. return b, nil
  1545. case 4:
  1546. b, _ := json.Marshal([]string{"eakID1", "eakID2"})
  1547. return b, nil
  1548. case 5:
  1549. b, _ := json.Marshal([]string{"eakID2"})
  1550. return b, nil
  1551. default:
  1552. assert.FatalError(t, errors.New("unexpected get iteration"))
  1553. return nil, errors.New("force get default")
  1554. }
  1555. },
  1556. MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
  1557. assert.Equals(t, bucket, externalAccountKeyIDsByProvisionerIDTable)
  1558. assert.Equals(t, string(key), provID)
  1559. switch callCounter {
  1560. case 0:
  1561. assert.Equals(t, old, nil)
  1562. newB, _ := json.Marshal([]string{"eakID"})
  1563. assert.Equals(t, nu, newB)
  1564. return newB, true, nil
  1565. case 1:
  1566. oldB, _ := json.Marshal([]string{"eakID"})
  1567. assert.Equals(t, old, oldB)
  1568. newB, _ := json.Marshal([]string{})
  1569. return newB, true, nil
  1570. case 2:
  1571. assert.Equals(t, old, nil)
  1572. newB, _ := json.Marshal([]string{"eakID1"})
  1573. assert.Equals(t, nu, newB)
  1574. return newB, true, nil
  1575. case 3:
  1576. oldB, _ := json.Marshal([]string{"eakID1"})
  1577. assert.Equals(t, old, oldB)
  1578. newB, _ := json.Marshal([]string{"eakID1", "eakID2"})
  1579. assert.Equals(t, nu, newB)
  1580. return newB, true, nil
  1581. case 4:
  1582. oldB, _ := json.Marshal([]string{"eakID1", "eakID2"})
  1583. assert.Equals(t, old, oldB)
  1584. newB, _ := json.Marshal([]string{"eakID2"})
  1585. assert.Equals(t, nu, newB)
  1586. return newB, true, nil
  1587. case 5:
  1588. oldB, _ := json.Marshal([]string{"eakID2"})
  1589. assert.Equals(t, old, oldB)
  1590. newB, _ := json.Marshal([]string{})
  1591. assert.Equals(t, nu, newB)
  1592. return newB, true, nil
  1593. default:
  1594. assert.FatalError(t, errors.New("unexpected get iteration"))
  1595. return nil, true, errors.New("force save default")
  1596. }
  1597. },
  1598. },
  1599. err: nil,
  1600. }
  1601. },
  1602. }
  1603. for name, run := range tests {
  1604. tc := run(t)
  1605. t.Run(name, func(t *testing.T) {
  1606. // goal of this test is to simulate multiple calls; no errors expected.
  1607. db := &DB{
  1608. db: tc.db,
  1609. }
  1610. err := db.addEAKID(tc.ctx, provID, "eakID")
  1611. if err != nil {
  1612. t.Errorf("DB.addEAKID() error = %v", err)
  1613. }
  1614. callCounter++
  1615. err = db.deleteEAKID(tc.ctx, provID, "eakID")
  1616. if err != nil {
  1617. t.Errorf("DB.deleteEAKID() error = %v", err)
  1618. }
  1619. callCounter++
  1620. err = db.addEAKID(tc.ctx, provID, "eakID1")
  1621. if err != nil {
  1622. t.Errorf("DB.addEAKID() error = %v", err)
  1623. }
  1624. callCounter++
  1625. err = db.addEAKID(tc.ctx, provID, "eakID2")
  1626. if err != nil {
  1627. t.Errorf("DB.addEAKID() error = %v", err)
  1628. }
  1629. callCounter++
  1630. err = db.deleteEAKID(tc.ctx, provID, "eakID1")
  1631. if err != nil {
  1632. t.Errorf("DB.deleteEAKID() error = %v", err)
  1633. }
  1634. callCounter++
  1635. err = db.deleteEAKID(tc.ctx, provID, "eakID2")
  1636. if err != nil {
  1637. t.Errorf("DB.deleteAKID() error = %v", err)
  1638. }
  1639. })
  1640. }
  1641. }
  1642. func Test_removeElement(t *testing.T) {
  1643. tests := []struct {
  1644. name string
  1645. slice []string
  1646. item string
  1647. want []string
  1648. }{
  1649. {
  1650. name: "remove-first",
  1651. slice: []string{"id1", "id2", "id3"},
  1652. item: "id1",
  1653. want: []string{"id2", "id3"},
  1654. },
  1655. {
  1656. name: "remove-last",
  1657. slice: []string{"id1", "id2", "id3"},
  1658. item: "id3",
  1659. want: []string{"id1", "id2"},
  1660. },
  1661. {
  1662. name: "remove-middle",
  1663. slice: []string{"id1", "id2", "id3"},
  1664. item: "id2",
  1665. want: []string{"id1", "id3"},
  1666. },
  1667. {
  1668. name: "remove-non-existing",
  1669. slice: []string{"id1", "id2", "id3"},
  1670. item: "none",
  1671. want: []string{"id1", "id2", "id3"},
  1672. },
  1673. }
  1674. for _, tt := range tests {
  1675. t.Run(tt.name, func(t *testing.T) {
  1676. got := removeElement(tt.slice, tt.item)
  1677. if !cmp.Equal(tt.want, got) {
  1678. t.Errorf("removeElement() diff =\n %s", cmp.Diff(tt.want, got))
  1679. }
  1680. })
  1681. }
  1682. }