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
48 package "netplan.io" do
52 package "cloud-init" do
56 interfaces = node[:networking][:interfaces].collect do |name, interface|
57 [interface[:interface], name]
60 node[:networking][:interfaces].each do |name, interface|
61 if interface[:interface] =~ /^(.*)\.(\d+)$/
62 vlan_interface = Regexp.last_match(1)
63 vlan_id = Regexp.last_match(2)
65 parent = interfaces[vlan_interface] || "vlans_#{vlan_interface}"
67 node.default_unless[:networking][:interfaces][parent][:interface] = vlan_interface
68 node.default_unless[:networking][:interfaces][parent][:vlans] = []
70 node.default[:networking][:interfaces][parent][:vlans] << vlan_id
73 next unless interface[:role] && (role = node[:networking][:roles][interface[:role]])
75 if interface[:inet] && role[:inet]
76 node.default_unless[:networking][:interfaces][name][:inet][:prefix] = role[:inet][:prefix]
77 node.default_unless[:networking][:interfaces][name][:inet][:gateway] = role[:inet][:gateway]
78 node.default_unless[:networking][:interfaces][name][:inet][:routes] = role[:inet][:routes]
81 if interface[:inet6] && role[:inet6]
82 node.default_unless[:networking][:interfaces][name][:inet6][:prefix] = role[:inet6][:prefix]
83 node.default_unless[:networking][:interfaces][name][:inet6][:gateway] = role[:inet6][:gateway]
84 node.default_unless[:networking][:interfaces][name][:inet6][:routes] = role[:inet6][:routes]
87 node.default_unless[:networking][:interfaces][name][:metric] = role[:metric]
88 node.default_unless[:networking][:interfaces][name][:zone] = role[:zone]
91 node[:networking][:interfaces].each do |_, interface|
92 if interface[:interface] =~ /^.*\.(\d+)$/
93 template "/etc/systemd/network/10-#{interface[:interface]}.netdev" do
94 source "vlan.netdev.erb"
98 variables :interface => interface, :vlan => Regexp.last_match(1)
99 notifies :run, "notify_group[networkctl-reload]"
101 elsif interface[:interface] =~ /^bond\d+$/
102 template "/etc/systemd/network/10-#{interface[:interface]}.netdev" do
103 source "bond.netdev.erb"
107 variables :interface => interface
108 notifies :run, "notify_group[networkctl-reload]"
111 interface[:bond][:slaves].each do |slave|
112 template "/etc/systemd/network/10-#{slave}.network" do
113 source "slave.network.erb"
117 variables :master => interface, :slave => slave
118 notifies :run, "notify_group[networkctl-reload]"
123 template "/etc/systemd/network/10-#{interface[:interface]}.network" do
128 variables :interface => interface
129 notifies :run, "notify_group[networkctl-reload]"
133 package "systemd-resolved" do
135 only_if { platform?("ubuntu") && node[:lsb][:release].to_f > 22.04 || platform?("debian") && node[:lsb][:release].to_f > 11.0 }
138 service "systemd-networkd" do
139 action [:enable, :start]
142 if node[:networking][:wireguard][:enabled]
143 wireguard_id = persistent_token("networking", "wireguard")
145 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]}"
147 package "wireguard-tools" do
149 options "--no-install-recommends"
152 directory "/var/lib/systemd/wireguard" do
154 group "systemd-network"
159 file "/var/lib/systemd/wireguard/private.key" do
160 action :create_if_missing
162 group "systemd-network"
164 content %x(wg genkey)
168 node.default[:networking][:wireguard][:public_key] = %x(wg pubkey < /var/lib/systemd/wireguard/private.key).chomp
170 file "/var/lib/systemd/wireguard/preshared.key" do
171 action :create_if_missing
173 group "systemd-network"
175 content keys["wireguard"]
178 if node[:roles].include?("gateway")
179 search(:node, "roles:gateway") do |gateway|
180 next if gateway.name == node.name
181 next unless gateway[:networking][:wireguard] && gateway[:networking][:wireguard][:enabled]
183 allowed_ips = gateway.ipaddresses(:role => :internal).map(&:subnet)
185 node.default[:networking][:wireguard][:peers] << {
186 :public_key => gateway[:networking][:wireguard][:public_key],
187 :allowed_ips => allowed_ips,
188 :endpoint => "#{gateway.name}:51820"
192 search(:node, "roles:prometheus") do |server|
193 allowed_ips = server.ipaddresses(:role => :internal).map(&:subnet)
195 if server[:networking][:private_address]
196 allowed_ips << "#{server[:networking][:private_address]}/32"
199 node.default[:networking][:wireguard][:peers] << {
200 :public_key => server[:networking][:wireguard][:public_key],
201 :allowed_ips => allowed_ips,
202 :endpoint => "#{server.name}:51820"
206 node.default[:networking][:wireguard][:peers] << {
207 :public_key => "7Oj9ufNlgidyH/xDc+aHQKMjJPqTmD/ab13agMh6AxA=",
208 :allowed_ips => "10.0.16.1/32",
209 :endpoint => "gate.compton.nu:51820"
213 node.default[:networking][:wireguard][:peers] << {
214 :public_key => "RofATnvlWxP3mt87+QKRXFE5MVxtoCcTsJ+yftZYEE4=",
215 :allowed_ips => "10.89.122.1/32",
216 :endpoint => "gate.firefishy.com:51820"
220 node.default[:networking][:wireguard][:peers] << {
221 :public_key => "YbUkREE9TAmomqgL/4Fh2e5u2Hh7drN/2o5qg3ndRxg=",
222 :allowed_ips => "10.89.123.1/32",
223 :endpoint => "roaming.firefishy.com:51820"
225 elsif node[:roles].include?("shenron")
226 search(:node, "roles:gateway") do |gateway|
227 allowed_ips = gateway.ipaddresses(:role => :internal).map(&:subnet)
229 node.default[:networking][:wireguard][:peers] << {
230 :public_key => gateway[:networking][:wireguard][:public_key],
231 :allowed_ips => allowed_ips,
232 :endpoint => "#{gateway.name}:51820"
237 file "/etc/systemd/network/wireguard.netdev" do
241 template "/etc/systemd/network/10-wg0.netdev" do
242 source "wireguard.netdev.erb"
244 group "systemd-network"
246 notifies :run, "execute[networkctl-delete-wg0]"
247 notifies :run, "notify_group[networkctl-reload]"
250 file "/etc/systemd/network/wireguard.network" do
254 template "/etc/systemd/network/10-wg0.network" do
255 source "wireguard.network.erb"
259 notifies :run, "execute[networkctl-reload]"
262 execute "networkctl-delete-wg0" do
264 command "networkctl delete wg0"
265 only_if { ::File.exist?("/sys/class/net/wg0") }
269 notify_group "networkctl-reload"
271 execute "networkctl-reload" do
273 command "networkctl reload"
274 subscribes :run, "notify_group[networkctl-reload]"
277 ohai "reload-hostname" do
282 execute "hostnamectl-set-hostname" do
283 command "hostnamectl set-hostname #{node[:networking][:hostname]}"
284 notifies :reload, "ohai[reload-hostname]"
285 not_if { kitchen? || node[:hostnamectl][:static_hostname] == node[:networking][:hostname] }
288 template "/etc/hosts" do
296 service "systemd-resolved" do
297 action [:enable, :start]
300 directory "/etc/systemd/resolved.conf.d" do
306 template "/etc/systemd/resolved.conf.d/99-chef.conf" do
307 source "resolved.conf.erb"
311 notifies :restart, "service[systemd-resolved]", :immediately
314 if node[:filesystem][:by_mountpoint][:"/etc/resolv.conf"]
315 execute "umount-resolve-conf" do
316 command "umount -c /etc/resolv.conf"
320 link "/etc/resolv.conf" do
321 to "../run/systemd/resolve/stub-resolv.conf"
324 hosts = { :inet => [], :inet6 => [] }
326 search(:node, "networking:interfaces").collect do |n|
327 next if n[:fqdn] == node[:fqdn]
329 n.interfaces.each do |interface|
330 next unless interface[:role] == "external"
332 hosts[:inet] << interface[:inet][:address] if interface[:inet]
333 hosts[:inet6] << interface[:inet6][:address] if interface[:inet6]
341 node.interfaces(:role => :external).each do |interface|
342 interfaces << interface[:interface]
345 template "/etc/nftables.conf" do
346 source "nftables.conf.erb"
350 variables :interfaces => interfaces, :hosts => hosts
351 notifies :reload, "service[nftables]"
354 directory "/var/lib/nftables" do
360 template "/usr/local/bin/nftables" do
361 source "nftables.erb"
367 systemd_service "nftables-stop" do
373 systemd_service "nftables-chef" do
376 exec_start "/usr/local/bin/nftables start"
377 exec_reload "/usr/local/bin/nftables reload"
378 exec_stop "/usr/local/bin/nftables stop"
381 if node[:networking][:firewall][:enabled]
382 service "nftables" do
383 action [:enable, :start]
386 service "nftables" do
387 action [:disable, :stop]
391 if node[:networking][:wireguard][:enabled]
392 firewall_rule "accept-wireguard" do
396 source :osm unless node[:roles].include?("gateway")
402 firewall_rule "accept-http-osm" do
407 dest_ports %w[http https]
410 firewall_rule "accept-http" do
414 dest_ports %w[http https]
415 rate_limit node[:networking][:firewall][:http_rate_limit]
416 connection_limit node[:networking][:firewall][:http_connection_limit]