-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Only load module ngx_http_modsecurity_module.so when option enable-mo… #4119
Only load module ngx_http_modsecurity_module.so when option enable-mo… #4119
Conversation
Hi @tammert. Thanks for your PR. I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
rootfs/etc/nginx/template/nginx.tmpl
Outdated
@@ -16,7 +16,9 @@ pid {{ .PID }}; | |||
load_module /etc/nginx/modules/ngx_http_geoip2_module.so; | |||
{{ end }} | |||
|
|||
{{ if $all.Cfg.EnableModsecurity }} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't enough because ModSecurity can be enabled per location too: https://github.com/kubernetes/ingress-nginx/blob/38ab2203331568d05eb7c587f1a4ff72e1a32623/rootfs/etc/nginx/template/nginx.tmpl#L1117
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I missed your "special notes"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I said above still holds: #4119 (comment)
hrm, looking at implementation and the e2e tests https://github.com/kubernetes/ingress-nginx/blob/master/test/e2e/annotations/modsecurity.go I think that documentation is not correct. |
Actually, I would argue that the current implementation for enabling it for separate locations might be flawed. The background for this: we first tried configuring it with the Ingress Annotation, however that resulted in the situation where We solved this situation by loading all rule files once, in the HTTP block. Then, for each Ingress, we used In that design, the top-level ConfigMap configuration would be the place to arrange all this, but that would result in a larger refactor than the original scope of the issue/PR. What do you think? |
@tammert are you saying it is not possible to enable modsecurity per location? (I never used modsecurity personally). Can we not fix the existing implementation instead to make sure when you set the annotation only it enables it in the respective locations and nowhere else? Can you add some more assertion to the e2e tests to show that in fact it gets configured for all location and not just for the locations where it is intended to be enabled via annotation? |
No it actually works the way it's been designed: if you set the Annotation on an Ingress, those locations will have ModSecurity enabled (without having to configure it instance-level, in the So let's say you have an Ingress with the Annotation that contains a total of 10 locations, spread over any number of hostnames. In this case, by setting the Annotation on the Ingress, you would load all rule files 10 times, which ModSecurity seems to hold in memory, thus increasing memory for NGINX by a lot. Because of this, I would argue that actually configuring (at the very least) the core rule sets for each location is not good practice. If you load them once on instance level, in the Does that make more sense? |
@tammert that makes sense to me. Basically when the configmap setting is enabled then modesecurity will be configured in HTTP level and be enabled for all locations. And if we want a location to not have it, then an annotation can be used to whitelist a specific ingress locations. @aledbf what do you think? |
@ElvinEfendi exactly. And what might be good to realize is that by default, ModSecurity runs in I think if we manage to convey this design properly via the documentation it should be much easier for people to start effectively using ModSecurity. |
Are you sure? This PR was merged this week #4091 |
@aledbf I was actually working with the recent codebase, so yes. Good to see someone else has noticed this as well though :). However, that PR only fixes the situation where the ConfigMap contains `enable-modsecurity: "true". A valid configuration that still has this issue: nothing configured for ModSecurity in the ConfigMap, with an Annotation on the Ingress. In this case, every location in the Ingress (which, in my experience, would probably be more than a single location) would still load the rules, resulting in multiple loads with corresponding memory use. By putting the loading of rules entirely behind the ConfigMap configuration it would be simpler for the user to configure, with almost no chance for multiple loads. Again, I hope this makes sense! I struggled to get it properly configured when I started using this functionality and I hope I can improve it for others. |
/ok-to-test |
Codecov Report
@@ Coverage Diff @@
## master #4119 +/- ##
=========================================
+ Coverage 57.66% 57.77% +0.1%
=========================================
Files 87 87
Lines 6463 6479 +16
=========================================
+ Hits 3727 3743 +16
Misses 2306 2306
Partials 430 430
Continue to review full report at Codecov.
|
/retest |
@ElvinEfendi @aledbf tests seem to have passed so take a look at the code when you get a chance :) If I need to rebase or squash the commits, let me know. |
@tammert I prefer to do this kind of thing in code and not in the template |
rootfs/etc/nginx/template/nginx.tmpl
Outdated
{{ end }} | ||
{{ end }} | ||
{{ end }} | ||
{{ end }} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please introduce a new function (i.e shouldLoadModSecurityModule($servers)
) in template.go
and move this logic to there. That way you can have a unit test for it too.
Then below you can just call that function.
@ElvinEfendi @aledbf thanks so much for the feedback! I've learned a lot from this PR. The code should be much neater now: I've introduced the |
/approve |
@tammert ready to squash the commits now. |
@ElvinEfendi commit squashing is done. |
/lgtm |
@tammert thanks! |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: aledbf, ElvinEfendi, tammert The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
What this PR does / why we need it:
Ensures the
load_module
directive forngx_http_modsecurity_module.so
is only present innginx.conf
whenenable-modsecurity: true
is present in the ConfigMap, instead of always. For everyone not using ModSecurity, this should provide a slightly more lean application.Which issue this PR fixes:
Fixes #3842
Special notes for your reviewer:
ModSecurity can be enabled on via ConfigMap or Annotation. However, in the documentation for the Annotation it clearly states:
Because of this, I feel like we can simply place the loading of the module behind the ConfigMap option.