Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added WRP validator middleware functionality #306

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions primaryHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"github.com/xmidt-org/sallust"
"github.com/xmidt-org/touchstone"
"go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux"
"go.uber.org/multierr"
"go.uber.org/zap"

"github.com/xmidt-org/webpa-common/secure/handler"
Expand Down Expand Up @@ -63,7 +64,7 @@
jwtAuthConfigKey = "jwtValidator"
wrpCheckConfigKey = "WRPCheck"

deviceID = "deviceID"
deviceID = "devicID"

enforceCheck = "enforce"
)
Expand Down Expand Up @@ -391,7 +392,7 @@
otelmux.WithPropagators(tracing.Propagator()),
otelmux.WithTracerProvider(tracing.TracerProvider()),
}
router.Use(otelmux.Middleware("mainSpan", otelMuxOptions...), candlelight.EchoFirstTraceNodeInfo(tracing.Propagator()))
router.Use(otelmux.Middleware("mainSpan", otelMuxOptions...), candlelight.EchoFirstTraceNodeInfo(tracing.Propagator()), ValidateWRP())

Check warning on line 395 in primaryHandler.go

View check run for this annotation

Codecov / codecov/patch

primaryHandler.go#L395

Added line #L395 was not covered by tests

router.NotFoundHandler = http.HandlerFunc(func(response http.ResponseWriter, _ *http.Request) {
xhttp.WriteError(response, http.StatusBadRequest, "Invalid endpoint")
Expand Down Expand Up @@ -561,3 +562,31 @@
})
})
}

func ValidateWRP() func(http.Handler) http.Handler {
return func(delegate http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

ctx := r.Context()
if msg, ok := wrpcontext.Get[*wrp.Message](ctx); ok {
Comment on lines +570 to +571
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ctx := r.Context()
if msg, ok := wrpcontext.Get[*wrp.Message](ctx); ok {
if msg, ok := wrpcontext.Get[*wrp.Message](r.Context()); ok {

validators := wrp.SpecValidators()
var err error
for _, v := range validators {
err = multierr.Append(err, v.Validate(*msg))
}
if err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(
w,
`{"code": %d, "message": "%s"}`,
http.StatusBadRequest,
fmt.Sprintf("failed to validate WRP message: %s", err),
)
return
}

Check warning on line 587 in primaryHandler.go

View check run for this annotation

Codecov / codecov/patch

primaryHandler.go#L566-L587

Added lines #L566 - L587 were not covered by tests
}
delegate.ServeHTTP(w, r)

Check warning on line 589 in primaryHandler.go

View check run for this annotation

Codecov / codecov/patch

primaryHandler.go#L589

Added line #L589 was not covered by tests
Comment on lines +588 to +589
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
delegate.ServeHTTP(w, r)
}
delegate.ServeHTTP(w, r)

})
}
}