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

Disable default modsecurity_rules_file if modsecurity-snippet is specified #8021

Merged
merged 15 commits into from
Dec 23, 2021

Conversation

besha100
Copy link
Contributor

@besha100 besha100 commented Dec 8, 2021

The default modsecurity_rules_file (/etc/nginx/modsecurity/modsecurity.conf;) has settings that override the ModSecurity-snippet if it is specified with custom config settings like "SecAuditLog", "SecAuditLogStorageDir" and "SecRuleEngine On".

What this PR does / why we need it:

To disable the default modsecurity_rules_file (/etc/nginx/modsecurity/modsecurity.conf;) if custom modsecurity-snippet is specified

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation only

Which issue/s this PR fixes

6307
6438
4640

How Has This Been Tested?

In the values yaml file have the config below
config:
enable-modsecurity: "true"
enable-owasp-modsecurity-crs: "false"
modsecurity-snippet: |-
SecRuleEngine On
SecAuditEngine RelevantOnly
SecAuditLogParts ABIJDEFHZ
SecAuditLogRelevantStatus "^(?:05912300)"
SecAuditLogType Concurrent
SecAuditLog /var/tmp/modsec_audit.log
SecAuditLogStorageDir /var/tmp/

Then deploying the latest nginx controller
helm upgrade --install --values your-values-with-config-above.yaml your-release-name ingress-nginx/ingress-nginx --version 4.0.13

After you deploy, exec into one nginx pod and browse " /etc/nginx/nginx.conf ". You will find after your modsecurity custom config the below line is included

  modsecurity_rules_file /etc/nginx/modsecurity/modsecurity.conf;

Browsing this file, you will find some settings that conflict with the settings in your "modsecurity-snippet: | " such as:

    SecRuleEngine DetectionOnly
    SecAuditLog /var/log/modsec_audit.log
    SecAuditLogStorageDir /var/log/audit/

Verification:
make some suspicious traffic to your nginx ingress such as

curl http://your-url/index.html\?exec\=/bin/bash

Then when you exec into the nginx pod, you will see the logs are in "/var/log/modsec_audit.log" (from the default modsecurity file) instead of the customized configured one "/var/tmp/modsec_audit.log" in the (modsecurity-snippet)

This PR will not have this include file " modsecurity_rules_file /etc/nginx/modsecurity/modsecurity.conf" in the nginx configuration file. After deploying this PR, all the custom modsecurity-snippet will take effect and not be overridden.
Then if you do suspicious traffic to your nginx ingress like the one above,t he audit log will be /var/tmp/modsec_audit.log , and SecRuleEngine On will be on the blocking mode according to the rules in your modsecurity-snippet

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I've read the CONTRIBUTION guide
  • I have added tests to cover my changes.
  • All new and existing tests passed.

…cifed

The default modsecurity_rules_file overwrites the ModSecurity-snippet if it is specified with custom config settings like "SecRuleEngine On". This will not let Modsecurity be in blocking mode even if "SecRuleEngine On" is specified in the ModSecurity-snippet configuration
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Dec 8, 2021

CLA Signed

The committers are authorized under a signed CLA.

@k8s-ci-robot
Copy link
Contributor

Thanks for your pull request. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please follow instructions at https://git.k8s.io/community/CLA.md#the-contributor-license-agreement to sign the CLA.

It may take a couple minutes for the CLA signature to be fully registered; after that, please reply here with a new comment and we'll verify. Thanks.


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. I understand the commands that are listed here.

@k8s-ci-robot k8s-ci-robot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-kind Indicates a PR lacks a `kind/foo` label and requires one. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Dec 8, 2021
@k8s-ci-robot
Copy link
Contributor

Welcome @besha100!

It looks like this is your first PR to kubernetes/ingress-nginx 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes/ingress-nginx has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot
Copy link
Contributor

Hi @besha100. 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 /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

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.

@k8s-ci-robot k8s-ci-robot added needs-priority size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Dec 8, 2021
@besha100
Copy link
Contributor Author

besha100 commented Dec 8, 2021

/assign @thockin

Copy link
Contributor

@iamNoah1 iamNoah1 left a comment

Choose a reason for hiding this comment

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

#6307

I find it strange that you add a commented line that does nothing if modsecurity snippet is speficied and only enable modsecuritx rules file in the else case. IMHO it would be more straight forward if we would have only the if case where we add the rules file if snippet is empty and have no else case at all.

Also I would expect some tests that cover this change.

@iamNoah1
Copy link
Contributor

iamNoah1 commented Dec 8, 2021

/triage accepted
/priority important-longterm
/kind bug

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. kind/bug Categorizes issue or PR as related to a bug. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority needs-kind Indicates a PR lacks a `kind/foo` label and requires one. labels Dec 8, 2021
Only have the default Modsecurity conf settings in case Modsecurity configuration snippet is not present and remove unnecessary comments
@besha100
Copy link
Contributor Author

/test pull-ingress-nginx-test

@besha100
Copy link
Contributor Author

@iamNoah1 I need help here. All e2e tests have passed here .
but in the build log this one failed and couldn't figure out why? can you please help? it doesn't look like one of my changes

@besha100 besha100 requested a review from iamNoah1 December 16, 2021 09:54
@iamNoah1
Copy link
Contributor

/retest-required

@@ -1781,8 +1781,8 @@ func TestModSecurityForLocation(t *testing.T) {
{"configmap enabled, configmap OWASP enabled, annotation enabled, OWASP disabled", true, true, true, true, false, "", "", ""},
{"configmap disabled, annotation enabled, OWASP disabled", false, false, true, true, false, "", "", fmt.Sprintf("%v%v", loadModule, modSecCfg)},
{"configmap disabled, annotation disabled, OWASP disabled", false, false, false, true, false, "", "", ""},
{"configmap disabled, annotation enabled, OWASP disabled", false, false, true, true, false, testRule, "", fmt.Sprintf("%v%v%v", loadModule, modsecRule, modSecCfg)},
{"configmap disabled, annotation enabled, OWASP enabled", false, false, true, true, false, testRule, "", fmt.Sprintf("%v%v%v", loadModule, modsecRule, modSecCfg)},
{"configmap disabled, annotation enabled, OWASP disabled", false, false, true, true, false, testRule, "", fmt.Sprintf("%v%v%v", loadModule, modsecRule)},
Copy link
Contributor

Choose a reason for hiding this comment

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

Hm, I guess this could be a problem. Why did you remove it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@iamNoah1 I removed only modSecCfg which is (modsecurity_rules_file /etc/nginx/modsecurity/modsecurity.conf) line. Becasue it shouldn't exist with modsecRule which is (modsecurity_rules).

Now, I reverted this change and now you can see the error is changed and now it is
"
=== RUN TestModSecurityForLocation
template_test.go:1805: configmap disabled, annotation enabled, OWASP disabled: expected 'modsecurity on;
modsecurity_rules '
#RULE#
';
modsecurity_rules_file /etc/nginx/modsecurity/modsecurity.conf;
' but returned 'modsecurity on;
modsecurity_rules '
#RULE#
';
'
template_test.go:1805: configmap disabled, annotation enabled, OWASP enabled: expected 'modsecurity on;
modsecurity_rules '
#RULE#
';
modsecurity_rules_file /etc/nginx/modsecurity/modsecurity.conf;
' but returned 'modsecurity on;
modsecurity_rules '
#RULE#
';
'
--- FAIL: TestModSecurityForLocation (0.00s)

"

These are the two tests that I have removed modSecCfg in the existence of modsecRule.
Can you advise on this.
Thank you:)

Copy link
Contributor

Choose a reason for hiding this comment

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

ok, then probably I was wrong and your initial change was correct. Anyways I am not that familiar with the Code and will try to drag some more experienced devs in.

Copy link
Contributor

@iamNoah1 iamNoah1 Dec 16, 2021

Choose a reason for hiding this comment

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

Ok I think I found it:

k8s.io/ingress-nginx/internal/ingress/controller/template internal/ingress/controller/template/template_test.go:1784:109: Sprintf format %v reads arg #3, but call has 2 args internal/ingress/controller/template/template_test.go:1785:108: Sprintf format %v reads arg #3, but call has 2 args

---> seems that your initial change was correct but you forgott to adjust the %vs.

@besha100 besha100 requested a review from iamNoah1 December 16, 2021 13:17
Copy link
Contributor

@iamNoah1 iamNoah1 left a comment

Choose a reason for hiding this comment

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

see my last comment

@besha100
Copy link
Contributor Author

@iamNoah1 MAN, YOU ARE THE BEST. I fixed it and now all tests have passed :)
Thank you

@besha100 besha100 requested a review from iamNoah1 December 16, 2021 19:14
@besha100
Copy link
Contributor Author

@rikatz Can you please review and approve

Copy link
Contributor

@iamNoah1 iamNoah1 left a comment

Choose a reason for hiding this comment

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

/lgtm
As far as I can say

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Dec 17, 2021
@rikatz
Copy link
Contributor

rikatz commented Dec 17, 2021

hey, didn't read everything right now (sorry), but a question: I understand this is a bug, but can this also be a breaking change for folks already relying on modsecurity and this behavior?

Thanks!

@iamNoah1
Copy link
Contributor

@rikatz @besha100 As far as I understand and I may be wrong, the behavior is buggy at the moment. People may have implemented a workaround in order to get the expected behavior. But I don't think that this change will breaks those peoples workaround. @besha100 can you please check that.

@besha100
Copy link
Contributor Author

besha100 commented Dec 17, 2021

@rikatz @iamNoah1 I will clarify who, what and how this will affect the folks who use modsecurity.
There are 3 ways different setup/behavior for ModSecurity:

1- Default Configuration: Here Modsecurity is enabled by setting "enable-modsecurity: true". With this ModSecurity will run in “Detection-Only” mode using the default configuration. This PR is not affecting this way, as the line (modsecurity_rules_file /etc/nginx/modsecurity/modsecurity.conf;) will exist in the nginx config.

2- OWASP Core Rule Set: Here Modsecurity is enabled by "enable-modsecurity: true" and "enable-owasp-core-rules: true". Here usually modsecurity-snippet is not specified and thus the change will not affect it. But even if it is specified, there is already a logic to include this file "modsecurity_rules_file /etc/nginx/owasp-modsecurity-crs/nginx-modsecurity.conf;" as configuration/rule file, as this file includes some other rule files and a config file (crs-setup.conf). Thus, This PR is also not affecting this way.

3- Snippet: Here modsecurity is enabled by "enable-modsecurity: true" and "modsecurity-snippet: |-". This PR is only affecting this way of implementing Modsecurity. But this way is currently working as the first approach and not as it meant, because the custom settings you specify under "modsecurity-snippet: |-". will be overridden by the default config file.
This PR will make Modsecurity with the exact config you specify under snippet, but here folks are expected to have their own settings or including/referring to rules files because the default ModSecurity file rules will not be used.

@iamNoah1 you are right:) Here people like these had to mount a volume in the controller container to override the default config in "/etc/nginx/modsecurity/modsecurity.conf". and I agree with you that those who have such workaround solutions won't be affected as they already include custom config/rule files under "modsecurity-snippet: |-".
With this change implemented, Even if someone just want the default modsecurity config, they can include it under "modsecurity-snippet: |-".

To summarize, This PR affects only approcah 3 and approach 3 is currently not working as expected and rather working like approach 1. Folks who want to use approach 3 need to explicitly write the settings/rules or include files under "modsecurity-snippet: |-" that has the custom settings and rules they need. So, I believe this shouldn't be a breaking change for anyone.

@PuneeshMotwani
Copy link

@rikatz - can you please review and approve, We have been waiting for this change since a while.

Good Work @besha100 in getting this out of the door. 🥇

@besha100
Copy link
Contributor Author

@rikatz Have you managed to look into this and my answer above?

@rikatz
Copy link
Contributor

rikatz commented Dec 23, 2021

/approve
Thanks for the explanation and the PR :) happy holidays

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: besha100, iamNoah1, rikatz

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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Dec 23, 2021
@k8s-ci-robot k8s-ci-robot merged commit fea7fed into kubernetes:main Dec 23, 2021
@rikatz rikatz added this to the v1.1.1 milestone Jan 9, 2022
rchshld pushed a commit to joomcode/ingress-nginx that referenced this pull request May 19, 2023
…ified (kubernetes#8021)

* Disabled default modsecurity_rules_file if modsecurity-snippet is specifed

The default modsecurity_rules_file overwrites the ModSecurity-snippet if it is specified with custom config settings like "SecRuleEngine On". This will not let Modsecurity be in blocking mode even if "SecRuleEngine On" is specified in the ModSecurity-snippet configuration

* Remove unnecessary comments

Only have the default Modsecurity conf settings in case Modsecurity configuration snippet is not present and remove unnecessary comments

* Fixed modsecurity default file only if Modsecurity snippet present

Fixed if condition  Modsecurity snippet present have modsecurity default config file

* Added e2e test to disabling modsecurity conf

Added e2e in case modsecurity-snippet enabled to disable settings in default modsecurity.conf

* Validate writing to a different location

Validate also modsecurity to write to a different location instead of the default directory

* Fixed the formatting

* Fixed if empty ModsecuritySnippet

* Fixed ModsecuritySnippet condition

* Fixed the condition also in ingress controller template

* Removed the default config condition  in ingress controller template

* Fixed the default config condition in ingress controller template

* Fixed pull-ingress-nginx-test

* Revert "Fixed the default config condition in ingress controller template"

This reverts commit 9d38eca.

* Revert template_test

* Adjusted the formating %v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants