Skip to content

Commit 0c8abcc

Browse files
bovy89span786
authored andcommitted
feat(apache::vhost): refactor ModSecurity configuration handling
- Add modsec_rule_engine parameter (Enum['On','Off','DetectionOnly'], default 'On') to explicitly control SecRuleEngine directive - Deprecate modsec_disable_vhost boolean in favor of modsec_rule_engine (retain backward compatibility via deprecation warning) - Security fragment template is now always included (as before) - Add EPP signature with explicit type enforcement to _security.epp - Guard all non-engine directives in _security.epp behind modsec_rule_engine != 'Off' to prevent inert configuration noise when ModSecurity is disabled
1 parent 47bba2c commit 0c8abcc

5 files changed

Lines changed: 167 additions & 55 deletions

File tree

‎REFERENCE.md‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6352,7 +6352,7 @@ Default value: `$apache::params::modsec_dir`
63526352

63536353
##### <a name="-apache--mod--security--modsec_secruleengine"></a>`modsec_secruleengine`
63546354

6355-
Data type: `String`
6355+
Data type: `Enum['On', 'Off', 'DetectionOnly']`
63566356

63576357
Configures the rules engine.
63586358

@@ -8108,6 +8108,7 @@ The following parameters are available in the `apache::vhost` defined type:
81088108
* [`userdir`](#-apache--vhost--userdir)
81098109
* [`proxy_protocol`](#-apache--vhost--proxy_protocol)
81108110
* [`proxy_protocol_exceptions`](#-apache--vhost--proxy_protocol_exceptions)
8111+
* [`modsec_rule_engine`](#-apache--vhost--modsec_rule_engine)
81118112

81128113
##### <a name="-apache--vhost--access_log"></a>`access_log`
81138114

@@ -8983,7 +8984,8 @@ Default value: `undef`
89838984

89848985
Data type: `Boolean`
89858986

8986-
Disables `mod_security` on a virtual host. Only valid if `apache::mod::security` is included.
8987+
Deprecated. Disables `mod_security` on a virtual host. Use `modsec_rule_engine => 'Off'` instead.
8988+
Only valid if `apache::mod::security` is included.
89878989

89888990
Default value: `false`
89898991

@@ -11052,6 +11054,15 @@ Disable processing of PROXY header for certain hosts or networks
1105211054

1105311055
Default value: `[]`
1105411056

11057+
##### <a name="-apache--vhost--modsec_rule_engine"></a>`modsec_rule_engine`
11058+
11059+
Data type: `Optional[Enum['On', 'Off', 'DetectionOnly']]`
11060+
11061+
Configures the ModSecurity rules engine for this virtual host. When unset, the virtual host
11062+
inherits the global `apache::mod::security::modsec_secruleengine` setting.
11063+
11064+
Default value: `undef`
11065+
1105511066
### <a name="apache--vhost--custom"></a>`apache::vhost::custom`
1105611067

1105711068
The `apache::vhost::custom` defined type is a thin wrapper around the `apache::custom_config` defined type, and simply overrides some of its default settings specific to the virtual host directory in Apache.

‎manifests/mod/security.pp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@
184184
Boolean $custom_rules = $apache::params::modsec_custom_rules,
185185
Optional[Array[String]] $custom_rules_set = $apache::params::modsec_custom_rules_set,
186186
Stdlib::Absolutepath $modsec_dir = $apache::params::modsec_dir,
187-
String $modsec_secruleengine = $apache::params::modsec_secruleengine,
188-
Integer[0, 9] $debug_log_level = 0,
187+
Enum['On', 'Off', 'DetectionOnly'] $modsec_secruleengine = $apache::params::modsec_secruleengine,
188+
Integer[0, 9] $debug_log_level = 0,
189189
String $audit_log_relevant_status = '^(?:5|4(?!04))',
190190
String $audit_log_parts = $apache::params::modsec_audit_log_parts,
191191
String $audit_log_type = $apache::params::modsec_audit_log_type,

‎manifests/vhost.pp‎

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,8 @@
475475
# Configures the maximum request body size (in bytes) ModSecurity accepts for buffering.
476476
#
477477
# @param modsec_disable_vhost
478-
# Disables `mod_security` on a virtual host. Only valid if `apache::mod::security` is included.
478+
# Deprecated. Disables `mod_security` on a virtual host. Use `modsec_rule_engine => 'Off'` instead.
479+
# Only valid if `apache::mod::security` is included.
479480
#
480481
# @param modsec_disable_ids
481482
# Removes `mod_security` IDs from the virtual host.<br />
@@ -1710,6 +1711,11 @@
17101711
#
17111712
# @param proxy_protocol_exceptions
17121713
# Disable processing of PROXY header for certain hosts or networks
1714+
#
1715+
# @param modsec_rule_engine
1716+
# Configures the ModSecurity rules engine for this virtual host. When unset, the virtual host
1717+
# inherits the global `apache::mod::security::modsec_secruleengine` setting.
1718+
#
17131719
define apache::vhost (
17141720
Variant[Stdlib::Absolutepath, Boolean] $docroot,
17151721
Boolean $manage_docroot = true,
@@ -1973,6 +1979,7 @@
19731979
Optional[Variant[String[1], Array[String[1]]]] $userdir = undef,
19741980
Optional[Boolean] $proxy_protocol = undef,
19751981
Array[Stdlib::Host] $proxy_protocol_exceptions = [],
1982+
Optional[Enum['On', 'Off', 'DetectionOnly']] $modsec_rule_engine = undef,
19761983
) {
19771984
# The base class must be included first because it is used by parameter defaults
19781985
if ! defined(Class['apache']) {
@@ -2181,28 +2188,19 @@
21812188
}
21822189

21832190
## Create a global LocationMatch if locations aren't defined
2184-
if $modsec_disable_ids {
2185-
if $modsec_disable_ids =~ Array {
2186-
$_modsec_disable_ids = { '.*' => $modsec_disable_ids }
2187-
} else {
2188-
$_modsec_disable_ids = $modsec_disable_ids
2189-
}
2191+
$_modsec_disable_ids = $modsec_disable_ids ? {
2192+
Array => { '.*' => $modsec_disable_ids },
2193+
default => $modsec_disable_ids,
21902194
}
21912195

2192-
if $modsec_disable_msgs {
2193-
if $modsec_disable_msgs =~ Array {
2194-
$_modsec_disable_msgs = { '.*' => $modsec_disable_msgs }
2195-
} else {
2196-
$_modsec_disable_msgs = $modsec_disable_msgs
2197-
}
2196+
$_modsec_disable_msgs = $modsec_disable_msgs ? {
2197+
Array => { '.*' => $modsec_disable_msgs },
2198+
default => $modsec_disable_msgs,
21982199
}
21992200

2200-
if $modsec_disable_tags {
2201-
if $modsec_disable_tags =~ Array {
2202-
$_modsec_disable_tags = { '.*' => $modsec_disable_tags }
2203-
} else {
2204-
$_modsec_disable_tags = $modsec_disable_tags
2205-
}
2201+
$_modsec_disable_tags = $modsec_disable_tags ? {
2202+
Array => { '.*' => $modsec_disable_tags },
2203+
default => $modsec_disable_tags,
22062204
}
22072205

22082206
concat { "${priority_real}${filename}.conf":
@@ -2840,19 +2838,33 @@
28402838
}
28412839
}
28422840

2843-
if $modsec_disable_vhost or $modsec_disable_ids or !empty($modsec_disable_ips) or $modsec_disable_msgs or $modsec_disable_tags or $modsec_audit_log_destination or ($modsec_inbound_anomaly_threshold and $modsec_outbound_anomaly_threshold) or $modsec_allowed_methods {
2844-
$security_params = {
2845-
'modsec_disable_vhost' => $modsec_disable_vhost,
2846-
'modsec_audit_log_destination' => $modsec_audit_log_destination,
2847-
'_modsec_disable_ids' => $modsec_disable_ids,
2848-
'modsec_disable_ips' => $modsec_disable_ips,
2849-
'_modsec_disable_msgs' => $modsec_disable_msgs,
2850-
'_modsec_disable_tags' => $modsec_disable_tags,
2851-
'modsec_body_limit' => $modsec_body_limit,
2852-
'modsec_inbound_anomaly_threshold' => $modsec_inbound_anomaly_threshold,
2853-
'modsec_outbound_anomaly_threshold' => $modsec_outbound_anomaly_threshold,
2854-
'modsec_allowed_methods' => $modsec_allowed_methods,
2855-
}
2841+
if $modsec_disable_vhost {
2842+
deprecation('apache::vhost::modsec_disable_vhost', 'This parameter is deprecated. Set `modsec_rule_engine` to `Off` instead.')
2843+
}
2844+
2845+
$_modsec_rule_engine = $modsec_rule_engine ? {
2846+
undef => $modsec_disable_vhost ? {
2847+
true => 'Off',
2848+
default => undef,
2849+
},
2850+
default => $modsec_rule_engine,
2851+
}
2852+
2853+
$modsec_enabled = ($modsec_rule_engine != undef or $modsec_disable_vhost or $modsec_audit_log_destination != undef or $modsec_disable_ids != undef or !empty($modsec_disable_ips) or $modsec_disable_msgs != undef or $modsec_disable_tags != undef or $modsec_body_limit != undef or $modsec_inbound_anomaly_threshold != undef or $modsec_outbound_anomaly_threshold != undef or $modsec_allowed_methods != undef)
2854+
2855+
$security_params = {
2856+
'_modsec_rule_engine' => $_modsec_rule_engine,
2857+
'modsec_audit_log_destination' => $modsec_audit_log_destination,
2858+
'_modsec_disable_ids' => $_modsec_disable_ids,
2859+
'modsec_disable_ips' => $modsec_disable_ips,
2860+
'_modsec_disable_msgs' => $_modsec_disable_msgs,
2861+
'_modsec_disable_tags' => $_modsec_disable_tags,
2862+
'modsec_body_limit' => $modsec_body_limit,
2863+
'modsec_inbound_anomaly_threshold' => $modsec_inbound_anomaly_threshold,
2864+
'modsec_outbound_anomaly_threshold' => $modsec_outbound_anomaly_threshold,
2865+
'modsec_allowed_methods' => $modsec_allowed_methods,
2866+
}
2867+
if $modsec_enabled {
28562868
concat::fragment { "${name}-security":
28572869
target => "${priority_real}${filename}.conf",
28582870
order => 320,

‎spec/defines/vhost_spec.rb‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,6 +1417,82 @@
14171417
content: %r{^\s*SecAuditLog "/var/log/#{apache_name}/rspec\.example\.com_security\.log"$},
14181418
)
14191419
}
1420+
1421+
it {
1422+
expect(subject).to contain_concat__fragment('rspec.example.com-security')
1423+
.without_content(%r{^\s*SecRuleEngine\s})
1424+
}
1425+
end
1426+
1427+
context 'modsec_rule_engine On' do
1428+
let :params do
1429+
{
1430+
'docroot' => '/rspec/docroot',
1431+
'modsec_rule_engine' => 'On'
1432+
}
1433+
end
1434+
1435+
it { is_expected.to compile }
1436+
1437+
it {
1438+
expect(subject).to contain_concat__fragment('rspec.example.com-security').with(
1439+
content: %r{^\s*SecRuleEngine On$},
1440+
)
1441+
}
1442+
end
1443+
1444+
context 'modsec_rule_engine DetectionOnly' do
1445+
let :params do
1446+
{
1447+
'docroot' => '/rspec/docroot',
1448+
'modsec_rule_engine' => 'DetectionOnly'
1449+
}
1450+
end
1451+
1452+
it { is_expected.to compile }
1453+
1454+
it {
1455+
expect(subject).to contain_concat__fragment('rspec.example.com-security').with(
1456+
content: %r{^\s*SecRuleEngine DetectionOnly$},
1457+
)
1458+
}
1459+
end
1460+
1461+
context 'modsec_rule_engine Off' do
1462+
let :params do
1463+
{
1464+
'docroot' => '/rspec/docroot',
1465+
'modsec_rule_engine' => 'Off',
1466+
'modsec_audit_log' => true,
1467+
'modsec_allowed_methods' => 'GET HEAD POST OPTIONS'
1468+
}
1469+
end
1470+
1471+
it { is_expected.to compile }
1472+
1473+
it {
1474+
expect(subject).to contain_concat__fragment('rspec.example.com-security')
1475+
.with_content(%r{^\s*SecRuleEngine Off$})
1476+
.without_content(%r{^\s*SecAuditLog\s})
1477+
.without_content(%r{^\s*SecAction\s})
1478+
}
1479+
end
1480+
1481+
context 'legacy modsec_disable_vhost' do
1482+
let :params do
1483+
{
1484+
'docroot' => '/rspec/docroot',
1485+
'modsec_disable_vhost' => true
1486+
}
1487+
end
1488+
1489+
it { is_expected.to compile }
1490+
1491+
it {
1492+
expect(subject).to contain_concat__fragment('rspec.example.com-security').with(
1493+
content: %r{^\s*SecRuleEngine Off$},
1494+
)
1495+
}
14201496
end
14211497

14221498
context 'modsec_audit_log_file' do

‎templates/vhost/_security.epp‎

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,67 @@
1+
<%- | Optional[Enum['On', 'Off', 'DetectionOnly']] $_modsec_rule_engine,
2+
Optional[String] $modsec_audit_log_destination,
3+
Optional[Hash] $_modsec_disable_ids,
4+
Array[String] $modsec_disable_ips,
5+
Optional[Hash] $_modsec_disable_msgs,
6+
Optional[Hash] $_modsec_disable_tags,
7+
Optional[String] $modsec_body_limit,
8+
Optional[Integer] $modsec_inbound_anomaly_threshold,
9+
Optional[Integer] $modsec_outbound_anomaly_threshold,
10+
Optional[String] $modsec_allowed_methods,
11+
| -%>
112
<IfModule mod_security2.c>
2-
<% if $modsec_disable_vhost {-%>
3-
SecRuleEngine Off
13+
<% if $_modsec_rule_engine { -%>
14+
SecRuleEngine <%= $_modsec_rule_engine %>
415
<% } -%>
5-
<% if $modsec_audit_log_destination {-%>
16+
<% if $_modsec_rule_engine != 'Off' { -%>
17+
<% if $modsec_audit_log_destination { -%>
618
SecAuditLog "<%= $modsec_audit_log_destination %>"
719
<% } -%>
8-
<% if $_modsec_disable_ids =~ Hash {-%>
9-
<% $_modsec_disable_ids.each |$location, $rules| {-%>
20+
<% if $_modsec_disable_ids =~ Hash and !$_modsec_disable_ids.empty { -%>
21+
<% $_modsec_disable_ids.each |$location, $rules| { -%>
1022
<LocationMatch <%= $location %>>
11-
<% Array($rules).each |$rule| {-%>
23+
<% Array($rules).each |$rule| { -%>
1224
SecRuleRemoveById <%= $rule %>
1325
<% } -%>
1426
</LocationMatch>
1527
<% } -%>
1628
<% } -%>
17-
<% unless $modsec_disable_ips.empty {%>
29+
<% unless $modsec_disable_ips.empty { -%>
1830
SecRule REMOTE_ADDR "<%= join($modsec_disable_ips, ',') %>" "nolog,allow,id:1234123455"
19-
SecAction "phase:2,pass,nolog,id:1234123456"
31+
SecAction "phase:2,pass,nolog,id:1234123456"
2032
<% } -%>
21-
<% if $_modsec_disable_msgs =~ Hash {-%>
22-
<% $_modsec_disable_msgs.each |$location, $rules| {-%>
33+
<% if $_modsec_disable_msgs =~ Hash and !$_modsec_disable_msgs.empty { -%>
34+
<% $_modsec_disable_msgs.each |$location, $rules| { -%>
2335
<LocationMatch <%= $location %>>
24-
<% Array($rules).each |$rule| {-%>
36+
<% Array($rules).each |$rule| { -%>
2537
SecRuleRemoveByMsg "<%= $rule %>"
2638
<% } -%>
2739
</LocationMatch>
2840
<% } -%>
2941
<% } -%>
30-
<% if $_modsec_disable_tags =~ Hash {-%>
31-
<% $_modsec_disable_tags.each |$location, $rules| {-%>
42+
<% if $_modsec_disable_tags =~ Hash and !$_modsec_disable_tags.empty { -%>
43+
<% $_modsec_disable_tags.each |$location, $rules| { -%>
3244
<LocationMatch <%= $location %>>
33-
<% Array($rules).each |$rule| {-%>
45+
<% Array($rules).each |$rule| { -%>
3446
SecRuleRemoveByTag "<%= $rule %>"
3547
<% } -%>
3648
</LocationMatch>
3749
<% } -%>
3850
<% } -%>
39-
<% if $modsec_body_limit {-%>
51+
<% if $modsec_body_limit { -%>
4052
SecRequestBodyLimit <%= $modsec_body_limit %>
4153
<% } -%>
42-
<% if $modsec_inbound_anomaly_threshold and $modsec_outbound_anomaly_threshold {-%>
54+
<% if $modsec_inbound_anomaly_threshold and $modsec_outbound_anomaly_threshold { -%>
4355
SecAction \
4456
"id:900110,\
4557
phase:1,\
4658
nolog,\
4759
pass,\
4860
t:none,\
49-
setvar:tx.inbound_anomaly_score_threshold=<%= $modsec_inbound_anomaly_threshold -%>, \
50-
setvar:tx.outbound_anomaly_score_threshold=<%= $modsec_outbound_anomaly_threshold -%>"
61+
setvar:tx.inbound_anomaly_score_threshold=<%= $modsec_inbound_anomaly_threshold %>, \
62+
setvar:tx.outbound_anomaly_score_threshold=<%= $modsec_outbound_anomaly_threshold %>"
5163
<% } -%>
52-
<% if $modsec_allowed_methods {-%>
64+
<% if $modsec_allowed_methods { -%>
5365
SecAction \
5466
"id:900200,\
5567
phase:1,\
@@ -58,4 +70,5 @@
5870
t:none,\
5971
setvar:'tx.allowed_methods=<%= $modsec_allowed_methods -%>'"
6072
<% } -%>
73+
<% } -%>
6174
</IfModule>

0 commit comments

Comments
 (0)