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

Exports IsRuleEngineOff #504

Merged
merged 8 commits into from
Nov 21, 2022
Merged

Conversation

M4tteoP
Copy link
Member

@M4tteoP M4tteoP commented Nov 11, 2022

Follows #499, Just like RequestBodyAccess and ResponseBodyAccess, connectors could benefit from retrieving the current status of the engine. I also added a note to explicitly tell that it is the current status per-transaction basis in order to be aware of using it without making wrong assumptions.

@M4tteoP M4tteoP requested a review from a team as a code owner November 11, 2022 14:47
Copy link
Member

@jptosso jptosso left a comment

Choose a reason for hiding this comment

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

LGTM!

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.

Sometimes it is easy to get a too broad API because of this kind of changes where we expose an internal state and expect clients to deal with the expected behaviour so before we merge this I would expect to exercise this API in the http package and prove what can every value do for us.

@jcchavezs
Copy link
Member

Also if finally export this we should document what are the expectations for connectors as for example, DetectionOnly means analysis but no interruptions whereas Off means no analysis at all and while user might not notice, this is different in terms of audit logs.

@codecov-commenter
Copy link

codecov-commenter commented Nov 12, 2022

Codecov Report

Base: 78.04% // Head: 78.08% // Increases project coverage by +0.03% 🎉

Coverage data is based on head (eeee825) compared to base (128e9a3).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##           v3/dev     #504      +/-   ##
==========================================
+ Coverage   78.04%   78.08%   +0.03%     
==========================================
  Files         145      145              
  Lines        6861     6876      +15     
==========================================
+ Hits         5355     5369      +14     
- Misses       1268     1270       +2     
+ Partials      238      237       -1     
Flag Coverage Δ
default 73.34% <100.00%> (+0.04%) ⬆️
examples 27.19% <40.00%> (-0.01%) ⬇️
ftw 55.47% <40.00%> (-0.07%) ⬇️
tinygo 71.29% <0.00%> (-0.08%) ⬇️

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

Impacted Files Coverage Δ
http/interceptor.go 45.34% <100.00%> (+0.31%) ⬆️
http/middleware.go 88.77% <100.00%> (+0.86%) ⬆️
internal/corazawaf/transaction.go 79.19% <100.00%> (+0.36%) ⬆️
config.go 34.00% <0.00%> (-1.79%) ⬇️

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 at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@M4tteoP
Copy link
Member Author

M4tteoP commented Nov 12, 2022

Thanks @jcchavezs for the review. I see your concern. I tried to properly document the exported methods and the expected use cases. Happy to keep the conversation going, and eventually revert the changes if we feel that there is too much risk of being misused.
My main use case would be corazawaf/coraza-proxy-wasm#68

http/middleware.go Outdated Show resolved Hide resolved
@jcchavezs
Copy link
Member

I think there are more tweaks that could be applied to middlewares/interceptors based on this. My main concern here would be: do we want to expose RuleEngineStatus() or just isRuleEngineOff()? We need a PR for proxy-wasm also exercising this API.

@M4tteoP
Copy link
Member Author

M4tteoP commented Nov 12, 2022

I think there are more tweaks that could be applied to middlewares/interceptors based on this.

I agree, I'm thinking about them, but they are tricky to be integrated with the interceptor logic.

do we want to expose RuleEngineStatus() or just isRuleEngineOff()?

I see both values in Off and DetectionOnly. E.g. if detection only, you have to send the buffer to Coraza, but you may assume that Coraza will never require a deny action. Therefore, you may also already send data to the backend server

@jcchavezs
Copy link
Member

Relevant #505

@jcchavezs
Copy link
Member

I think what I am chasing here is: do we want to expose the state or just let connectors know if engine is on/off. I was playing with it in the http middleware and ended up using three states:

  • doing nothing when engine is off
  • analysing the request/response body in a separate goroutine (as it won't change the response)

I guess only on/off works for proxy/wasm? how about caddy @jptosso ?

@M4tteoP
Copy link
Member Author

M4tteoP commented Nov 15, 2022

analysing the request/response body in a separate goroutine (as it won't change the response)

This is Detection only status, isn't it? I'm still wondering about the corner case of a rule triggered in detection only with ctl:ruleEngine=On. In that case, we should consider that Detection only may still end up with disruptive actions, any other early assumption may be wrong.

@jcchavezs
Copy link
Member

jcchavezs commented Nov 15, 2022 via email

@fzipi
Copy link
Member

fzipi commented Nov 15, 2022

I think it makes sense to expose the value or any method doing the same will do also.

@jptosso jptosso self-requested a review November 16, 2022 13:07
@jptosso
Copy link
Member

jptosso commented Nov 16, 2022

@jcchavezs on Caddy the connector will still fill all variables even if the engine is off. I'm not sure if it makes sense to control that behavior from the connector side, but maybe boolean methods would be more developer-friendly, like IsEngineEnabled().

@jcchavezs
Copy link
Member

So I guess we only need to expose EngineRuleOff or IsEngineRuleOff

@M4tteoP M4tteoP changed the title Exports RuleEngineStatus Exports isEngineRuleOff Nov 18, 2022
@M4tteoP
Copy link
Member Author

M4tteoP commented Nov 18, 2022

Mostly reverted this PR.

  • The exported function is now IsEngineRuleOff.
  • Revisited the comments about the proper usage of RequestBodyAccessible() and ResponseBodyAccessible().
  • Reverted middleware changes in favor of the more comprehensive feat: optimize body buffering. #505

The main use case in my mind was about DetectionOnly status that would have permitted the connectors to send information to Coraza and at the same time let the request continue. But I came up with that it is a wrong early assumption and it would impact at least one use case.

Sorry for the noise and many thanks for the feedback

@jcchavezs
Copy link
Member

jcchavezs commented Nov 18, 2022 via email

Copy link
Member

@fzipi fzipi left a comment

Choose a reason for hiding this comment

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

LGTM also!

@jcchavezs
Copy link
Member

jcchavezs commented Nov 18, 2022

Do you mind using the condition in the HTTP middleware to return same response writer when engine is off?

@M4tteoP M4tteoP changed the title Exports isEngineRuleOff Exports IsRuleEngineOff Nov 18, 2022
@M4tteoP
Copy link
Member Author

M4tteoP commented Nov 19, 2022

Done @jcchavezs, I hope this is what you expected. I felt was right to just keep the transaction generation thinking that Coraza is actually aware that a new request is coming, but then no rules are processed.

I also inverted "Rule" and "Engine" inside the function name being everywhere called RuleEngine (e.g. SecRuleEngine)

@jcchavezs
Copy link
Member

jcchavezs commented Nov 20, 2022 via email

@M4tteoP
Copy link
Member Author

M4tteoP commented Nov 21, 2022

My bad. I added the call to downstream to do the response. I'm thinking about anticipating, even more, the check to return the same response writer, but we need the transaction to check for engine off.

I added TestHttpServerWithRuleEngineOff decoupling TestHttpServer with a new helper function called runAgainstWaf. It adds flexibility to run different sets of requests against different Waf configs. Hope you like it 🙏

@jcchavezs jcchavezs merged commit bffb435 into corazawaf:v3/dev Nov 21, 2022
@M4tteoP M4tteoP deleted the export_enginestatus branch December 3, 2022 16:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants