Class: Inventory::SecurityGroup::Open

Inherits:
Base
  • Object
show all
Includes:
Shared
Defined in:
lib/inventory/security_group/open.rb

Instance Method Summary collapse

Methods included from Shared

#unused_security_groups, #used_security_groups

Methods inherited from Base

eager_load!, inherited, #initialize, #report, #show, #sort, subclasses, #test_mode

Methods included from AwsServices

#acm, #cfn, #cw, #eb, #ec2, #ecs, #elbv1, #elbv2, #iam, #pricing, #rds, #route53

Methods included from Inventory::Shared

#instances, #security_groups

Constructor Details

This class inherits a constructor from Inventory::Base

Instance Method Details

#combine_ports(port_objects) ⇒ Object

Examples

Input:

ports: [80, 443]

Output:

ports: [80, 443

Input:

ports: [8001, 8000..8002]

Output:

ports: [8000..8002]


67
68
69
70
71
72
73
74
# File 'lib/inventory/security_group/open.rb', line 67

def combine_ports(port_objects)
  ports = port_objects.inject([]) do |array, port|
    ports = port.is_a?(Range) ? port.to_a : [port]
    array += ports
    array
  end.uniq
  ports.arrange
end

#dataObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/inventory/security_group/open.rb', line 11

def data
  opened_security_groups_in_use = opened_security_groups.select do |sg|
    group_ids_in_use = used_security_groups.map(&:group_id)
    group_ids_in_use.include?(sg.group_id)
  end

  # Only display used security groups that have opened ports for review.
  # will delete the unused security groups anyway.
  opened_security_groups_in_use.map do |sg|
    ports = ports_open_to_world(sg)
    [
      sg.group_name,
      ports
    ]
  end
end

#headerObject



7
8
9
# File 'lib/inventory/security_group/open.rb', line 7

def header
  ["Security Group", "Open to World"]
end

#opened_security_groupsObject



28
29
30
31
32
33
# File 'lib/inventory/security_group/open.rb', line 28

def opened_security_groups
  security_groups.select do |sg|
    ports = ports_open_to_world(sg)
    !ports.empty?
  end
end

#ports_open_to_world(sg) ⇒ Object

Returns an Array of ports with a cidr of 0.0.0.0/0



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/inventory/security_group/open.rb', line 36

def ports_open_to_world(sg)
  ip_permissions = sg.ip_permissions.select do |permission|
      permission.ip_ranges.detect do |ip_range|
        ip_range.include?('0.0.0.0/0')
      end
    end

  ports = ip_permissions.map do |p|
    if p.from_port == p.to_port
      p.from_port
    else
      (p.from_port..p.to_port)
    end
  end

  ports = combine_ports(ports)
  # convert to string for printing
  ports.map(&:to_s).join(', ')
end