Skip to content

Commit

Permalink
Merge pull request #136 from Comcast/fixMissingAuth
Browse files Browse the repository at this point in the history
adding missing auth decoration to endpoints
  • Loading branch information
johnabass authored Apr 12, 2019
2 parents e167daa + 7cd1943 commit 83980e2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
14 changes: 5 additions & 9 deletions src/caduceus/caduceus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
11 changes: 8 additions & 3 deletions src/caduceus/primaryHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
)
Expand All @@ -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
}

Expand Down
6 changes: 4 additions & 2 deletions src/caduceus/primaryHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}

Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 83980e2

Please sign in to comment.