Class: AwSec::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/aw_sec/core.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.secure(group_names, public_ip, options = {}) ⇒ Object



6
7
8
9
# File 'lib/aw_sec/core.rb', line 6

def self.secure(group_names, public_ip, options = {})
  client = AwSec::Core.new
  client.secure(group_names, public_ip, options)
end

Instance Method Details

#connObject



100
101
102
103
104
105
106
107
# File 'lib/aw_sec/core.rb', line 100

def conn
  @conn ||= Fog::Compute.new({
    :provider => 'AWS',
  	:region => @region,
  	:aws_access_key_id => @aws_key,
  	:aws_secret_access_key => @aws_secret
  	})
end

#get_groups(group_names) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/aw_sec/core.rb', line 63

def get_groups(group_names)
  groups = []
  group_names.each do |group_name|
    groups << conn.security_groups.get(group_name)
  end
  
  groups
end

#is_authorized?(group, ip) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
93
94
# File 'lib/aw_sec/core.rb', line 87

def is_authorized?(group, ip)
	return group.ip_permissions.detect do |ip_permission|
		ip_permission['ipRanges'].first && ip_permission['ipRanges'].first['cidrIp'] == ip &&
			ip_permission['fromPort'] == port &&
			ip_permission['ipProtocol'] == 'tcp' &&
			ip_permission['toPort'] == port
	end
end

#list_ips(group) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/aw_sec/core.rb', line 50

def list_ips(group)
  result = []
	group.ip_permissions.detect do |ip_permission|
		result << ip_permission['ipRanges'].collect{ |i| i["cidrIp"] } if ip_permission["toPort"] == port
	end

  result.flatten!
end

#portObject



96
97
98
# File 'lib/aw_sec/core.rb', line 96

def port
  @port
end

#revoke_access(group, ip) ⇒ Object



59
60
61
# File 'lib/aw_sec/core.rb', line 59

def revoke_access(group, ip)
  group.revoke_port_range(port..port, :cidr_ip => ip)
end

#safe_authorize_port(group, ip) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/aw_sec/core.rb', line 72

def safe_authorize_port(group, ip)
	if group.ip_permissions.nil?
		authorized = false
	else
		authorized = is_authorized?(group, ip)
	end
	unless authorized
    begin
      group.authorize_port_range(port..port, :cidr_ip => ip)
    rescue => exc
      puts "Failed #{exc.message}"
    end
	end
end

#secure(group_names, public_ip, options = {}) ⇒ Object



11
12
13
14
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
44
45
46
47
48
# File 'lib/aw_sec/core.rb', line 11

def secure(group_names, public_ip, options = {})
  public_ip = public_ip
  @port = options[:port] || 22
  @region = options[:aws_region] 
  @aws_key = options[:aws_key] 
  @aws_secret = options[:aws_secret] 
  revoke_all = options.has_key?(:revoke_all) ? options[:revoke_all] : true
  wtlist = options[:whitelist] || []
  
  whitelist = []
  public_ip = "#{public_ip}/32" unless public_ip =~  /\//
  wtlist.each do |ip|
    whitelist << "#{ip}/32" unless ip =~  /\//
  end
  
  puts "Connecting AWS..."
  groups = get_groups(group_names)
  groups.each do |group|
    puts "Configuring #{group.name}"
    granted_ips = list_ips(group) || []
    puts "Existing IPs with access to port #{port}: #{granted_ips.join(',')}"
    allowed_ips = granted_ips.select { |i| whitelist.include? i }
    allowed_ips << public_ip
    if revoke_all
      granted_ips.each do |ip|
        unless allowed_ips.include? ip
          puts "Revoking access to #{ip}"
          revoke_access(group, ip)
        end
      end
    end
    granted_ips.uniq!
    allowed_ips.each do |ip|
      puts "Granting access to port #{port} to #{ip}"
      safe_authorize_port(group, ip)
    end
  end
end