Represents a security group in EC2.
@attr_reader [String] description The short informal description
given when the group was created.
@attr_reader [String] name The name of the security group.
@attr_reader [String] owner_id The security group owner’s id.
@attr_reader [String,nil] vpc_id If this is a VPC security group,
vpc_id is the ID of the VPC this group was created in. Returns false otherwise.
alias for ingress permissions
@return [String] The ID of the security group.
@return [String] The ID of the security group.
@private
# File lib/aws/ec2/security_group.rb, line 325 def self.describe_call_name :describe_security_groups end
# File lib/aws/ec2/security_group.rb, line 41 def initialize id, options = {} @id = id super end
Adds ingress rules for ICMP pings. Defaults to 0.0.0.0/0 for the list of allowed IP ranges the ping can come from.
security_group.allow_ping # anyone can ping servers in this group # only allow ping from a particular address security_group.allow_ping('123.123.123.123/0')
@param [String] ip_ranges One or more IP ranges to allow ping from.
Defaults to 0.0.0.0/0
@return [nil]
# File lib/aws/ec2/security_group.rb, line 112 def allow_ping *sources sources << '0.0.0.0/0' if sources.empty? authorize_ingress('icmp', -1, *sources) end
Deletes this security group.
If you attempt to delete a security group that contains instances, or attempt to delete a security group that is referenced by another security group, an error is raised. For example, if security group B has a rule that allows access from security group A, security group A cannot be deleted until the rule is removed. @return [nil]
# File lib/aws/ec2/security_group.rb, line 309 def delete client.delete_security_group(:group_id => id) nil end
# File lib/aws/ec2/security_group.rb, line 328 def describe_call_name; self.class.describe_call_name; end
Removes ingress rules for ICMP pings. Defaults to 0.0.0.0/0 for the list of IP ranges to revoke.
@param [String] ip_ranges One or more IP ranges to allow ping from.
Defaults to 0.0.0.0/0
@return [nil]
# File lib/aws/ec2/security_group.rb, line 125 def disallow_ping *sources sources << '0.0.0.0/0' if sources.empty? revoke_ingress('icmp', -1, *sources) end
@return [SecurityGroup::EgressIpPermissionCollection] Returns a
collection of {IpPermission} objects that represents all of the egress permissions this security group has authorizations for.
# File lib/aws/ec2/security_group.rb, line 95 def egress_ip_permissions EgressIpPermissionCollection.new(self, :config => config) end
@return [Boolean] True if the security group exists.
# File lib/aws/ec2/security_group.rb, line 68 def exists? client.describe_security_groups(:filters => [ { :name => "group-id", :values => [id] } ]).security_group_index.key?(id) end
@private
# File lib/aws/ec2/security_group.rb, line 320 def inflected_name "group" end
@return [SecurityGroup::IngressIpPermissionCollection] Returns a
collection of {IpPermission} objects that represents all of the (ingress) permissions this security group has authorizations for.
# File lib/aws/ec2/security_group.rb, line 87 def ingress_ip_permissions IngressIpPermissionCollection.new(self, :config => config) end
@private
# File lib/aws/ec2/security_group.rb, line 315 def resource_type 'security-group' end
Revokes an egress (outound) ip permission. This is the inverse operation to {authorize_egress}. See {authorize_egress} for param and option documentation.
@see authorize_egress
@return [nil]
# File lib/aws/ec2/security_group.rb, line 293 def revoke_egress *sources client.revoke_security_group_egress( :group_id => id, :ip_permissions => [egress_opts(sources)]) nil end
Revokes an ingress (inbound) ip permission. This is the inverse operation to {authorize_ingress}. See {authorize_ingress} for param and option documentation.
@see authorize_ingress
@return [nil]
# File lib/aws/ec2/security_group.rb, line 241 def revoke_ingress protocol, ports, *sources client.revoke_security_group_ingress( :group_id => id, :ip_permissions => [ingress_opts(protocol, ports, sources)] ) nil end
Returns true if this security group is a VPC security group and not an EC2 security group. VPC security groups belong to a VPC subnet and can have egress rules. @return [Boolean] Returns true if this is a VPC security group and
false if this is an EC2 security group.
# File lib/aws/ec2/security_group.rb, line 79 def vpc? vpc_id ? true : false end
@private
# File lib/aws/ec2/security_group.rb, line 350 def egress_opts args ensure_vpc do last = args.last if last.is_a?(Hash) and (last.key?(:protocol) or last.key?(:ports)) # hashes at the end of egress methods could be a hash intedned # to be a source, like: # # { :group_id => ..., :user_id => ... } # options = args.pop else options = {} end opts = {} opts[:ip_protocol] = [nil,:any, '-1'].include?(options[:protocol]) ? '-1' : options[:protocol].to_s.downcase if options[:ports] opts[:from_port] = Array(options[:ports]).first.to_i opts[:to_port] = Array(options[:ports]).last.to_i end ips, groups = parse_sources(args) opts[:ip_ranges] = ips unless ips.empty? opts[:user_id_group_pairs] = groups unless groups.empty? opts end end
@private
# File lib/aws/ec2/security_group.rb, line 435 def ensure_vpc &block raise 'operation permitted for VPC security groups only' unless vpc? yield end
@private
# File lib/aws/ec2/security_group.rb, line 442 def find_in_response(resp) resp.security_group_index[id] end
@private
# File lib/aws/ec2/security_group.rb, line 332 def ingress_opts protocol, ports, sources opts = {} opts[:ip_protocol] = protocol.to_s.downcase opts[:from_port] = Array(ports).first.to_i opts[:to_port] = Array(ports).last.to_i ips, groups = parse_sources(sources) opts[:ip_ranges] = ips unless ips.empty? opts[:user_id_group_pairs] = groups unless groups.empty? opts end
@private
# File lib/aws/ec2/security_group.rb, line 388 def parse_sources sources ips = [] groups = [] sources.each do |source| case source when String ips << { :cidr_ip => source } when SecurityGroup groups << { :group_id => source.id, :user_id => source.owner_id } when ELB::LoadBalancer groups << source.source_security_group when Hash # group name or id required unless source.has_key?(:group_id) or source.has_key?(:group_name) raise ArgumentError, 'invalid ip permission hash, ' + 'must provide :group_id or :group_name' end # prevent typos unless source.keys - [:group_id, :group_name, :user_id] == [] raise ArgumentError, 'invalid ip permission hash, ' + 'only accepts the following keys, :group_id, :group_name, :user_id' end groups << source else raise ArgumentError, 'invalid ingress ip permission, ' + 'expected CIDR IP address or SecurityGroup' end end ips << { :cidr_ip => '0.0.0.0/0' } if ips.empty? and groups.empty? [ips, groups] end