Class: PuppetX::SIMP::IPTables

Inherits:
Object
  • Object
show all
Defined in:
lib/puppetx/simp/iptables.rb

Defined Under Namespace

Classes: Rule

Instance Method Summary collapse

Constructor Details

#initialize(rules_str) ⇒ IPTables

Returns a new instance of IPTables.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/puppetx/simp/iptables.rb', line 7

def initialize(rules_str)
  # The @tables Hash is a way to reference arrays of rules based on the
  # table that contains the rule.

  @tables = { }

  new_rules = []

  current_table = nil
  rules_str.chomp.split("\n").each do |rule|
    # Skip Inline Comments
    next if rule =~ /^\s*(#.*)?$/

    if rule =~ /^\s*\*/
      # New tables means new rules
      @tables[current_table][:rules] = sort_rules(new_rules) unless new_rules.empty?

      current_table = add_table(rule)

      new_rules = []

      next
    end

    if current_table
      rule = PuppetX::SIMP::IPTables::Rule.new(rule, current_table)

      if rule
        if rule.rule_type == :chain
          @tables[current_table][:chains] << rule
        elsif rule.rule_type == :rule
          new_rules << rule
        end
      end
    end
  end

  # Need to sort the last set of rules in the stack
  @tables[current_table][:rules] = sort_rules(new_rules) unless new_rules.empty?

  prune_chains!
end

Instance Method Details

#==(to_cmp) ⇒ Object

A comparator for two rulesets



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/puppetx/simp/iptables.rb', line 51

def ==(to_cmp)
  # Fast matching
  unless tables == to_cmp.tables
    Puppet.debug('Tables differ between rulesets')
    return false
  end

  unless report == to_cmp.report
    Puppet.debug('Reports differ between rulesets')
    return false
  end

  # Comprehensive search
  tables.each do |table|
    unless rules(table) == to_cmp.rules(table)
      Puppet.debug("Rules in table '#{table}' differ")
      return false
    end
  end
end

#add_chains(table, new_chains) ⇒ Object

Add chains to the passed table

Parameters:

  • table (String)

    The table to which to add the chains

  • new_chains (Array[<PuppetX::SIMP::IPTables::Rule, String>])

    Chains that should be added to the table



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/puppetx/simp/iptables.rb', line 165

def add_chains(table, new_chains)
  Array(new_chains).each do |chain|
    if chain.is_a?(String)
      if chain[0].chr == ':'
        chain = PuppetX::SIMP::IPTables::Rule.new(chain, table)
      else
        chain = PuppetX::SIMP::IPTables::Rule.new(':' + chain + ' - [0:0]', table)
      end
    end

    if chain.is_a?(PuppetX::SIMP::IPTables::Rule)
      process_chain(chain)

      if !chains(table).find { |x| x.chain == chain.chain }
        @tables[table][:chains].push(chain)
      end
    else
      fail "chain must be a PuppetX::SIMP::IPTables::Rule or a String not #{chain.class}"
    end
  end
end

#add_table(table) ⇒ String

Add the passed ‘table’ to the list of tables

Parameters:

  • table (String)

Returns:

  • (String)

    The passed table



136
137
138
139
140
141
142
143
144
# File 'lib/puppetx/simp/iptables.rb', line 136

def add_table(table)
  if table =~ /^\s*(\*.*)/
    @tables[$1.strip] ||= { :chains => [], :rules => [] }
  else
    fail "Table '#{table}' must start with a '*'"
  end

  return table
end

#append_rules(table, new_rules) ⇒ Object

Append rules to the existing rules while preserving chain order

Will not add a duplicate rule to the chain

Parameters:

  • table (String)

    The table to which to add the chains

  • new_rules (Array[<PuppetX::SIMP::IPTables::Rule, String>])

    Rules that should be appended to the table



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/puppetx/simp/iptables.rb', line 265

def append_rules(table, new_rules)
  Array(new_rules).each do |rule|
    if rule.is_a?(String)
      rule = PuppetX::SIMP::IPTables::Rule.new(rule)
    end

    if rule.is_a?(PuppetX::SIMP::IPTables::Rule)
      process_rule(rule)

      if @tables[table][:rules].empty?
        @tables[table][:rules] << rule
      else
        end_index = @tables[table][:rules].rindex{|x| x.chain == rule.chain} || -2

        unless rule == @tables[table][:rules][end_index]
          end_index = end_index + 1

          @tables[table][:rules].insert(end_index, rule)
        end
      end
    else
      fail "rule must be a PuppetX::SIMP::IPTables::Rule not #{rule.class}"
    end
  end
end

#chains(table) ⇒ Array[PuppetX::SIMP::IPTables::Rule]

Return all ‘chains’ for the given table

Parameters:

  • table (String)

    The table from which to return the chains

Returns:



153
154
155
# File 'lib/puppetx/simp/iptables.rb', line 153

def chains(table)
  return @tables[table][:chains]
end

#include_rule?(table, rule) ⇒ Boolean

Return whether or not the table contains a matching rule

Parameters:

Returns:

  • (Boolean)

    Boolean



301
302
303
304
305
306
307
# File 'lib/puppetx/simp/iptables.rb', line 301

def include_rule?(table, rule)
  rules(table).each do |current_rule|
    return true if current_rule == rule
  end

  return false
end

#merge(iptables_obj) ⇒ PuppetX::SIMP::IPTables

Merge the passed IPTables object with the current object

This involves adding all chains from the passed object and prepending all rules to the existing rules.

Parameters:

Returns:



109
110
111
# File 'lib/puppetx/simp/iptables.rb', line 109

def merge(iptables_obj)
  return PuppetX::SIMP::IPTables.new(to_s).merge!(iptables_obj)
end

#merge!(iptables_obj) ⇒ Object

The same behavior as ‘merge` but affecting the current object

Parameters:



118
119
120
121
122
123
124
125
126
127
# File 'lib/puppetx/simp/iptables.rb', line 118

def merge!(iptables_obj)
  iptables_obj.tables.each do |table|
    add_chains(table, iptables_obj.chains(table))
    prepend_rules(table, iptables_obj.rules(table))
  end

  prune_chains!

  return self
end

#optimizeObject

Optimize all of the rules and return an optimized object

Optimizations include:

* Elimination of duplicate repeated rules
* Collection of ports into multiport matches in rules where the rest
  of the rule is identical

Returns:

  • PuppetX::SIMP::IPTables



442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# File 'lib/puppetx/simp/iptables.rb', line 442

def optimize
  new_rules = PuppetX::SIMP::IPTables.new('')

  # Hard coded limit in iptables multiport rules.
  max_ports = 15

  tables.each do |table|
    new_rules.add_table(table)
    new_rules.add_chains(table, chains(table))

    existing_rules = rules(table)

    pending_rule = nil

    existing_rules.each_with_index do |rule, index|
      if pending_rule
        prev_ports = []
        new_ports = []
        prev_multiport = false
        port_type = nil

        prev_rule = PuppetX::SIMP::IPTables::Rule.new(
          pending_rule.to_s.split(/\s+-/).delete_if{ |x|
            retval = false
            if x.empty?
              retval = true
            elsif x =~ /m(ulti)?port/
              prev_multiport = true
              retval = true
            elsif x =~ /(d(estination-)?|s(ource-)?)ports?\s+(.*)/
              port_type = $1[0].chr
              prev_ports += $4.split(',')
              retval = true
            end
            retval
          }.join(' -'), table
        )

        new_rule = PuppetX::SIMP::IPTables::Rule.new(
          rule.to_s.split(/\s+-/).delete_if{ |x|
            retval = false
            if x.empty? || x =~ /m(ulti)?port/
              retval = true
            elsif x =~ /(d(estination-)?|s(ource-)?)ports?\s+(.*)/
              # Add ranges as sub-arrays.
              new_ports += $4.split(',')
              retval = true
            end
            retval
          }.join(' -'), table
        )

        if new_rule == prev_rule
          Puppet.debug("Rule:\n  #{new_rule} matches\n  #{prev_rule}")
          # Flatten when comparing sizes to account for ranges.
          new_ports = (prev_ports + new_ports).uniq

          slice_array(new_ports, max_ports).each do |sub_ports|
            pending_rule = PuppetX::SIMP::IPTables::Rule.new(prev_rule.dup.insert(-2,"m multiport --#{port_type}ports #{sub_ports.sort.uniq.join(',')}").join(' -'))

            # If on the last rule, just write it out
            if (index == (existing_rules.count - 1))
              Puppet.debug("Adding: rule '#{pending_rule}' to table '#{table}'")
              new_rules.append_rules(table, pending_rule)
            end
          end
        else
          Puppet.debug("No match for: #{rule}")

          Puppet.debug("Adding: pending rule '#{pending_rule}' to table '#{table}'")
          new_rules.append_rules(table, pending_rule)

          Puppet.debug("Adding: rule '#{rule}' to table '#{table}'")
          new_rules.append_rules(table, rule)

          pending_rule = nil
        end
      else
        # Make sure we have a valid rule for multiport compression.
        #
        # Ignore if we're the last rule since this is only used for
        # pending rule optimization
        if (index != (existing_rules.count - 1)) &&
           rule.to_s !~ /-p(rotocol)?\s+(ud|tc)p/ &&
           rule.to_s !~ /--(d(estination-)?|s(ource-)?)ports?\s+/
        then
          pending_rule = rule
        else
          Puppet.debug("Adding: rule '#{rule}' to table '#{table}'")
          new_rules.append_rules(table, rule)

          pending_rule = nil
        end
      end
    end
  end

  return new_rules.prune_chains!
end

#prepend_rules(table, new_rules) ⇒ Object

Prepend rules to the existing rules while preserving chain order

Parameters:

  • table (String)

    The table to which to add the chains

  • new_rules (Array[<PuppetX::SIMP::IPTables::Rule, String>])

    Rules that should be prepended to the table



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/puppetx/simp/iptables.rb', line 231

def prepend_rules(table, new_rules)
  Array(new_rules).each do |rule|
    if rule.is_a?(String)
      rule = PuppetX::SIMP::IPTables::Rule.new(rule)
    end

    if rule.is_a?(PuppetX::SIMP::IPTables::Rule)
      process_rule(rule)

      if @tables[table][:rules].empty?
        @tables[table][:rules] << rule
      else
        start_index = @tables[table][:rules].index{|x| x.chain == rule.chain} || 0

        unless rule == @tables[table][:rules][start_index]
          @tables[table][:rules].insert(start_index, rule)
        end
      end
    else
      fail "rule must be a PuppetX::SIMP::IPTables::Rule not #{rule.class}"
    end
  end
end

#preserve_match(regex = [], components = ['chain', 'jump', 'input_interface', 'output_interface']) ⇒ Object

Identify rules to be preserved from all tables

Parameters:

  • regex (Array[Regexp]) (defaults to: [])

    A list of regular expressions that should be matched against

  • components (Array[String]) (defaults to: ['chain', 'jump', 'input_interface', 'output_interface'])

    A list of the rule subcomponents that you want to match against all or any of the regular expressions

Returns:

  • PuppetX::SIMP::IPTables



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/puppetx/simp/iptables.rb', line 340

def preserve_match(regex = [], components = ['chain', 'jump', 'input_interface', 'output_interface'])
  result = PuppetX::SIMP::IPTables.new('')

  @tables.each_key do |table|

    # Preserve all existing chains
    result.add_chains(table, chains(table))

    rules(table).each do |rule|
      # Ignore all rules dropped by SIMP
      next if (rule.rule_hash['comment'] && rule.rule_hash['comment'][:value].start_with?('SIMP:'))

      Array(regex).each do |cmp|
        Array(components).each do |component|
          val = rule.send(component)

          if cmp.match(val)
            result.prepend_rules(table, rule)
          end
        end
      end
    end
  end

  return result
end

#prune_chains!Object

Remove any chains from the tables that do not have a matching rule



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/puppetx/simp/iptables.rb', line 188

def prune_chains!
  all_chains = @tables.collect { |key, value|
    @tables[key][:rules]
  }.
  flatten.map { |rule|
    new_rule = [rule.chain]
    new_rule << rule.jump if rule.jump
  }.flatten.uniq

  tables.each do |table|
    chains_to_keep = []

    chains(table).each do |chain|
      if all_chains.include?(chain.chain)
        chains_to_keep << chain
      end
    end

    @tables[table][:chains] = chains_to_keep
  end

  return self
end

#report(to_ignore = [], enable_tracking = true) ⇒ Hash

Produces a hash-based report on the number of iptables rules, and type of operation in a given chain.

The report hash is simply a key to count match of each of the different rule types for ease of comparison.

You may optionally pass an array of compiled regular expressions. If this array is present, all items with an interface, chain, or jump matching the regex will be ignored.

Parameters:

  • to_ignore (Array[Regexp]) (defaults to: [])

    Regular expressions that should be ignored

Returns:

  • (Hash)


382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/puppetx/simp/iptables.rb', line 382

def report(to_ignore=[], enable_tracking=true)
  result = {}

  tables.each do |table|
    result[table] ||= {}

    rules(table).each do |rule|
      ignore_rule = false
      to_ignore.each do |ignore|
        if [rule.chain, rule.jump, rule.input_interface, rule.output_interface].find {|x| ignore.match(x)}
         ignore_rule = true
         break
        end
      end

      next if ignore_rule

      result[table][rule.chain] ||= {}

      # Need a unique key target for precise matches
      tgt_key = [rule.input_interface, rule.output_interface, rule.jump].compact.join('|')

      unless tgt_key.empty?
        result[table][rule.chain][tgt_key] ||= {}
        result[table][rule.chain][tgt_key][:count] ||= 0
        result[table][rule.chain][tgt_key][:count] += 1

        # Better effort matching that just counts which should pick up
        # basic changes but isn't near perfect

        # These get stripped out by the underlying iptables
        listen_all = [ '0.0.0.0/0', '::/0', '::0/0' ]
        rule_parts = rule.to_s.scan(%r{(?:\s(?:tcp|udp|udplite|icmp|esp|ah|sctp|mh|(?:\d\.|\S:).+?/\d+)|-\S+?port.+?\d+)(?:\s|$)}).map(&:strip) - listen_all

        unless rule_parts.empty?
          result[table][rule.chain][:parts] ||= Set.new
          result[table][rule.chain][:parts] = Set.new(rule_parts)
        end

        if enable_tracking
          # Best, but fragile, matching
          result[table][rule.chain][:tracking] ||= Set.new
          result[table][rule.chain][:tracking] << rule.rule_hash
        end
      end
    end
  end

  result
end

#rules(table) ⇒ Array[PuppetX::SIMP::IPTables::Rule]

Return all ‘rules’ for the given table

Parameters:

  • table (String)

    The table from which to return the rules

Returns:



219
220
221
# File 'lib/puppetx/simp/iptables.rb', line 219

def rules(table)
  return @tables[table][:rules]
end

#sort_rules(to_sort) ⇒ Array[PuppetX::SIMP::IPTables::Rule]

Sort a list of rules into the same order that ‘iptables-save` uses on output

Parameters:

Returns:



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/puppetx/simp/iptables.rb', line 79

def sort_rules(to_sort)
  ordered_chains = to_sort.collect{|r| r.chain}.uniq

  ordered_rules = []

  ordered_chains.each do |chain|
    ordered_rules += to_sort.select{|r| r.chain == chain}
  end

  return ordered_rules
end

#tablesArray[String]

Return all IPTables ‘tables’

Returns:

  • (Array[String])


95
96
97
# File 'lib/puppetx/simp/iptables.rb', line 95

def tables
  return @tables.keys.sort
end

#to_sString

Return a string that is ready for processing by the ‘iptables-restore` command

Returns:

  • (String)


314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/puppetx/simp/iptables.rb', line 314

def to_s
  result = []

  tables.each do |table|
    result << table

    result += chains(table).map(&:rule)
    result += rules(table).map(&:rule)

    result << 'COMMIT'
  end

  result.join("\n")
end