provisioner_test.go 36 KB

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