root / manifests / init.pp @ 8842a597
Historique | Voir | Annoter | Télécharger (6,18 ko)
1 | e17693e3 | Steve Traylen | # @summary Configure nftables |
---|---|---|---|
2 | # |
||
3 | 03d9e7da | Steve Traylen | # @example allow dns out and do not allow ntp out |
4 | 2063deaf | hashworks | # class{ 'nftables': |
5 | # out_ntp => false, |
||
6 | # out_dns => true, |
||
7 | b3a7a6dd | tr | # } |
8 | e17693e3 | Steve Traylen | # |
9 | b9785000 | Steve Traylen | # @example do not flush particular tables, fail2ban in this case |
10 | 2063deaf | hashworks | # class{ 'nftables': |
11 | # noflush_tables => ['inet-f2b-table'], |
||
12 | 03d9e7da | Steve Traylen | # } |
13 | # |
||
14 | b3a7a6dd | tr | # @param out_all |
15 | e17693e3 | Steve Traylen | # 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 | 09cba182 | Steve Traylen | # @param out_dns |
26 | # Allow outbound to dns servers. |
||
27 | e17693e3 | Steve Traylen | # |
28 | # @param out_https |
||
29 | # Allow outbound to https servers. |
||
30 | # |
||
31 | 79e9a23f | Nacho Barrientos | # @param out_icmp |
32 | # Allow outbound ICMPv4/v6 traffic. |
||
33 | # |
||
34 | e17693e3 | Steve Traylen | # @param in_ssh |
35 | # Allow inbound to ssh servers. |
||
36 | # |
||
37 | 79e9a23f | Nacho Barrientos | # @param in_icmp |
38 | # Allow inbound ICMPv4/v6 traffic. |
||
39 | # |
||
40 | 7b9d6ffc | Nacho Barrientos | # @param inet_filter |
41 | # Add default tables, chains and rules to process traffic. |
||
42 | # |
||
43 | 82d10659 | Nacho Barrientos | # @param nat |
44 | # Add default tables and chains to process NAT traffic. |
||
45 | # |
||
46 | fcb79d73 | Ben Morrice | # @param nat_table_name |
47 | # The name of the 'nat' table. |
||
48 | # |
||
49 | 802d80d1 | Nacho Barrientos | # @param sets |
50 | # Allows sourcing set definitions directly from Hiera. |
||
51 | # |
||
52 | ac0af4aa | Nacho Barrientos | # @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 | b10c6216 | Nacho Barrientos | # @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 | 70727742 | Nacho Barrientos | # @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 | ea96d5db | Nacho Barrientos | # @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 | 24a5a2a7 | tr | # @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 | ae9872e2 | Nacho Barrientos | # @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 | 03d9e7da | Steve Traylen | # @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 | 09cba182 | Steve Traylen | # @param rules |
87 | # Specify hashes of `nftables::rule`s via hiera |
||
88 | # |
||
89 | 0c9bc308 | hashworks | # @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 | 8842a597 | Tim Meusel | # @param nft_path |
94 | # Path to the nft binary |
||
95 | # |
||
96 | be0b08e1 | tr | class nftables ( |
97 | 31b17627 | Steve Traylen | Boolean $in_ssh = true, |
98 | Boolean $in_icmp = true, |
||
99 | Boolean $out_ntp = true, |
||
100 | Boolean $out_dns = true, |
||
101 | Boolean $out_http = true, |
||
102 | Boolean $out_https = true, |
||
103 | Boolean $out_icmp = true, |
||
104 | Boolean $out_all = false, |
||
105 | Boolean $in_out_conntrack = true, |
||
106 | Boolean $fwd_conntrack = false, |
||
107 | 7b9d6ffc | Nacho Barrientos | Boolean $inet_filter = true, |
108 | 31b17627 | Steve Traylen | Boolean $nat = true, |
109 | Hash $rules = {}, |
||
110 | Hash $sets = {}, |
||
111 | String $log_prefix = '[nftables] %<chain>s %<comment>s', |
||
112 | fcb79d73 | Ben Morrice | String[1] $nat_table_name = 'nat', |
113 | 31b17627 | Steve Traylen | Variant[Boolean[false], String] $log_limit = '3/minute burst 5 packets', |
114 | Variant[Boolean[false], Pattern[/icmp(v6|x)? type .+|tcp reset/]] $reject_with = 'icmpx type port-unreachable', |
||
115 | Variant[Boolean[false], Enum['mask']] $firewalld_enable = 'mask', |
||
116 | Optional[Array[Pattern[/^(ip|ip6|inet)-[-a-zA-Z0-9_]+$/],1]] $noflush_tables = undef, |
||
117 | 0c9bc308 | hashworks | Stdlib::Unixpath $configuration_path, |
118 | 8842a597 | Tim Meusel | Stdlib::Unixpath $nft_path, |
119 | be0b08e1 | tr | ) { |
120 | 11bf7237 | Steve Traylen | package { 'nftables': |
121 | 0ba57c66 | mh | ensure => installed, |
122 | 11bf7237 | Steve Traylen | } -> file_line { |
123 | 0ba57c66 | mh | 'enable_nftables': |
124 | line => 'include "/etc/nftables/puppet.nft"', |
||
125 | 0c9bc308 | hashworks | path => $configuration_path, |
126 | 0ba57c66 | mh | notify => Service['nftables'], |
127 | 11bf7237 | Steve Traylen | } -> file { |
128 | 0ba57c66 | mh | default: |
129 | e140adff | tr | owner => 'root', |
130 | group => 'root', |
||
131 | mode => '0640'; |
||
132 | 0c9bc308 | hashworks | '/etc/nftables': |
133 | ensure => directory, |
||
134 | mode => '0750'; |
||
135 | 30462da1 | Steve Traylen | '/etc/nftables/puppet-preflight': |
136 | ensure => directory, |
||
137 | mode => '0750', |
||
138 | purge => true, |
||
139 | force => true, |
||
140 | recurse => true; |
||
141 | '/etc/nftables/puppet-preflight.nft': |
||
142 | 82d10659 | Nacho Barrientos | ensure => file, |
143 | 7b9d6ffc | Nacho Barrientos | content => epp('nftables/config/puppet.nft.epp', { |
144 | 'inet_filter' => $inet_filter, |
||
145 | 'nat' => $nat, |
||
146 | 'noflush' => $noflush_tables |
||
147 | } |
||
148 | ); |
||
149 | 11bf7237 | Steve Traylen | } ~> exec { |
150 | 30462da1 | Steve Traylen | 'nft validate': |
151 | refreshonly => true, |
||
152 | 8842a597 | Tim Meusel | command => "${nft_path} -I /etc/nftables/puppet-preflight -c -f /etc/nftables/puppet-preflight.nft || ( /usr/bin/echo '#CONFIG BROKEN' >> /etc/nftables/puppet-preflight.nft && /bin/false)"; |
153 | 11bf7237 | Steve Traylen | } -> file { |
154 | 30462da1 | Steve Traylen | default: |
155 | owner => 'root', |
||
156 | group => 'root', |
||
157 | mode => '0640'; |
||
158 | 0ba57c66 | mh | '/etc/nftables/puppet.nft': |
159 | 82d10659 | Nacho Barrientos | ensure => file, |
160 | 7b9d6ffc | Nacho Barrientos | content => epp('nftables/config/puppet.nft.epp', { |
161 | 'inet_filter' => $inet_filter, |
||
162 | 'nat' => $nat, |
||
163 | 'noflush' => $noflush_tables |
||
164 | } |
||
165 | ); |
||
166 | 0ba57c66 | mh | '/etc/nftables/puppet': |
167 | ensure => directory, |
||
168 | 5acb554a | tr | mode => '0750', |
169 | 0ba57c66 | mh | purge => true, |
170 | force => true, |
||
171 | recurse => true; |
||
172 | 11bf7237 | Steve Traylen | } ~> service { 'nftables': |
173 | 30462da1 | Steve Traylen | ensure => running, |
174 | enable => true, |
||
175 | hasrestart => true, |
||
176 | restart => '/usr/bin/systemctl reload nftables', |
||
177 | } |
||
178 | |||
179 | 11bf7237 | Steve Traylen | systemd::dropin_file { 'puppet_nft.conf': |
180 | 03d9e7da | Steve Traylen | ensure => present, |
181 | unit => 'nftables.service', |
||
182 | 0c9bc308 | hashworks | content => epp('nftables/systemd/puppet_nft.conf.epp', { |
183 | 'configuration_path' => $configuration_path, |
||
184 | 8842a597 | Tim Meusel | 'nft_path' => $nft_path, |
185 | 0c9bc308 | hashworks | }), |
186 | 03d9e7da | Steve Traylen | notify => Service['nftables'], |
187 | 0ba57c66 | mh | } |
188 | |||
189 | c4b1b93b | Steve Traylen | # firewalld.enable can be mask or false depending upon if firewalld is installed or not |
190 | # https://tickets.puppetlabs.com/browse/PUP-10814 |
||
191 | 11bf7237 | Steve Traylen | service { 'firewalld': |
192 | f02562f2 | tr | ensure => stopped, |
193 | ae9872e2 | Nacho Barrientos | enable => $firewalld_enable, |
194 | f02562f2 | tr | } |
195 | |||
196 | 7b9d6ffc | Nacho Barrientos | if $inet_filter { |
197 | include nftables::inet_filter |
||
198 | } |
||
199 | |||
200 | 82d10659 | Nacho Barrientos | if $nat { |
201 | include nftables::ip_nat |
||
202 | } |
||
203 | b3a7a6dd | tr | |
204 | # inject custom rules e.g. from hiera |
||
205 | 66ed7f61 | mh | $rules.each |$n,$v| { |
206 | 11bf7237 | Steve Traylen | nftables::rule { |
207 | 66ed7f61 | mh | $n: |
208 | 11bf7237 | Steve Traylen | * => $v, |
209 | 66ed7f61 | mh | } |
210 | } |
||
211 | 802d80d1 | Nacho Barrientos | |
212 | # inject custom sets e.g. from hiera |
||
213 | $sets.each |$n,$v| { |
||
214 | 11bf7237 | Steve Traylen | nftables::set { |
215 | 802d80d1 | Nacho Barrientos | $n: |
216 | 11bf7237 | Steve Traylen | * => $v, |
217 | 802d80d1 | Nacho Barrientos | } |
218 | } |
||
219 | 0ba57c66 | mh | } |