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
# @summary Configure nftables
2
#
3
# @example allow dns out and do not allow ntp out
4
#   class{ 'nftables':
5
#     out_ntp => false,
6
#     out_dns => true,
7
#   }
8
#
9
# @example do not flush particular tables, fail2ban in this case
10
#   class{ 'nftables':
11
#     noflush_tables => ['inet-f2b-table'],
12
#   }
13
#
14
# @param out_all
15
#   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
# @param out_dns
26
#   Allow outbound to dns servers.
27
#
28
# @param out_https
29
#   Allow outbound to https servers.
30
#
31
# @param out_icmp
32
#   Allow outbound ICMPv4/v6 traffic.
33
#
34
# @param in_ssh
35
#   Allow inbound to ssh servers.
36
#
37
# @param in_icmp
38
#   Allow inbound ICMPv4/v6 traffic.
39
#
40
# @param inet_filter
41
#   Add default tables, chains and rules to process traffic.
42
#
43
# @param nat
44
#   Add default tables and chains to process NAT traffic.
45
#
46
# @param nat_table_name
47
#   The name of the 'nat' table.
48
#
49
# @param purge_unmanaged_rules
50
#   Prohibits in-memory rules that are not declared in Puppet
51
#   code. Setting this to true activates a check that reloads nftables
52
#   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
# @param sets
59
#   Allows sourcing set definitions directly from Hiera.
60
#
61
# @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
# @param log_discarded
68
#   Allow to log discarded packets
69
#
70
# @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
# @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
# @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
# @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
# @param in_out_drop_invalid
90
#   Drops invalid packets in INPUT and OUTPUT
91
#
92
# @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
# @param fwd_drop_invalid
97
#   Drops invalid packets in FORWARD
98
#
99
# @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
# @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
# @param rules
109
#   Specify hashes of `nftables::rule`s via hiera
110
#
111
# @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
# @param nft_path
116
#   Path to the nft binary
117
#
118
# @param echo
119
#   Path to the echo binary
120
#
121
# @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
# @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
class nftables (
131
  Stdlib::Unixpath $echo,
132
  Stdlib::Unixpath $configuration_path,
133
  Stdlib::Unixpath $nft_path,
134
  Stdlib::Filemode $default_config_mode,
135
  Boolean $clobber_default_config = false,
136
  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
  Boolean $in_out_drop_invalid = $in_out_conntrack,
146
  Boolean $fwd_conntrack = false,
147
  Boolean $fwd_drop_invalid = $fwd_conntrack,
148
  Boolean $inet_filter = true,
149
  Boolean $nat = true,
150
  Boolean $purge_unmanaged_rules = false,
151
  Hash $rules = {},
152
  Hash $sets = {},
153
  String $log_prefix = '[nftables] %<chain>s %<comment>s',
154
  Optional[Integer[0]] $log_group = undef,
155
  String[1] $nat_table_name = 'nat',
156
  Stdlib::Unixpath $inmem_rules_hash_file = '/var/tmp/puppet-nft-memhash',
157
  Boolean $log_discarded = true,
158
  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
  Optional[Array[Pattern[/^(ip|ip6|inet|arp|bridge|netdev)-[-a-zA-Z0-9_]+$/],1]] $noflush_tables = undef,
162
) {
163
  package { 'nftables':
164
    ensure => installed,
165
  }
166

    
167
  # /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
  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
    default:
197
      owner => 'root',
198
      group => 'root',
199
      mode  => $default_config_mode;
200
    '/etc/nftables':
201
      ensure => directory,
202
      mode   => $default_config_mode;
203
    '/etc/nftables/puppet-preflight':
204
      ensure  => directory,
205
      mode    => $default_config_mode,
206
      purge   => true,
207
      force   => true,
208
      recurse => true;
209
    '/etc/nftables/puppet-preflight.nft':
210
      ensure  => file,
211
      content => epp('nftables/config/puppet.nft.epp', {
212
          'inet_filter' => $inet_filter,
213
          'nat'         => $nat,
214
          'noflush'     => $noflush_tables
215
        }
216
      );
217
  } ~> exec {
218
    'nft validate':
219
      refreshonly => true,
220
      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
  } -> file {
222
    default:
223
      owner => 'root',
224
      group => 'root',
225
      mode  => $default_config_mode;
226
    '/etc/nftables/puppet.nft':
227
      ensure  => file,
228
      content => epp('nftables/config/puppet.nft.epp', {
229
          'inet_filter' => $inet_filter,
230
          'nat'         => $nat,
231
          'noflush'     => $noflush_tables
232
        }
233
      );
234
    '/etc/nftables/puppet':
235
      ensure  => directory,
236
      mode    => $default_config_mode,
237
      purge   => true,
238
      force   => true,
239
      recurse => true;
240
  } ~> service { 'nftables':
241
    ensure     => running,
242
    enable     => true,
243
    hasrestart => true,
244
    restart    => 'PATH=/usr/bin:/bin systemctl reload nftables',
245
  }
246

    
247
  if $purge_unmanaged_rules {
248
    # Reload nftables ruleset from disk if running state not match last service change hash, or is absent (-s required to ignore counters)
249
    exec { 'nftables_memory_state_check':
250
      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
    }
256

    
257
    # Generate nftables hash upon changes to the nftables service
258
    exec { 'nftables_generate_hash':
259
      command     => ["nft -s list ruleset | sha1sum > ${inmem_rules_hash_file}"],
260
      path        => $facts['path'],
261
      provider    => shell,
262
      subscribe   => Service['nftables'],
263
      refreshonly => true,
264
    }
265
  }
266

    
267
  systemd::dropin_file { 'puppet_nft.conf':
268
    ensure  => present,
269
    unit    => 'nftables.service',
270
    content => epp('nftables/systemd/puppet_nft.conf.epp', {
271
        'configuration_path' => $configuration_path,
272
        'nft_path'           => $nft_path,
273
    }),
274
    notify  => Service['nftables'],
275
  }
276

    
277
  # firewalld.enable can be mask or false depending upon if firewalld is installed or not
278
  # https://tickets.puppetlabs.com/browse/PUP-10814
279
  service { 'firewalld':
280
    ensure => stopped,
281
    enable => $firewalld_enable,
282
  }
283

    
284
  if $inet_filter {
285
    include nftables::inet_filter
286
  }
287

    
288
  if $nat {
289
    include nftables::ip_nat
290
  }
291

    
292
  # inject custom rules e.g. from hiera
293
  $rules.each |$n,$v| {
294
    nftables::rule {
295
      $n:
296
        * => $v,
297
    }
298
  }
299

    
300
  # inject custom sets e.g. from hiera
301
  $sets.each |$n,$v| {
302
    nftables::set {
303
      $n:
304
        * => $v,
305
    }
306
  }
307
}