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

Module configuration refactoring #139

Closed

Conversation

defanator
Copy link
Collaborator

The proposed changes are mostly aimed to improve overall code readability.

As a bonus, 2ae2a10 introduces a little feature of extending connector banner message with a total number of loaded rules with breakdown by rule source, e.g.:

2018/11/30 08:20:22 [notice] 9116#9116: ModSecurity-nginx v1.0.0 (rules loaded inline/local/remote: 0/1634/0)

It is being used for creating ModSecurity instance via msc_init() call,
as well as setting up necessary cleanups for the instance.

While here:

 - refactored cleanup handler for common context to highlight that its
   primary goal is to release memory consumed by rules;

 - made all sanity checks related code to be included only when
   MODSECURITY_SANITY_CHECKS is true.
Also, there is no need to set corresponding pointers to NULL as cleanup
handler should be called only once (per context).
@zimmerle zimmerle self-assigned this Nov 30, 2018
@zimmerle
Copy link
Contributor

zimmerle commented Dec 1, 2018

Hi @defanator,

The only thing that is crucial in terms of Rules merge, is the fact that somehow they have to respect the hierarchy in order to reduce the memory footprint. Two different virtual hosts can make usage of a rules set loaded in server configuration, instead of load two different copies of the rules into memory.

In the effort owasp-modsecurity/ModSecurity#1970 we are trying to allow the rules to be reloaded is run time, also, we are going to reduce the memory usage by replacing some textual tags with binary properties (good for memory and cpu) and shared_pointers instead of string repetition, pretty simple change that will produce a great result. But, yet, better to load the rules into memory only once, than have it loaded multiple times.

With that said, i think your patch is fine :) Indeed, it does improved the readability. The amount of loaded rules is amazing.

@defanator
Copy link
Collaborator Author

@zimmerle rules merging hasn't changed with the proposed PR, I've just removed duplicate parts of code (i.e. merge_srv_conf and merge_loc_conf were actually doing the same stuff).

In regards to the dynamic modsecurity configuration management, I definitely like the idea. Currently one has to reload nginx by sending SIGHUP to master process after modsecurity configuration has changed; despite such approach can work without breaking live traffic (thanks to nginx' ability to gracefully pass incoming load to new set of workers), avoiding workers restart is a way better thing. It may bring a number of challenges though.

@defanator
Copy link
Collaborator Author

@zimmerle in regards to memory footprint - yes, I'm pretty sure (lib)modsecurity (and/or connector modules) should be smart enough to detect whether the exactly same set of rules is being loaded for multiple entities (servers / locations / virtual hosts / you name it), and avoid any sort of duplicates.

I can imagine how to achieve this with loading rules from file (e.g. you can store and check SHA sums / modification times of a conf and its includes). Using remote sources may require additional checks like passing "If-Modified" and checking response codes, etc. Anyway, there is a room for improvements.

Copy link
Contributor

@victorhora victorhora left a comment

Choose a reason for hiding this comment

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

Oh man, I really like that the code is simplified 👍 Also loved the number of loaded rules in log message at start. Will certainly be helpful in a number of cases, including SecRemoteRules 👍

Looks good to me :)

@zimmerle what do you think about merging this and tag a release as 1.1 or 1.0.1? :)

zimmerle pushed a commit that referenced this pull request Dec 17, 2018
@zimmerle
Copy link
Contributor

Merged! ;) thanks!

@zimmerle zimmerle closed this Dec 17, 2018
@defanator defanator deleted the configuration-refactoring branch December 26, 2018 06:23
pracj3am pushed a commit to cdn77/ModSecurity-nginx that referenced this pull request Nov 4, 2022
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

3 participants