]> git.openstreetmap.org Git - chef.git/commitdiff
Generalise configuration of firewall sets
authorTom Hughes <tom@compton.nu>
Sat, 11 Mar 2023 14:45:43 +0000 (14:45 +0000)
committerTom Hughes <tom@compton.nu>
Sat, 11 Mar 2023 14:47:01 +0000 (14:47 +0000)
cookbooks/networking/resources/firewall_rule.rb
cookbooks/networking/templates/default/nftables.conf.erb

index 4cda0486bbbdcea894eaff7bbbe837fbcbe5c0e1..7d7d45b7ab9b9dab044877cb3b6e9f9d19afc40e 100644 (file)
@@ -75,7 +75,9 @@ action_class do
     if new_resource.connection_limit
       set = "connlimit-#{new_resource.rule}-#{ip}"
 
-      node.default[:networking][:firewall][:sets] << set
+      node.default[:networking][:firewall][:sets] << {
+        :name => set, :type => set_type(ip), :flags => %w[dynamic]
+      }
 
       rule << "add @#{set} { #{ip} saddr ct count #{new_resource.connection_limit} }"
     end
@@ -85,7 +87,9 @@ action_class do
       rate = Regexp.last_match(1)
       burst = Regexp.last_match(2)
 
-      node.default[:networking][:firewall][:sets] << set
+      node.default[:networking][:firewall][:sets] << {
+        :name => set, :type => set_type(ip), :flags => %w[dynamic], :timeout => 120
+      }
 
       rule << "update @#{set} { #{ip} saddr limit rate #{rate}/second burst #{burst} packets }"
     end
@@ -134,4 +138,11 @@ action_class do
       "{ #{Array(addresses).map(&:to_s).join(', ')} }"
     end
   end
+
+  def set_type(ip)
+    case ip
+    when "ip" then "ipv4_addr"
+    when "ip6" then "ipv6_addr"
+    end
+  end
 end
index 4a16fb0985df84bb0263d398f2b6b09384510876..74d104fadae5cf9e38f7f698bd148c8f3d9175c3 100644 (file)
@@ -47,15 +47,13 @@ table inet chef-filter {
   }
 
 <% node[:networking][:firewall][:sets].each do |set| -%>
-  set <%= set %> {
-<% if set.end_with?("-ip") -%>
-    type ipv4_addr
-<% elsif set.end_with?("-ip6") -%>
-    type ipv6_addr
+  set <%= set[:name] %> {
+    type <%= set[:type] %>
+<% if set[:flags] -%>
+    flags <%= set[:flags].join(", ") %>
 <% end -%>
-    flags dynamic
-<% unless set.start_with?("connlimit-") -%>
-    timeout 120s
+<% if set[:timeout] -%>
+    timeout <%= set[:timeout] %>s
 <% end -%>
   }