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_value do |interface|
 
  61   next unless interface[:interface] =~ /^(.*)\.(\d+)$/
 
  63   vlan_interface = Regexp.last_match(1)
 
  64   vlan_id = Regexp.last_match(2)
 
  66   parent = interfaces[vlan_interface] || "vlans_#{vlan_interface}"
 
  68   node.default_unless[:networking][:interfaces][parent][:interface] = vlan_interface
 
  69   node.default_unless[:networking][:interfaces][parent][:vlans] = []
 
  71   node.default[:networking][:interfaces][parent][:vlans] << vlan_id
 
  74 node[:networking][:interfaces].each_value do |interface|
 
  75   if interface[:interface] =~ /^.*\.(\d+)$/
 
  76     template "/etc/systemd/network/10-#{interface[:interface]}.netdev" do
 
  77       source "vlan.netdev.erb"
 
  81       variables :interface => interface, :vlan => Regexp.last_match(1)
 
  82       notifies :run, "notify_group[networkctl-reload]"
 
  84   elsif interface[:interface] =~ /^bond\d+$/
 
  85     next unless interface[:bond]
 
  87     template "/etc/systemd/network/10-#{interface[:interface]}.netdev" do
 
  88       source "bond.netdev.erb"
 
  92       variables :interface => interface
 
  93       notifies :run, "notify_group[networkctl-reload]"
 
  96     interface[:bond][:slaves].each do |slave|
 
  97       template "/etc/systemd/network/10-#{slave}.network" do
 
  98         source "slave.network.erb"
 
 102         variables :master => interface, :slave => slave
 
 103         notifies :run, "notify_group[networkctl-reload]"
 
 108   template "/etc/systemd/network/10-#{interface[:interface]}.network" do
 
 113     variables :interface => interface
 
 114     notifies :run, "notify_group[networkctl-reload]"
 
 118 package "systemd-resolved" do
 
 120   only_if { platform?("ubuntu") && node[:lsb][:release].to_f > 22.04 || platform?("debian") && node[:lsb][:release].to_f > 11.0 }
 
 123 service "systemd-networkd" do
 
 124   action [:enable, :start]
 
 127 if node[:networking][:wireguard][:enabled]
 
 128   wireguard_id = persistent_token("networking", "wireguard")
 
 130   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]}"
 
 132   package "wireguard-tools" do
 
 134     options "--no-install-recommends"
 
 137   directory "/var/lib/systemd/wireguard" do
 
 139     group "systemd-network"
 
 144   file "/var/lib/systemd/wireguard/private.key" do
 
 145     action :create_if_missing
 
 147     group "systemd-network"
 
 149     content %x(wg genkey)
 
 153   node.default[:networking][:wireguard][:public_key] = %x(wg pubkey < /var/lib/systemd/wireguard/private.key).chomp
 
 155   file "/var/lib/systemd/wireguard/preshared.key" do
 
 156     action :create_if_missing
 
 158     group "systemd-network"
 
 160     content keys["wireguard"]
 
 163   if node[:roles].include?("gateway")
 
 164     search(:node, "roles:gateway") do |gateway|
 
 165       next if gateway.name == node.name
 
 166       next unless gateway[:networking][:wireguard] && gateway[:networking][:wireguard][:enabled]
 
 168       allowed_ips = gateway.ipaddresses(:role => :internal).map(&:subnet)
 
 170       node.default[:networking][:wireguard][:peers] << {
 
 171         :public_key => gateway[:networking][:wireguard][:public_key],
 
 172         :allowed_ips => allowed_ips,
 
 173         :endpoint => "#{gateway.name}:51820"
 
 177     search(:node, "roles:prometheus") do |server|
 
 178       allowed_ips = server.ipaddresses(:role => :internal).map(&:subnet)
 
 180       if server[:networking][:private_address]
 
 181         allowed_ips << "#{server[:networking][:private_address]}/32"
 
 184       node.default[:networking][:wireguard][:peers] << {
 
 185         :public_key => server[:networking][:wireguard][:public_key],
 
 186         :allowed_ips => allowed_ips,
 
 187         :endpoint => "#{server.name}:51820"
 
 191     node.default[:networking][:wireguard][:peers] << {
 
 192       :public_key => "7Oj9ufNlgidyH/xDc+aHQKMjJPqTmD/ab13agMh6AxA=",
 
 193       :allowed_ips => "10.0.16.1/32",
 
 194       :endpoint => "gate.compton.nu:51820"
 
 198     node.default[:networking][:wireguard][:peers] << {
 
 199       :public_key => "RofATnvlWxP3mt87+QKRXFE5MVxtoCcTsJ+yftZYEE4=",
 
 200       :allowed_ips => "10.89.122.1/32",
 
 201       :endpoint => "gate.firefishy.com:51820"
 
 205     node.default[:networking][:wireguard][:peers] << {
 
 206       :public_key => "YbUkREE9TAmomqgL/4Fh2e5u2Hh7drN/2o5qg3ndRxg=",
 
 207       :allowed_ips => "10.89.123.1/32",
 
 208       :endpoint => "roaming.firefishy.com:51820"
 
 210   elsif node[:roles].include?("shenron")
 
 211     search(:node, "roles:gateway") do |gateway|
 
 212       allowed_ips = gateway.ipaddresses(:role => :internal).map(&:subnet)
 
 214       node.default[:networking][:wireguard][:peers] << {
 
 215         :public_key => gateway[:networking][:wireguard][:public_key],
 
 216         :allowed_ips => allowed_ips,
 
 217         :endpoint => "#{gateway.name}:51820"
 
 222   file "/etc/systemd/network/wireguard.netdev" do
 
 226   template "/etc/systemd/network/10-wg0.netdev" do
 
 227     source "wireguard.netdev.erb"
 
 229     group "systemd-network"
 
 231     notifies :run, "execute[networkctl-delete-wg0]"
 
 232     notifies :run, "notify_group[networkctl-reload]"
 
 235   file "/etc/systemd/network/wireguard.network" do
 
 239   template "/etc/systemd/network/10-wg0.network" do
 
 240     source "wireguard.network.erb"
 
 244     notifies :run, "execute[networkctl-reload]"
 
 247   execute "networkctl-delete-wg0" do
 
 249     command "networkctl delete wg0"
 
 250     only_if { ::File.exist?("/sys/class/net/wg0") }
 
 254 # Setup dokken network in systemd-networkd to avoid systemd-networkd-wait-online delay
 
 255 template "/etc/systemd/network/dokken.network" do
 
 256   source "dokken.network.erb"
 
 260   notifies :run, "execute[networkctl-reload]", :immediately
 
 264 notify_group "networkctl-reload"
 
 266 execute "networkctl-reload" do
 
 268   command "networkctl reload"
 
 269   subscribes :run, "notify_group[networkctl-reload]"
 
 272 ohai "reload-hostname" do
 
 277 execute "hostnamectl-set-hostname" do
 
 278   command "hostnamectl set-hostname #{node[:networking][:hostname]}"
 
 279   notifies :reload, "ohai[reload-hostname]"
 
 280   not_if { kitchen? || node[:hostnamectl][:static_hostname] == node[:networking][:hostname] }
 
 283 template "/etc/hosts" do
 
 291 service "systemd-resolved" do
 
 292   action [:enable, :start]
 
 295 directory "/etc/systemd/resolved.conf.d" do
 
 301 template "/etc/systemd/resolved.conf.d/99-chef.conf" do
 
 302   source "resolved.conf.erb"
 
 306   notifies :restart, "service[systemd-resolved]", :immediately
 
 309 if node[:filesystem][:by_mountpoint][:"/etc/resolv.conf"]
 
 310   execute "umount-resolve-conf" do
 
 311     command "umount -c /etc/resolv.conf"
 
 315 link "/etc/resolv.conf" do
 
 316   to "../run/systemd/resolve/stub-resolv.conf"
 
 321 gem_package "dbus-systemd" do
 
 322   gem_binary node[:ruby][:system_gem]
 
 325 prometheus_exporter "resolved" do
 
 327   user "systemd-resolve"
 
 328   restrict_address_families "AF_UNIX"
 
 331 hosts = { :inet => [], :inet6 => [] }
 
 333 search(:node, "networking:interfaces").collect do |n|
 
 334   next if n[:fqdn] == node[:fqdn]
 
 336   n.interfaces.each do |interface|
 
 337     next unless interface[:role] == "external"
 
 339     hosts[:inet] << interface[:inet][:address] if interface[:inet]
 
 340     hosts[:inet6] << interface[:inet6][:address] if interface[:inet6]
 
 346 interfaces = node.interfaces(:role => :external).map do |interface|
 
 347   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]