| 1 | package VM::EC2::SecurityGroup::IpPermission; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | VM::EC2::SecurityGroup::IpPermission - Object describing a firewall rule in an EC2 security group. | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | $ec2 = VM::EC2->new(...); | ||
| 10 | $sg = $ec2->describe_security_groups(-name=>'My Group'); | ||
| 11 | |||
| 12 | my @rules = $sg->ipPermissions; | ||
| 13 | for my $rule (@rules) { # each rule is a VM::EC2::SecurityGroup::IpPermission | ||
| 14 | $protocol = $rule->ipProtocol; | ||
| 15 | $fromPort = $rule->fromPort; | ||
| 16 | $toPort = $rule->toPort; | ||
| 17 | @ranges = $rule->ipRanges; | ||
| 18 | @groups = $rule->groups; | ||
| 19 | } | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | This object is used to describe the firewall rules defined within an | ||
| 24 | Amazon EC2 security group. It is returned by the | ||
| 25 | L<VM::EC2::SecurityGroup> object's ipPermissions() and | ||
| 26 | ipPermissionsEgress() methods (these are also known as | ||
| 27 | inbound_permissions() and outbound_permissions()). | ||
| 28 | |||
| 29 | =head1 METHODS | ||
| 30 | |||
| 31 | =cut | ||
| 32 | |||
| 33 | |||
| 34 | use strict; | ||
| 35 | use base 'VM::EC2::Generic'; | ||
| 36 | use VM::EC2::SecurityGroup::GroupPermission; | ||
| 37 | |||
| 38 | =head2 $protocol = $rule->ipProtocol | ||
| 39 | |||
| 40 | Return the IP protocol for this rule: one of "tcp", "udp" or "icmp". | ||
| 41 | |||
| 42 | =head2 $port = $rule->fromPort | ||
| 43 | |||
| 44 | Start of the port range defined by this rule, or the ICMP type | ||
| 45 | code. This will be a numeric value, like 80, or -1 to indicate all | ||
| 46 | ports/codes. | ||
| 47 | |||
| 48 | =head2 $port = $rule->toPort | ||
| 49 | |||
| 50 | End of the port range defined by this rule, or the ICMP type | ||
| 51 | code. This will be a numeric value, like 80, or -1 to indicate all | ||
| 52 | ports/codes. | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | sub valid_fields { | ||
| 57 | qw(ipProtocol fromPort toPort groups ipRanges); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub short_name { | ||
| 61 | my $s = shift; | ||
| 62 | local $^W = 0; | ||
| 63 | my $from = ($s->ipRanges && (' FROM CIDR '.join(',',sort $s->ipRanges))) . | ||
| 64 | ($s->groups && (' GRPNAME '.join(',', sort $s->groups))); | ||
| 65 | sprintf("%s(%s..%s)%s",$s->ipProtocol,$s->fromPort,$s->toPort,$from); | ||
| 66 | } | ||
| 67 | |||
| 68 | =head2 @ips = $rule->ipRanges | ||
| 69 | |||
| 70 | This method will return a list of the IP addresses that are allowed to | ||
| 71 | originate or receive traffic, provided that the rule defines IP-based | ||
| 72 | firewall filtering. | ||
| 73 | |||
| 74 | Each address is a CIDR (classless internet domain routing) address in | ||
| 75 | the form a.b.c.d/n, such as 10.23.91.0/24 | ||
| 76 | (http://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing). The | ||
| 77 | "any" address is in the form 0.0.0.0/0. | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | sub ipRanges { | ||
| 82 | my $self = shift; | ||
| 83 | my $r = $self->SUPER::ipRanges or return; | ||
| 84 | return map {$_->{cidrIp}} @{$r->{item}}; | ||
| 85 | } | ||
| 86 | |||
| 87 | =head2 @groups = $rule->groups | ||
| 88 | |||
| 89 | This method will return a list of the security groups that are allowed | ||
| 90 | to originate or receive traffic from instances assigned to this | ||
| 91 | security group, provided that the rule defines group-based traffic | ||
| 92 | filtering. | ||
| 93 | |||
| 94 | Each returned object is a L<VM::EC2::SecurityGroup::GroupPermission>, | ||
| 95 | not a L<VM::EC2::SecurityGroup>. The reason for this is that these | ||
| 96 | traffic filtering groups can include security groups owned by other | ||
| 97 | accounts | ||
| 98 | |||
| 99 | The GroupPermission objects define the methods userId(), groupId() and | ||
| 100 | groupName(). | ||
| 101 | |||
| 102 | =cut | ||
| 103 | |||
| 104 | sub groups { | ||
| 105 | my $self = shift; | ||
| 106 | my $g = $self->SUPER::groups or return; | ||
| 107 | my @g = map { VM::EC2::SecurityGroup::GroupPermission->new($_,$self->aws) } @{$g->{item}}; | ||
| 108 | foreach (@g) {$_->ownerId($self->ownerId)}; | ||
| 109 | return @g; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub ownerId { | ||
| 113 | my $self = shift; | ||
| 114 | my $d = $self->{ownerId}; | ||
| 115 | $self->{ownerId} = shift if @_; | ||
| 116 | $d; | ||
| 117 | } | ||
| 118 | |||
| 119 | =head1 STRING OVERLOADING | ||
| 120 | |||
| 121 | When used in a string context, this object will interpolate the rule | ||
| 122 | using the following templates: | ||
| 123 | |||
| 124 | TCP port 22 open to any host: | ||
| 125 | |||
| 126 | "tcp(22..22) FROM CIDR 0.0.0.0/0" | ||
| 127 | |||
| 128 | TCP ports 23 through 39 open to the two class C networks 192.168.0.* | ||
| 129 | and 192.168.1.*: | ||
| 130 | |||
| 131 | "tcp(23..29) FROM CIDR 192.168.0.0/24,192.168.1.0/24" | ||
| 132 | |||
| 133 | UDP port 80 from security group "default" owned by you and the group | ||
| 134 | named "farmville" owned by user 9999999: | ||
| 135 | |||
| 136 | "udp(80..80) GRPNAME default,9999999/farmville" | ||
| 137 | |||
| 138 | =head1 SEE ALSO | ||
| 139 | |||
| 140 | L<VM::EC2> | ||
| 141 | L<VM::EC2::Generic> | ||
| 142 | L<VM::EC2::Instance> | ||
| 143 | L<VM::EC2::SecurityGroup> | ||
| 144 | L<VM::EC2::SecurityGroup::IpPermission> | ||
| 145 | L<VM::EC2::SecurityGroup::GroupPermission> | ||
| 146 | |||
| 147 | =head1 AUTHOR | ||
| 148 | |||
| 149 | Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>. | ||
| 150 | |||
| 151 | Copyright (c) 2011 Ontario Institute for Cancer Research | ||
| 152 | |||
| 153 | This package and its accompanying libraries is free software; you can | ||
| 154 | redistribute it and/or modify it under the terms of the GPL (either | ||
| 155 | version 1, or at your option, any later version) or the Artistic | ||
| 156 | License 2.0. Refer to LICENSE for the full license text. In addition, | ||
| 157 | please see DISCLAIMER.txt for disclaimers of warranty. | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | |||
| 162 | 1; | ||
| 163 |