| 1 | package VM::EC2::REST::security_group; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use VM::EC2 ''; # important not to import anything! | ||
| 5 | package VM::EC2; # add methods to VM::EC2 | ||
| 6 | |||
| 7 | VM::EC2::Dispatch->register( | ||
| 8 | DescribeSecurityGroups => 'fetch_items,securityGroupInfo,VM::EC2::SecurityGroup', | ||
| 9 | CreateSecurityGroup => 'VM::EC2::SecurityGroup', | ||
| 10 | DeleteSecurityGroup => 'boolean', | ||
| 11 | AuthorizeSecurityGroupIngress => 'boolean', | ||
| 12 | AuthorizeSecurityGroupEgress => 'boolean', | ||
| 13 | RevokeSecurityGroupIngress => 'boolean', | ||
| 14 | RevokeSecurityGroupEgress => 'boolean', | ||
| 15 | ); | ||
| 16 | |||
| 17 | =head1 NAME VM::EC2::REST::security_group | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | use VM::EC2 ':standard'; | ||
| 22 | |||
| 23 | =head1 METHODS | ||
| 24 | |||
| 25 | The methods in this section allow you to query and manipulate security | ||
| 26 | groups (firewall rules). See L<VM::EC2::SecurityGroup> for functionality | ||
| 27 | that is available through these objects. | ||
| 28 | |||
| 29 | Implemented: | ||
| 30 | AuthorizeSecurityGroupEgress (EC2-VPC only) | ||
| 31 | AuthorizeSecurityGroupIngress | ||
| 32 | CreateSecurityGroup | ||
| 33 | DeleteSecurityGroup | ||
| 34 | DescribeSecurityGroups | ||
| 35 | RevokeSecurityGroupEgress (EC2-VPC only) | ||
| 36 | RevokeSecurityGroupIngress | ||
| 37 | |||
| 38 | Unimplemented: | ||
| 39 | (none) | ||
| 40 | |||
| 41 | |||
| 42 | =head2 @sg = $ec2->describe_security_groups(@group_ids) | ||
| 43 | |||
| 44 | =head2 @sg = $ec2->describe_security_groups(%args); | ||
| 45 | |||
| 46 | =head2 @sg = $ec2->describe_security_groups(\%filters); | ||
| 47 | |||
| 48 | Searches for security groups (firewall rules) matching the provided | ||
| 49 | filters and return a series of VM::EC2::SecurityGroup objects. | ||
| 50 | |||
| 51 | In the named-argument form you can provide the following optional | ||
| 52 | arguments: | ||
| 53 | |||
| 54 | -group_name A single group name or an arrayref containing a list | ||
| 55 | of names | ||
| 56 | |||
| 57 | -name Shorter version of -group_name | ||
| 58 | |||
| 59 | -group_id A single group id (i.e. 'sg-12345') or an arrayref | ||
| 60 | containing a list of ids | ||
| 61 | |||
| 62 | -filter Filter on tags and other attributes. | ||
| 63 | |||
| 64 | The -filter argument name can be omitted if there are no other | ||
| 65 | arguments you wish to pass. | ||
| 66 | |||
| 67 | The full list of security group filters can be found at: | ||
| 68 | http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeSecurityGroups.html | ||
| 69 | |||
| 70 | =cut | ||
| 71 | |||
| 72 | sub describe_security_groups { | ||
| 73 | my $self = shift; | ||
| 74 | my %args = $self->args(-group_id=>@_); | ||
| 75 | $args{-group_name} ||= $args{-name}; | ||
| 76 | my @params = map { $self->list_parm($_,\%args) } qw(GroupName GroupId); | ||
| 77 | push @params,$self->filter_parm(\%args); | ||
| 78 | return $self->call('DescribeSecurityGroups',@params); | ||
| 79 | } | ||
| 80 | |||
| 81 | =head2 $group = $ec2->create_security_group(-group_name=>$name, | ||
| 82 | -group_description=>$description, | ||
| 83 | -vpc_id => $vpc_id | ||
| 84 | ) | ||
| 85 | |||
| 86 | Create a security group. Arguments are: | ||
| 87 | |||
| 88 | -group_name Name of the security group (required) | ||
| 89 | -group_description Description of the security group (required) | ||
| 90 | -vpc_id Virtual private cloud security group ID | ||
| 91 | (required for VPC security groups) | ||
| 92 | |||
| 93 | For convenience, you may use -name and -description as aliases for | ||
| 94 | -group_name and -group_description respectively. | ||
| 95 | |||
| 96 | If succcessful, the method returns an object of type | ||
| 97 | L<VM::EC2::SecurityGroup>. | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | sub create_security_group { | ||
| 102 | my $self = shift; | ||
| 103 | my %args = @_; | ||
| 104 | $args{-group_name} ||= $args{-name}; | ||
| 105 | $args{-group_description} ||= $args{-description}; | ||
| 106 | $args{-group_name} && $args{-group_description} | ||
| 107 | or croak "create_security_group() requires -group_name and -group_description arguments"; | ||
| 108 | |||
| 109 | my @param; | ||
| 110 | push @param,$self->single_parm($_=>\%args) foreach qw(GroupName GroupDescription VpcId); | ||
| 111 | my $g = $self->call('CreateSecurityGroup',@param) or return; | ||
| 112 | return eval { | ||
| 113 | my $sg; | ||
| 114 | local $SIG{ALRM} = sub {die "timeout"}; | ||
| 115 | alarm(60); | ||
| 116 | until ($sg = $self->describe_security_groups($g)) { sleep 1 } | ||
| 117 | alarm(0); | ||
| 118 | $sg; | ||
| 119 | }; | ||
| 120 | } | ||
| 121 | |||
| 122 | =head2 $boolean = $ec2->delete_security_group($group_id) | ||
| 123 | |||
| 124 | =head2 $boolean = $ec2->delete_security_group(-group_id => $group_id, | ||
| 125 | -group_name => $name); | ||
| 126 | |||
| 127 | Delete a security group. Arguments are: | ||
| 128 | |||
| 129 | -group_name Name of the security group | ||
| 130 | -group_id ID of the security group | ||
| 131 | |||
| 132 | Either -group_name or -group_id is required. In the single-argument | ||
| 133 | form, the method deletes the security group given by its id. | ||
| 134 | |||
| 135 | If succcessful, the method returns true. | ||
| 136 | |||
| 137 | =cut | ||
| 138 | |||
| 139 | sub delete_security_group { | ||
| 140 | my $self = shift; | ||
| 141 | my %args = $self->args(-group_id=>@_); | ||
| 142 | $args{-group_name} ||= $args{-name}; | ||
| 143 | my @param = $self->single_parm(GroupName=>\%args); | ||
| 144 | push @param,$self->single_parm(GroupId=>\%args); | ||
| 145 | return $self->call('DeleteSecurityGroup',@param); | ||
| 146 | } | ||
| 147 | |||
| 148 | =head2 $boolean = $ec2->update_security_group($security_group) | ||
| 149 | |||
| 150 | Add one or more incoming firewall rules to a security group. The rules | ||
| 151 | to add are stored in a L<VM::EC2::SecurityGroup> which is created | ||
| 152 | either by describe_security_groups() or create_security_group(). This method combines | ||
| 153 | the actions AuthorizeSecurityGroupIngress, | ||
| 154 | AuthorizeSecurityGroupEgress, RevokeSecurityGroupIngress, and | ||
| 155 | RevokeSecurityGroupEgress. | ||
| 156 | |||
| 157 | For details, see L<VM::EC2::SecurityGroup>. Here is a brief summary: | ||
| 158 | |||
| 159 | $sg = $ec2->create_security_group(-name=>'MyGroup',-description=>'Example group'); | ||
| 160 | |||
| 161 | # TCP on port 80 for the indicated address ranges | ||
| 162 | $sg->authorize_incoming(-protocol => 'tcp', | ||
| 163 | -port => 80, | ||
| 164 | -source_ip => ['192.168.2.0/24','192.168.2.1/24'}); | ||
| 165 | |||
| 166 | # TCP on ports 22 and 23 from anyone | ||
| 167 | $sg->authorize_incoming(-protocol => 'tcp', | ||
| 168 | -port => '22..23', | ||
| 169 | -source_ip => '0.0.0.0/0'); | ||
| 170 | |||
| 171 | # ICMP on echo (ping) port from anyone | ||
| 172 | $sg->authorize_incoming(-protocol => 'icmp', | ||
| 173 | -port => -1, | ||
| 174 | -source_ip => '0.0.0.0/0'); | ||
| 175 | |||
| 176 | # TCP to port 25 (mail) from instances belonging to | ||
| 177 | # the "Mail relay" group belonging to user 12345678. | ||
| 178 | $sg->authorize_incoming(-protocol => 'tcp', | ||
| 179 | -port => 25, | ||
| 180 | -group => '12345678/Mail relay'); | ||
| 181 | |||
| 182 | $result = $ec2->update_security_group($sg); | ||
| 183 | |||
| 184 | or more simply: | ||
| 185 | |||
| 186 | $result = $sg->update(); | ||
| 187 | |||
| 188 | =cut | ||
| 189 | |||
| 190 | sub update_security_group { | ||
| 191 | my $self = shift; | ||
| 192 | my $sg = shift; | ||
| 193 | my $group_id = $sg->groupId; | ||
| 194 | my $result = 1; | ||
| 195 | |||
| 196 | for my $action (qw(Authorize Revoke)) { | ||
| 197 | for my $direction (qw(Ingress Egress)) { | ||
| 198 | my @permissions = $sg->_uncommitted_permissions($action,$direction) or next; | ||
| 199 | my $call = "${action}SecurityGroup${direction}"; | ||
| 200 | my @param = (GroupId=>$group_id); | ||
| 201 | push @param,$self->_security_group_parm(\@permissions); | ||
| 202 | my $r = $self->call($call=>@param); | ||
| 203 | $result &&= $r; | ||
| 204 | } | ||
| 205 | } | ||
| 206 | return $result; | ||
| 207 | } | ||
| 208 | |||
| 209 | sub _security_group_parm { | ||
| 210 | my $self = shift; | ||
| 211 | my $permissions = shift; | ||
| 212 | my @param; | ||
| 213 | |||
| 214 | for (my $i=0;$i<@$permissions;$i++) { | ||
| 215 | my $perm = $permissions->[$i]; | ||
| 216 | my $n = $i+1; | ||
| 217 | push @param,("IpPermissions.$n.IpProtocol"=>$perm->ipProtocol); | ||
| 218 | push @param,("IpPermissions.$n.FromPort" => $perm->fromPort); | ||
| 219 | push @param,("IpPermissions.$n.ToPort" => $perm->toPort); | ||
| 220 | my @cidr = $perm->ipRanges; | ||
| 221 | for (my $i=0;$i<@cidr;$i++) { | ||
| 222 | my $m = $i+1; | ||
| 223 | push @param,("IpPermissions.$n.IpRanges.$m.CidrIp"=>$cidr[$i]); | ||
| 224 | } | ||
| 225 | my @groups = $perm->groups; | ||
| 226 | for (my $i=0;$i<@groups;$i++) { | ||
| 227 | my $m = $i+1; | ||
| 228 | my $group = $groups[$i]; | ||
| 229 | if (defined $group->groupId) { | ||
| 230 | push @param,("IpPermissions.$n.Groups.$m.GroupId" => $group->groupId); | ||
| 231 | } else { | ||
| 232 | push @param,("IpPermissions.$n.Groups.$m.UserId" => $group->userId); | ||
| 233 | push @param,("IpPermissions.$n.Groups.$m.GroupName"=> $group->groupName); | ||
| 234 | } | ||
| 235 | } | ||
| 236 | } | ||
| 237 | return @param; | ||
| 238 | } | ||
| 239 | |||
| 240 | =head1 SEE ALSO | ||
| 241 | |||
| 242 | L<VM::EC2> | ||
| 243 | |||
| 244 | =head1 AUTHOR | ||
| 245 | |||
| 246 | Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>. | ||
| 247 | |||
| 248 | Copyright (c) 2011 Ontario Institute for Cancer Research | ||
| 249 | |||
| 250 | This package and its accompanying libraries is free software; you can | ||
| 251 | redistribute it and/or modify it under the terms of the GPL (either | ||
| 252 | version 1, or at your option, any later version) or the Artistic | ||
| 253 | License 2.0. Refer to LICENSE for the full license text. In addition, | ||
| 254 | please see DISCLAIMER.txt for disclaimers of warranty. | ||
| 255 | |||
| 256 | =cut | ||
| 257 | |||
| 258 | |||
| 259 | 1; |