root / manifests / init.pp @ 7b9d6ffc
Historique | Voir | Annoter | Télécharger (5,63 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 sets |
47 |
# Allows sourcing set definitions directly from Hiera. |
48 |
# |
49 |
# @param log_prefix |
50 |
# String that will be used as prefix when logging packets. It can contain |
51 |
# two variables using standard sprintf() string-formatting: |
52 |
# * chain: Will be replaced by the name of the chain. |
53 |
# * comment: Allows chains to add extra comments. |
54 |
# |
55 |
# @param log_limit |
56 |
# String with the content of a limit statement to be applied |
57 |
# to the rules that log discarded traffic. Set to false to |
58 |
# disable rate limiting. |
59 |
# |
60 |
# @param reject_with |
61 |
# How to discard packets not matching any rule. If `false`, the |
62 |
# fate of the packet will be defined by the chain policy (normally |
63 |
# drop), otherwise the packet will be rejected with the REJECT_WITH |
64 |
# policy indicated by the value of this parameter. |
65 |
# |
66 |
# @param in_out_conntrack |
67 |
# Adds INPUT and OUTPUT rules to allow traffic that's part of an |
68 |
# established connection and also to drop invalid packets. |
69 |
# |
70 |
# @param fwd_conntrack |
71 |
# Adds FORWARD rules to allow traffic that's part of an |
72 |
# established connection and also to drop invalid packets. |
73 |
# |
74 |
# @param firewalld_enable |
75 |
# Configures how the firewalld systemd service unit is enabled. It might be |
76 |
# useful to set this to false if you're externaly removing firewalld from |
77 |
# the system completely. |
78 |
# |
79 |
# @param noflush_tables |
80 |
# If specified only other existings tables will be flushed. |
81 |
# If left unset all tables will be flushed via a `flush ruleset` |
82 |
# |
83 |
# @param rules |
84 |
# Specify hashes of `nftables::rule`s via hiera |
85 |
# |
86 |
class nftables ( |
87 |
Boolean $in_ssh = true, |
88 |
Boolean $in_icmp = true, |
89 |
Boolean $out_ntp = true, |
90 |
Boolean $out_dns = true, |
91 |
Boolean $out_http = true, |
92 |
Boolean $out_https = true, |
93 |
Boolean $out_icmp = true, |
94 |
Boolean $out_all = false, |
95 |
Boolean $in_out_conntrack = true, |
96 |
Boolean $fwd_conntrack = false, |
97 |
Boolean $inet_filter = true, |
98 |
Boolean $nat = true, |
99 |
Hash $rules = {}, |
100 |
Hash $sets = {}, |
101 |
String $log_prefix = '[nftables] %<chain>s %<comment>s', |
102 |
Variant[Boolean[false], String] $log_limit = '3/minute burst 5 packets', |
103 |
Variant[Boolean[false], Pattern[/icmp(v6|x)? type .+|tcp reset/]] $reject_with = 'icmpx type port-unreachable', |
104 |
Variant[Boolean[false], Enum['mask']] $firewalld_enable = 'mask', |
105 |
Optional[Array[Pattern[/^(ip|ip6|inet)-[-a-zA-Z0-9_]+$/],1]] $noflush_tables = undef, |
106 |
) { |
107 |
package { 'nftables': |
108 |
ensure => installed, |
109 |
} -> file_line { |
110 |
'enable_nftables': |
111 |
line => 'include "/etc/nftables/puppet.nft"', |
112 |
path => '/etc/sysconfig/nftables.conf', |
113 |
notify => Service['nftables'], |
114 |
} -> file { |
115 |
default: |
116 |
owner => 'root', |
117 |
group => 'root', |
118 |
mode => '0640'; |
119 |
'/etc/nftables/puppet-preflight': |
120 |
ensure => directory, |
121 |
mode => '0750', |
122 |
purge => true, |
123 |
force => true, |
124 |
recurse => true; |
125 |
'/etc/nftables/puppet-preflight.nft': |
126 |
ensure => file, |
127 |
content => epp('nftables/config/puppet.nft.epp', { |
128 |
'inet_filter' => $inet_filter, |
129 |
'nat' => $nat, |
130 |
'noflush' => $noflush_tables |
131 |
} |
132 |
); |
133 |
} ~> exec { |
134 |
'nft validate': |
135 |
refreshonly => true, |
136 |
command => '/usr/sbin/nft -I /etc/nftables/puppet-preflight -c -f /etc/nftables/puppet-preflight.nft || ( /usr/bin/echo "#CONFIG BROKEN" >> /etc/nftables/puppet-preflight.nft && /bin/false)'; |
137 |
} -> file { |
138 |
default: |
139 |
owner => 'root', |
140 |
group => 'root', |
141 |
mode => '0640'; |
142 |
'/etc/nftables/puppet.nft': |
143 |
ensure => file, |
144 |
content => epp('nftables/config/puppet.nft.epp', { |
145 |
'inet_filter' => $inet_filter, |
146 |
'nat' => $nat, |
147 |
'noflush' => $noflush_tables |
148 |
} |
149 |
); |
150 |
'/etc/nftables/puppet': |
151 |
ensure => directory, |
152 |
mode => '0750', |
153 |
purge => true, |
154 |
force => true, |
155 |
recurse => true; |
156 |
} ~> service { 'nftables': |
157 |
ensure => running, |
158 |
enable => true, |
159 |
hasrestart => true, |
160 |
restart => '/usr/bin/systemctl reload nftables', |
161 |
} |
162 |
|
163 |
systemd::dropin_file { 'puppet_nft.conf': |
164 |
ensure => present, |
165 |
unit => 'nftables.service', |
166 |
content => file('nftables/systemd/puppet_nft.conf'), |
167 |
notify => Service['nftables'], |
168 |
} |
169 |
|
170 |
# firewalld.enable can be mask or false depending upon if firewalld is installed or not |
171 |
# https://tickets.puppetlabs.com/browse/PUP-10814 |
172 |
service { 'firewalld': |
173 |
ensure => stopped, |
174 |
enable => $firewalld_enable, |
175 |
} |
176 |
|
177 |
if $inet_filter { |
178 |
include nftables::inet_filter |
179 |
} |
180 |
|
181 |
if $nat { |
182 |
include nftables::ip_nat |
183 |
} |
184 |
|
185 |
# inject custom rules e.g. from hiera |
186 |
$rules.each |$n,$v| { |
187 |
nftables::rule { |
188 |
$n: |
189 |
* => $v, |
190 |
} |
191 |
} |
192 |
|
193 |
# inject custom sets e.g. from hiera |
194 |
$sets.each |$n,$v| { |
195 |
nftables::set { |
196 |
$n: |
197 |
* => $v, |
198 |
} |
199 |
} |
200 |
} |