root / manifests / init.pp @ ffc8b86f
Historique | Voir | Annoter | Télécharger (6,6 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_limit |
59 |
# String with the content of a limit statement to be applied |
60 |
# to the rules that log discarded traffic. Set to false to |
61 |
# disable rate limiting. |
62 |
# |
63 |
# @param reject_with |
64 |
# How to discard packets not matching any rule. If `false`, the |
65 |
# fate of the packet will be defined by the chain policy (normally |
66 |
# drop), otherwise the packet will be rejected with the REJECT_WITH |
67 |
# policy indicated by the value of this parameter. |
68 |
# |
69 |
# @param in_out_conntrack |
70 |
# Adds INPUT and OUTPUT rules to allow traffic that's part of an |
71 |
# established connection and also to drop invalid packets. |
72 |
# |
73 |
# @param fwd_conntrack |
74 |
# Adds FORWARD rules to allow traffic that's part of an |
75 |
# established connection and also to drop invalid packets. |
76 |
# |
77 |
# @param firewalld_enable |
78 |
# Configures how the firewalld systemd service unit is enabled. It might be |
79 |
# useful to set this to false if you're externaly removing firewalld from |
80 |
# the system completely. |
81 |
# |
82 |
# @param noflush_tables |
83 |
# If specified only other existings tables will be flushed. |
84 |
# If left unset all tables will be flushed via a `flush ruleset` |
85 |
# |
86 |
# @param rules |
87 |
# Specify hashes of `nftables::rule`s via hiera |
88 |
# |
89 |
# @param configuration_path |
90 |
# The absolute path to the principal nftables configuration file. The default |
91 |
# varies depending on the system, and is set in the module's data. |
92 |
# |
93 |
# @param nft_path |
94 |
# Path to the nft binary |
95 |
# |
96 |
# @param echo |
97 |
# Path to the echo binary |
98 |
# |
99 |
# @param default_config_mode |
100 |
# The default file & dir mode for configuration files and directories. The |
101 |
# default varies depending on the system, and is set in the module's data. |
102 |
# |
103 |
class nftables ( |
104 |
Stdlib::Unixpath $echo, |
105 |
Stdlib::Unixpath $configuration_path, |
106 |
Stdlib::Unixpath $nft_path, |
107 |
Stdlib::Filemode $default_config_mode, |
108 |
Boolean $in_ssh = true, |
109 |
Boolean $in_icmp = true, |
110 |
Boolean $out_ntp = true, |
111 |
Boolean $out_dns = true, |
112 |
Boolean $out_http = true, |
113 |
Boolean $out_https = true, |
114 |
Boolean $out_icmp = true, |
115 |
Boolean $out_all = false, |
116 |
Boolean $in_out_conntrack = true, |
117 |
Boolean $fwd_conntrack = false, |
118 |
Boolean $inet_filter = true, |
119 |
Boolean $nat = true, |
120 |
Hash $rules = {}, |
121 |
Hash $sets = {}, |
122 |
String $log_prefix = '[nftables] %<chain>s %<comment>s', |
123 |
String[1] $nat_table_name = 'nat', |
124 |
Variant[Boolean[false], String] $log_limit = '3/minute burst 5 packets', |
125 |
Variant[Boolean[false], Pattern[/icmp(v6|x)? type .+|tcp reset/]] $reject_with = 'icmpx type port-unreachable', |
126 |
Variant[Boolean[false], Enum['mask']] $firewalld_enable = 'mask', |
127 |
Optional[Array[Pattern[/^(ip|ip6|inet|arp|bridge|netdev)-[-a-zA-Z0-9_]+$/],1]] $noflush_tables = undef, |
128 |
) { |
129 |
package { 'nftables': |
130 |
ensure => installed, |
131 |
} -> file_line { |
132 |
'enable_nftables': |
133 |
line => 'include "/etc/nftables/puppet.nft"', |
134 |
path => $configuration_path, |
135 |
notify => Service['nftables'], |
136 |
} -> file { |
137 |
default: |
138 |
owner => 'root', |
139 |
group => 'root', |
140 |
mode => $default_config_mode; |
141 |
'/etc/nftables': |
142 |
ensure => directory, |
143 |
mode => $default_config_mode; |
144 |
'/etc/nftables/puppet-preflight': |
145 |
ensure => directory, |
146 |
mode => $default_config_mode, |
147 |
purge => true, |
148 |
force => true, |
149 |
recurse => true; |
150 |
'/etc/nftables/puppet-preflight.nft': |
151 |
ensure => file, |
152 |
content => epp('nftables/config/puppet.nft.epp', { |
153 |
'inet_filter' => $inet_filter, |
154 |
'nat' => $nat, |
155 |
'noflush' => $noflush_tables |
156 |
} |
157 |
); |
158 |
} ~> exec { |
159 |
'nft validate': |
160 |
refreshonly => true, |
161 |
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 |
162 |
} -> file { |
163 |
default: |
164 |
owner => 'root', |
165 |
group => 'root', |
166 |
mode => $default_config_mode; |
167 |
'/etc/nftables/puppet.nft': |
168 |
ensure => file, |
169 |
content => epp('nftables/config/puppet.nft.epp', { |
170 |
'inet_filter' => $inet_filter, |
171 |
'nat' => $nat, |
172 |
'noflush' => $noflush_tables |
173 |
} |
174 |
); |
175 |
'/etc/nftables/puppet': |
176 |
ensure => directory, |
177 |
mode => $default_config_mode, |
178 |
purge => true, |
179 |
force => true, |
180 |
recurse => true; |
181 |
} ~> service { 'nftables': |
182 |
ensure => running, |
183 |
enable => true, |
184 |
hasrestart => true, |
185 |
restart => 'PATH=/usr/bin:/bin systemctl reload nftables', |
186 |
} |
187 |
|
188 |
systemd::dropin_file { 'puppet_nft.conf': |
189 |
ensure => present, |
190 |
unit => 'nftables.service', |
191 |
content => epp('nftables/systemd/puppet_nft.conf.epp', { |
192 |
'configuration_path' => $configuration_path, |
193 |
'nft_path' => $nft_path, |
194 |
}), |
195 |
notify => Service['nftables'], |
196 |
} |
197 |
|
198 |
# firewalld.enable can be mask or false depending upon if firewalld is installed or not |
199 |
# https://tickets.puppetlabs.com/browse/PUP-10814 |
200 |
service { 'firewalld': |
201 |
ensure => stopped, |
202 |
enable => $firewalld_enable, |
203 |
} |
204 |
|
205 |
if $inet_filter { |
206 |
include nftables::inet_filter |
207 |
} |
208 |
|
209 |
if $nat { |
210 |
include nftables::ip_nat |
211 |
} |
212 |
|
213 |
# inject custom rules e.g. from hiera |
214 |
$rules.each |$n,$v| { |
215 |
nftables::rule { |
216 |
$n: |
217 |
* => $v, |
218 |
} |
219 |
} |
220 |
|
221 |
# inject custom sets e.g. from hiera |
222 |
$sets.each |$n,$v| { |
223 |
nftables::set { |
224 |
$n: |
225 |
* => $v, |
226 |
} |
227 |
} |
228 |
} |