4
0

policy_test.go 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629
  1. package authority
  2. import (
  3. "context"
  4. "errors"
  5. "reflect"
  6. "testing"
  7. "github.com/go-jose/go-jose/v3"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/smallstep/linkedca"
  10. "github.com/smallstep/certificates/authority/admin"
  11. "github.com/smallstep/certificates/authority/administrator"
  12. "github.com/smallstep/certificates/authority/config"
  13. "github.com/smallstep/certificates/authority/policy"
  14. "github.com/smallstep/certificates/authority/provisioner"
  15. "github.com/smallstep/certificates/db"
  16. )
  17. func TestAuthority_checkPolicy(t *testing.T) {
  18. type test struct {
  19. ctx context.Context
  20. currentAdmin *linkedca.Admin
  21. otherAdmins []*linkedca.Admin
  22. policy *linkedca.Policy
  23. err *PolicyError
  24. }
  25. tests := map[string]func(t *testing.T) test{
  26. "fail/NewX509PolicyEngine-error": func(t *testing.T) test {
  27. return test{
  28. ctx: context.Background(),
  29. policy: &linkedca.Policy{
  30. X509: &linkedca.X509Policy{
  31. Allow: &linkedca.X509Names{
  32. Dns: []string{"**.local"},
  33. },
  34. },
  35. },
  36. err: &PolicyError{
  37. Typ: ConfigurationFailure,
  38. Err: errors.New("cannot parse permitted domain constraint \"**.local\": domain constraint \"**.local\" can only have wildcard as starting character"),
  39. },
  40. }
  41. },
  42. "fail/currentAdmin-evaluation-error": func(t *testing.T) test {
  43. return test{
  44. ctx: context.Background(),
  45. currentAdmin: &linkedca.Admin{Subject: "*"},
  46. otherAdmins: []*linkedca.Admin{},
  47. policy: &linkedca.Policy{
  48. X509: &linkedca.X509Policy{
  49. Allow: &linkedca.X509Names{
  50. Dns: []string{"*.local"},
  51. },
  52. },
  53. },
  54. err: &PolicyError{
  55. Typ: EvaluationFailure,
  56. Err: errors.New("cannot parse dns domain \"*\""),
  57. },
  58. }
  59. },
  60. "fail/currentAdmin-lockout": func(t *testing.T) test {
  61. return test{
  62. ctx: context.Background(),
  63. currentAdmin: &linkedca.Admin{Subject: "step"},
  64. otherAdmins: []*linkedca.Admin{
  65. {
  66. Subject: "otherAdmin",
  67. },
  68. },
  69. policy: &linkedca.Policy{
  70. X509: &linkedca.X509Policy{
  71. Allow: &linkedca.X509Names{
  72. Dns: []string{"*.local"},
  73. },
  74. },
  75. },
  76. err: &PolicyError{
  77. Typ: AdminLockOut,
  78. Err: errors.New("the provided policy would lock out [step] from the CA. Please create an x509 policy to include [step] as an allowed DNS name"),
  79. },
  80. }
  81. },
  82. "fail/otherAdmins-evaluation-error": func(t *testing.T) test {
  83. return test{
  84. ctx: context.Background(),
  85. currentAdmin: &linkedca.Admin{Subject: "step"},
  86. otherAdmins: []*linkedca.Admin{
  87. {
  88. Subject: "other",
  89. },
  90. {
  91. Subject: "**",
  92. },
  93. },
  94. policy: &linkedca.Policy{
  95. X509: &linkedca.X509Policy{
  96. Allow: &linkedca.X509Names{
  97. Dns: []string{"step", "other", "*.local"},
  98. },
  99. },
  100. },
  101. err: &PolicyError{
  102. Typ: EvaluationFailure,
  103. Err: errors.New("cannot parse dns domain \"**\""),
  104. },
  105. }
  106. },
  107. "fail/otherAdmins-lockout": func(t *testing.T) test {
  108. return test{
  109. ctx: context.Background(),
  110. currentAdmin: &linkedca.Admin{Subject: "step"},
  111. otherAdmins: []*linkedca.Admin{
  112. {
  113. Subject: "otherAdmin",
  114. },
  115. },
  116. policy: &linkedca.Policy{
  117. X509: &linkedca.X509Policy{
  118. Allow: &linkedca.X509Names{
  119. Dns: []string{"step"},
  120. },
  121. },
  122. },
  123. err: &PolicyError{
  124. Typ: AdminLockOut,
  125. Err: errors.New("the provided policy would lock out [otherAdmin] from the CA. Please create an x509 policy to include [otherAdmin] as an allowed DNS name"),
  126. },
  127. }
  128. },
  129. "ok/no-policy": func(t *testing.T) test {
  130. return test{
  131. ctx: context.Background(),
  132. currentAdmin: &linkedca.Admin{Subject: "step"},
  133. otherAdmins: []*linkedca.Admin{},
  134. policy: nil,
  135. }
  136. },
  137. "ok/empty-policy": func(t *testing.T) test {
  138. return test{
  139. ctx: context.Background(),
  140. currentAdmin: &linkedca.Admin{Subject: "step"},
  141. otherAdmins: []*linkedca.Admin{},
  142. policy: &linkedca.Policy{
  143. X509: &linkedca.X509Policy{
  144. Allow: &linkedca.X509Names{
  145. Dns: []string{},
  146. },
  147. },
  148. },
  149. }
  150. },
  151. "ok/policy": func(t *testing.T) test {
  152. return test{
  153. ctx: context.Background(),
  154. currentAdmin: &linkedca.Admin{Subject: "step"},
  155. otherAdmins: []*linkedca.Admin{
  156. {
  157. Subject: "otherAdmin",
  158. },
  159. },
  160. policy: &linkedca.Policy{
  161. X509: &linkedca.X509Policy{
  162. Allow: &linkedca.X509Names{
  163. Dns: []string{"step", "otherAdmin"},
  164. },
  165. },
  166. },
  167. }
  168. },
  169. }
  170. for name, prep := range tests {
  171. tc := prep(t)
  172. t.Run(name, func(t *testing.T) {
  173. a := &Authority{}
  174. err := a.checkPolicy(tc.ctx, tc.currentAdmin, tc.otherAdmins, tc.policy)
  175. if tc.err == nil {
  176. assert.Nil(t, err)
  177. } else {
  178. assert.IsType(t, &PolicyError{}, err)
  179. var pe *PolicyError
  180. if assert.True(t, errors.As(err, &pe)) {
  181. assert.Equal(t, tc.err.Typ, pe.Typ)
  182. assert.Equal(t, tc.err.Error(), pe.Error())
  183. }
  184. }
  185. })
  186. }
  187. }
  188. func mustPolicyEngine(t *testing.T, options *policy.Options) *policy.Engine {
  189. engine, err := policy.New(options)
  190. if err != nil {
  191. t.Fatal(err)
  192. }
  193. return engine
  194. }
  195. func TestAuthority_reloadPolicyEngines(t *testing.T) {
  196. existingPolicyEngine, err := policy.New(&policy.Options{
  197. X509: &policy.X509PolicyOptions{
  198. AllowedNames: &policy.X509NameOptions{
  199. DNSDomains: []string{"*.hosts.example.com"},
  200. },
  201. },
  202. SSH: &policy.SSHPolicyOptions{
  203. Host: &policy.SSHHostCertificateOptions{
  204. AllowedNames: &policy.SSHNameOptions{
  205. DNSDomains: []string{"*.hosts.example.com"},
  206. },
  207. },
  208. User: &policy.SSHUserCertificateOptions{
  209. AllowedNames: &policy.SSHNameOptions{
  210. EmailAddresses: []string{"@mails.example.com"},
  211. },
  212. },
  213. },
  214. })
  215. assert.NoError(t, err)
  216. newX509Options := &policy.Options{
  217. X509: &policy.X509PolicyOptions{
  218. AllowedNames: &policy.X509NameOptions{
  219. DNSDomains: []string{"*.local"},
  220. },
  221. DeniedNames: &policy.X509NameOptions{
  222. DNSDomains: []string{"badhost.local"},
  223. },
  224. AllowWildcardNames: true,
  225. },
  226. }
  227. newSSHHostOptions := &policy.Options{
  228. SSH: &policy.SSHPolicyOptions{
  229. Host: &policy.SSHHostCertificateOptions{
  230. AllowedNames: &policy.SSHNameOptions{
  231. DNSDomains: []string{"*.local"},
  232. },
  233. DeniedNames: &policy.SSHNameOptions{
  234. DNSDomains: []string{"badhost.local"},
  235. },
  236. },
  237. },
  238. }
  239. newSSHUserOptions := &policy.Options{
  240. SSH: &policy.SSHPolicyOptions{
  241. User: &policy.SSHUserCertificateOptions{
  242. AllowedNames: &policy.SSHNameOptions{
  243. Principals: []string{"*"},
  244. },
  245. DeniedNames: &policy.SSHNameOptions{
  246. Principals: []string{"root"},
  247. },
  248. },
  249. },
  250. }
  251. newSSHOptions := &policy.Options{
  252. SSH: &policy.SSHPolicyOptions{
  253. Host: &policy.SSHHostCertificateOptions{
  254. AllowedNames: &policy.SSHNameOptions{
  255. DNSDomains: []string{"*.local"},
  256. },
  257. DeniedNames: &policy.SSHNameOptions{
  258. DNSDomains: []string{"badhost.local"},
  259. },
  260. },
  261. User: &policy.SSHUserCertificateOptions{
  262. AllowedNames: &policy.SSHNameOptions{
  263. Principals: []string{"*"},
  264. },
  265. DeniedNames: &policy.SSHNameOptions{
  266. Principals: []string{"root"},
  267. },
  268. },
  269. },
  270. }
  271. newOptions := &policy.Options{
  272. X509: &policy.X509PolicyOptions{
  273. AllowedNames: &policy.X509NameOptions{
  274. DNSDomains: []string{"*.local"},
  275. },
  276. DeniedNames: &policy.X509NameOptions{
  277. DNSDomains: []string{"badhost.local"},
  278. },
  279. AllowWildcardNames: true,
  280. },
  281. SSH: &policy.SSHPolicyOptions{
  282. Host: &policy.SSHHostCertificateOptions{
  283. AllowedNames: &policy.SSHNameOptions{
  284. DNSDomains: []string{"*.local"},
  285. },
  286. DeniedNames: &policy.SSHNameOptions{
  287. DNSDomains: []string{"badhost.local"},
  288. },
  289. },
  290. User: &policy.SSHUserCertificateOptions{
  291. AllowedNames: &policy.SSHNameOptions{
  292. Principals: []string{"*"},
  293. },
  294. DeniedNames: &policy.SSHNameOptions{
  295. Principals: []string{"root"},
  296. },
  297. },
  298. },
  299. }
  300. newAdminX509Options := &policy.Options{
  301. X509: &policy.X509PolicyOptions{
  302. AllowedNames: &policy.X509NameOptions{
  303. DNSDomains: []string{"*.local"},
  304. },
  305. },
  306. }
  307. newAdminSSHHostOptions := &policy.Options{
  308. SSH: &policy.SSHPolicyOptions{
  309. Host: &policy.SSHHostCertificateOptions{
  310. AllowedNames: &policy.SSHNameOptions{
  311. DNSDomains: []string{"*.local"},
  312. },
  313. },
  314. },
  315. }
  316. newAdminSSHUserOptions := &policy.Options{
  317. SSH: &policy.SSHPolicyOptions{
  318. User: &policy.SSHUserCertificateOptions{
  319. AllowedNames: &policy.SSHNameOptions{
  320. EmailAddresses: []string{"@example.com"},
  321. },
  322. },
  323. },
  324. }
  325. newAdminOptions := &policy.Options{
  326. X509: &policy.X509PolicyOptions{
  327. AllowedNames: &policy.X509NameOptions{
  328. DNSDomains: []string{"*.local"},
  329. },
  330. DeniedNames: &policy.X509NameOptions{
  331. DNSDomains: []string{"badhost.local"},
  332. },
  333. AllowWildcardNames: true,
  334. },
  335. SSH: &policy.SSHPolicyOptions{
  336. Host: &policy.SSHHostCertificateOptions{
  337. AllowedNames: &policy.SSHNameOptions{
  338. DNSDomains: []string{"*.local"},
  339. },
  340. DeniedNames: &policy.SSHNameOptions{
  341. DNSDomains: []string{"badhost.local"},
  342. },
  343. },
  344. User: &policy.SSHUserCertificateOptions{
  345. AllowedNames: &policy.SSHNameOptions{
  346. EmailAddresses: []string{"@example.com"},
  347. },
  348. DeniedNames: &policy.SSHNameOptions{
  349. EmailAddresses: []string{"baduser@example.com"},
  350. },
  351. },
  352. },
  353. }
  354. tests := []struct {
  355. name string
  356. config *config.Config
  357. adminDB admin.DB
  358. ctx context.Context
  359. expected *policy.Engine
  360. wantErr bool
  361. }{
  362. {
  363. name: "fail/standalone-x509-policy",
  364. config: &config.Config{
  365. AuthorityConfig: &config.AuthConfig{
  366. EnableAdmin: false,
  367. Policy: &policy.Options{
  368. X509: &policy.X509PolicyOptions{
  369. AllowedNames: &policy.X509NameOptions{
  370. DNSDomains: []string{"**.local"},
  371. },
  372. },
  373. },
  374. },
  375. },
  376. ctx: context.Background(),
  377. wantErr: true,
  378. expected: existingPolicyEngine,
  379. },
  380. {
  381. name: "fail/standalone-ssh-host-policy",
  382. config: &config.Config{
  383. AuthorityConfig: &config.AuthConfig{
  384. EnableAdmin: false,
  385. Policy: &policy.Options{
  386. SSH: &policy.SSHPolicyOptions{
  387. Host: &policy.SSHHostCertificateOptions{
  388. AllowedNames: &policy.SSHNameOptions{
  389. DNSDomains: []string{"**.local"},
  390. },
  391. },
  392. },
  393. },
  394. },
  395. },
  396. ctx: context.Background(),
  397. wantErr: true,
  398. expected: existingPolicyEngine,
  399. },
  400. {
  401. name: "fail/standalone-ssh-user-policy",
  402. config: &config.Config{
  403. AuthorityConfig: &config.AuthConfig{
  404. EnableAdmin: false,
  405. Policy: &policy.Options{
  406. SSH: &policy.SSHPolicyOptions{
  407. User: &policy.SSHUserCertificateOptions{
  408. AllowedNames: &policy.SSHNameOptions{
  409. EmailAddresses: []string{"**example.com"},
  410. },
  411. },
  412. },
  413. },
  414. },
  415. },
  416. ctx: context.Background(),
  417. wantErr: true,
  418. expected: existingPolicyEngine,
  419. },
  420. {
  421. name: "fail/adminDB.GetAuthorityPolicy-error",
  422. config: &config.Config{
  423. AuthorityConfig: &config.AuthConfig{
  424. EnableAdmin: true,
  425. },
  426. },
  427. adminDB: &admin.MockDB{
  428. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  429. return nil, errors.New("force")
  430. },
  431. },
  432. ctx: context.Background(),
  433. wantErr: true,
  434. expected: existingPolicyEngine,
  435. },
  436. {
  437. name: "fail/admin-x509-policy",
  438. config: &config.Config{
  439. AuthorityConfig: &config.AuthConfig{
  440. EnableAdmin: true,
  441. },
  442. },
  443. adminDB: &admin.MockDB{
  444. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  445. return &linkedca.Policy{
  446. X509: &linkedca.X509Policy{
  447. Allow: &linkedca.X509Names{
  448. Dns: []string{"**.local"},
  449. },
  450. },
  451. }, nil
  452. },
  453. },
  454. ctx: context.Background(),
  455. wantErr: true,
  456. expected: existingPolicyEngine,
  457. },
  458. {
  459. name: "fail/admin-ssh-host-policy",
  460. config: &config.Config{
  461. AuthorityConfig: &config.AuthConfig{
  462. EnableAdmin: true,
  463. },
  464. },
  465. adminDB: &admin.MockDB{
  466. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  467. return &linkedca.Policy{
  468. Ssh: &linkedca.SSHPolicy{
  469. Host: &linkedca.SSHHostPolicy{
  470. Allow: &linkedca.SSHHostNames{
  471. Dns: []string{"**.local"},
  472. },
  473. },
  474. },
  475. }, nil
  476. },
  477. },
  478. ctx: context.Background(),
  479. wantErr: true,
  480. expected: existingPolicyEngine,
  481. },
  482. {
  483. name: "fail/admin-ssh-user-policy",
  484. config: &config.Config{
  485. AuthorityConfig: &config.AuthConfig{
  486. EnableAdmin: true,
  487. },
  488. },
  489. adminDB: &admin.MockDB{
  490. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  491. return &linkedca.Policy{
  492. Ssh: &linkedca.SSHPolicy{
  493. User: &linkedca.SSHUserPolicy{
  494. Allow: &linkedca.SSHUserNames{
  495. Emails: []string{"@@example.com"},
  496. },
  497. },
  498. },
  499. }, nil
  500. },
  501. },
  502. ctx: context.Background(),
  503. wantErr: true,
  504. expected: existingPolicyEngine,
  505. },
  506. {
  507. name: "ok/linkedca-unsupported",
  508. config: &config.Config{
  509. AuthorityConfig: &config.AuthConfig{
  510. EnableAdmin: true,
  511. },
  512. },
  513. adminDB: &linkedCaClient{},
  514. ctx: context.Background(),
  515. wantErr: false,
  516. expected: existingPolicyEngine,
  517. },
  518. {
  519. name: "ok/standalone-no-policy",
  520. config: &config.Config{
  521. AuthorityConfig: &config.AuthConfig{
  522. EnableAdmin: false,
  523. Policy: nil,
  524. },
  525. },
  526. ctx: context.Background(),
  527. wantErr: false,
  528. expected: mustPolicyEngine(t, nil),
  529. },
  530. {
  531. name: "ok/standalone-x509-policy",
  532. config: &config.Config{
  533. AuthorityConfig: &config.AuthConfig{
  534. EnableAdmin: false,
  535. Policy: &policy.Options{
  536. X509: &policy.X509PolicyOptions{
  537. AllowedNames: &policy.X509NameOptions{
  538. DNSDomains: []string{"*.local"},
  539. },
  540. DeniedNames: &policy.X509NameOptions{
  541. DNSDomains: []string{"badhost.local"},
  542. },
  543. AllowWildcardNames: true,
  544. },
  545. },
  546. },
  547. },
  548. ctx: context.Background(),
  549. wantErr: false,
  550. expected: mustPolicyEngine(t, newX509Options),
  551. },
  552. {
  553. name: "ok/standalone-ssh-host-policy",
  554. config: &config.Config{
  555. AuthorityConfig: &config.AuthConfig{
  556. EnableAdmin: false,
  557. Policy: &policy.Options{
  558. SSH: &policy.SSHPolicyOptions{
  559. Host: &policy.SSHHostCertificateOptions{
  560. AllowedNames: &policy.SSHNameOptions{
  561. DNSDomains: []string{"*.local"},
  562. },
  563. DeniedNames: &policy.SSHNameOptions{
  564. DNSDomains: []string{"badhost.local"},
  565. },
  566. },
  567. },
  568. },
  569. },
  570. },
  571. ctx: context.Background(),
  572. wantErr: false,
  573. expected: mustPolicyEngine(t, newSSHHostOptions),
  574. },
  575. {
  576. name: "ok/standalone-ssh-user-policy",
  577. config: &config.Config{
  578. AuthorityConfig: &config.AuthConfig{
  579. EnableAdmin: false,
  580. Policy: &policy.Options{
  581. SSH: &policy.SSHPolicyOptions{
  582. User: &policy.SSHUserCertificateOptions{
  583. AllowedNames: &policy.SSHNameOptions{
  584. Principals: []string{"*"},
  585. },
  586. DeniedNames: &policy.SSHNameOptions{
  587. Principals: []string{"root"},
  588. },
  589. },
  590. },
  591. },
  592. },
  593. },
  594. ctx: context.Background(),
  595. wantErr: false,
  596. expected: mustPolicyEngine(t, newSSHUserOptions),
  597. },
  598. {
  599. name: "ok/standalone-ssh-policy",
  600. config: &config.Config{
  601. AuthorityConfig: &config.AuthConfig{
  602. EnableAdmin: false,
  603. Policy: &policy.Options{
  604. SSH: &policy.SSHPolicyOptions{
  605. Host: &policy.SSHHostCertificateOptions{
  606. AllowedNames: &policy.SSHNameOptions{
  607. DNSDomains: []string{"*.local"},
  608. },
  609. DeniedNames: &policy.SSHNameOptions{
  610. DNSDomains: []string{"badhost.local"},
  611. },
  612. },
  613. User: &policy.SSHUserCertificateOptions{
  614. AllowedNames: &policy.SSHNameOptions{
  615. Principals: []string{"*"},
  616. },
  617. DeniedNames: &policy.SSHNameOptions{
  618. Principals: []string{"root"},
  619. },
  620. },
  621. },
  622. },
  623. },
  624. },
  625. ctx: context.Background(),
  626. wantErr: false,
  627. expected: mustPolicyEngine(t, newSSHOptions),
  628. },
  629. {
  630. name: "ok/standalone-full-policy",
  631. config: &config.Config{
  632. AuthorityConfig: &config.AuthConfig{
  633. EnableAdmin: false,
  634. Policy: &policy.Options{
  635. X509: &policy.X509PolicyOptions{
  636. AllowedNames: &policy.X509NameOptions{
  637. DNSDomains: []string{"*.local"},
  638. },
  639. DeniedNames: &policy.X509NameOptions{
  640. DNSDomains: []string{"badhost.local"},
  641. },
  642. AllowWildcardNames: true,
  643. },
  644. SSH: &policy.SSHPolicyOptions{
  645. Host: &policy.SSHHostCertificateOptions{
  646. AllowedNames: &policy.SSHNameOptions{
  647. DNSDomains: []string{"*.local"},
  648. },
  649. DeniedNames: &policy.SSHNameOptions{
  650. DNSDomains: []string{"badhost.local"},
  651. },
  652. },
  653. User: &policy.SSHUserCertificateOptions{
  654. AllowedNames: &policy.SSHNameOptions{
  655. Principals: []string{"*"},
  656. },
  657. DeniedNames: &policy.SSHNameOptions{
  658. Principals: []string{"root"},
  659. },
  660. },
  661. },
  662. },
  663. },
  664. },
  665. ctx: context.Background(),
  666. wantErr: false,
  667. expected: mustPolicyEngine(t, newOptions),
  668. },
  669. {
  670. name: "ok/admin-x509-policy",
  671. config: &config.Config{
  672. AuthorityConfig: &config.AuthConfig{
  673. EnableAdmin: true,
  674. },
  675. },
  676. adminDB: &admin.MockDB{
  677. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  678. return &linkedca.Policy{
  679. X509: &linkedca.X509Policy{
  680. Allow: &linkedca.X509Names{
  681. Dns: []string{"*.local"},
  682. },
  683. },
  684. }, nil
  685. },
  686. },
  687. ctx: context.Background(),
  688. wantErr: false,
  689. expected: mustPolicyEngine(t, newAdminX509Options),
  690. },
  691. {
  692. name: "ok/admin-ssh-host-policy",
  693. config: &config.Config{
  694. AuthorityConfig: &config.AuthConfig{
  695. EnableAdmin: true,
  696. },
  697. },
  698. adminDB: &admin.MockDB{
  699. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  700. return &linkedca.Policy{
  701. Ssh: &linkedca.SSHPolicy{
  702. Host: &linkedca.SSHHostPolicy{
  703. Allow: &linkedca.SSHHostNames{
  704. Dns: []string{"*.local"},
  705. },
  706. },
  707. },
  708. }, nil
  709. },
  710. },
  711. ctx: context.Background(),
  712. wantErr: false,
  713. expected: mustPolicyEngine(t, newAdminSSHHostOptions),
  714. },
  715. {
  716. name: "ok/admin-ssh-user-policy",
  717. config: &config.Config{
  718. AuthorityConfig: &config.AuthConfig{
  719. EnableAdmin: true,
  720. },
  721. },
  722. adminDB: &admin.MockDB{
  723. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  724. return &linkedca.Policy{
  725. Ssh: &linkedca.SSHPolicy{
  726. User: &linkedca.SSHUserPolicy{
  727. Allow: &linkedca.SSHUserNames{
  728. Emails: []string{"@example.com"},
  729. },
  730. },
  731. },
  732. }, nil
  733. },
  734. },
  735. ctx: context.Background(),
  736. wantErr: false,
  737. expected: mustPolicyEngine(t, newAdminSSHUserOptions),
  738. },
  739. {
  740. name: "ok/admin-full-policy",
  741. config: &config.Config{
  742. AuthorityConfig: &config.AuthConfig{
  743. EnableAdmin: true,
  744. },
  745. },
  746. ctx: context.Background(),
  747. adminDB: &admin.MockDB{
  748. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  749. return &linkedca.Policy{
  750. X509: &linkedca.X509Policy{
  751. Allow: &linkedca.X509Names{
  752. Dns: []string{"*.local"},
  753. },
  754. Deny: &linkedca.X509Names{
  755. Dns: []string{"badhost.local"},
  756. },
  757. AllowWildcardNames: true,
  758. },
  759. Ssh: &linkedca.SSHPolicy{
  760. Host: &linkedca.SSHHostPolicy{
  761. Allow: &linkedca.SSHHostNames{
  762. Dns: []string{"*.local"},
  763. },
  764. Deny: &linkedca.SSHHostNames{
  765. Dns: []string{"badhost.local"},
  766. },
  767. },
  768. User: &linkedca.SSHUserPolicy{
  769. Allow: &linkedca.SSHUserNames{
  770. Emails: []string{"@example.com"},
  771. },
  772. Deny: &linkedca.SSHUserNames{
  773. Emails: []string{"baduser@example.com"},
  774. },
  775. },
  776. },
  777. }, nil
  778. },
  779. },
  780. wantErr: false,
  781. expected: mustPolicyEngine(t, newAdminOptions),
  782. },
  783. {
  784. // both DB and JSON config; DB config is taken if Admin API is enabled
  785. name: "ok/admin-over-standalone",
  786. config: &config.Config{
  787. AuthorityConfig: &config.AuthConfig{
  788. EnableAdmin: true,
  789. Policy: &policy.Options{
  790. SSH: &policy.SSHPolicyOptions{
  791. Host: &policy.SSHHostCertificateOptions{
  792. AllowedNames: &policy.SSHNameOptions{
  793. DNSDomains: []string{"*.local"},
  794. },
  795. DeniedNames: &policy.SSHNameOptions{
  796. DNSDomains: []string{"badhost.local"},
  797. },
  798. },
  799. User: &policy.SSHUserCertificateOptions{
  800. AllowedNames: &policy.SSHNameOptions{
  801. Principals: []string{"*"},
  802. },
  803. DeniedNames: &policy.SSHNameOptions{
  804. Principals: []string{"root"},
  805. },
  806. },
  807. },
  808. },
  809. },
  810. },
  811. ctx: context.Background(),
  812. adminDB: &admin.MockDB{
  813. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  814. return &linkedca.Policy{
  815. X509: &linkedca.X509Policy{
  816. Allow: &linkedca.X509Names{
  817. Dns: []string{"*.local"},
  818. },
  819. Deny: &linkedca.X509Names{
  820. Dns: []string{"badhost.local"},
  821. },
  822. AllowWildcardNames: true,
  823. },
  824. }, nil
  825. },
  826. },
  827. wantErr: false,
  828. expected: mustPolicyEngine(t, newX509Options),
  829. },
  830. }
  831. for _, tt := range tests {
  832. t.Run(tt.name, func(t *testing.T) {
  833. a := &Authority{
  834. config: tt.config,
  835. adminDB: tt.adminDB,
  836. policyEngine: existingPolicyEngine,
  837. }
  838. if err := a.reloadPolicyEngines(tt.ctx); (err != nil) != tt.wantErr {
  839. t.Errorf("Authority.reloadPolicyEngines() error = %v, wantErr %v", err, tt.wantErr)
  840. }
  841. assert.Equal(t, tt.expected, a.policyEngine)
  842. })
  843. }
  844. }
  845. func TestAuthority_checkAuthorityPolicy(t *testing.T) {
  846. type fields struct {
  847. provisioners *provisioner.Collection
  848. admins *administrator.Collection
  849. db db.AuthDB
  850. adminDB admin.DB
  851. }
  852. type args struct {
  853. ctx context.Context
  854. currentAdmin *linkedca.Admin
  855. provName string
  856. p *linkedca.Policy
  857. }
  858. tests := []struct {
  859. name string
  860. fields fields
  861. args args
  862. wantErr bool
  863. }{
  864. {
  865. name: "no policy",
  866. fields: fields{},
  867. args: args{
  868. currentAdmin: nil,
  869. provName: "prov",
  870. p: nil,
  871. },
  872. wantErr: false,
  873. },
  874. {
  875. name: "fail/adminDB.GetAdmins-error",
  876. fields: fields{
  877. admins: administrator.NewCollection(nil),
  878. adminDB: &admin.MockDB{
  879. MockGetAdmins: func(ctx context.Context) ([]*linkedca.Admin, error) {
  880. return nil, errors.New("force")
  881. },
  882. },
  883. },
  884. args: args{
  885. currentAdmin: &linkedca.Admin{Subject: "step"},
  886. provName: "prov",
  887. p: &linkedca.Policy{
  888. X509: &linkedca.X509Policy{
  889. Allow: &linkedca.X509Names{
  890. Dns: []string{"step", "otherAdmin"},
  891. },
  892. },
  893. },
  894. },
  895. wantErr: true,
  896. },
  897. {
  898. name: "fail/policy",
  899. fields: fields{
  900. admins: administrator.NewCollection(nil),
  901. adminDB: &admin.MockDB{
  902. MockGetAdmins: func(ctx context.Context) ([]*linkedca.Admin, error) {
  903. return []*linkedca.Admin{
  904. {
  905. Id: "adminID1",
  906. Subject: "anotherAdmin",
  907. },
  908. {
  909. Id: "adminID2",
  910. Subject: "step",
  911. },
  912. {
  913. Id: "adminID3",
  914. Subject: "otherAdmin",
  915. },
  916. }, nil
  917. },
  918. },
  919. },
  920. args: args{
  921. currentAdmin: &linkedca.Admin{Subject: "step"},
  922. provName: "prov",
  923. p: &linkedca.Policy{
  924. X509: &linkedca.X509Policy{
  925. Allow: &linkedca.X509Names{
  926. Dns: []string{"step", "otherAdmin"},
  927. },
  928. },
  929. },
  930. },
  931. wantErr: true,
  932. },
  933. {
  934. name: "ok",
  935. fields: fields{
  936. admins: administrator.NewCollection(nil),
  937. adminDB: &admin.MockDB{
  938. MockGetAdmins: func(ctx context.Context) ([]*linkedca.Admin, error) {
  939. return []*linkedca.Admin{
  940. {
  941. Id: "adminID2",
  942. Subject: "step",
  943. },
  944. {
  945. Id: "adminID3",
  946. Subject: "otherAdmin",
  947. },
  948. }, nil
  949. },
  950. },
  951. },
  952. args: args{
  953. currentAdmin: &linkedca.Admin{Subject: "step"},
  954. provName: "prov",
  955. p: &linkedca.Policy{
  956. X509: &linkedca.X509Policy{
  957. Allow: &linkedca.X509Names{
  958. Dns: []string{"step", "otherAdmin"},
  959. },
  960. },
  961. },
  962. },
  963. wantErr: false,
  964. },
  965. }
  966. for _, tt := range tests {
  967. t.Run(tt.name, func(t *testing.T) {
  968. a := &Authority{
  969. provisioners: tt.fields.provisioners,
  970. admins: tt.fields.admins,
  971. db: tt.fields.db,
  972. adminDB: tt.fields.adminDB,
  973. }
  974. if err := a.checkAuthorityPolicy(tt.args.ctx, tt.args.currentAdmin, tt.args.p); (err != nil) != tt.wantErr {
  975. t.Errorf("Authority.checkProvisionerPolicy() error = %v, wantErr %v", err, tt.wantErr)
  976. }
  977. })
  978. }
  979. }
  980. func TestAuthority_checkProvisionerPolicy(t *testing.T) {
  981. jwkProvisioner := &provisioner.JWK{
  982. ID: "jwkID",
  983. Type: "JWK",
  984. Name: "jwkProv",
  985. Key: &jose.JSONWebKey{KeyID: "jwkKeyID"},
  986. }
  987. provisioners := provisioner.NewCollection(testAudiences)
  988. provisioners.Store(jwkProvisioner)
  989. admins := administrator.NewCollection(provisioners)
  990. admins.Store(&linkedca.Admin{
  991. Id: "adminID",
  992. Subject: "step",
  993. ProvisionerId: "jwkID",
  994. }, jwkProvisioner)
  995. type fields struct {
  996. provisioners *provisioner.Collection
  997. admins *administrator.Collection
  998. db db.AuthDB
  999. adminDB admin.DB
  1000. }
  1001. type args struct {
  1002. ctx context.Context
  1003. provName string
  1004. p *linkedca.Policy
  1005. }
  1006. tests := []struct {
  1007. name string
  1008. fields fields
  1009. args args
  1010. wantErr bool
  1011. }{
  1012. {
  1013. name: "no policy",
  1014. fields: fields{},
  1015. args: args{
  1016. provName: "prov",
  1017. p: nil,
  1018. },
  1019. wantErr: false,
  1020. },
  1021. {
  1022. name: "fail/policy",
  1023. fields: fields{
  1024. provisioners: provisioners,
  1025. admins: admins,
  1026. },
  1027. args: args{
  1028. provName: "jwkProv",
  1029. p: &linkedca.Policy{
  1030. X509: &linkedca.X509Policy{
  1031. Allow: &linkedca.X509Names{
  1032. Dns: []string{"otherAdmin"}, // step not in policy
  1033. },
  1034. },
  1035. },
  1036. },
  1037. wantErr: true,
  1038. },
  1039. {
  1040. name: "ok",
  1041. fields: fields{
  1042. provisioners: provisioners,
  1043. admins: admins,
  1044. },
  1045. args: args{
  1046. provName: "jwkProv",
  1047. p: &linkedca.Policy{
  1048. X509: &linkedca.X509Policy{
  1049. Allow: &linkedca.X509Names{
  1050. Dns: []string{"step", "otherAdmin"},
  1051. },
  1052. },
  1053. },
  1054. },
  1055. wantErr: false,
  1056. },
  1057. }
  1058. for _, tt := range tests {
  1059. t.Run(tt.name, func(t *testing.T) {
  1060. a := &Authority{
  1061. provisioners: tt.fields.provisioners,
  1062. admins: tt.fields.admins,
  1063. db: tt.fields.db,
  1064. adminDB: tt.fields.adminDB,
  1065. }
  1066. if err := a.checkProvisionerPolicy(tt.args.ctx, tt.args.provName, tt.args.p); (err != nil) != tt.wantErr {
  1067. t.Errorf("Authority.checkProvisionerPolicy() error = %v, wantErr %v", err, tt.wantErr)
  1068. }
  1069. })
  1070. }
  1071. }
  1072. func TestAuthority_RemoveAuthorityPolicy(t *testing.T) {
  1073. type fields struct {
  1074. config *config.Config
  1075. db db.AuthDB
  1076. adminDB admin.DB
  1077. }
  1078. type args struct {
  1079. ctx context.Context
  1080. }
  1081. tests := []struct {
  1082. name string
  1083. fields fields
  1084. args args
  1085. wantErr *PolicyError
  1086. }{
  1087. {
  1088. name: "fail/adminDB.DeleteAuthorityPolicy",
  1089. fields: fields{
  1090. config: &config.Config{
  1091. AuthorityConfig: &config.AuthConfig{
  1092. EnableAdmin: true,
  1093. },
  1094. },
  1095. adminDB: &admin.MockDB{
  1096. MockDeleteAuthorityPolicy: func(ctx context.Context) error {
  1097. return errors.New("force")
  1098. },
  1099. },
  1100. },
  1101. wantErr: &PolicyError{
  1102. Typ: StoreFailure,
  1103. Err: errors.New("force"),
  1104. },
  1105. },
  1106. {
  1107. name: "fail/a.reloadPolicyEngines",
  1108. fields: fields{
  1109. config: &config.Config{
  1110. AuthorityConfig: &config.AuthConfig{
  1111. EnableAdmin: true,
  1112. },
  1113. },
  1114. adminDB: &admin.MockDB{
  1115. MockDeleteAuthorityPolicy: func(ctx context.Context) error {
  1116. return nil
  1117. },
  1118. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  1119. return nil, errors.New("force")
  1120. },
  1121. },
  1122. },
  1123. wantErr: &PolicyError{
  1124. Typ: ReloadFailure,
  1125. Err: errors.New("error reloading policy engines when deleting authority policy: error getting policy to (re)load policy engines: force"),
  1126. },
  1127. },
  1128. {
  1129. name: "ok",
  1130. fields: fields{
  1131. config: &config.Config{
  1132. AuthorityConfig: &config.AuthConfig{
  1133. EnableAdmin: true,
  1134. },
  1135. },
  1136. adminDB: &admin.MockDB{
  1137. MockDeleteAuthorityPolicy: func(ctx context.Context) error {
  1138. return nil
  1139. },
  1140. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  1141. return nil, nil
  1142. },
  1143. },
  1144. },
  1145. },
  1146. }
  1147. for _, tt := range tests {
  1148. t.Run(tt.name, func(t *testing.T) {
  1149. a := &Authority{
  1150. config: tt.fields.config,
  1151. db: tt.fields.db,
  1152. adminDB: tt.fields.adminDB,
  1153. }
  1154. err := a.RemoveAuthorityPolicy(tt.args.ctx)
  1155. if err != nil {
  1156. var pe *PolicyError
  1157. if assert.True(t, errors.As(err, &pe)) {
  1158. assert.Equal(t, tt.wantErr.Typ, pe.Typ)
  1159. assert.Equal(t, tt.wantErr.Err.Error(), pe.Err.Error())
  1160. }
  1161. return
  1162. }
  1163. })
  1164. }
  1165. }
  1166. func TestAuthority_GetAuthorityPolicy(t *testing.T) {
  1167. type fields struct {
  1168. config *config.Config
  1169. db db.AuthDB
  1170. adminDB admin.DB
  1171. }
  1172. type args struct {
  1173. ctx context.Context
  1174. }
  1175. tests := []struct {
  1176. name string
  1177. fields fields
  1178. args args
  1179. want *linkedca.Policy
  1180. wantErr *PolicyError
  1181. }{
  1182. {
  1183. name: "fail/adminDB.GetAuthorityPolicy",
  1184. fields: fields{
  1185. config: &config.Config{
  1186. AuthorityConfig: &config.AuthConfig{
  1187. EnableAdmin: true,
  1188. },
  1189. },
  1190. adminDB: &admin.MockDB{
  1191. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  1192. return nil, errors.New("force")
  1193. },
  1194. },
  1195. },
  1196. wantErr: &PolicyError{
  1197. Typ: InternalFailure,
  1198. Err: errors.New("force"),
  1199. },
  1200. },
  1201. {
  1202. name: "ok",
  1203. fields: fields{
  1204. config: &config.Config{
  1205. AuthorityConfig: &config.AuthConfig{
  1206. EnableAdmin: true,
  1207. },
  1208. },
  1209. adminDB: &admin.MockDB{
  1210. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  1211. return &linkedca.Policy{}, nil
  1212. },
  1213. },
  1214. },
  1215. want: &linkedca.Policy{},
  1216. },
  1217. }
  1218. for _, tt := range tests {
  1219. t.Run(tt.name, func(t *testing.T) {
  1220. a := &Authority{
  1221. config: tt.fields.config,
  1222. db: tt.fields.db,
  1223. adminDB: tt.fields.adminDB,
  1224. }
  1225. got, err := a.GetAuthorityPolicy(tt.args.ctx)
  1226. if err != nil {
  1227. var pe *PolicyError
  1228. if assert.True(t, errors.As(err, &pe)) {
  1229. assert.Equal(t, tt.wantErr.Typ, pe.Typ)
  1230. assert.Equal(t, tt.wantErr.Err.Error(), pe.Err.Error())
  1231. }
  1232. return
  1233. }
  1234. if !reflect.DeepEqual(got, tt.want) {
  1235. t.Errorf("Authority.GetAuthorityPolicy() = %v, want %v", got, tt.want)
  1236. }
  1237. })
  1238. }
  1239. }
  1240. func TestAuthority_CreateAuthorityPolicy(t *testing.T) {
  1241. type fields struct {
  1242. config *config.Config
  1243. db db.AuthDB
  1244. adminDB admin.DB
  1245. }
  1246. type args struct {
  1247. ctx context.Context
  1248. adm *linkedca.Admin
  1249. p *linkedca.Policy
  1250. }
  1251. tests := []struct {
  1252. name string
  1253. fields fields
  1254. args args
  1255. want *linkedca.Policy
  1256. wantErr *PolicyError
  1257. }{
  1258. {
  1259. name: "fail/a.checkAuthorityPolicy",
  1260. fields: fields{
  1261. config: &config.Config{
  1262. AuthorityConfig: &config.AuthConfig{
  1263. EnableAdmin: true,
  1264. },
  1265. },
  1266. adminDB: &admin.MockDB{
  1267. MockGetAdmins: func(ctx context.Context) ([]*linkedca.Admin, error) {
  1268. return nil, errors.New("force")
  1269. },
  1270. },
  1271. },
  1272. args: args{
  1273. ctx: context.Background(),
  1274. adm: &linkedca.Admin{Subject: "step"},
  1275. p: &linkedca.Policy{
  1276. X509: &linkedca.X509Policy{
  1277. Allow: &linkedca.X509Names{
  1278. Dns: []string{"step", "otherAdmin"},
  1279. },
  1280. },
  1281. },
  1282. },
  1283. wantErr: &PolicyError{
  1284. Typ: InternalFailure,
  1285. Err: errors.New("error retrieving admins: force"),
  1286. },
  1287. },
  1288. {
  1289. name: "fail/adminDB.CreateAuthorityPolicy",
  1290. fields: fields{
  1291. config: &config.Config{
  1292. AuthorityConfig: &config.AuthConfig{
  1293. EnableAdmin: true,
  1294. },
  1295. },
  1296. adminDB: &admin.MockDB{
  1297. MockGetAdmins: func(ctx context.Context) ([]*linkedca.Admin, error) {
  1298. return []*linkedca.Admin{}, nil
  1299. },
  1300. MockCreateAuthorityPolicy: func(ctx context.Context, policy *linkedca.Policy) error {
  1301. return errors.New("force")
  1302. },
  1303. },
  1304. },
  1305. args: args{
  1306. ctx: context.Background(),
  1307. adm: &linkedca.Admin{Subject: "step"},
  1308. p: &linkedca.Policy{
  1309. X509: &linkedca.X509Policy{
  1310. Allow: &linkedca.X509Names{
  1311. Dns: []string{"step", "otherAdmin"},
  1312. },
  1313. },
  1314. },
  1315. },
  1316. wantErr: &PolicyError{
  1317. Typ: StoreFailure,
  1318. Err: errors.New("force"),
  1319. },
  1320. },
  1321. {
  1322. name: "fail/a.reloadPolicyEngines",
  1323. fields: fields{
  1324. config: &config.Config{
  1325. AuthorityConfig: &config.AuthConfig{
  1326. EnableAdmin: true,
  1327. },
  1328. },
  1329. adminDB: &admin.MockDB{
  1330. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  1331. return nil, errors.New("force")
  1332. },
  1333. MockGetAdmins: func(ctx context.Context) ([]*linkedca.Admin, error) {
  1334. return []*linkedca.Admin{}, nil
  1335. },
  1336. },
  1337. },
  1338. args: args{
  1339. ctx: context.Background(),
  1340. adm: &linkedca.Admin{Subject: "step"},
  1341. p: &linkedca.Policy{
  1342. X509: &linkedca.X509Policy{
  1343. Allow: &linkedca.X509Names{
  1344. Dns: []string{"step", "otherAdmin"},
  1345. },
  1346. },
  1347. },
  1348. },
  1349. wantErr: &PolicyError{
  1350. Typ: ReloadFailure,
  1351. Err: errors.New("error reloading policy engines when creating authority policy: error getting policy to (re)load policy engines: force"),
  1352. },
  1353. },
  1354. {
  1355. name: "ok",
  1356. fields: fields{
  1357. config: &config.Config{
  1358. AuthorityConfig: &config.AuthConfig{
  1359. EnableAdmin: true,
  1360. },
  1361. },
  1362. adminDB: &admin.MockDB{
  1363. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  1364. return &linkedca.Policy{
  1365. X509: &linkedca.X509Policy{
  1366. Allow: &linkedca.X509Names{
  1367. Dns: []string{"step", "otherAdmin"},
  1368. },
  1369. },
  1370. }, nil
  1371. },
  1372. MockGetAdmins: func(ctx context.Context) ([]*linkedca.Admin, error) {
  1373. return []*linkedca.Admin{}, nil
  1374. },
  1375. },
  1376. },
  1377. args: args{
  1378. ctx: context.Background(),
  1379. adm: &linkedca.Admin{Subject: "step"},
  1380. p: &linkedca.Policy{
  1381. X509: &linkedca.X509Policy{
  1382. Allow: &linkedca.X509Names{
  1383. Dns: []string{"step", "otherAdmin"},
  1384. },
  1385. },
  1386. },
  1387. },
  1388. want: &linkedca.Policy{
  1389. X509: &linkedca.X509Policy{
  1390. Allow: &linkedca.X509Names{
  1391. Dns: []string{"step", "otherAdmin"},
  1392. },
  1393. },
  1394. },
  1395. },
  1396. }
  1397. for _, tt := range tests {
  1398. t.Run(tt.name, func(t *testing.T) {
  1399. a := &Authority{
  1400. config: tt.fields.config,
  1401. db: tt.fields.db,
  1402. adminDB: tt.fields.adminDB,
  1403. }
  1404. got, err := a.CreateAuthorityPolicy(tt.args.ctx, tt.args.adm, tt.args.p)
  1405. if err != nil {
  1406. var pe *PolicyError
  1407. if assert.True(t, errors.As(err, &pe)) {
  1408. assert.Equal(t, tt.wantErr.Typ, pe.Typ)
  1409. assert.Equal(t, tt.wantErr.Err.Error(), pe.Err.Error())
  1410. }
  1411. return
  1412. }
  1413. if !reflect.DeepEqual(got, tt.want) {
  1414. t.Errorf("Authority.CreateAuthorityPolicy() = %v, want %v", got, tt.want)
  1415. }
  1416. })
  1417. }
  1418. }
  1419. func TestAuthority_UpdateAuthorityPolicy(t *testing.T) {
  1420. type fields struct {
  1421. config *config.Config
  1422. db db.AuthDB
  1423. adminDB admin.DB
  1424. }
  1425. type args struct {
  1426. ctx context.Context
  1427. adm *linkedca.Admin
  1428. p *linkedca.Policy
  1429. }
  1430. tests := []struct {
  1431. name string
  1432. fields fields
  1433. args args
  1434. want *linkedca.Policy
  1435. wantErr *PolicyError
  1436. }{
  1437. {
  1438. name: "fail/a.checkAuthorityPolicy",
  1439. fields: fields{
  1440. config: &config.Config{
  1441. AuthorityConfig: &config.AuthConfig{
  1442. EnableAdmin: true,
  1443. },
  1444. },
  1445. adminDB: &admin.MockDB{
  1446. MockGetAdmins: func(ctx context.Context) ([]*linkedca.Admin, error) {
  1447. return nil, errors.New("force")
  1448. },
  1449. },
  1450. },
  1451. args: args{
  1452. ctx: context.Background(),
  1453. adm: &linkedca.Admin{Subject: "step"},
  1454. p: &linkedca.Policy{
  1455. X509: &linkedca.X509Policy{
  1456. Allow: &linkedca.X509Names{
  1457. Dns: []string{"step", "otherAdmin"},
  1458. },
  1459. },
  1460. },
  1461. },
  1462. wantErr: &PolicyError{
  1463. Typ: InternalFailure,
  1464. Err: errors.New("error retrieving admins: force"),
  1465. },
  1466. },
  1467. {
  1468. name: "fail/adminDB.UpdateAuthorityPolicy",
  1469. fields: fields{
  1470. config: &config.Config{
  1471. AuthorityConfig: &config.AuthConfig{
  1472. EnableAdmin: true,
  1473. },
  1474. },
  1475. adminDB: &admin.MockDB{
  1476. MockGetAdmins: func(ctx context.Context) ([]*linkedca.Admin, error) {
  1477. return []*linkedca.Admin{}, nil
  1478. },
  1479. MockUpdateAuthorityPolicy: func(ctx context.Context, policy *linkedca.Policy) error {
  1480. return errors.New("force")
  1481. },
  1482. },
  1483. },
  1484. args: args{
  1485. ctx: context.Background(),
  1486. adm: &linkedca.Admin{Subject: "step"},
  1487. p: &linkedca.Policy{
  1488. X509: &linkedca.X509Policy{
  1489. Allow: &linkedca.X509Names{
  1490. Dns: []string{"step", "otherAdmin"},
  1491. },
  1492. },
  1493. },
  1494. },
  1495. wantErr: &PolicyError{
  1496. Typ: StoreFailure,
  1497. Err: errors.New("force"),
  1498. },
  1499. },
  1500. {
  1501. name: "fail/a.reloadPolicyEngines",
  1502. fields: fields{
  1503. config: &config.Config{
  1504. AuthorityConfig: &config.AuthConfig{
  1505. EnableAdmin: true,
  1506. },
  1507. },
  1508. adminDB: &admin.MockDB{
  1509. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  1510. return nil, errors.New("force")
  1511. },
  1512. MockGetAdmins: func(ctx context.Context) ([]*linkedca.Admin, error) {
  1513. return []*linkedca.Admin{}, nil
  1514. },
  1515. },
  1516. },
  1517. args: args{
  1518. ctx: context.Background(),
  1519. adm: &linkedca.Admin{Subject: "step"},
  1520. p: &linkedca.Policy{
  1521. X509: &linkedca.X509Policy{
  1522. Allow: &linkedca.X509Names{
  1523. Dns: []string{"step", "otherAdmin"},
  1524. },
  1525. },
  1526. },
  1527. },
  1528. wantErr: &PolicyError{
  1529. Typ: ReloadFailure,
  1530. Err: errors.New("error reloading policy engines when updating authority policy: error getting policy to (re)load policy engines: force"),
  1531. },
  1532. },
  1533. {
  1534. name: "ok",
  1535. fields: fields{
  1536. config: &config.Config{
  1537. AuthorityConfig: &config.AuthConfig{
  1538. EnableAdmin: true,
  1539. },
  1540. },
  1541. adminDB: &admin.MockDB{
  1542. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  1543. return &linkedca.Policy{
  1544. X509: &linkedca.X509Policy{
  1545. Allow: &linkedca.X509Names{
  1546. Dns: []string{"step", "otherAdmin"},
  1547. },
  1548. },
  1549. }, nil
  1550. },
  1551. MockUpdateAuthorityPolicy: func(ctx context.Context, policy *linkedca.Policy) error {
  1552. return nil
  1553. },
  1554. MockGetAdmins: func(ctx context.Context) ([]*linkedca.Admin, error) {
  1555. return []*linkedca.Admin{}, nil
  1556. },
  1557. },
  1558. },
  1559. args: args{
  1560. ctx: context.Background(),
  1561. adm: &linkedca.Admin{Subject: "step"},
  1562. p: &linkedca.Policy{
  1563. X509: &linkedca.X509Policy{
  1564. Allow: &linkedca.X509Names{
  1565. Dns: []string{"step", "otherAdmin"},
  1566. },
  1567. },
  1568. },
  1569. },
  1570. want: &linkedca.Policy{
  1571. X509: &linkedca.X509Policy{
  1572. Allow: &linkedca.X509Names{
  1573. Dns: []string{"step", "otherAdmin"},
  1574. },
  1575. },
  1576. },
  1577. },
  1578. }
  1579. for _, tt := range tests {
  1580. t.Run(tt.name, func(t *testing.T) {
  1581. a := &Authority{
  1582. config: tt.fields.config,
  1583. db: tt.fields.db,
  1584. adminDB: tt.fields.adminDB,
  1585. }
  1586. got, err := a.UpdateAuthorityPolicy(tt.args.ctx, tt.args.adm, tt.args.p)
  1587. if err != nil {
  1588. var pe *PolicyError
  1589. if assert.True(t, errors.As(err, &pe)) {
  1590. assert.Equal(t, tt.wantErr.Typ, pe.Typ)
  1591. assert.Equal(t, tt.wantErr.Err.Error(), pe.Err.Error())
  1592. }
  1593. return
  1594. }
  1595. if !reflect.DeepEqual(got, tt.want) {
  1596. t.Errorf("Authority.UpdateAuthorityPolicy() = %v, want %v", got, tt.want)
  1597. }
  1598. })
  1599. }
  1600. }