Class: PuppetX::SIMP::IPTables::Rule
- Inherits:
-
Object
- Object
- PuppetX::SIMP::IPTables::Rule
- Defined in:
- lib/puppetx/simp/iptables/rule.rb
Instance Attribute Summary collapse
-
#chain ⇒ Object
readonly
Returns the value of attribute chain.
-
#complex ⇒ Object
readonly
This is true if the rule has more than just a jump in it.
-
#input_interface ⇒ Object
readonly
Returns the value of attribute input_interface.
-
#jump ⇒ Object
readonly
Returns the value of attribute jump.
-
#output_interface ⇒ Object
readonly
Returns the value of attribute output_interface.
-
#rule ⇒ Object
readonly
Returns the value of attribute rule.
-
#rule_hash ⇒ Object
readonly
Returns the value of attribute rule_hash.
-
#rule_type ⇒ Object
readonly
Returns the value of attribute rule_type.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(rule_str, table) ⇒ Rule
constructor
Create the particular rule.
-
#normalize_addresses(to_normalize) ⇒ Object
Retained for backward compatibiilty.
- #to_s ⇒ Object
Constructor Details
#initialize(rule_str, table) ⇒ Rule
Create the particular rule. The containing table should be passed in for future reference.
119 120 121 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 |
# File 'lib/puppetx/simp/iptables/rule.rb', line 119 def initialize(rule_str, table) @rule = rule_str.strip @rule_type = :rule if table.nil? || table.empty? 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' @rule_type = :commit elsif @rule =~ %r{^\s*:(.*)\s+(.*)\s} @chain = ::Regexp.last_match(1) @rule = ":#{@chain} #{::Regexp.last_match(2)} [0:0]" @rule_type = :chain end # If there is only a jump, then the rule is simple return unless (parsed_rule[:rule_hash].keys - ['A', 'D', 'I', 'R', 'N', 'P', 'j']).empty? @complex = false end |
Instance Attribute Details
#chain ⇒ Object (readonly)
Returns the value of attribute chain.
6 7 8 |
# File 'lib/puppetx/simp/iptables/rule.rb', line 6 def chain @chain end |
#complex ⇒ Object (readonly)
This is true if the rule has more than just a jump in it.
13 14 15 |
# File 'lib/puppetx/simp/iptables/rule.rb', line 13 def complex @complex end |
#input_interface ⇒ Object (readonly)
Returns the value of attribute input_interface.
8 9 10 |
# File 'lib/puppetx/simp/iptables/rule.rb', line 8 def input_interface @input_interface end |
#jump ⇒ Object (readonly)
Returns the value of attribute jump.
7 8 9 |
# File 'lib/puppetx/simp/iptables/rule.rb', line 7 def jump @jump end |
#output_interface ⇒ Object (readonly)
Returns the value of attribute output_interface.
9 10 11 |
# File 'lib/puppetx/simp/iptables/rule.rb', line 9 def output_interface @output_interface end |
#rule ⇒ Object (readonly)
Returns the value of attribute rule.
3 4 5 |
# File 'lib/puppetx/simp/iptables/rule.rb', line 3 def rule @rule end |
#rule_hash ⇒ Object (readonly)
Returns the value of attribute rule_hash.
10 11 12 |
# File 'lib/puppetx/simp/iptables/rule.rb', line 10 def rule_hash @rule_hash end |
#rule_type ⇒ Object (readonly)
Returns the value of attribute rule_type.
4 5 6 |
# File 'lib/puppetx/simp/iptables/rule.rb', line 4 def rule_type @rule_type end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
5 6 7 |
# File 'lib/puppetx/simp/iptables/rule.rb', line 5 def table @table end |
Class Method Details
.normalize_addresses(to_normalize) ⇒ Object
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 |
# File 'lib/puppetx/simp/iptables/rule.rb', line 15 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}" # rubocop:disable Lint/ShadowedException rescue ArgumentError, NoMethodError, IPAddr::InvalidAddressError normalized_array << item end # rubocop:enable Lint/ShadowedException else normalized_array << item end end return normalized_array.first if normalized_array.size == 1 normalized_array end |
.parse(rule) ⇒ Object
88 89 90 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 |
# File 'lib/puppetx/simp/iptables/rule.rb', line 88 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 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 |
# 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 = {} negate = false until opt_arr.empty? begin opt_parser.parse!(opt_arr) opt_arr.shift rescue OptionParser::InvalidOption => e e.recover(opt_arr) key = opt_arr.shift.gsub(%r{^-*}, '') value = [] value << opt_arr.shift while opt_arr.first && (opt_arr.first[0] != '-') 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 opts end |
Instance Method Details
#==(other) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/puppetx/simp/iptables/rule.rb', line 161 def ==(other) return false if other.nil? || other.rule_hash.nil? || other.rule_hash.empty? return true if rule.strip == other.to_s.strip return false if @rule_hash.size != other.rule_hash.size local_hash = Marshal.load(Marshal.dump(@rule_hash)) other_hash = Marshal.load(Marshal.dump(other.rule_hash)) local_hash == other_hash end |
#normalize_addresses(to_normalize) ⇒ Object
Retained for backward compatibiilty
157 158 159 |
# File 'lib/puppetx/simp/iptables/rule.rb', line 157 def normalize_addresses(to_normalize) self.class.normalize_addresses(to_normalize) end |
#to_s ⇒ Object
152 153 154 |
# File 'lib/puppetx/simp/iptables/rule.rb', line 152 def to_s @rule end |