Module: PuppetX::IntechWIFI::Network_Rules

Defined in:
lib/puppet_x/intechwifi/network_rules.rb

Class Method Summary collapse

Class Method Details

.AwsToPuppetString(data, region, &awscmd) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/puppet_x/intechwifi/network_rules.rb', line 24

def self.AwsToPuppetString(data, region, &awscmd)
  result = data.map{|gress|
      # Convert the protocol.
      protocol = self.IpProtocolToString(gress["IpProtocol"])

      #  Convert the location.
      locations = self.FormatLocation gress, region, &awscmd

      #  Convert the ports.
      ports = self.FormatPorts gress

      locations.map{|location| "#{protocol}|#{ports}|#{location}"}
  }.flatten().sort()
  return result
end

.FormatLocation(data, region, &awscmd) ⇒ Object



51
52
53
54
55
56
# File 'lib/puppet_x/intechwifi/network_rules.rb', line 51

def self.FormatLocation data, region, &awscmd
  result = []
  result << self.FormatLocationFromIpRanges(data["IpRanges"]) if data["IpRanges"].length > 0
  result << self.FormatLocationFromGroupPairs(data["UserIdGroupPairs"], region, &awscmd)  if data["UserIdGroupPairs"].length > 0
  result.flatten
end

.FormatLocationFromGroupPairs(source, region, &awscmd) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/puppet_x/intechwifi/network_rules.rb', line 76

def self.FormatLocationFromGroupPairs source, region, &awscmd
  #  Yeah, nice weather isnt it?  Lets solve this problem later.
  source.map{|location|
    location_sgid = location['GroupId']
    sg_name =  PuppetX::IntechWIFI::AwsCmds.find_name_by_id(region, 'security-group', location_sgid, &awscmd)
    "sg|#{sg_name}"
  }
end

.FormatLocationFromIpRanges(source) ⇒ Object



71
72
73
74
# File 'lib/puppet_x/intechwifi/network_rules.rb', line 71

def self.FormatLocationFromIpRanges source
  #  Take the contents of the IPRanges array and convert into a string fragment.
  source.map{|cidr| "cidr|#{cidr['CidrIp']}"}
end

.FormatPorts(data) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/puppet_x/intechwifi/network_rules.rb', line 58

def self.FormatPorts data
  from = data["FromPort"]
  to = data["ToPort"]

  if from and to and from != to
    "#{from}-#{to}"
  elsif from and to
    "#{from}"
  else
    ""
  end
end

.IpProtocolToString(source) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/puppet_x/intechwifi/network_rules.rb', line 41

def self.IpProtocolToString source
  if source == "-1"
    "all"
  else
    source
  end
end

.MakeCidr(cidr, index, total) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/puppet_x/intechwifi/network_rules.rb', line 85

def self.MakeCidr(cidr, index, total)
  cidr_array = cidr.split("/")
  cidr_base = cidr_array[0]
  cidr_range = cidr_array[1]

  # Convert the cidr_base into a number.
  cidr_integer = cidr_base.split(".").map(&:to_i).reduce(0) { |sum, num| (sum << 8) + num }

  # Calculate the size of each cidr.
  bitshift = 0
  loop do
    offset = 1 << bitshift
    break unless offset < total
    bitshift += 1
  end

  new_cidr_size = cidr_range.to_i + bitshift
  new_base = cidr_integer + (index << (32 - new_cidr_size))

  (new_base >> 24).to_s + "." + (new_base >> 16 & 0xFF).to_s + "." + (new_base >> 8 & 0xFF).to_s + "." + (new_base & 0xFF).to_s + "/" + new_cidr_size.to_s
end

.RouteRuleMatch(declared, detected) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/puppet_x/intechwifi/network_rules.rb', line 107

def self.RouteRuleMatch(declared, detected)
  should = declared.split('|')
  is = detected.split('|')

  cidr_match = (should[0] == is[0])
  target_match = ((should[1] == is[1]) and (should[2] == is[2]))
  blackhole = should[1] == 'blackhole'
  cidr_match and (target_match or blackhole)
end