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[:networking][:interfaces][name][:inet][:prefix] = role[:inet][:prefix]
 
  73     node.default[:networking][:interfaces][name][:inet][:gateway] = role[:inet][:gateway]
 
  74     node.default[:networking][:interfaces][name][:inet][:routes] = role[:inet][:routes]
 
  77   if interface[:inet6] && role[:inet6]
 
  78     node.default[:networking][:interfaces][name][:inet6][:prefix] = role[:inet6][:prefix]
 
  79     node.default[:networking][:interfaces][name][:inet6][:gateway] = role[:inet6][:gateway]
 
  80     node.default[:networking][:interfaces][name][:inet6][:routes] = role[:inet6][:routes]
 
  83   node.default[:networking][:interfaces][name][:metric] = role[:metric]
 
  84   node.default[: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 service "systemd-networkd" do
 
 130   action [:enable, :start]
 
 133 if node[:networking][:wireguard][:enabled]
 
 134   wireguard_id = persistent_token("networking", "wireguard")
 
 136   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]}"
 
 138   package "wireguard-tools" do
 
 140     options "--no-install-recommends"
 
 143   directory "/var/lib/systemd/wireguard" do
 
 145     group "systemd-network"
 
 150   file "/var/lib/systemd/wireguard/private.key" do
 
 151     action :create_if_missing
 
 153     group "systemd-network"
 
 155     content %x(wg genkey)
 
 159   node.default[:networking][:wireguard][:public_key] = %x(wg pubkey < /var/lib/systemd/wireguard/private.key).chomp
 
 161   file "/var/lib/systemd/wireguard/preshared.key" do
 
 162     action :create_if_missing
 
 164     group "systemd-network"
 
 166     content keys["wireguard"]
 
 169   if node[:roles].include?("gateway")
 
 170     search(:node, "roles:gateway") do |gateway|
 
 171       next if gateway.name == node.name
 
 172       next unless gateway[:networking][:wireguard] && gateway[:networking][:wireguard][:enabled]
 
 174       allowed_ips = gateway.ipaddresses(:role => :internal).map(&:subnet)
 
 176       node.default[:networking][:wireguard][:peers] << {
 
 177         :public_key => gateway[:networking][:wireguard][:public_key],
 
 178         :allowed_ips => allowed_ips,
 
 179         :endpoint => "#{gateway.name}:51820"
 
 183     search(:node, "roles:prometheus") do |server|
 
 184       allowed_ips = server.ipaddresses(:role => :internal).map(&:subnet)
 
 186       if server[:networking][:private_address]
 
 187         allowed_ips << "#{server[:networking][:private_address]}/32"
 
 190       node.default[:networking][:wireguard][:peers] << {
 
 191         :public_key => server[:networking][:wireguard][:public_key],
 
 192         :allowed_ips => allowed_ips,
 
 193         :endpoint => "#{server.name}:51820"
 
 197     node.default[:networking][:wireguard][:peers] << {
 
 198       :public_key => "7Oj9ufNlgidyH/xDc+aHQKMjJPqTmD/ab13agMh6AxA=",
 
 199       :allowed_ips => "10.0.16.1/32",
 
 200       :endpoint => "gate.compton.nu:51820"
 
 204     node.default[:networking][:wireguard][:peers] << {
 
 205       :public_key => "RofATnvlWxP3mt87+QKRXFE5MVxtoCcTsJ+yftZYEE4=",
 
 206       :allowed_ips => "10.89.122.1/32",
 
 207       :endpoint => "gate.firefishy.com:51820"
 
 211     node.default[:networking][:wireguard][:peers] << {
 
 212       :public_key => "YbUkREE9TAmomqgL/4Fh2e5u2Hh7drN/2o5qg3ndRxg=",
 
 213       :allowed_ips => "10.89.123.1/32",
 
 214       :endpoint => "roaming.firefishy.com:51820"
 
 216   elsif node[:roles].include?("shenron")
 
 217     search(:node, "roles:gateway") do |gateway|
 
 218       allowed_ips = gateway.ipaddresses(:role => :internal).map(&:subnet)
 
 220       node.default[:networking][:wireguard][:peers] << {
 
 221         :public_key => gateway[:networking][:wireguard][:public_key],
 
 222         :allowed_ips => allowed_ips,
 
 223         :endpoint => "#{gateway.name}:51820"
 
 228   file "/etc/systemd/network/wireguard.netdev" do
 
 232   template "/etc/systemd/network/10-wg0.netdev" do
 
 233     source "wireguard.netdev.erb"
 
 235     group "systemd-network"
 
 237     notifies :run, "execute[networkctl-delete-wg0]"
 
 238     notifies :run, "notify_group[networkctl-reload]"
 
 241   file "/etc/systemd/network/wireguard.network" do
 
 245   template "/etc/systemd/network/10-wg0.network" do
 
 246     source "wireguard.network.erb"
 
 250     notifies :run, "execute[networkctl-reload]"
 
 253   execute "networkctl-delete-wg0" do
 
 255     command "networkctl delete wg0"
 
 256     only_if { ::File.exist?("/sys/class/net/wg0") }
 
 260 notify_group "networkctl-reload"
 
 262 execute "networkctl-reload" do
 
 264   command "networkctl reload"
 
 265   subscribes :run, "notify_group[networkctl-reload]"
 
 268 ohai "reload-hostname" do
 
 273 execute "hostnamectl-set-hostname" do
 
 274   command "hostnamectl set-hostname #{node[:networking][:hostname]}"
 
 275   notifies :reload, "ohai[reload-hostname]"
 
 276   not_if { kitchen? || node[:hostnamectl][:static_hostname] == node[:networking][:hostname] }
 
 279 template "/etc/hosts" do
 
 287 service "systemd-resolved" do
 
 288   action [:enable, :start]
 
 291 directory "/etc/systemd/resolved.conf.d" do
 
 297 template "/etc/systemd/resolved.conf.d/99-chef.conf" do
 
 298   source "resolved.conf.erb"
 
 302   notifies :restart, "service[systemd-resolved]", :immediately
 
 305 if node[:filesystem][:by_mountpoint][:"/etc/resolv.conf"]
 
 306   execute "umount-resolve-conf" do
 
 307     command "umount -c /etc/resolv.conf"
 
 311 link "/etc/resolv.conf" do
 
 312   to "../run/systemd/resolve/stub-resolv.conf"
 
 315 hosts = { :inet => [], :inet6 => [] }
 
 317 search(:node, "networking:interfaces").collect do |n|
 
 318   next if n[:fqdn] == node[:fqdn]
 
 320   n.interfaces.each do |interface|
 
 321     next unless interface[:role] == "external"
 
 323     hosts[:inet] << interface[:inet][:address] if interface[:inet]
 
 324     hosts[:inet6] << interface[:inet6][:address] if interface[:inet6]
 
 332 node.interfaces(:role => :external).each do |interface|
 
 333   interfaces << interface[:interface]
 
 336 template "/etc/nftables.conf" do
 
 337   source "nftables.conf.erb"
 
 341   variables :interfaces => interfaces, :hosts => hosts
 
 342   notifies :reload, "service[nftables]"
 
 345 directory "/var/lib/nftables" do
 
 351 template "/usr/local/bin/nftables" do
 
 352   source "nftables.erb"
 
 358 systemd_service "nftables-stop" do
 
 364 systemd_service "nftables-chef" do
 
 367   exec_start "/usr/local/bin/nftables start"
 
 368   exec_reload "/usr/local/bin/nftables reload"
 
 369   exec_stop "/usr/local/bin/nftables stop"
 
 372 if node[:networking][:firewall][:enabled]
 
 373   service "nftables" do
 
 374     action [:enable, :start]
 
 377   service "nftables" do
 
 378     action [:disable, :stop]
 
 382 if node[:networking][:wireguard][:enabled]
 
 383   firewall_rule "accept-wireguard" do
 
 387     source :osm unless node[:roles].include?("gateway")
 
 393 firewall_rule "accept-http" do
 
 397   dest_ports %w[http https]
 
 398   rate_limit node[:networking][:firewall][:http_rate_limit]
 
 399   connection_limit node[:networking][:firewall][:http_connection_limit]