Class: Egi::Fedcloud::Cloudhound::GocdbSite

Inherits:
Object
  • Object
show all
Defined in:
lib/egi/fedcloud/cloudhound/gocdb_site.rb

Constant Summary collapse

INVALID_RANGES =
%w(255.255.255.255/0.0.0.0 0.0.0.0/0.0.0.0)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element) ⇒ GocdbSite



11
12
13
14
15
16
17
18
19
20
# File 'lib/egi/fedcloud/cloudhound/gocdb_site.rb', line 11

def initialize(element)
  Egi::Fedcloud::Cloudhound::Log.debug "[#{self.class}] Initializing with #{element.inspect}"
  @name = self.class.extract_text(element.locate('SHORT_NAME'))
  @csirt_email = self.class.extract_text(element.locate('CSIRT_EMAIL'))
  @contact_email = self.class.extract_text(element.locate('CONTACT_EMAIL'))
  @ngi = self.class.extract_text(element.locate('ROC'))

  Egi::Fedcloud::Cloudhound::Log.debug "[#{self.class}] Extracting IP ranges for site #{@name}"
  @ip_ranges = self.class.extract_ranges(self.class.extract_text(element.locate('SITE_IP')))
end

Instance Attribute Details

#contact_emailObject (readonly)

Returns the value of attribute contact_email.



8
9
10
# File 'lib/egi/fedcloud/cloudhound/gocdb_site.rb', line 8

def contact_email
  @contact_email
end

#csirt_emailObject (readonly)

Returns the value of attribute csirt_email.



8
9
10
# File 'lib/egi/fedcloud/cloudhound/gocdb_site.rb', line 8

def csirt_email
  @csirt_email
end

#ip_rangesObject (readonly)

Returns the value of attribute ip_ranges.



8
9
10
# File 'lib/egi/fedcloud/cloudhound/gocdb_site.rb', line 8

def ip_ranges
  @ip_ranges
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/egi/fedcloud/cloudhound/gocdb_site.rb', line 8

def name
  @name
end

#ngiObject (readonly)

Returns the value of attribute ngi.



8
9
10
# File 'lib/egi/fedcloud/cloudhound/gocdb_site.rb', line 8

def ngi
  @ngi
end

Class Method Details

.extract_ranges(text_ranges) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/egi/fedcloud/cloudhound/gocdb_site.rb', line 47

def extract_ranges(text_ranges)
  ranges = text_ranges.split(/[,;]/).reject { |el| el.blank? || !el.include?('/') || INVALID_RANGES.include?(el) }

  Egi::Fedcloud::Cloudhound::Log.debug "[#{self}] -- Ranges: #{ranges.inspect}"
  ranges.map! do |range|
    begin
      IPAddr.new(range)
    rescue
      Egi::Fedcloud::Cloudhound::Log.warn "[#{self}] -- Invalid range #{range.inspect}"
      nil
    end
  end
  ranges.compact!

  ranges
end

.extract_text(element) ⇒ Object



65
66
67
# File 'lib/egi/fedcloud/cloudhound/gocdb_site.rb', line 65

def extract_text(element)
  (element && element.first) ? element.first.text : ''
end

Instance Method Details

#contactsObject



35
36
37
38
39
40
41
42
43
# File 'lib/egi/fedcloud/cloudhound/gocdb_site.rb', line 35

def contacts
  {
    name: name,
    type: nil,
    status: nil,
    csirt: csirt_email,
    ngi: ngi
  }
end

#in_range?(ip) ⇒ Boolean



23
24
25
26
27
28
29
30
31
32
# File 'lib/egi/fedcloud/cloudhound/gocdb_site.rb', line 23

def in_range?(ip)
  ip = ip.to_s if ip.kind_of?(IPAddr)

  Egi::Fedcloud::Cloudhound::Log.debug "[#{self.class}] Checking #{ip.inspect} against #{name}'s ranges"
  ip_ranges.each do |my_range|
    Egi::Fedcloud::Cloudhound::Log.debug "[#{self.class}] -- #{my_range.inspect}"
    return true if my_range.include?(ip)
  end
  false
end