2 # Cookbook:: networking
5 # Copyright:: 2010, OpenStreetMap Foundation.
6 # Copyright:: 2009, Opscode, Inc.
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
12 # https://www.apache.org/licenses/LICENSE-2.0
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.
21 # * node[:networking][:nameservers]
26 keys = data_bag_item("networking", "keys")
33 "renderer" => "networkd",
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]
49 node.default[:networking][:interfaces][name][:metric] = role[:metric]
50 node.default[:networking][:interfaces][name][:zone] = role[:zone]
53 if interface[:address]
54 prefix = node[:networking][:interfaces][name][:prefix]
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)
60 interface = node[:networking][:interfaces][name]
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),
70 elsif interface[:interface] =~ /^bond\d+$/
71 netplan["network"]["bonds"][interface[:interface]] ||= {
77 netplan["network"]["ethernets"][interface[:interface]] ||= {
84 if interface[:address]
85 deviceplan["addresses"].push("#{interface[:address]}/#{prefix}")
89 deviceplan["mtu"] = interface[:mtu]
93 deviceplan["interfaces"] = interface[:bond][:slaves].to_a
95 deviceplan["parameters"] = {
96 "mode" => interface[:bond][:mode] || "active-backup",
97 "primary" => interface[:bond][:slaves].first,
98 "mii-monitor-interval" => interface[:bond][:miimon] || 100,
99 "down-delay" => interface[:bond][:downdelay] || 200,
100 "up-delay" => interface[:bond][:updelay] || 200
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]
107 if interface[:gateway]
108 if interface[:family] == "inet"
109 default_route = "0.0.0.0/0"
110 elsif interface[:family] == "inet6"
111 default_route = "::/0"
114 deviceplan["routes"].push(
115 "to" => default_route,
116 "via" => interface[:gateway],
117 "metric" => interface[:metric],
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:
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],
137 if interface[:routes]
138 interface[:routes].each do |to, parameters|
139 next if parameters[:via] == interface[:address]
145 route["type"] = parameters[:type] if parameters[:type]
146 route["via"] = parameters[:via] if parameters[:via]
147 route["metric"] = parameters[:metric] if parameters[:metric]
149 deviceplan["routes"].push(route)
153 node.rm(:networking, :interfaces, name)
157 netplan["network"]["bonds"].each_value do |bond|
158 bond["interfaces"].each do |interface|
159 netplan["network"]["ethernets"][interface] ||= { "accept-ra" => false }
163 netplan["network"]["vlans"].each_value do |vlan|
164 unless vlan["link"] =~ /^bond\d+$/
165 netplan["network"]["ethernets"][vlan["link"]] ||= { "accept-ra" => false }
169 file "/etc/netplan/01-netcfg.yaml" do
173 file "/etc/netplan/50-cloud-init.yaml" do
177 file "/etc/netplan/99-chef.yaml" do
181 content YAML.dump(netplan)
184 package "cloud-init" do
188 if node[:networking][:wireguard][:enabled]
189 wireguard_id = persistent_token("networking", "wireguard")
191 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]}"
193 package "wireguard-tools" do
197 directory "/var/lib/systemd/wireguard" do
199 group "systemd-network"
204 file "/var/lib/systemd/wireguard/private.key" do
205 action :create_if_missing
207 group "systemd-network"
209 content %x(wg genkey)
213 node.default[:networking][:wireguard][:public_key] = %x(wg pubkey < /var/lib/systemd/wireguard/private.key).chomp
215 file "/var/lib/systemd/wireguard/preshared.key" do
216 action :create_if_missing
218 group "systemd-network"
220 content keys["wireguard"]
223 if node[:roles].include?("gateway")
224 search(:node, "roles:gateway") do |gateway|
225 next if gateway.name == node.name
226 next unless gateway[:networking][:wireguard] && gateway[:networking][:wireguard][:enabled]
228 allowed_ips = gateway.interfaces(:role => :internal).map do |interface|
229 "#{interface[:network]}/#{interface[:prefix]}"
232 node.default[:networking][:wireguard][:peers] << {
233 :public_key => gateway[:networking][:wireguard][:public_key],
234 :allowed_ips => allowed_ips,
235 :endpoint => "#{gateway.name}:51820"
239 search(:node, "roles:mail OR roles:prometheus") do |server|
240 allowed_ips = server.interfaces(:role => :internal).map do |interface|
241 "#{interface[:network]}/#{interface[:prefix]}"
244 if server[:networking][:private_address]
245 allowed_ips << "#{server[:networking][:private_address]}/32"
248 node.default[:networking][:wireguard][:peers] << {
249 :public_key => server[:networking][:wireguard][:public_key],
250 :allowed_ips => allowed_ips,
251 :endpoint => "#{server.name}:51820"
255 node.default[:networking][:wireguard][:peers] << {
256 :public_key => "7Oj9ufNlgidyH/xDc+aHQKMjJPqTmD/ab13agMh6AxA=",
257 :allowed_ips => "10.0.16.1/32",
258 :endpoint => "gate.compton.nu:51820"
262 template "/etc/systemd/network/wireguard.netdev" do
263 source "wireguard.netdev.erb"
265 group "systemd-network"
269 template "/etc/systemd/network/wireguard.network" do
270 source "wireguard.network.erb"
276 if node[:lsb][:release].to_f < 20.04
277 execute "ip-link-delete-wg0" do
279 command "ip link delete wg0"
280 subscribes :run, "template[/etc/systemd/network/wireguard.netdev]"
281 only_if { ::File.exist?("/sys/class/net/wg0") }
284 service "systemd-networkd" do
286 subscribes :restart, "template[/etc/systemd/network/wireguard.netdev]"
287 subscribes :restart, "template[/etc/systemd/network/wireguard.network]"
288 not_if { ENV.key?("TEST_KITCHEN") }
291 execute "networkctl-delete-wg0" do
293 command "networkctl delete wg0"
294 subscribes :run, "template[/etc/systemd/network/wireguard.netdev]"
295 only_if { ::File.exist?("/sys/class/net/wg0") }
298 execute "networkctl-reload" do
300 command "networkctl reload"
301 subscribes :run, "template[/etc/systemd/network/wireguard.netdev]"
302 subscribes :run, "template[/etc/systemd/network/wireguard.network]"
303 not_if { ENV.key?("TEST_KITCHEN") }
308 ohai "reload-hostname" do
313 execute "hostnamectl-set-hostname" do
314 command "hostnamectl set-hostname #{node[:networking][:hostname]}"
315 notifies :reload, "ohai[reload-hostname]"
316 not_if { ENV.key?("TEST_KITCHEN") || node[:hostnamectl][:static_hostname] == node[:networking][:hostname] }
319 template "/etc/hosts" do
324 not_if { ENV["TEST_KITCHEN"] }
327 service "systemd-resolved" do
328 action [:enable, :start]
331 directory "/etc/systemd/resolved.conf.d" do
337 template "/etc/systemd/resolved.conf.d/99-chef.conf" do
338 source "resolved.conf.erb"
342 notifies :restart, "service[systemd-resolved]", :immediately
345 if node[:filesystem][:by_mountpoint][:"/etc/resolv.conf"]
346 execute "umount-resolve-conf" do
347 command "umount -c /etc/resolv.conf"
351 link "/etc/resolv.conf" do
352 to "../run/systemd/resolve/stub-resolv.conf"
357 search(:node, "networking:interfaces").collect do |n|
358 next if n[:fqdn] == node[:fqdn]
360 n.interfaces.each do |interface|
361 next unless interface[:role] == "external" && interface[:zone]
363 zones[interface[:zone]] ||= {}
364 zones[interface[:zone]][interface[:family]] ||= []
365 zones[interface[:zone]][interface[:family]] << interface[:address]
371 systemd_service "shorewall-docker" do
374 exec_stop "/sbin/shorewall $OPTIONS stop"
375 notifies :restart, "service[shorewall]"
378 template "/etc/default/shorewall" do
379 source "shorewall-default.erb"
383 notifies :restart, "service[shorewall]"
386 template "/etc/shorewall/shorewall.conf" do
387 source "shorewall.conf.erb"
391 notifies :restart, "service[shorewall]"
394 template "/etc/shorewall/zones" do
395 source "shorewall-zones.erb"
399 variables :type => "ipv4"
400 notifies :restart, "service[shorewall]"
403 template "/etc/shorewall/interfaces" do
404 source "shorewall-interfaces.erb"
408 notifies :restart, "service[shorewall]"
411 template "/etc/shorewall/hosts" do
412 source "shorewall-hosts.erb"
416 variables :zones => zones
417 notifies :restart, "service[shorewall]"
420 template "/etc/shorewall/conntrack" do
421 source "shorewall-conntrack.erb"
425 notifies :restart, "service[shorewall]"
426 only_if { node[:networking][:firewall][:raw] }
429 template "/etc/shorewall/policy" do
430 source "shorewall-policy.erb"
434 notifies :restart, "service[shorewall]"
437 template "/etc/shorewall/rules" do
438 source "shorewall-rules.erb"
442 variables :family => "inet"
443 notifies :restart, "service[shorewall]"
446 template "/etc/shorewall/stoppedrules" do
447 source "shorewall-stoppedrules.erb"
451 notifies :restart, "service[shorewall]"
454 if node[:networking][:firewall][:enabled]
455 service "shorewall" do
456 action [:enable, :start]
457 supports :restart => true
458 status_command "shorewall status"
462 service "shorewall" do
463 action [:disable, :stop]
464 supports :restart => true
465 status_command "shorewall status"
470 template "/etc/logrotate.d/shorewall" do
471 source "logrotate.shorewall.erb"
475 variables :name => "shorewall"
478 firewall_rule "limit-icmp-echo" do
484 dest_ports "echo-request"
485 rate_limit "s:1/sec:5"
488 if node[:networking][:wireguard][:enabled]
489 wireguard_source = if node[:roles].include?("gateway")
495 firewall_rule "accept-wireguard" do
497 source wireguard_source
505 if node[:roles].include?("gateway")
506 template "/etc/shorewall/masq" do
507 source "shorewall-masq.erb"
511 notifies :restart, "service[shorewall]"
514 file "/etc/shorewall/masq" do
516 notifies :restart, "service[shorewall]"
520 unless node.interfaces(:family => :inet6).empty?
523 template "/etc/default/shorewall6" do
524 source "shorewall-default.erb"
528 notifies :restart, "service[shorewall6]"
531 template "/etc/shorewall6/shorewall6.conf" do
532 source "shorewall6.conf.erb"
536 notifies :restart, "service[shorewall6]"
539 template "/etc/shorewall6/zones" do
540 source "shorewall-zones.erb"
544 variables :type => "ipv6"
545 notifies :restart, "service[shorewall6]"
548 template "/etc/shorewall6/interfaces" do
549 source "shorewall6-interfaces.erb"
553 notifies :restart, "service[shorewall6]"
556 template "/etc/shorewall6/hosts" do
557 source "shorewall6-hosts.erb"
561 variables :zones => zones
562 notifies :restart, "service[shorewall6]"
565 template "/etc/shorewall6/conntrack" do
566 source "shorewall-conntrack.erb"
570 notifies :restart, "service[shorewall6]"
571 only_if { node[:networking][:firewall][:raw] }
574 template "/etc/shorewall6/policy" do
575 source "shorewall-policy.erb"
579 notifies :restart, "service[shorewall6]"
582 template "/etc/shorewall6/rules" do
583 source "shorewall-rules.erb"
587 variables :family => "inet6"
588 notifies :restart, "service[shorewall6]"
591 if node[:networking][:firewall][:enabled]
592 service "shorewall6" do
593 action [:enable, :start]
594 supports :restart => true
595 status_command "shorewall6 status"
599 service "shorewall6" do
600 action [:disable, :stop]
601 supports :restart => true
602 status_command "shorewall6 status"
607 template "/etc/logrotate.d/shorewall6" do
608 source "logrotate.shorewall.erb"
612 variables :name => "shorewall6"
615 firewall_rule "limit-icmp6-echo" do
621 dest_ports "echo-request"
622 rate_limit "s:1/sec:5"
626 firewall_rule "accept-http" do
632 rate_limit node[:networking][:firewall][:http_rate_limit]
633 connection_limit node[:networking][:firewall][:http_connection_limit]
636 firewall_rule "accept-https" do
642 rate_limit node[:networking][:firewall][:http_rate_limit]
643 connection_limit node[:networking][:firewall][:http_connection_limit]