webhook_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. package provisioner
  2. import (
  3. "context"
  4. "crypto/hmac"
  5. "crypto/sha256"
  6. "crypto/tls"
  7. "crypto/x509"
  8. "encoding/base64"
  9. "encoding/hex"
  10. "encoding/json"
  11. "errors"
  12. "fmt"
  13. "io"
  14. "net/http"
  15. "net/http/httptest"
  16. "testing"
  17. "time"
  18. "github.com/stretchr/testify/assert"
  19. "github.com/stretchr/testify/require"
  20. "go.step.sm/crypto/pemutil"
  21. "go.step.sm/crypto/x509util"
  22. "go.step.sm/linkedca"
  23. "github.com/smallstep/certificates/internal/requestid"
  24. "github.com/smallstep/certificates/webhook"
  25. )
  26. func TestWebhookController_isCertTypeOK(t *testing.T) {
  27. type test struct {
  28. wc *WebhookController
  29. wh *Webhook
  30. want bool
  31. }
  32. tests := map[string]test{
  33. "all/all": {
  34. wc: &WebhookController{certType: linkedca.Webhook_ALL},
  35. wh: &Webhook{CertType: linkedca.Webhook_ALL.String()},
  36. want: true,
  37. },
  38. "all/x509": {
  39. wc: &WebhookController{certType: linkedca.Webhook_ALL},
  40. wh: &Webhook{CertType: linkedca.Webhook_X509.String()},
  41. want: true,
  42. },
  43. "all/ssh": {
  44. wc: &WebhookController{certType: linkedca.Webhook_ALL},
  45. wh: &Webhook{CertType: linkedca.Webhook_SSH.String()},
  46. want: true,
  47. },
  48. `all/""`: {
  49. wc: &WebhookController{certType: linkedca.Webhook_ALL},
  50. wh: &Webhook{},
  51. want: true,
  52. },
  53. "x509/all": {
  54. wc: &WebhookController{certType: linkedca.Webhook_X509},
  55. wh: &Webhook{CertType: linkedca.Webhook_ALL.String()},
  56. want: true,
  57. },
  58. "x509/x509": {
  59. wc: &WebhookController{certType: linkedca.Webhook_X509},
  60. wh: &Webhook{CertType: linkedca.Webhook_X509.String()},
  61. want: true,
  62. },
  63. "x509/ssh": {
  64. wc: &WebhookController{certType: linkedca.Webhook_X509},
  65. wh: &Webhook{CertType: linkedca.Webhook_SSH.String()},
  66. want: false,
  67. },
  68. `x509/""`: {
  69. wc: &WebhookController{certType: linkedca.Webhook_X509},
  70. wh: &Webhook{},
  71. want: true,
  72. },
  73. "ssh/all": {
  74. wc: &WebhookController{certType: linkedca.Webhook_SSH},
  75. wh: &Webhook{CertType: linkedca.Webhook_ALL.String()},
  76. want: true,
  77. },
  78. "ssh/x509": {
  79. wc: &WebhookController{certType: linkedca.Webhook_SSH},
  80. wh: &Webhook{CertType: linkedca.Webhook_X509.String()},
  81. want: false,
  82. },
  83. "ssh/ssh": {
  84. wc: &WebhookController{certType: linkedca.Webhook_SSH},
  85. wh: &Webhook{CertType: linkedca.Webhook_SSH.String()},
  86. want: true,
  87. },
  88. `ssh/""`: {
  89. wc: &WebhookController{certType: linkedca.Webhook_SSH},
  90. wh: &Webhook{},
  91. want: true,
  92. },
  93. }
  94. for name, test := range tests {
  95. t.Run(name, func(t *testing.T) {
  96. assert.Equal(t, test.want, test.wc.isCertTypeOK(test.wh))
  97. })
  98. }
  99. }
  100. // withRequestID is a helper that calls into [requestid.NewContext] and returns
  101. // a new context with the requestID added.
  102. func withRequestID(t *testing.T, ctx context.Context, requestID string) context.Context {
  103. t.Helper()
  104. return requestid.NewContext(ctx, requestID)
  105. }
  106. func TestWebhookController_Enrich(t *testing.T) {
  107. cert, err := pemutil.ReadCertificate("testdata/certs/x5c-leaf.crt", pemutil.WithFirstBlock())
  108. require.NoError(t, err)
  109. type test struct {
  110. ctl *WebhookController
  111. ctx context.Context
  112. req *webhook.RequestBody
  113. responses []*webhook.ResponseBody
  114. expectErr bool
  115. expectTemplateData any
  116. assertRequest func(t *testing.T, req *webhook.RequestBody)
  117. }
  118. tests := map[string]test{
  119. "ok/no enriching webhooks": {
  120. ctl: &WebhookController{
  121. client: http.DefaultClient,
  122. webhooks: []*Webhook{{Name: "people", Kind: "AUTHORIZING"}},
  123. TemplateData: nil,
  124. },
  125. req: &webhook.RequestBody{},
  126. responses: nil,
  127. expectErr: false,
  128. expectTemplateData: nil,
  129. },
  130. "ok/one webhook": {
  131. ctl: &WebhookController{
  132. client: http.DefaultClient,
  133. webhooks: []*Webhook{{Name: "people", Kind: "ENRICHING"}},
  134. TemplateData: x509util.TemplateData{},
  135. },
  136. ctx: withRequestID(t, context.Background(), "reqID"),
  137. req: &webhook.RequestBody{},
  138. responses: []*webhook.ResponseBody{{Allow: true, Data: map[string]any{"role": "bar"}}},
  139. expectErr: false,
  140. expectTemplateData: x509util.TemplateData{"Webhooks": map[string]any{"people": map[string]any{"role": "bar"}}},
  141. },
  142. "ok/two webhooks": {
  143. ctl: &WebhookController{
  144. client: http.DefaultClient,
  145. webhooks: []*Webhook{
  146. {Name: "people", Kind: "ENRICHING"},
  147. {Name: "devices", Kind: "ENRICHING"},
  148. },
  149. TemplateData: x509util.TemplateData{},
  150. },
  151. ctx: withRequestID(t, context.Background(), "reqID"),
  152. req: &webhook.RequestBody{},
  153. responses: []*webhook.ResponseBody{
  154. {Allow: true, Data: map[string]any{"role": "bar"}},
  155. {Allow: true, Data: map[string]any{"serial": "123"}},
  156. },
  157. expectErr: false,
  158. expectTemplateData: x509util.TemplateData{
  159. "Webhooks": map[string]any{
  160. "devices": map[string]any{"serial": "123"},
  161. "people": map[string]any{"role": "bar"},
  162. },
  163. },
  164. },
  165. "ok/x509 only": {
  166. ctl: &WebhookController{
  167. client: http.DefaultClient,
  168. webhooks: []*Webhook{
  169. {Name: "people", Kind: "ENRICHING", CertType: linkedca.Webhook_SSH.String()},
  170. {Name: "devices", Kind: "ENRICHING"},
  171. },
  172. TemplateData: x509util.TemplateData{},
  173. certType: linkedca.Webhook_X509,
  174. },
  175. ctx: withRequestID(t, context.Background(), "reqID"),
  176. req: &webhook.RequestBody{},
  177. responses: []*webhook.ResponseBody{
  178. {Allow: true, Data: map[string]any{"role": "bar"}},
  179. {Allow: true, Data: map[string]any{"serial": "123"}},
  180. },
  181. expectErr: false,
  182. expectTemplateData: x509util.TemplateData{
  183. "Webhooks": map[string]any{
  184. "devices": map[string]any{"serial": "123"},
  185. },
  186. },
  187. },
  188. "ok/with options": {
  189. ctl: &WebhookController{
  190. client: http.DefaultClient,
  191. webhooks: []*Webhook{{Name: "people", Kind: "ENRICHING"}},
  192. TemplateData: x509util.TemplateData{},
  193. options: []webhook.RequestBodyOption{webhook.WithX5CCertificate(cert)},
  194. },
  195. ctx: withRequestID(t, context.Background(), "reqID"),
  196. req: &webhook.RequestBody{},
  197. responses: []*webhook.ResponseBody{{Allow: true, Data: map[string]any{"role": "bar"}}},
  198. expectErr: false,
  199. expectTemplateData: x509util.TemplateData{"Webhooks": map[string]any{"people": map[string]any{"role": "bar"}}},
  200. assertRequest: func(t *testing.T, req *webhook.RequestBody) {
  201. key, err := x509.MarshalPKIXPublicKey(cert.PublicKey)
  202. require.NoError(t, err)
  203. assert.Equal(t, &webhook.X5CCertificate{
  204. Raw: cert.Raw,
  205. PublicKey: key,
  206. PublicKeyAlgorithm: cert.PublicKeyAlgorithm.String(),
  207. NotBefore: cert.NotBefore,
  208. NotAfter: cert.NotAfter,
  209. }, req.X5CCertificate)
  210. },
  211. },
  212. "deny": {
  213. ctl: &WebhookController{
  214. client: http.DefaultClient,
  215. webhooks: []*Webhook{{Name: "people", Kind: "ENRICHING"}},
  216. TemplateData: x509util.TemplateData{},
  217. },
  218. ctx: withRequestID(t, context.Background(), "reqID"),
  219. req: &webhook.RequestBody{},
  220. responses: []*webhook.ResponseBody{{Allow: false}},
  221. expectErr: true,
  222. expectTemplateData: x509util.TemplateData{},
  223. },
  224. "fail/with options": {
  225. ctl: &WebhookController{
  226. client: http.DefaultClient,
  227. webhooks: []*Webhook{{Name: "people", Kind: "ENRICHING"}},
  228. TemplateData: x509util.TemplateData{},
  229. options: []webhook.RequestBodyOption{webhook.WithX5CCertificate(&x509.Certificate{
  230. PublicKey: []byte("bad"),
  231. })},
  232. },
  233. ctx: withRequestID(t, context.Background(), "reqID"),
  234. req: &webhook.RequestBody{},
  235. responses: []*webhook.ResponseBody{{Allow: false}},
  236. expectErr: true,
  237. expectTemplateData: x509util.TemplateData{},
  238. },
  239. }
  240. for name, test := range tests {
  241. t.Run(name, func(t *testing.T) {
  242. for i, wh := range test.ctl.webhooks {
  243. var j = i
  244. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  245. assert.Equal(t, "reqID", r.Header.Get("X-Request-ID"))
  246. err := json.NewEncoder(w).Encode(test.responses[j])
  247. require.NoError(t, err)
  248. }))
  249. // nolint: gocritic // defer in loop isn't a memory leak
  250. defer ts.Close()
  251. wh.URL = ts.URL
  252. }
  253. err := test.ctl.Enrich(test.ctx, test.req)
  254. if (err != nil) != test.expectErr {
  255. t.Fatalf("Got err %v, want %v", err, test.expectErr)
  256. }
  257. assert.Equal(t, test.expectTemplateData, test.ctl.TemplateData)
  258. if test.assertRequest != nil {
  259. test.assertRequest(t, test.req)
  260. }
  261. })
  262. }
  263. }
  264. func TestWebhookController_Authorize(t *testing.T) {
  265. cert, err := pemutil.ReadCertificate("testdata/certs/x5c-leaf.crt", pemutil.WithFirstBlock())
  266. require.NoError(t, err)
  267. type test struct {
  268. ctl *WebhookController
  269. ctx context.Context
  270. req *webhook.RequestBody
  271. responses []*webhook.ResponseBody
  272. expectErr bool
  273. assertRequest func(t *testing.T, req *webhook.RequestBody)
  274. }
  275. tests := map[string]test{
  276. "ok/no enriching webhooks": {
  277. ctl: &WebhookController{
  278. client: http.DefaultClient,
  279. webhooks: []*Webhook{{Name: "people", Kind: "ENRICHING"}},
  280. },
  281. req: &webhook.RequestBody{},
  282. responses: nil,
  283. expectErr: false,
  284. },
  285. "ok": {
  286. ctl: &WebhookController{
  287. client: http.DefaultClient,
  288. webhooks: []*Webhook{{Name: "people", Kind: "AUTHORIZING"}},
  289. },
  290. ctx: withRequestID(t, context.Background(), "reqID"),
  291. req: &webhook.RequestBody{},
  292. responses: []*webhook.ResponseBody{{Allow: true}},
  293. expectErr: false,
  294. },
  295. "ok/ssh only": {
  296. ctl: &WebhookController{
  297. client: http.DefaultClient,
  298. webhooks: []*Webhook{{Name: "people", Kind: "AUTHORIZING", CertType: linkedca.Webhook_X509.String()}},
  299. certType: linkedca.Webhook_SSH,
  300. },
  301. ctx: withRequestID(t, context.Background(), "reqID"),
  302. req: &webhook.RequestBody{},
  303. responses: []*webhook.ResponseBody{{Allow: false}},
  304. expectErr: false,
  305. },
  306. "ok/with options": {
  307. ctl: &WebhookController{
  308. client: http.DefaultClient,
  309. webhooks: []*Webhook{{Name: "people", Kind: "AUTHORIZING"}},
  310. options: []webhook.RequestBodyOption{webhook.WithX5CCertificate(cert)},
  311. },
  312. ctx: withRequestID(t, context.Background(), "reqID"),
  313. req: &webhook.RequestBody{},
  314. responses: []*webhook.ResponseBody{{Allow: true}},
  315. expectErr: false,
  316. assertRequest: func(t *testing.T, req *webhook.RequestBody) {
  317. key, err := x509.MarshalPKIXPublicKey(cert.PublicKey)
  318. require.NoError(t, err)
  319. assert.Equal(t, &webhook.X5CCertificate{
  320. Raw: cert.Raw,
  321. PublicKey: key,
  322. PublicKeyAlgorithm: cert.PublicKeyAlgorithm.String(),
  323. NotBefore: cert.NotBefore,
  324. NotAfter: cert.NotAfter,
  325. }, req.X5CCertificate)
  326. },
  327. },
  328. "deny": {
  329. ctl: &WebhookController{
  330. client: http.DefaultClient,
  331. webhooks: []*Webhook{{Name: "people", Kind: "AUTHORIZING"}},
  332. },
  333. ctx: withRequestID(t, context.Background(), "reqID"),
  334. req: &webhook.RequestBody{},
  335. responses: []*webhook.ResponseBody{{Allow: false}},
  336. expectErr: true,
  337. },
  338. "fail/with options": {
  339. ctl: &WebhookController{
  340. client: http.DefaultClient,
  341. webhooks: []*Webhook{{Name: "people", Kind: "AUTHORIZING"}},
  342. options: []webhook.RequestBodyOption{webhook.WithX5CCertificate(&x509.Certificate{
  343. PublicKey: []byte("bad"),
  344. })},
  345. },
  346. ctx: withRequestID(t, context.Background(), "reqID"),
  347. req: &webhook.RequestBody{},
  348. responses: []*webhook.ResponseBody{{Allow: false}},
  349. expectErr: true,
  350. },
  351. }
  352. for name, test := range tests {
  353. t.Run(name, func(t *testing.T) {
  354. for i, wh := range test.ctl.webhooks {
  355. var j = i
  356. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  357. assert.Equal(t, "reqID", r.Header.Get("X-Request-ID"))
  358. err := json.NewEncoder(w).Encode(test.responses[j])
  359. require.NoError(t, err)
  360. }))
  361. // nolint: gocritic // defer in loop isn't a memory leak
  362. defer ts.Close()
  363. wh.URL = ts.URL
  364. }
  365. err := test.ctl.Authorize(test.ctx, test.req)
  366. if (err != nil) != test.expectErr {
  367. t.Fatalf("Got err %v, want %v", err, test.expectErr)
  368. }
  369. if test.assertRequest != nil {
  370. test.assertRequest(t, test.req)
  371. }
  372. })
  373. }
  374. }
  375. func TestWebhook_Do(t *testing.T) {
  376. csr := parseCertificateRequest(t, "testdata/certs/ecdsa.csr")
  377. type test struct {
  378. webhook Webhook
  379. dataArg any
  380. requestID string
  381. webhookResponse webhook.ResponseBody
  382. expectPath string
  383. errStatusCode int
  384. serverErrMsg string
  385. expectErr error
  386. // expectToken any
  387. }
  388. tests := map[string]test{
  389. "ok": {
  390. webhook: Webhook{
  391. ID: "abc123",
  392. Secret: "c2VjcmV0Cg==",
  393. },
  394. requestID: "reqID",
  395. webhookResponse: webhook.ResponseBody{
  396. Data: map[string]interface{}{"role": "dba"},
  397. },
  398. },
  399. "ok/no-request-id": {
  400. webhook: Webhook{
  401. ID: "abc123",
  402. Secret: "c2VjcmV0Cg==",
  403. },
  404. webhookResponse: webhook.ResponseBody{
  405. Data: map[string]interface{}{"role": "dba"},
  406. },
  407. },
  408. "ok/bearer": {
  409. webhook: Webhook{
  410. ID: "abc123",
  411. Secret: "c2VjcmV0Cg==",
  412. BearerToken: "mytoken",
  413. },
  414. requestID: "reqID",
  415. webhookResponse: webhook.ResponseBody{
  416. Data: map[string]interface{}{"role": "dba"},
  417. },
  418. },
  419. "ok/basic": {
  420. webhook: Webhook{
  421. ID: "abc123",
  422. Secret: "c2VjcmV0Cg==",
  423. BasicAuth: struct {
  424. Username string
  425. Password string
  426. }{
  427. Username: "myuser",
  428. Password: "mypass",
  429. },
  430. },
  431. requestID: "reqID",
  432. webhookResponse: webhook.ResponseBody{
  433. Data: map[string]interface{}{"role": "dba"},
  434. },
  435. },
  436. "ok/templated-url": {
  437. webhook: Webhook{
  438. ID: "abc123",
  439. // scheme, host, port will come from test server
  440. URL: "/users/{{ .username }}?region={{ .region }}",
  441. Secret: "c2VjcmV0Cg==",
  442. },
  443. requestID: "reqID",
  444. dataArg: map[string]interface{}{"username": "areed", "region": "central"},
  445. webhookResponse: webhook.ResponseBody{
  446. Data: map[string]interface{}{"role": "dba"},
  447. },
  448. expectPath: "/users/areed?region=central",
  449. },
  450. /*
  451. "ok/token from ssh template": {
  452. webhook: Webhook{
  453. ID: "abc123",
  454. Secret: "c2VjcmV0Cg==",
  455. },
  456. webhookResponse: webhook.ResponseBody{
  457. Data: map[string]interface{}{"role": "dba"},
  458. },
  459. dataArg: sshutil.TemplateData{sshutil.TokenKey: "token"},
  460. expectToken: "token",
  461. },
  462. "ok/token from x509 template": {
  463. webhook: Webhook{
  464. ID: "abc123",
  465. Secret: "c2VjcmV0Cg==",
  466. },
  467. webhookResponse: webhook.ResponseBody{
  468. Data: map[string]interface{}{"role": "dba"},
  469. },
  470. dataArg: x509util.TemplateData{sshutil.TokenKey: "token"},
  471. expectToken: "token",
  472. },
  473. */
  474. "ok/allow": {
  475. webhook: Webhook{
  476. ID: "abc123",
  477. Secret: "c2VjcmV0Cg==",
  478. },
  479. requestID: "reqID",
  480. webhookResponse: webhook.ResponseBody{
  481. Allow: true,
  482. },
  483. },
  484. "fail/404": {
  485. webhook: Webhook{
  486. ID: "abc123",
  487. Secret: "c2VjcmV0Cg==",
  488. },
  489. webhookResponse: webhook.ResponseBody{
  490. Data: map[string]interface{}{"role": "dba"},
  491. },
  492. requestID: "reqID",
  493. errStatusCode: 404,
  494. serverErrMsg: "item not found",
  495. expectErr: errors.New("Webhook server responded with 404"),
  496. },
  497. }
  498. for name, tc := range tests {
  499. t.Run(name, func(t *testing.T) {
  500. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  501. if tc.requestID != "" {
  502. assert.Equal(t, tc.requestID, r.Header.Get("X-Request-ID"))
  503. }
  504. assert.Equal(t, tc.webhook.ID, r.Header.Get("X-Smallstep-Webhook-ID"))
  505. sig, err := hex.DecodeString(r.Header.Get("X-Smallstep-Signature"))
  506. assert.NoError(t, err)
  507. body, err := io.ReadAll(r.Body)
  508. assert.NoError(t, err)
  509. secret, err := base64.StdEncoding.DecodeString(tc.webhook.Secret)
  510. assert.NoError(t, err)
  511. h := hmac.New(sha256.New, secret)
  512. h.Write(body)
  513. mac := h.Sum(nil)
  514. assert.True(t, hmac.Equal(sig, mac))
  515. switch {
  516. case tc.webhook.BearerToken != "":
  517. ah := fmt.Sprintf("Bearer %s", tc.webhook.BearerToken)
  518. assert.Equal(t, ah, r.Header.Get("Authorization"))
  519. case tc.webhook.BasicAuth.Username != "" || tc.webhook.BasicAuth.Password != "":
  520. whReq, err := http.NewRequest("", "", http.NoBody)
  521. require.NoError(t, err)
  522. whReq.SetBasicAuth(tc.webhook.BasicAuth.Username, tc.webhook.BasicAuth.Password)
  523. ah := whReq.Header.Get("Authorization")
  524. assert.Equal(t, ah, whReq.Header.Get("Authorization"))
  525. default:
  526. assert.Equal(t, "", r.Header.Get("Authorization"))
  527. }
  528. if tc.expectPath != "" {
  529. assert.Equal(t, tc.expectPath, r.URL.Path+"?"+r.URL.RawQuery)
  530. }
  531. if tc.errStatusCode != 0 {
  532. http.Error(w, tc.serverErrMsg, tc.errStatusCode)
  533. return
  534. }
  535. reqBody := new(webhook.RequestBody)
  536. err = json.Unmarshal(body, reqBody)
  537. require.NoError(t, err)
  538. err = json.NewEncoder(w).Encode(tc.webhookResponse)
  539. require.NoError(t, err)
  540. }))
  541. defer ts.Close()
  542. tc.webhook.URL = ts.URL + tc.webhook.URL
  543. reqBody, err := webhook.NewRequestBody(webhook.WithX509CertificateRequest(csr))
  544. require.NoError(t, err)
  545. ctx := context.Background()
  546. if tc.requestID != "" {
  547. ctx = withRequestID(t, ctx, tc.requestID)
  548. }
  549. ctx, cancel := context.WithTimeout(ctx, time.Second*10)
  550. defer cancel()
  551. got, err := tc.webhook.DoWithContext(ctx, http.DefaultClient, reqBody, tc.dataArg)
  552. if tc.expectErr != nil {
  553. assert.Equal(t, tc.expectErr.Error(), err.Error())
  554. return
  555. }
  556. assert.NoError(t, err)
  557. assert.Equal(t, &tc.webhookResponse, got)
  558. })
  559. }
  560. t.Run("disableTLSClientAuth", func(t *testing.T) {
  561. ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  562. w.Write([]byte("{}"))
  563. }))
  564. ts.TLS.ClientAuth = tls.RequireAnyClientCert
  565. wh := Webhook{
  566. URL: ts.URL,
  567. }
  568. cert, err := tls.LoadX509KeyPair("testdata/certs/foo.crt", "testdata/secrets/foo.key")
  569. require.NoError(t, err)
  570. transport := http.DefaultTransport.(*http.Transport).Clone()
  571. transport.TLSClientConfig = &tls.Config{
  572. InsecureSkipVerify: true,
  573. Certificates: []tls.Certificate{cert},
  574. }
  575. client := &http.Client{
  576. Transport: transport,
  577. }
  578. reqBody, err := webhook.NewRequestBody(webhook.WithX509CertificateRequest(csr))
  579. require.NoError(t, err)
  580. ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
  581. defer cancel()
  582. _, err = wh.DoWithContext(ctx, client, reqBody, nil)
  583. require.NoError(t, err)
  584. ctx, cancel = context.WithTimeout(context.Background(), time.Second*10)
  585. defer cancel()
  586. wh.DisableTLSClientAuth = true
  587. _, err = wh.DoWithContext(ctx, client, reqBody, nil)
  588. require.Error(t, err)
  589. })
  590. }