| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699 |
- package provisioner
- import (
- "context"
- "crypto/hmac"
- "crypto/sha256"
- "crypto/tls"
- "crypto/x509"
- _ "embed"
- "encoding/base64"
- "encoding/hex"
- "encoding/json"
- "errors"
- "fmt"
- "io"
- "net/http"
- "net/http/httptest"
- "testing"
- "time"
- "github.com/smallstep/certificates/internal/httptransport"
- "github.com/smallstep/certificates/middleware/requestid"
- "github.com/smallstep/certificates/webhook"
- "github.com/smallstep/linkedca"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- "go.step.sm/crypto/pemutil"
- "go.step.sm/crypto/x509util"
- )
- func TestWebhookController_isCertTypeOK(t *testing.T) {
- type test struct {
- wc *WebhookController
- wh *Webhook
- want bool
- }
- tests := map[string]test{
- "all/all": {
- wc: &WebhookController{certType: linkedca.Webhook_ALL},
- wh: &Webhook{CertType: linkedca.Webhook_ALL.String()},
- want: true,
- },
- "all/x509": {
- wc: &WebhookController{certType: linkedca.Webhook_ALL},
- wh: &Webhook{CertType: linkedca.Webhook_X509.String()},
- want: true,
- },
- "all/ssh": {
- wc: &WebhookController{certType: linkedca.Webhook_ALL},
- wh: &Webhook{CertType: linkedca.Webhook_SSH.String()},
- want: true,
- },
- `all/""`: {
- wc: &WebhookController{certType: linkedca.Webhook_ALL},
- wh: &Webhook{},
- want: true,
- },
- "x509/all": {
- wc: &WebhookController{certType: linkedca.Webhook_X509},
- wh: &Webhook{CertType: linkedca.Webhook_ALL.String()},
- want: true,
- },
- "x509/x509": {
- wc: &WebhookController{certType: linkedca.Webhook_X509},
- wh: &Webhook{CertType: linkedca.Webhook_X509.String()},
- want: true,
- },
- "x509/ssh": {
- wc: &WebhookController{certType: linkedca.Webhook_X509},
- wh: &Webhook{CertType: linkedca.Webhook_SSH.String()},
- want: false,
- },
- `x509/""`: {
- wc: &WebhookController{certType: linkedca.Webhook_X509},
- wh: &Webhook{},
- want: true,
- },
- "ssh/all": {
- wc: &WebhookController{certType: linkedca.Webhook_SSH},
- wh: &Webhook{CertType: linkedca.Webhook_ALL.String()},
- want: true,
- },
- "ssh/x509": {
- wc: &WebhookController{certType: linkedca.Webhook_SSH},
- wh: &Webhook{CertType: linkedca.Webhook_X509.String()},
- want: false,
- },
- "ssh/ssh": {
- wc: &WebhookController{certType: linkedca.Webhook_SSH},
- wh: &Webhook{CertType: linkedca.Webhook_SSH.String()},
- want: true,
- },
- `ssh/""`: {
- wc: &WebhookController{certType: linkedca.Webhook_SSH},
- wh: &Webhook{},
- want: true,
- },
- }
- for name, test := range tests {
- t.Run(name, func(t *testing.T) {
- assert.Equal(t, test.want, test.wc.isCertTypeOK(test.wh))
- })
- }
- }
- // withRequestID is a helper that calls into [requestid.NewContext] and returns
- // a new context with the requestID added.
- func withRequestID(t *testing.T, ctx context.Context, requestID string) context.Context {
- t.Helper()
- return requestid.NewContext(ctx, requestID)
- }
- func TestWebhookController_Enrich(t *testing.T) {
- cert, err := pemutil.ReadCertificate("testdata/certs/x5c-leaf.crt", pemutil.WithFirstBlock())
- require.NoError(t, err)
- type test struct {
- ctl *WebhookController
- ctx context.Context
- req *webhook.RequestBody
- responses []*webhook.ResponseBody
- expectErr bool
- expectTemplateData any
- assertRequest func(t *testing.T, req *webhook.RequestBody)
- assertError func(t *testing.T, err error)
- }
- tests := map[string]test{
- "ok/no enriching webhooks": {
- ctl: &WebhookController{
- client: http.DefaultClient,
- webhooks: []*Webhook{{Name: "people", Kind: "AUTHORIZING"}},
- TemplateData: nil,
- },
- req: &webhook.RequestBody{},
- responses: nil,
- expectErr: false,
- expectTemplateData: nil,
- },
- "ok/one webhook": {
- ctl: &WebhookController{
- client: http.DefaultClient,
- webhooks: []*Webhook{{Name: "people", Kind: "ENRICHING"}},
- TemplateData: x509util.TemplateData{},
- },
- ctx: withRequestID(t, context.Background(), "reqID"),
- req: &webhook.RequestBody{},
- responses: []*webhook.ResponseBody{{Allow: true, Data: map[string]any{"role": "bar"}}},
- expectErr: false,
- expectTemplateData: x509util.TemplateData{"Webhooks": map[string]any{"people": map[string]any{"role": "bar"}}},
- },
- "ok/two webhooks": {
- ctl: &WebhookController{
- client: http.DefaultClient,
- webhooks: []*Webhook{
- {Name: "people", Kind: "ENRICHING"},
- {Name: "devices", Kind: "ENRICHING"},
- },
- TemplateData: x509util.TemplateData{},
- },
- ctx: withRequestID(t, context.Background(), "reqID"),
- req: &webhook.RequestBody{},
- responses: []*webhook.ResponseBody{
- {Allow: true, Data: map[string]any{"role": "bar"}},
- {Allow: true, Data: map[string]any{"serial": "123"}},
- },
- expectErr: false,
- expectTemplateData: x509util.TemplateData{
- "Webhooks": map[string]any{
- "devices": map[string]any{"serial": "123"},
- "people": map[string]any{"role": "bar"},
- },
- },
- },
- "ok/x509 only": {
- ctl: &WebhookController{
- client: http.DefaultClient,
- webhooks: []*Webhook{
- {Name: "people", Kind: "ENRICHING", CertType: linkedca.Webhook_SSH.String()},
- {Name: "devices", Kind: "ENRICHING"},
- },
- TemplateData: x509util.TemplateData{},
- certType: linkedca.Webhook_X509,
- },
- ctx: withRequestID(t, context.Background(), "reqID"),
- req: &webhook.RequestBody{},
- responses: []*webhook.ResponseBody{
- {Allow: true, Data: map[string]any{"role": "bar"}},
- {Allow: true, Data: map[string]any{"serial": "123"}},
- },
- expectErr: false,
- expectTemplateData: x509util.TemplateData{
- "Webhooks": map[string]any{
- "devices": map[string]any{"serial": "123"},
- },
- },
- },
- "ok/with options": {
- ctl: &WebhookController{
- client: http.DefaultClient,
- webhooks: []*Webhook{{Name: "people", Kind: "ENRICHING"}},
- TemplateData: x509util.TemplateData{},
- options: []webhook.RequestBodyOption{webhook.WithX5CCertificate(cert)},
- },
- ctx: withRequestID(t, context.Background(), "reqID"),
- req: &webhook.RequestBody{},
- responses: []*webhook.ResponseBody{{Allow: true, Data: map[string]any{"role": "bar"}}},
- expectErr: false,
- expectTemplateData: x509util.TemplateData{"Webhooks": map[string]any{"people": map[string]any{"role": "bar"}}},
- assertRequest: func(t *testing.T, req *webhook.RequestBody) {
- key, err := x509.MarshalPKIXPublicKey(cert.PublicKey)
- require.NoError(t, err)
- assert.Equal(t, &webhook.X5CCertificate{
- Raw: cert.Raw,
- PublicKey: key,
- PublicKeyAlgorithm: cert.PublicKeyAlgorithm.String(),
- NotBefore: cert.NotBefore,
- NotAfter: cert.NotAfter,
- }, req.X5CCertificate)
- },
- },
- "deny": {
- ctl: &WebhookController{
- client: http.DefaultClient,
- webhooks: []*Webhook{{Name: "people", Kind: "ENRICHING"}},
- TemplateData: x509util.TemplateData{},
- },
- ctx: withRequestID(t, context.Background(), "reqID"),
- req: &webhook.RequestBody{},
- responses: []*webhook.ResponseBody{{Allow: false}},
- expectErr: true,
- expectTemplateData: x509util.TemplateData{},
- assertError: func(t *testing.T, err error) {
- assert.Equal(t, ErrWebhookDenied, err)
- },
- },
- "deny/with error": {
- ctl: &WebhookController{
- client: http.DefaultClient,
- webhooks: []*Webhook{{Name: "people", Kind: "ENRICHING"}},
- TemplateData: x509util.TemplateData{},
- },
- ctx: withRequestID(t, context.Background(), "reqID"),
- req: &webhook.RequestBody{},
- responses: []*webhook.ResponseBody{{Allow: false, Error: &webhook.Error{
- Code: "theCode", Message: "Some message",
- }}},
- expectErr: true,
- expectTemplateData: x509util.TemplateData{},
- assertError: func(t *testing.T, err error) {
- assert.Equal(t, &webhook.Error{
- Code: "theCode", Message: "Some message",
- }, err)
- },
- },
- "fail/with options": {
- ctl: &WebhookController{
- client: http.DefaultClient,
- webhooks: []*Webhook{{Name: "people", Kind: "ENRICHING"}},
- TemplateData: x509util.TemplateData{},
- options: []webhook.RequestBodyOption{webhook.WithX5CCertificate(&x509.Certificate{
- PublicKey: []byte("bad"),
- })},
- },
- ctx: withRequestID(t, context.Background(), "reqID"),
- req: &webhook.RequestBody{},
- responses: []*webhook.ResponseBody{{Allow: false}},
- expectErr: true,
- expectTemplateData: x509util.TemplateData{},
- },
- }
- for name, test := range tests {
- t.Run(name, func(t *testing.T) {
- for i, wh := range test.ctl.webhooks {
- var j = i
- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- assert.Equal(t, "reqID", r.Header.Get("X-Request-ID"))
- err := json.NewEncoder(w).Encode(test.responses[j])
- require.NoError(t, err)
- }))
- // nolint: gocritic // defer in loop isn't a memory leak
- defer ts.Close()
- wh.URL = ts.URL
- }
- err := test.ctl.Enrich(test.ctx, test.req)
- if (err != nil) != test.expectErr {
- t.Fatalf("Got err %v, want %v", err, test.expectErr)
- }
- assert.Equal(t, test.expectTemplateData, test.ctl.TemplateData)
- if test.assertRequest != nil {
- test.assertRequest(t, test.req)
- }
- if test.assertError != nil {
- test.assertError(t, err)
- }
- })
- }
- }
- func TestWebhookController_Authorize(t *testing.T) {
- cert, err := pemutil.ReadCertificate("testdata/certs/x5c-leaf.crt", pemutil.WithFirstBlock())
- require.NoError(t, err)
- type test struct {
- ctl *WebhookController
- ctx context.Context
- req *webhook.RequestBody
- responses []*webhook.ResponseBody
- expectErr bool
- assertRequest func(t *testing.T, req *webhook.RequestBody)
- assertError func(t *testing.T, err error)
- }
- tests := map[string]test{
- "ok/no enriching webhooks": {
- ctl: &WebhookController{
- client: http.DefaultClient,
- webhooks: []*Webhook{{Name: "people", Kind: "ENRICHING"}},
- },
- req: &webhook.RequestBody{},
- responses: nil,
- expectErr: false,
- },
- "ok": {
- ctl: &WebhookController{
- client: http.DefaultClient,
- webhooks: []*Webhook{{Name: "people", Kind: "AUTHORIZING"}},
- },
- ctx: withRequestID(t, context.Background(), "reqID"),
- req: &webhook.RequestBody{},
- responses: []*webhook.ResponseBody{{Allow: true}},
- expectErr: false,
- },
- "ok/ssh only": {
- ctl: &WebhookController{
- client: http.DefaultClient,
- webhooks: []*Webhook{{Name: "people", Kind: "AUTHORIZING", CertType: linkedca.Webhook_X509.String()}},
- certType: linkedca.Webhook_SSH,
- },
- ctx: withRequestID(t, context.Background(), "reqID"),
- req: &webhook.RequestBody{},
- responses: []*webhook.ResponseBody{{Allow: false}},
- expectErr: false,
- },
- "ok/with options": {
- ctl: &WebhookController{
- client: http.DefaultClient,
- webhooks: []*Webhook{{Name: "people", Kind: "AUTHORIZING"}},
- options: []webhook.RequestBodyOption{webhook.WithX5CCertificate(cert)},
- },
- ctx: withRequestID(t, context.Background(), "reqID"),
- req: &webhook.RequestBody{},
- responses: []*webhook.ResponseBody{{Allow: true}},
- expectErr: false,
- assertRequest: func(t *testing.T, req *webhook.RequestBody) {
- key, err := x509.MarshalPKIXPublicKey(cert.PublicKey)
- require.NoError(t, err)
- assert.Equal(t, &webhook.X5CCertificate{
- Raw: cert.Raw,
- PublicKey: key,
- PublicKeyAlgorithm: cert.PublicKeyAlgorithm.String(),
- NotBefore: cert.NotBefore,
- NotAfter: cert.NotAfter,
- }, req.X5CCertificate)
- },
- },
- "deny": {
- ctl: &WebhookController{
- client: http.DefaultClient,
- webhooks: []*Webhook{{Name: "people", Kind: "AUTHORIZING"}},
- },
- ctx: withRequestID(t, context.Background(), "reqID"),
- req: &webhook.RequestBody{},
- responses: []*webhook.ResponseBody{{Allow: false}},
- expectErr: true,
- assertError: func(t *testing.T, err error) {
- assert.Equal(t, ErrWebhookDenied, err)
- },
- },
- "deny/withError": {
- ctl: &WebhookController{
- client: http.DefaultClient,
- webhooks: []*Webhook{{Name: "people", Kind: "AUTHORIZING"}},
- },
- ctx: withRequestID(t, context.Background(), "reqID"),
- req: &webhook.RequestBody{},
- responses: []*webhook.ResponseBody{{Allow: false, Error: &webhook.Error{
- Code: "theCode", Message: "Some message",
- }}},
- expectErr: true,
- assertError: func(t *testing.T, err error) {
- assert.Equal(t, &webhook.Error{
- Code: "theCode", Message: "Some message",
- }, err)
- },
- },
- "fail/with options": {
- ctl: &WebhookController{
- client: http.DefaultClient,
- webhooks: []*Webhook{{Name: "people", Kind: "AUTHORIZING"}},
- options: []webhook.RequestBodyOption{webhook.WithX5CCertificate(&x509.Certificate{
- PublicKey: []byte("bad"),
- })},
- },
- ctx: withRequestID(t, context.Background(), "reqID"),
- req: &webhook.RequestBody{},
- responses: []*webhook.ResponseBody{{Allow: false}},
- expectErr: true,
- },
- }
- for name, test := range tests {
- t.Run(name, func(t *testing.T) {
- for i, wh := range test.ctl.webhooks {
- var j = i
- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- assert.Equal(t, "reqID", r.Header.Get("X-Request-ID"))
- err := json.NewEncoder(w).Encode(test.responses[j])
- require.NoError(t, err)
- }))
- // nolint: gocritic // defer in loop isn't a memory leak
- defer ts.Close()
- wh.URL = ts.URL
- }
- err := test.ctl.Authorize(test.ctx, test.req)
- if (err != nil) != test.expectErr {
- t.Fatalf("Got err %v, want %v", err, test.expectErr)
- }
- if test.assertRequest != nil {
- test.assertRequest(t, test.req)
- }
- if test.assertError != nil {
- test.assertError(t, err)
- }
- })
- }
- }
- func TestWebhook_Do(t *testing.T) {
- csr := parseCertificateRequest(t, "testdata/certs/ecdsa.csr")
- type test struct {
- webhook Webhook
- dataArg any
- requestID string
- webhookResponse webhook.ResponseBody
- expectPath string
- errStatusCode int
- serverErrMsg string
- expectErr error
- // expectToken any
- }
- tests := map[string]test{
- "ok": {
- webhook: Webhook{
- ID: "abc123",
- Secret: "c2VjcmV0Cg==",
- },
- requestID: "reqID",
- webhookResponse: webhook.ResponseBody{
- Data: map[string]interface{}{"role": "dba"},
- },
- },
- "ok/no-request-id": {
- webhook: Webhook{
- ID: "abc123",
- Secret: "c2VjcmV0Cg==",
- },
- webhookResponse: webhook.ResponseBody{
- Data: map[string]interface{}{"role": "dba"},
- },
- },
- "ok/bearer": {
- webhook: Webhook{
- ID: "abc123",
- Secret: "c2VjcmV0Cg==",
- BearerToken: "mytoken",
- },
- requestID: "reqID",
- webhookResponse: webhook.ResponseBody{
- Data: map[string]interface{}{"role": "dba"},
- },
- },
- "ok/basic": {
- webhook: Webhook{
- ID: "abc123",
- Secret: "c2VjcmV0Cg==",
- BasicAuth: struct {
- Username string
- Password string
- }{
- Username: "myuser",
- Password: "mypass",
- },
- },
- requestID: "reqID",
- webhookResponse: webhook.ResponseBody{
- Data: map[string]interface{}{"role": "dba"},
- },
- },
- "ok/templated-url": {
- webhook: Webhook{
- ID: "abc123",
- // scheme, host, port will come from test server
- URL: "/users/{{ .username }}?region={{ .region }}",
- Secret: "c2VjcmV0Cg==",
- },
- requestID: "reqID",
- dataArg: map[string]interface{}{"username": "areed", "region": "central"},
- webhookResponse: webhook.ResponseBody{
- Data: map[string]interface{}{"role": "dba"},
- },
- expectPath: "/users/areed?region=central",
- },
- /*
- "ok/token from ssh template": {
- webhook: Webhook{
- ID: "abc123",
- Secret: "c2VjcmV0Cg==",
- },
- webhookResponse: webhook.ResponseBody{
- Data: map[string]interface{}{"role": "dba"},
- },
- dataArg: sshutil.TemplateData{sshutil.TokenKey: "token"},
- expectToken: "token",
- },
- "ok/token from x509 template": {
- webhook: Webhook{
- ID: "abc123",
- Secret: "c2VjcmV0Cg==",
- },
- webhookResponse: webhook.ResponseBody{
- Data: map[string]interface{}{"role": "dba"},
- },
- dataArg: x509util.TemplateData{sshutil.TokenKey: "token"},
- expectToken: "token",
- },
- */
- "ok/allow": {
- webhook: Webhook{
- ID: "abc123",
- Secret: "c2VjcmV0Cg==",
- },
- requestID: "reqID",
- webhookResponse: webhook.ResponseBody{
- Allow: true,
- },
- },
- "fail/404": {
- webhook: Webhook{
- ID: "abc123",
- Secret: "c2VjcmV0Cg==",
- },
- webhookResponse: webhook.ResponseBody{
- Data: map[string]interface{}{"role": "dba"},
- },
- requestID: "reqID",
- errStatusCode: 404,
- serverErrMsg: "item not found",
- expectErr: errors.New("Webhook server responded with 404"),
- },
- }
- for name, tc := range tests {
- t.Run(name, func(t *testing.T) {
- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- if tc.requestID != "" {
- assert.Equal(t, tc.requestID, r.Header.Get("X-Request-ID"))
- }
- assert.Equal(t, tc.webhook.ID, r.Header.Get("X-Smallstep-Webhook-ID"))
- sig, err := hex.DecodeString(r.Header.Get("X-Smallstep-Signature"))
- assert.NoError(t, err)
- body, err := io.ReadAll(r.Body)
- assert.NoError(t, err)
- secret, err := base64.StdEncoding.DecodeString(tc.webhook.Secret)
- assert.NoError(t, err)
- h := hmac.New(sha256.New, secret)
- h.Write(body)
- mac := h.Sum(nil)
- assert.True(t, hmac.Equal(sig, mac))
- switch {
- case tc.webhook.BearerToken != "":
- ah := fmt.Sprintf("Bearer %s", tc.webhook.BearerToken)
- assert.Equal(t, ah, r.Header.Get("Authorization"))
- case tc.webhook.BasicAuth.Username != "" || tc.webhook.BasicAuth.Password != "":
- whReq, err := http.NewRequest("", "", http.NoBody)
- require.NoError(t, err)
- whReq.SetBasicAuth(tc.webhook.BasicAuth.Username, tc.webhook.BasicAuth.Password)
- ah := whReq.Header.Get("Authorization")
- assert.Equal(t, ah, whReq.Header.Get("Authorization"))
- default:
- assert.Equal(t, "", r.Header.Get("Authorization"))
- }
- if tc.expectPath != "" {
- assert.Equal(t, tc.expectPath, r.URL.Path+"?"+r.URL.RawQuery)
- }
- if tc.errStatusCode != 0 {
- http.Error(w, tc.serverErrMsg, tc.errStatusCode)
- return
- }
- reqBody := new(webhook.RequestBody)
- err = json.Unmarshal(body, reqBody)
- require.NoError(t, err)
- err = json.NewEncoder(w).Encode(tc.webhookResponse)
- require.NoError(t, err)
- }))
- defer ts.Close()
- tc.webhook.URL = ts.URL + tc.webhook.URL
- reqBody, err := webhook.NewRequestBody(webhook.WithX509CertificateRequest(csr))
- require.NoError(t, err)
- ctx := context.Background()
- if tc.requestID != "" {
- ctx = withRequestID(t, ctx, tc.requestID)
- }
- ctx, cancel := context.WithTimeout(ctx, time.Second*10)
- defer cancel()
- got, err := tc.webhook.DoWithContext(ctx, http.DefaultClient, httptransport.NoopWrapper(), reqBody, tc.dataArg)
- if tc.expectErr != nil {
- assert.Equal(t, tc.expectErr.Error(), err.Error())
- return
- }
- assert.NoError(t, err)
- assert.Equal(t, &tc.webhookResponse, got)
- })
- }
- t.Run("disableTLSClientAuth", func(t *testing.T) {
- ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- w.Write([]byte("{}"))
- }))
- ts.TLS.ClientAuth = tls.RequireAnyClientCert
- wh := Webhook{
- URL: ts.URL,
- }
- cert, err := tls.LoadX509KeyPair("testdata/certs/foo.crt", "testdata/secrets/foo.key")
- require.NoError(t, err)
- transport := httptransport.New()
- transport.TLSClientConfig = &tls.Config{
- InsecureSkipVerify: true,
- Certificates: []tls.Certificate{cert},
- }
- client := &http.Client{
- Transport: transport,
- }
- reqBody, err := webhook.NewRequestBody(webhook.WithX509CertificateRequest(csr))
- require.NoError(t, err)
- ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
- defer cancel()
- _, err = wh.DoWithContext(ctx, client, httptransport.NoopWrapper(), reqBody, nil)
- require.NoError(t, err)
- ctx, cancel = context.WithTimeout(context.Background(), time.Second*10)
- defer cancel()
- wh.DisableTLSClientAuth = true
- _, err = wh.DoWithContext(ctx, client, httptransport.NoopWrapper(), reqBody, nil)
- require.Error(t, err)
- })
- }
- func TestWebhook_Validate(t *testing.T) {
- tests := []struct {
- name string
- webhook *Webhook
- assertion assert.ErrorAssertionFunc
- }{
- {"ok enriching", &Webhook{Name: "devices", URL: "https://localhost:3000", Kind: "ENRICHING"}, assert.NoError},
- {"ok authorizing", &Webhook{Name: "devices", URL: "https://localhost:3000/devices", Kind: "AUTHORIZING"}, assert.NoError},
- {"fail name", &Webhook{Name: "", URL: "https://localhost:3000", Kind: "ENRICHING"}, assert.Error},
- {"fail url", &Webhook{Name: "devices", URL: "", Kind: "ENRICHING"}, assert.Error},
- {"fail bad url", &Webhook{Name: "devices", URL: "https://{{.Templated.Host}}", Kind: "ENRICHING"}, assert.Error},
- {"fail host", &Webhook{Name: "devices", URL: "https:opaque", Kind: "ENRICHING"}, assert.Error},
- {"fail scheme", &Webhook{Name: "devices", URL: "http://localhost", Kind: "ENRICHING"}, assert.Error},
- {"fail user", &Webhook{Name: "devices", URL: "https://user:pass@localhost", Kind: "ENRICHING"}, assert.Error},
- {"fail kind", &Webhook{Name: "devices", URL: "https://localhost:3000", Kind: ""}, assert.Error},
- {"fail bad kind", &Webhook{Name: "devices", URL: "https://localhost:3000", Kind: "SOMETHING"}, assert.Error},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- tt.assertion(t, tt.webhook.Validate())
- })
- }
- }
|