4
0

webhook_test.go 22 KB

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