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")
28 file "/etc/netplan/00-installer-config.yaml" do
32 file "/etc/netplan/01-netcfg.yaml" do
36 file "/etc/netplan/50-cloud-init.yaml" do
40 file "/etc/netplan/99-chef.yaml" do
44 package "netplan.io" do
48 package "cloud-init" do
52 interfaces = node[:networking][:interfaces].collect do |name, interface|
53 [interface[:interface], name]
56 node[:networking][:interfaces].each do |name, interface|
57 if interface[:interface] =~ /^(.*)\.(\d+)$/
58 vlan_interface = Regexp.last_match(1)
59 vlan_id = Regexp.last_match(2)
61 parent = interfaces[vlan_interface] || "vlans_#{vlan_interface}"
63 node.default_unless[:networking][:interfaces][parent][:interface] = vlan_interface
64 node.default_unless[:networking][:interfaces][parent][:vlans] = []
66 node.default[:networking][:interfaces][parent][:vlans] << vlan_id
69 next unless interface[:role] && (role = node[:networking][:roles][interface[:role]])
71 if interface[:inet] && role[:inet]
72 node.default_unless[:networking][:interfaces][name][:inet][:prefix] = role[:inet][:prefix]
73 node.default_unless[:networking][:interfaces][name][:inet][:gateway] = role[:inet][:gateway]
74 node.default_unless[:networking][:interfaces][name][:inet][:routes] = role[:inet][:routes]
77 if interface[:inet6] && role[:inet6]
78 node.default_unless[:networking][:interfaces][name][:inet6][:prefix] = role[:inet6][:prefix]
79 node.default_unless[:networking][:interfaces][name][:inet6][:gateway] = role[:inet6][:gateway]
80 node.default_unless[:networking][:interfaces][name][:inet6][:routes] = role[:inet6][:routes]
83 node.default_unless[:networking][:interfaces][name][:metric] = role[:metric]
84 node.default_unless[:networking][:interfaces][name][:zone] = role[:zone]
87 node[:networking][:interfaces].each do |_, interface|
88 if interface[:interface] =~ /^.*\.(\d+)$/
89 template "/etc/systemd/network/10-#{interface[:interface]}.netdev" do
90 source "vlan.netdev.erb"
94 variables :interface => interface, :vlan => Regexp.last_match(1)
95 notifies :run, "notify_group[networkctl-reload]"
97 elsif interface[:interface] =~ /^bond\d+$/
98 template "/etc/systemd/network/10-#{interface[:interface]}.netdev" do
99 source "bond.netdev.erb"
103 variables :interface => interface
104 notifies :run, "notify_group[networkctl-reload]"
107 interface[:bond][:slaves].each do |slave|
108 template "/etc/systemd/network/10-#{slave}.network" do
109 source "slave.network.erb"
113 variables :master => interface, :slave => slave
114 notifies :run, "notify_group[networkctl-reload]"
119 template "/etc/systemd/network/10-#{interface[:interface]}.network" do
124 variables :interface => interface
125 notifies :run, "notify_group[networkctl-reload]"
129 package "systemd-resolved" do
131 only_if { platform?("ubuntu") && node[:lsb][:release].to_f > 22.04 || platform?("debian") && node[:lsb][:release].to_f > 11.0 }
134 service "systemd-networkd" do
135 action [:enable, :start]
138 if node[:networking][:wireguard][:enabled]
139 wireguard_id = persistent_token("networking", "wireguard")
141 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]}"
143 package "wireguard-tools" do
145 options "--no-install-recommends"
148 directory "/var/lib/systemd/wireguard" do
150 group "systemd-network"
155 file "/var/lib/systemd/wireguard/private.key" do
156 action :create_if_missing
158 group "systemd-network"
160 content %x(wg genkey)
164 node.default[:networking][:wireguard][:public_key] = %x(wg pubkey < /var/lib/systemd/wireguard/private.key).chomp
166 file "/var/lib/systemd/wireguard/preshared.key" do
167 action :create_if_missing
169 group "systemd-network"
171 content keys["wireguard"]
174 if node[:roles].include?("gateway")
175 search(:node, "roles:gateway") do |gateway|
176 next if gateway.name == node.name
177 next unless gateway[:networking][:wireguard] && gateway[:networking][:wireguard][:enabled]
179 allowed_ips = gateway.ipaddresses(:role => :internal).map(&:subnet)
181 node.default[:networking][:wireguard][:peers] << {
182 :public_key => gateway[:networking][:wireguard][:public_key],
183 :allowed_ips => allowed_ips,
184 :endpoint => "#{gateway.name}:51820"
188 search(:node, "roles:prometheus") do |server|
189 allowed_ips = server.ipaddresses(:role => :internal).map(&:subnet)
191 if server[:networking][:private_address]
192 allowed_ips << "#{server[:networking][:private_address]}/32"
195 node.default[:networking][:wireguard][:peers] << {
196 :public_key => server[:networking][:wireguard][:public_key],
197 :allowed_ips => allowed_ips,
198 :endpoint => "#{server.name}:51820"
202 node.default[:networking][:wireguard][:peers] << {
203 :public_key => "7Oj9ufNlgidyH/xDc+aHQKMjJPqTmD/ab13agMh6AxA=",
204 :allowed_ips => "10.0.16.1/32",
205 :endpoint => "gate.compton.nu:51820"
209 node.default[:networking][:wireguard][:peers] << {
210 :public_key => "RofATnvlWxP3mt87+QKRXFE5MVxtoCcTsJ+yftZYEE4=",
211 :allowed_ips => "10.89.122.1/32",
212 :endpoint => "gate.firefishy.com:51820"
216 node.default[:networking][:wireguard][:peers] << {
217 :public_key => "YbUkREE9TAmomqgL/4Fh2e5u2Hh7drN/2o5qg3ndRxg=",
218 :allowed_ips => "10.89.123.1/32",
219 :endpoint => "roaming.firefishy.com:51820"
221 elsif node[:roles].include?("shenron")
222 search(:node, "roles:gateway") do |gateway|
223 allowed_ips = gateway.ipaddresses(:role => :internal).map(&:subnet)
225 node.default[:networking][:wireguard][:peers] << {
226 :public_key => gateway[:networking][:wireguard][:public_key],
227 :allowed_ips => allowed_ips,
228 :endpoint => "#{gateway.name}:51820"
233 file "/etc/systemd/network/wireguard.netdev" do
237 template "/etc/systemd/network/10-wg0.netdev" do
238 source "wireguard.netdev.erb"
240 group "systemd-network"
242 notifies :run, "execute[networkctl-delete-wg0]"
243 notifies :run, "notify_group[networkctl-reload]"
246 file "/etc/systemd/network/wireguard.network" do
250 template "/etc/systemd/network/10-wg0.network" do
251 source "wireguard.network.erb"
255 notifies :run, "execute[networkctl-reload]"
258 execute "networkctl-delete-wg0" do
260 command "networkctl delete wg0"
261 only_if { ::File.exist?("/sys/class/net/wg0") }
265 notify_group "networkctl-reload"
267 execute "networkctl-reload" do
269 command "networkctl reload"
270 subscribes :run, "notify_group[networkctl-reload]"
273 ohai "reload-hostname" do
278 execute "hostnamectl-set-hostname" do
279 command "hostnamectl set-hostname #{node[:networking][:hostname]}"
280 notifies :reload, "ohai[reload-hostname]"
281 not_if { kitchen? || node[:hostnamectl][:static_hostname] == node[:networking][:hostname] }
284 template "/etc/hosts" do
292 service "systemd-resolved" do
293 action [:enable, :start]
296 directory "/etc/systemd/resolved.conf.d" do
302 template "/etc/systemd/resolved.conf.d/99-chef.conf" do
303 source "resolved.conf.erb"
307 notifies :restart, "service[systemd-resolved]", :immediately
310 if node[:filesystem][:by_mountpoint][:"/etc/resolv.conf"]
311 execute "umount-resolve-conf" do
312 command "umount -c /etc/resolv.conf"
316 link "/etc/resolv.conf" do
317 to "../run/systemd/resolve/stub-resolv.conf"
320 hosts = { :inet => [], :inet6 => [] }
322 search(:node, "networking:interfaces").collect do |n|
323 next if n[:fqdn] == node[:fqdn]
325 n.interfaces.each do |interface|
326 next unless interface[:role] == "external"
328 hosts[:inet] << interface[:inet][:address] if interface[:inet]
329 hosts[:inet6] << interface[:inet6][:address] if interface[:inet6]
337 node.interfaces(:role => :external).each do |interface|
338 interfaces << interface[:interface]
341 template "/etc/nftables.conf" do
342 source "nftables.conf.erb"
346 variables :interfaces => interfaces, :hosts => hosts
347 notifies :reload, "service[nftables]"
350 directory "/var/lib/nftables" do
356 template "/usr/local/bin/nftables" do
357 source "nftables.erb"
363 systemd_service "nftables-stop" do
369 systemd_service "nftables-chef" do
372 exec_start "/usr/local/bin/nftables start"
373 exec_reload "/usr/local/bin/nftables reload"
374 exec_stop "/usr/local/bin/nftables stop"
377 if node[:networking][:firewall][:enabled]
378 service "nftables" do
379 action [:enable, :start]
382 service "nftables" do
383 action [:disable, :stop]
387 if node[:networking][:wireguard][:enabled]
388 firewall_rule "accept-wireguard" do
392 source :osm unless node[:roles].include?("gateway")
398 firewall_rule "accept-http-osm" do
403 dest_ports %w[http https]
406 firewall_rule "accept-http" do
410 dest_ports %w[http https]
411 rate_limit node[:networking][:firewall][:http_rate_limit]
412 connection_limit node[:networking][:firewall][:http_connection_limit]