Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / manifests / init.pp @ 1d331291

Historique | Voir | Annoter | Télécharger (8,92 ko)

1 e17693e3 Steve Traylen
# @summary Configure nftables
2
#
3 03d9e7da Steve Traylen
# @example allow dns out and do not allow ntp out
4 2063deaf hashworks
#   class{ 'nftables':
5
#     out_ntp => false,
6
#     out_dns => true,
7 b3a7a6dd tr
#   }
8 e17693e3 Steve Traylen
#
9 b9785000 Steve Traylen
# @example do not flush particular tables, fail2ban in this case
10 2063deaf hashworks
#   class{ 'nftables':
11
#     noflush_tables => ['inet-f2b-table'],
12 03d9e7da Steve Traylen
#   }
13
#
14 b3a7a6dd tr
# @param out_all
15 e17693e3 Steve Traylen
#   Allow all outbound connections. If `true` then all other
16
#   out parameters `out_ntp`, `out_dns`, ... will be assuemed
17
#   false.
18
#
19
# @param out_ntp
20
#   Allow outbound to ntp servers.
21
#
22
# @param out_http
23
#   Allow outbound to http servers.
24
#
25 09cba182 Steve Traylen
# @param out_dns
26
#   Allow outbound to dns servers.
27 e17693e3 Steve Traylen
#
28
# @param out_https
29
#   Allow outbound to https servers.
30
#
31 79e9a23f Nacho Barrientos
# @param out_icmp
32
#   Allow outbound ICMPv4/v6 traffic.
33
#
34 e17693e3 Steve Traylen
# @param in_ssh
35
#   Allow inbound to ssh servers.
36
#
37 79e9a23f Nacho Barrientos
# @param in_icmp
38
#   Allow inbound ICMPv4/v6 traffic.
39
#
40 7b9d6ffc Nacho Barrientos
# @param inet_filter
41
#   Add default tables, chains and rules to process traffic.
42
#
43 82d10659 Nacho Barrientos
# @param nat
44
#   Add default tables and chains to process NAT traffic.
45
#
46 fcb79d73 Ben Morrice
# @param nat_table_name
47
#   The name of the 'nat' table.
48
#
49 0b1e3353 canihavethisone
# @param purge_unmanaged_rules
50
#   Prohibits in-memory rules that are not declared in Puppet
51 c6941cfe canihavethisone
#   code. Setting this to true activates a check that reloads nftables
52 0b1e3353 canihavethisone
#   if the rules in memory have been modified without Puppet.
53
#
54
# @param inmem_rules_hash_file
55
#   The name of the file where the hash of the in-memory rules
56
#   will be stored.
57
#
58 802d80d1 Nacho Barrientos
# @param sets
59
#   Allows sourcing set definitions directly from Hiera.
60
#
61 ac0af4aa Nacho Barrientos
# @param log_prefix
62
#   String that will be used as prefix when logging packets. It can contain
63
#   two variables using standard sprintf() string-formatting:
64
#    * chain: Will be replaced by the name of the chain.
65
#    * comment: Allows chains to add extra comments.
66
#
67 a9bbb10d Vadym Chepkov
# @param log_discarded
68
#   Allow to log discarded packets
69
#
70 b10c6216 Nacho Barrientos
# @param log_limit
71
#  String with the content of a limit statement to be applied
72
#  to the rules that log discarded traffic. Set to false to
73
#  disable rate limiting.
74
#
75 70727742 Nacho Barrientos
# @param reject_with
76
#   How to discard packets not matching any rule. If `false`, the
77
#   fate of the packet will be defined by the chain policy (normally
78
#   drop), otherwise the packet will be rejected with the REJECT_WITH
79
#   policy indicated by the value of this parameter.
80
#
81 ea96d5db Nacho Barrientos
# @param in_out_conntrack
82
#   Adds INPUT and OUTPUT rules to allow traffic that's part of an
83
#   established connection and also to drop invalid packets.
84
#
85 eac19d14 Tim Meusel
# @param in_out_drop_invalid
86
#   Drops invalid packets in INPUT and OUTPUT
87
#
88 24a5a2a7 tr
# @param fwd_conntrack
89
#   Adds FORWARD rules to allow traffic that's part of an
90
#   established connection and also to drop invalid packets.
91
#
92 eac19d14 Tim Meusel
# @param fwd_drop_invalid
93
#   Drops invalid packets in FORWARD
94
#
95 ae9872e2 Nacho Barrientos
# @param firewalld_enable
96
#   Configures how the firewalld systemd service unit is enabled. It might be
97
#   useful to set this to false if you're externaly removing firewalld from
98
#   the system completely.
99
#
100 03d9e7da Steve Traylen
# @param noflush_tables
101
#   If specified only other existings tables will be flushed.
102
#   If left unset all tables will be flushed via a `flush ruleset`
103
#
104 09cba182 Steve Traylen
# @param rules
105
#   Specify hashes of `nftables::rule`s via hiera
106
#
107 0c9bc308 hashworks
# @param configuration_path
108
#   The absolute path to the principal nftables configuration file. The default
109
#   varies depending on the system, and is set in the module's data.
110
#
111 8842a597 Tim Meusel
# @param nft_path
112
#   Path to the nft binary
113
#
114 7fb93f38 Tim Meusel
# @param echo
115
#   Path to the echo binary
116
#
117 0b7bcb5d mh
# @param default_config_mode
118
#   The default file & dir mode for configuration files and directories. The
119
#   default varies depending on the system, and is set in the module's data.
120
#
121 a528bf59 Steve Traylen
# @param clobber_default_config
122
#   Should the existing OS provided rules in the `configuration_path` be removed? If
123
#   they are not being removed this module will add all of its configuration to the end of
124
#   the existing rules.
125
#
126 be0b08e1 tr
class nftables (
127 5b13f220 Javier Angulo
  Stdlib::Unixpath $echo,
128
  Stdlib::Unixpath $configuration_path,
129
  Stdlib::Unixpath $nft_path,
130
  Stdlib::Filemode $default_config_mode,
131 a528bf59 Steve Traylen
  Boolean $clobber_default_config = false,
132 31b17627 Steve Traylen
  Boolean $in_ssh = true,
133
  Boolean $in_icmp = true,
134
  Boolean $out_ntp = true,
135
  Boolean $out_dns = true,
136
  Boolean $out_http = true,
137
  Boolean $out_https = true,
138
  Boolean $out_icmp = true,
139
  Boolean $out_all = false,
140
  Boolean $in_out_conntrack = true,
141 eac19d14 Tim Meusel
  Boolean $in_out_drop_invalid = $in_out_conntrack,
142 31b17627 Steve Traylen
  Boolean $fwd_conntrack = false,
143 eac19d14 Tim Meusel
  Boolean $fwd_drop_invalid = $fwd_conntrack,
144 7b9d6ffc Nacho Barrientos
  Boolean $inet_filter = true,
145 31b17627 Steve Traylen
  Boolean $nat = true,
146 0b1e3353 canihavethisone
  Boolean $purge_unmanaged_rules = false,
147 31b17627 Steve Traylen
  Hash $rules = {},
148
  Hash $sets = {},
149
  String $log_prefix = '[nftables] %<chain>s %<comment>s',
150 fcb79d73 Ben Morrice
  String[1] $nat_table_name = 'nat',
151 e0bb7852 canihavethisone
  Stdlib::Unixpath $inmem_rules_hash_file = '/var/tmp/puppet-nft-memhash',
152 a9bbb10d Vadym Chepkov
  Boolean $log_discarded = true,
153 31b17627 Steve Traylen
  Variant[Boolean[false], String] $log_limit = '3/minute burst 5 packets',
154
  Variant[Boolean[false], Pattern[/icmp(v6|x)? type .+|tcp reset/]] $reject_with = 'icmpx type port-unreachable',
155
  Variant[Boolean[false], Enum['mask']] $firewalld_enable = 'mask',
156 1fd3f550 Luis Fernández Álvarez
  Optional[Array[Pattern[/^(ip|ip6|inet|arp|bridge|netdev)-[-a-zA-Z0-9_]+$/],1]] $noflush_tables = undef,
157 be0b08e1 tr
) {
158 11bf7237 Steve Traylen
  package { 'nftables':
159 0ba57c66 mh
    ensure => installed,
160 a528bf59 Steve Traylen
  }
161
162
  if $clobber_default_config {
163
    file { $configuration_path:
164
      ensure  => file,
165
      owner   => 'root',
166
      group   => 'root',
167
      mode    => $default_config_mode,
168
      content => "#Puppet Managed\ninclude \"/etc/nftables/puppet.nft\"\n",
169
      require => Package['nftables'],
170
      before  => File['/etc/nftables'],
171
      notify  => Service['nftables'],
172
    }
173
  } else {
174
    file_line { 'enable_nftables':
175
      line    => 'include "/etc/nftables/puppet.nft"',
176
      path    => $configuration_path,
177
      require => Package['nftables'],
178
      before  => File['/etc/nftables'],
179
      notify  => Service['nftables'],
180
    }
181
  }
182
183
  file {
184 0ba57c66 mh
    default:
185 e140adff tr
      owner => 'root',
186
      group => 'root',
187 0b7bcb5d mh
      mode  => $default_config_mode;
188 0c9bc308 hashworks
    '/etc/nftables':
189
      ensure => directory,
190 0b7bcb5d mh
      mode   => $default_config_mode;
191 30462da1 Steve Traylen
    '/etc/nftables/puppet-preflight':
192
      ensure  => directory,
193 0b7bcb5d mh
      mode    => $default_config_mode,
194 30462da1 Steve Traylen
      purge   => true,
195
      force   => true,
196
      recurse => true;
197
    '/etc/nftables/puppet-preflight.nft':
198 82d10659 Nacho Barrientos
      ensure  => file,
199 7b9d6ffc Nacho Barrientos
      content => epp('nftables/config/puppet.nft.epp', {
200
          'inet_filter' => $inet_filter,
201
          'nat'         => $nat,
202
          'noflush'     => $noflush_tables
203
        }
204
      );
205 11bf7237 Steve Traylen
  } ~> exec {
206 30462da1 Steve Traylen
    'nft validate':
207
      refreshonly => true,
208 d7e26575 Tim Meusel
      command     => "${nft_path} -I /etc/nftables/puppet-preflight -c -f /etc/nftables/puppet-preflight.nft || ( ${echo} '#CONFIG BROKEN' >> /etc/nftables/puppet-preflight.nft && /bin/false)"; # lint:ignore:check_unsafe_interpolations
209 11bf7237 Steve Traylen
  } -> file {
210 30462da1 Steve Traylen
    default:
211
      owner => 'root',
212
      group => 'root',
213 0b7bcb5d mh
      mode  => $default_config_mode;
214 0ba57c66 mh
    '/etc/nftables/puppet.nft':
215 82d10659 Nacho Barrientos
      ensure  => file,
216 7b9d6ffc Nacho Barrientos
      content => epp('nftables/config/puppet.nft.epp', {
217
          'inet_filter' => $inet_filter,
218
          'nat'         => $nat,
219
          'noflush'     => $noflush_tables
220
        }
221
      );
222 0ba57c66 mh
    '/etc/nftables/puppet':
223
      ensure  => directory,
224 0b7bcb5d mh
      mode    => $default_config_mode,
225 0ba57c66 mh
      purge   => true,
226
      force   => true,
227
      recurse => true;
228 11bf7237 Steve Traylen
  } ~> service { 'nftables':
229 30462da1 Steve Traylen
    ensure     => running,
230
    enable     => true,
231
    hasrestart => true,
232 cc9fc807 Tim Meusel
    restart    => 'PATH=/usr/bin:/bin systemctl reload nftables',
233 30462da1 Steve Traylen
  }
234
235 0b1e3353 canihavethisone
  if $purge_unmanaged_rules {
236 16fd95b1 canihavethisone
    # Reload nftables ruleset from disk if running state not match last service change hash, or is absent (-s required to ignore counters)
237 3016d428 canihavethisone
    exec { 'nftables_memory_state_check':
238 c1bd001d canihavethisone
      command  => ['echo', 'reloading_nftables'],
239
      path     => $facts['path'],
240
      provider => shell,
241
      unless   => ["test -s ${inmem_rules_hash_file} -a \"$(nft -s list ruleset | sha1sum)\" = \"$(cat ${inmem_rules_hash_file})\""],
242
      notify   => Service['nftables'],
243 0b1e3353 canihavethisone
    }
244
245 16fd95b1 canihavethisone
    # Generate nftables hash upon changes to the nftables service 
246 0b2ccdda canihavethisone
    exec { 'nftables_generate_hash':
247 c1bd001d canihavethisone
      command     => ["nft -s list ruleset | sha1sum > ${inmem_rules_hash_file}"],
248 c00bcf2d canihavethisone
      path        => $facts['path'],
249 c1bd001d canihavethisone
      provider    => shell,
250 0b1e3353 canihavethisone
      subscribe   => Service['nftables'],
251
      refreshonly => true,
252
    }
253
  }
254
255 11bf7237 Steve Traylen
  systemd::dropin_file { 'puppet_nft.conf':
256 03d9e7da Steve Traylen
    ensure  => present,
257
    unit    => 'nftables.service',
258 0c9bc308 hashworks
    content => epp('nftables/systemd/puppet_nft.conf.epp', {
259
        'configuration_path' => $configuration_path,
260 8842a597 Tim Meusel
        'nft_path'           => $nft_path,
261 0c9bc308 hashworks
    }),
262 03d9e7da Steve Traylen
    notify  => Service['nftables'],
263 0ba57c66 mh
  }
264
265 c4b1b93b Steve Traylen
  # firewalld.enable can be mask or false depending upon if firewalld is installed or not
266
  # https://tickets.puppetlabs.com/browse/PUP-10814
267 11bf7237 Steve Traylen
  service { 'firewalld':
268 f02562f2 tr
    ensure => stopped,
269 ae9872e2 Nacho Barrientos
    enable => $firewalld_enable,
270 f02562f2 tr
  }
271
272 7b9d6ffc Nacho Barrientos
  if $inet_filter {
273
    include nftables::inet_filter
274
  }
275
276 82d10659 Nacho Barrientos
  if $nat {
277
    include nftables::ip_nat
278
  }
279 b3a7a6dd tr
280
  # inject custom rules e.g. from hiera
281 66ed7f61 mh
  $rules.each |$n,$v| {
282 11bf7237 Steve Traylen
    nftables::rule {
283 66ed7f61 mh
      $n:
284 11bf7237 Steve Traylen
        * => $v,
285 66ed7f61 mh
    }
286
  }
287 802d80d1 Nacho Barrientos
288
  # inject custom sets e.g. from hiera
289
  $sets.each |$n,$v| {
290 11bf7237 Steve Traylen
    nftables::set {
291 802d80d1 Nacho Barrientos
      $n:
292 11bf7237 Steve Traylen
        * => $v,
293 802d80d1 Nacho Barrientos
    }
294
  }
295 0ba57c66 mh
}