Răsfoiți Sursa

Validate webhooks

This commit validates the webhooks when they are configured on the
ca.json
Mariano Cano 9 luni în urmă
părinte
comite
bd169be5dd

+ 9 - 3
acme/linker.go

@@ -200,9 +200,15 @@ func (l *linker) Middleware(next http.Handler) http.Handler {
 			return
 		}
 
-		acmeProv, ok := p.(*provisioner.ACME)
-		if !ok {
-			render.Error(w, r, NewError(ErrorAccountDoesNotExistType, "provisioner must be of type ACME"))
+		var acmeProv *provisioner.ACME
+		switch prov := p.(type) {
+		case *provisioner.ACME:
+			acmeProv = prov
+		case provisioner.Uninitialized:
+			render.Error(w, r, NewDetailedError(ErrorUnauthorizedType, "provisioner is disabled due to an initialization error"))
+			return
+		default:
+			render.Error(w, r, NewDetailedError(ErrorUnauthorizedType, "provisioner must be of type ACME"))
 			return
 		}
 

+ 5 - 0
authority/provisioner/controller.go

@@ -48,6 +48,11 @@ func NewController(p Interface, claims *Claims, config Config, options *Options)
 	if wt == nil {
 		wt = httptransport.NoopWrapper()
 	}
+	for _, wh := range options.GetWebhooks() {
+		if err := wh.Validate(); err != nil {
+			return nil, err
+		}
+	}
 
 	return &Controller{
 		Interface:             p,

+ 9 - 2
authority/provisioner/scep_test.go

@@ -279,6 +279,8 @@ func Test_selectValidationMethod(t *testing.T) {
 			Options: &Options{
 				Webhooks: []*Webhook{
 					{
+						Name: "challenge",
+						URL:  "https://scep.challenge",
 						Kind: linkedca.Webhook_SCEPCHALLENGE.String(),
 					},
 				},
@@ -295,6 +297,8 @@ func Test_selectValidationMethod(t *testing.T) {
 			Options: &Options{
 				Webhooks: []*Webhook{
 					{
+						Name: "authorizing",
+						URL:  "https://scep.authorizing",
 						Kind: linkedca.Webhook_AUTHORIZING.String(),
 					},
 				},
@@ -311,6 +315,8 @@ func Test_selectValidationMethod(t *testing.T) {
 			Options: &Options{
 				Webhooks: []*Webhook{
 					{
+						Name: "authorizing",
+						URL:  "https://scep.authorizing",
 						Kind: linkedca.Webhook_AUTHORIZING.String(),
 					},
 				},
@@ -341,7 +347,7 @@ func TestSCEP_ValidateChallenge(t *testing.T) {
 		Allow bool `json:"allow"`
 		Data  any  `json:"data"`
 	}
-	okServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+	okServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 		req := &request{}
 		err := json.NewDecoder(r.Body).Decode(req)
 		require.NoError(t, err)
@@ -363,6 +369,7 @@ func TestSCEP_ValidateChallenge(t *testing.T) {
 		w.WriteHeader(200)
 		w.Write(b)
 	}))
+	httpclient := okServer.Client()
 	t.Cleanup(okServer.Close)
 	type args struct {
 		challenge     string
@@ -471,7 +478,7 @@ func TestSCEP_ValidateChallenge(t *testing.T) {
 	}
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
-			err := tt.p.Init(Config{Claims: globalProvisionerClaims, WebhookClient: http.DefaultClient})
+			err := tt.p.Init(Config{Claims: globalProvisionerClaims, WebhookClient: httpclient})
 			require.NoError(t, err)
 			ctx := context.Background()
 

+ 41 - 0
authority/provisioner/webhook.go

@@ -11,6 +11,8 @@ import (
 	"fmt"
 	"log"
 	"net/http"
+	"net/url"
+	"strings"
 	"text/template"
 	"time"
 
@@ -143,6 +145,45 @@ type Webhook struct {
 	} `json:"-"`
 }
 
+// Validate validates a webhook, only name, url and kind are required.
+func (w *Webhook) Validate() error {
+	if w == nil {
+		return nil
+	}
+
+	// name
+	if w.Name == "" {
+		return errors.New("webhook name is required")
+	}
+
+	// url
+	parsedURL, err := url.Parse(w.URL)
+	if err != nil {
+		return errors.New("webhook url is invalid")
+	}
+	if parsedURL.Host == "" {
+		return errors.New("webhook url is invalid")
+	}
+	if parsedURL.Scheme != "https" {
+		return errors.New("webhook url must use https")
+	}
+	if parsedURL.User != nil {
+		return errors.New("webhook url may not contain username or password")
+	}
+
+	// kind
+	if w.Kind == "" {
+		return errors.New("webhook kind is required")
+	}
+	w.Kind = strings.ToUpper(w.Kind)
+	kind, ok := linkedca.Webhook_Kind_value[w.Kind]
+	if !ok || kind == 0 {
+		return errors.New("webhook kind is invalid")
+	}
+
+	return nil
+}
+
 // TransportWrapper wraps the set of functions mapping [http.Transport] references to
 // [http.RoundTripper].
 type TransportWrapper = httptransport.Wrapper

+ 29 - 6
authority/provisioner/webhook_test.go

@@ -6,6 +6,7 @@ import (
 	"crypto/sha256"
 	"crypto/tls"
 	"crypto/x509"
+	_ "embed"
 	"encoding/base64"
 	"encoding/hex"
 	"encoding/json"
@@ -17,16 +18,14 @@ import (
 	"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"
-
-	"github.com/smallstep/linkedca"
 	"go.step.sm/crypto/pemutil"
 	"go.step.sm/crypto/x509util"
-
-	"github.com/smallstep/certificates/internal/httptransport"
-	"github.com/smallstep/certificates/middleware/requestid"
-	"github.com/smallstep/certificates/webhook"
 )
 
 func TestWebhookController_isCertTypeOK(t *testing.T) {
@@ -674,3 +673,27 @@ func TestWebhook_Do(t *testing.T) {
 		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())
+		})
+	}
+}