Projet

Général

Profil

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

root / manifests / init.pp @ master

Historique | Voir | Annoter | Télécharger (9,35 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 2ad7193b Tomas Barton
# @param log_group
76
#  When specified, the Linux kernel will pass the packet to nfnetlink_log
77
#  which will send the log through a netlink socket to the specified group.
78
#
79 70727742 Nacho Barrientos
# @param reject_with
80
#   How to discard packets not matching any rule. If `false`, the
81
#   fate of the packet will be defined by the chain policy (normally
82
#   drop), otherwise the packet will be rejected with the REJECT_WITH
83
#   policy indicated by the value of this parameter.
84
#
85 ea96d5db Nacho Barrientos
# @param in_out_conntrack
86
#   Adds INPUT and OUTPUT rules to allow traffic that's part of an
87
#   established connection and also to drop invalid packets.
88
#
89 eac19d14 Tim Meusel
# @param in_out_drop_invalid
90
#   Drops invalid packets in INPUT and OUTPUT
91
#
92 24a5a2a7 tr
# @param fwd_conntrack
93
#   Adds FORWARD rules to allow traffic that's part of an
94
#   established connection and also to drop invalid packets.
95
#
96 eac19d14 Tim Meusel
# @param fwd_drop_invalid
97
#   Drops invalid packets in FORWARD
98
#
99 ae9872e2 Nacho Barrientos
# @param firewalld_enable
100
#   Configures how the firewalld systemd service unit is enabled. It might be
101
#   useful to set this to false if you're externaly removing firewalld from
102
#   the system completely.
103
#
104 03d9e7da Steve Traylen
# @param noflush_tables
105
#   If specified only other existings tables will be flushed.
106
#   If left unset all tables will be flushed via a `flush ruleset`
107
#
108 09cba182 Steve Traylen
# @param rules
109
#   Specify hashes of `nftables::rule`s via hiera
110
#
111 0c9bc308 hashworks
# @param configuration_path
112
#   The absolute path to the principal nftables configuration file. The default
113
#   varies depending on the system, and is set in the module's data.
114
#
115 8842a597 Tim Meusel
# @param nft_path
116
#   Path to the nft binary
117
#
118 7fb93f38 Tim Meusel
# @param echo
119
#   Path to the echo binary
120
#
121 0b7bcb5d mh
# @param default_config_mode
122
#   The default file & dir mode for configuration files and directories. The
123
#   default varies depending on the system, and is set in the module's data.
124
#
125 a528bf59 Steve Traylen
# @param clobber_default_config
126
#   Should the existing OS provided rules in the `configuration_path` be removed? If
127
#   they are not being removed this module will add all of its configuration to the end of
128
#   the existing rules.
129
#
130 be0b08e1 tr
class nftables (
131 5b13f220 Javier Angulo
  Stdlib::Unixpath $echo,
132
  Stdlib::Unixpath $configuration_path,
133
  Stdlib::Unixpath $nft_path,
134
  Stdlib::Filemode $default_config_mode,
135 a528bf59 Steve Traylen
  Boolean $clobber_default_config = false,
136 31b17627 Steve Traylen
  Boolean $in_ssh = true,
137
  Boolean $in_icmp = true,
138
  Boolean $out_ntp = true,
139
  Boolean $out_dns = true,
140
  Boolean $out_http = true,
141
  Boolean $out_https = true,
142
  Boolean $out_icmp = true,
143
  Boolean $out_all = false,
144
  Boolean $in_out_conntrack = true,
145 eac19d14 Tim Meusel
  Boolean $in_out_drop_invalid = $in_out_conntrack,
146 31b17627 Steve Traylen
  Boolean $fwd_conntrack = false,
147 eac19d14 Tim Meusel
  Boolean $fwd_drop_invalid = $fwd_conntrack,
148 7b9d6ffc Nacho Barrientos
  Boolean $inet_filter = true,
149 31b17627 Steve Traylen
  Boolean $nat = true,
150 0b1e3353 canihavethisone
  Boolean $purge_unmanaged_rules = false,
151 31b17627 Steve Traylen
  Hash $rules = {},
152
  Hash $sets = {},
153
  String $log_prefix = '[nftables] %<chain>s %<comment>s',
154 2ad7193b Tomas Barton
  Optional[Integer[0]] $log_group = undef,
155 fcb79d73 Ben Morrice
  String[1] $nat_table_name = 'nat',
156 e0bb7852 canihavethisone
  Stdlib::Unixpath $inmem_rules_hash_file = '/var/tmp/puppet-nft-memhash',
157 a9bbb10d Vadym Chepkov
  Boolean $log_discarded = true,
158 31b17627 Steve Traylen
  Variant[Boolean[false], String] $log_limit = '3/minute burst 5 packets',
159
  Variant[Boolean[false], Pattern[/icmp(v6|x)? type .+|tcp reset/]] $reject_with = 'icmpx type port-unreachable',
160
  Variant[Boolean[false], Enum['mask']] $firewalld_enable = 'mask',
161 1fd3f550 Luis Fernández Álvarez
  Optional[Array[Pattern[/^(ip|ip6|inet|arp|bridge|netdev)-[-a-zA-Z0-9_]+$/],1]] $noflush_tables = undef,
162 be0b08e1 tr
) {
163 11bf7237 Steve Traylen
  package { 'nftables':
164 0ba57c66 mh
    ensure => installed,
165 a528bf59 Steve Traylen
  }
166
167 8f4434ed Steve Traylen
  # /etc/services file is needed, on newer OSes this is a dependency on nftables
168
  if $facts['os']['name'] == 'Ubuntu' and $facts['os']['release']['major'] == '20.04' {
169
    package { 'netbase':
170
      ensure => present,
171
    }
172
  }
173
174 a528bf59 Steve Traylen
  if $clobber_default_config {
175
    file { $configuration_path:
176
      ensure  => file,
177
      owner   => 'root',
178
      group   => 'root',
179
      mode    => $default_config_mode,
180
      content => "#Puppet Managed\ninclude \"/etc/nftables/puppet.nft\"\n",
181
      require => Package['nftables'],
182
      before  => File['/etc/nftables'],
183
      notify  => Service['nftables'],
184
    }
185
  } else {
186
    file_line { 'enable_nftables':
187
      line    => 'include "/etc/nftables/puppet.nft"',
188
      path    => $configuration_path,
189
      require => Package['nftables'],
190
      before  => File['/etc/nftables'],
191
      notify  => Service['nftables'],
192
    }
193
  }
194
195
  file {
196 0ba57c66 mh
    default:
197 e140adff tr
      owner => 'root',
198
      group => 'root',
199 0b7bcb5d mh
      mode  => $default_config_mode;
200 0c9bc308 hashworks
    '/etc/nftables':
201
      ensure => directory,
202 0b7bcb5d mh
      mode   => $default_config_mode;
203 30462da1 Steve Traylen
    '/etc/nftables/puppet-preflight':
204
      ensure  => directory,
205 0b7bcb5d mh
      mode    => $default_config_mode,
206 30462da1 Steve Traylen
      purge   => true,
207
      force   => true,
208
      recurse => true;
209
    '/etc/nftables/puppet-preflight.nft':
210 82d10659 Nacho Barrientos
      ensure  => file,
211 7b9d6ffc Nacho Barrientos
      content => epp('nftables/config/puppet.nft.epp', {
212
          'inet_filter' => $inet_filter,
213
          'nat'         => $nat,
214
          'noflush'     => $noflush_tables
215
        }
216
      );
217 11bf7237 Steve Traylen
  } ~> exec {
218 30462da1 Steve Traylen
    'nft validate':
219
      refreshonly => true,
220 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
221 11bf7237 Steve Traylen
  } -> file {
222 30462da1 Steve Traylen
    default:
223
      owner => 'root',
224
      group => 'root',
225 0b7bcb5d mh
      mode  => $default_config_mode;
226 0ba57c66 mh
    '/etc/nftables/puppet.nft':
227 82d10659 Nacho Barrientos
      ensure  => file,
228 7b9d6ffc Nacho Barrientos
      content => epp('nftables/config/puppet.nft.epp', {
229
          'inet_filter' => $inet_filter,
230
          'nat'         => $nat,
231
          'noflush'     => $noflush_tables
232
        }
233
      );
234 0ba57c66 mh
    '/etc/nftables/puppet':
235
      ensure  => directory,
236 0b7bcb5d mh
      mode    => $default_config_mode,
237 0ba57c66 mh
      purge   => true,
238
      force   => true,
239
      recurse => true;
240 11bf7237 Steve Traylen
  } ~> service { 'nftables':
241 30462da1 Steve Traylen
    ensure     => running,
242
    enable     => true,
243
    hasrestart => true,
244 cc9fc807 Tim Meusel
    restart    => 'PATH=/usr/bin:/bin systemctl reload nftables',
245 30462da1 Steve Traylen
  }
246
247 0b1e3353 canihavethisone
  if $purge_unmanaged_rules {
248 16fd95b1 canihavethisone
    # Reload nftables ruleset from disk if running state not match last service change hash, or is absent (-s required to ignore counters)
249 3016d428 canihavethisone
    exec { 'nftables_memory_state_check':
250 c1bd001d canihavethisone
      command  => ['echo', 'reloading_nftables'],
251
      path     => $facts['path'],
252
      provider => shell,
253
      unless   => ["test -s ${inmem_rules_hash_file} -a \"$(nft -s list ruleset | sha1sum)\" = \"$(cat ${inmem_rules_hash_file})\""],
254
      notify   => Service['nftables'],
255 0b1e3353 canihavethisone
    }
256
257 6097389c Tim Meusel
    # Generate nftables hash upon changes to the nftables service
258 0b2ccdda canihavethisone
    exec { 'nftables_generate_hash':
259 c1bd001d canihavethisone
      command     => ["nft -s list ruleset | sha1sum > ${inmem_rules_hash_file}"],
260 c00bcf2d canihavethisone
      path        => $facts['path'],
261 c1bd001d canihavethisone
      provider    => shell,
262 0b1e3353 canihavethisone
      subscribe   => Service['nftables'],
263
      refreshonly => true,
264
    }
265
  }
266
267 11bf7237 Steve Traylen
  systemd::dropin_file { 'puppet_nft.conf':
268 03d9e7da Steve Traylen
    ensure  => present,
269
    unit    => 'nftables.service',
270 0c9bc308 hashworks
    content => epp('nftables/systemd/puppet_nft.conf.epp', {
271
        'configuration_path' => $configuration_path,
272 8842a597 Tim Meusel
        'nft_path'           => $nft_path,
273 0c9bc308 hashworks
    }),
274 03d9e7da Steve Traylen
    notify  => Service['nftables'],
275 0ba57c66 mh
  }
276
277 c4b1b93b Steve Traylen
  # firewalld.enable can be mask or false depending upon if firewalld is installed or not
278
  # https://tickets.puppetlabs.com/browse/PUP-10814
279 11bf7237 Steve Traylen
  service { 'firewalld':
280 f02562f2 tr
    ensure => stopped,
281 ae9872e2 Nacho Barrientos
    enable => $firewalld_enable,
282 f02562f2 tr
  }
283
284 7b9d6ffc Nacho Barrientos
  if $inet_filter {
285
    include nftables::inet_filter
286
  }
287
288 82d10659 Nacho Barrientos
  if $nat {
289
    include nftables::ip_nat
290
  }
291 b3a7a6dd tr
292
  # inject custom rules e.g. from hiera
293 66ed7f61 mh
  $rules.each |$n,$v| {
294 11bf7237 Steve Traylen
    nftables::rule {
295 66ed7f61 mh
      $n:
296 11bf7237 Steve Traylen
        * => $v,
297 66ed7f61 mh
    }
298
  }
299 802d80d1 Nacho Barrientos
300
  # inject custom sets e.g. from hiera
301
  $sets.each |$n,$v| {
302 11bf7237 Steve Traylen
    nftables::set {
303 802d80d1 Nacho Barrientos
      $n:
304 11bf7237 Steve Traylen
        * => $v,
305 802d80d1 Nacho Barrientos
    }
306
  }
307 0ba57c66 mh
}