Skip to content

Commit e44f100

Browse files
authored
Merge pull request #2145 from k2patel/MODULES-11061
(MODULES-11061) mod_security custom rule functionality
2 parents d237c5f + 2577bd9 commit e44f100

File tree

6 files changed

+109
-0
lines changed

6 files changed

+109
-0
lines changed

REFERENCE.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5969,6 +5969,8 @@ The following parameters are available in the `apache::mod::security` class:
59695969
* [`activated_rules`](#activated_rules)
59705970
* [`modsec_dir`](#modsec_dir)
59715971
* [`modsec_secruleengine`](#modsec_secruleengine)
5972+
* [`custom_rules`](#custom_rules)
5973+
* [`custom_rules_set`](#custom_rules_set)
59725974
* [`audit_log_relevant_status`](#audit_log_relevant_status)
59735975
* [`audit_log_parts`](#audit_log_parts)
59745976
* [`audit_log_type`](#audit_log_type)
@@ -6041,6 +6043,23 @@ Configures the rules engine.
60416043

60426044
Default value: `$apache::params::modsec_secruleengine`
60436045

6046+
##### <a name="custom_rules"></a>`custom_rules`
6047+
6048+
Data type: `Boolean`
6049+
6050+
Enable Custom rules for security, this does not create or provide any rules rather it facilitate to add custom rules.
6051+
If enabled, must provide `custom_rules_set`.
6052+
6053+
Default value: `$apache::params::modsec_custom_rules`
6054+
6055+
##### <a name="custom_rules_set"></a>`custom_rules_set`
6056+
6057+
Data type: `[Array]`
6058+
6059+
Customrules must be array of rules, for an example `['REMOTE_ADDR "^127.0.0.1" "id:199999,phase:1,nolog,allow,ctl:ruleEngine=off"']`.
6060+
Configures the set of custom rules.
6061+
6062+
Default value: `$apache::params::modsec_custom_rules_set`
60446063
##### <a name="audit_log_relevant_status"></a>`audit_log_relevant_status`
60456064

60466065
Data type: `Any`

manifests/mod/security.pp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@
9797
$version = $apache::params::modsec_version,
9898
$crs_package = $apache::params::modsec_crs_package,
9999
$activated_rules = $apache::params::modsec_default_rules,
100+
$custom_rules = $apache::params::modsec_custom_rules,
101+
$custom_rules_set = $apache::params::modsec_custom_rules_set,
100102
$modsec_dir = $apache::params::modsec_dir,
101103
$modsec_secruleengine = $apache::params::modsec_secruleengine,
102104
$audit_log_relevant_status = '^(?:5|4(?!04))',
@@ -216,6 +218,27 @@
216218
notify => Class['apache::service'],
217219
}
218220

221+
if $custom_rules {
222+
# Template to add custom rule and included in security configuration
223+
file {"${modsec_dir}/custom_rules":
224+
ensure => directory,
225+
owner => $apache::params::user,
226+
group => $apache::params::group,
227+
mode => $apache::file_mode,
228+
require => File[$modsec_dir],
229+
}
230+
231+
file { "${modsec_dir}/custom_rules/custom_01_rules.conf":
232+
ensure => file,
233+
owner => $apache::params::user,
234+
group => $apache::params::group,
235+
mode => $apache::file_mode,
236+
content => template('apache/mod/security_custom.conf.erb'),
237+
require => File["${modsec_dir}/custom_rules"],
238+
notify => Class['apache::service'],
239+
}
240+
}
241+
219242
if $manage_security_crs {
220243
# Template uses:
221244
# - $_secdefaultaction

manifests/params.pp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636

3737
$modsec_audit_log_parts = 'ABIJDEFHZ'
3838
$modsec_audit_log_type = 'Serial'
39+
$modsec_custom_rules = false
40+
$modsec_custom_rules_set = undef
3941

4042
# no client certs should be trusted for auth by default.
4143
$ssl_certs_dir = undef

spec/classes/mod/security_spec.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,33 @@
103103

104104
it { is_expected.not_to contain_file('/etc/httpd/modsecurity.d/security_crs.conf') }
105105
end
106+
describe 'with custom parameters' do
107+
let :params do
108+
{
109+
custom_rules: false,
110+
}
111+
end
112+
113+
it {
114+
is_expected.not_to contain_file('/etc/httpd/modsecurity.d/custom_rules/custom_01_rules.conf')
115+
}
116+
end
117+
describe 'with parameters' do
118+
let :params do
119+
{
120+
custom_rules: true,
121+
custom_rules_set: ['REMOTE_ADDR "^127.0.0.1" "id:199999,phase:1,nolog,allow,ctl:ruleEngine=off"'],
122+
}
123+
end
124+
125+
it {
126+
is_expected.to contain_file('/etc/httpd/modsecurity.d/custom_rules').with(
127+
ensure: 'directory', path: '/etc/httpd/modsecurity.d/custom_rules',
128+
owner: 'apache', group: 'apache'
129+
)
130+
}
131+
it { is_expected.to contain_file('/etc/httpd/modsecurity.d/custom_rules/custom_01_rules.conf').with_content %r{^\s*.*"id:199999,phase:1,nolog,allow,ctl:ruleEngine=off"$} }
132+
end
106133
end
107134
when 'Debian'
108135
context 'on Debian based systems' do
@@ -189,6 +216,35 @@
189216
end
190217
end
191218

219+
describe 'with custom parameters' do
220+
let :params do
221+
{
222+
custom_rules: false,
223+
}
224+
end
225+
226+
it {
227+
is_expected.not_to contain_file('/etc/modsecurity/custom_rules/custom_01_rules.conf')
228+
}
229+
end
230+
231+
describe 'with parameters' do
232+
let :params do
233+
{
234+
custom_rules: true,
235+
custom_rules_set: ['REMOTE_ADDR "^127.0.0.1" "id:199999,phase:1,nolog,allow,ctl:ruleEngine=off"'],
236+
}
237+
end
238+
239+
it {
240+
is_expected.to contain_file('/etc/modsecurity/custom_rules').with(
241+
ensure: 'directory', path: '/etc/modsecurity/custom_rules',
242+
owner: 'www-data', group: 'www-data'
243+
)
244+
}
245+
it { is_expected.to contain_file('/etc/modsecurity/custom_rules/custom_01_rules.conf').with_content %r{\s*.*"id:199999,phase:1,nolog,allow,ctl:ruleEngine=off"$} }
246+
end
247+
192248
describe 'with mod security version' do
193249
let :params do
194250
{

templates/mod/security.conf.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
# Default recommended configuration
33
SecRuleEngine <%= @modsec_secruleengine %>
44
SecRequestBodyAccess On
5+
<%- if @custom_rules -%>
6+
Include <%= @modsec_dir %>/custom_rules/*.conf
7+
<%- end -%>
58
SecRule REQUEST_HEADERS:Content-Type "text/xml" \
69
"id:'200000',phase:1,t:none,t:lowercase,pass,nolog,ctl:requestBodyProcessor=XML"
710
SecRequestBodyLimit <%= @secrequestbodylimit %>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# This file is managed by puppet, any direct modification will be overwritten.
2+
<% if defined?(@custom_rules_set) && ! @custom_rules_set.empty? -%>
3+
<% @custom_rules_set.each do |secrule| -%>
4+
SecRule <%= secrule %>
5+
<% end -%>
6+
<% end -%>

0 commit comments

Comments
 (0)