Class: PuppetX::SIMP::IPTables::Rule

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rule_str, table) ⇒ Rule

Create the particular rule. The containing table should be passed in for future reference.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/puppetx/simp/iptables/rule.rb', line 122

def initialize(rule_str, table)
  @rule = rule_str.strip
  @rule_type = :rule

  if table.nil? or table.empty? then
    raise(Puppet::Error, "All rules must have an associated table: '#{rule}'")
  end

  @table = table.strip

  parsed_rule = PuppetX::SIMP::IPTables::Rule.parse(rule)

  @chain = parsed_rule[:chain]
  @jump = parsed_rule[:jump]
  @input_interface = parsed_rule[:input_interface]
  @output_interface = parsed_rule[:output_interface]
  @rule_hash = parsed_rule[:rule_hash]

  @complex = true

  if @rule == 'COMMIT' then
    @rule_type = :commit
  elsif @rule =~ /^\s*:(.*)\s+(.*)\s/
    @chain = $1
    @rule = ":#{@chain} #{$2} [0:0]"
    @rule_type = :chain
  end

  # If there is only a jump, then the rule is simple
  if (parsed_rule[:rule_hash].keys - ['A','D','I','R','N','P','j']).empty?
    @complex = false
  end
end

Instance Attribute Details

#chainObject (readonly)

Returns the value of attribute chain.



8
9
10
# File 'lib/puppetx/simp/iptables/rule.rb', line 8

def chain
  @chain
end

#complexObject (readonly)

This is true if the rule has more than just a jump in it.



15
16
17
# File 'lib/puppetx/simp/iptables/rule.rb', line 15

def complex
  @complex
end

#input_interfaceObject (readonly)

Returns the value of attribute input_interface.



10
11
12
# File 'lib/puppetx/simp/iptables/rule.rb', line 10

def input_interface
  @input_interface
end

#jumpObject (readonly)

Returns the value of attribute jump.



9
10
11
# File 'lib/puppetx/simp/iptables/rule.rb', line 9

def jump
  @jump
end

#output_interfaceObject (readonly)

Returns the value of attribute output_interface.



11
12
13
# File 'lib/puppetx/simp/iptables/rule.rb', line 11

def output_interface
  @output_interface
end

#ruleObject (readonly)

Returns the value of attribute rule.



5
6
7
# File 'lib/puppetx/simp/iptables/rule.rb', line 5

def rule
  @rule
end

#rule_hashObject (readonly)

Returns the value of attribute rule_hash.



12
13
14
# File 'lib/puppetx/simp/iptables/rule.rb', line 12

def rule_hash
  @rule_hash
end

#rule_typeObject (readonly)

Returns the value of attribute rule_type.



6
7
8
# File 'lib/puppetx/simp/iptables/rule.rb', line 6

def rule_type
  @rule_type
end

#tableObject (readonly)

Returns the value of attribute table.



7
8
9
# File 'lib/puppetx/simp/iptables/rule.rb', line 7

def table
  @table
end

Class Method Details

.normalize_addresses(to_normalize) ⇒ Object



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
# File 'lib/puppetx/simp/iptables/rule.rb', line 17

def self.normalize_addresses(to_normalize)
  require 'ipaddr'

  normalized_array = []

  Array(to_normalize).each do |item|
    # Short circuit if it's obviously not an IP address
    if (item.count('.') == 3) || (item.count(':') > 1)
      begin
        test_addr = IPAddr.new(item)

        # Grab the netmask from the string and assign a reasonable default
        # if one does not exist
        test_netmask = item.split('/')[1] || (test_addr.family == 2 ? '32' : '128')

        normalized_array << "#{test_addr}/#{test_netmask}"
      rescue ArgumentError, NoMethodError, IPAddr::InvalidAddressError
        normalized_array << item
      end
    else
      normalized_array << item
    end
  end

  return normalized_array.first if (normalized_array.size == 1)
  return normalized_array
end

.parse(rule) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/puppetx/simp/iptables/rule.rb', line 91

def self.parse(rule)
  output = {
    :chain => nil,
    :jump => nil,
    :input_interface => nil,
    :output_interface => nil
  }

  rule_hash = PuppetX::SIMP::IPTables::Rule.to_hash(rule)

  if rule_hash
    chain = rule_hash.find{ |k,_| ['A','D','I','R','N','P'].include?(k)}
    output[:chain] = chain.last[:value] if chain

    jump = rule_hash.find{ |k,_| ['j'].include?(k)}
    output[:jump] = jump.last[:value] if jump

    input_interface = rule_hash.find{ |k,_| ['i'].include?(k)}
    output[:input_interface] = input_interface.last[:value] if input_interface

    output_interface = rule_hash.find{ |k,_| ['o'].include?(k)}
    output[:output_interface] = output_interface.last[:value] if output_interface
  end

  output[:rule_hash] = rule_hash

  return output
end

.to_hash(rule) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/puppetx/simp/iptables/rule.rb', line 45

def self.to_hash(rule)
  require 'optparse'
  require 'shellwords'


  opt_arr = Shellwords.shellwords(rule)

  opt_parser = OptionParser.new

  opts = Hash.new
  negate = false

  until opt_arr.empty? do
    begin
      opt_parser.parse!(opt_arr)
      opt_arr.shift
    rescue OptionParser::InvalidOption => e
      e.recover(opt_arr)

      key = opt_arr.shift.gsub(/^-*/,'')
      value = []

      while opt_arr.first && (opt_arr.first[0] != '-')
        value << opt_arr.shift
      end

      negate_next = false
      if !value.empty? && (value.last.strip == '!')
        value.pop
        negate_next = true
      end

      next if !negate && ((value == ['0.0.0.0/0']) || (value == ['::/0']))

      opts[key] ||= { :value => nil, :negate => negate }
      opts[key][:value] = value.join(' ')
      opts[key][:value] = opts[key][:value].split(',').sort if opts[key][:value].include?(',')
      opts[key][:value] = normalize_addresses(opts[key][:value])

      negate = negate_next
    end
  end

  return opts
end

Instance Method Details

#==(other_rule) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/puppetx/simp/iptables/rule.rb', line 165

def ==(other_rule)
  return false if (other_rule.nil? || other_rule.rule_hash.nil? || other_rule.rule_hash.empty?)

  return true if (rule.strip == other_rule.to_s.strip)

  return false if (@rule_hash.size != other_rule.rule_hash.size)

  local_hash = Marshal.load(Marshal.dump(@rule_hash))
  other_hash = Marshal.load(Marshal.dump(other_rule.rule_hash))

  return local_hash == other_hash
end

#normalize_addresses(to_normalize) ⇒ Object

Retained for backward compatibiilty



161
162
163
# File 'lib/puppetx/simp/iptables/rule.rb', line 161

def normalize_addresses(to_normalize)
  self.class.normalize_addresses(to_normalize)
end

#to_sObject



156
157
158
# File 'lib/puppetx/simp/iptables/rule.rb', line 156

def to_s
  return @rule
end