policy_test.go 58 KB

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