From 7cd194352e311371c420de2ff76eef31737366f3 Mon Sep 17 00:00:00 2001 From: njharter Date: Fri, 12 Apr 2019 12:24:29 -0700 Subject: [PATCH] adding auth decoration to /hook & /hooks endpoints --- src/caduceus/caduceus.go | 14 +++++--------- src/caduceus/primaryHandler.go | 11 ++++++++--- src/caduceus/primaryHandler_test.go | 6 ++++-- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/caduceus/caduceus.go b/src/caduceus/caduceus.go index ad5b68ac..b1e43a02 100644 --- a/src/caduceus/caduceus.go +++ b/src/caduceus/caduceus.go @@ -116,12 +116,6 @@ func caduceus(arguments []string) int { maxOutstanding: 0, } - primaryHandler, err := NewPrimaryHandler(logger, v, serverWrapper) - if err != nil { - fmt.Fprintf(os.Stderr, "Validator error: %v\n", err) - return 1 - } - webhookFactory, err := webhook.NewFactory(v) if err != nil { fmt.Fprintf(os.Stderr, "Error creating new webhook factory: %s\n", err) @@ -130,9 +124,11 @@ func caduceus(arguments []string) int { webhookRegistry, webhookHandler := webhookFactory.NewRegistryAndHandler(metricsRegistry) webhookFactory.SetExternalUpdate(caduceusSenderWrapper.Update) - // register webhook end points for api - primaryHandler.HandleFunc("/hook", webhookRegistry.UpdateRegistry) - primaryHandler.HandleFunc("/hooks", webhookRegistry.GetRegistry) + primaryHandler, err := NewPrimaryHandler(logger, v, serverWrapper, &webhookRegistry) + if err != nil { + fmt.Fprintf(os.Stderr, "Validator error: %v\n", err) + return 1 + } scheme := v.GetString("scheme") if len(scheme) < 1 { diff --git a/src/caduceus/primaryHandler.go b/src/caduceus/primaryHandler.go index 52ab292f..61a744ab 100644 --- a/src/caduceus/primaryHandler.go +++ b/src/caduceus/primaryHandler.go @@ -7,6 +7,7 @@ import ( "github.com/Comcast/webpa-common/secure" "github.com/Comcast/webpa-common/secure/handler" "github.com/Comcast/webpa-common/secure/key" + "github.com/Comcast/webpa-common/webhook" "github.com/SermoDigital/jose/jwt" "github.com/go-kit/kit/log" "github.com/gorilla/mux" @@ -28,7 +29,7 @@ type JWTValidator struct { Custom secure.JWTValidatorFactory } -func NewPrimaryHandler(l log.Logger, v *viper.Viper, sw *ServerHandler) (*mux.Router, error) { +func NewPrimaryHandler(l log.Logger, v *viper.Viper, sw *ServerHandler, reg *webhook.Registry) (*mux.Router, error) { var ( router = mux.NewRouter() ) @@ -47,16 +48,20 @@ func NewPrimaryHandler(l log.Logger, v *viper.Viper, sw *ServerHandler) (*mux.Ro authorizationDecorator := alice.New(authHandler.Decorate) - return configServerRouter(router, authorizationDecorator, sw), nil + return configServerRouter(router, authorizationDecorator, sw, reg), nil } -func configServerRouter(router *mux.Router, primaryHandler alice.Chain, serverWrapper *ServerHandler) *mux.Router { +func configServerRouter(router *mux.Router, primaryHandler alice.Chain, serverWrapper *ServerHandler, webhookRegistry *webhook.Registry) *mux.Router { var singleContentType = func(r *http.Request, _ *mux.RouteMatch) bool { return len(r.Header["Content-Type"]) == 1 //require single specification for Content-Type Header } router.Handle("/"+fmt.Sprintf("%s/%s", baseURI, version)+"/notify", primaryHandler.Then(serverWrapper)).Methods("POST").HeadersRegexp("Content-Type", "application/msgpack").MatcherFunc(singleContentType) + // register webhook end points + router.Handle("/hook", primaryHandler.ThenFunc(webhookRegistry.UpdateRegistry)).Methods("POST") + router.Handle("/hooks", primaryHandler.ThenFunc(webhookRegistry.GetRegistry)).Methods("GET") + return router } diff --git a/src/caduceus/primaryHandler_test.go b/src/caduceus/primaryHandler_test.go index 0d201a24..79bf50e5 100644 --- a/src/caduceus/primaryHandler_test.go +++ b/src/caduceus/primaryHandler_test.go @@ -8,6 +8,7 @@ import ( "github.com/Comcast/webpa-common/logging" "github.com/Comcast/webpa-common/secure" "github.com/Comcast/webpa-common/secure/handler" + "github.com/Comcast/webpa-common/webhook" "github.com/gorilla/mux" "github.com/justinas/alice" "github.com/spf13/viper" @@ -20,11 +21,12 @@ func TestNewPrimaryHandler(t *testing.T) { l = logging.New(nil) viper = viper.New() sw = &ServerHandler{} + reg = &webhook.Registry{} expectedAuthHeader = []string{"Basic xxxxxxx"} ) viper.Set("authHeader", expectedAuthHeader) - if _, err := NewPrimaryHandler(l, viper, sw); err != nil { + if _, err := NewPrimaryHandler(l, viper, sw, reg); err != nil { t.Fatalf("NewPrimaryHandler failed: %v", err) } @@ -90,7 +92,7 @@ func TestMuxServerConfig(t *testing.T) { authHandler := handler.AuthorizationHandler{Validator: nil} caduceusHandler := alice.New(authHandler.Decorate) - router := configServerRouter(mux.NewRouter(), caduceusHandler, serverWrapper) + router := configServerRouter(mux.NewRouter(), caduceusHandler, serverWrapper, &webhook.Registry{}) t.Run("TestMuxResponseCorrectMSP", func(t *testing.T) { req := exampleRequest("1234", "application/msgpack", "/api/v3/notify")