policy_test.go 60 KB

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