]> review.fuel-infra Code Review - puppet-modules/puppetlabs-firewall.git/commitdiff
(#10025) Add support for --tcp-flags
authorThomas Vander Stichele <thomas (at) apestaart (dot) org>
Sun, 4 Mar 2012 17:16:20 +0000 (18:16 +0100)
committerKen Barber <ken@bob.sh>
Sat, 9 Jun 2012 23:12:46 +0000 (00:12 +0100)
lib/puppet/provider/firewall/iptables.rb
lib/puppet/type/firewall.rb
spec/fixtures/iptables/conversion_hash.rb

index f4de3544b7a13f0884a2254dfcde112ce4456b07..5602e1d69dfafa040f8e06fa7a259ef0a861799a 100644 (file)
@@ -44,6 +44,7 @@ Puppet::Type.type(:firewall).provide :iptables, :parent => Puppet::Provider::Fir
     :state => "-m state --state",
     :sport => "-m multiport --sports",
     :table => "-t",
+    :tcp_flags => "-m tcp --tcp-flags",
     :todest => "--to-destination",
     :toports => "--to-ports",
     :tosource => "--to-source",
@@ -56,7 +57,7 @@ Puppet::Type.type(:firewall).provide :iptables, :parent => Puppet::Provider::Fir
   # changes between puppet runs, the changed rules will be re-applied again.
   # This order can be determined by going through iptables source code or just tweaking and trying manually
   @resource_list = [:table, :source, :destination, :iniface, :outiface,
-    :proto, :gid, :uid, :sport, :dport, :port, :name, :state, :icmp, :limit, :burst,
+    :proto, :tcp_flags, :gid, :uid, :sport, :dport, :port, :name, :state, :icmp, :limit, :burst,
     :jump, :todest, :tosource, :toports, :log_level, :log_prefix, :reject, :set_mark]
 
   def insert
@@ -115,6 +116,10 @@ Puppet::Type.type(:firewall).provide :iptables, :parent => Puppet::Provider::Fir
     keys = []
     values = line.dup
 
+    # --tcp-flags takes two values; we cheat by adding " around it
+    # so it behaves like --comment
+    values = values.sub(/--tcp-flags (\S*) (\S*)/, '--tcp-flags "\1 \2"')
+
     @resource_list.reverse.each do |k|
       if values.slice!(/\s#{@resource_map[k]}/)
         keys << k
@@ -247,7 +252,14 @@ Puppet::Type.type(:firewall).provide :iptables, :parent => Puppet::Provider::Fir
         end
       end
 
-      if resource_value.is_a?(Array)
+      # our tcp_flags takes a single string with comma lists separated
+      # by space
+      # --tcp-flags expects two arguments
+      if res == :tcp_flags
+        one, two = resource_value.split(' ')
+        args << one
+        args << two
+      elsif resource_value.is_a?(Array)
         args << resource_value.join(',')
       else
         args << resource_value
index 59db2eaa739cf9943feb7199938938587714f2ca..11997a0fa4120898d30a196200538f55c97d9337 100644 (file)
@@ -212,6 +212,22 @@ Puppet::Type.newtype(:firewall) do
     defaultto "tcp"
   end
 
+  # tcp-specific
+  newproperty(:tcp_flags) do
+    desc <<-EOS
+      Match when the TCP flags are as specified.
+      Is a string with a list of comma-separated flag names for the mask,
+      then a space, then a comma-separated list of flags that should be set.
+      The flags are: SYN ACK FIN RST URG PSH ALL NONE
+      Note that you specify them in the order that iptables --list-rules
+      would list them to avoid having puppet think you changed the flags.
+      Example: FIN,SYN,RST,ACK SYN matches packets with the SYN bit set and the
+              ACK,RST and FIN bits cleared.  Such packets are used to request
+               TCP  connection initiation.
+    EOS
+  end
+
+
   # Iptables specific
   newproperty(:chain, :required_features => :iptables) do
     desc <<-EOS
index 053c1e4dc3378070cd5681d5425eaaf64cdacbb2..3b59cd51364b512f037addd24204189779b562a7 100644 (file)
@@ -85,6 +85,23 @@ ARGS_TO_HASH = {
       :sport => ["15","512-1024"],
     },
   },
+  'tcp_flags_1' => {
+    :line => '-A INPUT -p tcp -m tcp --tcp-flags SYN,RST,ACK,FIN SYN -m comment --comment "000 initiation"',
+    :table => 'filter',
+    :compare_all => true,
+    :chain => 'INPUT',
+    :proto => 'tcp',
+    :params => {
+      :chain => "INPUT",
+      :ensure => :present,
+      :line => '-A INPUT -p tcp -m tcp --tcp-flags SYN,RST,ACK,FIN SYN -m comment --comment "000 initiation"',
+      :name => "000 initiation",
+      :proto => "tcp",
+      :provider => "iptables",
+      :table => "filter",
+      :tcp_flags => "SYN,RST,ACK,FIN SYN",
+    },
+  },
   'state_returns_sorted_values' => {
     :line => '-A INPUT -m state --state INVALID,RELATED,ESTABLISHED',
     :table => 'filter',
@@ -324,6 +341,15 @@ HASH_TO_ARGS = {
     },
     :args => ["-t", :filter, "-p", :tcp, "-m", "multiport", "--dports", "15,512:1024", "-m", "comment", "--comment", "100 sport range"],
   },
+  'tcp_flags_1' => {
+    :params => {
+      :name => "000 initiation",
+      :tcp_flags => "SYN,RST,ACK,FIN SYN",
+      :table => "filter",
+    },
+
+    :args => ["-t", :filter, "-p", :tcp, "-m", "tcp", "--tcp-flags", "SYN,RST,ACK,FIN", "SYN", "-m", "comment", "--comment", "000 initiation",]
+  },
   'states_set_from_array' => {
     :params => {
       :name => "100 states_set_from_array",