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]
 
  31     "renderer" => "networkd",
 
  38 node[:networking][:interfaces].each do |name, interface|
 
  39   if interface[:interface]
 
  40     if interface[:role] && (role = node[:networking][:roles][interface[:role]])
 
  41       if role[interface[:family]]
 
  42         node.normal[:networking][:interfaces][name][:prefix] = role[interface[:family]][:prefix]
 
  43         node.normal[:networking][:interfaces][name][:gateway] = role[interface[:family]][:gateway]
 
  44         node.normal[:networking][:interfaces][name][:routes] = role[interface[:family]][:routes]
 
  47       node.normal[:networking][:interfaces][name][:metric] = role[:metric]
 
  48       node.normal[:networking][:interfaces][name][:zone] = role[:zone]
 
  51     if interface[:address]
 
  52       prefix = node[:networking][:interfaces][name][:prefix]
 
  54       node.normal[:networking][:interfaces][name][:netmask] = (~IPAddr.new(interface[:address]).mask(0)).mask(prefix)
 
  55       node.normal[:networking][:interfaces][name][:network] = IPAddr.new(interface[:address]).mask(prefix)
 
  58     interface = node[:networking][:interfaces][name]
 
  60     deviceplan = if interface[:interface] =~ /^(.*)\.(\d+)$/
 
  61                    netplan["network"]["vlans"][interface[:interface]] ||= {
 
  62                      "id" => Regexp.last_match(2).to_i,
 
  63                      "link" => Regexp.last_match(1),
 
  68                  elsif interface[:interface] =~ /^bond\d+$/
 
  69                    netplan["network"]["bonds"][interface[:interface]] ||= {
 
  75                    netplan["network"]["ethernets"][interface[:interface]] ||= {
 
  82     if interface[:address]
 
  83       deviceplan["addresses"].push("#{interface[:address]}/#{prefix}")
 
  87       deviceplan["mtu"] = interface[:mtu]
 
  91       deviceplan["interfaces"] = interface[:bond][:slaves].to_a
 
  93       deviceplan["parameters"] = {
 
  94         "mode" => interface[:bond][:mode] || "active-backup",
 
  95         "primary" => interface[:bond][:slaves].first,
 
  96         "mii-monitor-interval" => interface[:bond][:miimon] || 100,
 
  97         "down-delay" => interface[:bond][:downdelay] || 200,
 
  98         "up-delay" => interface[:bond][:updelay] || 200
 
 101       deviceplan["parameters"]["transmit-hash-policy"] = interface[:bond][:xmithashpolicy] if interface[:bond][:xmithashpolicy]
 
 102       deviceplan["parameters"]["lacp-rate"] = interface[:bond][:lacprate] if interface[:bond][:lacprate]
 
 105     if interface[:gateway]
 
 106       if interface[:family] == "inet"
 
 107         default_route = "0.0.0.0/0"
 
 108       elsif interface[:family] == "inet6"
 
 109         default_route = "::/0"
 
 112       deviceplan["routes"].push(
 
 113         "to" => default_route,
 
 114         "via" => interface[:gateway],
 
 115         "metric" => interface[:metric],
 
 119       # This ordering relies on systemd-networkd adding routes
 
 120       # in reverse order and will need moving before the previous
 
 121       # route once that is fixed:
 
 123       # https://github.com/systemd/systemd/issues/5430
 
 124       # https://github.com/systemd/systemd/pull/10938
 
 125       if interface[:family] == "inet6" &&
 
 126          !interface[:network].include?(interface[:gateway]) &&
 
 127          !IPAddr.new("fe80::/64").include?(interface[:gateway])
 
 128         deviceplan["routes"].push(
 
 129           "to" => interface[:gateway],
 
 135     if interface[:routes]
 
 136       interface[:routes].each do |to, parameters|
 
 141         route["type"] = parameters[:type] if parameters[:type]
 
 142         route["via"] = parameters[:via] if parameters[:via]
 
 143         route["metric"] = parameters[:metric] if parameters[:metric]
 
 145         deviceplan["routes"].push(route)
 
 149     node.rm(:networking, :interfaces, name)
 
 153 netplan["network"]["bonds"].each_value do |bond|
 
 154   bond["interfaces"].each do |interface|
 
 155     netplan["network"]["ethernets"][interface] ||= { "accept-ra" => false }
 
 159 netplan["network"]["vlans"].each_value do |vlan|
 
 160   unless vlan["link"] =~ /^bond\d+$/
 
 161     netplan["network"]["ethernets"][vlan["link"]] ||= { "accept-ra" => false }
 
 165 file "/etc/netplan/01-netcfg.yaml" do
 
 169 file "/etc/netplan/50-cloud-init.yaml" do
 
 173 file "/etc/netplan/99-chef.yaml" do
 
 177   content YAML.dump(netplan)
 
 180 package "cloud-init" do
 
 184 ohai "reload-hostname" do
 
 189 execute "hostnamectl-set-hostname" do
 
 190   command "hostnamectl set-hostname #{node[:networking][:hostname]}"
 
 191   notifies :reload, "ohai[reload-hostname]"
 
 192   not_if { ENV.key?("TEST_KITCHEN") || node[:hostnamectl][:static_hostname] == node[:networking][:hostname] }
 
 195 template "/etc/hosts" do
 
 200   not_if { ENV["TEST_KITCHEN"] }
 
 203 service "systemd-resolved" do
 
 204   action [:enable, :start]
 
 207 directory "/etc/systemd/resolved.conf.d" do
 
 213 template "/etc/systemd/resolved.conf.d/99-chef.conf" do
 
 214   source "resolved.conf.erb"
 
 218   notifies :restart, "service[systemd-resolved]", :immediately
 
 221 if node[:filesystem][:by_mountpoint][:"/etc/resolv.conf"]
 
 222   mount "/etc/resolv.conf" do
 
 224     device node[:filesystem][:by_mountpoint][:"/etc/resolv.conf"][:devices].first
 
 228 link "/etc/resolv.conf" do
 
 229   to "../run/systemd/resolve/stub-resolv.conf"
 
 232 if node[:networking][:tcp_fastopen_key]
 
 233   fastopen_keys = data_bag_item("networking", "fastopen")
 
 235   node.normal[:sysctl][:tcp_fastopen] = {
 
 236     :comment => "Set shared key for TCP fast open",
 
 238       "net.ipv4.tcp_fastopen_key" => fastopen_keys[node[:networking][:tcp_fastopen_key]]
 
 243 node.interfaces(:role => :internal) do |interface|
 
 244   if interface[:gateway] && interface[:gateway] != interface[:address]
 
 245     search(:node, "networking_interfaces*address:#{interface[:gateway]}") do |gateway|
 
 246       next unless gateway[:openvpn]
 
 248       gateway[:openvpn][:tunnels].each_value do |tunnel|
 
 249         if tunnel[:peer][:address]
 
 250           route tunnel[:peer][:address] do
 
 251             netmask "255.255.255.255"
 
 252             gateway interface[:gateway]
 
 253             device interface[:interface]
 
 257         next unless tunnel[:peer][:networks]
 
 259         tunnel[:peer][:networks].each do |network|
 
 260           route network[:address] do
 
 261             netmask network[:netmask]
 
 262             gateway interface[:gateway]
 
 263             device interface[:interface]
 
 273 search(:node, "networking:interfaces").collect do |n|
 
 274   next if n[:fqdn] == node[:fqdn]
 
 276   n.interfaces.each do |interface|
 
 277     next unless interface[:role] == "external" && interface[:zone]
 
 279     zones[interface[:zone]] ||= {}
 
 280     zones[interface[:zone]][interface[:family]] ||= []
 
 281     zones[interface[:zone]][interface[:family]] << interface[:address]
 
 287 template "/etc/default/shorewall" do
 
 288   source "shorewall-default.erb"
 
 292   notifies :restart, "service[shorewall]"
 
 295 template "/etc/shorewall/shorewall.conf" do
 
 296   source "shorewall.conf.erb"
 
 300   notifies :restart, "service[shorewall]"
 
 303 template "/etc/shorewall/zones" do
 
 304   source "shorewall-zones.erb"
 
 308   variables :type => "ipv4"
 
 309   notifies :restart, "service[shorewall]"
 
 312 template "/etc/shorewall/interfaces" do
 
 313   source "shorewall-interfaces.erb"
 
 317   notifies :restart, "service[shorewall]"
 
 320 template "/etc/shorewall/hosts" do
 
 321   source "shorewall-hosts.erb"
 
 325   variables :zones => zones
 
 326   notifies :restart, "service[shorewall]"
 
 329 template "/etc/shorewall/conntrack" do
 
 330   source "shorewall-conntrack.erb"
 
 334   notifies :restart, "service[shorewall]"
 
 335   only_if { node[:networking][:firewall][:raw] }
 
 338 template "/etc/shorewall/policy" do
 
 339   source "shorewall-policy.erb"
 
 343   notifies :restart, "service[shorewall]"
 
 346 template "/etc/shorewall/rules" do
 
 347   source "shorewall-rules.erb"
 
 351   variables :family => "inet"
 
 352   notifies :restart, "service[shorewall]"
 
 355 service "shorewall" do
 
 356   action [:enable, :start]
 
 357   supports :restart => true
 
 358   status_command "shorewall status"
 
 361 template "/etc/logrotate.d/shorewall" do
 
 362   source "logrotate.shorewall.erb"
 
 366   variables :name => "shorewall"
 
 369 firewall_rule "limit-icmp-echo" do
 
 375   dest_ports "echo-request"
 
 376   rate_limit "s:1/sec:5"
 
 379 %w[ucl ams bm].each do |zone|
 
 380   firewall_rule "accept-openvpn-#{zone}" do
 
 385     dest_ports "1194:1197"
 
 386     source_ports "1194:1197"
 
 390 if node[:roles].include?("gateway")
 
 391   template "/etc/shorewall/masq" do
 
 392     source "shorewall-masq.erb"
 
 396     notifies :restart, "service[shorewall]"
 
 399   file "/etc/shorewall/masq" do
 
 401     notifies :restart, "service[shorewall]"
 
 405 unless node.interfaces(:family => :inet6).empty?
 
 408   template "/etc/default/shorewall6" do
 
 409     source "shorewall-default.erb"
 
 413     notifies :restart, "service[shorewall6]"
 
 416   template "/etc/shorewall6/shorewall6.conf" do
 
 417     source "shorewall6.conf.erb"
 
 421     notifies :restart, "service[shorewall6]"
 
 424   template "/etc/shorewall6/zones" do
 
 425     source "shorewall-zones.erb"
 
 429     variables :type => "ipv6"
 
 430     notifies :restart, "service[shorewall6]"
 
 433   template "/etc/shorewall6/interfaces" do
 
 434     source "shorewall6-interfaces.erb"
 
 438     notifies :restart, "service[shorewall6]"
 
 441   template "/etc/shorewall6/hosts" do
 
 442     source "shorewall6-hosts.erb"
 
 446     variables :zones => zones
 
 447     notifies :restart, "service[shorewall6]"
 
 450   template "/etc/shorewall6/conntrack" do
 
 451     source "shorewall-conntrack.erb"
 
 455     notifies :restart, "service[shorewall6]"
 
 456     only_if { node[:networking][:firewall][:raw] }
 
 459   template "/etc/shorewall6/policy" do
 
 460     source "shorewall-policy.erb"
 
 464     notifies :restart, "service[shorewall6]"
 
 467   template "/etc/shorewall6/rules" do
 
 468     source "shorewall-rules.erb"
 
 472     variables :family => "inet6"
 
 473     notifies :restart, "service[shorewall6]"
 
 476   service "shorewall6" do
 
 477     action [:enable, :start]
 
 478     supports :restart => true
 
 479     status_command "shorewall6 status"
 
 482   template "/etc/logrotate.d/shorewall6" do
 
 483     source "logrotate.shorewall.erb"
 
 487     variables :name => "shorewall6"
 
 490   firewall_rule "limit-icmp6-echo" do
 
 496     dest_ports "echo-request"
 
 497     rate_limit "s:1/sec:5"
 
 501 firewall_rule "accept-http" do
 
 507   rate_limit node[:networking][:firewall][:http_rate_limit]
 
 508   connection_limit node[:networking][:firewall][:http_connection_limit]
 
 511 firewall_rule "accept-https" do
 
 517   rate_limit node[:networking][:firewall][:http_rate_limit]
 
 518   connection_limit node[:networking][:firewall][:http_connection_limit]