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



110
111
112
113
114
115
116
117
# File 'lib/aw_sec/core.rb', line 110

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



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/aw_sec/core.rb', line 64

def get_groups(group_names)
  groups = []
			if group_names.is_a? String
to_loop = [group_names] 
			else
to_loop = group_names
			end
  to_loop.each do |group_name|
    groups << conn.security_groups.get(group_name)
  end
  
  groups
end

#is_authorized?(group, ip) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
100
101
102
103
104
# File 'lib/aw_sec/core.rb', line 97

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



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

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



106
107
108
# File 'lib/aw_sec/core.rb', line 106

def port
  @port
end

#revoke_access(group, ip) ⇒ Object



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

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

#safe_authorize_port(group, ip) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/aw_sec/core.rb', line 78

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
	if exc.message =~ /InvalidPermission.Duplicate/
		puts "#{ip} already has access" 
	else
		puts "Failed #{exc.message}"
	end
    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
49
# 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|
next if group.nil?
    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