Projet

Général

Profil

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

root / manifests / init.pp @ 08b9f1d0

Historique | Voir | Annoter | Télécharger (6,7 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 fwd_conntrack
77
#   Adds FORWARD rules to allow traffic that's part of an
78
#   established connection and also to drop invalid packets.
79
#
80
# @param firewalld_enable
81
#   Configures how the firewalld systemd service unit is enabled. It might be
82
#   useful to set this to false if you're externaly removing firewalld from
83
#   the system completely.
84
#
85
# @param noflush_tables
86
#   If specified only other existings tables will be flushed.
87
#   If left unset all tables will be flushed via a `flush ruleset`
88
#
89
# @param rules
90
#   Specify hashes of `nftables::rule`s via hiera
91
#
92
# @param configuration_path
93
#   The absolute path to the principal nftables configuration file. The default
94
#   varies depending on the system, and is set in the module's data.
95
#
96
# @param nft_path
97
#   Path to the nft binary
98
#
99
# @param echo
100
#   Path to the echo binary
101
#
102
# @param default_config_mode
103
#   The default file & dir mode for configuration files and directories. The
104
#   default varies depending on the system, and is set in the module's data.
105
#
106
class nftables (
107
  Stdlib::Unixpath $echo,
108
  Stdlib::Unixpath $configuration_path,
109
  Stdlib::Unixpath $nft_path,
110
  Stdlib::Filemode $default_config_mode,
111
  Boolean $in_ssh = true,
112
  Boolean $in_icmp = true,
113
  Boolean $out_ntp = true,
114
  Boolean $out_dns = true,
115
  Boolean $out_http = true,
116
  Boolean $out_https = true,
117
  Boolean $out_icmp = true,
118
  Boolean $out_all = false,
119
  Boolean $in_out_conntrack = true,
120
  Boolean $fwd_conntrack = false,
121
  Boolean $inet_filter = true,
122
  Boolean $nat = true,
123
  Hash $rules = {},
124
  Hash $sets = {},
125
  String $log_prefix = '[nftables] %<chain>s %<comment>s',
126
  String[1] $nat_table_name = 'nat',
127
  Boolean $log_discarded = true,
128
  Variant[Boolean[false], String] $log_limit = '3/minute burst 5 packets',
129
  Variant[Boolean[false], Pattern[/icmp(v6|x)? type .+|tcp reset/]] $reject_with = 'icmpx type port-unreachable',
130
  Variant[Boolean[false], Enum['mask']] $firewalld_enable = 'mask',
131
  Optional[Array[Pattern[/^(ip|ip6|inet|arp|bridge|netdev)-[-a-zA-Z0-9_]+$/],1]] $noflush_tables = undef,
132
) {
133
  package { 'nftables':
134
    ensure => installed,
135
  } -> file_line {
136
    'enable_nftables':
137
      line   => 'include "/etc/nftables/puppet.nft"',
138
      path   => $configuration_path,
139
      notify => Service['nftables'],
140
  } -> file {
141
    default:
142
      owner => 'root',
143
      group => 'root',
144
      mode  => $default_config_mode;
145
    '/etc/nftables':
146
      ensure => directory,
147
      mode   => $default_config_mode;
148
    '/etc/nftables/puppet-preflight':
149
      ensure  => directory,
150
      mode    => $default_config_mode,
151
      purge   => true,
152
      force   => true,
153
      recurse => true;
154
    '/etc/nftables/puppet-preflight.nft':
155
      ensure  => file,
156
      content => epp('nftables/config/puppet.nft.epp', {
157
          'inet_filter' => $inet_filter,
158
          'nat'         => $nat,
159
          'noflush'     => $noflush_tables
160
        }
161
      );
162
  } ~> exec {
163
    'nft validate':
164
      refreshonly => true,
165
      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
166
  } -> file {
167
    default:
168
      owner => 'root',
169
      group => 'root',
170
      mode  => $default_config_mode;
171
    '/etc/nftables/puppet.nft':
172
      ensure  => file,
173
      content => epp('nftables/config/puppet.nft.epp', {
174
          'inet_filter' => $inet_filter,
175
          'nat'         => $nat,
176
          'noflush'     => $noflush_tables
177
        }
178
      );
179
    '/etc/nftables/puppet':
180
      ensure  => directory,
181
      mode    => $default_config_mode,
182
      purge   => true,
183
      force   => true,
184
      recurse => true;
185
  } ~> service { 'nftables':
186
    ensure     => running,
187
    enable     => true,
188
    hasrestart => true,
189
    restart    => 'PATH=/usr/bin:/bin systemctl reload nftables',
190
  }
191

    
192
  systemd::dropin_file { 'puppet_nft.conf':
193
    ensure  => present,
194
    unit    => 'nftables.service',
195
    content => epp('nftables/systemd/puppet_nft.conf.epp', {
196
        'configuration_path' => $configuration_path,
197
        'nft_path'           => $nft_path,
198
    }),
199
    notify  => Service['nftables'],
200
  }
201

    
202
  # firewalld.enable can be mask or false depending upon if firewalld is installed or not
203
  # https://tickets.puppetlabs.com/browse/PUP-10814
204
  service { 'firewalld':
205
    ensure => stopped,
206
    enable => $firewalld_enable,
207
  }
208

    
209
  if $inet_filter {
210
    include nftables::inet_filter
211
  }
212

    
213
  if $nat {
214
    include nftables::ip_nat
215
  }
216

    
217
  # inject custom rules e.g. from hiera
218
  $rules.each |$n,$v| {
219
    nftables::rule {
220
      $n:
221
        * => $v,
222
    }
223
  }
224

    
225
  # inject custom sets e.g. from hiera
226
  $sets.each |$n,$v| {
227
    nftables::set {
228
      $n:
229
        * => $v,
230
    }
231
  }
232
}