| 
									
										
										
										
											2014-08-27 11:17:06 -04:00
										 |  |  | from __future__ import unicode_literals | 
					
						
							| 
									
										
										
										
											2013-02-21 23:13:01 -05:00
										 |  |  | import boto | 
					
						
							| 
									
										
										
										
											2019-01-21 13:30:22 +01:00
										 |  |  | import boto3 | 
					
						
							| 
									
										
										
										
											2013-08-03 17:21:25 -04:00
										 |  |  | import sure  # noqa | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  | import pytest | 
					
						
							| 
									
										
										
										
											2019-05-26 03:02:14 +02:00
										 |  |  | from botocore.exceptions import ClientError | 
					
						
							| 
									
										
										
										
											2013-02-21 23:13:01 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-21 13:30:22 +01:00
										 |  |  | from moto import mock_ec2_deprecated, mock_ec2 | 
					
						
							| 
									
										
										
										
											2013-02-21 23:13:01 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-11-14 17:23:56 -06:00
										 |  |  | def test_default_network_acl_created_with_vpc(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_vpc("the_key", "the secret") | 
					
						
							| 
									
										
										
										
											2014-11-14 17:23:56 -06:00
										 |  |  |     vpc = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  |     all_network_acls = conn.get_all_network_acls() | 
					
						
							| 
									
										
										
										
											2017-01-11 17:37:57 -08:00
										 |  |  |     all_network_acls.should.have.length_of(2) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-14 17:23:56 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2013-02-21 23:13:01 -05:00
										 |  |  | def test_network_acls(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_vpc("the_key", "the secret") | 
					
						
							| 
									
										
										
										
											2014-11-12 15:33:31 -06:00
										 |  |  |     vpc = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  |     network_acl = conn.create_network_acl(vpc.id) | 
					
						
							| 
									
										
										
										
											2014-11-14 17:23:56 -06:00
										 |  |  |     all_network_acls = conn.get_all_network_acls() | 
					
						
							| 
									
										
										
										
											2017-01-11 17:37:57 -08:00
										 |  |  |     all_network_acls.should.have.length_of(3) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-14 17:23:56 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-11-14 17:23:56 -06:00
										 |  |  | def test_new_subnet_associates_with_default_network_acl(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_vpc("the_key", "the secret") | 
					
						
							| 
									
										
										
										
											2017-01-11 17:37:57 -08:00
										 |  |  |     vpc = conn.get_all_vpcs()[0] | 
					
						
							| 
									
										
										
										
											2014-11-14 17:23:56 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 17:33:25 +02:00
										 |  |  |     subnet = conn.create_subnet(vpc.id, "172.31.112.0/20") | 
					
						
							| 
									
										
										
										
											2014-11-12 15:33:31 -06:00
										 |  |  |     all_network_acls = conn.get_all_network_acls() | 
					
						
							|  |  |  |     all_network_acls.should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-14 17:23:56 -06:00
										 |  |  |     acl = all_network_acls[0] | 
					
						
							| 
									
										
										
										
											2019-05-28 17:33:25 +02:00
										 |  |  |     acl.associations.should.have.length_of(7) | 
					
						
							| 
									
										
										
										
											2017-01-11 17:37:57 -08:00
										 |  |  |     [a.subnet_id for a in acl.associations].should.contain(subnet.id) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-12 15:33:31 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-11-12 15:33:31 -06:00
										 |  |  | def test_network_acl_entries(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_vpc("the_key", "the secret") | 
					
						
							| 
									
										
										
										
											2014-11-12 15:33:31 -06:00
										 |  |  |     vpc = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     network_acl = conn.create_network_acl(vpc.id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     network_acl_entry = conn.create_network_acl_entry( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         network_acl.id, | 
					
						
							|  |  |  |         110, | 
					
						
							|  |  |  |         6, | 
					
						
							|  |  |  |         "ALLOW", | 
					
						
							|  |  |  |         "0.0.0.0/0", | 
					
						
							|  |  |  |         False, | 
					
						
							|  |  |  |         port_range_from="443", | 
					
						
							|  |  |  |         port_range_to="443", | 
					
						
							| 
									
										
										
										
											2014-11-12 15:33:31 -06:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     all_network_acls = conn.get_all_network_acls() | 
					
						
							| 
									
										
										
										
											2017-01-11 17:37:57 -08:00
										 |  |  |     all_network_acls.should.have.length_of(3) | 
					
						
							| 
									
										
										
										
											2014-11-12 15:33:31 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     test_network_acl = next(na for na in all_network_acls if na.id == network_acl.id) | 
					
						
							| 
									
										
										
										
											2014-11-14 17:23:56 -06:00
										 |  |  |     entries = test_network_acl.network_acl_entries | 
					
						
							| 
									
										
										
										
											2014-11-12 15:33:31 -06:00
										 |  |  |     entries.should.have.length_of(1) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     entries[0].rule_number.should.equal("110") | 
					
						
							|  |  |  |     entries[0].protocol.should.equal("6") | 
					
						
							|  |  |  |     entries[0].rule_action.should.equal("ALLOW") | 
					
						
							| 
									
										
										
										
											2014-11-12 15:33:31 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-14 13:03:43 +01:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							|  |  |  | def test_delete_network_acl_entry(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_vpc("the_key", "the secret") | 
					
						
							| 
									
										
										
										
											2017-05-14 13:03:43 +01:00
										 |  |  |     vpc = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     network_acl = conn.create_network_acl(vpc.id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     conn.create_network_acl_entry( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         network_acl.id, | 
					
						
							|  |  |  |         110, | 
					
						
							|  |  |  |         6, | 
					
						
							|  |  |  |         "ALLOW", | 
					
						
							|  |  |  |         "0.0.0.0/0", | 
					
						
							|  |  |  |         False, | 
					
						
							|  |  |  |         port_range_from="443", | 
					
						
							|  |  |  |         port_range_to="443", | 
					
						
							| 
									
										
										
										
											2017-05-14 13:03:43 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn.delete_network_acl_entry(network_acl.id, 110, False) | 
					
						
							| 
									
										
										
										
											2017-05-14 13:03:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     all_network_acls = conn.get_all_network_acls() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     test_network_acl = next(na for na in all_network_acls if na.id == network_acl.id) | 
					
						
							| 
									
										
										
										
											2017-05-14 13:03:43 +01:00
										 |  |  |     entries = test_network_acl.network_acl_entries | 
					
						
							|  |  |  |     entries.should.have.length_of(0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2_deprecated | 
					
						
							|  |  |  | def test_replace_network_acl_entry(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_vpc("the_key", "the secret") | 
					
						
							| 
									
										
										
										
											2017-05-14 13:03:43 +01:00
										 |  |  |     vpc = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     network_acl = conn.create_network_acl(vpc.id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     conn.create_network_acl_entry( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         network_acl.id, | 
					
						
							|  |  |  |         110, | 
					
						
							|  |  |  |         6, | 
					
						
							|  |  |  |         "ALLOW", | 
					
						
							|  |  |  |         "0.0.0.0/0", | 
					
						
							|  |  |  |         False, | 
					
						
							|  |  |  |         port_range_from="443", | 
					
						
							|  |  |  |         port_range_to="443", | 
					
						
							| 
									
										
										
										
											2017-05-14 13:03:43 +01:00
										 |  |  |     ) | 
					
						
							|  |  |  |     conn.replace_network_acl_entry( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         network_acl.id, | 
					
						
							|  |  |  |         110, | 
					
						
							|  |  |  |         -1, | 
					
						
							|  |  |  |         "DENY", | 
					
						
							|  |  |  |         "0.0.0.0/0", | 
					
						
							|  |  |  |         False, | 
					
						
							|  |  |  |         port_range_from="22", | 
					
						
							|  |  |  |         port_range_to="22", | 
					
						
							| 
									
										
										
										
											2017-05-14 13:03:43 +01:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     all_network_acls = conn.get_all_network_acls() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     test_network_acl = next(na for na in all_network_acls if na.id == network_acl.id) | 
					
						
							| 
									
										
										
										
											2017-05-14 13:03:43 +01:00
										 |  |  |     entries = test_network_acl.network_acl_entries | 
					
						
							|  |  |  |     entries.should.have.length_of(1) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     entries[0].rule_number.should.equal("110") | 
					
						
							|  |  |  |     entries[0].protocol.should.equal("-1") | 
					
						
							|  |  |  |     entries[0].rule_action.should.equal("DENY") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-14 13:03:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-11-14 17:23:56 -06:00
										 |  |  | def test_associate_new_network_acl_with_subnet(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_vpc("the_key", "the secret") | 
					
						
							| 
									
										
										
										
											2014-11-14 17:23:56 -06:00
										 |  |  |     vpc = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  |     subnet = conn.create_subnet(vpc.id, "10.0.0.0/18") | 
					
						
							|  |  |  |     network_acl = conn.create_network_acl(vpc.id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     conn.associate_network_acl(network_acl.id, subnet.id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     all_network_acls = conn.get_all_network_acls() | 
					
						
							| 
									
										
										
										
											2017-01-11 17:37:57 -08:00
										 |  |  |     all_network_acls.should.have.length_of(3) | 
					
						
							| 
									
										
										
										
											2014-11-14 17:23:56 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     test_network_acl = next(na for na in all_network_acls if na.id == network_acl.id) | 
					
						
							| 
									
										
										
										
											2014-11-14 17:23:56 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     test_network_acl.associations.should.have.length_of(1) | 
					
						
							|  |  |  |     test_network_acl.associations[0].subnet_id.should.equal(subnet.id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-11-14 17:23:56 -06:00
										 |  |  | def test_delete_network_acl(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_vpc("the_key", "the secret") | 
					
						
							| 
									
										
										
										
											2014-11-14 17:23:56 -06:00
										 |  |  |     vpc = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  |     subnet = conn.create_subnet(vpc.id, "10.0.0.0/18") | 
					
						
							|  |  |  |     network_acl = conn.create_network_acl(vpc.id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     all_network_acls = conn.get_all_network_acls() | 
					
						
							| 
									
										
										
										
											2017-01-11 17:37:57 -08:00
										 |  |  |     all_network_acls.should.have.length_of(3) | 
					
						
							| 
									
										
										
										
											2014-11-14 17:23:56 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     any(acl.id == network_acl.id for acl in all_network_acls).should.be.ok | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     conn.delete_network_acl(network_acl.id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     updated_network_acls = conn.get_all_network_acls() | 
					
						
							| 
									
										
										
										
											2017-01-11 17:37:57 -08:00
										 |  |  |     updated_network_acls.should.have.length_of(2) | 
					
						
							| 
									
										
										
										
											2014-11-14 17:23:56 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     any(acl.id == network_acl.id for acl in updated_network_acls).shouldnt.be.ok | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-11-20 12:02:38 -06:00
										 |  |  | def test_network_acl_tagging(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_vpc("the_key", "the secret") | 
					
						
							| 
									
										
										
										
											2014-11-20 12:02:38 -06:00
										 |  |  |     vpc = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  |     network_acl = conn.create_network_acl(vpc.id) | 
					
						
							| 
									
										
										
										
											2014-11-14 17:23:56 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-20 12:02:38 -06:00
										 |  |  |     network_acl.add_tag("a key", "some value") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     tag = conn.get_all_tags()[0] | 
					
						
							|  |  |  |     tag.name.should.equal("a key") | 
					
						
							|  |  |  |     tag.value.should.equal("some value") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     all_network_acls = conn.get_all_network_acls() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     test_network_acl = next(na for na in all_network_acls if na.id == network_acl.id) | 
					
						
							| 
									
										
										
										
											2014-11-20 12:02:38 -06:00
										 |  |  |     test_network_acl.tags.should.have.length_of(1) | 
					
						
							|  |  |  |     test_network_acl.tags["a key"].should.equal("some value") | 
					
						
							| 
									
										
										
										
											2019-01-21 13:30:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_new_subnet_in_new_vpc_associates_with_default_network_acl(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							|  |  |  |     new_vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16") | 
					
						
							| 
									
										
										
										
											2019-01-21 13:30:22 +01:00
										 |  |  |     new_vpc.reload() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     subnet = ec2.create_subnet(VpcId=new_vpc.id, CidrBlock="10.0.0.0/24") | 
					
						
							| 
									
										
										
										
											2019-01-21 13:30:22 +01:00
										 |  |  |     subnet.reload() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     new_vpcs_default_network_acl = next(iter(new_vpc.network_acls.all()), None) | 
					
						
							|  |  |  |     new_vpcs_default_network_acl.reload() | 
					
						
							|  |  |  |     new_vpcs_default_network_acl.vpc_id.should.equal(new_vpc.id) | 
					
						
							|  |  |  |     new_vpcs_default_network_acl.associations.should.have.length_of(1) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     new_vpcs_default_network_acl.associations[0]["SubnetId"].should.equal(subnet.id) | 
					
						
							| 
									
										
										
										
											2019-01-21 15:36:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_default_network_acl_default_entries(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2019-01-21 15:36:49 +01:00
										 |  |  |     default_network_acl = next(iter(ec2.network_acls.all()), None) | 
					
						
							|  |  |  |     default_network_acl.is_default.should.be.ok | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     default_network_acl.entries.should.have.length_of(4) | 
					
						
							|  |  |  |     unique_entries = [] | 
					
						
							|  |  |  |     for entry in default_network_acl.entries: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         entry["CidrBlock"].should.equal("0.0.0.0/0") | 
					
						
							|  |  |  |         entry["Protocol"].should.equal("-1") | 
					
						
							|  |  |  |         entry["RuleNumber"].should.be.within([100, 32767]) | 
					
						
							|  |  |  |         entry["RuleAction"].should.be.within(["allow", "deny"]) | 
					
						
							|  |  |  |         assert type(entry["Egress"]) is bool | 
					
						
							|  |  |  |         if entry["RuleAction"] == "allow": | 
					
						
							|  |  |  |             entry["RuleNumber"].should.be.equal(100) | 
					
						
							| 
									
										
										
										
											2019-01-21 15:36:49 +01:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             entry["RuleNumber"].should.be.equal(32767) | 
					
						
							| 
									
										
										
										
											2019-01-21 15:36:49 +01:00
										 |  |  |         if entry not in unique_entries: | 
					
						
							|  |  |  |             unique_entries.append(entry) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     unique_entries.should.have.length_of(4) | 
					
						
							| 
									
										
										
										
											2019-05-26 03:02:14 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_delete_default_network_acl_default_entry(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2019-05-26 03:02:14 +02:00
										 |  |  |     default_network_acl = next(iter(ec2.network_acls.all()), None) | 
					
						
							|  |  |  |     default_network_acl.is_default.should.be.ok | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     default_network_acl.entries.should.have.length_of(4) | 
					
						
							|  |  |  |     first_default_network_acl_entry = default_network_acl.entries[0] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     default_network_acl.delete_entry( | 
					
						
							|  |  |  |         Egress=first_default_network_acl_entry["Egress"], | 
					
						
							|  |  |  |         RuleNumber=first_default_network_acl_entry["RuleNumber"], | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-05-26 03:02:14 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     default_network_acl.entries.should.have.length_of(3) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_duplicate_network_acl_entry(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2019-05-26 03:02:14 +02:00
										 |  |  |     default_network_acl = next(iter(ec2.network_acls.all()), None) | 
					
						
							|  |  |  |     default_network_acl.is_default.should.be.ok | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     rule_number = 200 | 
					
						
							|  |  |  |     egress = True | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     default_network_acl.create_entry( | 
					
						
							|  |  |  |         CidrBlock="0.0.0.0/0", | 
					
						
							|  |  |  |         Egress=egress, | 
					
						
							|  |  |  |         Protocol="-1", | 
					
						
							|  |  |  |         RuleAction="allow", | 
					
						
							|  |  |  |         RuleNumber=rule_number, | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-05-26 03:02:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(ClientError) as ex: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         default_network_acl.create_entry( | 
					
						
							|  |  |  |             CidrBlock="10.0.0.0/0", | 
					
						
							|  |  |  |             Egress=egress, | 
					
						
							|  |  |  |             Protocol="-1", | 
					
						
							|  |  |  |             RuleAction="deny", | 
					
						
							|  |  |  |             RuleNumber=rule_number, | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     str(ex.value).should.equal( | 
					
						
							| 
									
										
										
										
											2019-05-26 03:02:14 +02:00
										 |  |  |         "An error occurred (NetworkAclEntryAlreadyExists) when calling the CreateNetworkAclEntry " | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "operation: The network acl entry identified by {} already exists.".format( | 
					
						
							|  |  |  |             rule_number | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2020-05-26 07:04:59 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_describe_network_acls(): | 
					
						
							|  |  |  |     conn = boto3.client("ec2", region_name="us-west-2") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     vpc = conn.create_vpc(CidrBlock="10.0.0.0/16") | 
					
						
							|  |  |  |     vpc_id = vpc["Vpc"]["VpcId"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     network_acl = conn.create_network_acl(VpcId=vpc_id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     network_acl_id = network_acl["NetworkAcl"]["NetworkAclId"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = conn.describe_network_acls(NetworkAclIds=[network_acl_id]) | 
					
						
							|  |  |  |     result = resp["NetworkAcls"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     result.should.have.length_of(1) | 
					
						
							|  |  |  |     result[0]["NetworkAclId"].should.equal(network_acl_id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp2 = conn.describe_network_acls()["NetworkAcls"] | 
					
						
							|  |  |  |     resp2.should.have.length_of(3) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     with pytest.raises(ClientError) as ex: | 
					
						
							| 
									
										
										
										
											2020-05-26 07:04:59 -03:00
										 |  |  |         conn.describe_network_acls(NetworkAclIds=["1"]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     str(ex.value).should.equal( | 
					
						
							| 
									
										
										
										
											2020-05-26 07:04:59 -03:00
										 |  |  |         "An error occurred (InvalidRouteTableID.NotFound) when calling the " | 
					
						
							|  |  |  |         "DescribeNetworkAcls operation: The routeTable ID '1' does not exist" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2020-11-02 19:00:02 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_create_network_acl_with_tags(): | 
					
						
							|  |  |  |     conn = boto3.client("ec2", region_name="us-west-2") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     vpc = conn.create_vpc(CidrBlock="10.0.0.0/16") | 
					
						
							|  |  |  |     vpc_id = vpc["Vpc"]["VpcId"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     network_acl = conn.create_network_acl( | 
					
						
							|  |  |  |         VpcId=vpc_id, | 
					
						
							|  |  |  |         TagSpecifications=[ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 "ResourceType": "network-acl", | 
					
						
							|  |  |  |                 "Tags": [{"Key": "test", "Value": "TestTags"}], | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     (len(network_acl.get("NetworkAcl").get("Tags"))).should.equal(1) | 
					
						
							|  |  |  |     network_acl.get("NetworkAcl").get("Tags").should.equal( | 
					
						
							|  |  |  |         [{"Key": "test", "Value": "TestTags"}] | 
					
						
							|  |  |  |     ) |