policy_test.go 55 KB

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