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

Export http helper functions #774

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

redradrat
Copy link

The http handler wrapper is great, but when compiling a custom transaction flow doesn't allow access to ProcessRequest and ProcessResponse anymore. The wrapper itself is cool, but in a lot of cases access to the actual transaction flow would be required. An example would be taking actions if an interruption occurred. e.g. producing metrics, notifying something, etc.

So in general to have an easier time writing a custom, wrapper-like flow with the nice Process helpers, would be great to have them exported again.

Thank you for contributing to Coraza WAF, your effort is greatly appreciated
Before submitting check if what you want to add to coraza list meets quality standards before sending pull request. Thanks!

Make sure that you've checked the boxes below before you submit PR:

Thanks for your contribution ❤️

@redradrat redradrat requested a review from a team as a code owner April 12, 2023 08:59
@codecov
Copy link

codecov bot commented Apr 12, 2023

Codecov Report

Patch coverage: 87.50% and no project coverage change.

Comparison is base (6f11f53) 81.85% compared to head (4bf02c7) 81.85%.

❗ Current head 4bf02c7 differs from pull request most recent head 12a90ae. Consider uploading reports for the commit 12a90ae to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@
##           v3/dev     #774   +/-   ##
=======================================
  Coverage   81.85%   81.85%           
=======================================
  Files         153      153           
  Lines        8188     8188           
=======================================
  Hits         6702     6702           
  Misses       1267     1267           
  Partials      219      219           
Flag Coverage Δ
default 78.08% <75.00%> (ø)
examples 25.99% <75.00%> (ø)
ftw 49.09% <37.50%> (ø)
ftw-multiphase 49.21% <37.50%> (ø)
tinygo 77.23% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
http/middleware.go 84.95% <83.33%> (ø)
http/interceptor.go 51.28% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Member

@jcchavezs jcchavezs left a comment

Choose a reason for hiding this comment

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

Thanks for this @redradrat. I am not too happy with exposing internal details in such a way. I'd really prefer to learn the reasons on why you can't use middleware (i.e. what is missing) and how we could improve it so you can use it. This change sounds like an early abstraction and we did not do that for coraza-caddy (where the code is almost identical but we can't reuse this middleware due to a signature difference) as we wanted to collect use cases and then come up with the right abstraction. Request for the feature, not for the implementation.

See

@redradrat
Copy link
Author

@jcchavezs really our first use case would have been to inject logic when an interruption was triggered. The simple example would be to report what happened with a prometheus gauge for example. I can adapt the PR to utilise functions as args for WrapHandler maybe?

From my perspective the point here is, that coraza is a WAF framework, flexible for integration no? So having hooks at points within the http handler wrapper would make sense to inject logic, instead of replicating the handler.

@jcchavezs
Copy link
Member

jcchavezs commented Apr 12, 2023 via email

@redradrat
Copy link
Author

awesome... thanks for your thoughts. I'll give it a shot @jcchavezs

@jcchavezs
Copy link
Member

Giving a second thought maybe we can accept a observer interface which would report metrics for this? Something like

type Observer interface {
  // onRequest is invoked when a request is started
  func onRequest(*http.Request, types.Transaction)
  // onInterruption is invoked when a request is interrupted
  func onInterruption(*http.Request, types.Transaction)
}

Ping @anuraaga

@anuraaga
Copy link
Contributor

Those hooks seem to make sense to me - I'm a bit hesitant on exposing Transaction though as it would give a lot of surface for footgunning. Ideally the information passed to the hook is something immutable - unless we know of use cases that need mutability in hooks?

@jcchavezs
Copy link
Member

jcchavezs commented Apr 13, 2023 via email

@anuraaga
Copy link
Contributor

TransactionState is also mutable - basically we currently have Transaction as a view for use in middleware and TransactionState as a view for use in plugins. This would be more of a view for use in management or something like that - maybe having yet another view is overengineering though 🤔

@jcchavezs
Copy link
Member

jcchavezs commented Apr 13, 2023 via email

@jcchavezs
Copy link
Member

jcchavezs commented Apr 13, 2023

@redradrat so something like

type TransactionCallback interface {
  // onRequest is invoked when a request is started
  func onRequest(*http.Request)

  // onInterruption is invoked when a request is interrupted
  func onInterruption(*http.Request, *types.Interruption)
}

might do the trick. I wonder if matched rules (hold by transaction) is something we should also pass cc @piyushroshan and finally if we should include the phase in onInterruption method like we do in coraza-proxy-wasm. See https://github.com/corazawaf/coraza-proxy-wasm/pull/29/files#diff-368a9e52513f8700cc50c424de862c537fc5c1a492c4598b97b960f46fc1c877R38

Anyways, any feedback on the metrics needed would be great.

Ping @RedXanadu as he might have insightful feedback.

@jcchavezs
Copy link
Member

Any movement on this @redradrat ?

@redradrat
Copy link
Author

redradrat commented May 30, 2023

Hey @jcchavezs. Actually, yes. We did quite some rewriting on the http package internally, I'll be cleaning that up to open another PR here. Hope you agree to the changes. We had to split up the Wrapper into a request and a response section because the interceptor would just not work for us on response... Long story short, I'm still up for pushing sthg here :)

@fzipi
Copy link
Member

fzipi commented Apr 27, 2024

@jcchavezs @redradrat so... what's next here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants