Projet

Général

Profil

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

root / manifests / init.pp @ f24e622f

Historique | Voir | Annoter | Télécharger (7,66 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 sets
50
#   Allows sourcing set definitions directly from Hiera.
51
#
52
# @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
# @param log_discarded
59
#   Allow to log discarded packets
60
#
61
# @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
# @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
# @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
# @param in_out_drop_invalid
77
#   Drops invalid packets in INPUT and OUTPUT
78
#
79
# @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
# @param fwd_drop_invalid
84
#   Drops invalid packets in FORWARD
85
#
86
# @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
# @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
# @param rules
96
#   Specify hashes of `nftables::rule`s via hiera
97
#
98
# @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
# @param nft_path
103
#   Path to the nft binary
104
#
105
# @param echo
106
#   Path to the echo binary
107
#
108
# @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
# @param clobber_default_config
113
#   Should the existing OS provided rules in the `configuration_path` be removed? If
114
#   they are not being removed this module will add all of its configuration to the end of
115
#   the existing rules.
116
#
117
class nftables (
118
  Stdlib::Unixpath $echo,
119
  Stdlib::Unixpath $configuration_path,
120
  Stdlib::Unixpath $nft_path,
121
  Stdlib::Filemode $default_config_mode,
122
  Boolean $clobber_default_config = false,
123
  Boolean $in_ssh = true,
124
  Boolean $in_icmp = true,
125
  Boolean $out_ntp = true,
126
  Boolean $out_dns = true,
127
  Boolean $out_http = true,
128
  Boolean $out_https = true,
129
  Boolean $out_icmp = true,
130
  Boolean $out_all = false,
131
  Boolean $in_out_conntrack = true,
132
  Boolean $in_out_drop_invalid = $in_out_conntrack,
133
  Boolean $fwd_conntrack = false,
134
  Boolean $fwd_drop_invalid = $fwd_conntrack,
135
  Boolean $inet_filter = true,
136
  Boolean $nat = true,
137
  Hash $rules = {},
138
  Hash $sets = {},
139
  String $log_prefix = '[nftables] %<chain>s %<comment>s',
140
  String[1] $nat_table_name = 'nat',
141
  Boolean $log_discarded = true,
142
  Variant[Boolean[false], String] $log_limit = '3/minute burst 5 packets',
143
  Variant[Boolean[false], Pattern[/icmp(v6|x)? type .+|tcp reset/]] $reject_with = 'icmpx type port-unreachable',
144
  Variant[Boolean[false], Enum['mask']] $firewalld_enable = 'mask',
145
  Optional[Array[Pattern[/^(ip|ip6|inet|arp|bridge|netdev)-[-a-zA-Z0-9_]+$/],1]] $noflush_tables = undef,
146
) {
147
  package { 'nftables':
148
    ensure => installed,
149
  }
150

    
151
  if $clobber_default_config {
152
    file { $configuration_path:
153
      ensure  => file,
154
      owner   => 'root',
155
      group   => 'root',
156
      mode    => $default_config_mode,
157
      content => "#Puppet Managed\ninclude \"/etc/nftables/puppet.nft\"\n",
158
      require => Package['nftables'],
159
      before  => File['/etc/nftables'],
160
      notify  => Service['nftables'],
161
    }
162
  } else {
163
    file_line { 'enable_nftables':
164
      line    => 'include "/etc/nftables/puppet.nft"',
165
      path    => $configuration_path,
166
      require => Package['nftables'],
167
      before  => File['/etc/nftables'],
168
      notify  => Service['nftables'],
169
    }
170
  }
171

    
172
  file {
173
    default:
174
      owner => 'root',
175
      group => 'root',
176
      mode  => $default_config_mode;
177
    '/etc/nftables':
178
      ensure => directory,
179
      mode   => $default_config_mode;
180
    '/etc/nftables/puppet-preflight':
181
      ensure  => directory,
182
      mode    => $default_config_mode,
183
      purge   => true,
184
      force   => true,
185
      recurse => true;
186
    '/etc/nftables/puppet-preflight.nft':
187
      ensure  => file,
188
      content => epp('nftables/config/puppet.nft.epp', {
189
          'inet_filter' => $inet_filter,
190
          'nat'         => $nat,
191
          'noflush'     => $noflush_tables
192
        }
193
      );
194
  } ~> exec {
195
    'nft validate':
196
      refreshonly => true,
197
      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
198
  } -> file {
199
    default:
200
      owner => 'root',
201
      group => 'root',
202
      mode  => $default_config_mode;
203
    '/etc/nftables/puppet.nft':
204
      ensure  => file,
205
      content => epp('nftables/config/puppet.nft.epp', {
206
          'inet_filter' => $inet_filter,
207
          'nat'         => $nat,
208
          'noflush'     => $noflush_tables
209
        }
210
      );
211
    '/etc/nftables/puppet':
212
      ensure  => directory,
213
      mode    => $default_config_mode,
214
      purge   => true,
215
      force   => true,
216
      recurse => true;
217
  } ~> service { 'nftables':
218
    ensure     => running,
219
    enable     => true,
220
    hasrestart => true,
221
    restart    => 'PATH=/usr/bin:/bin systemctl reload nftables',
222
  }
223

    
224
  systemd::dropin_file { 'puppet_nft.conf':
225
    ensure  => present,
226
    unit    => 'nftables.service',
227
    content => epp('nftables/systemd/puppet_nft.conf.epp', {
228
        'configuration_path' => $configuration_path,
229
        'nft_path'           => $nft_path,
230
    }),
231
    notify  => Service['nftables'],
232
  }
233

    
234
  # firewalld.enable can be mask or false depending upon if firewalld is installed or not
235
  # https://tickets.puppetlabs.com/browse/PUP-10814
236
  service { 'firewalld':
237
    ensure => stopped,
238
    enable => $firewalld_enable,
239
  }
240

    
241
  if $inet_filter {
242
    include nftables::inet_filter
243
  }
244

    
245
  if $nat {
246
    include nftables::ip_nat
247
  }
248

    
249
  # inject custom rules e.g. from hiera
250
  $rules.each |$n,$v| {
251
    nftables::rule {
252
      $n:
253
        * => $v,
254
    }
255
  }
256

    
257
  # inject custom sets e.g. from hiera
258
  $sets.each |$n,$v| {
259
    nftables::set {
260
      $n:
261
        * => $v,
262
    }
263
  }
264
}