diff --git a/REFERENCE.md b/REFERENCE.md index 423e89c7f5..6de74e6878 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -6352,7 +6352,7 @@ Default value: `$apache::params::modsec_dir` ##### `modsec_secruleengine` -Data type: `String` +Data type: `Enum['On', 'Off', 'DetectionOnly']` Configures the rules engine. @@ -8108,6 +8108,7 @@ The following parameters are available in the `apache::vhost` defined type: * [`userdir`](#-apache--vhost--userdir) * [`proxy_protocol`](#-apache--vhost--proxy_protocol) * [`proxy_protocol_exceptions`](#-apache--vhost--proxy_protocol_exceptions) +* [`modsec_rule_engine`](#-apache--vhost--modsec_rule_engine) ##### `access_log` @@ -8983,7 +8984,8 @@ Default value: `undef` Data type: `Boolean` -Disables `mod_security` on a virtual host. Only valid if `apache::mod::security` is included. +Deprecated. Disables `mod_security` on a virtual host. Use `modsec_rule_engine => 'Off'` instead. +Only valid if `apache::mod::security` is included. Default value: `false` @@ -11052,6 +11054,15 @@ Disable processing of PROXY header for certain hosts or networks Default value: `[]` +##### `modsec_rule_engine` + +Data type: `Optional[Enum['On', 'Off', 'DetectionOnly']]` + +Configures the ModSecurity rules engine for this virtual host. When unset, the virtual host +inherits the global `apache::mod::security::modsec_secruleengine` setting. + +Default value: `undef` + ### `apache::vhost::custom` 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. diff --git a/manifests/mod/security.pp b/manifests/mod/security.pp index adeb659435..2b95bb6b97 100644 --- a/manifests/mod/security.pp +++ b/manifests/mod/security.pp @@ -184,8 +184,8 @@ Boolean $custom_rules = $apache::params::modsec_custom_rules, Optional[Array[String]] $custom_rules_set = $apache::params::modsec_custom_rules_set, Stdlib::Absolutepath $modsec_dir = $apache::params::modsec_dir, - String $modsec_secruleengine = $apache::params::modsec_secruleengine, - Integer[0, 9] $debug_log_level = 0, + Enum['On', 'Off', 'DetectionOnly'] $modsec_secruleengine = $apache::params::modsec_secruleengine, + Integer[0, 9] $debug_log_level = 0, String $audit_log_relevant_status = '^(?:5|4(?!04))', String $audit_log_parts = $apache::params::modsec_audit_log_parts, String $audit_log_type = $apache::params::modsec_audit_log_type, diff --git a/manifests/vhost.pp b/manifests/vhost.pp index 9718541f93..fff8d076ad 100644 --- a/manifests/vhost.pp +++ b/manifests/vhost.pp @@ -475,7 +475,8 @@ # Configures the maximum request body size (in bytes) ModSecurity accepts for buffering. # # @param modsec_disable_vhost -# Disables `mod_security` on a virtual host. Only valid if `apache::mod::security` is included. +# Deprecated. Disables `mod_security` on a virtual host. Use `modsec_rule_engine => 'Off'` instead. +# Only valid if `apache::mod::security` is included. # # @param modsec_disable_ids # Removes `mod_security` IDs from the virtual host.
@@ -1710,6 +1711,11 @@ # # @param proxy_protocol_exceptions # Disable processing of PROXY header for certain hosts or networks +# +# @param modsec_rule_engine +# Configures the ModSecurity rules engine for this virtual host. When unset, the virtual host +# inherits the global `apache::mod::security::modsec_secruleengine` setting. +# define apache::vhost ( Variant[Stdlib::Absolutepath, Boolean] $docroot, Boolean $manage_docroot = true, @@ -1973,6 +1979,7 @@ Optional[Variant[String[1], Array[String[1]]]] $userdir = undef, Optional[Boolean] $proxy_protocol = undef, Array[Stdlib::Host] $proxy_protocol_exceptions = [], + Optional[Enum['On', 'Off', 'DetectionOnly']] $modsec_rule_engine = undef, ) { # The base class must be included first because it is used by parameter defaults if ! defined(Class['apache']) { @@ -2181,28 +2188,19 @@ } ## Create a global LocationMatch if locations aren't defined - if $modsec_disable_ids { - if $modsec_disable_ids =~ Array { - $_modsec_disable_ids = { '.*' => $modsec_disable_ids } - } else { - $_modsec_disable_ids = $modsec_disable_ids - } + $_modsec_disable_ids = $modsec_disable_ids ? { + Array => { '.*' => $modsec_disable_ids }, + default => $modsec_disable_ids, } - if $modsec_disable_msgs { - if $modsec_disable_msgs =~ Array { - $_modsec_disable_msgs = { '.*' => $modsec_disable_msgs } - } else { - $_modsec_disable_msgs = $modsec_disable_msgs - } + $_modsec_disable_msgs = $modsec_disable_msgs ? { + Array => { '.*' => $modsec_disable_msgs }, + default => $modsec_disable_msgs, } - if $modsec_disable_tags { - if $modsec_disable_tags =~ Array { - $_modsec_disable_tags = { '.*' => $modsec_disable_tags } - } else { - $_modsec_disable_tags = $modsec_disable_tags - } + $_modsec_disable_tags = $modsec_disable_tags ? { + Array => { '.*' => $modsec_disable_tags }, + default => $modsec_disable_tags, } concat { "${priority_real}${filename}.conf": @@ -2840,19 +2838,33 @@ } } - 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 { - $security_params = { - 'modsec_disable_vhost' => $modsec_disable_vhost, - 'modsec_audit_log_destination' => $modsec_audit_log_destination, - '_modsec_disable_ids' => $modsec_disable_ids, - 'modsec_disable_ips' => $modsec_disable_ips, - '_modsec_disable_msgs' => $modsec_disable_msgs, - '_modsec_disable_tags' => $modsec_disable_tags, - 'modsec_body_limit' => $modsec_body_limit, - 'modsec_inbound_anomaly_threshold' => $modsec_inbound_anomaly_threshold, - 'modsec_outbound_anomaly_threshold' => $modsec_outbound_anomaly_threshold, - 'modsec_allowed_methods' => $modsec_allowed_methods, - } + if $modsec_disable_vhost { + deprecation('apache::vhost::modsec_disable_vhost', 'This parameter is deprecated. Set `modsec_rule_engine` to `Off` instead.') + } + + $_modsec_rule_engine = $modsec_rule_engine ? { + undef => $modsec_disable_vhost ? { + true => 'Off', + default => undef, + }, + default => $modsec_rule_engine, + } + + $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) + + $security_params = { + '_modsec_rule_engine' => $_modsec_rule_engine, + 'modsec_audit_log_destination' => $modsec_audit_log_destination, + '_modsec_disable_ids' => $_modsec_disable_ids, + 'modsec_disable_ips' => $modsec_disable_ips, + '_modsec_disable_msgs' => $_modsec_disable_msgs, + '_modsec_disable_tags' => $_modsec_disable_tags, + 'modsec_body_limit' => $modsec_body_limit, + 'modsec_inbound_anomaly_threshold' => $modsec_inbound_anomaly_threshold, + 'modsec_outbound_anomaly_threshold' => $modsec_outbound_anomaly_threshold, + 'modsec_allowed_methods' => $modsec_allowed_methods, + } + if $modsec_enabled { concat::fragment { "${name}-security": target => "${priority_real}${filename}.conf", order => 320, diff --git a/spec/defines/vhost_spec.rb b/spec/defines/vhost_spec.rb index 355c20741a..602f84b9b5 100644 --- a/spec/defines/vhost_spec.rb +++ b/spec/defines/vhost_spec.rb @@ -1417,6 +1417,82 @@ content: %r{^\s*SecAuditLog "/var/log/#{apache_name}/rspec\.example\.com_security\.log"$}, ) } + + it { + expect(subject).to contain_concat__fragment('rspec.example.com-security') + .without_content(%r{^\s*SecRuleEngine\s}) + } + end + + context 'modsec_rule_engine On' do + let :params do + { + 'docroot' => '/rspec/docroot', + 'modsec_rule_engine' => 'On' + } + end + + it { is_expected.to compile } + + it { + expect(subject).to contain_concat__fragment('rspec.example.com-security').with( + content: %r{^\s*SecRuleEngine On$}, + ) + } + end + + context 'modsec_rule_engine DetectionOnly' do + let :params do + { + 'docroot' => '/rspec/docroot', + 'modsec_rule_engine' => 'DetectionOnly' + } + end + + it { is_expected.to compile } + + it { + expect(subject).to contain_concat__fragment('rspec.example.com-security').with( + content: %r{^\s*SecRuleEngine DetectionOnly$}, + ) + } + end + + context 'modsec_rule_engine Off' do + let :params do + { + 'docroot' => '/rspec/docroot', + 'modsec_rule_engine' => 'Off', + 'modsec_audit_log' => true, + 'modsec_allowed_methods' => 'GET HEAD POST OPTIONS' + } + end + + it { is_expected.to compile } + + it { + expect(subject).to contain_concat__fragment('rspec.example.com-security') + .with_content(%r{^\s*SecRuleEngine Off$}) + .without_content(%r{^\s*SecAuditLog\s}) + .without_content(%r{^\s*SecAction\s}) + } + end + + context 'legacy modsec_disable_vhost' do + let :params do + { + 'docroot' => '/rspec/docroot', + 'modsec_disable_vhost' => true + } + end + + it { is_expected.to compile } + + it { + expect(subject).to contain_concat__fragment('rspec.example.com-security').with( + content: %r{^\s*SecRuleEngine Off$}, + ) + } end context 'modsec_audit_log_file' do diff --git a/templates/vhost/_security.epp b/templates/vhost/_security.epp index 24d3b4a39c..e249926b31 100644 --- a/templates/vhost/_security.epp +++ b/templates/vhost/_security.epp @@ -1,55 +1,67 @@ +<%- | Optional[Enum['On', 'Off', 'DetectionOnly']] $_modsec_rule_engine, + Optional[String] $modsec_audit_log_destination, + Optional[Hash] $_modsec_disable_ids, + Array[String] $modsec_disable_ips, + Optional[Hash] $_modsec_disable_msgs, + Optional[Hash] $_modsec_disable_tags, + Optional[String] $modsec_body_limit, + Optional[Integer] $modsec_inbound_anomaly_threshold, + Optional[Integer] $modsec_outbound_anomaly_threshold, + Optional[String] $modsec_allowed_methods, +| -%> -<% if $modsec_disable_vhost {-%> - SecRuleEngine Off +<% if $_modsec_rule_engine { -%> + SecRuleEngine <%= $_modsec_rule_engine %> <% } -%> -<% if $modsec_audit_log_destination {-%> +<% if $_modsec_rule_engine != 'Off' { -%> +<% if $modsec_audit_log_destination { -%> SecAuditLog "<%= $modsec_audit_log_destination %>" <% } -%> -<% if $_modsec_disable_ids =~ Hash {-%> -<% $_modsec_disable_ids.each |$location, $rules| {-%> +<% if $_modsec_disable_ids =~ Hash and !$_modsec_disable_ids.empty { -%> +<% $_modsec_disable_ids.each |$location, $rules| { -%> > -<% Array($rules).each |$rule| {-%> +<% Array($rules).each |$rule| { -%> SecRuleRemoveById <%= $rule %> <% } -%> <% } -%> <% } -%> -<% unless $modsec_disable_ips.empty {%> +<% unless $modsec_disable_ips.empty { -%> SecRule REMOTE_ADDR "<%= join($modsec_disable_ips, ',') %>" "nolog,allow,id:1234123455" - SecAction "phase:2,pass,nolog,id:1234123456" + SecAction "phase:2,pass,nolog,id:1234123456" <% } -%> -<% if $_modsec_disable_msgs =~ Hash {-%> -<% $_modsec_disable_msgs.each |$location, $rules| {-%> +<% if $_modsec_disable_msgs =~ Hash and !$_modsec_disable_msgs.empty { -%> +<% $_modsec_disable_msgs.each |$location, $rules| { -%> > -<% Array($rules).each |$rule| {-%> +<% Array($rules).each |$rule| { -%> SecRuleRemoveByMsg "<%= $rule %>" <% } -%> <% } -%> <% } -%> -<% if $_modsec_disable_tags =~ Hash {-%> -<% $_modsec_disable_tags.each |$location, $rules| {-%> +<% if $_modsec_disable_tags =~ Hash and !$_modsec_disable_tags.empty { -%> +<% $_modsec_disable_tags.each |$location, $rules| { -%> > -<% Array($rules).each |$rule| {-%> +<% Array($rules).each |$rule| { -%> SecRuleRemoveByTag "<%= $rule %>" <% } -%> <% } -%> <% } -%> -<% if $modsec_body_limit {-%> +<% if $modsec_body_limit { -%> SecRequestBodyLimit <%= $modsec_body_limit %> <% } -%> -<% if $modsec_inbound_anomaly_threshold and $modsec_outbound_anomaly_threshold {-%> +<% if $modsec_inbound_anomaly_threshold and $modsec_outbound_anomaly_threshold { -%> SecAction \ "id:900110,\ phase:1,\ nolog,\ pass,\ t:none,\ - setvar:tx.inbound_anomaly_score_threshold=<%= $modsec_inbound_anomaly_threshold -%>, \ - setvar:tx.outbound_anomaly_score_threshold=<%= $modsec_outbound_anomaly_threshold -%>" + setvar:tx.inbound_anomaly_score_threshold=<%= $modsec_inbound_anomaly_threshold %>, \ + setvar:tx.outbound_anomaly_score_threshold=<%= $modsec_outbound_anomaly_threshold %>" <% } -%> -<% if $modsec_allowed_methods {-%> +<% if $modsec_allowed_methods { -%> SecAction \ "id:900200,\ phase:1,\ @@ -58,4 +70,5 @@ t:none,\ setvar:'tx.allowed_methods=<%= $modsec_allowed_methods -%>'" <% } -%> +<% } -%>