Projet

Général

Profil

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

root / manifests / init.pp @ 79ef6104

Historique | Voir | Annoter | Télécharger (6,93 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 802d80d1 Nacho Barrientos
# @param sets
50
#   Allows sourcing set definitions directly from Hiera.
51
#
52 ac0af4aa Nacho Barrientos
# @param log_prefix
53
#   String that will be used as prefix when logging packets. It can contain
54
#   two variables using standard sprintf() string-formatting:
55
#    * chain: Will be replaced by the name of the chain.
56
#    * comment: Allows chains to add extra comments.
57
#
58 a9bbb10d Vadym Chepkov
# @param log_discarded
59
#   Allow to log discarded packets
60
#
61 b10c6216 Nacho Barrientos
# @param log_limit
62
#  String with the content of a limit statement to be applied
63
#  to the rules that log discarded traffic. Set to false to
64
#  disable rate limiting.
65
#
66 70727742 Nacho Barrientos
# @param reject_with
67
#   How to discard packets not matching any rule. If `false`, the
68
#   fate of the packet will be defined by the chain policy (normally
69
#   drop), otherwise the packet will be rejected with the REJECT_WITH
70
#   policy indicated by the value of this parameter.
71
#
72 ea96d5db Nacho Barrientos
# @param in_out_conntrack
73
#   Adds INPUT and OUTPUT rules to allow traffic that's part of an
74
#   established connection and also to drop invalid packets.
75
#
76 eac19d14 Tim Meusel
# @param in_out_drop_invalid
77
#   Drops invalid packets in INPUT and OUTPUT
78
#
79 24a5a2a7 tr
# @param fwd_conntrack
80
#   Adds FORWARD rules to allow traffic that's part of an
81
#   established connection and also to drop invalid packets.
82
#
83 eac19d14 Tim Meusel
# @param fwd_drop_invalid
84
#   Drops invalid packets in FORWARD
85
#
86 ae9872e2 Nacho Barrientos
# @param firewalld_enable
87
#   Configures how the firewalld systemd service unit is enabled. It might be
88
#   useful to set this to false if you're externaly removing firewalld from
89
#   the system completely.
90
#
91 03d9e7da Steve Traylen
# @param noflush_tables
92
#   If specified only other existings tables will be flushed.
93
#   If left unset all tables will be flushed via a `flush ruleset`
94
#
95 09cba182 Steve Traylen
# @param rules
96
#   Specify hashes of `nftables::rule`s via hiera
97
#
98 0c9bc308 hashworks
# @param configuration_path
99
#   The absolute path to the principal nftables configuration file. The default
100
#   varies depending on the system, and is set in the module's data.
101
#
102 8842a597 Tim Meusel
# @param nft_path
103
#   Path to the nft binary
104
#
105 7fb93f38 Tim Meusel
# @param echo
106
#   Path to the echo binary
107
#
108 0b7bcb5d mh
# @param default_config_mode
109
#   The default file & dir mode for configuration files and directories. The
110
#   default varies depending on the system, and is set in the module's data.
111
#
112 be0b08e1 tr
class nftables (
113 5b13f220 Javier Angulo
  Stdlib::Unixpath $echo,
114
  Stdlib::Unixpath $configuration_path,
115
  Stdlib::Unixpath $nft_path,
116
  Stdlib::Filemode $default_config_mode,
117 31b17627 Steve Traylen
  Boolean $in_ssh = true,
118
  Boolean $in_icmp = true,
119
  Boolean $out_ntp = true,
120
  Boolean $out_dns = true,
121
  Boolean $out_http = true,
122
  Boolean $out_https = true,
123
  Boolean $out_icmp = true,
124
  Boolean $out_all = false,
125
  Boolean $in_out_conntrack = true,
126 eac19d14 Tim Meusel
  Boolean $in_out_drop_invalid = $in_out_conntrack,
127 31b17627 Steve Traylen
  Boolean $fwd_conntrack = false,
128 eac19d14 Tim Meusel
  Boolean $fwd_drop_invalid = $fwd_conntrack,
129 7b9d6ffc Nacho Barrientos
  Boolean $inet_filter = true,
130 31b17627 Steve Traylen
  Boolean $nat = true,
131
  Hash $rules = {},
132
  Hash $sets = {},
133
  String $log_prefix = '[nftables] %<chain>s %<comment>s',
134 fcb79d73 Ben Morrice
  String[1] $nat_table_name = 'nat',
135 a9bbb10d Vadym Chepkov
  Boolean $log_discarded = true,
136 31b17627 Steve Traylen
  Variant[Boolean[false], String] $log_limit = '3/minute burst 5 packets',
137
  Variant[Boolean[false], Pattern[/icmp(v6|x)? type .+|tcp reset/]] $reject_with = 'icmpx type port-unreachable',
138
  Variant[Boolean[false], Enum['mask']] $firewalld_enable = 'mask',
139 1fd3f550 Luis Fernández Álvarez
  Optional[Array[Pattern[/^(ip|ip6|inet|arp|bridge|netdev)-[-a-zA-Z0-9_]+$/],1]] $noflush_tables = undef,
140 be0b08e1 tr
) {
141 11bf7237 Steve Traylen
  package { 'nftables':
142 0ba57c66 mh
    ensure => installed,
143 11bf7237 Steve Traylen
  } -> file_line {
144 0ba57c66 mh
    'enable_nftables':
145
      line   => 'include "/etc/nftables/puppet.nft"',
146 0c9bc308 hashworks
      path   => $configuration_path,
147 0ba57c66 mh
      notify => Service['nftables'],
148 11bf7237 Steve Traylen
  } -> file {
149 0ba57c66 mh
    default:
150 e140adff tr
      owner => 'root',
151
      group => 'root',
152 0b7bcb5d mh
      mode  => $default_config_mode;
153 0c9bc308 hashworks
    '/etc/nftables':
154
      ensure => directory,
155 0b7bcb5d mh
      mode   => $default_config_mode;
156 30462da1 Steve Traylen
    '/etc/nftables/puppet-preflight':
157
      ensure  => directory,
158 0b7bcb5d mh
      mode    => $default_config_mode,
159 30462da1 Steve Traylen
      purge   => true,
160
      force   => true,
161
      recurse => true;
162
    '/etc/nftables/puppet-preflight.nft':
163 82d10659 Nacho Barrientos
      ensure  => file,
164 7b9d6ffc Nacho Barrientos
      content => epp('nftables/config/puppet.nft.epp', {
165
          'inet_filter' => $inet_filter,
166
          'nat'         => $nat,
167
          'noflush'     => $noflush_tables
168
        }
169
      );
170 11bf7237 Steve Traylen
  } ~> exec {
171 30462da1 Steve Traylen
    'nft validate':
172
      refreshonly => true,
173 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
174 11bf7237 Steve Traylen
  } -> file {
175 30462da1 Steve Traylen
    default:
176
      owner => 'root',
177
      group => 'root',
178 0b7bcb5d mh
      mode  => $default_config_mode;
179 0ba57c66 mh
    '/etc/nftables/puppet.nft':
180 82d10659 Nacho Barrientos
      ensure  => file,
181 7b9d6ffc Nacho Barrientos
      content => epp('nftables/config/puppet.nft.epp', {
182
          'inet_filter' => $inet_filter,
183
          'nat'         => $nat,
184
          'noflush'     => $noflush_tables
185
        }
186
      );
187 0ba57c66 mh
    '/etc/nftables/puppet':
188
      ensure  => directory,
189 0b7bcb5d mh
      mode    => $default_config_mode,
190 0ba57c66 mh
      purge   => true,
191
      force   => true,
192
      recurse => true;
193 11bf7237 Steve Traylen
  } ~> service { 'nftables':
194 30462da1 Steve Traylen
    ensure     => running,
195
    enable     => true,
196
    hasrestart => true,
197 cc9fc807 Tim Meusel
    restart    => 'PATH=/usr/bin:/bin systemctl reload nftables',
198 30462da1 Steve Traylen
  }
199
200 11bf7237 Steve Traylen
  systemd::dropin_file { 'puppet_nft.conf':
201 03d9e7da Steve Traylen
    ensure  => present,
202
    unit    => 'nftables.service',
203 0c9bc308 hashworks
    content => epp('nftables/systemd/puppet_nft.conf.epp', {
204
        'configuration_path' => $configuration_path,
205 8842a597 Tim Meusel
        'nft_path'           => $nft_path,
206 0c9bc308 hashworks
    }),
207 03d9e7da Steve Traylen
    notify  => Service['nftables'],
208 0ba57c66 mh
  }
209
210 c4b1b93b Steve Traylen
  # firewalld.enable can be mask or false depending upon if firewalld is installed or not
211
  # https://tickets.puppetlabs.com/browse/PUP-10814
212 11bf7237 Steve Traylen
  service { 'firewalld':
213 f02562f2 tr
    ensure => stopped,
214 ae9872e2 Nacho Barrientos
    enable => $firewalld_enable,
215 f02562f2 tr
  }
216
217 7b9d6ffc Nacho Barrientos
  if $inet_filter {
218
    include nftables::inet_filter
219
  }
220
221 82d10659 Nacho Barrientos
  if $nat {
222
    include nftables::ip_nat
223
  }
224 b3a7a6dd tr
225
  # inject custom rules e.g. from hiera
226 66ed7f61 mh
  $rules.each |$n,$v| {
227 11bf7237 Steve Traylen
    nftables::rule {
228 66ed7f61 mh
      $n:
229 11bf7237 Steve Traylen
        * => $v,
230 66ed7f61 mh
    }
231
  }
232 802d80d1 Nacho Barrientos
233
  # inject custom sets e.g. from hiera
234
  $sets.each |$n,$v| {
235 11bf7237 Steve Traylen
    nftables::set {
236 802d80d1 Nacho Barrientos
      $n:
237 11bf7237 Steve Traylen
        * => $v,
238 802d80d1 Nacho Barrientos
    }
239
  }
240 0ba57c66 mh
}