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
35 property :connection_limit, :kind_of => [String, Integer]
36 property :helper, :kind_of => String
38 property :compile_time, TrueClass, :default => true
54 if new_resource.family.nil?
55 add_nftables_rule(action, "inet")
56 add_nftables_rule(action, "inet6")
57 elsif new_resource.family.to_s == "inet"
58 add_nftables_rule(action, "inet")
59 elsif new_resource.family.to_s == "inet6"
60 add_nftables_rule(action, "inet6")
64 def add_nftables_rule(action, family)
69 when "inet6" then "ip6"
72 proto = new_resource.proto
74 if new_resource.source_ports
75 rule << "#{proto} sport { #{nftables_source_ports} }"
78 if new_resource.dest_ports
79 rule << "#{proto} dport { #{nftables_dest_ports} }"
82 if new_resource.source == "osm"
83 rule << "#{ip} saddr @#{ip}-osm-addresses"
84 elsif new_resource.source =~ /^net:(.*)$/
85 addresses = Regexp.last_match(1).split(",").join(", ")
87 rule << "#{ip} saddr { #{addresses} }"
90 if new_resource.dest == "osm"
91 rule << "#{ip} daddr @#{ip}-osm-addresses"
92 elsif new_resource.dest =~ /^net:(.*)$/
93 addresses = Regexp.last_match(1).split(",").join(", ")
95 rule << "#{ip} daddr { #{addresses} }"
98 rule << "ct state new" if new_resource.proto == "tcp"
100 if new_resource.connection_limit
101 set = "connlimit-#{new_resource.rule}-#{ip}"
103 node.default[:networking][:firewall][:sets] << set
105 rule << "add @#{set} { #{ip} saddr ct count #{new_resource.connection_limit} }"
108 if new_resource.rate_limit =~ %r{^s:(\d+)/sec:(\d+)$}
109 set = "ratelimit-#{new_resource.rule}-#{ip}"
110 rate = Regexp.last_match(1)
111 burst = Regexp.last_match(2)
113 node.default[:networking][:firewall][:sets] << set
115 rule << "update @#{set} { #{ip} saddr limit rate #{rate}/second burst #{burst} packets }"
118 if new_resource.helper
119 helper = "#{new_resource.rule}-#{new_resource.helper}"
121 node.default[:networking][:firewall][:helpers] << {
122 :name => helper, :helper => new_resource.helper, :protocol => proto
125 rule << "ct helper set #{helper}"
129 when :accept then "accept"
130 when :drop then "jump log-and-drop"
131 when :reject then "jump log-and-reject"
134 if new_resource.source == "fw"
135 node.default[:networking][:firewall][:outgoing] << rule.join(" ")
136 elsif new_resource.dest == "fw"
137 node.default[:networking][:firewall][:incoming] << rule.join(" ")
141 def nftables_source_ports
142 Array(new_resource.source_ports).map(&:to_s).join(",")
145 def nftables_dest_ports
146 Array(new_resource.dest_ports).map(&:to_s).join(",")