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] << set
80 rule << "add @#{set} { #{ip} saddr ct count #{new_resource.connection_limit} }"
83 if new_resource.rate_limit =~ %r{^s:(\d+)/sec:(\d+)$}
84 set = "ratelimit-#{new_resource.rule}-#{ip}"
85 rate = Regexp.last_match(1)
86 burst = Regexp.last_match(2)
88 node.default[:networking][:firewall][:sets] << set
90 rule << "update @#{set} { #{ip} saddr limit rate #{rate}/second burst #{burst} packets }"
93 if new_resource.helper
94 helper = "#{new_resource.rule}-#{new_resource.helper}"
96 node.default[:networking][:firewall][:helpers] << {
97 :name => helper, :helper => new_resource.helper, :protocol => protocol
100 rule << "ct helper set #{helper}"
104 when :accept then "accept"
105 when :drop then "jump log-and-drop"
106 when :reject then "jump log-and-reject"
109 node.default[:networking][:firewall][new_resource.context] << rule.join(" ")
112 def addresses(addresses, ip)
113 if addresses.is_a?(Symbol)
116 Array(addresses).map do |address|
117 if ip == "ip" && IPAddr.new(address).ipv4?
119 elsif ip == "ip6" && IPAddr.new(address).ipv6?
126 def format_ports(ports)
127 "{ #{Array(ports).map(&:to_s).join(', ')} }"
130 def format_addresses(addresses, ip)
131 if addresses.is_a?(Symbol)
132 "@#{ip}-#{addresses}-addresses"
134 "{ #{Array(addresses).map(&:to_s).join(', ')} }"