Class: AwSec::Core
- Inherits:
-
Object
- Object
- AwSec::Core
- Defined in:
- lib/aw_sec/core.rb
Class Method Summary collapse
Instance Method Summary collapse
- #conn ⇒ Object
- #get_groups(group_names) ⇒ Object
- #is_authorized?(group, ip) ⇒ Boolean
- #list_ips(group) ⇒ Object
- #port ⇒ Object
- #revoke_access(group, ip) ⇒ Object
- #safe_authorize_port(group, ip) ⇒ Object
- #secure(group_names, public_ip, options = {}) ⇒ Object
Class Method Details
Instance Method Details
#conn ⇒ Object
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
97 98 99 100 101 102 103 104 |
# File 'lib/aw_sec/core.rb', line 97 def (group, ip) return group..detect do || ['ipRanges'].first && ['ipRanges'].first['cidrIp'] == ip && ['fromPort'] == port && ['ipProtocol'] == 'tcp' && ['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..detect do || result << ['ipRanges'].collect{ |i| i["cidrIp"] } if ["toPort"] == port end result.flatten! end |
#port ⇒ Object
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 (group, ip) if group..nil? = false else = (group, ip) end unless begin group.(port..port, :cidr_ip => ip) rescue => exc if exc. =~ /InvalidPermission.Duplicate/ puts "#{ip} already has access" else puts "Failed #{exc.}" 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, = {}) public_ip = public_ip @port = [:port] || 22 @region = [:aws_region] @aws_key = [:aws_key] @aws_secret = [:aws_secret] revoke_all = .has_key?(:revoke_all) ? [:revoke_all] : true wtlist = [: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}" (group, ip) end end end |