| 1 | package VM::EC2::VPC::NetworkAcl; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | VM::EC2::VPC::NetworkAcl - Virtual Private Cloud network ACL | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use VM::EC2; | ||
| 10 | |||
| 11 | my $ec2 = VM::EC2->new(...); | ||
| 12 | my @acls = $ec2->describe_network_acls(-network_acl_id=>'acl-12345678'); | ||
| 13 | foreach my $acl (@acls) { | ||
| 14 | my $vpc_id = $acl->vpcId; | ||
| 15 | my $default = $acl->default; | ||
| 16 | my @entries = $acl->entries; | ||
| 17 | my @assoc = $acl->associations; | ||
| 18 | ... | ||
| 19 | } | ||
| 20 | |||
| 21 | my $acl = $ec2->create_network_acl_entry(...); | ||
| 22 | |||
| 23 | |||
| 24 | =head1 DESCRIPTION | ||
| 25 | |||
| 26 | This object represents an Amazon EC2 VPC network ACL, and is returned by | ||
| 27 | VM::EC2->describe_network_acls() and ->create_network_acl() | ||
| 28 | |||
| 29 | =head1 METHODS | ||
| 30 | |||
| 31 | These object methods are supported: | ||
| 32 | |||
| 33 | networkAclId -- The network ACL's ID. | ||
| 34 | vpcId -- The ID of the VPC the network ACL is in. | ||
| 35 | default -- Whether this is the default network ACL in the VPC. | ||
| 36 | entrySet -- A list of entries (rules) in the network ACL. | ||
| 37 | associationSet -- A list of associations between the network ACL and | ||
| 38 | one or more subnets. | ||
| 39 | tagSet -- Tags assigned to the resource. | ||
| 40 | associations -- Alias for associationSet. | ||
| 41 | entries -- Alias for entrySet. | ||
| 42 | |||
| 43 | The object also supports the tags() method described in | ||
| 44 | L<VM::EC2::Generic>: | ||
| 45 | |||
| 46 | =head1 CONVENIENCE METHODS | ||
| 47 | |||
| 48 | =head2 $success = $acl->create_entry(%args) | ||
| 49 | =head2 $success = $acl->create_entry($acl_entry) | ||
| 50 | |||
| 51 | Creates an entry (i.e., rule) in a network ACL with the rule number you | ||
| 52 | specified. Each network ACL has a set of numbered ingress rules and a | ||
| 53 | separate set of numbered egress rules. When determining whether a packet | ||
| 54 | should be allowed in or out of a subnet associated with the ACL, Amazon | ||
| 55 | VPC processes the entries in the ACL according to the rule numbers, in | ||
| 56 | ascending order. | ||
| 57 | |||
| 58 | Arguments: | ||
| 59 | |||
| 60 | -rule_number -- Rule number to assign to the entry (e.g., 100). | ||
| 61 | ACL entries are processed in ascending order by | ||
| 62 | rule number. Positive integer from 1 to 32766. | ||
| 63 | (Required) | ||
| 64 | -protocol -- The IP protocol the rule applies to. You can use | ||
| 65 | -1 to mean all protocols. See | ||
| 66 | http://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml | ||
| 67 | for a list of protocol numbers. (Required) | ||
| 68 | -rule_action -- Indicates whether to allow or deny traffic that | ||
| 69 | matches the rule. allow | deny (Required) | ||
| 70 | -egress -- Indicates whether this rule applies to egress | ||
| 71 | traffic from the subnet (true) or ingress traffic | ||
| 72 | to the subnet (false). Default is false. | ||
| 73 | -cidr_block -- The CIDR range to allow or deny, in CIDR notation | ||
| 74 | (e.g., 172.16.0.0/24). (Required) | ||
| 75 | -icmp_code -- For the ICMP protocol, the ICMP code. You can use | ||
| 76 | -1 to specify all ICMP codes for the given ICMP | ||
| 77 | type. Required if specifying 1 (ICMP) for protocol. | ||
| 78 | -icmp_type -- For the ICMP protocol, the ICMP type. You can use | ||
| 79 | -1 to specify all ICMP types. Required if | ||
| 80 | specifying 1 (ICMP) for the protocol | ||
| 81 | -port_from -- The first port in the range. Required if specifying | ||
| 82 | 6 (TCP) or 17 (UDP) for the protocol. | ||
| 83 | -port_to -- The last port in the range. Required if specifying | ||
| 84 | 6 (TCP) or 17 (UDP) for the protocol. | ||
| 85 | |||
| 86 | Alternately, can pass an existing ACL entry object L<VM::EC2::VPC::NetworkAcl::Entry> | ||
| 87 | as the only argument for ease in copying entries from one ACL to another. | ||
| 88 | |||
| 89 | Returns true on successful creation. | ||
| 90 | |||
| 91 | =head2 $success = $acl->delete_entry(%args) | ||
| 92 | =head2 $success = $acl->delete_entry($acl_entry) | ||
| 93 | |||
| 94 | Deletes an ingress or egress entry (i.e., rule) from a network ACL. | ||
| 95 | |||
| 96 | Arguments: | ||
| 97 | |||
| 98 | -network_acl_id -- ID of the ACL where the entry will be created | ||
| 99 | |||
| 100 | -rule_number -- Rule number of the entry (e.g., 100). | ||
| 101 | |||
| 102 | Optional arguments: | ||
| 103 | |||
| 104 | -egress -- Whether the rule to delete is an egress rule (true) or ingress | ||
| 105 | rule (false). Default is false. | ||
| 106 | |||
| 107 | Alternately, can pass an existing ACL entry object L<VM::EC2::VPC::NetworkAcl::Entry> | ||
| 108 | as the only argument to ease deletion of entries. | ||
| 109 | |||
| 110 | Returns true on successful deletion. | ||
| 111 | |||
| 112 | =head2 $success = replace_entry(%args) | ||
| 113 | =head2 $success = replace_entry($acl_entry) | ||
| 114 | |||
| 115 | Replaces an entry (i.e., rule) in a network ACL. | ||
| 116 | |||
| 117 | Arguments: | ||
| 118 | |||
| 119 | -network_acl_id -- ID of the ACL where the entry will be created | ||
| 120 | (Required) | ||
| 121 | -rule_number -- Rule number of the entry to replace. (Required) | ||
| 122 | -protocol -- The IP protocol the rule applies to. You can use | ||
| 123 | -1 to mean all protocols. See | ||
| 124 | http://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml | ||
| 125 | for a list of protocol numbers. (Required) | ||
| 126 | -rule_action -- Indicates whether to allow or deny traffic that | ||
| 127 | matches the rule. allow | deny (Required) | ||
| 128 | -egress -- Indicates whether this rule applies to egress | ||
| 129 | traffic from the subnet (true) or ingress traffic | ||
| 130 | to the subnet (false). Default is false. | ||
| 131 | -cidr_block -- The CIDR range to allow or deny, in CIDR notation | ||
| 132 | (e.g., 172.16.0.0/24). (Required) | ||
| 133 | -icmp_code -- For the ICMP protocol, the ICMP code. You can use | ||
| 134 | -1 to specify all ICMP codes for the given ICMP | ||
| 135 | type. Required if specifying 1 (ICMP) for protocol. | ||
| 136 | -icmp_type -- For the ICMP protocol, the ICMP type. You can use | ||
| 137 | -1 to specify all ICMP types. Required if | ||
| 138 | specifying 1 (ICMP) for the protocol | ||
| 139 | -port_from -- The first port in the range. Required if specifying | ||
| 140 | 6 (TCP) or 17 (UDP) for the protocol. | ||
| 141 | -port_to -- The last port in the range. Only required if | ||
| 142 | specifying 6 (TCP) or 17 (UDP) for the protocol and | ||
| 143 | is a different port than -port_from. | ||
| 144 | |||
| 145 | Alternately, can pass an existing ACL entry object L<VM::EC2::VPC::NetworkAcl::Entry> | ||
| 146 | as the only argument for ease in replacing entries from one ACL to another. | ||
| 147 | The rule number in the passed entry object must already exist in the ACL. | ||
| 148 | |||
| 149 | Returns true on successful replacement. | ||
| 150 | |||
| 151 | =head2 $association_id = $acl->associate($subnet_id) | ||
| 152 | |||
| 153 | Associates the ACL with a subnet in the same VPC. Replaces | ||
| 154 | whatever ACL the subnet was associated with previously. | ||
| 155 | |||
| 156 | =head2 $association_id = $acl->disassociate($subnet_id) | ||
| 157 | |||
| 158 | Disassociates the ACL with a subnet in the same VPC. The subnet | ||
| 159 | will then be associated with the default ACL. | ||
| 160 | |||
| 161 | =head1 STRING OVERLOADING | ||
| 162 | |||
| 163 | When used in a string context, this object will interpolate the | ||
| 164 | networkAclId. | ||
| 165 | |||
| 166 | =head1 SEE ALSO | ||
| 167 | |||
| 168 | L<VM::EC2> | ||
| 169 | L<VM::EC2::Generic> | ||
| 170 | L<VM::EC2::Tag> | ||
| 171 | L<VM::EC2::VPC> | ||
| 172 | L<VM::EC2::VPC::NetworkAcl::Entry> | ||
| 173 | L<VM::EC2::VPC::NetworkAcl::Association> | ||
| 174 | |||
| 175 | =head1 AUTHOR | ||
| 176 | |||
| 177 | Lance Kinley E<lt>lkinley@loyaltymethods.comE<gt>. | ||
| 178 | |||
| 179 | Copyright (c) 2012 Loyalty Methods, Inc. | ||
| 180 | |||
| 181 | This package and its accompanying libraries is free software; you can | ||
| 182 | redistribute it and/or modify it under the terms of the GPL (either | ||
| 183 | version 1, or at your option, any later version) or the Artistic | ||
| 184 | License 2.0. Refer to LICENSE for the full license text. In addition, | ||
| 185 | please see DISCLAIMER.txt for disclaimers of warranty. | ||
| 186 | |||
| 187 | =cut | ||
| 188 | |||
| 189 | use strict; | ||
| 190 | use base 'VM::EC2::Generic'; | ||
| 191 | use VM::EC2::VPC::NetworkAcl::Entry; | ||
| 192 | use VM::EC2::VPC::NetworkAcl::Association; | ||
| 193 | use Carp 'croak'; | ||
| 194 | |||
| 195 | sub primary_id { shift->networkAclId } | ||
| 196 | |||
| 197 | sub valid_fields { | ||
| 198 | my $self = shift; | ||
| 199 | return qw(networkAclId vpcId default entrySet associationSet tagSet); | ||
| 200 | } | ||
| 201 | |||
| 202 | sub default { | ||
| 203 | my $self = shift; | ||
| 204 | my $entries = $self->SUPER::default; | ||
| 205 | return $entries eq 'true'; | ||
| 206 | } | ||
| 207 | |||
| 208 | sub entrySet { | ||
| 209 | my $self = shift; | ||
| 210 | my $entries = $self->SUPER::entrySet; | ||
| 211 | return map { VM::EC2::VPC::NetworkAcl::Entry->new($_,$self->aws) } @{$entries->{item}}; | ||
| 212 | } | ||
| 213 | |||
| 214 | sub entries { shift->entrySet } | ||
| 215 | |||
| 216 | sub associationSet { | ||
| 217 | my $self = shift; | ||
| 218 | my $assoc = $self->SUPER::associationSet; | ||
| 219 | return map { VM::EC2::VPC::NetworkAcl::Association->new($_,$self->aws) } @{$assoc->{item}}; | ||
| 220 | } | ||
| 221 | |||
| 222 | sub associations { shift->associationSet } | ||
| 223 | |||
| 224 | sub create_entry { | ||
| 225 | my $self = shift; | ||
| 226 | my %args; | ||
| 227 | if (@_ == 1 && $_[0] !~ /^-/ && ref $_[0] eq 'VM::EC2::VPC::NetworkAcl::Entry') { | ||
| 228 | my $entry = shift; | ||
| 229 | $args{-rule_number} = $entry->ruleNumber; | ||
| 230 | $args{-protocol} = $entry->protocol; | ||
| 231 | $args{-rule_action} = $entry->ruleAction; | ||
| 232 | $args{-egress} = $entry->egress; | ||
| 233 | $args{-cidr_block} = $entry->cidrBlock; | ||
| 234 | $args{-icmpType} = $entry->icmpType; | ||
| 235 | $args{-icmpCode} = $entry->icmpCode; | ||
| 236 | $args{-port_from} = $entry->portRangeFrom; | ||
| 237 | $args{-port_to} = $entry->portRangeTo; | ||
| 238 | } | ||
| 239 | else { | ||
| 240 | %args = @_; | ||
| 241 | } | ||
| 242 | $args{-network_acl_id} = $self->networkAclId; | ||
| 243 | return $self->aws->create_network_acl_entry(%args); | ||
| 244 | } | ||
| 245 | |||
| 246 | sub delete_entry { | ||
| 247 | my $self = shift; | ||
| 248 | my %args; | ||
| 249 | if (@_ == 1 && $_[0] !~ /^-/ && ref $_[0] eq 'VM::EC2::VPC::NetworkAcl::Entry') { | ||
| 250 | my $entry = shift; | ||
| 251 | $args{-rule_number} = $entry->ruleNumber; | ||
| 252 | $args{-egress} = $entry->egress; | ||
| 253 | } | ||
| 254 | else { | ||
| 255 | %args = @_; | ||
| 256 | } | ||
| 257 | $args{-network_acl_id} = $self->networkAclId; | ||
| 258 | return $self->aws->delete_network_acl_entry(%args); | ||
| 259 | } | ||
| 260 | |||
| 261 | sub replace_entry { | ||
| 262 | my $self = shift; | ||
| 263 | my %args; | ||
| 264 | if (@_ == 1 && $_[0] !~ /^-/ && ref $_[0] eq 'VM::EC2::VPC::NetworkAcl::Entry') { | ||
| 265 | my $entry = shift; | ||
| 266 | $args{-rule_number} = $entry->ruleNumber; | ||
| 267 | $args{-protocol} = $entry->protocol; | ||
| 268 | $args{-rule_action} = $entry->ruleAction; | ||
| 269 | $args{-egress} = $entry->egress; | ||
| 270 | $args{-cidr_block} = $entry->cidrBlock; | ||
| 271 | $args{-icmpType} = $entry->icmpType; | ||
| 272 | $args{-icmpCode} = $entry->icmpCode; | ||
| 273 | $args{-port_from} = $entry->portRangeFrom; | ||
| 274 | $args{-port_to} = $entry->portRangeTo; | ||
| 275 | } | ||
| 276 | else { | ||
| 277 | %args = @_; | ||
| 278 | } | ||
| 279 | $args{-network_acl_id} = $self->networkAclId; | ||
| 280 | return $self->aws->replace_network_acl_entry(%args); | ||
| 281 | } | ||
| 282 | |||
| 283 | sub associate { | ||
| 284 | my $self = shift; | ||
| 285 | my $subnet_id = shift or croak "usage: associate(\$subnet_id)"; | ||
| 286 | # get the acl the subnet is currently associated with in order the find the association id | ||
| 287 | my $acl = $self->aws->describe_network_acls(-filter=>{ 'association.subnet-id' => $subnet_id}) | ||
| 288 | or croak "associate(): Cannot determine ACL for subnet $subnet_id"; | ||
| 289 | my ($association) = grep { $_->subnetId eq $subnet_id } $acl->associations; | ||
| 290 | my $association_id = $association->networkAclAssociationId; | ||
| 291 | my $network_acl_id = $self->networkAclId; | ||
| 292 | return $self->aws->replace_network_acl_association(-association_id=>$association_id,-network_acl_id=>$network_acl_id); | ||
| 293 | } | ||
| 294 | |||
| 295 | sub disassociate { | ||
| 296 | my $self = shift; | ||
| 297 | my $subnet_id = shift or croak "usage: associate(\$subnet_id)"; | ||
| 298 | $self->default and croak "Cannot disassociate subnet from default ACL"; | ||
| 299 | # determine association id for this acl | ||
| 300 | my ($association) = grep { $_->subnetId eq $subnet_id } $self->associations; | ||
| 301 | my $association_id = $association->networkAclAssociationId; | ||
| 302 | # determine default acl for this subnet | ||
| 303 | my $default_acl = $self->aws->describe_network_acls(-filter=>{ 'default' => 'true', 'vpc-id' => $self->vpcId}) | ||
| 304 | or croak "disassociate(): Cannot determine default ACL"; | ||
| 305 | my $network_acl_id = $default_acl->networkAclId; | ||
| 306 | return $self->aws->replace_network_acl_association(-association_id=>$association_id,-network_acl_id=>$network_acl_id); | ||
| 307 | } | ||
| 308 | |||
| 309 | 1; |