webhook_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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/unsupported-webhook-kind": func(t *testing.T) test {
  179. prov := &linkedca.Provisioner{
  180. Name: "provName",
  181. }
  182. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  183. adminErr := admin.NewError(admin.ErrorBadRequestType, `(line 5:13): invalid value for enum type: "UNSUPPORTED"`)
  184. adminErr.Message = `(line 5:13): invalid value for enum type: "UNSUPPORTED"`
  185. body := []byte(`
  186. {
  187. "name": "metadata",
  188. "url": "https://example.com",
  189. "kind": "UNSUPPORTED",
  190. }`)
  191. return test{
  192. ctx: ctx,
  193. body: body,
  194. err: adminErr,
  195. statusCode: 400,
  196. }
  197. },
  198. "fail/auth.UpdateProvisioner-error": func(t *testing.T) test {
  199. adm := &linkedca.Admin{
  200. Subject: "step",
  201. }
  202. prov := &linkedca.Provisioner{
  203. Name: "provName",
  204. }
  205. ctx := linkedca.NewContextWithAdmin(context.Background(), adm)
  206. ctx = linkedca.NewContextWithProvisioner(ctx, prov)
  207. adminErr := admin.NewError(admin.ErrorServerInternalType, "error creating provisioner webhook: force")
  208. adminErr.Message = "error creating provisioner webhook: force"
  209. body := []byte(`{"name": "metadata", "url": "https://example.com", "kind": "ENRICHING"}`)
  210. return test{
  211. ctx: ctx,
  212. auth: &mockAdminAuthority{
  213. MockUpdateProvisioner: func(ctx context.Context, nu *linkedca.Provisioner) error {
  214. return &authority.PolicyError{
  215. Typ: authority.StoreFailure,
  216. Err: errors.New("force"),
  217. }
  218. },
  219. },
  220. body: body,
  221. err: adminErr,
  222. statusCode: 500,
  223. }
  224. },
  225. "ok": func(t *testing.T) test {
  226. prov := &linkedca.Provisioner{
  227. Name: "provName",
  228. }
  229. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  230. body := []byte(`{"name": "metadata", "url": "https://example.com", "kind": "ENRICHING", "certType": "X509"}`)
  231. return test{
  232. ctx: ctx,
  233. auth: &mockAdminAuthority{
  234. MockUpdateProvisioner: func(ctx context.Context, nu *linkedca.Provisioner) error {
  235. assert.Equal(t, linkedca.Webhook_X509, nu.Webhooks[0].CertType)
  236. return nil
  237. },
  238. },
  239. body: body,
  240. response: &linkedca.Webhook{
  241. Name: "metadata",
  242. Url: "https://example.com",
  243. Kind: linkedca.Webhook_ENRICHING,
  244. CertType: linkedca.Webhook_X509,
  245. },
  246. statusCode: 201,
  247. }
  248. },
  249. }
  250. for name, prep := range tests {
  251. tc := prep(t)
  252. t.Run(name, func(t *testing.T) {
  253. mockMustAuthority(t, tc.auth)
  254. ctx := admin.NewContext(tc.ctx, &admin.MockDB{})
  255. war := NewWebhookAdminResponder()
  256. req := httptest.NewRequest("POST", "/foo", io.NopCloser(bytes.NewBuffer(tc.body)))
  257. req = req.WithContext(ctx)
  258. w := httptest.NewRecorder()
  259. war.CreateProvisionerWebhook(w, req)
  260. res := w.Result()
  261. assert.Equal(t, tc.statusCode, res.StatusCode)
  262. if res.StatusCode >= 400 {
  263. body, err := io.ReadAll(res.Body)
  264. res.Body.Close()
  265. assert.NoError(t, err)
  266. ae := testAdminError{}
  267. assert.NoError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  268. assert.Equal(t, tc.err.Type, ae.Type)
  269. assert.Equal(t, tc.err.StatusCode(), res.StatusCode)
  270. assert.Equal(t, tc.err.Detail, ae.Detail)
  271. assert.Equal(t, []string{"application/json"}, res.Header["Content-Type"])
  272. // when the error message starts with "proto", we expect it to have
  273. // a syntax error (in the tests). If the message doesn't start with "proto",
  274. // we expect a full string match.
  275. if strings.HasPrefix(tc.err.Message, "proto:") {
  276. assert.True(t, strings.Contains(ae.Message, "syntax error"))
  277. } else {
  278. assert.Equal(t, tc.err.Message, ae.Message)
  279. }
  280. return
  281. }
  282. resp := &linkedca.Webhook{}
  283. body, err := io.ReadAll(res.Body)
  284. assert.NoError(t, err)
  285. assert.NoError(t, protojson.Unmarshal(body, resp))
  286. assertEqualWebhook(t, tc.response, resp)
  287. assert.NotEmpty(t, resp.Secret)
  288. assert.NotEmpty(t, resp.Id)
  289. })
  290. }
  291. }
  292. func TestWebhookAdminResponder_DeleteProvisionerWebhook(t *testing.T) {
  293. type test struct {
  294. auth adminAuthority
  295. err *admin.Error
  296. statusCode int
  297. provisionerWebhooks []*linkedca.Webhook
  298. webhookName string
  299. }
  300. var tests = map[string]func(t *testing.T) test{
  301. "fail/auth.UpdateProvisioner-error": func(t *testing.T) test {
  302. adminErr := admin.NewError(admin.ErrorServerInternalType, "error deleting provisioner webhook: force")
  303. adminErr.Message = "error deleting provisioner webhook: force"
  304. return test{
  305. err: adminErr,
  306. auth: &mockAdminAuthority{
  307. MockUpdateProvisioner: func(ctx context.Context, nu *linkedca.Provisioner) error {
  308. return &authority.PolicyError{
  309. Typ: authority.StoreFailure,
  310. Err: errors.New("force"),
  311. }
  312. },
  313. },
  314. statusCode: 500,
  315. webhookName: "my-webhook",
  316. provisionerWebhooks: []*linkedca.Webhook{
  317. {Name: "my-webhook", Url: "https://example.com", Kind: linkedca.Webhook_ENRICHING},
  318. },
  319. }
  320. },
  321. "ok/not-found": func(t *testing.T) test {
  322. return test{
  323. statusCode: 200,
  324. webhookName: "no-exists",
  325. provisionerWebhooks: nil,
  326. }
  327. },
  328. "ok": func(t *testing.T) test {
  329. return test{
  330. statusCode: 200,
  331. webhookName: "exists",
  332. auth: &mockAdminAuthority{
  333. MockUpdateProvisioner: func(ctx context.Context, nu *linkedca.Provisioner) error {
  334. assert.Equal(t, nu.Webhooks, []*linkedca.Webhook{
  335. {Name: "my-2nd-webhook", Url: "https://example.com", Kind: linkedca.Webhook_ENRICHING},
  336. })
  337. return nil
  338. },
  339. },
  340. provisionerWebhooks: []*linkedca.Webhook{
  341. {Name: "exists", Url: "https.example.com", Kind: linkedca.Webhook_ENRICHING},
  342. {Name: "my-2nd-webhook", Url: "https://example.com", Kind: linkedca.Webhook_ENRICHING},
  343. },
  344. }
  345. },
  346. }
  347. for name, prep := range tests {
  348. tc := prep(t)
  349. t.Run(name, func(t *testing.T) {
  350. mockMustAuthority(t, tc.auth)
  351. chiCtx := chi.NewRouteContext()
  352. chiCtx.URLParams.Add("webhookName", tc.webhookName)
  353. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  354. prov := &linkedca.Provisioner{
  355. Name: "provName",
  356. Webhooks: tc.provisionerWebhooks,
  357. }
  358. ctx = linkedca.NewContextWithProvisioner(ctx, prov)
  359. ctx = admin.NewContext(ctx, &admin.MockDB{})
  360. req := httptest.NewRequest("DELETE", "/foo", nil).WithContext(ctx)
  361. war := NewWebhookAdminResponder()
  362. w := httptest.NewRecorder()
  363. war.DeleteProvisionerWebhook(w, req)
  364. res := w.Result()
  365. assert.Equal(t, tc.statusCode, res.StatusCode)
  366. if res.StatusCode >= 400 {
  367. body, err := io.ReadAll(res.Body)
  368. res.Body.Close()
  369. assert.NoError(t, err)
  370. ae := testAdminError{}
  371. assert.NoError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  372. assert.Equal(t, tc.err.Type, ae.Type)
  373. assert.Equal(t, tc.err.StatusCode(), res.StatusCode)
  374. assert.Equal(t, tc.err.Detail, ae.Detail)
  375. assert.Equal(t, []string{"application/json"}, res.Header["Content-Type"])
  376. // when the error message starts with "proto", we expect it to have
  377. // a syntax error (in the tests). If the message doesn't start with "proto",
  378. // we expect a full string match.
  379. if strings.HasPrefix(tc.err.Message, "proto:") {
  380. assert.True(t, strings.Contains(ae.Message, "syntax error"))
  381. } else {
  382. assert.Equal(t, tc.err.Message, ae.Message)
  383. }
  384. return
  385. }
  386. body, err := io.ReadAll(res.Body)
  387. assert.NoError(t, err)
  388. res.Body.Close()
  389. response := DeleteResponse{}
  390. assert.NoError(t, json.Unmarshal(bytes.TrimSpace(body), &response))
  391. assert.Equal(t, "ok", response.Status)
  392. assert.Equal(t, []string{"application/json"}, res.Header["Content-Type"])
  393. })
  394. }
  395. }
  396. func TestWebhookAdminResponder_UpdateProvisionerWebhook(t *testing.T) {
  397. type test struct {
  398. auth adminAuthority
  399. adminDB admin.DB
  400. body []byte
  401. ctx context.Context
  402. err *admin.Error
  403. response *linkedca.Webhook
  404. statusCode int
  405. }
  406. var tests = map[string]func(t *testing.T) test{
  407. "fail/not-found": func(t *testing.T) test {
  408. prov := &linkedca.Provisioner{
  409. Name: "provName",
  410. Webhooks: []*linkedca.Webhook{{Name: "exists", Url: "https://example.com", Kind: linkedca.Webhook_ENRICHING}},
  411. }
  412. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  413. err := admin.NewError(admin.ErrorNotFoundType, `provisioner "provName" has no webhook with the name "no-exists"`)
  414. err.Message = `provisioner "provName" has no webhook with the name "no-exists"`
  415. body := []byte(`
  416. {
  417. "name": "no-exists",
  418. "url": "https://example.com",
  419. "kind": "ENRICHING"
  420. }`)
  421. return test{
  422. ctx: ctx,
  423. adminDB: &admin.MockDB{},
  424. body: body,
  425. err: err,
  426. statusCode: 404,
  427. }
  428. },
  429. "fail/read.ProtoJSON": func(t *testing.T) test {
  430. prov := &linkedca.Provisioner{
  431. Name: "provName",
  432. Webhooks: []*linkedca.Webhook{{Name: "my-webhook", Url: "https://example.com", Kind: linkedca.Webhook_ENRICHING}},
  433. }
  434. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  435. adminErr := admin.NewError(admin.ErrorBadRequestType, "proto: syntax error (line 1:2): invalid value ?")
  436. adminErr.Message = "proto: syntax error (line 1:2): invalid value ?"
  437. body := []byte("{?}")
  438. return test{
  439. ctx: ctx,
  440. adminDB: &admin.MockDB{},
  441. body: body,
  442. err: adminErr,
  443. statusCode: 400,
  444. }
  445. },
  446. "fail/missing-name": func(t *testing.T) test {
  447. prov := &linkedca.Provisioner{
  448. Name: "provName",
  449. Webhooks: []*linkedca.Webhook{{Name: "my-webhook", Url: "https://example.com", Kind: linkedca.Webhook_ENRICHING}},
  450. }
  451. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  452. adminErr := admin.NewError(admin.ErrorBadRequestType, "webhook name is required")
  453. adminErr.Message = "webhook name is required"
  454. body := []byte(`{"url": "https://example.com", "kind": "ENRICHING"}`)
  455. return test{
  456. ctx: ctx,
  457. adminDB: &admin.MockDB{},
  458. body: body,
  459. err: adminErr,
  460. statusCode: 400,
  461. }
  462. },
  463. "fail/missing-url": func(t *testing.T) test {
  464. prov := &linkedca.Provisioner{
  465. Name: "provName",
  466. Webhooks: []*linkedca.Webhook{{Name: "my-webhook", Url: "https://example.com", Kind: linkedca.Webhook_ENRICHING}},
  467. }
  468. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  469. adminErr := admin.NewError(admin.ErrorBadRequestType, "webhook url is invalid")
  470. adminErr.Message = "webhook url is invalid"
  471. body := []byte(`{"name": "metadata", "kind": "ENRICHING"}`)
  472. return test{
  473. ctx: ctx,
  474. adminDB: &admin.MockDB{},
  475. body: body,
  476. err: adminErr,
  477. statusCode: 400,
  478. }
  479. },
  480. "fail/relative-url": func(t *testing.T) test {
  481. prov := &linkedca.Provisioner{
  482. Name: "provName",
  483. Webhooks: []*linkedca.Webhook{{Name: "my-webhook", Url: "https://example.com", Kind: linkedca.Webhook_ENRICHING}},
  484. }
  485. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  486. adminErr := admin.NewError(admin.ErrorBadRequestType, "webhook url is invalid")
  487. adminErr.Message = "webhook url is invalid"
  488. body := []byte(`{"name": "metadata", "url": "example.com/path", "kind": "ENRICHING"}`)
  489. return test{
  490. ctx: ctx,
  491. adminDB: &admin.MockDB{},
  492. body: body,
  493. err: adminErr,
  494. statusCode: 400,
  495. }
  496. },
  497. "fail/http-url": func(t *testing.T) test {
  498. prov := &linkedca.Provisioner{
  499. Name: "provName",
  500. Webhooks: []*linkedca.Webhook{{Name: "my-webhook", Url: "https://example.com", Kind: linkedca.Webhook_ENRICHING}},
  501. }
  502. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  503. adminErr := admin.NewError(admin.ErrorBadRequestType, "webhook url must use https")
  504. adminErr.Message = "webhook url must use https"
  505. body := []byte(`{"name": "metadata", "url": "http://example.com", "kind": "ENRICHING"}`)
  506. return test{
  507. ctx: ctx,
  508. adminDB: &admin.MockDB{},
  509. body: body,
  510. err: adminErr,
  511. statusCode: 400,
  512. }
  513. },
  514. "fail/basic-auth-in-url": func(t *testing.T) test {
  515. prov := &linkedca.Provisioner{
  516. Name: "provName",
  517. Webhooks: []*linkedca.Webhook{{Name: "my-webhook", Url: "https://example.com", Kind: linkedca.Webhook_ENRICHING}},
  518. }
  519. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  520. adminErr := admin.NewError(admin.ErrorBadRequestType, "webhook url may not contain username or password")
  521. adminErr.Message = "webhook url may not contain username or password"
  522. body := []byte(`
  523. {
  524. "name": "my-webhook",
  525. "url": "https://user:pass@example.com",
  526. "kind": "ENRICHING"
  527. }`)
  528. return test{
  529. ctx: ctx,
  530. adminDB: &admin.MockDB{},
  531. body: body,
  532. err: adminErr,
  533. statusCode: 400,
  534. }
  535. },
  536. "fail/different-secret-in-request": func(t *testing.T) test {
  537. prov := &linkedca.Provisioner{
  538. Name: "provName",
  539. Webhooks: []*linkedca.Webhook{{Name: "my-webhook", Url: "https://example.com", Kind: linkedca.Webhook_ENRICHING, Secret: "c2VjcmV0"}},
  540. }
  541. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  542. adminErr := admin.NewError(admin.ErrorBadRequestType, "webhook secret cannot be updated")
  543. adminErr.Message = "webhook secret cannot be updated"
  544. body := []byte(`
  545. {
  546. "name": "my-webhook",
  547. "url": "https://example.com",
  548. "kind": "ENRICHING",
  549. "secret": "secret"
  550. }`)
  551. return test{
  552. ctx: ctx,
  553. body: body,
  554. err: adminErr,
  555. statusCode: 400,
  556. }
  557. },
  558. "fail/auth.UpdateProvisioner-error": func(t *testing.T) test {
  559. prov := &linkedca.Provisioner{
  560. Name: "provName",
  561. Webhooks: []*linkedca.Webhook{{Name: "my-webhook", Url: "https://example.com", Kind: linkedca.Webhook_ENRICHING}},
  562. }
  563. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  564. adminErr := admin.NewError(admin.ErrorServerInternalType, "error updating provisioner webhook: force")
  565. adminErr.Message = "error updating provisioner webhook: force"
  566. body := []byte(`{"name": "my-webhook", "url": "https://example.com", "kind": "ENRICHING"}`)
  567. return test{
  568. ctx: ctx,
  569. adminDB: &admin.MockDB{},
  570. auth: &mockAdminAuthority{
  571. MockUpdateProvisioner: func(ctx context.Context, nu *linkedca.Provisioner) error {
  572. return &authority.PolicyError{
  573. Typ: authority.StoreFailure,
  574. Err: errors.New("force"),
  575. }
  576. },
  577. },
  578. body: body,
  579. err: adminErr,
  580. statusCode: 500,
  581. }
  582. },
  583. "ok": func(t *testing.T) test {
  584. prov := &linkedca.Provisioner{
  585. Name: "provName",
  586. Webhooks: []*linkedca.Webhook{{Name: "my-webhook", Url: "https://example.com", Kind: linkedca.Webhook_ENRICHING}},
  587. }
  588. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  589. body := []byte(`{"name": "my-webhook", "url": "https://example.com", "kind": "ENRICHING"}`)
  590. return test{
  591. ctx: ctx,
  592. adminDB: &admin.MockDB{},
  593. auth: &mockAdminAuthority{
  594. MockUpdateProvisioner: func(ctx context.Context, nu *linkedca.Provisioner) error {
  595. return nil
  596. },
  597. },
  598. body: body,
  599. response: &linkedca.Webhook{
  600. Name: "my-webhook",
  601. Url: "https://example.com",
  602. Kind: linkedca.Webhook_ENRICHING,
  603. },
  604. statusCode: 201,
  605. }
  606. },
  607. }
  608. for name, prep := range tests {
  609. tc := prep(t)
  610. t.Run(name, func(t *testing.T) {
  611. mockMustAuthority(t, tc.auth)
  612. ctx := admin.NewContext(tc.ctx, tc.adminDB)
  613. war := NewWebhookAdminResponder()
  614. req := httptest.NewRequest("PUT", "/foo", io.NopCloser(bytes.NewBuffer(tc.body)))
  615. req = req.WithContext(ctx)
  616. w := httptest.NewRecorder()
  617. war.UpdateProvisionerWebhook(w, req)
  618. res := w.Result()
  619. assert.Equal(t, tc.statusCode, res.StatusCode)
  620. if res.StatusCode >= 400 {
  621. body, err := io.ReadAll(res.Body)
  622. res.Body.Close()
  623. assert.NoError(t, err)
  624. ae := testAdminError{}
  625. assert.NoError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  626. assert.Equal(t, tc.err.Type, ae.Type)
  627. assert.Equal(t, tc.err.StatusCode(), res.StatusCode)
  628. assert.Equal(t, tc.err.Detail, ae.Detail)
  629. assert.Equal(t, []string{"application/json"}, res.Header["Content-Type"])
  630. // when the error message starts with "proto", we expect it to have
  631. // a syntax error (in the tests). If the message doesn't start with "proto",
  632. // we expect a full string match.
  633. if strings.HasPrefix(tc.err.Message, "proto:") {
  634. assert.True(t, strings.Contains(ae.Message, "syntax error"))
  635. } else {
  636. assert.Equal(t, tc.err.Message, ae.Message)
  637. }
  638. return
  639. }
  640. resp := &linkedca.Webhook{}
  641. body, err := io.ReadAll(res.Body)
  642. assert.NoError(t, err)
  643. assert.NoError(t, protojson.Unmarshal(body, resp))
  644. assertEqualWebhook(t, tc.response, resp)
  645. })
  646. }
  647. }