policy_test.go 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  1. package nosql
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "reflect"
  7. "testing"
  8. "github.com/smallstep/assert"
  9. "github.com/smallstep/certificates/authority/admin"
  10. "github.com/smallstep/certificates/db"
  11. "github.com/smallstep/linkedca"
  12. "github.com/smallstep/nosql"
  13. nosqldb "github.com/smallstep/nosql/database"
  14. )
  15. func TestDB_getDBAuthorityPolicyBytes(t *testing.T) {
  16. authID := "authID"
  17. type test struct {
  18. ctx context.Context
  19. authorityID string
  20. db nosql.DB
  21. err error
  22. adminErr *admin.Error
  23. }
  24. var tests = map[string]func(t *testing.T) test{
  25. "fail/not-found": func(t *testing.T) test {
  26. return test{
  27. ctx: context.Background(),
  28. authorityID: authID,
  29. db: &db.MockNoSQLDB{
  30. MGet: func(bucket, key []byte) ([]byte, error) {
  31. assert.Equals(t, bucket, authorityPoliciesTable)
  32. assert.Equals(t, string(key), authID)
  33. return nil, nosqldb.ErrNotFound
  34. },
  35. },
  36. adminErr: admin.NewError(admin.ErrorNotFoundType, "authority policy not found"),
  37. }
  38. },
  39. "fail/db.Get-error": func(t *testing.T) test {
  40. return test{
  41. ctx: context.Background(),
  42. authorityID: authID,
  43. db: &db.MockNoSQLDB{
  44. MGet: func(bucket, key []byte) ([]byte, error) {
  45. assert.Equals(t, bucket, authorityPoliciesTable)
  46. assert.Equals(t, string(key), authID)
  47. return nil, errors.New("force")
  48. },
  49. },
  50. err: errors.New("error loading authority policy: force"),
  51. }
  52. },
  53. "ok": func(t *testing.T) test {
  54. return test{
  55. ctx: context.Background(),
  56. authorityID: authID,
  57. db: &db.MockNoSQLDB{
  58. MGet: func(bucket, key []byte) ([]byte, error) {
  59. assert.Equals(t, bucket, authorityPoliciesTable)
  60. assert.Equals(t, string(key), authID)
  61. return []byte("foo"), nil
  62. },
  63. },
  64. }
  65. },
  66. }
  67. for name, run := range tests {
  68. tc := run(t)
  69. t.Run(name, func(t *testing.T) {
  70. d := DB{db: tc.db}
  71. if b, err := d.getDBAuthorityPolicyBytes(tc.ctx, tc.authorityID); err != nil {
  72. var ae *admin.Error
  73. if errors.As(err, &ae) {
  74. if assert.NotNil(t, tc.adminErr) {
  75. assert.Equals(t, ae.Type, tc.adminErr.Type)
  76. assert.Equals(t, ae.Detail, tc.adminErr.Detail)
  77. assert.Equals(t, ae.Status, tc.adminErr.Status)
  78. assert.Equals(t, ae.Err.Error(), tc.adminErr.Err.Error())
  79. assert.Equals(t, ae.Detail, tc.adminErr.Detail)
  80. }
  81. } else {
  82. if assert.NotNil(t, tc.err) {
  83. assert.HasPrefix(t, err.Error(), tc.err.Error())
  84. }
  85. }
  86. } else if assert.Nil(t, tc.err) && assert.Nil(t, tc.adminErr) {
  87. assert.Equals(t, string(b), "foo")
  88. }
  89. })
  90. }
  91. }
  92. func TestDB_getDBAuthorityPolicy(t *testing.T) {
  93. authID := "authID"
  94. type test struct {
  95. ctx context.Context
  96. authorityID string
  97. db nosql.DB
  98. err error
  99. adminErr *admin.Error
  100. dbap *dbAuthorityPolicy
  101. }
  102. var tests = map[string]func(t *testing.T) test{
  103. "fail/not-found": func(t *testing.T) test {
  104. return test{
  105. ctx: context.Background(),
  106. authorityID: authID,
  107. db: &db.MockNoSQLDB{
  108. MGet: func(bucket, key []byte) ([]byte, error) {
  109. assert.Equals(t, bucket, authorityPoliciesTable)
  110. assert.Equals(t, string(key), authID)
  111. return nil, nosqldb.ErrNotFound
  112. },
  113. },
  114. adminErr: admin.NewError(admin.ErrorNotFoundType, "authority policy not found"),
  115. }
  116. },
  117. "fail/unmarshal-error": func(t *testing.T) test {
  118. return test{
  119. ctx: context.Background(),
  120. authorityID: authID,
  121. db: &db.MockNoSQLDB{
  122. MGet: func(bucket, key []byte) ([]byte, error) {
  123. assert.Equals(t, bucket, authorityPoliciesTable)
  124. assert.Equals(t, string(key), authID)
  125. return []byte("foo"), nil
  126. },
  127. },
  128. err: errors.New("error unmarshaling policy bytes into dbAuthorityPolicy"),
  129. }
  130. },
  131. "fail/authorityID-error": func(t *testing.T) test {
  132. dbp := &dbAuthorityPolicy{
  133. ID: "ID",
  134. AuthorityID: "diffAuthID",
  135. Policy: linkedToDB(&linkedca.Policy{
  136. X509: &linkedca.X509Policy{
  137. Allow: &linkedca.X509Names{
  138. Dns: []string{"*.local"},
  139. },
  140. },
  141. }),
  142. }
  143. b, err := json.Marshal(dbp)
  144. assert.FatalError(t, err)
  145. return test{
  146. ctx: context.Background(),
  147. authorityID: authID,
  148. db: &db.MockNoSQLDB{
  149. MGet: func(bucket, key []byte) ([]byte, error) {
  150. assert.Equals(t, bucket, authorityPoliciesTable)
  151. assert.Equals(t, string(key), authID)
  152. return b, nil
  153. },
  154. },
  155. adminErr: admin.NewError(admin.ErrorAuthorityMismatchType,
  156. "authority policy is not owned by authority authID"),
  157. }
  158. },
  159. "ok/empty-bytes": func(t *testing.T) test {
  160. return test{
  161. ctx: context.Background(),
  162. authorityID: authID,
  163. db: &db.MockNoSQLDB{
  164. MGet: func(bucket, key []byte) ([]byte, error) {
  165. assert.Equals(t, bucket, authorityPoliciesTable)
  166. assert.Equals(t, string(key), authID)
  167. return []byte{}, nil
  168. },
  169. },
  170. }
  171. },
  172. "ok": func(t *testing.T) test {
  173. dbap := &dbAuthorityPolicy{
  174. ID: "ID",
  175. AuthorityID: authID,
  176. Policy: linkedToDB(&linkedca.Policy{
  177. X509: &linkedca.X509Policy{
  178. Allow: &linkedca.X509Names{
  179. Dns: []string{"*.local"},
  180. },
  181. },
  182. }),
  183. }
  184. b, err := json.Marshal(dbap)
  185. assert.FatalError(t, err)
  186. return test{
  187. ctx: context.Background(),
  188. authorityID: authID,
  189. db: &db.MockNoSQLDB{
  190. MGet: func(bucket, key []byte) ([]byte, error) {
  191. assert.Equals(t, bucket, authorityPoliciesTable)
  192. assert.Equals(t, string(key), authID)
  193. return b, nil
  194. },
  195. },
  196. dbap: dbap,
  197. }
  198. },
  199. }
  200. for name, run := range tests {
  201. tc := run(t)
  202. t.Run(name, func(t *testing.T) {
  203. d := DB{db: tc.db, authorityID: admin.DefaultAuthorityID}
  204. dbp, err := d.getDBAuthorityPolicy(tc.ctx, tc.authorityID)
  205. switch {
  206. case err != nil:
  207. var ae *admin.Error
  208. if errors.As(err, &ae) {
  209. if assert.NotNil(t, tc.adminErr) {
  210. assert.Equals(t, ae.Type, tc.adminErr.Type)
  211. assert.Equals(t, ae.Detail, tc.adminErr.Detail)
  212. assert.Equals(t, ae.Status, tc.adminErr.Status)
  213. assert.Equals(t, ae.Err.Error(), tc.adminErr.Err.Error())
  214. assert.Equals(t, ae.Detail, tc.adminErr.Detail)
  215. }
  216. } else {
  217. if assert.NotNil(t, tc.err) {
  218. assert.HasPrefix(t, err.Error(), tc.err.Error())
  219. }
  220. }
  221. case assert.Nil(t, tc.err) && assert.Nil(t, tc.adminErr) && tc.dbap == nil:
  222. assert.Nil(t, dbp)
  223. case assert.Nil(t, tc.err) && assert.Nil(t, tc.adminErr):
  224. assert.Equals(t, dbp.ID, "ID")
  225. assert.Equals(t, dbp.AuthorityID, tc.dbap.AuthorityID)
  226. assert.Equals(t, dbp.Policy, tc.dbap.Policy)
  227. }
  228. })
  229. }
  230. }
  231. func TestDB_CreateAuthorityPolicy(t *testing.T) {
  232. authID := "authID"
  233. type test struct {
  234. ctx context.Context
  235. authorityID string
  236. policy *linkedca.Policy
  237. db nosql.DB
  238. err error
  239. adminErr *admin.Error
  240. }
  241. var tests = map[string]func(t *testing.T) test{
  242. "fail/save-error": func(t *testing.T) test {
  243. policy := &linkedca.Policy{
  244. X509: &linkedca.X509Policy{
  245. Allow: &linkedca.X509Names{
  246. Dns: []string{"*.local"},
  247. },
  248. },
  249. }
  250. return test{
  251. ctx: context.Background(),
  252. authorityID: authID,
  253. policy: policy,
  254. db: &db.MockNoSQLDB{
  255. MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
  256. assert.Equals(t, bucket, authorityPoliciesTable)
  257. assert.Equals(t, string(key), authID)
  258. var _dbap = new(dbAuthorityPolicy)
  259. assert.FatalError(t, json.Unmarshal(nu, _dbap))
  260. assert.Equals(t, _dbap.ID, authID)
  261. assert.Equals(t, _dbap.AuthorityID, authID)
  262. assert.Equals(t, _dbap.Policy, linkedToDB(policy))
  263. return nil, false, errors.New("force")
  264. },
  265. },
  266. adminErr: admin.NewErrorISE("error creating authority policy: error saving authority authority_policy: force"),
  267. }
  268. },
  269. "ok": func(t *testing.T) test {
  270. policy := &linkedca.Policy{
  271. X509: &linkedca.X509Policy{
  272. Allow: &linkedca.X509Names{
  273. Dns: []string{"*.local"},
  274. },
  275. },
  276. }
  277. return test{
  278. ctx: context.Background(),
  279. authorityID: authID,
  280. policy: policy,
  281. db: &db.MockNoSQLDB{
  282. MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
  283. assert.Equals(t, bucket, authorityPoliciesTable)
  284. assert.Equals(t, old, nil)
  285. var _dbap = new(dbAuthorityPolicy)
  286. assert.FatalError(t, json.Unmarshal(nu, _dbap))
  287. assert.Equals(t, _dbap.ID, authID)
  288. assert.Equals(t, _dbap.AuthorityID, authID)
  289. assert.Equals(t, _dbap.Policy, linkedToDB(policy))
  290. return nil, true, nil
  291. },
  292. },
  293. }
  294. },
  295. }
  296. for name, run := range tests {
  297. tc := run(t)
  298. t.Run(name, func(t *testing.T) {
  299. d := DB{db: tc.db, authorityID: tc.authorityID}
  300. if err := d.CreateAuthorityPolicy(tc.ctx, tc.policy); err != nil {
  301. var ae *admin.Error
  302. if errors.As(err, &ae) {
  303. if assert.NotNil(t, tc.adminErr) {
  304. assert.Equals(t, ae.Type, tc.adminErr.Type)
  305. assert.Equals(t, ae.Detail, tc.adminErr.Detail)
  306. assert.Equals(t, ae.Status, tc.adminErr.Status)
  307. assert.Equals(t, ae.Err.Error(), tc.adminErr.Err.Error())
  308. assert.Equals(t, ae.Detail, tc.adminErr.Detail)
  309. }
  310. } else {
  311. if assert.NotNil(t, tc.err) {
  312. assert.HasPrefix(t, err.Error(), tc.err.Error())
  313. }
  314. }
  315. }
  316. })
  317. }
  318. }
  319. func TestDB_GetAuthorityPolicy(t *testing.T) {
  320. authID := "authID"
  321. type test struct {
  322. ctx context.Context
  323. authorityID string
  324. policy *linkedca.Policy
  325. db nosql.DB
  326. err error
  327. adminErr *admin.Error
  328. }
  329. var tests = map[string]func(t *testing.T) test{
  330. "fail/not-found": func(t *testing.T) test {
  331. return test{
  332. ctx: context.Background(),
  333. authorityID: authID,
  334. db: &db.MockNoSQLDB{
  335. MGet: func(bucket, key []byte) ([]byte, error) {
  336. assert.Equals(t, bucket, authorityPoliciesTable)
  337. assert.Equals(t, string(key), authID)
  338. return nil, nosqldb.ErrNotFound
  339. },
  340. },
  341. adminErr: admin.NewError(admin.ErrorNotFoundType, "authority policy not found"),
  342. }
  343. },
  344. "fail/db.Get-error": func(t *testing.T) test {
  345. return test{
  346. ctx: context.Background(),
  347. authorityID: authID,
  348. db: &db.MockNoSQLDB{
  349. MGet: func(bucket, key []byte) ([]byte, error) {
  350. assert.Equals(t, bucket, authorityPoliciesTable)
  351. assert.Equals(t, string(key), authID)
  352. return nil, errors.New("force")
  353. },
  354. },
  355. err: errors.New("error loading authority policy: force"),
  356. }
  357. },
  358. "ok": func(t *testing.T) test {
  359. policy := &linkedca.Policy{
  360. X509: &linkedca.X509Policy{
  361. Allow: &linkedca.X509Names{
  362. Dns: []string{"*.local"},
  363. },
  364. },
  365. }
  366. return test{
  367. ctx: context.Background(),
  368. authorityID: authID,
  369. policy: policy,
  370. db: &db.MockNoSQLDB{
  371. MGet: func(bucket, key []byte) ([]byte, error) {
  372. assert.Equals(t, bucket, authorityPoliciesTable)
  373. assert.Equals(t, string(key), authID)
  374. dbap := &dbAuthorityPolicy{
  375. ID: authID,
  376. AuthorityID: authID,
  377. Policy: linkedToDB(policy),
  378. }
  379. b, err := json.Marshal(dbap)
  380. assert.FatalError(t, err)
  381. return b, nil
  382. },
  383. },
  384. }
  385. },
  386. }
  387. for name, run := range tests {
  388. tc := run(t)
  389. t.Run(name, func(t *testing.T) {
  390. d := DB{db: tc.db, authorityID: tc.authorityID}
  391. got, err := d.GetAuthorityPolicy(tc.ctx)
  392. if err != nil {
  393. var ae *admin.Error
  394. if errors.As(err, &ae) {
  395. if assert.NotNil(t, tc.adminErr) {
  396. assert.Equals(t, ae.Type, tc.adminErr.Type)
  397. assert.Equals(t, ae.Detail, tc.adminErr.Detail)
  398. assert.Equals(t, ae.Status, tc.adminErr.Status)
  399. assert.Equals(t, ae.Err.Error(), tc.adminErr.Err.Error())
  400. assert.Equals(t, ae.Detail, tc.adminErr.Detail)
  401. }
  402. } else {
  403. if assert.NotNil(t, tc.err) {
  404. assert.HasPrefix(t, err.Error(), tc.err.Error())
  405. }
  406. }
  407. return
  408. }
  409. assert.NotNil(t, got)
  410. assert.Equals(t, tc.policy, got)
  411. })
  412. }
  413. }
  414. func TestDB_UpdateAuthorityPolicy(t *testing.T) {
  415. authID := "authID"
  416. type test struct {
  417. ctx context.Context
  418. authorityID string
  419. policy *linkedca.Policy
  420. db nosql.DB
  421. err error
  422. adminErr *admin.Error
  423. }
  424. var tests = map[string]func(t *testing.T) test{
  425. "fail/not-found": func(t *testing.T) test {
  426. return test{
  427. ctx: context.Background(),
  428. authorityID: authID,
  429. db: &db.MockNoSQLDB{
  430. MGet: func(bucket, key []byte) ([]byte, error) {
  431. assert.Equals(t, bucket, authorityPoliciesTable)
  432. assert.Equals(t, string(key), authID)
  433. return nil, nosqldb.ErrNotFound
  434. },
  435. },
  436. adminErr: admin.NewError(admin.ErrorNotFoundType, "authority policy not found"),
  437. }
  438. },
  439. "fail/db.Get-error": func(t *testing.T) test {
  440. return test{
  441. ctx: context.Background(),
  442. authorityID: authID,
  443. db: &db.MockNoSQLDB{
  444. MGet: func(bucket, key []byte) ([]byte, error) {
  445. assert.Equals(t, bucket, authorityPoliciesTable)
  446. assert.Equals(t, string(key), authID)
  447. return nil, errors.New("force")
  448. },
  449. },
  450. err: errors.New("error loading authority policy: force"),
  451. }
  452. },
  453. "fail/save-error": func(t *testing.T) test {
  454. oldPolicy := &linkedca.Policy{
  455. X509: &linkedca.X509Policy{
  456. Allow: &linkedca.X509Names{
  457. Dns: []string{"*.localhost"},
  458. },
  459. },
  460. }
  461. policy := &linkedca.Policy{
  462. X509: &linkedca.X509Policy{
  463. Allow: &linkedca.X509Names{
  464. Dns: []string{"*.local"},
  465. },
  466. },
  467. }
  468. return test{
  469. ctx: context.Background(),
  470. authorityID: authID,
  471. policy: policy,
  472. db: &db.MockNoSQLDB{
  473. MGet: func(bucket, key []byte) ([]byte, error) {
  474. assert.Equals(t, bucket, authorityPoliciesTable)
  475. assert.Equals(t, string(key), authID)
  476. dbap := &dbAuthorityPolicy{
  477. ID: authID,
  478. AuthorityID: authID,
  479. Policy: linkedToDB(oldPolicy),
  480. }
  481. b, err := json.Marshal(dbap)
  482. assert.FatalError(t, err)
  483. return b, nil
  484. },
  485. MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
  486. assert.Equals(t, bucket, authorityPoliciesTable)
  487. assert.Equals(t, string(key), authID)
  488. var _dbap = new(dbAuthorityPolicy)
  489. assert.FatalError(t, json.Unmarshal(nu, _dbap))
  490. assert.Equals(t, _dbap.ID, authID)
  491. assert.Equals(t, _dbap.AuthorityID, authID)
  492. assert.Equals(t, _dbap.Policy, linkedToDB(policy))
  493. return nil, false, errors.New("force")
  494. },
  495. },
  496. adminErr: admin.NewErrorISE("error updating authority policy: error saving authority authority_policy: force"),
  497. }
  498. },
  499. "ok": func(t *testing.T) test {
  500. oldPolicy := &linkedca.Policy{
  501. X509: &linkedca.X509Policy{
  502. Allow: &linkedca.X509Names{
  503. Dns: []string{"*.localhost"},
  504. },
  505. },
  506. }
  507. policy := &linkedca.Policy{
  508. X509: &linkedca.X509Policy{
  509. Allow: &linkedca.X509Names{
  510. Dns: []string{"*.local"},
  511. },
  512. },
  513. }
  514. return test{
  515. ctx: context.Background(),
  516. authorityID: authID,
  517. policy: policy,
  518. db: &db.MockNoSQLDB{
  519. MGet: func(bucket, key []byte) ([]byte, error) {
  520. assert.Equals(t, bucket, authorityPoliciesTable)
  521. assert.Equals(t, string(key), authID)
  522. dbap := &dbAuthorityPolicy{
  523. ID: authID,
  524. AuthorityID: authID,
  525. Policy: linkedToDB(oldPolicy),
  526. }
  527. b, err := json.Marshal(dbap)
  528. assert.FatalError(t, err)
  529. return b, nil
  530. },
  531. MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
  532. assert.Equals(t, bucket, authorityPoliciesTable)
  533. assert.Equals(t, string(key), authID)
  534. var _dbap = new(dbAuthorityPolicy)
  535. assert.FatalError(t, json.Unmarshal(nu, _dbap))
  536. assert.Equals(t, _dbap.ID, authID)
  537. assert.Equals(t, _dbap.AuthorityID, authID)
  538. assert.Equals(t, _dbap.Policy, linkedToDB(policy))
  539. return nil, true, nil
  540. },
  541. },
  542. }
  543. },
  544. }
  545. for name, run := range tests {
  546. tc := run(t)
  547. t.Run(name, func(t *testing.T) {
  548. d := DB{db: tc.db, authorityID: tc.authorityID}
  549. if err := d.UpdateAuthorityPolicy(tc.ctx, tc.policy); err != nil {
  550. var ae *admin.Error
  551. if errors.As(err, &ae) {
  552. if assert.NotNil(t, tc.adminErr) {
  553. assert.Equals(t, ae.Type, tc.adminErr.Type)
  554. assert.Equals(t, ae.Detail, tc.adminErr.Detail)
  555. assert.Equals(t, ae.Status, tc.adminErr.Status)
  556. assert.Equals(t, ae.Err.Error(), tc.adminErr.Err.Error())
  557. assert.Equals(t, ae.Detail, tc.adminErr.Detail)
  558. }
  559. } else {
  560. if assert.NotNil(t, tc.err) {
  561. assert.HasPrefix(t, err.Error(), tc.err.Error())
  562. }
  563. }
  564. return
  565. }
  566. })
  567. }
  568. }
  569. func TestDB_DeleteAuthorityPolicy(t *testing.T) {
  570. authID := "authID"
  571. type test struct {
  572. ctx context.Context
  573. authorityID string
  574. db nosql.DB
  575. err error
  576. adminErr *admin.Error
  577. }
  578. var tests = map[string]func(t *testing.T) test{
  579. "fail/not-found": func(t *testing.T) test {
  580. return test{
  581. ctx: context.Background(),
  582. authorityID: authID,
  583. db: &db.MockNoSQLDB{
  584. MGet: func(bucket, key []byte) ([]byte, error) {
  585. assert.Equals(t, bucket, authorityPoliciesTable)
  586. assert.Equals(t, string(key), authID)
  587. return nil, nosqldb.ErrNotFound
  588. },
  589. },
  590. adminErr: admin.NewError(admin.ErrorNotFoundType, "authority policy not found"),
  591. }
  592. },
  593. "fail/db.Get-error": func(t *testing.T) test {
  594. return test{
  595. ctx: context.Background(),
  596. authorityID: authID,
  597. db: &db.MockNoSQLDB{
  598. MGet: func(bucket, key []byte) ([]byte, error) {
  599. assert.Equals(t, bucket, authorityPoliciesTable)
  600. assert.Equals(t, string(key), authID)
  601. return nil, errors.New("force")
  602. },
  603. },
  604. err: errors.New("error loading authority policy: force"),
  605. }
  606. },
  607. "fail/save-error": func(t *testing.T) test {
  608. oldPolicy := &linkedca.Policy{
  609. X509: &linkedca.X509Policy{
  610. Allow: &linkedca.X509Names{
  611. Dns: []string{"*.localhost"},
  612. },
  613. },
  614. }
  615. return test{
  616. ctx: context.Background(),
  617. authorityID: authID,
  618. db: &db.MockNoSQLDB{
  619. MGet: func(bucket, key []byte) ([]byte, error) {
  620. assert.Equals(t, bucket, authorityPoliciesTable)
  621. assert.Equals(t, string(key), authID)
  622. dbap := &dbAuthorityPolicy{
  623. ID: authID,
  624. AuthorityID: authID,
  625. Policy: linkedToDB(oldPolicy),
  626. }
  627. b, err := json.Marshal(dbap)
  628. assert.FatalError(t, err)
  629. return b, nil
  630. },
  631. MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
  632. assert.Equals(t, bucket, authorityPoliciesTable)
  633. assert.Equals(t, string(key), authID)
  634. assert.Equals(t, nil, nu)
  635. return nil, false, errors.New("force")
  636. },
  637. },
  638. adminErr: admin.NewErrorISE("error deleting authority policy: error saving authority authority_policy: force"),
  639. }
  640. },
  641. "ok": func(t *testing.T) test {
  642. oldPolicy := &linkedca.Policy{
  643. X509: &linkedca.X509Policy{
  644. Allow: &linkedca.X509Names{
  645. Dns: []string{"*.localhost"},
  646. },
  647. },
  648. }
  649. return test{
  650. ctx: context.Background(),
  651. authorityID: authID,
  652. db: &db.MockNoSQLDB{
  653. MGet: func(bucket, key []byte) ([]byte, error) {
  654. assert.Equals(t, bucket, authorityPoliciesTable)
  655. assert.Equals(t, string(key), authID)
  656. dbap := &dbAuthorityPolicy{
  657. ID: authID,
  658. AuthorityID: authID,
  659. Policy: linkedToDB(oldPolicy),
  660. }
  661. b, err := json.Marshal(dbap)
  662. assert.FatalError(t, err)
  663. return b, nil
  664. },
  665. MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
  666. assert.Equals(t, bucket, authorityPoliciesTable)
  667. assert.Equals(t, string(key), authID)
  668. assert.Equals(t, nil, nu)
  669. return nil, true, nil
  670. },
  671. },
  672. }
  673. },
  674. }
  675. for name, run := range tests {
  676. tc := run(t)
  677. t.Run(name, func(t *testing.T) {
  678. d := DB{db: tc.db, authorityID: tc.authorityID}
  679. if err := d.DeleteAuthorityPolicy(tc.ctx); err != nil {
  680. var ae *admin.Error
  681. if errors.As(err, &ae) {
  682. if assert.NotNil(t, tc.adminErr) {
  683. assert.Equals(t, ae.Type, tc.adminErr.Type)
  684. assert.Equals(t, ae.Detail, tc.adminErr.Detail)
  685. assert.Equals(t, ae.Status, tc.adminErr.Status)
  686. assert.Equals(t, ae.Err.Error(), tc.adminErr.Err.Error())
  687. assert.Equals(t, ae.Detail, tc.adminErr.Detail)
  688. }
  689. } else {
  690. if assert.NotNil(t, tc.err) {
  691. assert.HasPrefix(t, err.Error(), tc.err.Error())
  692. }
  693. }
  694. return
  695. }
  696. })
  697. }
  698. }
  699. func Test_linkedToDB(t *testing.T) {
  700. type args struct {
  701. p *linkedca.Policy
  702. }
  703. tests := []struct {
  704. name string
  705. args args
  706. want *dbPolicy
  707. }{
  708. {
  709. name: "nil policy",
  710. args: args{
  711. p: nil,
  712. },
  713. want: nil,
  714. },
  715. {
  716. name: "no x509 nor ssh",
  717. args: args{
  718. p: &linkedca.Policy{},
  719. },
  720. want: nil,
  721. },
  722. {
  723. name: "x509",
  724. args: args{
  725. p: &linkedca.Policy{
  726. X509: &linkedca.X509Policy{
  727. Allow: &linkedca.X509Names{
  728. Dns: []string{"*.local"},
  729. Ips: []string{"192.168.0.1/24"},
  730. Emails: []string{"@example.com"},
  731. Uris: []string{"*.example.com"},
  732. CommonNames: []string{"some name"},
  733. },
  734. Deny: &linkedca.X509Names{
  735. Dns: []string{"badhost.local"},
  736. Ips: []string{"192.168.0.30"},
  737. Emails: []string{"root@example.com"},
  738. Uris: []string{"bad.example.com"},
  739. CommonNames: []string{"bad name"},
  740. },
  741. AllowWildcardNames: true,
  742. },
  743. },
  744. },
  745. want: &dbPolicy{
  746. X509: &dbX509Policy{
  747. Allow: &dbX509Names{
  748. DNSDomains: []string{"*.local"},
  749. IPRanges: []string{"192.168.0.1/24"},
  750. EmailAddresses: []string{"@example.com"},
  751. URIDomains: []string{"*.example.com"},
  752. CommonNames: []string{"some name"},
  753. },
  754. Deny: &dbX509Names{
  755. DNSDomains: []string{"badhost.local"},
  756. IPRanges: []string{"192.168.0.30"},
  757. EmailAddresses: []string{"root@example.com"},
  758. URIDomains: []string{"bad.example.com"},
  759. CommonNames: []string{"bad name"},
  760. },
  761. AllowWildcardNames: true,
  762. },
  763. },
  764. },
  765. {
  766. name: "ssh user",
  767. args: args{
  768. p: &linkedca.Policy{
  769. Ssh: &linkedca.SSHPolicy{
  770. User: &linkedca.SSHUserPolicy{
  771. Allow: &linkedca.SSHUserNames{
  772. Emails: []string{"@example.com"},
  773. Principals: []string{"user"},
  774. },
  775. Deny: &linkedca.SSHUserNames{
  776. Emails: []string{"root@example.com"},
  777. Principals: []string{"root"},
  778. },
  779. },
  780. },
  781. },
  782. },
  783. want: &dbPolicy{
  784. SSH: &dbSSHPolicy{
  785. User: &dbSSHUserPolicy{
  786. Allow: &dbSSHUserNames{
  787. EmailAddresses: []string{"@example.com"},
  788. Principals: []string{"user"},
  789. },
  790. Deny: &dbSSHUserNames{
  791. EmailAddresses: []string{"root@example.com"},
  792. Principals: []string{"root"},
  793. },
  794. },
  795. },
  796. },
  797. },
  798. {
  799. name: "full ssh policy",
  800. args: args{
  801. p: &linkedca.Policy{
  802. Ssh: &linkedca.SSHPolicy{
  803. Host: &linkedca.SSHHostPolicy{
  804. Allow: &linkedca.SSHHostNames{
  805. Dns: []string{"*.local"},
  806. Ips: []string{"192.168.0.1/24"},
  807. Principals: []string{"host"},
  808. },
  809. Deny: &linkedca.SSHHostNames{
  810. Dns: []string{"badhost.local"},
  811. Ips: []string{"192.168.0.30"},
  812. Principals: []string{"bad"},
  813. },
  814. },
  815. },
  816. },
  817. },
  818. want: &dbPolicy{
  819. SSH: &dbSSHPolicy{
  820. Host: &dbSSHHostPolicy{
  821. Allow: &dbSSHHostNames{
  822. DNSDomains: []string{"*.local"},
  823. IPRanges: []string{"192.168.0.1/24"},
  824. Principals: []string{"host"},
  825. },
  826. Deny: &dbSSHHostNames{
  827. DNSDomains: []string{"badhost.local"},
  828. IPRanges: []string{"192.168.0.30"},
  829. Principals: []string{"bad"},
  830. },
  831. },
  832. },
  833. },
  834. },
  835. {
  836. name: "full policy",
  837. args: args{
  838. p: &linkedca.Policy{
  839. X509: &linkedca.X509Policy{
  840. Allow: &linkedca.X509Names{
  841. Dns: []string{"*.local"},
  842. Ips: []string{"192.168.0.1/24"},
  843. Emails: []string{"@example.com"},
  844. Uris: []string{"*.example.com"},
  845. CommonNames: []string{"some name"},
  846. },
  847. Deny: &linkedca.X509Names{
  848. Dns: []string{"badhost.local"},
  849. Ips: []string{"192.168.0.30"},
  850. Emails: []string{"root@example.com"},
  851. Uris: []string{"bad.example.com"},
  852. CommonNames: []string{"bad name"},
  853. },
  854. AllowWildcardNames: true,
  855. },
  856. Ssh: &linkedca.SSHPolicy{
  857. User: &linkedca.SSHUserPolicy{
  858. Allow: &linkedca.SSHUserNames{
  859. Emails: []string{"@example.com"},
  860. Principals: []string{"user"},
  861. },
  862. Deny: &linkedca.SSHUserNames{
  863. Emails: []string{"root@example.com"},
  864. Principals: []string{"root"},
  865. },
  866. },
  867. Host: &linkedca.SSHHostPolicy{
  868. Allow: &linkedca.SSHHostNames{
  869. Dns: []string{"*.local"},
  870. Ips: []string{"192.168.0.1/24"},
  871. Principals: []string{"host"},
  872. },
  873. Deny: &linkedca.SSHHostNames{
  874. Dns: []string{"badhost.local"},
  875. Ips: []string{"192.168.0.30"},
  876. Principals: []string{"bad"},
  877. },
  878. },
  879. },
  880. },
  881. },
  882. want: &dbPolicy{
  883. X509: &dbX509Policy{
  884. Allow: &dbX509Names{
  885. DNSDomains: []string{"*.local"},
  886. IPRanges: []string{"192.168.0.1/24"},
  887. EmailAddresses: []string{"@example.com"},
  888. URIDomains: []string{"*.example.com"},
  889. CommonNames: []string{"some name"},
  890. },
  891. Deny: &dbX509Names{
  892. DNSDomains: []string{"badhost.local"},
  893. IPRanges: []string{"192.168.0.30"},
  894. EmailAddresses: []string{"root@example.com"},
  895. URIDomains: []string{"bad.example.com"},
  896. CommonNames: []string{"bad name"},
  897. },
  898. AllowWildcardNames: true,
  899. },
  900. SSH: &dbSSHPolicy{
  901. User: &dbSSHUserPolicy{
  902. Allow: &dbSSHUserNames{
  903. EmailAddresses: []string{"@example.com"},
  904. Principals: []string{"user"},
  905. },
  906. Deny: &dbSSHUserNames{
  907. EmailAddresses: []string{"root@example.com"},
  908. Principals: []string{"root"},
  909. },
  910. },
  911. Host: &dbSSHHostPolicy{
  912. Allow: &dbSSHHostNames{
  913. DNSDomains: []string{"*.local"},
  914. IPRanges: []string{"192.168.0.1/24"},
  915. Principals: []string{"host"},
  916. },
  917. Deny: &dbSSHHostNames{
  918. DNSDomains: []string{"badhost.local"},
  919. IPRanges: []string{"192.168.0.30"},
  920. Principals: []string{"bad"},
  921. },
  922. },
  923. },
  924. },
  925. },
  926. }
  927. for _, tt := range tests {
  928. t.Run(tt.name, func(t *testing.T) {
  929. if got := linkedToDB(tt.args.p); !reflect.DeepEqual(got, tt.want) {
  930. t.Errorf("linkedToDB() = %v, want %v", got, tt.want)
  931. }
  932. })
  933. }
  934. }
  935. func Test_dbToLinked(t *testing.T) {
  936. type args struct {
  937. p *dbPolicy
  938. }
  939. tests := []struct {
  940. name string
  941. args args
  942. want *linkedca.Policy
  943. }{
  944. {
  945. name: "nil policy",
  946. args: args{
  947. p: nil,
  948. },
  949. want: nil,
  950. },
  951. {
  952. name: "x509",
  953. args: args{
  954. p: &dbPolicy{
  955. X509: &dbX509Policy{
  956. Allow: &dbX509Names{
  957. DNSDomains: []string{"*.local"},
  958. IPRanges: []string{"192.168.0.1/24"},
  959. EmailAddresses: []string{"@example.com"},
  960. URIDomains: []string{"*.example.com"},
  961. CommonNames: []string{"some name"},
  962. },
  963. Deny: &dbX509Names{
  964. DNSDomains: []string{"badhost.local"},
  965. IPRanges: []string{"192.168.0.30"},
  966. EmailAddresses: []string{"root@example.com"},
  967. URIDomains: []string{"bad.example.com"},
  968. CommonNames: []string{"bad name"},
  969. },
  970. AllowWildcardNames: true,
  971. },
  972. },
  973. },
  974. want: &linkedca.Policy{
  975. X509: &linkedca.X509Policy{
  976. Allow: &linkedca.X509Names{
  977. Dns: []string{"*.local"},
  978. Ips: []string{"192.168.0.1/24"},
  979. Emails: []string{"@example.com"},
  980. Uris: []string{"*.example.com"},
  981. CommonNames: []string{"some name"},
  982. },
  983. Deny: &linkedca.X509Names{
  984. Dns: []string{"badhost.local"},
  985. Ips: []string{"192.168.0.30"},
  986. Emails: []string{"root@example.com"},
  987. Uris: []string{"bad.example.com"},
  988. CommonNames: []string{"bad name"},
  989. },
  990. AllowWildcardNames: true,
  991. },
  992. },
  993. },
  994. {
  995. name: "ssh user",
  996. args: args{
  997. p: &dbPolicy{
  998. SSH: &dbSSHPolicy{
  999. User: &dbSSHUserPolicy{
  1000. Allow: &dbSSHUserNames{
  1001. EmailAddresses: []string{"@example.com"},
  1002. Principals: []string{"user"},
  1003. },
  1004. Deny: &dbSSHUserNames{
  1005. EmailAddresses: []string{"root@example.com"},
  1006. Principals: []string{"root"},
  1007. },
  1008. },
  1009. },
  1010. },
  1011. },
  1012. want: &linkedca.Policy{
  1013. Ssh: &linkedca.SSHPolicy{
  1014. User: &linkedca.SSHUserPolicy{
  1015. Allow: &linkedca.SSHUserNames{
  1016. Emails: []string{"@example.com"},
  1017. Principals: []string{"user"},
  1018. },
  1019. Deny: &linkedca.SSHUserNames{
  1020. Emails: []string{"root@example.com"},
  1021. Principals: []string{"root"},
  1022. },
  1023. },
  1024. },
  1025. },
  1026. },
  1027. {
  1028. name: "ssh host",
  1029. args: args{
  1030. p: &dbPolicy{
  1031. SSH: &dbSSHPolicy{
  1032. Host: &dbSSHHostPolicy{
  1033. Allow: &dbSSHHostNames{
  1034. DNSDomains: []string{"*.local"},
  1035. IPRanges: []string{"192.168.0.1/24"},
  1036. Principals: []string{"host"},
  1037. },
  1038. Deny: &dbSSHHostNames{
  1039. DNSDomains: []string{"badhost.local"},
  1040. IPRanges: []string{"192.168.0.30"},
  1041. Principals: []string{"bad"},
  1042. },
  1043. },
  1044. },
  1045. },
  1046. },
  1047. want: &linkedca.Policy{
  1048. Ssh: &linkedca.SSHPolicy{
  1049. Host: &linkedca.SSHHostPolicy{
  1050. Allow: &linkedca.SSHHostNames{
  1051. Dns: []string{"*.local"},
  1052. Ips: []string{"192.168.0.1/24"},
  1053. Principals: []string{"host"},
  1054. },
  1055. Deny: &linkedca.SSHHostNames{
  1056. Dns: []string{"badhost.local"},
  1057. Ips: []string{"192.168.0.30"},
  1058. Principals: []string{"bad"},
  1059. },
  1060. },
  1061. },
  1062. },
  1063. },
  1064. {
  1065. name: "full policy",
  1066. args: args{
  1067. p: &dbPolicy{
  1068. X509: &dbX509Policy{
  1069. Allow: &dbX509Names{
  1070. DNSDomains: []string{"*.local"},
  1071. IPRanges: []string{"192.168.0.1/24"},
  1072. EmailAddresses: []string{"@example.com"},
  1073. URIDomains: []string{"*.example.com"},
  1074. CommonNames: []string{"some name"},
  1075. },
  1076. Deny: &dbX509Names{
  1077. DNSDomains: []string{"badhost.local"},
  1078. IPRanges: []string{"192.168.0.30"},
  1079. EmailAddresses: []string{"root@example.com"},
  1080. URIDomains: []string{"bad.example.com"},
  1081. CommonNames: []string{"bad name"},
  1082. },
  1083. AllowWildcardNames: true,
  1084. },
  1085. SSH: &dbSSHPolicy{
  1086. User: &dbSSHUserPolicy{
  1087. Allow: &dbSSHUserNames{
  1088. EmailAddresses: []string{"@example.com"},
  1089. Principals: []string{"user"},
  1090. },
  1091. Deny: &dbSSHUserNames{
  1092. EmailAddresses: []string{"root@example.com"},
  1093. Principals: []string{"root"},
  1094. },
  1095. },
  1096. Host: &dbSSHHostPolicy{
  1097. Allow: &dbSSHHostNames{
  1098. DNSDomains: []string{"*.local"},
  1099. IPRanges: []string{"192.168.0.1/24"},
  1100. Principals: []string{"host"},
  1101. },
  1102. Deny: &dbSSHHostNames{
  1103. DNSDomains: []string{"badhost.local"},
  1104. IPRanges: []string{"192.168.0.30"},
  1105. Principals: []string{"bad"},
  1106. },
  1107. },
  1108. },
  1109. },
  1110. },
  1111. want: &linkedca.Policy{
  1112. X509: &linkedca.X509Policy{
  1113. Allow: &linkedca.X509Names{
  1114. Dns: []string{"*.local"},
  1115. Ips: []string{"192.168.0.1/24"},
  1116. Emails: []string{"@example.com"},
  1117. Uris: []string{"*.example.com"},
  1118. CommonNames: []string{"some name"},
  1119. },
  1120. Deny: &linkedca.X509Names{
  1121. Dns: []string{"badhost.local"},
  1122. Ips: []string{"192.168.0.30"},
  1123. Emails: []string{"root@example.com"},
  1124. Uris: []string{"bad.example.com"},
  1125. CommonNames: []string{"bad name"},
  1126. },
  1127. AllowWildcardNames: true,
  1128. },
  1129. Ssh: &linkedca.SSHPolicy{
  1130. User: &linkedca.SSHUserPolicy{
  1131. Allow: &linkedca.SSHUserNames{
  1132. Emails: []string{"@example.com"},
  1133. Principals: []string{"user"},
  1134. },
  1135. Deny: &linkedca.SSHUserNames{
  1136. Emails: []string{"root@example.com"},
  1137. Principals: []string{"root"},
  1138. },
  1139. },
  1140. Host: &linkedca.SSHHostPolicy{
  1141. Allow: &linkedca.SSHHostNames{
  1142. Dns: []string{"*.local"},
  1143. Ips: []string{"192.168.0.1/24"},
  1144. Principals: []string{"host"},
  1145. },
  1146. Deny: &linkedca.SSHHostNames{
  1147. Dns: []string{"badhost.local"},
  1148. Ips: []string{"192.168.0.30"},
  1149. Principals: []string{"bad"},
  1150. },
  1151. },
  1152. },
  1153. },
  1154. },
  1155. }
  1156. for _, tt := range tests {
  1157. t.Run(tt.name, func(t *testing.T) {
  1158. if got := dbToLinked(tt.args.p); !reflect.DeepEqual(got, tt.want) {
  1159. t.Errorf("dbToLinked() = %v, want %v", got, tt.want)
  1160. }
  1161. })
  1162. }
  1163. }