]> git.openstreetmap.org Git - chef.git/blobdiff - cookbooks/networking/resources/firewall_rule.rb
Expire rate limit sets
[chef.git] / cookbooks / networking / resources / firewall_rule.rb
index e72a3df7883d7da389dc50c3302f77dfdc087431..75d73dc11ebb8dc217004a0a75e8194940057246 100644 (file)
@@ -106,15 +106,15 @@ action_class do
             end
 
     if new_resource.source_ports != "-"
-      rule << "#{proto} sport { #{new_resource.source_ports} }"
+      rule << "#{proto} sport { #{nftables_source_ports} }"
     end
 
     if new_resource.dest_ports != "-"
-      rule << "#{proto} dport { #{new_resource.dest_ports} }"
+      rule << "#{proto} dport { #{nftables_dest_ports} }"
     end
 
     if new_resource.source == "osm"
-      rule << "#{ip} saddr { $#{ip}-osm-addresses }"
+      rule << "#{ip} saddr @#{ip}-osm-addresses"
     elsif new_resource.source =~ /^net:(.*)$/
       addresses = Regexp.last_match(1).split(",").join(", ")
 
@@ -122,7 +122,7 @@ action_class do
     end
 
     if new_resource.dest == "osm"
-      rule << "#{ip} daddr $#{ip}-osm-addresses"
+      rule << "#{ip} daddr @#{ip}-osm-addresses"
     elsif new_resource.dest =~ /^net:(.*)$/
       addresses = Regexp.last_match(1).split(",").join(", ")
 
@@ -134,17 +134,21 @@ action_class do
     end
 
     if new_resource.connection_limit != "-"
-      rule << "ct count #{new_resource.connection_limit}"
+      set = "connlimit-#{new_resource.rule}-#{ip}"
+
+      node.default[:networking][:firewall][:sets] << set
+
+      rule << "add @#{set} { #{ip} saddr ct count #{new_resource.connection_limit} }"
     end
 
     if new_resource.rate_limit =~ %r{^s:(\d+)/sec:(\d+)$}
-      set = "#{new_resource.rule}-#{ip}"
+      set = "ratelimit-#{new_resource.rule}-#{ip}"
       rate = Regexp.last_match(1)
       burst = Regexp.last_match(2)
 
       node.default[:networking][:firewall][:sets] << set
 
-      rule << "add @#{set} { #{ip} saddr limit rate #{rate}/second burst #{burst} packets }"
+      rule << "update @#{set} { #{ip} saddr limit rate #{rate}/second burst #{burst} packets }"
     end
 
     rule << case action
@@ -159,4 +163,12 @@ action_class do
       node.default[:networking][:firewall][:incoming] << rule.join(" ")
     end
   end
+
+  def nftables_source_ports
+    new_resource.source_ports.to_s.sub(/:$/, "-65535").gsub(":", "-")
+  end
+
+  def nftables_dest_ports
+    new_resource.dest_ports.to_s.sub(/:$/, "-65535").gsub(":", "-")
+  end
 end