lib/VM/EC2/REST/security_group.pm

filter: Statementstmt 10.9% 8 / 73 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 25.0% 2 / 8 Podpod 0.0% 0 / 4 totaltotal 7.6% 10 / 131 CC 18 SCAR 35.9
CC
18
Cov
7%
create_security_group
72.0
update_security_group
30.0
_security_group_parm
30.0
CRAP 36.2
« lib/VM/EC2/REST/ebs.pm ↑ Summary blib/lib/VM/EC2.pm »
1 package VM::EC2::REST::security_group;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use VM::EC2 ''; # important not to import anything!
Subroutine BEGIN: called CC=1 SCAR=0.0
5 package VM::EC2; # add methods to VM::EC2
6  
7 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 0 ▶ my $self = shift;
Subroutine describe_security_groups: not called CC=2 SCAR=17.9
Pod undocumented
74 0 my %args = $self->args(-group_id=>@_);
75 0 ▶ $args{-group_name} ||= $args{-name};
Condition0%0/3
$args{"-group_name"} ||= $args{"-name"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"-group_name"} ||= $args{"-name"}
$args{"-group_name"} $args{"-name"}
Truth table0%0/3
A ||= B
A= $args{"-group_name"}B= $args{"-name"}
ABresult
1X1
011
000
76 0 my @params = map { $self->list_parm($_,\%args) } qw(GroupName GroupId);
77 0 push @params,$self->filter_parm(\%args);
78 0 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 0 ▶ my $self = shift;
Subroutine create_security_group: not called CC=8 SCAR=42.8
Pod undocumented
103 0 my %args = @_;
104 0 ▶ $args{-group_name} ||= $args{-name};
Condition0%0/3
$args{"-group_name"} ||= $args{"-name"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"-group_name"} ||= $args{"-name"}
$args{"-group_name"} $args{"-name"}
Truth table0%0/3
A ||= B
A= $args{"-group_name"}B= $args{"-name"}
ABresult
1X1
011
000
105 0 ▶ $args{-group_description} ||= $args{-description};
Condition0%0/3
$args{"-group_description"} ||= $args{"-description"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"-group_description"} ||= $args{"-description"}
$args{"-group_description"} $args{"-description"}
Truth table0%0/3
A ||= B
A= $args{"-group_description"}B= $args{"-description"}
ABresult
1X1
011
000
106 $args{-group_name} && $args{-group_description}
107 0 ▶ or croak "create_security_group() requires -group_name and -group_description arguments";
Branch: 1 branch
BranchTrueFalseTotal
$args{"-group_name"} and $args{"-group_description"} or croak("create_security_group() requires -group_name and -group_description arguments") 0 0 0
Condition0%0/3
$args{"-group_name"} and $args{"-group_description"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"-group_name"} and $args{"-group_description"}
$args{"-group_name"} $args{"-group_description"}
Truth table0%0/3
A and B
A= $args{"-group_name"}B= $args{"-group_description"}
ABresult
0X0
100
111
108  
109 0 my @param;
110 0 push @param,$self->single_parm($_=>\%args) foreach qw(GroupName GroupDescription VpcId);
111 0 ▶ my $g = $self->call('CreateSecurityGroup',@param) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $g = $self->call("CreateSecurityGroup", @param) or return 0 0 0
112 0 return eval {
113 0 my $sg;
114 0 ▶ local $SIG{ALRM} = sub {die "timeout"};
Subroutine __ANON__: not called CC=1 SCAR=6.9
115 0 alarm(60);
116 0 ▶ until ($sg = $self->describe_security_groups($g)) { sleep 1 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($sg = $self->describe_security_groups($g)) 0 0 0
117 0 alarm(0);
118 0 $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 0 ▶ my $self = shift;
Subroutine delete_security_group: not called CC=2 SCAR=17.9
Pod undocumented
141 0 my %args = $self->args(-group_id=>@_);
142 0 ▶ $args{-group_name} ||= $args{-name};
Condition0%0/3
$args{"-group_name"} ||= $args{"-name"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"-group_name"} ||= $args{"-name"}
$args{"-group_name"} $args{"-name"}
Truth table0%0/3
A ||= B
A= $args{"-group_name"}B= $args{"-name"}
ABresult
1X1
011
000
143 0 my @param = $self->single_parm(GroupName=>\%args);
144 0 push @param,$self->single_parm(GroupId=>\%args);
145 0 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 0 ▶ my $self = shift;
Subroutine update_security_group: not called CC=5 SCAR=34.0
Pod undocumented
192 0 my $sg = shift;
193 0 my $group_id = $sg->groupId;
194 0 my $result = 1;
195
196 0 for my $action (qw(Authorize Revoke)) {
197 0 for my $direction (qw(Ingress Egress)) {
198 0 ▶ my @permissions = $sg->_uncommitted_permissions($action,$direction) or next;
Branch: 1 branch
BranchTrueFalseTotal
my(@permissions) = $sg->_uncommitted_permissions($action, $direction) or next 0 0 0
199 0 my $call = "${action}SecurityGroup${direction}";
200 0 my @param = (GroupId=>$group_id);
201 0 push @param,$self->_security_group_parm(\@permissions);
202 0 my $r = $self->call($call=>@param);
203 0 ▶ $result &&= $r;
Condition0%0/3
$result &&= $r
!ll&&!rl&&r
000
MC/DC0%0/2
$result &&= $r
$result $r
Truth table0%0/3
A &&= B
A= $resultB= $r
ABresult
0X0
100
111
204 }
205 }
206 0 return $result;
207 }
208  
209 sub _security_group_parm {
210 0 ▶ my $self = shift;
Subroutine _security_group_parm: not called CC=5 SCAR=34.0
211 0 my $permissions = shift;
212 0 my @param;
213  
214 0 ▶ for (my $i=0;$i<@$permissions;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$permissions) 0 0 0
215 0 my $perm = $permissions->[$i];
216 0 my $n = $i+1;
217 0 push @param,("IpPermissions.$n.IpProtocol"=>$perm->ipProtocol);
218 0 push @param,("IpPermissions.$n.FromPort" => $perm->fromPort);
219 0 push @param,("IpPermissions.$n.ToPort" => $perm->toPort);
220 0 my @cidr = $perm->ipRanges;
221 0 ▶ for (my $i=0;$i<@cidr;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @cidr) 0 0 0
222 0 my $m = $i+1;
223 0 push @param,("IpPermissions.$n.IpRanges.$m.CidrIp"=>$cidr[$i]);
224 }
225 0 my @groups = $perm->groups;
226 0 ▶ for (my $i=0;$i<@groups;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @groups) 0 0 0
227 0 my $m = $i+1;
228 0 my $group = $groups[$i];
229 0 ▶ if (defined $group->groupId) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $group->groupId) { } 0 0 0
230 0 push @param,("IpPermissions.$n.Groups.$m.GroupId" => $group->groupId);
231 } else {
232 0 push @param,("IpPermissions.$n.Groups.$m.UserId" => $group->userId);
233 0 push @param,("IpPermissions.$n.Groups.$m.GroupName"=> $group->groupName);
234 }
235 }
236 }
237 0 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 7 1;
« lib/VM/EC2/REST/ebs.pm ↑ Summary blib/lib/VM/EC2.pm »