| 1 | package VM::EC2::SecurityGroup; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | VM::EC2::SecurityGroup - Object describing an Amazon EC2 security group | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use VM::EC2; | ||
| 10 | |||
| 11 | $ec2 = VM::EC2->new(...); | ||
| 12 | @sg = $ec2->describe_security_groups; | ||
| 13 | for my $sg (@sg) { | ||
| 14 | $name = $sg->groupName; | ||
| 15 | $id = $sg->groupId; | ||
| 16 | $desc = $sg->groupDescription; | ||
| 17 | $tags = $sg->tags; | ||
| 18 | @inbound_permissions = $sg->ipPermissions; | ||
| 19 | @outbound_permissions = $sg->ipPermissionsEgress; | ||
| 20 | for $i (@inbound_permissions) { | ||
| 21 | $protocol = $i->ipProtocol; | ||
| 22 | $fromPort = $i->fromPort; | ||
| 23 | $toPort = $i->toPort; | ||
| 24 | @ranges = $i->ipRanges; | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | $sg = $sg[0]; | ||
| 29 | |||
| 30 | # Add a new security rule | ||
| 31 | $sg->authorize_incoming(-protocol => 'tcp', | ||
| 32 | -port => 80, | ||
| 33 | -source_ip => ['192.168.2.0/24','192.168.2.1/24'}); | ||
| 34 | |||
| 35 | # write it to AWS. | ||
| 36 | $sg->update(); | ||
| 37 | |||
| 38 | =head1 DESCRIPTION | ||
| 39 | |||
| 40 | This object is used to describe an Amazon EC2 security group. It is | ||
| 41 | returned by VM::EC2->describe_security_groups(). You may also obtain | ||
| 42 | this object by calling an Instance object's groups() method, and then | ||
| 43 | invoking one of the group's permissions() method. See | ||
| 44 | L<VM::EC2::Group>. | ||
| 45 | |||
| 46 | =head1 METHODS | ||
| 47 | |||
| 48 | The following object methods are supported: | ||
| 49 | |||
| 50 | ownerId -- Owner of this security group | ||
| 51 | groupId -- ID of this security group | ||
| 52 | groupName -- Name of this security group | ||
| 53 | groupDescription -- Description of this group | ||
| 54 | vpcId -- Virtual Private Cloud ID, if applicable | ||
| 55 | ipPermissions -- A list of rules that govern incoming connections | ||
| 56 | to instances running under this security group. | ||
| 57 | Each rule is a | ||
| 58 | L<VM::EC2::SecurityGroup::IpPermission> object. | ||
| 59 | ipPermissionsEgress -- A list of rules that govern outgoing connections | ||
| 60 | from instances running under this security group. | ||
| 61 | Each rule is a | ||
| 62 | L<VM::EC2::SecurityGroup::IpPermission object>. | ||
| 63 | This field is only valid for VPC groups. | ||
| 64 | tags -- Hashref containing tags associated with this group. | ||
| 65 | See L<VM::EC2::Generic>. | ||
| 66 | |||
| 67 | For convenience, the following aliases are provided for commonly used methods: | ||
| 68 | |||
| 69 | inbound_permissions -- same as ipPermissions() | ||
| 70 | outbound_permissions -- same as ipPermissionsEgress() | ||
| 71 | name -- same as groupName() | ||
| 72 | |||
| 73 | See L<VM::EC2::SecurityGroup::IpPermission> for details on accessing | ||
| 74 | port numbers, IP ranges and other fields associated with incoming and | ||
| 75 | outgoing firewall rules. | ||
| 76 | |||
| 77 | =head1 MODIFYING FIREWALL RULES | ||
| 78 | |||
| 79 | To add or revoke firewall rules, call the authorize_incoming, | ||
| 80 | authorize_outgoing, revoke_incoming or revoke_outgoing() methods | ||
| 81 | one or more times. Each of these methods either adds or removes a | ||
| 82 | single firewall rule. After adding or revoking the desired rules, call | ||
| 83 | update() to write the modified group back to Amazon. The object will | ||
| 84 | change to reflect the new permissions. | ||
| 85 | |||
| 86 | =head2 $permission = $group->authorize_incoming(%args) | ||
| 87 | |||
| 88 | Add a rule for incoming firewall traffic. Arguments are as follows: | ||
| 89 | |||
| 90 | -protocol The protocol, either a string (tcp,udp,icmp) or | ||
| 91 | the corresponding protocol number (6, 17, 1). | ||
| 92 | Use -1 to indicate all protocols. (required) | ||
| 93 | |||
| 94 | -port, -ports The port or port range. When referring to a single | ||
| 95 | port, you may use either the port number or the | ||
| 96 | service name (e.g. "ssh"). For this to work the | ||
| 97 | service name must be located in /etc/services. | ||
| 98 | When specifying a port range, use "start..end" as | ||
| 99 | in "8000..9000". Note that this is a string that | ||
| 100 | contains two dots, and not two numbers separated | ||
| 101 | by the perl range operator. For the icmp protocol, | ||
| 102 | this argument corresponds to the ICMP type number. | ||
| 103 | (required). | ||
| 104 | |||
| 105 | -group, -groups Security groups to authorize. Instances that belong | ||
| 106 | to the named security groups will be allowed | ||
| 107 | access. You may specify either a single group or | ||
| 108 | a list of groups as an arrayref. The following | ||
| 109 | syntaxes are recognized: | ||
| 110 | |||
| 111 | "sg-12345" authorize group with this groupId | ||
| 112 | "12345/my group" authorize group named "my group" | ||
| 113 | owned by user 12345 | ||
| 114 | "my group" authorize group named "my group" | ||
| 115 | owned by yourself | ||
| 116 | |||
| 117 | -source, -source_ip Authorize incoming traffic from an IP address, IP | ||
| 118 | address range, or set of such ranges. IP | ||
| 119 | addresses use the CIDR notation of a.b.c.d/mask, | ||
| 120 | as described in | ||
| 121 | http://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing. | ||
| 122 | Pass an arrayref to simultaneously authorize | ||
| 123 | multiple CIDR ranges. | ||
| 124 | |||
| 125 | The result of this call is a L<VM::EC2::SecurityGroup::IpPermission> | ||
| 126 | object corresponding to the rule you defined. Note that the rule is | ||
| 127 | not written to Amazon until you call update(). | ||
| 128 | |||
| 129 | Here are some examples: | ||
| 130 | |||
| 131 | $sg->authorize_incoming(-protocol => 'tcp', | ||
| 132 | -port => 80, | ||
| 133 | -source_ip => ['192.168.2.0/24','192.168.2.1/24'}); | ||
| 134 | |||
| 135 | # TCP on ports 22 and 23 from anyone | ||
| 136 | $sg->authorize_incoming(-protocol => 'tcp', | ||
| 137 | -port => '22..23', | ||
| 138 | -source_ip => '0.0.0.0/0'); | ||
| 139 | |||
| 140 | # ICMP on echo (ping) port from anyone | ||
| 141 | $sg->authorize_incoming(-protocol => 'icmp', | ||
| 142 | -port => -1, | ||
| 143 | -source_ip => '0.0.0.0/0'); | ||
| 144 | |||
| 145 | # TCP to port 25 (mail) from instances belonging to | ||
| 146 | # the "Mail relay" group belonging to user 12345678. | ||
| 147 | $sg->authorize_incoming(-protocol => 'tcp', | ||
| 148 | -port => 25, | ||
| 149 | -group => '12345678/Mail relay'); | ||
| 150 | |||
| 151 | =head2 $permission = $group->authorize_outgoing(%args) | ||
| 152 | |||
| 153 | This is identical to authorize_incoming() except that the rule applies | ||
| 154 | to outbound traffic. Only VPC security groups can define outgoing | ||
| 155 | firewall rules. | ||
| 156 | |||
| 157 | =head2 $permission = $group->revoke_incoming($rule) | ||
| 158 | |||
| 159 | =head2 $permission = $group->revoke_incoming(%args) | ||
| 160 | |||
| 161 | This method revokes an incoming firewall rule. You can call it with a | ||
| 162 | single argument consisting of a | ||
| 163 | L<VM::EC2::SecurityGroup::IpPermission> object in order to revoke that | ||
| 164 | rule. Alternatively, when called with the named arguments listed for | ||
| 165 | authorize_incoming(), it will attempt to match an existing rule to the | ||
| 166 | provided arguments and queue it for deletion. | ||
| 167 | |||
| 168 | Here is an example of revoking all rules that allow ssh (port 22) | ||
| 169 | access: | ||
| 170 | |||
| 171 | @ssh_rules = grep {$_->fromPort == 22} $group->ipPermissions; | ||
| 172 | $group->revoke_incoming($_) foreach @ssh_rules; | ||
| 173 | $group->update(); | ||
| 174 | |||
| 175 | =head2 $boolean = $group->update() | ||
| 176 | |||
| 177 | This method will write all queued rule authorizations and revocations | ||
| 178 | to Amazon, and return a true value if successful. The method will | ||
| 179 | return false if any of the rule updates failed. You can examine the | ||
| 180 | VM::EC2 object's error_str() method to determine what went wrong, and | ||
| 181 | check the group object's ipPermissions() method to see what firewall | ||
| 182 | rules are currently defined. | ||
| 183 | |||
| 184 | =head2 $boolean = $group->write() | ||
| 185 | |||
| 186 | An alias for update() | ||
| 187 | |||
| 188 | =head2 $group->refresh() | ||
| 189 | |||
| 190 | This method refreshes the group information from Amazon. It is called | ||
| 191 | automatically by update(). | ||
| 192 | |||
| 193 | =head1 STRING OVERLOADING | ||
| 194 | |||
| 195 | When used in a string context, this object will interpolate the | ||
| 196 | groupId. | ||
| 197 | |||
| 198 | =head1 SEE ALSO | ||
| 199 | |||
| 200 | L<VM::EC2> | ||
| 201 | L<VM::EC2::Generic> | ||
| 202 | L<VM::EC2::Instance> | ||
| 203 | L<VM::EC2::Group> | ||
| 204 | L<VM::EC2::SecurityGroup::IpPermission> | ||
| 205 | |||
| 206 | =head1 AUTHOR | ||
| 207 | |||
| 208 | Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>. | ||
| 209 | |||
| 210 | Copyright (c) 2011 Ontario Institute for Cancer Research | ||
| 211 | |||
| 212 | This package and its accompanying libraries is free software; you can | ||
| 213 | redistribute it and/or modify it under the terms of the GPL (either | ||
| 214 | version 1, or at your option, any later version) or the Artistic | ||
| 215 | License 2.0. Refer to LICENSE for the full license text. In addition, | ||
| 216 | please see DISCLAIMER.txt for disclaimers of warranty. | ||
| 217 | |||
| 218 | =cut | ||
| 219 | |||
| 220 | use strict; | ||
| 221 | use base 'VM::EC2::Generic'; | ||
| 222 | use VM::EC2 'security_group'; | ||
| 223 | use VM::EC2::SecurityGroup::IpPermission; | ||
| 224 | use Carp 'croak'; | ||
| 225 | |||
| 226 | sub valid_fields { | ||
| 227 | return qw(ownerId groupId groupName groupDescription vpcId ipPermissions ipPermissionsEgress tagSet); | ||
| 228 | } | ||
| 229 | |||
| 230 | sub primary_id { | ||
| 231 | my $self = shift; | ||
| 232 | return $self->groupId || $self->groupName; | ||
| 233 | } | ||
| 234 | |||
| 235 | sub name { shift->groupName } | ||
| 236 | |||
| 237 | sub inbound_permissions { shift->ipPermissions } | ||
| 238 | sub outbound_permissions { shift->ipPermissionsEgress } | ||
| 239 | |||
| 240 | sub ipPermissions { | ||
| 241 | my $self = shift; | ||
| 242 | my $p = $self->SUPER::ipPermissions or return; | ||
| 243 | my @p = map { VM::EC2::SecurityGroup::IpPermission->new($_, | ||
| 244 | $self->aws, | ||
| 245 | $self->xmlns, | ||
| 246 | $self->requestId) | ||
| 247 | } @{$p->{item}}; | ||
| 248 | |||
| 249 | # tell ip permissions about the owner -- needed for the | ||
| 250 | # group name string. | ||
| 251 | my $owner = $self->ownerId; | ||
| 252 | foreach (@p) {$_->ownerId($owner)} | ||
| 253 | return @p; | ||
| 254 | } | ||
| 255 | |||
| 256 | sub ipPermissionsEgress { | ||
| 257 | my $self = shift; | ||
| 258 | my $p = $self->SUPER::ipPermissionsEgress or return; | ||
| 259 | my @p = map { VM::EC2::SecurityGroup::IpPermission->new($_,$self->aws,$self->xmlns,$self->requestId)} | ||
| 260 | @{$p->{item}}; | ||
| 261 | |||
| 262 | # tell ip permissions about the owner -- needed for the | ||
| 263 | # group name string. | ||
| 264 | my $owner = $self->ownerId; | ||
| 265 | foreach (@p) {$_->ownerId($owner)} | ||
| 266 | return @p; | ||
| 267 | } | ||
| 268 | |||
| 269 | sub _uncommitted_permissions { | ||
| 270 | my $self = shift; | ||
| 271 | my ($action,$direction) = @_; # e.g. 'Authorize','Ingress' | ||
| 272 | my $perms = $self->{uncommitted}{$action}{$direction} or return; | ||
| 273 | return values %$perms; | ||
| 274 | } | ||
| 275 | |||
| 276 | sub authorize_incoming { | ||
| 277 | my $self = shift; | ||
| 278 | my $permission = $self->_new_permission(@_); | ||
| 279 | $self->{uncommitted}{Authorize}{Ingress}{$permission}=$permission; | ||
| 280 | } | ||
| 281 | |||
| 282 | sub authorize_outgoing { | ||
| 283 | my $self = shift; | ||
| 284 | my $permission = $self->_new_permission(@_); | ||
| 285 | $self->{uncommitted}{Authorize}{Egress}{$permission}=$permission; | ||
| 286 | } | ||
| 287 | |||
| 288 | sub revoke_incoming { | ||
| 289 | my $self = shift; | ||
| 290 | my $permission = $_[0] =~ /^-[A-Za-z]/ ? $self->_new_permission(@_) : shift; | ||
| 291 | if ($self->{uncommitted}{Authorize}{Ingress}{$permission}) { | ||
| 292 | delete $self->{uncommitted}{Authorize}{Ingress}{$permission}; | ||
| 293 | } | ||
| 294 | $self->{uncommitted}{Revoke}{Ingress}{$permission}=$permission; | ||
| 295 | } | ||
| 296 | |||
| 297 | sub revoke_outgoing { | ||
| 298 | my $self = shift; | ||
| 299 | my $permission = $_[0] =~ /^-[A-Za-z]/ ? $self->_new_permission(@_) : shift; | ||
| 300 | if ($self->{uncommitted}{Authorize}{Egress}{$permission}) { | ||
| 301 | delete $self->{uncommitted}{Authorize}{Egress}{$permission}; | ||
| 302 | } | ||
| 303 | $self->{uncommitted}{Revoke}{Egress}{$permission}=$permission; | ||
| 304 | } | ||
| 305 | |||
| 306 | # write permissions out to AWS | ||
| 307 | sub update { | ||
| 308 | my $self = shift; | ||
| 309 | my $aws = $self->aws; | ||
| 310 | my $result = $aws->update_security_group($self); | ||
| 311 | { | ||
| 312 | local $aws->{error}; # so we can do a double-fetch | ||
| 313 | $self->refresh; | ||
| 314 | } | ||
| 315 | return $result; | ||
| 316 | } | ||
| 317 | |||
| 318 | sub write { shift->update } | ||
| 319 | |||
| 320 | sub refresh { | ||
| 321 | my $self = shift; | ||
| 322 | my $i = $self->aws->describe_security_groups($self->groupId); | ||
| 323 | %$self = %$i if $i; | ||
| 324 | return defined $i; | ||
| 325 | } | ||
| 326 | |||
| 327 | sub _new_permission { | ||
| 328 | my $self = shift; | ||
| 329 | my %args = @_; | ||
| 330 | |||
| 331 | my $data = {}; # xml | ||
| 332 | |||
| 333 | my $protocol = lc $args{-protocol} or croak "-protocol argument required"; | ||
| 334 | $data->{ipProtocol} = $protocol; | ||
| 335 | |||
| 336 | $args{-source_ip} ||= $args{-source}; | ||
| 337 | |||
| 338 | my $ports = defined($args{-port}) ? $args{-port} : $args{-ports}; | ||
| 339 | my ($from_port,$to_port); | ||
| 340 | if ($ports =~ /^(\d+)\.\.(\d+)$/) { | ||
| 341 | $from_port = $1; | ||
| 342 | $to_port = $2; | ||
| 343 | } elsif ($ports =~ /^-?\d+$/) { | ||
| 344 | $from_port = $to_port = $ports; | ||
| 345 | } elsif (my @p = getservbyname($ports,$protocol)) { | ||
| 346 | $from_port = $to_port = $p[2]; | ||
| 347 | } else { | ||
| 348 | croak "value of -port argument not recognized"; | ||
| 349 | } | ||
| 350 | $data->{fromPort} = $from_port; | ||
| 351 | $data->{toPort} = $to_port; | ||
| 352 | |||
| 353 | my $group = $args{-groups} || $args{-group}; | ||
| 354 | my @groups = ref $group && ref $group eq 'ARRAY' ? @$group :$group ? ($group) : (); | ||
| 355 | for my $g (@groups) { | ||
| 356 | if ($g =~ /^sg-[a-f0-9]+$/) { | ||
| 357 | push @{$data->{groups}{item}},{groupId=>$g}; | ||
| 358 | } elsif (my ($userid,$groupname) = $g =~ m!(\d+)/(.+)!) { | ||
| 359 | push @{$data->{groups}{item}},{userId=>$userid,groupName=>$groupname}; | ||
| 360 | } else { | ||
| 361 | my $userid = $self->aws->account_id; | ||
| 362 | push @{$data->{groups}{item}},{userId=>$userid,groupName=>$g}; | ||
| 363 | } | ||
| 364 | } | ||
| 365 | |||
| 366 | my $address = $args{-source_ip}; | ||
| 367 | $address && $group and croak "the -source_ip and -group arguments are mutually exclusive"; | ||
| 368 | $address ||= '0.0.0.0/0' unless $group; | ||
| 369 | |||
| 370 | my @addresses = ref $address && ref $address eq 'ARRAY' ? @$address | ||
| 371 | :$address ? ($address) | ||
| 372 | : (); | ||
| 373 | foreach (@addresses) { $_ = '0.0.0.0/0' if $_ eq 'any' } | ||
| 374 | $data->{ipRanges}{item} = [map {{cidrIp=>$_}} @addresses] if @addresses; | ||
| 375 | |||
| 376 | my $sg = VM::EC2::SecurityGroup::IpPermission->new($data,$self->aws); | ||
| 377 | $sg->ownerId($self->ownerId); | ||
| 378 | return $sg; | ||
| 379 | } | ||
| 380 | |||
| 381 | |||
| 382 | 1; |