policy_test.go 55 KB

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