root / manifests / init.pp @ 03d9e7da
Historique | Voir | Annoter | Télécharger (5,25 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 |
10 |
# In this case ignoring the fail2ban maintained |
11 |
# table |
12 |
# class{'nftables': |
13 |
# noflush_tables = ['inet-f2b-table'], |
14 |
# } |
15 |
# |
16 |
# @param out_all |
17 |
# Allow all outbound connections. If `true` then all other |
18 |
# out parameters `out_ntp`, `out_dns`, ... will be assuemed |
19 |
# false. |
20 |
# |
21 |
# @param out_ntp |
22 |
# Allow outbound to ntp servers. |
23 |
# |
24 |
# @param out_http |
25 |
# Allow outbound to http servers. |
26 |
# |
27 |
# @param out_https |
28 |
# Allow outbound to https servers. |
29 |
# |
30 |
# @param out_https |
31 |
# Allow outbound to https servers. |
32 |
# |
33 |
# @param out_icmp |
34 |
# Allow outbound ICMPv4/v6 traffic. |
35 |
# |
36 |
# @param in_ssh |
37 |
# Allow inbound to ssh servers. |
38 |
# |
39 |
# @param in_icmp |
40 |
# Allow inbound ICMPv4/v6 traffic. |
41 |
# |
42 |
# @param nat |
43 |
# Add default tables and chains to process NAT traffic. |
44 |
# |
45 |
# @param sets |
46 |
# Allows sourcing set definitions directly from Hiera. |
47 |
# |
48 |
# @param log_prefix |
49 |
# String that will be used as prefix when logging packets. It can contain |
50 |
# two variables using standard sprintf() string-formatting: |
51 |
# * chain: Will be replaced by the name of the chain. |
52 |
# * comment: Allows chains to add extra comments. |
53 |
# |
54 |
# @param log_limit |
55 |
# String with the content of a limit statement to be applied |
56 |
# to the rules that log discarded traffic. Set to false to |
57 |
# disable rate limiting. |
58 |
# |
59 |
# @param reject_with |
60 |
# How to discard packets not matching any rule. If `false`, the |
61 |
# fate of the packet will be defined by the chain policy (normally |
62 |
# drop), otherwise the packet will be rejected with the REJECT_WITH |
63 |
# policy indicated by the value of this parameter. |
64 |
# |
65 |
# @param in_out_conntrack |
66 |
# Adds INPUT and OUTPUT rules to allow traffic that's part of an |
67 |
# established connection and also to drop invalid packets. |
68 |
# |
69 |
# @param firewalld_enable |
70 |
# Configures how the firewalld systemd service unit is enabled. It might be |
71 |
# useful to set this to false if you're externaly removing firewalld from |
72 |
# the system completely. |
73 |
# |
74 |
# @param noflush_tables |
75 |
# If specified only other existings tables will be flushed. |
76 |
# If left unset all tables will be flushed via a `flush ruleset` |
77 |
# |
78 |
class nftables ( |
79 |
Boolean $in_ssh = true, |
80 |
Boolean $in_icmp = true, |
81 |
Boolean $out_ntp = true, |
82 |
Boolean $out_dns = true, |
83 |
Boolean $out_http = true, |
84 |
Boolean $out_https = true, |
85 |
Boolean $out_icmp = true, |
86 |
Boolean $out_all = false, |
87 |
Boolean $in_out_conntrack = true, |
88 |
Boolean $nat = true, |
89 |
Hash $rules = {}, |
90 |
Hash $sets = {}, |
91 |
String $log_prefix = '[nftables] %<chain>s %<comment>s', |
92 |
Variant[Boolean[false], String] |
93 |
$log_limit = '3/minute burst 5 packets', |
94 |
Variant[Boolean[false], Pattern[ |
95 |
/icmp(v6|x)? type .+|tcp reset/]] |
96 |
$reject_with = 'icmpx type port-unreachable', |
97 |
Variant[Boolean[false], Enum['mask']] |
98 |
$firewalld_enable = 'mask', |
99 |
Optional[Array[Pattern[/^(ip|ip6|inet)-[-a-zA-Z0-9_]+$/],1]] |
100 |
$noflush_tables = undef, |
101 |
) { |
102 |
|
103 |
package{'nftables': |
104 |
ensure => installed, |
105 |
} -> file_line{ |
106 |
'enable_nftables': |
107 |
line => 'include "/etc/nftables/puppet.nft"', |
108 |
path => '/etc/sysconfig/nftables.conf', |
109 |
notify => Service['nftables'], |
110 |
} -> file{ |
111 |
default: |
112 |
owner => 'root', |
113 |
group => 'root', |
114 |
mode => '0640'; |
115 |
'/etc/nftables/puppet-preflight': |
116 |
ensure => directory, |
117 |
mode => '0750', |
118 |
purge => true, |
119 |
force => true, |
120 |
recurse => true; |
121 |
'/etc/nftables/puppet-preflight.nft': |
122 |
ensure => file, |
123 |
content => epp('nftables/config/puppet.nft.epp', { 'nat' => $nat, 'noflush' => $noflush_tables }); |
124 |
} ~> exec{ |
125 |
'nft validate': |
126 |
refreshonly => true, |
127 |
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)'; |
128 |
} -> file{ |
129 |
default: |
130 |
owner => 'root', |
131 |
group => 'root', |
132 |
mode => '0640'; |
133 |
'/etc/nftables/puppet.nft': |
134 |
ensure => file, |
135 |
content => epp('nftables/config/puppet.nft.epp', { 'nat' => $nat, 'noflush' => $noflush_tables }); |
136 |
'/etc/nftables/puppet': |
137 |
ensure => directory, |
138 |
mode => '0750', |
139 |
purge => true, |
140 |
force => true, |
141 |
recurse => true; |
142 |
} ~> service{'nftables': |
143 |
ensure => running, |
144 |
enable => true, |
145 |
hasrestart => true, |
146 |
restart => '/usr/bin/systemctl reload nftables', |
147 |
} |
148 |
|
149 |
systemd::dropin_file{'puppet_nft.conf': |
150 |
ensure => present, |
151 |
unit => 'nftables.service', |
152 |
content => epp('nftables/systemd/puppet_nft.conf.epp', { 'noflush' => $noflush_tables }), |
153 |
notify => Service['nftables'], |
154 |
} |
155 |
|
156 |
service{'firewalld': |
157 |
ensure => stopped, |
158 |
enable => $firewalld_enable, |
159 |
} |
160 |
|
161 |
include nftables::inet_filter |
162 |
if $nat { |
163 |
include nftables::ip_nat |
164 |
} |
165 |
|
166 |
# inject custom rules e.g. from hiera |
167 |
$rules.each |$n,$v| { |
168 |
nftables::rule{ |
169 |
$n: |
170 |
* => $v |
171 |
} |
172 |
} |
173 |
|
174 |
# inject custom sets e.g. from hiera |
175 |
$sets.each |$n,$v| { |
176 |
nftables::set{ |
177 |
$n: |
178 |
* => $v |
179 |
} |
180 |
} |
181 |
} |