policy_test.go 39 KB

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