provisioner_test.go 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  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. <<<<<<< HEAD
  934. Template: []byte("{ {{ missingFunction }} }"),
  935. =======
  936. Template: []byte("{!?}"),
  937. >>>>>>> 440ac4b2 (Add provisioner template validation)
  938. },
  939. }
  940. body, err := protojson.Marshal(prov)
  941. assert.FatalError(t, err)
  942. auth := &mockAdminAuthority{
  943. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  944. assert.Equals(t, "provName", name)
  945. return &provisioner.OIDC{
  946. ID: "provID",
  947. Name: "provName",
  948. }, nil
  949. },
  950. }
  951. db := &admin.MockDB{
  952. MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  953. assert.Equals(t, "provID", id)
  954. return &linkedca.Provisioner{
  955. Id: "provID",
  956. Name: "provName",
  957. Type: linkedca.Provisioner_OIDC,
  958. AuthorityId: "authorityID",
  959. CreatedAt: timestamppb.New(createdAt),
  960. DeletedAt: timestamppb.New(deletedAt),
  961. }, nil
  962. },
  963. }
  964. return test{
  965. ctx: ctx,
  966. body: body,
  967. auth: auth,
  968. adminDB: db,
  969. statusCode: 400,
  970. err: &admin.Error{
  971. Type: "badRequest",
  972. Status: 400,
  973. Detail: "bad request",
  974. <<<<<<< HEAD
  975. Message: "invalid template: invalid X.509 template: error parsing template: template: template:1: function \"missingFunction\" not defined",
  976. =======
  977. Message: "invalid template: invalid X.509 template: invalid JSON: invalid character '!' looking for beginning of object key string",
  978. >>>>>>> 440ac4b2 (Add provisioner template validation)
  979. },
  980. }
  981. },
  982. "fail/auth.UpdateProvisioner": func(t *testing.T) test {
  983. chiCtx := chi.NewRouteContext()
  984. chiCtx.URLParams.Add("name", "provName")
  985. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  986. createdAt := time.Now()
  987. var deletedAt time.Time
  988. prov := &linkedca.Provisioner{
  989. Id: "provID",
  990. Type: linkedca.Provisioner_OIDC,
  991. Name: "provName",
  992. AuthorityId: "authorityID",
  993. CreatedAt: timestamppb.New(createdAt),
  994. DeletedAt: timestamppb.New(deletedAt),
  995. }
  996. body, err := protojson.Marshal(prov)
  997. assert.FatalError(t, err)
  998. auth := &mockAdminAuthority{
  999. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  1000. assert.Equals(t, "provName", name)
  1001. return &provisioner.OIDC{
  1002. ID: "provID",
  1003. Name: "provName",
  1004. }, nil
  1005. },
  1006. MockUpdateProvisioner: func(ctx context.Context, nu *linkedca.Provisioner) error {
  1007. assert.Equals(t, "provID", nu.Id)
  1008. assert.Equals(t, "provName", nu.Name)
  1009. return errors.New("force")
  1010. },
  1011. }
  1012. db := &admin.MockDB{
  1013. MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  1014. assert.Equals(t, "provID", id)
  1015. return &linkedca.Provisioner{
  1016. Id: "provID",
  1017. Name: "provName",
  1018. Type: linkedca.Provisioner_OIDC,
  1019. AuthorityId: "authorityID",
  1020. CreatedAt: timestamppb.New(createdAt),
  1021. DeletedAt: timestamppb.New(deletedAt),
  1022. }, nil
  1023. },
  1024. }
  1025. return test{
  1026. ctx: ctx,
  1027. body: body,
  1028. auth: auth,
  1029. adminDB: db,
  1030. statusCode: 500,
  1031. err: &admin.Error{
  1032. Type: "", // TODO(hs): this error can be improved
  1033. Status: 500,
  1034. Detail: "",
  1035. Message: "",
  1036. },
  1037. }
  1038. },
  1039. "ok": func(t *testing.T) test {
  1040. chiCtx := chi.NewRouteContext()
  1041. chiCtx.URLParams.Add("name", "provName")
  1042. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  1043. createdAt := time.Now()
  1044. var deletedAt time.Time
  1045. prov := &linkedca.Provisioner{
  1046. Id: "provID",
  1047. Type: linkedca.Provisioner_OIDC,
  1048. Name: "provName",
  1049. AuthorityId: "authorityID",
  1050. CreatedAt: timestamppb.New(createdAt),
  1051. DeletedAt: timestamppb.New(deletedAt),
  1052. Details: &linkedca.ProvisionerDetails{
  1053. Data: &linkedca.ProvisionerDetails_OIDC{
  1054. OIDC: &linkedca.OIDCProvisioner{
  1055. ClientId: "new-client-id",
  1056. ClientSecret: "new-client-secret",
  1057. },
  1058. },
  1059. },
  1060. }
  1061. body, err := protojson.Marshal(prov)
  1062. assert.FatalError(t, err)
  1063. auth := &mockAdminAuthority{
  1064. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  1065. assert.Equals(t, "provName", name)
  1066. return &provisioner.OIDC{
  1067. ID: "provID",
  1068. Name: "provName",
  1069. }, nil
  1070. },
  1071. MockUpdateProvisioner: func(ctx context.Context, nu *linkedca.Provisioner) error {
  1072. assert.Equals(t, "provID", nu.Id)
  1073. assert.Equals(t, "provName", nu.Name)
  1074. return nil
  1075. },
  1076. }
  1077. db := &admin.MockDB{
  1078. MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  1079. assert.Equals(t, "provID", id)
  1080. return &linkedca.Provisioner{
  1081. Id: "provID",
  1082. Name: "provName",
  1083. Type: linkedca.Provisioner_OIDC,
  1084. AuthorityId: "authorityID",
  1085. CreatedAt: timestamppb.New(createdAt),
  1086. DeletedAt: timestamppb.New(deletedAt),
  1087. }, nil
  1088. },
  1089. }
  1090. return test{
  1091. ctx: ctx,
  1092. body: body,
  1093. auth: auth,
  1094. adminDB: db,
  1095. statusCode: 200,
  1096. prov: prov,
  1097. }
  1098. },
  1099. }
  1100. for name, prep := range tests {
  1101. tc := prep(t)
  1102. t.Run(name, func(t *testing.T) {
  1103. mockMustAuthority(t, tc.auth)
  1104. ctx := admin.NewContext(tc.ctx, tc.adminDB)
  1105. req := httptest.NewRequest("POST", "/foo", io.NopCloser(bytes.NewBuffer(tc.body)))
  1106. req = req.WithContext(ctx)
  1107. w := httptest.NewRecorder()
  1108. UpdateProvisioner(w, req)
  1109. res := w.Result()
  1110. assert.Equals(t, tc.statusCode, res.StatusCode)
  1111. if res.StatusCode >= 400 {
  1112. body, err := io.ReadAll(res.Body)
  1113. res.Body.Close()
  1114. assert.FatalError(t, err)
  1115. adminErr := admin.Error{}
  1116. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &adminErr))
  1117. assert.Equals(t, tc.err.Type, adminErr.Type)
  1118. assert.Equals(t, tc.err.Detail, adminErr.Detail)
  1119. assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
  1120. if strings.HasPrefix(tc.err.Message, "proto:") {
  1121. assert.True(t, strings.Contains(adminErr.Message, "syntax error"))
  1122. } else {
  1123. assert.Equals(t, tc.err.Message, adminErr.Message)
  1124. }
  1125. return
  1126. }
  1127. prov := &linkedca.Provisioner{}
  1128. err := readProtoJSON(res.Body, prov)
  1129. assert.FatalError(t, err)
  1130. assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
  1131. opts := []cmp.Option{
  1132. cmpopts.IgnoreUnexported(
  1133. linkedca.Provisioner{}, linkedca.ProvisionerDetails{}, linkedca.ProvisionerDetails_OIDC{},
  1134. linkedca.OIDCProvisioner{}, timestamppb.Timestamp{},
  1135. ),
  1136. }
  1137. if !cmp.Equal(tc.prov, prov, opts...) {
  1138. t.Errorf("linkedca.Admin diff =\n%s", cmp.Diff(tc.prov, prov, opts...))
  1139. }
  1140. })
  1141. }
  1142. }
  1143. func Test_validateTemplates(t *testing.T) {
  1144. type args struct {
  1145. x509 *linkedca.Template
  1146. ssh *linkedca.Template
  1147. }
  1148. tests := []struct {
  1149. name string
  1150. args args
  1151. err error
  1152. }{
  1153. {
  1154. name: "ok",
  1155. args: args{},
  1156. err: nil,
  1157. },
  1158. {
  1159. name: "ok/x509",
  1160. args: args{
  1161. x509: &linkedca.Template{
  1162. Template: []byte(`{"x": 1}`),
  1163. },
  1164. },
  1165. err: nil,
  1166. },
  1167. {
  1168. name: "ok/ssh",
  1169. args: args{
  1170. ssh: &linkedca.Template{
  1171. Template: []byte(`{"x": 1}`),
  1172. },
  1173. },
  1174. err: nil,
  1175. },
  1176. {
  1177. <<<<<<< HEAD
  1178. name: "fail/x509-template-missing-quote",
  1179. args: args{
  1180. x509: &linkedca.Template{
  1181. Template: []byte(`{ {{printf "%q" "quoted}} }`),
  1182. },
  1183. },
  1184. err: errors.New("invalid X.509 template: error parsing template: template: template:1: unterminated quoted string"),
  1185. =======
  1186. name: "fail/x509-template-trailing-comma",
  1187. args: args{
  1188. x509: &linkedca.Template{
  1189. Template: []byte(`{"x": 1,}`),
  1190. },
  1191. },
  1192. err: errors.New("invalid X.509 template: invalid JSON: invalid character '}' looking for beginning of object key string"),
  1193. >>>>>>> 440ac4b2 (Add provisioner template validation)
  1194. },
  1195. {
  1196. name: "fail/x509-template-data",
  1197. args: args{
  1198. x509: &linkedca.Template{
  1199. Data: []byte(`{!?}`),
  1200. },
  1201. },
  1202. <<<<<<< HEAD
  1203. err: errors.New("invalid X.509 template data: error validating json template data"),
  1204. },
  1205. {
  1206. name: "fail/ssh-template-unknown-function",
  1207. args: args{
  1208. ssh: &linkedca.Template{
  1209. Template: []byte(`{ {{unknownFunction "foo"}} }`),
  1210. },
  1211. },
  1212. err: errors.New("invalid SSH template: error parsing template: template: template:1: function \"unknownFunction\" not defined"),
  1213. =======
  1214. err: errors.New("invalid X.509 template data: invalid JSON: invalid character '!' looking for beginning of object key string"),
  1215. },
  1216. {
  1217. name: "fail/ssh-template-trailing-comma",
  1218. args: args{
  1219. ssh: &linkedca.Template{
  1220. Template: []byte(`{"x": 1,}`),
  1221. },
  1222. },
  1223. err: errors.New("invalid SSH template: invalid JSON: invalid character '}' looking for beginning of object key string"),
  1224. >>>>>>> 440ac4b2 (Add provisioner template validation)
  1225. },
  1226. {
  1227. name: "fail/ssh-template-data",
  1228. args: args{
  1229. ssh: &linkedca.Template{
  1230. Data: []byte(`{!?}`),
  1231. },
  1232. },
  1233. <<<<<<< HEAD
  1234. err: errors.New("invalid SSH template data: error validating json template data"),
  1235. =======
  1236. err: errors.New("invalid SSH template data: invalid JSON: invalid character '!' looking for beginning of object key string"),
  1237. >>>>>>> 440ac4b2 (Add provisioner template validation)
  1238. },
  1239. }
  1240. for _, tt := range tests {
  1241. t.Run(tt.name, func(t *testing.T) {
  1242. err := validateTemplates(tt.args.x509, tt.args.ssh)
  1243. if tt.err != nil {
  1244. assert.Error(t, err)
  1245. assert.Equals(t, tt.err.Error(), err.Error())
  1246. return
  1247. }
  1248. assert.Nil(t, err)
  1249. })
  1250. }
  1251. }