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.
22 resource_name :firewall_rule
23 provides :firewall_rule
27 default_action :nothing
29 property :rule, :kind_of => String, :name_property => true
30 property :context, :kind_of => Symbol, :required => true, :is => [:incoming, :outgoing]
31 property :protocol, :kind_of => Symbol, :required => true, :is => [:udp, :tcp]
32 property :source, :kind_of => [String, Symbol, Array]
33 property :dest, :kind_of => [String, Symbol, Array]
34 property :dest_ports, :kind_of => [String, Integer, Array]
35 property :source_ports, :kind_of => [String, Integer, Array]
36 property :rate_limit, :kind_of => String
37 property :connection_limit, :kind_of => [String, Integer]
38 property :helper, :kind_of => String
40 property :compile_time, TrueClass, :default => true
43 add_rule(:accept, "ip")
44 add_rule(:accept, "ip6")
49 add_rule(:drop, "ip6")
53 add_rule(:reject, "ip")
54 add_rule(:reject, "ip6")
58 def add_rule(action, ip)
61 protocol = new_resource.protocol.to_s
63 source = addresses(new_resource.source, ip)
64 dest = addresses(new_resource.dest, ip)
66 return if new_resource.source && source.empty?
67 return if new_resource.dest && dest.empty?
69 rule << "#{protocol} sport #{format_ports(new_resource.source_ports)}" if new_resource.source_ports
70 rule << "#{protocol} dport #{format_ports(new_resource.dest_ports)}" if new_resource.dest_ports
71 rule << "#{ip} saddr #{format_addresses(source, ip)}" if new_resource.source
72 rule << "#{ip} daddr #{format_addresses(dest, ip)}" if new_resource.dest
73 rule << "ct state new" if new_resource.protocol == :tcp
75 if new_resource.connection_limit
76 set = "connlimit-#{new_resource.rule}-#{ip}"
78 node.default[:networking][:firewall][:sets] << {
79 :name => set, :type => set_type(ip), :flags => %w[dynamic]
82 rule << "add @#{set} { #{ip} saddr ct count #{new_resource.connection_limit} }"
85 if new_resource.rate_limit =~ %r{^s:(\d+)/sec:(\d+)$}
86 set = "ratelimit-#{new_resource.rule}-#{ip}"
87 rate = Regexp.last_match(1)
88 burst = Regexp.last_match(2)
90 node.default[:networking][:firewall][:sets] << {
91 :name => set, :type => set_type(ip), :flags => %w[dynamic], :timeout => 120
94 rule << "update @#{set} { #{ip} saddr limit rate #{rate}/second burst #{burst} packets }"
97 if new_resource.helper
98 helper = "#{new_resource.rule}-#{new_resource.helper}"
100 node.default[:networking][:firewall][:helpers] << {
101 :name => helper, :helper => new_resource.helper, :protocol => protocol
104 rule << "ct helper set #{helper}"
108 when :accept then "accept"
109 when :drop then "jump log-and-drop"
110 when :reject then "jump log-and-reject"
113 node.default[:networking][:firewall][new_resource.context] << rule.join(" ")
116 def addresses(addresses, ip)
117 if addresses.is_a?(Symbol)
120 Array(addresses).map do |address|
121 if ip == "ip" && IPAddr.new(address.to_s).ipv4?
123 elsif ip == "ip6" && IPAddr.new(address.to_s).ipv6?
130 def format_ports(ports)
131 "{ #{Array(ports).map(&:to_s).join(', ')} }"
134 def format_addresses(addresses, ip)
135 if addresses.is_a?(Symbol)
136 "@#{ip}-#{addresses}-addresses"
138 "{ #{Array(addresses).map(&:to_s).join(', ')} }"
144 when "ip" then "ipv4_addr"
145 when "ip6" then "ipv6_addr"