From 31bd9a249124319aa27388de5d0b9343f07782d0 Mon Sep 17 00:00:00 2001 From: Joel Unzain Date: Fri, 26 Jan 2018 16:45:07 -0800 Subject: [PATCH] small temporary refactor for jwt validation metrics --- src/glide.lock | 6 +++--- src/glide.yaml | 2 +- src/tr1d1um/tr1d1um.go | 37 ++++++++++++++++++------------------- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/glide.lock b/src/glide.lock index 217b1551..08494c49 100644 --- a/src/glide.lock +++ b/src/glide.lock @@ -1,5 +1,5 @@ -hash: 8da494b261f30cb25b1123e3f6c7bd86a9a084185c607579676191861ba3b583 -updated: 2018-01-26T12:09:46.730492292-08:00 +hash: f8a0c754b9e7d0af0c9269b5abd48e8d8301938ec663cedd20a3556d4fdc56c9 +updated: 2018-01-26T16:28:03.510138-08:00 imports: - name: github.com/aws/aws-sdk-go version: 7be45195c3af1b54a609812f90c05a7e492e2491 @@ -38,7 +38,7 @@ imports: subpackages: - linux - name: github.com/Comcast/webpa-common - version: 7ace8d3c96f2e4725e713378f744c3c090a880ce + version: d7cf234676e116d7b627936493c3259d35fe290f subpackages: - concurrent - convey diff --git a/src/glide.yaml b/src/glide.yaml index 0b606a50..805a2b2f 100644 --- a/src/glide.yaml +++ b/src/glide.yaml @@ -2,6 +2,6 @@ package: . homepage: https://github.com/Comcast/tr1d1um import: - package: github.com/Comcast/webpa-common - version: 7ace8d3c96f2e4725e713378f744c3c090a880ce + version: d7cf234676e116d7b627936493c3259d35fe290f - package: github.com/go-ozzo/ozzo-validation version: v3.3 diff --git a/src/tr1d1um/tr1d1um.go b/src/tr1d1um/tr1d1um.go index 49af65ad..9b06c6e6 100644 --- a/src/tr1d1um/tr1d1um.go +++ b/src/tr1d1um/tr1d1um.go @@ -99,8 +99,7 @@ func tr1d1um(arguments []string) (exitCode int) { return 1 } - valMetrics := secure.NewJWTValidationMeasures(metricsRegistry) - preHandler, err := SetUpPreHandler(v, logger, valMetrics) + preHandler, err := SetUpPreHandler(v, logger, metricsRegistry) if err != nil { fmt.Fprintf(os.Stderr, "error setting up prehandler: %s\n", err.Error()) @@ -242,30 +241,30 @@ func SetUpHandler(v *viper.Viper, logger log.Logger) (cHandler *ConversionHandle } //SetUpPreHandler configures the authorization requirements for requests to reach the main handler -func SetUpPreHandler(v *viper.Viper, logger log.Logger, m *secure.JWTValidationMeasures) (preHandler *alice.Chain, err error) { - validator, err := GetValidator(v, m) - if err != nil { - return - } - - authHandler := handler.AuthorizationHandler{ - HeaderName: "Authorization", - ForbiddenStatusCode: 403, - Validator: validator, - Logger: logger, - } +func SetUpPreHandler(v *viper.Viper, logger log.Logger, registry xmetrics.Registry) (preHandler *alice.Chain, err error) { + m := secure.NewJWTValidationMeasures(registry) + var validator secure.Validator + if validator, err = getValidator(v, m); err == nil { + + authHandler := handler.AuthorizationHandler{ + HeaderName: "Authorization", + ForbiddenStatusCode: 403, + Validator: validator, + Logger: logger, + } - authHandler.DefineMeasures(m) + authHandler.DefineMeasures(m) - newPreHandler := alice.New(authHandler.Decorate) - preHandler = &newPreHandler + newPreHandler := alice.New(authHandler.Decorate) + preHandler = &newPreHandler + } return } -//GetValidator returns a validator for JWT/Basic tokens +//getValidator returns a validator for JWT/Basic tokens //It reads in tokens from a config file. Zero or more tokens //can be read. -func GetValidator(v *viper.Viper, m *secure.JWTValidationMeasures) (validator secure.Validator, err error) { +func getValidator(v *viper.Viper, m *secure.JWTValidationMeasures) (validator secure.Validator, err error) { var jwtVals []JWTValidator v.UnmarshalKey("jwtValidators", &jwtVals)