2 # Cookbook:: networking
3 # Resource:: firewall_rule
5 # Copyright:: 2020, OpenStreetMap Foundation
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # https://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
20 resource_name :firewall_rule
21 provides :firewall_rule
25 default_action :nothing
27 property :rule, :kind_of => String, :name_property => true
28 property :family, :kind_of => [String, Symbol]
29 property :source, :kind_of => String, :required => true
30 property :dest, :kind_of => String, :required => true
31 property :proto, :kind_of => String, :required => true
32 property :dest_ports, :kind_of => [String, Integer, Array]
33 property :source_ports, :kind_of => [String, Integer, Array]
34 property :rate_limit, :kind_of => String, :default => "-"
35 property :connection_limit, :kind_of => [String, Integer], :default => "-"
37 property :compile_time, TrueClass, :default => true
53 if new_resource.family.nil?
54 add_nftables_rule(action, "inet")
55 add_nftables_rule(action, "inet6")
56 elsif new_resource.family.to_s == "inet"
57 add_nftables_rule(action, "inet")
58 elsif new_resource.family.to_s == "inet6"
59 add_nftables_rule(action, "inet6")
63 def add_nftables_rule(action, family)
68 when "inet6" then "ip6"
71 proto = new_resource.proto
73 if new_resource.source_ports
74 rule << "#{proto} sport { #{nftables_source_ports} }"
77 if new_resource.dest_ports
78 rule << "#{proto} dport { #{nftables_dest_ports} }"
81 if new_resource.source == "osm"
82 rule << "#{ip} saddr @#{ip}-osm-addresses"
83 elsif new_resource.source =~ /^net:(.*)$/
84 addresses = Regexp.last_match(1).split(",").join(", ")
86 rule << "#{ip} saddr { #{addresses} }"
89 if new_resource.dest == "osm"
90 rule << "#{ip} daddr @#{ip}-osm-addresses"
91 elsif new_resource.dest =~ /^net:(.*)$/
92 addresses = Regexp.last_match(1).split(",").join(", ")
94 rule << "#{ip} daddr { #{addresses} }"
97 rule << "ct state new" if new_resource.proto == "tcp"
99 if new_resource.connection_limit != "-"
100 set = "connlimit-#{new_resource.rule}-#{ip}"
102 node.default[:networking][:firewall][:sets] << set
104 rule << "add @#{set} { #{ip} saddr ct count #{new_resource.connection_limit} }"
107 if new_resource.rate_limit =~ %r{^s:(\d+)/sec:(\d+)$}
108 set = "ratelimit-#{new_resource.rule}-#{ip}"
109 rate = Regexp.last_match(1)
110 burst = Regexp.last_match(2)
112 node.default[:networking][:firewall][:sets] << set
114 rule << "update @#{set} { #{ip} saddr limit rate #{rate}/second burst #{burst} packets }"
118 when :accept then "accept"
119 when :drop then "jump log-and-drop"
120 when :reject then "jump log-and-reject"
123 if new_resource.source == "fw"
124 node.default[:networking][:firewall][:outgoing] << rule.join(" ")
125 elsif new_resource.dest == "fw"
126 node.default[:networking][:firewall][:incoming] << rule.join(" ")
130 def nftables_source_ports
131 Array(new_resource.source_ports).map(&:to_s).join(",")
134 def nftables_dest_ports
135 Array(new_resource.dest_ports).map(&:to_s).join(",")