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], :default => "-"
33 property :source_ports, :kind_of => [String, Integer], :default => "-"
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 node[:networking][:firewall][:engine] == "shorewall"
55 add_shorewall_rule(action)
56 elsif node[:networking][:firewall][:engine] == "nftables"
57 if new_resource.family.nil?
58 add_nftables_rule(action, "inet")
59 add_nftables_rule(action, "inet6")
60 elsif new_resource.family.to_s == "inet"
61 add_nftables_rule(action, "inet")
62 elsif new_resource.family.to_s == "inet6"
63 add_nftables_rule(action, "inet6")
68 def add_shorewall_rule(action)
70 :action => action.to_s.upcase,
71 :source => new_resource.source,
72 :dest => new_resource.dest,
73 :proto => new_resource.proto,
74 :dest_ports => new_resource.dest_ports.to_s,
75 :source_ports => new_resource.source_ports.to_s,
76 :rate_limit => new_resource.rate_limit,
77 :connection_limit => new_resource.connection_limit.to_s,
78 :helper => new_resource.helper
81 if new_resource.family.nil?
82 node.default[:networking][:firewall][:inet] << rule
83 node.default[:networking][:firewall][:inet6] << rule
84 elsif new_resource.family.to_s == "inet"
85 node.default[:networking][:firewall][:inet] << rule
86 elsif new_resource.family.to_s == "inet6"
87 node.default[:networking][:firewall][:inet6] << rule
89 log "Unsupported network family" do
95 def add_nftables_rule(action, family)
100 when "inet6" then "ip6"
103 proto = case new_resource.proto
104 when "udp" then "udp"
105 when "tcp", "tcp:syn" then "tcp"
108 if new_resource.source_ports != "-"
109 rule << "#{proto} sport { #{nftables_source_ports} }"
112 if new_resource.dest_ports != "-"
113 rule << "#{proto} dport { #{nftables_dest_ports} }"
116 if new_resource.source == "osm"
117 rule << "#{ip} saddr @#{ip}-osm-addresses"
118 elsif new_resource.source =~ /^net:(.*)$/
119 addresses = Regexp.last_match(1).split(",").join(", ")
121 rule << "#{ip} saddr { #{addresses} }"
124 if new_resource.dest == "osm"
125 rule << "#{ip} daddr @#{ip}-osm-addresses"
126 elsif new_resource.dest =~ /^net:(.*)$/
127 addresses = Regexp.last_match(1).split(",").join(", ")
129 rule << "#{ip} daddr { #{addresses} }"
132 if new_resource.proto == "tcp:syn"
133 rule << "ct state new"
136 if new_resource.connection_limit != "-"
137 set = "connlimit-#{new_resource.rule}-#{ip}"
139 node.default[:networking][:firewall][:sets] << set
141 rule << "add @#{set} { #{ip} saddr ct count #{new_resource.connection_limit} }"
144 if new_resource.rate_limit =~ %r{^s:(\d+)/sec:(\d+)$}
145 set = "ratelimit-#{new_resource.rule}-#{ip}"
146 rate = Regexp.last_match(1)
147 burst = Regexp.last_match(2)
149 node.default[:networking][:firewall][:sets] << set
151 rule << "update @#{set} { #{ip} saddr limit rate #{rate}/second burst #{burst} packets }"
155 when :accept then "accept"
156 when :drop then "jump log-and-drop"
157 when :reject then "jump log-and-reject"
160 if new_resource.source == "fw"
161 node.default[:networking][:firewall][:outgoing] << rule.join(" ")
162 elsif new_resource.dest == "fw"
163 node.default[:networking][:firewall][:incoming] << rule.join(" ")
167 def nftables_source_ports
168 new_resource.source_ports.to_s.sub(/:$/, "-65535").gsub(":", "-")
171 def nftables_dest_ports
172 new_resource.dest_ports.to_s.sub(/:$/, "-65535").gsub(":", "-")