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 |_, interface|
63 next unless interface[:interface] =~ /^(.*)\.(\d+)$/
65 vlan_interface = Regexp.last_match(1)
66 vlan_id = Regexp.last_match(2)
68 parent = interfaces[vlan_interface] || "vlans_#{vlan_interface}"
70 node.default_unless[:networking][:interfaces][parent][:interface] = vlan_interface
71 node.default_unless[:networking][:interfaces][parent][:vlans] = []
73 node.default[:networking][:interfaces][parent][:vlans] << vlan_id
76 node[:networking][:interfaces].each do |_, interface|
77 if interface[:interface] =~ /^.*\.(\d+)$/
78 template "/etc/systemd/network/10-#{interface[:interface]}.netdev" do
79 source "vlan.netdev.erb"
83 variables :interface => interface, :vlan => Regexp.last_match(1)
84 notifies :run, "notify_group[networkctl-reload]"
86 elsif interface[:interface] =~ /^bond\d+$/
87 next unless interface[:bond]
89 template "/etc/systemd/network/10-#{interface[:interface]}.netdev" do
90 source "bond.netdev.erb"
94 variables :interface => interface
95 notifies :run, "notify_group[networkctl-reload]"
98 interface[:bond][:slaves].each do |slave|
99 template "/etc/systemd/network/10-#{slave}.network" do
100 source "slave.network.erb"
104 variables :master => interface, :slave => slave
105 notifies :run, "notify_group[networkctl-reload]"
110 template "/etc/systemd/network/10-#{interface[:interface]}.network" do
115 variables :interface => interface
116 notifies :run, "notify_group[networkctl-reload]"
120 package "systemd-resolved" do
122 only_if { platform?("ubuntu") && node[:lsb][:release].to_f > 22.04 || platform?("debian") && node[:lsb][:release].to_f > 11.0 }
125 service "systemd-networkd" do
126 action [:enable, :start]
129 if node[:networking][:wireguard][:enabled]
130 wireguard_id = persistent_token("networking", "wireguard")
132 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]}"
134 package "wireguard-tools" do
136 options "--no-install-recommends"
139 directory "/var/lib/systemd/wireguard" do
141 group "systemd-network"
146 file "/var/lib/systemd/wireguard/private.key" do
147 action :create_if_missing
149 group "systemd-network"
151 content %x(wg genkey)
155 node.default[:networking][:wireguard][:public_key] = %x(wg pubkey < /var/lib/systemd/wireguard/private.key).chomp
157 file "/var/lib/systemd/wireguard/preshared.key" do
158 action :create_if_missing
160 group "systemd-network"
162 content keys["wireguard"]
165 if node[:roles].include?("gateway")
166 search(:node, "roles:gateway") do |gateway|
167 next if gateway.name == node.name
168 next unless gateway[:networking][:wireguard] && gateway[:networking][:wireguard][:enabled]
170 allowed_ips = gateway.ipaddresses(:role => :internal).map(&:subnet)
172 node.default[:networking][:wireguard][:peers] << {
173 :public_key => gateway[:networking][:wireguard][:public_key],
174 :allowed_ips => allowed_ips,
175 :endpoint => "#{gateway.name}:51820"
179 search(:node, "roles:prometheus") do |server|
180 allowed_ips = server.ipaddresses(:role => :internal).map(&:subnet)
182 if server[:networking][:private_address]
183 allowed_ips << "#{server[:networking][:private_address]}/32"
186 node.default[:networking][:wireguard][:peers] << {
187 :public_key => server[:networking][:wireguard][:public_key],
188 :allowed_ips => allowed_ips,
189 :endpoint => "#{server.name}:51820"
193 node.default[:networking][:wireguard][:peers] << {
194 :public_key => "7Oj9ufNlgidyH/xDc+aHQKMjJPqTmD/ab13agMh6AxA=",
195 :allowed_ips => "10.0.16.1/32",
196 :endpoint => "gate.compton.nu:51820"
200 node.default[:networking][:wireguard][:peers] << {
201 :public_key => "RofATnvlWxP3mt87+QKRXFE5MVxtoCcTsJ+yftZYEE4=",
202 :allowed_ips => "10.89.122.1/32",
203 :endpoint => "gate.firefishy.com:51820"
207 node.default[:networking][:wireguard][:peers] << {
208 :public_key => "YbUkREE9TAmomqgL/4Fh2e5u2Hh7drN/2o5qg3ndRxg=",
209 :allowed_ips => "10.89.123.1/32",
210 :endpoint => "roaming.firefishy.com:51820"
212 elsif node[:roles].include?("shenron")
213 search(:node, "roles:gateway") do |gateway|
214 allowed_ips = gateway.ipaddresses(:role => :internal).map(&:subnet)
216 node.default[:networking][:wireguard][:peers] << {
217 :public_key => gateway[:networking][:wireguard][:public_key],
218 :allowed_ips => allowed_ips,
219 :endpoint => "#{gateway.name}:51820"
224 file "/etc/systemd/network/wireguard.netdev" do
228 template "/etc/systemd/network/10-wg0.netdev" do
229 source "wireguard.netdev.erb"
231 group "systemd-network"
233 notifies :run, "execute[networkctl-delete-wg0]"
234 notifies :run, "notify_group[networkctl-reload]"
237 file "/etc/systemd/network/wireguard.network" do
241 template "/etc/systemd/network/10-wg0.network" do
242 source "wireguard.network.erb"
246 notifies :run, "execute[networkctl-reload]"
249 execute "networkctl-delete-wg0" do
251 command "networkctl delete wg0"
252 only_if { ::File.exist?("/sys/class/net/wg0") }
256 # Setup dokken network in systemd-networkd to avoid systemd-networkd-wait-online delay
257 template "/etc/systemd/network/dokken.network" do
258 source "dokken.network.erb"
262 notifies :run, "execute[networkctl-reload]", :immediately
266 notify_group "networkctl-reload"
268 execute "networkctl-reload" do
270 command "networkctl reload"
271 subscribes :run, "notify_group[networkctl-reload]"
274 ohai "reload-hostname" do
279 execute "hostnamectl-set-hostname" do
280 command "hostnamectl set-hostname #{node[:networking][:hostname]}"
281 notifies :reload, "ohai[reload-hostname]"
282 not_if { kitchen? || node[:hostnamectl][:static_hostname] == node[:networking][:hostname] }
285 template "/etc/hosts" do
293 service "systemd-resolved" do
294 action [:enable, :start]
297 directory "/etc/systemd/resolved.conf.d" do
303 template "/etc/systemd/resolved.conf.d/99-chef.conf" do
304 source "resolved.conf.erb"
308 notifies :restart, "service[systemd-resolved]", :immediately
311 if node[:filesystem][:by_mountpoint][:"/etc/resolv.conf"]
312 execute "umount-resolve-conf" do
313 command "umount -c /etc/resolv.conf"
317 link "/etc/resolv.conf" do
318 to "../run/systemd/resolve/stub-resolv.conf"
321 gem_package "dbus-systemd"
323 prometheus_exporter "resolved" do
325 user "systemd-resolve"
326 restrict_address_families "AF_UNIX"
329 hosts = { :inet => [], :inet6 => [] }
331 search(:node, "networking:interfaces").collect do |n|
332 next if n[:fqdn] == node[:fqdn]
334 n.interfaces.each do |interface|
335 next unless interface[:role] == "external"
337 hosts[:inet] << interface[:inet][:address] if interface[:inet]
338 hosts[:inet6] << interface[:inet6][:address] if interface[:inet6]
346 node.interfaces(:role => :external).each do |interface|
347 interfaces << interface[:interface]
350 template "/etc/nftables.conf" do
351 source "nftables.conf.erb"
355 variables :interfaces => interfaces, :hosts => hosts
356 notifies :reload, "service[nftables]"
359 directory "/var/lib/nftables" do
365 template "/usr/local/bin/nftables" do
366 source "nftables.erb"
372 systemd_service "nftables-stop" do
378 systemd_service "nftables-chef" do
381 exec_start "/usr/local/bin/nftables start"
382 exec_reload "/usr/local/bin/nftables reload"
383 exec_stop "/usr/local/bin/nftables stop"
386 if node[:networking][:firewall][:enabled]
387 service "nftables" do
388 action [:enable, :start]
391 service "nftables" do
392 action [:disable, :stop]
396 if node[:networking][:wireguard][:enabled]
397 firewall_rule "accept-wireguard" do
401 source :osm unless node[:roles].include?("gateway")
407 firewall_rule "accept-http-osm" do
412 dest_ports %w[http https]
415 firewall_rule "accept-http" do
419 dest_ports %w[http https]
420 rate_limit node[:networking][:firewall][:http_rate_limit]
421 connection_limit node[:networking][:firewall][:http_connection_limit]