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]
28 keys = data_bag_item("networking", "keys")
30 file "/etc/netplan/00-installer-config.yaml" do
34 file "/etc/netplan/01-netcfg.yaml" do
38 file "/etc/netplan/50-cloud-init.yaml" do
42 file "/etc/netplan/99-chef.yaml" do
50 package "netplan.io" do
54 package "cloud-init" do
58 interfaces = node[:networking][:interfaces].collect do |name, interface|
59 [interface[:interface], name]
62 node[:networking][:interfaces].each do |name, interface|
63 if interface[:interface] =~ /^(.*)\.(\d+)$/
64 vlan_interface = Regexp.last_match(1)
65 vlan_id = Regexp.last_match(2)
67 parent = interfaces[vlan_interface] || "vlans_#{vlan_interface}"
69 node.default_unless[:networking][:interfaces][parent][:interface] = vlan_interface
70 node.default_unless[:networking][:interfaces][parent][:vlans] = []
72 node.default[:networking][:interfaces][parent][:vlans] << vlan_id
75 next unless interface[:role] && (role = node[:networking][:roles][interface[:role]])
77 if interface[:inet] && role[:inet]
78 node.default_unless[:networking][:interfaces][name][:inet][:prefix] = role[:inet][:prefix]
79 node.default_unless[:networking][:interfaces][name][:inet][:gateway] = role[:inet][:gateway]
80 node.default_unless[:networking][:interfaces][name][:inet][:routes] = role[:inet][:routes]
83 if interface[:inet6] && role[:inet6]
84 node.default_unless[:networking][:interfaces][name][:inet6][:prefix] = role[:inet6][:prefix]
85 node.default_unless[:networking][:interfaces][name][:inet6][:gateway] = role[:inet6][:gateway]
86 node.default_unless[:networking][:interfaces][name][:inet6][:routes] = role[:inet6][:routes]
89 node.default_unless[:networking][:interfaces][name][:metric] = role[:metric]
90 node.default_unless[:networking][:interfaces][name][:zone] = role[:zone]
93 node[:networking][:interfaces].each do |_, interface|
94 if interface[:interface] =~ /^.*\.(\d+)$/
95 template "/etc/systemd/network/10-#{interface[:interface]}.netdev" do
96 source "vlan.netdev.erb"
100 variables :interface => interface, :vlan => Regexp.last_match(1)
101 notifies :run, "notify_group[networkctl-reload]"
103 elsif interface[:interface] =~ /^bond\d+$/
104 template "/etc/systemd/network/10-#{interface[:interface]}.netdev" do
105 source "bond.netdev.erb"
109 variables :interface => interface
110 notifies :run, "notify_group[networkctl-reload]"
113 interface[:bond][:slaves].each do |slave|
114 template "/etc/systemd/network/10-#{slave}.network" do
115 source "slave.network.erb"
119 variables :master => interface, :slave => slave
120 notifies :run, "notify_group[networkctl-reload]"
125 template "/etc/systemd/network/10-#{interface[:interface]}.network" do
130 variables :interface => interface
131 notifies :run, "notify_group[networkctl-reload]"
135 package "systemd-resolved" do
137 only_if { platform?("ubuntu") && node[:lsb][:release].to_f > 22.04 || platform?("debian") && node[:lsb][:release].to_f > 11.0 }
140 service "systemd-networkd" do
141 action [:enable, :start]
144 if node[:networking][:wireguard][:enabled]
145 wireguard_id = persistent_token("networking", "wireguard")
147 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]}"
149 package "wireguard-tools" do
151 options "--no-install-recommends"
154 directory "/var/lib/systemd/wireguard" do
156 group "systemd-network"
161 file "/var/lib/systemd/wireguard/private.key" do
162 action :create_if_missing
164 group "systemd-network"
166 content %x(wg genkey)
170 node.default[:networking][:wireguard][:public_key] = %x(wg pubkey < /var/lib/systemd/wireguard/private.key).chomp
172 file "/var/lib/systemd/wireguard/preshared.key" do
173 action :create_if_missing
175 group "systemd-network"
177 content keys["wireguard"]
180 if node[:roles].include?("gateway")
181 search(:node, "roles:gateway") do |gateway|
182 next if gateway.name == node.name
183 next unless gateway[:networking][:wireguard] && gateway[:networking][:wireguard][:enabled]
185 allowed_ips = gateway.ipaddresses(:role => :internal).map(&:subnet)
187 node.default[:networking][:wireguard][:peers] << {
188 :public_key => gateway[:networking][:wireguard][:public_key],
189 :allowed_ips => allowed_ips,
190 :endpoint => "#{gateway.name}:51820"
194 search(:node, "roles:prometheus") do |server|
195 allowed_ips = server.ipaddresses(:role => :internal).map(&:subnet)
197 if server[:networking][:private_address]
198 allowed_ips << "#{server[:networking][:private_address]}/32"
201 node.default[:networking][:wireguard][:peers] << {
202 :public_key => server[:networking][:wireguard][:public_key],
203 :allowed_ips => allowed_ips,
204 :endpoint => "#{server.name}:51820"
208 node.default[:networking][:wireguard][:peers] << {
209 :public_key => "7Oj9ufNlgidyH/xDc+aHQKMjJPqTmD/ab13agMh6AxA=",
210 :allowed_ips => "10.0.16.1/32",
211 :endpoint => "gate.compton.nu:51820"
215 node.default[:networking][:wireguard][:peers] << {
216 :public_key => "RofATnvlWxP3mt87+QKRXFE5MVxtoCcTsJ+yftZYEE4=",
217 :allowed_ips => "10.89.122.1/32",
218 :endpoint => "gate.firefishy.com:51820"
222 node.default[:networking][:wireguard][:peers] << {
223 :public_key => "YbUkREE9TAmomqgL/4Fh2e5u2Hh7drN/2o5qg3ndRxg=",
224 :allowed_ips => "10.89.123.1/32",
225 :endpoint => "roaming.firefishy.com:51820"
227 elsif node[:roles].include?("shenron")
228 search(:node, "roles:gateway") do |gateway|
229 allowed_ips = gateway.ipaddresses(:role => :internal).map(&:subnet)
231 node.default[:networking][:wireguard][:peers] << {
232 :public_key => gateway[:networking][:wireguard][:public_key],
233 :allowed_ips => allowed_ips,
234 :endpoint => "#{gateway.name}:51820"
239 file "/etc/systemd/network/wireguard.netdev" do
243 template "/etc/systemd/network/10-wg0.netdev" do
244 source "wireguard.netdev.erb"
246 group "systemd-network"
248 notifies :run, "execute[networkctl-delete-wg0]"
249 notifies :run, "notify_group[networkctl-reload]"
252 file "/etc/systemd/network/wireguard.network" do
256 template "/etc/systemd/network/10-wg0.network" do
257 source "wireguard.network.erb"
261 notifies :run, "execute[networkctl-reload]"
264 execute "networkctl-delete-wg0" do
266 command "networkctl delete wg0"
267 only_if { ::File.exist?("/sys/class/net/wg0") }
271 # Setup dokken network in systemd-networkd to avoid systemd-networkd-wait-online delay
272 template "/etc/systemd/network/dokken.network" do
273 source "dokken.network.erb"
277 notifies :run, "execute[networkctl-reload]", :immediately
281 notify_group "networkctl-reload"
283 execute "networkctl-reload" do
285 command "networkctl reload"
286 subscribes :run, "notify_group[networkctl-reload]"
289 ohai "reload-hostname" do
294 execute "hostnamectl-set-hostname" do
295 command "hostnamectl set-hostname #{node[:networking][:hostname]}"
296 notifies :reload, "ohai[reload-hostname]"
297 not_if { kitchen? || node[:hostnamectl][:static_hostname] == node[:networking][:hostname] }
300 template "/etc/hosts" do
308 service "systemd-resolved" do
309 action [:enable, :start]
312 directory "/etc/systemd/resolved.conf.d" do
318 template "/etc/systemd/resolved.conf.d/99-chef.conf" do
319 source "resolved.conf.erb"
323 notifies :restart, "service[systemd-resolved]", :immediately
326 if node[:filesystem][:by_mountpoint][:"/etc/resolv.conf"]
327 execute "umount-resolve-conf" do
328 command "umount -c /etc/resolv.conf"
332 link "/etc/resolv.conf" do
333 to "../run/systemd/resolve/stub-resolv.conf"
336 gem_package "dbus-systemd" do
337 gem_binary node[:ruby][:gem]
340 prometheus_exporter "resolved" do
342 user "systemd-resolve"
343 restrict_address_families "AF_UNIX"
346 hosts = { :inet => [], :inet6 => [] }
348 search(:node, "networking:interfaces").collect do |n|
349 next if n[:fqdn] == node[:fqdn]
351 n.interfaces.each do |interface|
352 next unless interface[:role] == "external"
354 hosts[:inet] << interface[:inet][:address] if interface[:inet]
355 hosts[:inet6] << interface[:inet6][:address] if interface[:inet6]
363 node.interfaces(:role => :external).each do |interface|
364 interfaces << interface[:interface]
367 template "/etc/nftables.conf" do
368 source "nftables.conf.erb"
372 variables :interfaces => interfaces, :hosts => hosts
373 notifies :reload, "service[nftables]"
376 directory "/var/lib/nftables" do
382 template "/usr/local/bin/nftables" do
383 source "nftables.erb"
389 systemd_service "nftables-stop" do
395 systemd_service "nftables-chef" do
398 exec_start "/usr/local/bin/nftables start"
399 exec_reload "/usr/local/bin/nftables reload"
400 exec_stop "/usr/local/bin/nftables stop"
403 if node[:networking][:firewall][:enabled]
404 service "nftables" do
405 action [:enable, :start]
408 service "nftables" do
409 action [:disable, :stop]
413 if node[:networking][:wireguard][:enabled]
414 firewall_rule "accept-wireguard" do
418 source :osm unless node[:roles].include?("gateway")
424 firewall_rule "accept-http-osm" do
429 dest_ports %w[http https]
432 firewall_rule "accept-http" do
436 dest_ports %w[http https]
437 rate_limit node[:networking][:firewall][:http_rate_limit]
438 connection_limit node[:networking][:firewall][:http_connection_limit]