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 => "-"
36 property :helper, :kind_of => String, :default => "-"
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 = case new_resource.proto
74 when "tcp", "tcp:syn" then "tcp"
77 if new_resource.source_ports
78 rule << "#{proto} sport { #{nftables_source_ports} }"
81 if new_resource.dest_ports
82 rule << "#{proto} dport { #{nftables_dest_ports} }"
85 if new_resource.source == "osm"
86 rule << "#{ip} saddr @#{ip}-osm-addresses"
87 elsif new_resource.source =~ /^net:(.*)$/
88 addresses = Regexp.last_match(1).split(",").join(", ")
90 rule << "#{ip} saddr { #{addresses} }"
93 if new_resource.dest == "osm"
94 rule << "#{ip} daddr @#{ip}-osm-addresses"
95 elsif new_resource.dest =~ /^net:(.*)$/
96 addresses = Regexp.last_match(1).split(",").join(", ")
98 rule << "#{ip} daddr { #{addresses} }"
101 if new_resource.proto == "tcp:syn"
102 rule << "ct state new"
105 if new_resource.connection_limit != "-"
106 set = "connlimit-#{new_resource.rule}-#{ip}"
108 node.default[:networking][:firewall][:sets] << set
110 rule << "add @#{set} { #{ip} saddr ct count #{new_resource.connection_limit} }"
113 if new_resource.rate_limit =~ %r{^s:(\d+)/sec:(\d+)$}
114 set = "ratelimit-#{new_resource.rule}-#{ip}"
115 rate = Regexp.last_match(1)
116 burst = Regexp.last_match(2)
118 node.default[:networking][:firewall][:sets] << set
120 rule << "update @#{set} { #{ip} saddr limit rate #{rate}/second burst #{burst} packets }"
124 when :accept then "accept"
125 when :drop then "jump log-and-drop"
126 when :reject then "jump log-and-reject"
129 if new_resource.source == "fw"
130 node.default[:networking][:firewall][:outgoing] << rule.join(" ")
131 elsif new_resource.dest == "fw"
132 node.default[:networking][:firewall][:incoming] << rule.join(" ")
136 def nftables_source_ports
137 Array(new_resource.source_ports).map(&:to_s).join(",")
140 def nftables_dest_ports
141 Array(new_resource.dest_ports).map(&:to_s).join(",")