provisioner_test.go 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  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/v5"
  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. "github.com/smallstep/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", http.NoBody)
  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", http.NoBody)
  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", http.NoBody)
  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", http.NoBody)
  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", http.NoBody)
  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", http.NoBody)
  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", http.NoBody)
  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/validateProvisioner": func(t *testing.T) test {
  329. prov := &linkedca.Provisioner{
  330. Id: "provID",
  331. Type: linkedca.Provisioner_OIDC,
  332. Name: "provName",
  333. Details: &linkedca.ProvisionerDetails{Data: &linkedca.ProvisionerDetails_JWK{
  334. JWK: &linkedca.JWKProvisioner{PublicKey: testJWKPublicKey(t)},
  335. }},
  336. }
  337. body, err := protojson.Marshal(prov)
  338. assert.FatalError(t, err)
  339. return test{
  340. ctx: context.Background(),
  341. body: body,
  342. auth: &mockAdminAuthority{},
  343. statusCode: 400,
  344. err: &admin.Error{
  345. Type: "badRequest",
  346. Status: 400,
  347. Detail: "bad request",
  348. Message: "provisioner details (JWK) do not match provisioner type (OIDC)",
  349. },
  350. }
  351. },
  352. "fail/validateTemplates": func(t *testing.T) test {
  353. prov := &linkedca.Provisioner{
  354. Id: "provID",
  355. Type: linkedca.Provisioner_OIDC,
  356. Name: "provName",
  357. Details: &linkedca.ProvisionerDetails{Data: &linkedca.ProvisionerDetails_OIDC{
  358. OIDC: &linkedca.OIDCProvisioner{
  359. ClientId: "client-id",
  360. ConfigurationEndpoint: "https://example.com/.well-known/openid-configuration",
  361. },
  362. }},
  363. X509Template: &linkedca.Template{
  364. Template: []byte(`{ {{missingFunction "foo"}} }`),
  365. },
  366. }
  367. body, err := protojson.Marshal(prov)
  368. assert.FatalError(t, err)
  369. return test{
  370. ctx: context.Background(),
  371. body: body,
  372. statusCode: 400,
  373. err: &admin.Error{
  374. Type: "badRequest",
  375. Status: 400,
  376. Detail: "bad request",
  377. Message: "invalid template: invalid X.509 template: error parsing template: template: template:1: function \"missingFunction\" not defined",
  378. },
  379. }
  380. },
  381. "fail/auth.StoreProvisioner": func(t *testing.T) test {
  382. prov := &linkedca.Provisioner{
  383. Id: "provID",
  384. Type: linkedca.Provisioner_OIDC,
  385. Name: "provName",
  386. Details: &linkedca.ProvisionerDetails{Data: &linkedca.ProvisionerDetails_OIDC{
  387. OIDC: &linkedca.OIDCProvisioner{
  388. ClientId: "client-id",
  389. ConfigurationEndpoint: "https://example.com/.well-known/openid-configuration",
  390. },
  391. }},
  392. }
  393. body, err := protojson.Marshal(prov)
  394. assert.FatalError(t, err)
  395. auth := &mockAdminAuthority{
  396. MockStoreProvisioner: func(ctx context.Context, prov *linkedca.Provisioner) error {
  397. assert.Equals(t, "provID", prov.Id)
  398. return errors.New("force")
  399. },
  400. }
  401. return test{
  402. ctx: context.Background(),
  403. body: body,
  404. auth: auth,
  405. statusCode: 500,
  406. err: &admin.Error{
  407. Type: admin.ErrorServerInternalType.String(),
  408. Status: 500,
  409. Detail: "the server experienced an internal error",
  410. Message: "error storing provisioner provName: force",
  411. },
  412. }
  413. },
  414. "ok": func(t *testing.T) test {
  415. prov := &linkedca.Provisioner{
  416. Id: "provID",
  417. Type: linkedca.Provisioner_OIDC,
  418. Name: "provName",
  419. Details: &linkedca.ProvisionerDetails{Data: &linkedca.ProvisionerDetails_OIDC{
  420. OIDC: &linkedca.OIDCProvisioner{
  421. ClientId: "client-id",
  422. ConfigurationEndpoint: "https://example.com/.well-known/openid-configuration",
  423. },
  424. }},
  425. }
  426. body, err := protojson.Marshal(prov)
  427. assert.FatalError(t, err)
  428. auth := &mockAdminAuthority{
  429. MockStoreProvisioner: func(ctx context.Context, prov *linkedca.Provisioner) error {
  430. assert.Equals(t, "provID", prov.Id)
  431. return nil
  432. },
  433. }
  434. return test{
  435. ctx: context.Background(),
  436. body: body,
  437. auth: auth,
  438. statusCode: 201,
  439. err: nil,
  440. prov: prov,
  441. }
  442. },
  443. }
  444. for name, prep := range tests {
  445. tc := prep(t)
  446. t.Run(name, func(t *testing.T) {
  447. mockMustAuthority(t, tc.auth)
  448. req := httptest.NewRequest("POST", "/foo", io.NopCloser(bytes.NewBuffer(tc.body)))
  449. req = req.WithContext(tc.ctx)
  450. w := httptest.NewRecorder()
  451. CreateProvisioner(w, req)
  452. res := w.Result()
  453. assert.Equals(t, tc.statusCode, res.StatusCode)
  454. if res.StatusCode >= 400 {
  455. body, err := io.ReadAll(res.Body)
  456. res.Body.Close()
  457. assert.FatalError(t, err)
  458. adminErr := admin.Error{}
  459. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &adminErr))
  460. assert.Equals(t, tc.err.Type, adminErr.Type)
  461. assert.Equals(t, tc.err.Detail, adminErr.Detail)
  462. assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
  463. if strings.HasPrefix(tc.err.Message, "proto:") {
  464. assert.True(t, strings.Contains(adminErr.Message, "syntax error"))
  465. } else {
  466. assert.Equals(t, tc.err.Message, adminErr.Message)
  467. }
  468. return
  469. }
  470. prov := &linkedca.Provisioner{}
  471. err := readProtoJSON(res.Body, prov)
  472. assert.FatalError(t, err)
  473. assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
  474. opts := []cmp.Option{
  475. cmpopts.IgnoreUnexported(
  476. linkedca.Provisioner{}, linkedca.ProvisionerDetails{}, linkedca.ProvisionerDetails_OIDC{},
  477. linkedca.OIDCProvisioner{}, timestamppb.Timestamp{},
  478. ),
  479. }
  480. if !cmp.Equal(tc.prov, prov, opts...) {
  481. t.Errorf("linkedca.Admin diff =\n%s", cmp.Diff(tc.prov, prov, opts...))
  482. }
  483. })
  484. }
  485. }
  486. func TestHandler_DeleteProvisioner(t *testing.T) {
  487. type test struct {
  488. ctx context.Context
  489. auth adminAuthority
  490. req *http.Request
  491. statusCode int
  492. err *admin.Error
  493. }
  494. var tests = map[string]func(t *testing.T) test{
  495. "fail/auth.LoadProvisionerByID": func(t *testing.T) test {
  496. req := httptest.NewRequest("DELETE", "/foo?id=provID", http.NoBody)
  497. chiCtx := chi.NewRouteContext()
  498. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  499. auth := &mockAdminAuthority{
  500. MockLoadProvisionerByID: func(id string) (provisioner.Interface, error) {
  501. assert.Equals(t, "provID", id)
  502. return nil, errors.New("force")
  503. },
  504. }
  505. return test{
  506. ctx: ctx,
  507. req: req,
  508. auth: auth,
  509. statusCode: 500,
  510. err: &admin.Error{
  511. Type: admin.ErrorServerInternalType.String(),
  512. Status: 500,
  513. Detail: "the server experienced an internal error",
  514. Message: "error loading provisioner provID: force",
  515. },
  516. }
  517. },
  518. "fail/auth.LoadProvisionerByName": func(t *testing.T) test {
  519. req := httptest.NewRequest("DELETE", "/foo", http.NoBody)
  520. chiCtx := chi.NewRouteContext()
  521. chiCtx.URLParams.Add("name", "provName")
  522. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  523. auth := &mockAdminAuthority{
  524. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  525. assert.Equals(t, "provName", name)
  526. return nil, errors.New("force")
  527. },
  528. }
  529. return test{
  530. ctx: ctx,
  531. req: req,
  532. auth: auth,
  533. statusCode: 500,
  534. err: &admin.Error{
  535. Type: admin.ErrorServerInternalType.String(),
  536. Status: 500,
  537. Detail: "the server experienced an internal error",
  538. Message: "error loading provisioner provName: force",
  539. },
  540. }
  541. },
  542. "fail/auth.RemoveProvisioner": func(t *testing.T) test {
  543. req := httptest.NewRequest("DELETE", "/foo", http.NoBody)
  544. chiCtx := chi.NewRouteContext()
  545. chiCtx.URLParams.Add("name", "provName")
  546. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  547. auth := &mockAdminAuthority{
  548. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  549. assert.Equals(t, "provName", name)
  550. return &provisioner.OIDC{
  551. ID: "provID",
  552. Name: "provName",
  553. Type: "OIDC",
  554. }, nil
  555. },
  556. MockRemoveProvisioner: func(ctx context.Context, id string) error {
  557. assert.Equals(t, "provID", id)
  558. return errors.New("force")
  559. },
  560. }
  561. return test{
  562. ctx: ctx,
  563. req: req,
  564. auth: auth,
  565. statusCode: 500,
  566. err: &admin.Error{
  567. Type: admin.ErrorServerInternalType.String(),
  568. Status: 500,
  569. Detail: "the server experienced an internal error",
  570. Message: "error removing provisioner provName: force",
  571. },
  572. }
  573. },
  574. "ok": func(t *testing.T) test {
  575. req := httptest.NewRequest("DELETE", "/foo", http.NoBody)
  576. chiCtx := chi.NewRouteContext()
  577. chiCtx.URLParams.Add("name", "provName")
  578. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  579. auth := &mockAdminAuthority{
  580. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  581. assert.Equals(t, "provName", name)
  582. return &provisioner.OIDC{
  583. ID: "provID",
  584. Name: "provName",
  585. Type: "OIDC",
  586. }, nil
  587. },
  588. MockRemoveProvisioner: func(ctx context.Context, id string) error {
  589. assert.Equals(t, "provID", id)
  590. return nil
  591. },
  592. }
  593. return test{
  594. ctx: ctx,
  595. req: req,
  596. auth: auth,
  597. statusCode: 200,
  598. err: nil,
  599. }
  600. },
  601. }
  602. for name, prep := range tests {
  603. tc := prep(t)
  604. t.Run(name, func(t *testing.T) {
  605. mockMustAuthority(t, tc.auth)
  606. req := tc.req.WithContext(tc.ctx)
  607. w := httptest.NewRecorder()
  608. DeleteProvisioner(w, req)
  609. res := w.Result()
  610. assert.Equals(t, tc.statusCode, res.StatusCode)
  611. if res.StatusCode >= 400 {
  612. body, err := io.ReadAll(res.Body)
  613. res.Body.Close()
  614. assert.FatalError(t, err)
  615. adminErr := admin.Error{}
  616. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &adminErr))
  617. assert.Equals(t, tc.err.Type, adminErr.Type)
  618. assert.Equals(t, tc.err.Message, adminErr.Message)
  619. assert.Equals(t, tc.err.Detail, adminErr.Detail)
  620. assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
  621. return
  622. }
  623. body, err := io.ReadAll(res.Body)
  624. res.Body.Close()
  625. assert.FatalError(t, err)
  626. response := DeleteResponse{}
  627. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &response))
  628. assert.Equals(t, "ok", response.Status)
  629. assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
  630. })
  631. }
  632. }
  633. func TestHandler_UpdateProvisioner(t *testing.T) {
  634. type test struct {
  635. ctx context.Context
  636. auth adminAuthority
  637. body []byte
  638. adminDB admin.DB
  639. statusCode int
  640. err *admin.Error
  641. prov *linkedca.Provisioner
  642. }
  643. var tests = map[string]func(t *testing.T) test{
  644. "fail/readProtoJSON": func(t *testing.T) test {
  645. body := []byte("{!?}")
  646. return test{
  647. ctx: context.Background(),
  648. body: body,
  649. adminDB: &admin.MockDB{},
  650. statusCode: 400,
  651. err: &admin.Error{
  652. Type: "badRequest",
  653. Status: 400,
  654. Detail: "bad request",
  655. Message: "proto: syntax error (line 1:2): invalid value !",
  656. },
  657. }
  658. },
  659. "fail/auth.LoadProvisionerByName": 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: "provID",
  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 nil, errors.New("force")
  674. },
  675. }
  676. return test{
  677. ctx: ctx,
  678. body: body,
  679. adminDB: &admin.MockDB{},
  680. auth: auth,
  681. statusCode: 500,
  682. err: &admin.Error{
  683. Type: admin.ErrorServerInternalType.String(),
  684. Status: 500,
  685. Detail: "the server experienced an internal error",
  686. Message: "error loading provisioner from cached configuration 'provName': force",
  687. },
  688. }
  689. },
  690. "fail/db.GetProvisioner": func(t *testing.T) test {
  691. chiCtx := chi.NewRouteContext()
  692. chiCtx.URLParams.Add("name", "provName")
  693. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  694. prov := &linkedca.Provisioner{
  695. Id: "provID",
  696. Type: linkedca.Provisioner_OIDC,
  697. Name: "provName",
  698. }
  699. body, err := protojson.Marshal(prov)
  700. assert.FatalError(t, err)
  701. auth := &mockAdminAuthority{
  702. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  703. assert.Equals(t, "provName", name)
  704. return &provisioner.OIDC{
  705. ID: "provID",
  706. Name: "provName",
  707. }, nil
  708. },
  709. }
  710. db := &admin.MockDB{
  711. MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  712. assert.Equals(t, "provID", id)
  713. return nil, errors.New("force")
  714. },
  715. }
  716. return test{
  717. ctx: ctx,
  718. body: body,
  719. auth: auth,
  720. adminDB: db,
  721. statusCode: 500,
  722. err: &admin.Error{
  723. Type: admin.ErrorServerInternalType.String(),
  724. Status: 500,
  725. Detail: "the server experienced an internal error",
  726. Message: "error loading provisioner from db 'provID': force",
  727. },
  728. }
  729. },
  730. "fail/change-id-error": func(t *testing.T) test {
  731. chiCtx := chi.NewRouteContext()
  732. chiCtx.URLParams.Add("name", "provName")
  733. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  734. prov := &linkedca.Provisioner{
  735. Id: "differentProvID",
  736. Type: linkedca.Provisioner_OIDC,
  737. Name: "provName",
  738. }
  739. body, err := protojson.Marshal(prov)
  740. assert.FatalError(t, err)
  741. auth := &mockAdminAuthority{
  742. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  743. assert.Equals(t, "provName", name)
  744. return &provisioner.OIDC{
  745. ID: "provID",
  746. Name: "provName",
  747. }, nil
  748. },
  749. }
  750. db := &admin.MockDB{
  751. MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  752. assert.Equals(t, "provID", id)
  753. return &linkedca.Provisioner{
  754. Id: "provID",
  755. Name: "provName",
  756. }, nil
  757. },
  758. }
  759. return test{
  760. ctx: ctx,
  761. body: body,
  762. auth: auth,
  763. adminDB: db,
  764. statusCode: 500,
  765. err: &admin.Error{
  766. Type: admin.ErrorServerInternalType.String(),
  767. Status: 500,
  768. Detail: "the server experienced an internal error",
  769. Message: "cannot change provisioner ID",
  770. },
  771. }
  772. },
  773. "fail/change-type-error": func(t *testing.T) test {
  774. chiCtx := chi.NewRouteContext()
  775. chiCtx.URLParams.Add("name", "provName")
  776. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  777. prov := &linkedca.Provisioner{
  778. Id: "provID",
  779. Type: linkedca.Provisioner_JWK,
  780. Name: "provName",
  781. }
  782. body, err := protojson.Marshal(prov)
  783. assert.FatalError(t, err)
  784. auth := &mockAdminAuthority{
  785. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  786. assert.Equals(t, "provName", name)
  787. return &provisioner.OIDC{
  788. ID: "provID",
  789. Name: "provName",
  790. }, nil
  791. },
  792. }
  793. db := &admin.MockDB{
  794. MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  795. assert.Equals(t, "provID", id)
  796. return &linkedca.Provisioner{
  797. Id: "provID",
  798. Name: "provName",
  799. Type: linkedca.Provisioner_OIDC,
  800. }, nil
  801. },
  802. }
  803. return test{
  804. ctx: ctx,
  805. body: body,
  806. auth: auth,
  807. adminDB: db,
  808. statusCode: 500,
  809. err: &admin.Error{
  810. Type: admin.ErrorServerInternalType.String(),
  811. Status: 500,
  812. Detail: "the server experienced an internal error",
  813. Message: "cannot change provisioner type",
  814. },
  815. }
  816. },
  817. "fail/change-authority-id-error": func(t *testing.T) test {
  818. chiCtx := chi.NewRouteContext()
  819. chiCtx.URLParams.Add("name", "provName")
  820. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  821. prov := &linkedca.Provisioner{
  822. Id: "provID",
  823. Type: linkedca.Provisioner_OIDC,
  824. Name: "provName",
  825. AuthorityId: "differentAuthorityID",
  826. }
  827. body, err := protojson.Marshal(prov)
  828. assert.FatalError(t, err)
  829. auth := &mockAdminAuthority{
  830. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  831. assert.Equals(t, "provName", name)
  832. return &provisioner.OIDC{
  833. ID: "provID",
  834. Name: "provName",
  835. }, nil
  836. },
  837. }
  838. db := &admin.MockDB{
  839. MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  840. assert.Equals(t, "provID", id)
  841. return &linkedca.Provisioner{
  842. Id: "provID",
  843. Name: "provName",
  844. Type: linkedca.Provisioner_OIDC,
  845. AuthorityId: "authorityID",
  846. }, nil
  847. },
  848. }
  849. return test{
  850. ctx: ctx,
  851. body: body,
  852. auth: auth,
  853. adminDB: db,
  854. statusCode: 500,
  855. err: &admin.Error{
  856. Type: admin.ErrorServerInternalType.String(),
  857. Status: 500,
  858. Detail: "the server experienced an internal error",
  859. Message: "cannot change provisioner authorityID",
  860. },
  861. }
  862. },
  863. "fail/change-createdAt-error": func(t *testing.T) test {
  864. chiCtx := chi.NewRouteContext()
  865. chiCtx.URLParams.Add("name", "provName")
  866. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  867. createdAt := time.Now()
  868. prov := &linkedca.Provisioner{
  869. Id: "provID",
  870. Type: linkedca.Provisioner_OIDC,
  871. Name: "provName",
  872. AuthorityId: "authorityID",
  873. CreatedAt: timestamppb.New(time.Now().Add(-1 * time.Hour)),
  874. }
  875. body, err := protojson.Marshal(prov)
  876. assert.FatalError(t, err)
  877. auth := &mockAdminAuthority{
  878. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  879. assert.Equals(t, "provName", name)
  880. return &provisioner.OIDC{
  881. ID: "provID",
  882. Name: "provName",
  883. }, nil
  884. },
  885. }
  886. db := &admin.MockDB{
  887. MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  888. assert.Equals(t, "provID", id)
  889. return &linkedca.Provisioner{
  890. Id: "provID",
  891. Name: "provName",
  892. Type: linkedca.Provisioner_OIDC,
  893. AuthorityId: "authorityID",
  894. CreatedAt: timestamppb.New(createdAt),
  895. }, nil
  896. },
  897. }
  898. return test{
  899. ctx: ctx,
  900. body: body,
  901. auth: auth,
  902. adminDB: db,
  903. statusCode: 500,
  904. err: &admin.Error{
  905. Type: admin.ErrorServerInternalType.String(),
  906. Status: 500,
  907. Detail: "the server experienced an internal error",
  908. Message: "cannot change provisioner createdAt",
  909. },
  910. }
  911. },
  912. "fail/change-deletedAt-error": func(t *testing.T) test {
  913. chiCtx := chi.NewRouteContext()
  914. chiCtx.URLParams.Add("name", "provName")
  915. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  916. createdAt := time.Now()
  917. var deletedAt time.Time
  918. prov := &linkedca.Provisioner{
  919. Id: "provID",
  920. Type: linkedca.Provisioner_OIDC,
  921. Name: "provName",
  922. AuthorityId: "authorityID",
  923. CreatedAt: timestamppb.New(createdAt),
  924. DeletedAt: timestamppb.New(time.Now()),
  925. }
  926. body, err := protojson.Marshal(prov)
  927. assert.FatalError(t, err)
  928. auth := &mockAdminAuthority{
  929. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  930. assert.Equals(t, "provName", name)
  931. return &provisioner.OIDC{
  932. ID: "provID",
  933. Name: "provName",
  934. }, nil
  935. },
  936. }
  937. db := &admin.MockDB{
  938. MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  939. assert.Equals(t, "provID", id)
  940. return &linkedca.Provisioner{
  941. Id: "provID",
  942. Name: "provName",
  943. Type: linkedca.Provisioner_OIDC,
  944. AuthorityId: "authorityID",
  945. CreatedAt: timestamppb.New(createdAt),
  946. DeletedAt: timestamppb.New(deletedAt),
  947. }, nil
  948. },
  949. }
  950. return test{
  951. ctx: ctx,
  952. body: body,
  953. auth: auth,
  954. adminDB: db,
  955. statusCode: 500,
  956. err: &admin.Error{
  957. Type: admin.ErrorServerInternalType.String(),
  958. Status: 500,
  959. Detail: "the server experienced an internal error",
  960. Message: "cannot change provisioner deletedAt",
  961. },
  962. }
  963. },
  964. // TODO(hs): ValidateClaims can't be mocked atm
  965. //"fail/ValidateClaims": func(t *testing.T) test { return test{} },
  966. "fail/validateProvisioner": func(t *testing.T) test {
  967. chiCtx := chi.NewRouteContext()
  968. chiCtx.URLParams.Add("name", "provName")
  969. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  970. createdAt := time.Now()
  971. var deletedAt time.Time
  972. prov := &linkedca.Provisioner{
  973. Id: "provID",
  974. Type: linkedca.Provisioner_OIDC,
  975. Name: "provName",
  976. AuthorityId: "authorityID",
  977. CreatedAt: timestamppb.New(createdAt),
  978. DeletedAt: timestamppb.New(deletedAt),
  979. Details: &linkedca.ProvisionerDetails{Data: &linkedca.ProvisionerDetails_JWK{
  980. JWK: &linkedca.JWKProvisioner{PublicKey: testJWKPublicKey(t)},
  981. }},
  982. }
  983. body, err := protojson.Marshal(prov)
  984. assert.FatalError(t, err)
  985. auth := &mockAdminAuthority{
  986. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  987. assert.Equals(t, "provName", name)
  988. return &provisioner.OIDC{
  989. ID: "provID",
  990. Name: "provName",
  991. }, nil
  992. },
  993. }
  994. db := &admin.MockDB{
  995. MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  996. assert.Equals(t, "provID", id)
  997. return &linkedca.Provisioner{
  998. Id: "provID",
  999. Name: "provName",
  1000. Type: linkedca.Provisioner_OIDC,
  1001. AuthorityId: "authorityID",
  1002. CreatedAt: timestamppb.New(createdAt),
  1003. DeletedAt: timestamppb.New(deletedAt),
  1004. }, nil
  1005. },
  1006. }
  1007. return test{
  1008. ctx: ctx,
  1009. body: body,
  1010. auth: auth,
  1011. adminDB: db,
  1012. statusCode: 400,
  1013. err: &admin.Error{
  1014. Type: "badRequest",
  1015. Status: 400,
  1016. Detail: "bad request",
  1017. Message: "provisioner details (JWK) do not match provisioner type (OIDC)",
  1018. },
  1019. }
  1020. },
  1021. "fail/validateTemplates": func(t *testing.T) test {
  1022. chiCtx := chi.NewRouteContext()
  1023. chiCtx.URLParams.Add("name", "provName")
  1024. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  1025. createdAt := time.Now()
  1026. var deletedAt time.Time
  1027. prov := &linkedca.Provisioner{
  1028. Id: "provID",
  1029. Type: linkedca.Provisioner_OIDC,
  1030. Name: "provName",
  1031. AuthorityId: "authorityID",
  1032. CreatedAt: timestamppb.New(createdAt),
  1033. DeletedAt: timestamppb.New(deletedAt),
  1034. Details: &linkedca.ProvisionerDetails{Data: &linkedca.ProvisionerDetails_OIDC{
  1035. OIDC: &linkedca.OIDCProvisioner{
  1036. ClientId: "client-id",
  1037. ConfigurationEndpoint: "https://example.com/.well-known/openid-configuration",
  1038. },
  1039. }},
  1040. X509Template: &linkedca.Template{
  1041. Template: []byte("{ {{ missingFunction }} }"),
  1042. },
  1043. }
  1044. body, err := protojson.Marshal(prov)
  1045. assert.FatalError(t, err)
  1046. auth := &mockAdminAuthority{
  1047. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  1048. assert.Equals(t, "provName", name)
  1049. return &provisioner.OIDC{
  1050. ID: "provID",
  1051. Name: "provName",
  1052. }, nil
  1053. },
  1054. }
  1055. db := &admin.MockDB{
  1056. MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  1057. assert.Equals(t, "provID", id)
  1058. return &linkedca.Provisioner{
  1059. Id: "provID",
  1060. Name: "provName",
  1061. Type: linkedca.Provisioner_OIDC,
  1062. AuthorityId: "authorityID",
  1063. CreatedAt: timestamppb.New(createdAt),
  1064. DeletedAt: timestamppb.New(deletedAt),
  1065. }, nil
  1066. },
  1067. }
  1068. return test{
  1069. ctx: ctx,
  1070. body: body,
  1071. auth: auth,
  1072. adminDB: db,
  1073. statusCode: 400,
  1074. err: &admin.Error{
  1075. Type: "badRequest",
  1076. Status: 400,
  1077. Detail: "bad request",
  1078. Message: "invalid template: invalid X.509 template: error parsing template: template: template:1: function \"missingFunction\" not defined",
  1079. },
  1080. }
  1081. },
  1082. "fail/auth.UpdateProvisioner": func(t *testing.T) test {
  1083. chiCtx := chi.NewRouteContext()
  1084. chiCtx.URLParams.Add("name", "provName")
  1085. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  1086. createdAt := time.Now()
  1087. var deletedAt time.Time
  1088. prov := &linkedca.Provisioner{
  1089. Id: "provID",
  1090. Type: linkedca.Provisioner_OIDC,
  1091. Name: "provName",
  1092. AuthorityId: "authorityID",
  1093. CreatedAt: timestamppb.New(createdAt),
  1094. DeletedAt: timestamppb.New(deletedAt),
  1095. Details: &linkedca.ProvisionerDetails{Data: &linkedca.ProvisionerDetails_OIDC{
  1096. OIDC: &linkedca.OIDCProvisioner{
  1097. ClientId: "client-id",
  1098. ConfigurationEndpoint: "https://example.com/.well-known/openid-configuration",
  1099. },
  1100. }},
  1101. }
  1102. body, err := protojson.Marshal(prov)
  1103. assert.FatalError(t, err)
  1104. auth := &mockAdminAuthority{
  1105. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  1106. assert.Equals(t, "provName", name)
  1107. return &provisioner.OIDC{
  1108. ID: "provID",
  1109. Name: "provName",
  1110. }, nil
  1111. },
  1112. MockUpdateProvisioner: func(ctx context.Context, nu *linkedca.Provisioner) error {
  1113. assert.Equals(t, "provID", nu.Id)
  1114. assert.Equals(t, "provName", nu.Name)
  1115. return errors.New("force")
  1116. },
  1117. }
  1118. db := &admin.MockDB{
  1119. MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  1120. assert.Equals(t, "provID", id)
  1121. return &linkedca.Provisioner{
  1122. Id: "provID",
  1123. Name: "provName",
  1124. Type: linkedca.Provisioner_OIDC,
  1125. AuthorityId: "authorityID",
  1126. CreatedAt: timestamppb.New(createdAt),
  1127. DeletedAt: timestamppb.New(deletedAt),
  1128. }, nil
  1129. },
  1130. }
  1131. return test{
  1132. ctx: ctx,
  1133. body: body,
  1134. auth: auth,
  1135. adminDB: db,
  1136. statusCode: 500,
  1137. err: &admin.Error{
  1138. Type: "", // TODO(hs): this error can be improved
  1139. Status: 500,
  1140. Detail: "",
  1141. Message: "",
  1142. },
  1143. }
  1144. },
  1145. "ok": func(t *testing.T) test {
  1146. chiCtx := chi.NewRouteContext()
  1147. chiCtx.URLParams.Add("name", "provName")
  1148. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  1149. createdAt := time.Now()
  1150. var deletedAt time.Time
  1151. prov := &linkedca.Provisioner{
  1152. Id: "provID",
  1153. Type: linkedca.Provisioner_OIDC,
  1154. Name: "provName",
  1155. AuthorityId: "authorityID",
  1156. CreatedAt: timestamppb.New(createdAt),
  1157. DeletedAt: timestamppb.New(deletedAt),
  1158. Details: &linkedca.ProvisionerDetails{
  1159. Data: &linkedca.ProvisionerDetails_OIDC{
  1160. OIDC: &linkedca.OIDCProvisioner{
  1161. ClientId: "new-client-id",
  1162. ClientSecret: "new-client-secret",
  1163. ConfigurationEndpoint: "https://example.com/.well-known/openid-configuration",
  1164. },
  1165. },
  1166. },
  1167. }
  1168. body, err := protojson.Marshal(prov)
  1169. assert.FatalError(t, err)
  1170. auth := &mockAdminAuthority{
  1171. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  1172. assert.Equals(t, "provName", name)
  1173. return &provisioner.OIDC{
  1174. ID: "provID",
  1175. Name: "provName",
  1176. }, nil
  1177. },
  1178. MockUpdateProvisioner: func(ctx context.Context, nu *linkedca.Provisioner) error {
  1179. assert.Equals(t, "provID", nu.Id)
  1180. assert.Equals(t, "provName", nu.Name)
  1181. return nil
  1182. },
  1183. }
  1184. db := &admin.MockDB{
  1185. MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  1186. assert.Equals(t, "provID", id)
  1187. return &linkedca.Provisioner{
  1188. Id: "provID",
  1189. Name: "provName",
  1190. Type: linkedca.Provisioner_OIDC,
  1191. AuthorityId: "authorityID",
  1192. CreatedAt: timestamppb.New(createdAt),
  1193. DeletedAt: timestamppb.New(deletedAt),
  1194. }, nil
  1195. },
  1196. }
  1197. return test{
  1198. ctx: ctx,
  1199. body: body,
  1200. auth: auth,
  1201. adminDB: db,
  1202. statusCode: 200,
  1203. prov: prov,
  1204. }
  1205. },
  1206. }
  1207. for name, prep := range tests {
  1208. tc := prep(t)
  1209. t.Run(name, func(t *testing.T) {
  1210. mockMustAuthority(t, tc.auth)
  1211. ctx := admin.NewContext(tc.ctx, tc.adminDB)
  1212. req := httptest.NewRequest("POST", "/foo", io.NopCloser(bytes.NewBuffer(tc.body)))
  1213. req = req.WithContext(ctx)
  1214. w := httptest.NewRecorder()
  1215. UpdateProvisioner(w, req)
  1216. res := w.Result()
  1217. assert.Equals(t, tc.statusCode, res.StatusCode)
  1218. if res.StatusCode >= 400 {
  1219. body, err := io.ReadAll(res.Body)
  1220. res.Body.Close()
  1221. assert.FatalError(t, err)
  1222. adminErr := admin.Error{}
  1223. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &adminErr))
  1224. assert.Equals(t, tc.err.Type, adminErr.Type)
  1225. assert.Equals(t, tc.err.Detail, adminErr.Detail)
  1226. assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
  1227. if strings.HasPrefix(tc.err.Message, "proto:") {
  1228. assert.True(t, strings.Contains(adminErr.Message, "syntax error"))
  1229. } else {
  1230. assert.Equals(t, tc.err.Message, adminErr.Message)
  1231. }
  1232. return
  1233. }
  1234. prov := &linkedca.Provisioner{}
  1235. err := readProtoJSON(res.Body, prov)
  1236. assert.FatalError(t, err)
  1237. assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
  1238. opts := []cmp.Option{
  1239. cmpopts.IgnoreUnexported(
  1240. linkedca.Provisioner{}, linkedca.ProvisionerDetails{}, linkedca.ProvisionerDetails_OIDC{},
  1241. linkedca.OIDCProvisioner{}, timestamppb.Timestamp{},
  1242. ),
  1243. }
  1244. if !cmp.Equal(tc.prov, prov, opts...) {
  1245. t.Errorf("linkedca.Admin diff =\n%s", cmp.Diff(tc.prov, prov, opts...))
  1246. }
  1247. })
  1248. }
  1249. }
  1250. func Test_validateTemplates(t *testing.T) {
  1251. type args struct {
  1252. x509 *linkedca.Template
  1253. ssh *linkedca.Template
  1254. }
  1255. tests := []struct {
  1256. name string
  1257. args args
  1258. err error
  1259. }{
  1260. {
  1261. name: "ok",
  1262. args: args{},
  1263. err: nil,
  1264. },
  1265. {
  1266. name: "ok/x509",
  1267. args: args{
  1268. x509: &linkedca.Template{
  1269. Template: []byte(`{"x": 1}`),
  1270. },
  1271. },
  1272. err: nil,
  1273. },
  1274. {
  1275. name: "ok/ssh",
  1276. args: args{
  1277. ssh: &linkedca.Template{
  1278. Template: []byte(`{"x": 1}`),
  1279. },
  1280. },
  1281. err: nil,
  1282. },
  1283. {
  1284. name: "fail/x509-template-missing-quote",
  1285. args: args{
  1286. x509: &linkedca.Template{
  1287. Template: []byte(`{ {{printf "%q" "quoted}} }`),
  1288. },
  1289. },
  1290. err: errors.New("invalid X.509 template: error parsing template: template: template:1: unterminated quoted string"),
  1291. },
  1292. {
  1293. name: "fail/x509-template-data",
  1294. args: args{
  1295. x509: &linkedca.Template{
  1296. Data: []byte(`{!?}`),
  1297. },
  1298. },
  1299. err: errors.New("invalid X.509 template data: error validating json template data"),
  1300. },
  1301. {
  1302. name: "fail/ssh-template-unknown-function",
  1303. args: args{
  1304. ssh: &linkedca.Template{
  1305. Template: []byte(`{ {{unknownFunction "foo"}} }`),
  1306. },
  1307. },
  1308. err: errors.New("invalid SSH template: error parsing template: template: template:1: function \"unknownFunction\" not defined"),
  1309. },
  1310. {
  1311. name: "fail/ssh-template-data",
  1312. args: args{
  1313. ssh: &linkedca.Template{
  1314. Data: []byte(`{!?}`),
  1315. },
  1316. },
  1317. err: errors.New("invalid SSH template data: error validating json template data"),
  1318. },
  1319. }
  1320. for _, tt := range tests {
  1321. t.Run(tt.name, func(t *testing.T) {
  1322. err := validateTemplates(tt.args.x509, tt.args.ssh)
  1323. if tt.err != nil {
  1324. assert.Error(t, err)
  1325. assert.Equals(t, tt.err.Error(), err.Error())
  1326. return
  1327. }
  1328. assert.Nil(t, err)
  1329. })
  1330. }
  1331. }