root / manifests / init.pp @ 7dcae593
Historique | Voir | Annoter | Télécharger (5,88 ko)
1 | e17693e3 | Steve Traylen | # @summary Configure nftables |
---|---|---|---|
2 | # |
||
3 | 03d9e7da | Steve Traylen | # @example allow dns out and do not allow ntp out |
4 | e17693e3 | Steve Traylen | # 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 | 03d9e7da | Steve Traylen | # class{'nftables': |
11 | # noflush_tables = ['inet-f2b-table'], |
||
12 | # } |
||
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 | 82d10659 | Nacho Barrientos | # @param nat |
41 | # Add default tables and chains to process NAT traffic. |
||
42 | # |
||
43 | 802d80d1 | Nacho Barrientos | # @param sets |
44 | # Allows sourcing set definitions directly from Hiera. |
||
45 | # |
||
46 | ac0af4aa | Nacho Barrientos | # @param log_prefix |
47 | # String that will be used as prefix when logging packets. It can contain |
||
48 | # two variables using standard sprintf() string-formatting: |
||
49 | # * chain: Will be replaced by the name of the chain. |
||
50 | # * comment: Allows chains to add extra comments. |
||
51 | # |
||
52 | b10c6216 | Nacho Barrientos | # @param log_limit |
53 | # String with the content of a limit statement to be applied |
||
54 | # to the rules that log discarded traffic. Set to false to |
||
55 | # disable rate limiting. |
||
56 | # |
||
57 | 70727742 | Nacho Barrientos | # @param reject_with |
58 | # How to discard packets not matching any rule. If `false`, the |
||
59 | # fate of the packet will be defined by the chain policy (normally |
||
60 | # drop), otherwise the packet will be rejected with the REJECT_WITH |
||
61 | # policy indicated by the value of this parameter. |
||
62 | # |
||
63 | ea96d5db | Nacho Barrientos | # @param in_out_conntrack |
64 | # Adds INPUT and OUTPUT rules to allow traffic that's part of an |
||
65 | # established connection and also to drop invalid packets. |
||
66 | # |
||
67 | 24a5a2a7 | tr | # @param fwd_conntrack |
68 | # Adds FORWARD rules to allow traffic that's part of an |
||
69 | # established connection and also to drop invalid packets. |
||
70 | # |
||
71 | ae9872e2 | Nacho Barrientos | # @param firewalld_enable |
72 | # Configures how the firewalld systemd service unit is enabled. It might be |
||
73 | # useful to set this to false if you're externaly removing firewalld from |
||
74 | # the system completely. |
||
75 | # |
||
76 | 03d9e7da | Steve Traylen | # @param noflush_tables |
77 | # If specified only other existings tables will be flushed. |
||
78 | # If left unset all tables will be flushed via a `flush ruleset` |
||
79 | # |
||
80 | 09cba182 | Steve Traylen | # @param rules |
81 | # Specify hashes of `nftables::rule`s via hiera |
||
82 | # |
||
83 | 7fe7a4aa | Kienan Stewart | # @param configuration_path |
84 | de7a904c | Kienan Stewart | # The absolute path to the principal nftables configuration file. The default |
85 | # varies depending on the system, and is set in the module's data. |
||
86 | 8d22a441 | Kienan Stewart | # |
87 | be0b08e1 | tr | class nftables ( |
88 | 31b17627 | Steve Traylen | Boolean $in_ssh = true, |
89 | Boolean $in_icmp = true, |
||
90 | Boolean $out_ntp = true, |
||
91 | Boolean $out_dns = true, |
||
92 | Boolean $out_http = true, |
||
93 | Boolean $out_https = true, |
||
94 | Boolean $out_icmp = true, |
||
95 | Boolean $out_all = false, |
||
96 | Boolean $in_out_conntrack = true, |
||
97 | Boolean $fwd_conntrack = false, |
||
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 | de7a904c | Kienan Stewart | Stdlib::Unixpath $configuration_path, |
107 | cb6f3584 | Kienan Stewart | Stdlib::Unixpath $nft_path = '/usr/sbin/nft', |
108 | Stdlib::Unixpath $systemctl_path = '/usr/bin/systemctl', |
||
109 | Stdlib::Unixpath $echo_path = '/usr/bin/echo', |
||
110 | be0b08e1 | tr | ) { |
111 | 11bf7237 | Steve Traylen | package { 'nftables': |
112 | 0ba57c66 | mh | ensure => installed, |
113 | 11bf7237 | Steve Traylen | } -> file_line { |
114 | 0ba57c66 | mh | 'enable_nftables': |
115 | line => 'include "/etc/nftables/puppet.nft"', |
||
116 | 7fe7a4aa | Kienan Stewart | path => $configuration_path, |
117 | 0ba57c66 | mh | notify => Service['nftables'], |
118 | 11bf7237 | Steve Traylen | } -> file { |
119 | 0ba57c66 | mh | default: |
120 | e140adff | tr | owner => 'root', |
121 | group => 'root', |
||
122 | mode => '0640'; |
||
123 | 8d22a441 | Kienan Stewart | '/etc/nftables': |
124 | ensure => directory, |
||
125 | mode => '0750'; |
||
126 | 30462da1 | Steve Traylen | '/etc/nftables/puppet-preflight': |
127 | ensure => directory, |
||
128 | mode => '0750', |
||
129 | purge => true, |
||
130 | force => true, |
||
131 | recurse => true; |
||
132 | '/etc/nftables/puppet-preflight.nft': |
||
133 | 82d10659 | Nacho Barrientos | ensure => file, |
134 | 03d9e7da | Steve Traylen | content => epp('nftables/config/puppet.nft.epp', { 'nat' => $nat, 'noflush' => $noflush_tables }); |
135 | 11bf7237 | Steve Traylen | } ~> exec { |
136 | 30462da1 | Steve Traylen | 'nft validate': |
137 | refreshonly => true, |
||
138 | cb6f3584 | Kienan Stewart | command => "${nft_path} -I /etc/nftables/puppet-preflight -c -f /etc/nftables/puppet-preflight.nft || ( ${echo_path} \"#CONFIG BROKEN\" >> /etc/nftables/puppet-preflight.nft && /bin/false)"; |
139 | 11bf7237 | Steve Traylen | } -> file { |
140 | 30462da1 | Steve Traylen | default: |
141 | owner => 'root', |
||
142 | group => 'root', |
||
143 | mode => '0640'; |
||
144 | 0ba57c66 | mh | '/etc/nftables/puppet.nft': |
145 | 82d10659 | Nacho Barrientos | ensure => file, |
146 | 03d9e7da | Steve Traylen | content => epp('nftables/config/puppet.nft.epp', { 'nat' => $nat, 'noflush' => $noflush_tables }); |
147 | 0ba57c66 | mh | '/etc/nftables/puppet': |
148 | ensure => directory, |
||
149 | 5acb554a | tr | mode => '0750', |
150 | 0ba57c66 | mh | purge => true, |
151 | force => true, |
||
152 | recurse => true; |
||
153 | 11bf7237 | Steve Traylen | } ~> service { 'nftables': |
154 | 30462da1 | Steve Traylen | ensure => running, |
155 | enable => true, |
||
156 | hasrestart => true, |
||
157 | cb6f3584 | Kienan Stewart | restart => "${systemctl_path} reload nftables", |
158 | 30462da1 | Steve Traylen | } |
159 | |||
160 | a86c6409 | Kienan Stewart | $puppet_nft_vars = { |
161 | 7fe7a4aa | Kienan Stewart | 'configuration_path' => $configuration_path, |
162 | cb6f3584 | Kienan Stewart | 'nft_path' => $nft_path, |
163 | a86c6409 | Kienan Stewart | } |
164 | 11bf7237 | Steve Traylen | systemd::dropin_file { 'puppet_nft.conf': |
165 | 03d9e7da | Steve Traylen | ensure => present, |
166 | unit => 'nftables.service', |
||
167 | a86c6409 | Kienan Stewart | content => epp('nftables/systemd/puppet_nft.conf.epp', $puppet_nft_vars), |
168 | 03d9e7da | Steve Traylen | notify => Service['nftables'], |
169 | 0ba57c66 | mh | } |
170 | |||
171 | c4b1b93b | Steve Traylen | # firewalld.enable can be mask or false depending upon if firewalld is installed or not |
172 | # https://tickets.puppetlabs.com/browse/PUP-10814 |
||
173 | 11bf7237 | Steve Traylen | service { 'firewalld': |
174 | f02562f2 | tr | ensure => stopped, |
175 | ae9872e2 | Nacho Barrientos | enable => $firewalld_enable, |
176 | f02562f2 | tr | } |
177 | |||
178 | c8092701 | tr | include nftables::inet_filter |
179 | 82d10659 | Nacho Barrientos | if $nat { |
180 | include nftables::ip_nat |
||
181 | } |
||
182 | b3a7a6dd | tr | |
183 | # inject custom rules e.g. from hiera |
||
184 | 66ed7f61 | mh | $rules.each |$n,$v| { |
185 | 11bf7237 | Steve Traylen | nftables::rule { |
186 | 66ed7f61 | mh | $n: |
187 | 11bf7237 | Steve Traylen | * => $v, |
188 | 66ed7f61 | mh | } |
189 | } |
||
190 | 802d80d1 | Nacho Barrientos | |
191 | # inject custom sets e.g. from hiera |
||
192 | $sets.each |$n,$v| { |
||
193 | 11bf7237 | Steve Traylen | nftables::set { |
194 | 802d80d1 | Nacho Barrientos | $n: |
195 | 11bf7237 | Steve Traylen | * => $v, |
196 | 802d80d1 | Nacho Barrientos | } |
197 | } |
||
198 | 0ba57c66 | mh | } |