]> git.openstreetmap.org Git - chef.git/blob - cookbooks/networking/recipes/default.rb
Preserve blocklists over firewall restarts
[chef.git] / cookbooks / networking / recipes / default.rb
1 #
2 # Cookbook:: networking
3 # Recipe:: default
4 #
5 # Copyright:: 2010, OpenStreetMap Foundation.
6 # Copyright:: 2009, Opscode, Inc.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 #     https://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 #
20 # = Requires
21 # * node[:networking][:nameservers]
22
23 require "ipaddr"
24 require "yaml"
25
26 keys = data_bag_item("networking", "keys")
27
28 package "netplan.io"
29
30 netplan = {
31   "network" => {
32     "version" => 2,
33     "renderer" => "networkd",
34     "ethernets" => {},
35     "bonds" => {},
36     "vlans" => {}
37   }
38 }
39
40 node[:networking][:interfaces].each do |name, interface|
41   if interface[:interface]
42     if interface[:role] && (role = node[:networking][:roles][interface[:role]])
43       if role[interface[:family]]
44         node.default[:networking][:interfaces][name][:prefix] = role[interface[:family]][:prefix]
45         node.default[:networking][:interfaces][name][:gateway] = role[interface[:family]][:gateway]
46         node.default[:networking][:interfaces][name][:routes] = role[interface[:family]][:routes]
47       end
48
49       node.default[:networking][:interfaces][name][:metric] = role[:metric]
50       node.default[:networking][:interfaces][name][:zone] = role[:zone]
51     end
52
53     if interface[:address]
54       prefix = node[:networking][:interfaces][name][:prefix]
55
56       node.default[:networking][:interfaces][name][:netmask] = (~IPAddr.new(interface[:address]).mask(0)).mask(prefix)
57       node.default[:networking][:interfaces][name][:network] = IPAddr.new(interface[:address]).mask(prefix)
58     end
59
60     interface = node[:networking][:interfaces][name]
61
62     deviceplan = if interface[:interface] =~ /^(.*)\.(\d+)$/
63                    netplan["network"]["vlans"][interface[:interface]] ||= {
64                      "id" => Regexp.last_match(2).to_i,
65                      "link" => Regexp.last_match(1),
66                      "accept-ra" => false,
67                      "addresses" => [],
68                      "routes" => []
69                    }
70                  elsif interface[:interface] =~ /^bond\d+$/
71                    netplan["network"]["bonds"][interface[:interface]] ||= {
72                      "accept-ra" => false,
73                      "addresses" => [],
74                      "routes" => []
75                    }
76                  else
77                    netplan["network"]["ethernets"][interface[:interface]] ||= {
78                      "accept-ra" => false,
79                      "addresses" => [],
80                      "routes" => []
81                    }
82                  end
83
84     if interface[:address]
85       deviceplan["addresses"].push("#{interface[:address]}/#{prefix}")
86     end
87
88     if interface[:mtu]
89       deviceplan["mtu"] = interface[:mtu]
90     end
91
92     if interface[:bond]
93       deviceplan["interfaces"] = interface[:bond][:slaves].to_a
94
95       deviceplan["parameters"] = {
96         "mode" => interface[:bond][:mode] || "active-backup",
97         "mii-monitor-interval" => interface[:bond][:miimon] || 100,
98         "down-delay" => interface[:bond][:downdelay] || 200,
99         "up-delay" => interface[:bond][:updelay] || 200
100       }
101
102       deviceplan["parameters"]["primary"] = interface[:bond][:slaves].first if deviceplan["parameters"]["mode"] == "active-backup"
103       deviceplan["parameters"]["transmit-hash-policy"] = interface[:bond][:xmithashpolicy] if interface[:bond][:xmithashpolicy]
104       deviceplan["parameters"]["lacp-rate"] = interface[:bond][:lacprate] if interface[:bond][:lacprate]
105     end
106
107     if interface[:gateway] && interface[:gateway] != interface[:address]
108       if interface[:family] == "inet"
109         default_route = "0.0.0.0/0"
110       elsif interface[:family] == "inet6"
111         default_route = "::/0"
112       end
113
114       deviceplan["routes"].push(
115         "to" => default_route,
116         "via" => interface[:gateway],
117         "metric" => interface[:metric],
118         "on-link" => true
119       )
120
121       # This ordering relies on systemd-networkd adding routes
122       # in reverse order and will need moving before the previous
123       # route once that is fixed:
124       #
125       # https://github.com/systemd/systemd/issues/5430
126       # https://github.com/systemd/systemd/pull/10938
127       if interface[:family] == "inet6" &&
128          !interface[:network].include?(interface[:gateway]) &&
129          !IPAddr.new("fe80::/64").include?(interface[:gateway])
130         deviceplan["routes"].push(
131           "to" => interface[:gateway],
132           "scope" => "link"
133         )
134       end
135     end
136
137     if interface[:routes]
138       interface[:routes].each do |to, parameters|
139         next if parameters[:via] == interface[:address]
140
141         route = {
142           "to" => to
143         }
144
145         route["type"] = parameters[:type] if parameters[:type]
146         route["via"] = parameters[:via] if parameters[:via]
147         route["metric"] = parameters[:metric] if parameters[:metric]
148
149         deviceplan["routes"].push(route)
150       end
151     end
152   else
153     node.rm(:networking, :interfaces, name)
154   end
155 end
156
157 netplan["network"]["bonds"].each_value do |bond|
158   bond["interfaces"].each do |interface|
159     netplan["network"]["ethernets"][interface] ||= { "accept-ra" => false, "optional" => true }
160   end
161 end
162
163 netplan["network"]["vlans"].each_value do |vlan|
164   unless vlan["link"] =~ /^bond\d+$/
165     netplan["network"]["ethernets"][vlan["link"]] ||= { "accept-ra" => false }
166   end
167 end
168
169 file "/etc/netplan/00-installer-config.yaml" do
170   action :delete
171 end
172
173 file "/etc/netplan/01-netcfg.yaml" do
174   action :delete
175 end
176
177 file "/etc/netplan/50-cloud-init.yaml" do
178   action :delete
179 end
180
181 file "/etc/netplan/99-chef.yaml" do
182   owner "root"
183   group "root"
184   mode "644"
185   content YAML.dump(netplan)
186 end
187
188 package "cloud-init" do
189   action :purge
190 end
191
192 if node[:networking][:wireguard][:enabled]
193   wireguard_id = persistent_token("networking", "wireguard")
194
195   node.default[:networking][:wireguard][:address] = "fd43:e709:ea6d:1:#{wireguard_id[0, 4]}:#{wireguard_id[4, 4]}:#{wireguard_id[8, 4]}:#{wireguard_id[12, 4]}"
196
197   package "wireguard-tools" do
198     compile_time true
199     options "--no-install-recommends"
200   end
201
202   directory "/var/lib/systemd/wireguard" do
203     owner "root"
204     group "systemd-network"
205     mode "750"
206     compile_time true
207   end
208
209   file "/var/lib/systemd/wireguard/private.key" do
210     action :create_if_missing
211     owner "root"
212     group "systemd-network"
213     mode "640"
214     content %x(wg genkey)
215     compile_time true
216   end
217
218   node.default[:networking][:wireguard][:public_key] = %x(wg pubkey < /var/lib/systemd/wireguard/private.key).chomp
219
220   file "/var/lib/systemd/wireguard/preshared.key" do
221     action :create_if_missing
222     owner "root"
223     group "systemd-network"
224     mode "640"
225     content keys["wireguard"]
226   end
227
228   if node[:roles].include?("gateway")
229     search(:node, "roles:gateway") do |gateway|
230       next if gateway.name == node.name
231       next unless gateway[:networking][:wireguard] && gateway[:networking][:wireguard][:enabled]
232
233       allowed_ips = gateway.interfaces(:role => :internal).map do |interface|
234         "#{interface[:network]}/#{interface[:prefix]}"
235       end
236
237       node.default[:networking][:wireguard][:peers] << {
238         :public_key => gateway[:networking][:wireguard][:public_key],
239         :allowed_ips => allowed_ips,
240         :endpoint => "#{gateway.name}:51820"
241       }
242     end
243
244     search(:node, "roles:prometheus") do |server|
245       allowed_ips = server.interfaces(:role => :internal).map do |interface|
246         "#{interface[:network]}/#{interface[:prefix]}"
247       end
248
249       if server[:networking][:private_address]
250         allowed_ips << "#{server[:networking][:private_address]}/32"
251       end
252
253       node.default[:networking][:wireguard][:peers] << {
254         :public_key => server[:networking][:wireguard][:public_key],
255         :allowed_ips => allowed_ips,
256         :endpoint => "#{server.name}:51820"
257       }
258     end
259
260     node.default[:networking][:wireguard][:peers] << {
261       :public_key => "7Oj9ufNlgidyH/xDc+aHQKMjJPqTmD/ab13agMh6AxA=",
262       :allowed_ips => "10.0.16.1/32",
263       :endpoint => "gate.compton.nu:51820"
264     }
265
266     # Grant home
267     node.default[:networking][:wireguard][:peers] << {
268       :public_key => "RofATnvlWxP3mt87+QKRXFE5MVxtoCcTsJ+yftZYEE4=",
269       :allowed_ips => "10.89.122.1/32",
270       :endpoint => "gate.firefishy.com:51820"
271     }
272
273     # Grant roaming
274     node.default[:networking][:wireguard][:peers] << {
275       :public_key => "YbUkREE9TAmomqgL/4Fh2e5u2Hh7drN/2o5qg3ndRxg=",
276       :allowed_ips => "10.89.123.1/32",
277       :endpoint => "roaming.firefishy.com:51820"
278     }
279   elsif node[:roles].include?("shenron")
280     search(:node, "roles:gateway") do |gateway|
281       allowed_ips = gateway.interfaces(:role => :internal).map do |interface|
282         "#{interface[:network]}/#{interface[:prefix]}"
283       end
284
285       node.default[:networking][:wireguard][:peers] << {
286         :public_key => gateway[:networking][:wireguard][:public_key],
287         :allowed_ips => allowed_ips,
288         :endpoint => "#{gateway.name}:51820"
289       }
290     end
291   end
292
293   template "/etc/systemd/network/wireguard.netdev" do
294     source "wireguard.netdev.erb"
295     owner "root"
296     group "systemd-network"
297     mode "640"
298   end
299
300   template "/etc/systemd/network/wireguard.network" do
301     source "wireguard.network.erb"
302     owner "root"
303     group "root"
304     mode "644"
305   end
306
307   if node[:lsb][:release].to_f < 20.04
308     execute "ip-link-delete-wg0" do
309       action :nothing
310       command "ip link delete wg0"
311       subscribes :run, "template[/etc/systemd/network/wireguard.netdev]"
312       only_if { ::File.exist?("/sys/class/net/wg0") }
313     end
314
315     service "systemd-networkd" do
316       action :nothing
317       subscribes :restart, "template[/etc/systemd/network/wireguard.netdev]"
318       subscribes :restart, "template[/etc/systemd/network/wireguard.network]"
319       not_if { kitchen? }
320     end
321   else
322     execute "networkctl-delete-wg0" do
323       action :nothing
324       command "networkctl delete wg0"
325       subscribes :run, "template[/etc/systemd/network/wireguard.netdev]"
326       only_if { ::File.exist?("/sys/class/net/wg0") }
327     end
328
329     execute "networkctl-reload" do
330       action :nothing
331       command "networkctl reload"
332       subscribes :run, "template[/etc/systemd/network/wireguard.netdev]"
333       subscribes :run, "template[/etc/systemd/network/wireguard.network]"
334       not_if { kitchen? }
335     end
336   end
337 end
338
339 ohai "reload-hostname" do
340   action :nothing
341   plugin "hostname"
342 end
343
344 execute "hostnamectl-set-hostname" do
345   command "hostnamectl set-hostname #{node[:networking][:hostname]}"
346   notifies :reload, "ohai[reload-hostname]"
347   not_if { kitchen? || node[:hostnamectl][:static_hostname] == node[:networking][:hostname] }
348 end
349
350 template "/etc/hosts" do
351   source "hosts.erb"
352   owner "root"
353   group "root"
354   mode "644"
355   not_if { kitchen? }
356 end
357
358 service "systemd-resolved" do
359   action [:enable, :start]
360 end
361
362 directory "/etc/systemd/resolved.conf.d" do
363   owner "root"
364   group "root"
365   mode "755"
366 end
367
368 template "/etc/systemd/resolved.conf.d/99-chef.conf" do
369   source "resolved.conf.erb"
370   owner "root"
371   group "root"
372   mode "644"
373   notifies :restart, "service[systemd-resolved]", :immediately
374 end
375
376 if node[:filesystem][:by_mountpoint][:"/etc/resolv.conf"]
377   execute "umount-resolve-conf" do
378     command "umount -c /etc/resolv.conf"
379   end
380 end
381
382 link "/etc/resolv.conf" do
383   to "../run/systemd/resolve/stub-resolv.conf"
384 end
385
386 hosts = { "inet" => [], "inet6" => [] }
387
388 search(:node, "networking:interfaces").collect do |n|
389   next if n[:fqdn] == node[:fqdn]
390
391   n.interfaces.each do |interface|
392     next unless interface[:role] == "external"
393
394     hosts[interface[:family]] << interface[:address]
395   end
396 end
397
398 package "nftables"
399
400 interfaces = []
401
402 node.interfaces(:role => :external).each do |interface|
403   interfaces << interface[:interface]
404 end
405
406 template "/etc/nftables.conf" do
407   source "nftables.conf.erb"
408   owner "root"
409   group "root"
410   mode "755"
411   variables :interfaces => interfaces, :hosts => hosts
412   notifies :reload, "service[nftables]"
413 end
414
415 directory "/var/lib/nftables" do
416   owner "root"
417   group "root"
418   mode "755"
419 end
420
421 template "/usr/local/bin/nftables" do
422   source "nftables.erb"
423   owner "root"
424   group "root"
425   mode "755"
426 end
427
428 systemd_service "nftables-stop" do
429   action :delete
430   service "nftables"
431   dropin "stop"
432 end
433
434 systemd_service "nftables-chef" do
435   service "nftables"
436   dropin "chef"
437   exec_start "/usr/local/bin/nftables start"
438   exec_reload "/usr/local/bin/nftables reload"
439   exec_stop "/usr/local/bin/nftables stop"
440 end
441
442 if node[:networking][:firewall][:enabled]
443   service "nftables" do
444     action [:enable, :start]
445   end
446 else
447   service "nftables" do
448     action [:disable, :stop]
449   end
450 end
451
452 if node[:networking][:wireguard][:enabled]
453   firewall_rule "accept-wireguard" do
454     action :accept
455     context :incoming
456     protocol :udp
457     source :osm unless node[:roles].include?("gateway")
458     dest_ports "51820"
459     source_ports "51820"
460   end
461 end
462
463 firewall_rule "accept-http" do
464   action :accept
465   context :incoming
466   protocol :tcp
467   dest_ports %w[http https]
468   rate_limit node[:networking][:firewall][:http_rate_limit]
469   connection_limit node[:networking][:firewall][:http_connection_limit]
470 end