webhook_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. package api
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "errors"
  7. "io"
  8. "net/http/httptest"
  9. "strings"
  10. "testing"
  11. "github.com/go-chi/chi"
  12. "github.com/smallstep/certificates/authority"
  13. "github.com/smallstep/certificates/authority/admin"
  14. "github.com/stretchr/testify/assert"
  15. "go.step.sm/linkedca"
  16. "google.golang.org/protobuf/encoding/protojson"
  17. )
  18. // ignore secret and id since those are set by the server
  19. func assertEqualWebhook(t *testing.T, a, b *linkedca.Webhook) {
  20. assert.Equal(t, a.Name, b.Name)
  21. assert.Equal(t, a.Url, b.Url)
  22. assert.Equal(t, a.Kind, b.Kind)
  23. assert.Equal(t, a.CertType, b.CertType)
  24. assert.Equal(t, a.DisableTlsClientAuth, b.DisableTlsClientAuth)
  25. assert.Equal(t, a.GetAuth(), b.GetAuth())
  26. }
  27. func TestWebhookAdminResponder_CreateProvisionerWebhook(t *testing.T) {
  28. type test struct {
  29. auth adminAuthority
  30. body []byte
  31. ctx context.Context
  32. err *admin.Error
  33. response *linkedca.Webhook
  34. statusCode int
  35. }
  36. var tests = map[string]func(t *testing.T) test{
  37. "fail/existing-webhook": func(t *testing.T) test {
  38. webhook := &linkedca.Webhook{
  39. Name: "already-exists",
  40. Url: "https://example.com",
  41. }
  42. prov := &linkedca.Provisioner{
  43. Name: "provName",
  44. Webhooks: []*linkedca.Webhook{webhook},
  45. }
  46. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  47. err := admin.NewError(admin.ErrorConflictType, `provisioner "provName" already has a webhook with the name "already-exists"`)
  48. err.Message = `provisioner "provName" already has a webhook with the name "already-exists"`
  49. body := []byte(`
  50. {
  51. "name": "already-exists",
  52. "url": "https://example.com",
  53. "kind": "ENRICHING"
  54. }`)
  55. return test{
  56. ctx: ctx,
  57. body: body,
  58. err: err,
  59. statusCode: 409,
  60. }
  61. },
  62. "fail/read.ProtoJSON": func(t *testing.T) test {
  63. prov := &linkedca.Provisioner{
  64. Name: "provName",
  65. }
  66. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  67. adminErr := admin.NewError(admin.ErrorBadRequestType, "proto: syntax error (line 1:2): invalid value ?")
  68. adminErr.Message = "proto: syntax error (line 1:2): invalid value ?"
  69. body := []byte("{?}")
  70. return test{
  71. ctx: ctx,
  72. body: body,
  73. err: adminErr,
  74. statusCode: 400,
  75. }
  76. },
  77. "fail/missing-name": func(t *testing.T) test {
  78. prov := &linkedca.Provisioner{
  79. Name: "provName",
  80. }
  81. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  82. adminErr := admin.NewError(admin.ErrorBadRequestType, "webhook name is required")
  83. adminErr.Message = "webhook name is required"
  84. body := []byte(`{"url": "https://example.com", "kind": "ENRICHING"}`)
  85. return test{
  86. ctx: ctx,
  87. body: body,
  88. err: adminErr,
  89. statusCode: 400,
  90. }
  91. },
  92. "fail/missing-url": func(t *testing.T) test {
  93. prov := &linkedca.Provisioner{
  94. Name: "provName",
  95. }
  96. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  97. adminErr := admin.NewError(admin.ErrorBadRequestType, "webhook url is invalid")
  98. adminErr.Message = "webhook url is invalid"
  99. body := []byte(`{"name": "metadata", "kind": "ENRICHING"}`)
  100. return test{
  101. ctx: ctx,
  102. body: body,
  103. err: adminErr,
  104. statusCode: 400,
  105. }
  106. },
  107. "fail/relative-url": func(t *testing.T) test {
  108. prov := &linkedca.Provisioner{
  109. Name: "provName",
  110. }
  111. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  112. adminErr := admin.NewError(admin.ErrorBadRequestType, "webhook url is invalid")
  113. adminErr.Message = "webhook url is invalid"
  114. body := []byte(`{"name": "metadata", "url": "example.com/path", "kind": "ENRICHING"}`)
  115. return test{
  116. ctx: ctx,
  117. body: body,
  118. err: adminErr,
  119. statusCode: 400,
  120. }
  121. },
  122. "fail/http-url": func(t *testing.T) test {
  123. prov := &linkedca.Provisioner{
  124. Name: "provName",
  125. }
  126. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  127. adminErr := admin.NewError(admin.ErrorBadRequestType, "webhook url must use https")
  128. adminErr.Message = "webhook url must use https"
  129. body := []byte(`{"name": "metadata", "url": "http://example.com", "kind": "ENRICHING"}`)
  130. return test{
  131. ctx: ctx,
  132. body: body,
  133. err: adminErr,
  134. statusCode: 400,
  135. }
  136. },
  137. "fail/basic-auth-in-url": func(t *testing.T) test {
  138. prov := &linkedca.Provisioner{
  139. Name: "provName",
  140. }
  141. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  142. adminErr := admin.NewError(admin.ErrorBadRequestType, "webhook url may not contain username or password")
  143. adminErr.Message = "webhook url may not contain username or password"
  144. body := []byte(`
  145. {
  146. "name": "metadata",
  147. "url": "https://user:pass@example.com",
  148. "kind": "ENRICHING"
  149. }`)
  150. return test{
  151. ctx: ctx,
  152. body: body,
  153. err: adminErr,
  154. statusCode: 400,
  155. }
  156. },
  157. "fail/secret-in-request": func(t *testing.T) test {
  158. prov := &linkedca.Provisioner{
  159. Name: "provName",
  160. }
  161. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  162. adminErr := admin.NewError(admin.ErrorBadRequestType, "webhook secret must not be set")
  163. adminErr.Message = "webhook secret must not be set"
  164. body := []byte(`
  165. {
  166. "name": "metadata",
  167. "url": "https://example.com",
  168. "kind": "ENRICHING",
  169. "secret": "secret"
  170. }`)
  171. return test{
  172. ctx: ctx,
  173. body: body,
  174. err: adminErr,
  175. statusCode: 400,
  176. }
  177. },
  178. "fail/auth.UpdateProvisioner-error": func(t *testing.T) test {
  179. adm := &linkedca.Admin{
  180. Subject: "step",
  181. }
  182. prov := &linkedca.Provisioner{
  183. Name: "provName",
  184. }
  185. ctx := linkedca.NewContextWithAdmin(context.Background(), adm)
  186. ctx = linkedca.NewContextWithProvisioner(ctx, prov)
  187. adminErr := admin.NewError(admin.ErrorServerInternalType, "error creating provisioner webhook: force")
  188. adminErr.Message = "error creating provisioner webhook: force"
  189. body := []byte(`{"name": "metadata", "url": "https://example.com", "kind": "ENRICHING"}`)
  190. return test{
  191. ctx: ctx,
  192. auth: &mockAdminAuthority{
  193. MockUpdateProvisioner: func(ctx context.Context, nu *linkedca.Provisioner) error {
  194. return &authority.PolicyError{
  195. Typ: authority.StoreFailure,
  196. Err: errors.New("force"),
  197. }
  198. },
  199. },
  200. body: body,
  201. err: adminErr,
  202. statusCode: 500,
  203. }
  204. },
  205. "ok": func(t *testing.T) test {
  206. prov := &linkedca.Provisioner{
  207. Name: "provName",
  208. }
  209. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  210. body := []byte(`{"name": "metadata", "url": "https://example.com", "kind": "ENRICHING", "certType": "X509"}`)
  211. return test{
  212. ctx: ctx,
  213. auth: &mockAdminAuthority{
  214. MockUpdateProvisioner: func(ctx context.Context, nu *linkedca.Provisioner) error {
  215. assert.Equal(t, linkedca.Webhook_X509, nu.Webhooks[0].CertType)
  216. return nil
  217. },
  218. },
  219. body: body,
  220. response: &linkedca.Webhook{
  221. Name: "metadata",
  222. Url: "https://example.com",
  223. Kind: linkedca.Webhook_ENRICHING,
  224. CertType: linkedca.Webhook_X509,
  225. },
  226. statusCode: 201,
  227. }
  228. },
  229. }
  230. for name, prep := range tests {
  231. tc := prep(t)
  232. t.Run(name, func(t *testing.T) {
  233. mockMustAuthority(t, tc.auth)
  234. ctx := admin.NewContext(tc.ctx, &admin.MockDB{})
  235. war := NewWebhookAdminResponder()
  236. req := httptest.NewRequest("POST", "/foo", io.NopCloser(bytes.NewBuffer(tc.body)))
  237. req = req.WithContext(ctx)
  238. w := httptest.NewRecorder()
  239. war.CreateProvisionerWebhook(w, req)
  240. res := w.Result()
  241. assert.Equal(t, tc.statusCode, res.StatusCode)
  242. if res.StatusCode >= 400 {
  243. body, err := io.ReadAll(res.Body)
  244. res.Body.Close()
  245. assert.NoError(t, err)
  246. ae := testAdminError{}
  247. assert.NoError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  248. assert.Equal(t, tc.err.Type, ae.Type)
  249. assert.Equal(t, tc.err.StatusCode(), res.StatusCode)
  250. assert.Equal(t, tc.err.Detail, ae.Detail)
  251. assert.Equal(t, []string{"application/json"}, res.Header["Content-Type"])
  252. // when the error message starts with "proto", we expect it to have
  253. // a syntax error (in the tests). If the message doesn't start with "proto",
  254. // we expect a full string match.
  255. if strings.HasPrefix(tc.err.Message, "proto:") {
  256. assert.True(t, strings.Contains(ae.Message, "syntax error"))
  257. } else {
  258. assert.Equal(t, tc.err.Message, ae.Message)
  259. }
  260. return
  261. }
  262. resp := &linkedca.Webhook{}
  263. body, err := io.ReadAll(res.Body)
  264. assert.NoError(t, err)
  265. assert.NoError(t, protojson.Unmarshal(body, resp))
  266. assertEqualWebhook(t, tc.response, resp)
  267. assert.NotEmpty(t, resp.Secret)
  268. assert.NotEmpty(t, resp.Id)
  269. })
  270. }
  271. }
  272. func TestWebhookAdminResponder_DeleteProvisionerWebhook(t *testing.T) {
  273. type test struct {
  274. auth adminAuthority
  275. err *admin.Error
  276. statusCode int
  277. provisionerWebhooks []*linkedca.Webhook
  278. webhookName string
  279. }
  280. var tests = map[string]func(t *testing.T) test{
  281. "fail/auth.UpdateProvisioner-error": func(t *testing.T) test {
  282. adminErr := admin.NewError(admin.ErrorServerInternalType, "error deleting provisioner webhook: force")
  283. adminErr.Message = "error deleting provisioner webhook: force"
  284. return test{
  285. err: adminErr,
  286. auth: &mockAdminAuthority{
  287. MockUpdateProvisioner: func(ctx context.Context, nu *linkedca.Provisioner) error {
  288. return &authority.PolicyError{
  289. Typ: authority.StoreFailure,
  290. Err: errors.New("force"),
  291. }
  292. },
  293. },
  294. statusCode: 500,
  295. webhookName: "my-webhook",
  296. provisionerWebhooks: []*linkedca.Webhook{
  297. {Name: "my-webhook", Url: "https://example.com", Kind: linkedca.Webhook_ENRICHING},
  298. },
  299. }
  300. },
  301. "ok/not-found": func(t *testing.T) test {
  302. return test{
  303. statusCode: 200,
  304. webhookName: "no-exists",
  305. provisionerWebhooks: nil,
  306. }
  307. },
  308. "ok": func(t *testing.T) test {
  309. return test{
  310. statusCode: 200,
  311. webhookName: "exists",
  312. auth: &mockAdminAuthority{
  313. MockUpdateProvisioner: func(ctx context.Context, nu *linkedca.Provisioner) error {
  314. assert.Equal(t, nu.Webhooks, []*linkedca.Webhook{
  315. {Name: "my-2nd-webhook", Url: "https://example.com", Kind: linkedca.Webhook_ENRICHING},
  316. })
  317. return nil
  318. },
  319. },
  320. provisionerWebhooks: []*linkedca.Webhook{
  321. {Name: "exists", Url: "https.example.com", Kind: linkedca.Webhook_ENRICHING},
  322. {Name: "my-2nd-webhook", Url: "https://example.com", Kind: linkedca.Webhook_ENRICHING},
  323. },
  324. }
  325. },
  326. }
  327. for name, prep := range tests {
  328. tc := prep(t)
  329. t.Run(name, func(t *testing.T) {
  330. mockMustAuthority(t, tc.auth)
  331. chiCtx := chi.NewRouteContext()
  332. chiCtx.URLParams.Add("webhookName", tc.webhookName)
  333. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  334. prov := &linkedca.Provisioner{
  335. Name: "provName",
  336. Webhooks: tc.provisionerWebhooks,
  337. }
  338. ctx = linkedca.NewContextWithProvisioner(ctx, prov)
  339. ctx = admin.NewContext(ctx, &admin.MockDB{})
  340. req := httptest.NewRequest("DELETE", "/foo", nil).WithContext(ctx)
  341. war := NewWebhookAdminResponder()
  342. w := httptest.NewRecorder()
  343. war.DeleteProvisionerWebhook(w, req)
  344. res := w.Result()
  345. assert.Equal(t, tc.statusCode, res.StatusCode)
  346. if res.StatusCode >= 400 {
  347. body, err := io.ReadAll(res.Body)
  348. res.Body.Close()
  349. assert.NoError(t, err)
  350. ae := testAdminError{}
  351. assert.NoError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  352. assert.Equal(t, tc.err.Type, ae.Type)
  353. assert.Equal(t, tc.err.StatusCode(), res.StatusCode)
  354. assert.Equal(t, tc.err.Detail, ae.Detail)
  355. assert.Equal(t, []string{"application/json"}, res.Header["Content-Type"])
  356. // when the error message starts with "proto", we expect it to have
  357. // a syntax error (in the tests). If the message doesn't start with "proto",
  358. // we expect a full string match.
  359. if strings.HasPrefix(tc.err.Message, "proto:") {
  360. assert.True(t, strings.Contains(ae.Message, "syntax error"))
  361. } else {
  362. assert.Equal(t, tc.err.Message, ae.Message)
  363. }
  364. return
  365. }
  366. body, err := io.ReadAll(res.Body)
  367. assert.NoError(t, err)
  368. res.Body.Close()
  369. response := DeleteResponse{}
  370. assert.NoError(t, json.Unmarshal(bytes.TrimSpace(body), &response))
  371. assert.Equal(t, "ok", response.Status)
  372. assert.Equal(t, []string{"application/json"}, res.Header["Content-Type"])
  373. })
  374. }
  375. }
  376. func TestWebhookAdminResponder_UpdateProvisionerWebhook(t *testing.T) {
  377. type test struct {
  378. auth adminAuthority
  379. adminDB admin.DB
  380. body []byte
  381. ctx context.Context
  382. err *admin.Error
  383. response *linkedca.Webhook
  384. statusCode int
  385. }
  386. var tests = map[string]func(t *testing.T) test{
  387. "fail/not-found": func(t *testing.T) test {
  388. prov := &linkedca.Provisioner{
  389. Name: "provName",
  390. Webhooks: []*linkedca.Webhook{{Name: "exists", Url: "https://example.com", Kind: linkedca.Webhook_ENRICHING}},
  391. }
  392. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  393. err := admin.NewError(admin.ErrorNotFoundType, `provisioner "provName" has no webhook with the name "no-exists"`)
  394. err.Message = `provisioner "provName" has no webhook with the name "no-exists"`
  395. body := []byte(`
  396. {
  397. "name": "no-exists",
  398. "url": "https://example.com",
  399. "kind": "ENRICHING"
  400. }`)
  401. return test{
  402. ctx: ctx,
  403. adminDB: &admin.MockDB{},
  404. body: body,
  405. err: err,
  406. statusCode: 404,
  407. }
  408. },
  409. "fail/read.ProtoJSON": func(t *testing.T) test {
  410. prov := &linkedca.Provisioner{
  411. Name: "provName",
  412. Webhooks: []*linkedca.Webhook{{Name: "my-webhook", Url: "https://example.com", Kind: linkedca.Webhook_ENRICHING}},
  413. }
  414. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  415. adminErr := admin.NewError(admin.ErrorBadRequestType, "proto: syntax error (line 1:2): invalid value ?")
  416. adminErr.Message = "proto: syntax error (line 1:2): invalid value ?"
  417. body := []byte("{?}")
  418. return test{
  419. ctx: ctx,
  420. adminDB: &admin.MockDB{},
  421. body: body,
  422. err: adminErr,
  423. statusCode: 400,
  424. }
  425. },
  426. "fail/missing-name": func(t *testing.T) test {
  427. prov := &linkedca.Provisioner{
  428. Name: "provName",
  429. Webhooks: []*linkedca.Webhook{{Name: "my-webhook", Url: "https://example.com", Kind: linkedca.Webhook_ENRICHING}},
  430. }
  431. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  432. adminErr := admin.NewError(admin.ErrorBadRequestType, "webhook name is required")
  433. adminErr.Message = "webhook name is required"
  434. body := []byte(`{"url": "https://example.com", "kind": "ENRICHING"}`)
  435. return test{
  436. ctx: ctx,
  437. adminDB: &admin.MockDB{},
  438. body: body,
  439. err: adminErr,
  440. statusCode: 400,
  441. }
  442. },
  443. "fail/missing-url": func(t *testing.T) test {
  444. prov := &linkedca.Provisioner{
  445. Name: "provName",
  446. Webhooks: []*linkedca.Webhook{{Name: "my-webhook", Url: "https://example.com", Kind: linkedca.Webhook_ENRICHING}},
  447. }
  448. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  449. adminErr := admin.NewError(admin.ErrorBadRequestType, "webhook url is invalid")
  450. adminErr.Message = "webhook url is invalid"
  451. body := []byte(`{"name": "metadata", "kind": "ENRICHING"}`)
  452. return test{
  453. ctx: ctx,
  454. adminDB: &admin.MockDB{},
  455. body: body,
  456. err: adminErr,
  457. statusCode: 400,
  458. }
  459. },
  460. "fail/relative-url": func(t *testing.T) test {
  461. prov := &linkedca.Provisioner{
  462. Name: "provName",
  463. Webhooks: []*linkedca.Webhook{{Name: "my-webhook", Url: "https://example.com", Kind: linkedca.Webhook_ENRICHING}},
  464. }
  465. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  466. adminErr := admin.NewError(admin.ErrorBadRequestType, "webhook url is invalid")
  467. adminErr.Message = "webhook url is invalid"
  468. body := []byte(`{"name": "metadata", "url": "example.com/path", "kind": "ENRICHING"}`)
  469. return test{
  470. ctx: ctx,
  471. adminDB: &admin.MockDB{},
  472. body: body,
  473. err: adminErr,
  474. statusCode: 400,
  475. }
  476. },
  477. "fail/http-url": func(t *testing.T) test {
  478. prov := &linkedca.Provisioner{
  479. Name: "provName",
  480. Webhooks: []*linkedca.Webhook{{Name: "my-webhook", Url: "https://example.com", Kind: linkedca.Webhook_ENRICHING}},
  481. }
  482. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  483. adminErr := admin.NewError(admin.ErrorBadRequestType, "webhook url must use https")
  484. adminErr.Message = "webhook url must use https"
  485. body := []byte(`{"name": "metadata", "url": "http://example.com", "kind": "ENRICHING"}`)
  486. return test{
  487. ctx: ctx,
  488. adminDB: &admin.MockDB{},
  489. body: body,
  490. err: adminErr,
  491. statusCode: 400,
  492. }
  493. },
  494. "fail/basic-auth-in-url": func(t *testing.T) test {
  495. prov := &linkedca.Provisioner{
  496. Name: "provName",
  497. Webhooks: []*linkedca.Webhook{{Name: "my-webhook", Url: "https://example.com", Kind: linkedca.Webhook_ENRICHING}},
  498. }
  499. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  500. adminErr := admin.NewError(admin.ErrorBadRequestType, "webhook url may not contain username or password")
  501. adminErr.Message = "webhook url may not contain username or password"
  502. body := []byte(`
  503. {
  504. "name": "my-webhook",
  505. "url": "https://user:pass@example.com",
  506. "kind": "ENRICHING"
  507. }`)
  508. return test{
  509. ctx: ctx,
  510. adminDB: &admin.MockDB{},
  511. body: body,
  512. err: adminErr,
  513. statusCode: 400,
  514. }
  515. },
  516. "fail/different-secret-in-request": func(t *testing.T) test {
  517. prov := &linkedca.Provisioner{
  518. Name: "provName",
  519. Webhooks: []*linkedca.Webhook{{Name: "my-webhook", Url: "https://example.com", Kind: linkedca.Webhook_ENRICHING, Secret: "c2VjcmV0"}},
  520. }
  521. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  522. adminErr := admin.NewError(admin.ErrorBadRequestType, "webhook secret cannot be updated")
  523. adminErr.Message = "webhook secret cannot be updated"
  524. body := []byte(`
  525. {
  526. "name": "my-webhook",
  527. "url": "https://example.com",
  528. "kind": "ENRICHING",
  529. "secret": "secret"
  530. }`)
  531. return test{
  532. ctx: ctx,
  533. body: body,
  534. err: adminErr,
  535. statusCode: 400,
  536. }
  537. },
  538. "fail/auth.UpdateProvisioner-error": func(t *testing.T) test {
  539. prov := &linkedca.Provisioner{
  540. Name: "provName",
  541. Webhooks: []*linkedca.Webhook{{Name: "my-webhook", Url: "https://example.com", Kind: linkedca.Webhook_ENRICHING}},
  542. }
  543. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  544. adminErr := admin.NewError(admin.ErrorServerInternalType, "error updating provisioner webhook: force")
  545. adminErr.Message = "error updating provisioner webhook: force"
  546. body := []byte(`{"name": "my-webhook", "url": "https://example.com", "kind": "ENRICHING"}`)
  547. return test{
  548. ctx: ctx,
  549. adminDB: &admin.MockDB{},
  550. auth: &mockAdminAuthority{
  551. MockUpdateProvisioner: func(ctx context.Context, nu *linkedca.Provisioner) error {
  552. return &authority.PolicyError{
  553. Typ: authority.StoreFailure,
  554. Err: errors.New("force"),
  555. }
  556. },
  557. },
  558. body: body,
  559. err: adminErr,
  560. statusCode: 500,
  561. }
  562. },
  563. "ok": func(t *testing.T) test {
  564. prov := &linkedca.Provisioner{
  565. Name: "provName",
  566. Webhooks: []*linkedca.Webhook{{Name: "my-webhook", Url: "https://example.com", Kind: linkedca.Webhook_ENRICHING}},
  567. }
  568. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  569. body := []byte(`{"name": "my-webhook", "url": "https://example.com", "kind": "ENRICHING"}`)
  570. return test{
  571. ctx: ctx,
  572. adminDB: &admin.MockDB{},
  573. auth: &mockAdminAuthority{
  574. MockUpdateProvisioner: func(ctx context.Context, nu *linkedca.Provisioner) error {
  575. return nil
  576. },
  577. },
  578. body: body,
  579. response: &linkedca.Webhook{
  580. Name: "my-webhook",
  581. Url: "https://example.com",
  582. Kind: linkedca.Webhook_ENRICHING,
  583. },
  584. statusCode: 201,
  585. }
  586. },
  587. }
  588. for name, prep := range tests {
  589. tc := prep(t)
  590. t.Run(name, func(t *testing.T) {
  591. mockMustAuthority(t, tc.auth)
  592. ctx := admin.NewContext(tc.ctx, tc.adminDB)
  593. war := NewWebhookAdminResponder()
  594. req := httptest.NewRequest("PUT", "/foo", io.NopCloser(bytes.NewBuffer(tc.body)))
  595. req = req.WithContext(ctx)
  596. w := httptest.NewRecorder()
  597. war.UpdateProvisionerWebhook(w, req)
  598. res := w.Result()
  599. assert.Equal(t, tc.statusCode, res.StatusCode)
  600. if res.StatusCode >= 400 {
  601. body, err := io.ReadAll(res.Body)
  602. res.Body.Close()
  603. assert.NoError(t, err)
  604. ae := testAdminError{}
  605. assert.NoError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  606. assert.Equal(t, tc.err.Type, ae.Type)
  607. assert.Equal(t, tc.err.StatusCode(), res.StatusCode)
  608. assert.Equal(t, tc.err.Detail, ae.Detail)
  609. assert.Equal(t, []string{"application/json"}, res.Header["Content-Type"])
  610. // when the error message starts with "proto", we expect it to have
  611. // a syntax error (in the tests). If the message doesn't start with "proto",
  612. // we expect a full string match.
  613. if strings.HasPrefix(tc.err.Message, "proto:") {
  614. assert.True(t, strings.Contains(ae.Message, "syntax error"))
  615. } else {
  616. assert.Equal(t, tc.err.Message, ae.Message)
  617. }
  618. return
  619. }
  620. resp := &linkedca.Webhook{}
  621. body, err := io.ReadAll(res.Body)
  622. assert.NoError(t, err)
  623. assert.NoError(t, protojson.Unmarshal(body, resp))
  624. assertEqualWebhook(t, tc.response, resp)
  625. })
  626. }
  627. }