Puppet Class: iptables

Defined in:
manifests/init.pp

Summary

Manage iptables with default rule optimization and a failsafe fallback mode

Overview


> It is **highly recommended** that you place this module in “firewalld“ > mode if the underlying system supports it. > > You can do this by setting “iptables::use_firewalld: true“ in Hiera


This class will detect conflicts with the SIMP option “simp_options::firewall“ and, if necessary, cease management of IPTables in the case of a conflict.

In particular, this means that if “simp_options::firewall“ is “false“, but you have included this class, it will refuse to manage IPTables and will instead raise a warning.

If the “simp_options::firewall“ variable is not present, the module will manage IPTables as expected.

Parameters:

  • enable (Variant[Enum['ignore','firewalld'],Boolean]) (defaults to: simplib::lookup('simp_options::firewall', { 'default_value' => true }))

    Enable IPTables

    • If set to “true“ will enable management of IPTables

    • If set to “false“ will disable IPTables completely

    • If set to “ignore“ will stop managing IPTables

  • use_firewalld (Boolean) (defaults to: true)

    Explicitly enable management via “simp_firewalld“

    • Systems that do not have “firewalld“ installed will fall back to “iptables“

  • ensure (String) (defaults to: simplib::lookup('simp_options::package_ensure', { 'default_value' => 'installed' }))

    The state that the “package“ resources should target

    • May take any value acceptable to the native “package“ resource “ensure“ parameter

  • ipv6 (Boolean) (defaults to: true)

    Also manage IP6Tables

  • class_debug (Boolean) (defaults to: false)

    Print messages regarding rule comparisons

  • optimize_rules (Boolean) (defaults to: true)

    Run the inbuilt iptables rule optimizer to collapse the rules down to as small as is reasonably possible without reordering

    • IPSets have been incorporated via the ‘firewalld` module

  • precise_match (Boolean) (defaults to: false)

    Instead of matching rule counts, perform a more precise match against the running and to-be-applied rules. You may find that minor changes, such as a simple netmask change will not be enforced without enabling this option.

    • NOTE: You MUST use the exact same syntax that will be returned by ‘iptables-save` and `ip6tables-save` if you use this option!

    • For example, you cannot write ‘echo-request` for an ICMP echo match, you must instead use `8`.

  • ignore (Array[String[1]]) (defaults to: [])

    Regular expressions that you would like to match in order to preserve running rules

    • This modifies the behavior of the “iptables_optimize“ Type.

    • Do not include the beginning and ending “/“ but do include an end or beginning of word marker (“^“ and/or “$“) if appropriate

  • default_rules (Boolean) (defaults to: true)

    Enable the usual set of default deny rules that you would expect to see on most systems

    • Uses the following expectations of rule ordering (not enforced):

      * 1     -> ``ESTABLISHED`` and ``RELATED`` rules
      * 2-5   -> Standard ``ACCEPT`` and ``DENY`` rules
      * 6-10  -> ``JUMP`` to other rule sets
      * 11-20 -> Pure ``ACCEPT`` rules
      * 22-30 -> ``LOG`` and ``REJECT`` rules
      
  • scanblock (Boolean) (defaults to: false)

    Enable a technique for setting up port-based triggers that will block anyone connecting to the system for an hour after connection to a forbidden port

  • prevent_localhost_spoofing (Boolean) (defaults to: true)

    Add rules to “PREROUTING“ that will prevent spoofed packets from “localhost“ addresses from reaching your system

  • ports (Optional[Hash]) (defaults to: undef)

    A hash with structure as defined below that will open ports based on the structure of the hash. @example An example section of hieradata:

    iptables::ports:
      defaults:
        apply_to: ipv4
      80:
      53:
        proto: udp
      443:
        apply_to: ipv6
    

Author:



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'manifests/init.pp', line 106

class iptables (
  Variant[Enum['ignore','firewalld'],Boolean] $enable         = simplib::lookup('simp_options::firewall', { 'default_value' => true }),
  Boolean                         $use_firewalld              = true,
  String                          $ensure                     = simplib::lookup('simp_options::package_ensure', { 'default_value' => 'installed' }),
  Boolean                         $ipv6                       = true,
  Boolean                         $class_debug                = false,
  Boolean                         $optimize_rules             = true,
  Boolean                         $precise_match              = false,
  Array[String[1]]                $ignore                     = [],
  Boolean                         $default_rules              = true,
  Boolean                         $scanblock                  = false,
  Boolean                         $prevent_localhost_spoofing = true,
  Optional[Hash]                  $ports                      = undef
) {
  simplib::assert_metadata($module_name)

  if $enable != 'ignore' {
    # This is required in case you want to put firewalld in iptables mode
    contain 'iptables::install'

    if $use_firewalld {
      simplib::assert_optional_dependency($module_name, 'simp/simp_firewalld')

      include 'simp_firewalld'

      if $ports {
        iptables::ports {'firewalld':
          ports => $ports
        }
      }
    } else {
      contain 'iptables::service'

      if $default_rules { contain 'iptables::rules::base' }
      if $scanblock { contain 'iptables::rules::scanblock' }
      if $prevent_localhost_spoofing { contain 'iptables::rules::prevent_localhost_spoofing' }

      contain 'iptables::rules::default_drop'

      Class['iptables::install'] -> Class['iptables::service']

      file { '/etc/sysconfig/iptables':
        owner   => 'root',
        group   => 'root',
        mode    => '0640',
        require => Class['iptables::install']
      }

      # These are required to run if you are managing iptables with the custom
      # types at all.
      iptables_optimize { '/etc/sysconfig/iptables':
        optimize      => $optimize_rules,
        ignore        => $ignore,
        disable       => !$enable,
        precise_match => $precise_match,
        require       => Class['iptables::install']
      }

      if $ipv6 and $facts['ipv6_enabled'] {
        file { '/etc/sysconfig/ip6tables':
          owner   => 'root',
          group   => 'root',
          mode    => '0640',
          require => Class['iptables::install']
        }

        ip6tables_optimize { '/etc/sysconfig/ip6tables':
          optimize      => $optimize_rules,
          ignore        => $ignore,
          disable       => !$enable,
          precise_match => $precise_match,
          require       => Class['iptables::install']
        }
      }

      if $ports {
        iptables::ports {'iptables':
          ports   => $ports,
          require => Class['iptables::install']
        }
      }
    }
  }
}