provisioner_test.go 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. package api
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "errors"
  7. "io"
  8. "net/http"
  9. "net/http/httptest"
  10. "testing"
  11. "time"
  12. "github.com/go-chi/chi"
  13. "github.com/google/go-cmp/cmp"
  14. "github.com/google/go-cmp/cmp/cmpopts"
  15. "github.com/smallstep/assert"
  16. "github.com/smallstep/certificates/authority/admin"
  17. "github.com/smallstep/certificates/authority/provisioner"
  18. "go.step.sm/linkedca"
  19. "google.golang.org/protobuf/encoding/protojson"
  20. "google.golang.org/protobuf/types/known/timestamppb"
  21. )
  22. func TestHandler_GetProvisioner(t *testing.T) {
  23. type test struct {
  24. ctx context.Context
  25. auth adminAuthority
  26. adminDB admin.DB
  27. req *http.Request
  28. statusCode int
  29. err *admin.Error
  30. prov *linkedca.Provisioner
  31. }
  32. var tests = map[string]func(t *testing.T) test{
  33. "fail/auth.LoadProvisionerByID": func(t *testing.T) test {
  34. req := httptest.NewRequest("GET", "/foo?id=provID", nil)
  35. chiCtx := chi.NewRouteContext()
  36. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  37. auth := &mockAdminAuthority{
  38. MockLoadProvisionerByID: func(id string) (provisioner.Interface, error) {
  39. assert.Equals(t, "provID", id)
  40. return nil, errors.New("force")
  41. },
  42. }
  43. return test{
  44. ctx: ctx,
  45. req: req,
  46. auth: auth,
  47. statusCode: 500,
  48. err: &admin.Error{
  49. Type: admin.ErrorServerInternalType.String(),
  50. Status: 500,
  51. Detail: "the server experienced an internal error",
  52. Message: "error loading provisioner provID: force",
  53. },
  54. }
  55. },
  56. "fail/auth.LoadProvisionerByName": func(t *testing.T) test {
  57. req := httptest.NewRequest("GET", "/foo", nil)
  58. chiCtx := chi.NewRouteContext()
  59. chiCtx.URLParams.Add("name", "provName")
  60. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  61. auth := &mockAdminAuthority{
  62. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  63. assert.Equals(t, "provName", name)
  64. return nil, errors.New("force")
  65. },
  66. }
  67. return test{
  68. ctx: ctx,
  69. req: req,
  70. auth: auth,
  71. statusCode: 500,
  72. err: &admin.Error{
  73. Type: admin.ErrorServerInternalType.String(),
  74. Status: 500,
  75. Detail: "the server experienced an internal error",
  76. Message: "error loading provisioner provName: force",
  77. },
  78. }
  79. },
  80. "fail/db.GetProvisioner": func(t *testing.T) test {
  81. req := httptest.NewRequest("GET", "/foo", nil)
  82. chiCtx := chi.NewRouteContext()
  83. chiCtx.URLParams.Add("name", "provName")
  84. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  85. auth := &mockAdminAuthority{
  86. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  87. assert.Equals(t, "provName", name)
  88. return &provisioner.ACME{
  89. ID: "acmeID",
  90. Name: "provName",
  91. }, nil
  92. },
  93. }
  94. db := &admin.MockDB{
  95. MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  96. assert.Equals(t, "acmeID", id)
  97. return nil, admin.NewErrorISE("error loading provisioner provName: force")
  98. },
  99. }
  100. return test{
  101. ctx: ctx,
  102. req: req,
  103. auth: auth,
  104. adminDB: db,
  105. statusCode: 500,
  106. err: &admin.Error{
  107. Type: admin.ErrorServerInternalType.String(),
  108. Status: 500,
  109. Detail: "the server experienced an internal error",
  110. Message: "error loading provisioner provName: force",
  111. },
  112. }
  113. },
  114. "ok": func(t *testing.T) test {
  115. req := httptest.NewRequest("GET", "/foo", nil)
  116. chiCtx := chi.NewRouteContext()
  117. chiCtx.URLParams.Add("name", "provName")
  118. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  119. auth := &mockAdminAuthority{
  120. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  121. assert.Equals(t, "provName", name)
  122. return &provisioner.ACME{
  123. ID: "acmeID",
  124. Name: "provName",
  125. }, nil
  126. },
  127. }
  128. prov := &linkedca.Provisioner{
  129. Id: "acmeID",
  130. Type: linkedca.Provisioner_ACME,
  131. Name: "provName", // TODO(hs): other fields too?
  132. }
  133. db := &admin.MockDB{
  134. MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  135. assert.Equals(t, "acmeID", id)
  136. return prov, nil
  137. },
  138. }
  139. return test{
  140. ctx: ctx,
  141. req: req,
  142. auth: auth,
  143. adminDB: db,
  144. statusCode: 200,
  145. err: nil,
  146. prov: prov,
  147. }
  148. },
  149. }
  150. for name, prep := range tests {
  151. tc := prep(t)
  152. t.Run(name, func(t *testing.T) {
  153. h := &Handler{
  154. auth: tc.auth,
  155. adminDB: tc.adminDB,
  156. }
  157. req := tc.req.WithContext(tc.ctx)
  158. w := httptest.NewRecorder()
  159. h.GetProvisioner(w, req)
  160. res := w.Result()
  161. assert.Equals(t, tc.statusCode, res.StatusCode)
  162. if res.StatusCode >= 400 {
  163. body, err := io.ReadAll(res.Body)
  164. res.Body.Close()
  165. assert.FatalError(t, err)
  166. adminErr := admin.Error{}
  167. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &adminErr))
  168. assert.Equals(t, tc.err.Type, adminErr.Type)
  169. assert.Equals(t, tc.err.Message, adminErr.Message)
  170. assert.Equals(t, tc.err.Detail, adminErr.Detail)
  171. assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
  172. return
  173. }
  174. prov := &linkedca.Provisioner{}
  175. err := readProtoJSON(res.Body, prov)
  176. assert.FatalError(t, err)
  177. assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
  178. opts := []cmp.Option{cmpopts.IgnoreUnexported(linkedca.Provisioner{}, timestamppb.Timestamp{})}
  179. if !cmp.Equal(tc.prov, prov, opts...) {
  180. t.Errorf("h.GetProvisioner diff =\n%s", cmp.Diff(tc.prov, prov, opts...))
  181. }
  182. })
  183. }
  184. }
  185. func TestHandler_GetProvisioners(t *testing.T) {
  186. type test struct {
  187. ctx context.Context
  188. auth adminAuthority
  189. req *http.Request
  190. statusCode int
  191. err *admin.Error
  192. resp GetProvisionersResponse
  193. }
  194. var tests = map[string]func(t *testing.T) test{
  195. "fail/parse-cursor": func(t *testing.T) test {
  196. req := httptest.NewRequest("GET", "/foo?limit=X", nil)
  197. return test{
  198. ctx: context.Background(),
  199. statusCode: 400,
  200. req: req,
  201. err: &admin.Error{
  202. Status: 400,
  203. Type: admin.ErrorBadRequestType.String(),
  204. Detail: "bad request",
  205. Message: "error parsing cursor and limit from query params: limit 'X' is not an integer: strconv.Atoi: parsing \"X\": invalid syntax",
  206. },
  207. }
  208. },
  209. "fail/auth.GetProvisioners": func(t *testing.T) test {
  210. req := httptest.NewRequest("GET", "/foo", nil)
  211. auth := &mockAdminAuthority{
  212. MockGetProvisioners: func(cursor string, limit int) (provisioner.List, string, error) {
  213. assert.Equals(t, "", cursor)
  214. assert.Equals(t, 0, limit)
  215. return nil, "", errors.New("force")
  216. },
  217. }
  218. return test{
  219. ctx: context.Background(),
  220. req: req,
  221. auth: auth,
  222. statusCode: 500,
  223. err: &admin.Error{
  224. Type: "",
  225. Status: 500,
  226. Detail: "",
  227. Message: "The certificate authority encountered an Internal Server Error. Please see the certificate authority logs for more info.",
  228. },
  229. }
  230. },
  231. "ok": func(t *testing.T) test {
  232. req := httptest.NewRequest("GET", "/foo", nil)
  233. provisioners := provisioner.List{
  234. &provisioner.OIDC{
  235. Type: "OIDC",
  236. Name: "oidcProv",
  237. },
  238. &provisioner.ACME{
  239. Type: "ACME",
  240. Name: "provName",
  241. ForceCN: false,
  242. RequireEAB: false,
  243. },
  244. }
  245. auth := &mockAdminAuthority{
  246. MockGetProvisioners: func(cursor string, limit int) (provisioner.List, string, error) {
  247. assert.Equals(t, "", cursor)
  248. assert.Equals(t, 0, limit)
  249. return provisioners, "nextCursorValue", nil
  250. },
  251. }
  252. return test{
  253. ctx: context.Background(),
  254. req: req,
  255. auth: auth,
  256. statusCode: 200,
  257. err: nil,
  258. resp: GetProvisionersResponse{
  259. Provisioners: provisioners,
  260. NextCursor: "nextCursorValue",
  261. },
  262. }
  263. },
  264. }
  265. for name, prep := range tests {
  266. tc := prep(t)
  267. t.Run(name, func(t *testing.T) {
  268. h := &Handler{
  269. auth: tc.auth,
  270. }
  271. req := tc.req.WithContext(tc.ctx)
  272. w := httptest.NewRecorder()
  273. h.GetProvisioners(w, req)
  274. res := w.Result()
  275. assert.Equals(t, tc.statusCode, res.StatusCode)
  276. if res.StatusCode >= 400 {
  277. body, err := io.ReadAll(res.Body)
  278. res.Body.Close()
  279. assert.FatalError(t, err)
  280. adminErr := admin.Error{}
  281. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &adminErr))
  282. assert.Equals(t, tc.err.Type, adminErr.Type)
  283. assert.Equals(t, tc.err.Message, adminErr.Message)
  284. assert.Equals(t, tc.err.Detail, adminErr.Detail)
  285. assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
  286. return
  287. }
  288. body, err := io.ReadAll(res.Body)
  289. res.Body.Close()
  290. assert.FatalError(t, err)
  291. response := GetProvisionersResponse{}
  292. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &response))
  293. assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
  294. opts := []cmp.Option{cmpopts.IgnoreUnexported(provisioner.ACME{}, provisioner.OIDC{})}
  295. if !cmp.Equal(tc.resp, response, opts...) {
  296. t.Errorf("h.GetProvisioners diff =\n%s", cmp.Diff(tc.resp, response, opts...))
  297. }
  298. })
  299. }
  300. }
  301. func TestHandler_CreateProvisioner(t *testing.T) {
  302. type test struct {
  303. ctx context.Context
  304. auth adminAuthority
  305. body []byte
  306. statusCode int
  307. err *admin.Error
  308. prov *linkedca.Provisioner
  309. }
  310. var tests = map[string]func(t *testing.T) test{
  311. "fail/readProtoJSON": func(t *testing.T) test {
  312. body := []byte("{!?}")
  313. return test{
  314. ctx: context.Background(),
  315. body: body,
  316. statusCode: 500,
  317. err: &admin.Error{ // TODO(hs): this probably needs a better error
  318. Type: "",
  319. Status: 500,
  320. Detail: "",
  321. Message: "",
  322. },
  323. }
  324. },
  325. // TODO(hs): ValidateClaims can't be mocked atm
  326. // "fail/authority.ValidateClaims": func(t *testing.T) test {
  327. // return test{}
  328. // },
  329. "fail/auth.StoreProvisioner": func(t *testing.T) test {
  330. prov := &linkedca.Provisioner{
  331. Id: "provID",
  332. Type: linkedca.Provisioner_OIDC,
  333. Name: "provName",
  334. }
  335. body, err := protojson.Marshal(prov)
  336. assert.FatalError(t, err)
  337. auth := &mockAdminAuthority{
  338. MockStoreProvisioner: func(ctx context.Context, prov *linkedca.Provisioner) error {
  339. assert.Equals(t, "provID", prov.Id)
  340. return errors.New("force")
  341. },
  342. }
  343. return test{
  344. ctx: context.Background(),
  345. body: body,
  346. auth: auth,
  347. statusCode: 500,
  348. err: &admin.Error{
  349. Type: admin.ErrorServerInternalType.String(),
  350. Status: 500,
  351. Detail: "the server experienced an internal error",
  352. Message: "error storing provisioner provName: force",
  353. },
  354. }
  355. },
  356. "ok": func(t *testing.T) test {
  357. prov := &linkedca.Provisioner{
  358. Id: "provID",
  359. Type: linkedca.Provisioner_OIDC,
  360. Name: "provName",
  361. }
  362. body, err := protojson.Marshal(prov)
  363. assert.FatalError(t, err)
  364. auth := &mockAdminAuthority{
  365. MockStoreProvisioner: func(ctx context.Context, prov *linkedca.Provisioner) error {
  366. assert.Equals(t, "provID", prov.Id)
  367. return nil
  368. },
  369. }
  370. return test{
  371. ctx: context.Background(),
  372. body: body,
  373. auth: auth,
  374. statusCode: 201,
  375. err: nil,
  376. prov: prov,
  377. }
  378. },
  379. }
  380. for name, prep := range tests {
  381. tc := prep(t)
  382. t.Run(name, func(t *testing.T) {
  383. h := &Handler{
  384. auth: tc.auth,
  385. }
  386. req := httptest.NewRequest("POST", "/foo", io.NopCloser(bytes.NewBuffer(tc.body)))
  387. req = req.WithContext(tc.ctx)
  388. w := httptest.NewRecorder()
  389. h.CreateProvisioner(w, req)
  390. res := w.Result()
  391. assert.Equals(t, tc.statusCode, res.StatusCode)
  392. if res.StatusCode >= 400 {
  393. body, err := io.ReadAll(res.Body)
  394. res.Body.Close()
  395. assert.FatalError(t, err)
  396. adminErr := admin.Error{}
  397. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &adminErr))
  398. assert.Equals(t, tc.err.Type, adminErr.Type)
  399. assert.Equals(t, tc.err.Message, adminErr.Message)
  400. assert.Equals(t, tc.err.Detail, adminErr.Detail)
  401. assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
  402. return
  403. }
  404. prov := &linkedca.Provisioner{}
  405. err := readProtoJSON(res.Body, prov)
  406. assert.FatalError(t, err)
  407. assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
  408. opts := []cmp.Option{cmpopts.IgnoreUnexported(linkedca.Provisioner{}, timestamppb.Timestamp{})}
  409. if !cmp.Equal(tc.prov, prov, opts...) {
  410. t.Errorf("linkedca.Admin diff =\n%s", cmp.Diff(tc.prov, prov, opts...))
  411. }
  412. })
  413. }
  414. }
  415. func TestHandler_DeleteProvisioner(t *testing.T) {
  416. type test struct {
  417. ctx context.Context
  418. auth adminAuthority
  419. req *http.Request
  420. statusCode int
  421. err *admin.Error
  422. }
  423. var tests = map[string]func(t *testing.T) test{
  424. "fail/auth.LoadProvisionerByID": func(t *testing.T) test {
  425. req := httptest.NewRequest("DELETE", "/foo?id=provID", nil)
  426. chiCtx := chi.NewRouteContext()
  427. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  428. auth := &mockAdminAuthority{
  429. MockLoadProvisionerByID: func(id string) (provisioner.Interface, error) {
  430. assert.Equals(t, "provID", id)
  431. return nil, errors.New("force")
  432. },
  433. }
  434. return test{
  435. ctx: ctx,
  436. req: req,
  437. auth: auth,
  438. statusCode: 500,
  439. err: &admin.Error{
  440. Type: admin.ErrorServerInternalType.String(),
  441. Status: 500,
  442. Detail: "the server experienced an internal error",
  443. Message: "error loading provisioner provID: force",
  444. },
  445. }
  446. },
  447. "fail/auth.LoadProvisionerByName": func(t *testing.T) test {
  448. req := httptest.NewRequest("DELETE", "/foo", nil)
  449. chiCtx := chi.NewRouteContext()
  450. chiCtx.URLParams.Add("name", "provName")
  451. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  452. auth := &mockAdminAuthority{
  453. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  454. assert.Equals(t, "provName", name)
  455. return nil, errors.New("force")
  456. },
  457. }
  458. return test{
  459. ctx: ctx,
  460. req: req,
  461. auth: auth,
  462. statusCode: 500,
  463. err: &admin.Error{
  464. Type: admin.ErrorServerInternalType.String(),
  465. Status: 500,
  466. Detail: "the server experienced an internal error",
  467. Message: "error loading provisioner provName: force",
  468. },
  469. }
  470. },
  471. "fail/auth.RemoveProvisioner": func(t *testing.T) test {
  472. req := httptest.NewRequest("DELETE", "/foo", nil)
  473. chiCtx := chi.NewRouteContext()
  474. chiCtx.URLParams.Add("name", "provName")
  475. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  476. auth := &mockAdminAuthority{
  477. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  478. assert.Equals(t, "provName", name)
  479. return &provisioner.OIDC{
  480. ID: "provID",
  481. Name: "provName",
  482. Type: "OIDC",
  483. }, nil
  484. },
  485. MockRemoveProvisioner: func(ctx context.Context, id string) error {
  486. assert.Equals(t, "provID", id)
  487. return errors.New("force")
  488. },
  489. }
  490. return test{
  491. ctx: ctx,
  492. req: req,
  493. auth: auth,
  494. statusCode: 500,
  495. err: &admin.Error{
  496. Type: admin.ErrorServerInternalType.String(),
  497. Status: 500,
  498. Detail: "the server experienced an internal error",
  499. Message: "error removing provisioner provName: force",
  500. },
  501. }
  502. },
  503. "ok": func(t *testing.T) test {
  504. req := httptest.NewRequest("DELETE", "/foo", nil)
  505. chiCtx := chi.NewRouteContext()
  506. chiCtx.URLParams.Add("name", "provName")
  507. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  508. auth := &mockAdminAuthority{
  509. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  510. assert.Equals(t, "provName", name)
  511. return &provisioner.OIDC{
  512. ID: "provID",
  513. Name: "provName",
  514. Type: "OIDC",
  515. }, nil
  516. },
  517. MockRemoveProvisioner: func(ctx context.Context, id string) error {
  518. assert.Equals(t, "provID", id)
  519. return nil
  520. },
  521. }
  522. return test{
  523. ctx: ctx,
  524. req: req,
  525. auth: auth,
  526. statusCode: 200,
  527. err: nil,
  528. }
  529. },
  530. }
  531. for name, prep := range tests {
  532. tc := prep(t)
  533. t.Run(name, func(t *testing.T) {
  534. h := &Handler{
  535. auth: tc.auth,
  536. }
  537. req := tc.req.WithContext(tc.ctx)
  538. w := httptest.NewRecorder()
  539. h.DeleteProvisioner(w, req)
  540. res := w.Result()
  541. assert.Equals(t, tc.statusCode, res.StatusCode)
  542. if res.StatusCode >= 400 {
  543. body, err := io.ReadAll(res.Body)
  544. res.Body.Close()
  545. assert.FatalError(t, err)
  546. adminErr := admin.Error{}
  547. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &adminErr))
  548. assert.Equals(t, tc.err.Type, adminErr.Type)
  549. assert.Equals(t, tc.err.Message, adminErr.Message)
  550. assert.Equals(t, tc.err.Detail, adminErr.Detail)
  551. assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
  552. return
  553. }
  554. body, err := io.ReadAll(res.Body)
  555. res.Body.Close()
  556. assert.FatalError(t, err)
  557. response := DeleteResponse{}
  558. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &response))
  559. assert.Equals(t, "ok", response.Status)
  560. assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
  561. })
  562. }
  563. }
  564. func TestHandler_UpdateProvisioner(t *testing.T) {
  565. type test struct {
  566. ctx context.Context
  567. auth adminAuthority
  568. body []byte
  569. adminDB admin.DB
  570. statusCode int
  571. err *admin.Error
  572. prov *linkedca.Provisioner
  573. }
  574. var tests = map[string]func(t *testing.T) test{
  575. "fail/readProtoJSON": func(t *testing.T) test {
  576. body := []byte("{!?}")
  577. return test{
  578. ctx: context.Background(),
  579. body: body,
  580. statusCode: 500,
  581. err: &admin.Error{ // TODO(hs): this probably needs a better error
  582. Type: "",
  583. Status: 500,
  584. Detail: "",
  585. Message: "",
  586. },
  587. }
  588. },
  589. "fail/auth.LoadProvisionerByName": func(t *testing.T) test {
  590. chiCtx := chi.NewRouteContext()
  591. chiCtx.URLParams.Add("name", "provName")
  592. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  593. prov := &linkedca.Provisioner{
  594. Id: "provID",
  595. Type: linkedca.Provisioner_OIDC,
  596. Name: "provName",
  597. }
  598. body, err := protojson.Marshal(prov)
  599. assert.FatalError(t, err)
  600. auth := &mockAdminAuthority{
  601. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  602. assert.Equals(t, "provName", name)
  603. return nil, errors.New("force")
  604. },
  605. }
  606. return test{
  607. ctx: ctx,
  608. body: body,
  609. auth: auth,
  610. statusCode: 500,
  611. err: &admin.Error{
  612. Type: admin.ErrorServerInternalType.String(),
  613. Status: 500,
  614. Detail: "the server experienced an internal error",
  615. Message: "error loading provisioner from cached configuration 'provName': force",
  616. },
  617. }
  618. },
  619. "fail/db.GetProvisioner": func(t *testing.T) test {
  620. chiCtx := chi.NewRouteContext()
  621. chiCtx.URLParams.Add("name", "provName")
  622. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  623. prov := &linkedca.Provisioner{
  624. Id: "provID",
  625. Type: linkedca.Provisioner_OIDC,
  626. Name: "provName",
  627. }
  628. body, err := protojson.Marshal(prov)
  629. assert.FatalError(t, err)
  630. auth := &mockAdminAuthority{
  631. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  632. assert.Equals(t, "provName", name)
  633. return &provisioner.OIDC{
  634. ID: "provID",
  635. Name: "provName",
  636. }, nil
  637. },
  638. }
  639. db := &admin.MockDB{
  640. MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  641. assert.Equals(t, "provID", id)
  642. return nil, errors.New("force")
  643. },
  644. }
  645. return test{
  646. ctx: ctx,
  647. body: body,
  648. auth: auth,
  649. adminDB: db,
  650. statusCode: 500,
  651. err: &admin.Error{
  652. Type: admin.ErrorServerInternalType.String(),
  653. Status: 500,
  654. Detail: "the server experienced an internal error",
  655. Message: "error loading provisioner from db 'provID': force",
  656. },
  657. }
  658. },
  659. "fail/change-id-error": func(t *testing.T) test {
  660. chiCtx := chi.NewRouteContext()
  661. chiCtx.URLParams.Add("name", "provName")
  662. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  663. prov := &linkedca.Provisioner{
  664. Id: "differentProvID",
  665. Type: linkedca.Provisioner_OIDC,
  666. Name: "provName",
  667. }
  668. body, err := protojson.Marshal(prov)
  669. assert.FatalError(t, err)
  670. auth := &mockAdminAuthority{
  671. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  672. assert.Equals(t, "provName", name)
  673. return &provisioner.OIDC{
  674. ID: "provID",
  675. Name: "provName",
  676. }, nil
  677. },
  678. }
  679. db := &admin.MockDB{
  680. MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  681. assert.Equals(t, "provID", id)
  682. return &linkedca.Provisioner{
  683. Id: "provID",
  684. Name: "provName",
  685. }, nil
  686. },
  687. }
  688. return test{
  689. ctx: ctx,
  690. body: body,
  691. auth: auth,
  692. adminDB: db,
  693. statusCode: 500,
  694. err: &admin.Error{
  695. Type: admin.ErrorServerInternalType.String(),
  696. Status: 500,
  697. Detail: "the server experienced an internal error",
  698. Message: "cannot change provisioner ID",
  699. },
  700. }
  701. },
  702. "fail/change-type-error": func(t *testing.T) test {
  703. chiCtx := chi.NewRouteContext()
  704. chiCtx.URLParams.Add("name", "provName")
  705. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  706. prov := &linkedca.Provisioner{
  707. Id: "provID",
  708. Type: linkedca.Provisioner_JWK,
  709. Name: "provName",
  710. }
  711. body, err := protojson.Marshal(prov)
  712. assert.FatalError(t, err)
  713. auth := &mockAdminAuthority{
  714. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  715. assert.Equals(t, "provName", name)
  716. return &provisioner.OIDC{
  717. ID: "provID",
  718. Name: "provName",
  719. }, nil
  720. },
  721. }
  722. db := &admin.MockDB{
  723. MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  724. assert.Equals(t, "provID", id)
  725. return &linkedca.Provisioner{
  726. Id: "provID",
  727. Name: "provName",
  728. Type: linkedca.Provisioner_OIDC,
  729. }, nil
  730. },
  731. }
  732. return test{
  733. ctx: ctx,
  734. body: body,
  735. auth: auth,
  736. adminDB: db,
  737. statusCode: 500,
  738. err: &admin.Error{
  739. Type: admin.ErrorServerInternalType.String(),
  740. Status: 500,
  741. Detail: "the server experienced an internal error",
  742. Message: "cannot change provisioner type",
  743. },
  744. }
  745. },
  746. "fail/change-authority-id-error": func(t *testing.T) test {
  747. chiCtx := chi.NewRouteContext()
  748. chiCtx.URLParams.Add("name", "provName")
  749. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  750. prov := &linkedca.Provisioner{
  751. Id: "provID",
  752. Type: linkedca.Provisioner_OIDC,
  753. Name: "provName",
  754. AuthorityId: "differentAuthorityID",
  755. }
  756. body, err := protojson.Marshal(prov)
  757. assert.FatalError(t, err)
  758. auth := &mockAdminAuthority{
  759. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  760. assert.Equals(t, "provName", name)
  761. return &provisioner.OIDC{
  762. ID: "provID",
  763. Name: "provName",
  764. }, nil
  765. },
  766. }
  767. db := &admin.MockDB{
  768. MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  769. assert.Equals(t, "provID", id)
  770. return &linkedca.Provisioner{
  771. Id: "provID",
  772. Name: "provName",
  773. Type: linkedca.Provisioner_OIDC,
  774. AuthorityId: "authorityID",
  775. }, nil
  776. },
  777. }
  778. return test{
  779. ctx: ctx,
  780. body: body,
  781. auth: auth,
  782. adminDB: db,
  783. statusCode: 500,
  784. err: &admin.Error{
  785. Type: admin.ErrorServerInternalType.String(),
  786. Status: 500,
  787. Detail: "the server experienced an internal error",
  788. Message: "cannot change provisioner authorityID",
  789. },
  790. }
  791. },
  792. "fail/change-createdAt-error": func(t *testing.T) test {
  793. chiCtx := chi.NewRouteContext()
  794. chiCtx.URLParams.Add("name", "provName")
  795. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  796. createdAt := time.Now()
  797. prov := &linkedca.Provisioner{
  798. Id: "provID",
  799. Type: linkedca.Provisioner_OIDC,
  800. Name: "provName",
  801. AuthorityId: "authorityID",
  802. CreatedAt: timestamppb.New(time.Now().Add(-1 * time.Hour)),
  803. }
  804. body, err := protojson.Marshal(prov)
  805. assert.FatalError(t, err)
  806. auth := &mockAdminAuthority{
  807. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  808. assert.Equals(t, "provName", name)
  809. return &provisioner.OIDC{
  810. ID: "provID",
  811. Name: "provName",
  812. }, nil
  813. },
  814. }
  815. db := &admin.MockDB{
  816. MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  817. assert.Equals(t, "provID", id)
  818. return &linkedca.Provisioner{
  819. Id: "provID",
  820. Name: "provName",
  821. Type: linkedca.Provisioner_OIDC,
  822. AuthorityId: "authorityID",
  823. CreatedAt: timestamppb.New(createdAt),
  824. }, nil
  825. },
  826. }
  827. return test{
  828. ctx: ctx,
  829. body: body,
  830. auth: auth,
  831. adminDB: db,
  832. statusCode: 500,
  833. err: &admin.Error{
  834. Type: admin.ErrorServerInternalType.String(),
  835. Status: 500,
  836. Detail: "the server experienced an internal error",
  837. Message: "cannot change provisioner createdAt",
  838. },
  839. }
  840. },
  841. "fail/change-deletedAt-error": func(t *testing.T) test {
  842. chiCtx := chi.NewRouteContext()
  843. chiCtx.URLParams.Add("name", "provName")
  844. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  845. createdAt := time.Now()
  846. var deletedAt time.Time
  847. prov := &linkedca.Provisioner{
  848. Id: "provID",
  849. Type: linkedca.Provisioner_OIDC,
  850. Name: "provName",
  851. AuthorityId: "authorityID",
  852. CreatedAt: timestamppb.New(createdAt),
  853. DeletedAt: timestamppb.New(time.Now()),
  854. }
  855. body, err := protojson.Marshal(prov)
  856. assert.FatalError(t, err)
  857. auth := &mockAdminAuthority{
  858. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  859. assert.Equals(t, "provName", name)
  860. return &provisioner.OIDC{
  861. ID: "provID",
  862. Name: "provName",
  863. }, nil
  864. },
  865. }
  866. db := &admin.MockDB{
  867. MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  868. assert.Equals(t, "provID", id)
  869. return &linkedca.Provisioner{
  870. Id: "provID",
  871. Name: "provName",
  872. Type: linkedca.Provisioner_OIDC,
  873. AuthorityId: "authorityID",
  874. CreatedAt: timestamppb.New(createdAt),
  875. DeletedAt: timestamppb.New(deletedAt),
  876. }, nil
  877. },
  878. }
  879. return test{
  880. ctx: ctx,
  881. body: body,
  882. auth: auth,
  883. adminDB: db,
  884. statusCode: 500,
  885. err: &admin.Error{
  886. Type: admin.ErrorServerInternalType.String(),
  887. Status: 500,
  888. Detail: "the server experienced an internal error",
  889. Message: "cannot change provisioner deletedAt",
  890. },
  891. }
  892. },
  893. // TODO(hs): ValidateClaims can't be mocked atm
  894. //"fail/ValidateClaims": func(t *testing.T) test { return test{} },
  895. "fail/auth.UpdateProvisioner": func(t *testing.T) test {
  896. chiCtx := chi.NewRouteContext()
  897. chiCtx.URLParams.Add("name", "provName")
  898. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  899. createdAt := time.Now()
  900. var deletedAt time.Time
  901. prov := &linkedca.Provisioner{
  902. Id: "provID",
  903. Type: linkedca.Provisioner_OIDC,
  904. Name: "provName",
  905. AuthorityId: "authorityID",
  906. CreatedAt: timestamppb.New(createdAt),
  907. DeletedAt: timestamppb.New(deletedAt),
  908. }
  909. body, err := protojson.Marshal(prov)
  910. assert.FatalError(t, err)
  911. auth := &mockAdminAuthority{
  912. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  913. assert.Equals(t, "provName", name)
  914. return &provisioner.OIDC{
  915. ID: "provID",
  916. Name: "provName",
  917. }, nil
  918. },
  919. MockUpdateProvisioner: func(ctx context.Context, nu *linkedca.Provisioner) error {
  920. assert.Equals(t, "provID", nu.Id)
  921. assert.Equals(t, "provName", nu.Name)
  922. return errors.New("force")
  923. },
  924. }
  925. db := &admin.MockDB{
  926. MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  927. assert.Equals(t, "provID", id)
  928. return &linkedca.Provisioner{
  929. Id: "provID",
  930. Name: "provName",
  931. Type: linkedca.Provisioner_OIDC,
  932. AuthorityId: "authorityID",
  933. CreatedAt: timestamppb.New(createdAt),
  934. DeletedAt: timestamppb.New(deletedAt),
  935. }, nil
  936. },
  937. }
  938. return test{
  939. ctx: ctx,
  940. body: body,
  941. auth: auth,
  942. adminDB: db,
  943. statusCode: 500,
  944. err: &admin.Error{
  945. Type: "", // TODO(hs): this error can be improved
  946. Status: 500,
  947. Detail: "",
  948. Message: "",
  949. },
  950. }
  951. },
  952. "ok": func(t *testing.T) test {
  953. chiCtx := chi.NewRouteContext()
  954. chiCtx.URLParams.Add("name", "provName")
  955. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  956. createdAt := time.Now()
  957. var deletedAt time.Time
  958. prov := &linkedca.Provisioner{
  959. Id: "provID",
  960. Type: linkedca.Provisioner_OIDC,
  961. Name: "provName",
  962. AuthorityId: "authorityID",
  963. CreatedAt: timestamppb.New(createdAt),
  964. DeletedAt: timestamppb.New(deletedAt),
  965. Details: &linkedca.ProvisionerDetails{
  966. Data: &linkedca.ProvisionerDetails_OIDC{
  967. OIDC: &linkedca.OIDCProvisioner{
  968. ClientId: "new-client-id",
  969. ClientSecret: "new-client-secret",
  970. },
  971. },
  972. },
  973. }
  974. body, err := protojson.Marshal(prov)
  975. assert.FatalError(t, err)
  976. auth := &mockAdminAuthority{
  977. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  978. assert.Equals(t, "provName", name)
  979. return &provisioner.OIDC{
  980. ID: "provID",
  981. Name: "provName",
  982. }, nil
  983. },
  984. MockUpdateProvisioner: func(ctx context.Context, nu *linkedca.Provisioner) error {
  985. assert.Equals(t, "provID", nu.Id)
  986. assert.Equals(t, "provName", nu.Name)
  987. return nil
  988. },
  989. }
  990. db := &admin.MockDB{
  991. MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  992. assert.Equals(t, "provID", id)
  993. return &linkedca.Provisioner{
  994. Id: "provID",
  995. Name: "provName",
  996. Type: linkedca.Provisioner_OIDC,
  997. AuthorityId: "authorityID",
  998. CreatedAt: timestamppb.New(createdAt),
  999. DeletedAt: timestamppb.New(deletedAt),
  1000. }, nil
  1001. },
  1002. }
  1003. return test{
  1004. ctx: ctx,
  1005. body: body,
  1006. auth: auth,
  1007. adminDB: db,
  1008. statusCode: 200,
  1009. prov: prov,
  1010. }
  1011. },
  1012. }
  1013. for name, prep := range tests {
  1014. tc := prep(t)
  1015. t.Run(name, func(t *testing.T) {
  1016. h := &Handler{
  1017. auth: tc.auth,
  1018. adminDB: tc.adminDB,
  1019. }
  1020. req := httptest.NewRequest("POST", "/foo", io.NopCloser(bytes.NewBuffer(tc.body)))
  1021. req = req.WithContext(tc.ctx)
  1022. w := httptest.NewRecorder()
  1023. h.UpdateProvisioner(w, req)
  1024. res := w.Result()
  1025. assert.Equals(t, tc.statusCode, res.StatusCode)
  1026. if res.StatusCode >= 400 {
  1027. body, err := io.ReadAll(res.Body)
  1028. res.Body.Close()
  1029. assert.FatalError(t, err)
  1030. adminErr := admin.Error{}
  1031. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &adminErr))
  1032. assert.Equals(t, tc.err.Type, adminErr.Type)
  1033. assert.Equals(t, tc.err.Message, adminErr.Message)
  1034. assert.Equals(t, tc.err.Detail, adminErr.Detail)
  1035. assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
  1036. return
  1037. }
  1038. prov := &linkedca.Provisioner{}
  1039. err := readProtoJSON(res.Body, prov)
  1040. assert.FatalError(t, err)
  1041. assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
  1042. opts := []cmp.Option{
  1043. cmpopts.IgnoreUnexported(
  1044. linkedca.Provisioner{}, linkedca.ProvisionerDetails{}, linkedca.ProvisionerDetails_OIDC{},
  1045. linkedca.OIDCProvisioner{}, timestamppb.Timestamp{},
  1046. ),
  1047. }
  1048. if !cmp.Equal(tc.prov, prov, opts...) {
  1049. t.Errorf("linkedca.Admin diff =\n%s", cmp.Diff(tc.prov, prov, opts...))
  1050. }
  1051. })
  1052. }
  1053. }