| 
									
										
										
										
											2014-08-27 11:17:06 -04:00
										 |  |  | from __future__ import unicode_literals | 
					
						
							| 
									
										
										
										
											2016-10-20 17:25:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | import copy | 
					
						
							| 
									
										
										
										
											2020-08-04 11:20:57 +05:30
										 |  |  | import json | 
					
						
							| 
									
										
										
										
											2021-03-31 11:33:36 -07:00
										 |  |  | import unittest | 
					
						
							| 
									
										
										
										
											2016-10-20 17:25:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  | import pytest | 
					
						
							| 
									
										
										
										
											2014-08-25 15:09:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-20 23:01:09 +02:00
										 |  |  | import boto3 | 
					
						
							| 
									
										
										
										
											2013-02-21 23:13:01 -05:00
										 |  |  | import boto | 
					
						
							| 
									
										
										
										
											2017-01-11 22:35:27 -05:00
										 |  |  | from botocore.exceptions import ClientError | 
					
						
							| 
									
										
										
										
											2017-02-23 19:43:48 -05:00
										 |  |  | from boto.exception import EC2ResponseError | 
					
						
							| 
									
										
										
										
											2013-08-03 17:21:25 -04:00
										 |  |  | import sure  # noqa | 
					
						
							| 
									
										
										
										
											2013-02-21 23:13:01 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-31 11:33:36 -07:00
										 |  |  | from moto import mock_ec2, mock_ec2_deprecated, settings | 
					
						
							|  |  |  | from moto.ec2 import ec2_backend | 
					
						
							| 
									
										
										
										
											2013-02-21 23:13:01 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2013-02-23 15:26:54 -05:00
										 |  |  | def test_create_and_describe_security_group(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_ec2("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as ex: | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |         security_group = conn.create_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "test security group", "this is a test security group", dry_run=True | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     ex.value.error_code.should.equal("DryRunOperation") | 
					
						
							|  |  |  |     ex.value.status.should.equal(400) | 
					
						
							|  |  |  |     ex.value.message.should.equal( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "An error occurred (DryRunOperation) when calling the CreateSecurityGroup operation: Request would have succeeded, but DryRun flag is set" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     security_group = conn.create_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "test security group", "this is a test security group" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2013-02-23 15:26:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     security_group.name.should.equal("test security group") | 
					
						
							|  |  |  |     security_group.description.should.equal("this is a test security group") | 
					
						
							| 
									
										
										
										
											2013-02-23 15:26:54 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Trying to create another group with the same name should throw an error | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |         conn.create_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "test security group", "this is a test security group" | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("InvalidGroup.Duplicate") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2013-02-23 15:26:54 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     all_groups = conn.get_all_security_groups() | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     # The default group gets created automatically | 
					
						
							|  |  |  |     all_groups.should.have.length_of(3) | 
					
						
							| 
									
										
										
										
											2015-03-14 18:50:41 -04:00
										 |  |  |     group_names = [group.name for group in all_groups] | 
					
						
							|  |  |  |     set(group_names).should.equal(set(["default", "test security group"])) | 
					
						
							| 
									
										
										
										
											2013-02-23 15:26:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-20 16:22:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-05-11 17:37:00 -04:00
										 |  |  | def test_create_security_group_without_description_raises_error(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_ec2("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2014-08-25 10:54:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         conn.create_security_group("test security group", "") | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("MissingParameter") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2014-05-11 17:37:00 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2015-03-14 18:50:41 -04:00
										 |  |  | def test_default_security_group(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.ec2.connect_to_region("us-east-1") | 
					
						
							| 
									
										
										
										
											2015-03-14 18:50:41 -04:00
										 |  |  |     groups = conn.get_all_security_groups() | 
					
						
							| 
									
										
										
										
											2017-01-11 17:37:57 -08:00
										 |  |  |     groups.should.have.length_of(2) | 
					
						
							| 
									
										
										
										
											2015-03-14 18:50:41 -04:00
										 |  |  |     groups[0].name.should.equal("default") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2013-10-30 17:55:13 -07:00
										 |  |  | def test_create_and_describe_vpc_security_group(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_ec2("the_key", "the_secret") | 
					
						
							|  |  |  |     vpc_id = "vpc-5300000c" | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     security_group = conn.create_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "test security group", "this is a test security group", vpc_id=vpc_id | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2013-10-30 17:55:13 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     security_group.vpc_id.should.equal(vpc_id) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     security_group.name.should.equal("test security group") | 
					
						
							|  |  |  |     security_group.description.should.equal("this is a test security group") | 
					
						
							| 
									
										
										
										
											2013-10-30 17:55:13 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     # Trying to create another group with the same name in the same VPC should | 
					
						
							|  |  |  |     # throw an error | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |         conn.create_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "test security group", "this is a test security group", vpc_id | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("InvalidGroup.Duplicate") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2013-10-30 17:55:13 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     all_groups = conn.get_all_security_groups(filters={"vpc_id": [vpc_id]}) | 
					
						
							| 
									
										
										
										
											2013-10-30 17:55:13 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     all_groups[0].vpc_id.should.equal(vpc_id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     all_groups.should.have.length_of(1) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     all_groups[0].name.should.equal("test security group") | 
					
						
							| 
									
										
										
										
											2013-10-30 17:55:13 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-11 17:13:48 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2013-10-30 20:11:15 -07:00
										 |  |  | def test_create_two_security_groups_with_same_name_in_different_vpc(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_ec2("the_key", "the_secret") | 
					
						
							|  |  |  |     vpc_id = "vpc-5300000c" | 
					
						
							|  |  |  |     vpc_id2 = "vpc-5300000d" | 
					
						
							| 
									
										
										
										
											2013-10-30 20:11:15 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     conn.create_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "test security group", "this is a test security group", vpc_id | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     conn.create_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "test security group", "this is a test security group", vpc_id2 | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2013-10-30 20:11:15 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     all_groups = conn.get_all_security_groups() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-11 17:37:57 -08:00
										 |  |  |     all_groups.should.have.length_of(4) | 
					
						
							| 
									
										
										
										
											2015-03-14 18:50:41 -04:00
										 |  |  |     group_names = [group.name for group in all_groups] | 
					
						
							|  |  |  |     # The default group is created automatically | 
					
						
							|  |  |  |     set(group_names).should.equal(set(["default", "test security group"])) | 
					
						
							| 
									
										
										
										
											2013-10-30 20:11:15 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-23 15:26:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-18 17:29:13 -05:00
										 |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_create_two_security_groups_in_vpc_with_ipv6_enabled(): | 
					
						
							|  |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							|  |  |  |     vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16", AmazonProvidedIpv6CidrBlock=True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     security_group = ec2.create_security_group( | 
					
						
							|  |  |  |         GroupName="sg01", Description="Test security group sg01", VpcId=vpc.id | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-01 08:23:31 -05:00
										 |  |  |     # The security group must have two defaul egress rules (one for ipv4 and aonther for ipv6) | 
					
						
							| 
									
										
										
										
											2019-12-18 17:29:13 -05:00
										 |  |  |     security_group.ip_permissions_egress.should.have.length_of(2) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2013-02-23 15:26:54 -05:00
										 |  |  | def test_deleting_security_groups(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_ec2("the_key", "the_secret") | 
					
						
							|  |  |  |     security_group1 = conn.create_security_group("test1", "test1") | 
					
						
							|  |  |  |     conn.create_security_group("test2", "test2") | 
					
						
							| 
									
										
										
										
											2013-02-23 15:26:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-11 17:37:57 -08:00
										 |  |  |     conn.get_all_security_groups().should.have.length_of(4) | 
					
						
							| 
									
										
										
										
											2013-02-23 15:26:54 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Deleting a group that doesn't exist should throw an error | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         conn.delete_security_group("foobar") | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("InvalidGroup.NotFound") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2013-02-23 15:26:54 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Delete by name | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as ex: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         conn.delete_security_group("test2", dry_run=True) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     ex.value.error_code.should.equal("DryRunOperation") | 
					
						
							|  |  |  |     ex.value.status.should.equal(400) | 
					
						
							|  |  |  |     ex.value.message.should.equal( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "An error occurred (DryRunOperation) when calling the DeleteSecurityGroup operation: Request would have succeeded, but DryRun flag is set" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn.delete_security_group("test2") | 
					
						
							| 
									
										
										
										
											2017-01-11 17:37:57 -08:00
										 |  |  |     conn.get_all_security_groups().should.have.length_of(3) | 
					
						
							| 
									
										
										
										
											2013-02-23 15:26:54 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Delete by group id | 
					
						
							| 
									
										
										
										
											2013-12-06 14:34:13 -08:00
										 |  |  |     conn.delete_security_group(group_id=security_group1.id) | 
					
						
							| 
									
										
										
										
											2017-01-11 17:37:57 -08:00
										 |  |  |     conn.get_all_security_groups().should.have.length_of(2) | 
					
						
							| 
									
										
										
										
											2013-02-23 16:27:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-11 17:13:48 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2013-12-05 17:00:35 -08:00
										 |  |  | def test_delete_security_group_in_vpc(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_ec2("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2013-12-05 17:00:35 -08:00
										 |  |  |     vpc_id = "vpc-12345" | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     security_group1 = conn.create_security_group("test1", "test1", vpc_id) | 
					
						
							| 
									
										
										
										
											2013-12-05 17:00:35 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-05 17:56:46 -08:00
										 |  |  |     # this should not throw an exception | 
					
						
							| 
									
										
										
										
											2013-12-06 14:34:13 -08:00
										 |  |  |     conn.delete_security_group(group_id=security_group1.id) | 
					
						
							| 
									
										
										
										
											2013-12-05 17:00:35 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-23 16:27:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2013-02-23 16:27:43 -05:00
										 |  |  | def test_authorize_ip_range_and_revoke(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_ec2("the_key", "the_secret") | 
					
						
							|  |  |  |     security_group = conn.create_security_group("test", "test") | 
					
						
							| 
									
										
										
										
											2013-02-23 16:27:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as ex: | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |         success = security_group.authorize( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             ip_protocol="tcp", | 
					
						
							|  |  |  |             from_port="22", | 
					
						
							|  |  |  |             to_port="2222", | 
					
						
							|  |  |  |             cidr_ip="123.123.123.123/32", | 
					
						
							|  |  |  |             dry_run=True, | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     ex.value.error_code.should.equal("DryRunOperation") | 
					
						
							|  |  |  |     ex.value.status.should.equal(400) | 
					
						
							|  |  |  |     ex.value.message.should.equal( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "An error occurred (DryRunOperation) when calling the GrantSecurityGroupIngress operation: Request would have succeeded, but DryRun flag is set" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     success = security_group.authorize( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         ip_protocol="tcp", from_port="22", to_port="2222", cidr_ip="123.123.123.123/32" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2013-02-23 16:27:43 -05:00
										 |  |  |     assert success.should.be.true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     security_group = conn.get_all_security_groups(groupnames=["test"])[0] | 
					
						
							| 
									
										
										
										
											2013-02-23 16:27:43 -05:00
										 |  |  |     int(security_group.rules[0].to_port).should.equal(2222) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     security_group.rules[0].grants[0].cidr_ip.should.equal("123.123.123.123/32") | 
					
						
							| 
									
										
										
										
											2013-02-23 16:27:43 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Wrong Cidr should throw error | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         security_group.revoke( | 
					
						
							|  |  |  |             ip_protocol="tcp", | 
					
						
							|  |  |  |             from_port="22", | 
					
						
							|  |  |  |             to_port="2222", | 
					
						
							|  |  |  |             cidr_ip="123.123.123.122/32", | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("InvalidPermission.NotFound") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2013-02-23 16:27:43 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Actually revoke | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as ex: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         security_group.revoke( | 
					
						
							|  |  |  |             ip_protocol="tcp", | 
					
						
							|  |  |  |             from_port="22", | 
					
						
							|  |  |  |             to_port="2222", | 
					
						
							|  |  |  |             cidr_ip="123.123.123.123/32", | 
					
						
							|  |  |  |             dry_run=True, | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     ex.value.error_code.should.equal("DryRunOperation") | 
					
						
							|  |  |  |     ex.value.status.should.equal(400) | 
					
						
							|  |  |  |     ex.value.message.should.equal( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "An error occurred (DryRunOperation) when calling the RevokeSecurityGroupIngress operation: Request would have succeeded, but DryRun flag is set" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     security_group.revoke( | 
					
						
							|  |  |  |         ip_protocol="tcp", from_port="22", to_port="2222", cidr_ip="123.123.123.123/32" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2013-02-23 16:27:43 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     security_group = conn.get_all_security_groups()[0] | 
					
						
							|  |  |  |     security_group.rules.should.have.length_of(0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-15 12:36:11 -08:00
										 |  |  |     # Test for egress as well | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     egress_security_group = conn.create_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "testegress", "testegress", vpc_id="vpc-3432589" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as ex: | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |         success = conn.authorize_security_group_egress( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             egress_security_group.id, | 
					
						
							|  |  |  |             "tcp", | 
					
						
							|  |  |  |             from_port="22", | 
					
						
							|  |  |  |             to_port="2222", | 
					
						
							|  |  |  |             cidr_ip="123.123.123.123/32", | 
					
						
							|  |  |  |             dry_run=True, | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     ex.value.error_code.should.equal("DryRunOperation") | 
					
						
							|  |  |  |     ex.value.status.should.equal(400) | 
					
						
							|  |  |  |     ex.value.message.should.equal( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "An error occurred (DryRunOperation) when calling the GrantSecurityGroupEgress operation: Request would have succeeded, but DryRun flag is set" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     success = conn.authorize_security_group_egress( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         egress_security_group.id, | 
					
						
							|  |  |  |         "tcp", | 
					
						
							|  |  |  |         from_port="22", | 
					
						
							|  |  |  |         to_port="2222", | 
					
						
							|  |  |  |         cidr_ip="123.123.123.123/32", | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-01-15 12:36:11 -08:00
										 |  |  |     assert success.should.be.true | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     egress_security_group = conn.get_all_security_groups(groupnames="testegress")[0] | 
					
						
							| 
									
										
										
										
											2016-04-20 23:01:09 +02:00
										 |  |  |     # There are two egress rules associated with the security group: | 
					
						
							|  |  |  |     # the default outbound rule and the new one | 
					
						
							| 
									
										
										
										
											2016-04-19 23:50:46 +02:00
										 |  |  |     int(egress_security_group.rules_egress[1].to_port).should.equal(2222) | 
					
						
							| 
									
										
										
										
											2020-08-04 11:20:57 +05:30
										 |  |  |     actual_cidr = egress_security_group.rules_egress[1].grants[0].cidr_ip | 
					
						
							|  |  |  |     # Deal with Python2 dict->unicode, instead of dict->string | 
					
						
							| 
									
										
										
										
											2020-08-26 08:27:45 -05:00
										 |  |  |     if type(actual_cidr) == "unicode": | 
					
						
							|  |  |  |         actual_cidr = json.loads(actual_cidr.replace("u'", "'").replace("'", '"')) | 
					
						
							|  |  |  |     actual_cidr.should.equal("123.123.123.123/32") | 
					
						
							| 
									
										
										
										
											2016-01-15 12:36:11 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Wrong Cidr should throw error | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     egress_security_group.revoke.when.called_with( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         ip_protocol="tcp", from_port="22", to_port="2222", cidr_ip="123.123.123.122/32" | 
					
						
							|  |  |  |     ).should.throw(EC2ResponseError) | 
					
						
							| 
									
										
										
										
											2016-01-15 12:36:11 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Actually revoke | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as ex: | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |         conn.revoke_security_group_egress( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             egress_security_group.id, | 
					
						
							|  |  |  |             "tcp", | 
					
						
							|  |  |  |             from_port="22", | 
					
						
							|  |  |  |             to_port="2222", | 
					
						
							|  |  |  |             cidr_ip="123.123.123.123/32", | 
					
						
							|  |  |  |             dry_run=True, | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     ex.value.error_code.should.equal("DryRunOperation") | 
					
						
							|  |  |  |     ex.value.status.should.equal(400) | 
					
						
							|  |  |  |     ex.value.message.should.equal( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "An error occurred (DryRunOperation) when calling the RevokeSecurityGroupEgress operation: Request would have succeeded, but DryRun flag is set" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     conn.revoke_security_group_egress( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         egress_security_group.id, | 
					
						
							|  |  |  |         "tcp", | 
					
						
							|  |  |  |         from_port="22", | 
					
						
							|  |  |  |         to_port="2222", | 
					
						
							|  |  |  |         cidr_ip="123.123.123.123/32", | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-01-15 12:36:11 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     egress_security_group = conn.get_all_security_groups()[0] | 
					
						
							| 
									
										
										
										
											2016-04-20 23:01:09 +02:00
										 |  |  |     # There is still the default outbound rule | 
					
						
							| 
									
										
										
										
											2016-04-19 23:50:46 +02:00
										 |  |  |     egress_security_group.rules_egress.should.have.length_of(1) | 
					
						
							| 
									
										
										
										
											2016-01-15 12:36:11 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-26 00:31:01 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2013-02-23 16:27:43 -05:00
										 |  |  | def test_authorize_other_group_and_revoke(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_ec2("the_key", "the_secret") | 
					
						
							|  |  |  |     security_group = conn.create_security_group("test", "test") | 
					
						
							|  |  |  |     other_security_group = conn.create_security_group("other", "other") | 
					
						
							|  |  |  |     wrong_group = conn.create_security_group("wrong", "wrong") | 
					
						
							| 
									
										
										
										
											2013-02-23 16:27:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     success = security_group.authorize( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         ip_protocol="tcp", | 
					
						
							|  |  |  |         from_port="22", | 
					
						
							|  |  |  |         to_port="2222", | 
					
						
							|  |  |  |         src_group=other_security_group, | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2013-02-23 16:27:43 -05:00
										 |  |  |     assert success.should.be.true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     security_group = [ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         group for group in conn.get_all_security_groups() if group.name == "test" | 
					
						
							|  |  |  |     ][0] | 
					
						
							| 
									
										
										
										
											2013-02-23 16:27:43 -05:00
										 |  |  |     int(security_group.rules[0].to_port).should.equal(2222) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     security_group.rules[0].grants[0].group_id.should.equal(other_security_group.id) | 
					
						
							| 
									
										
										
										
											2013-02-23 16:27:43 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Wrong source group should throw error | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         security_group.revoke( | 
					
						
							|  |  |  |             ip_protocol="tcp", from_port="22", to_port="2222", src_group=wrong_group | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("InvalidPermission.NotFound") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2013-02-23 16:27:43 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Actually revoke | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     security_group.revoke( | 
					
						
							|  |  |  |         ip_protocol="tcp", | 
					
						
							|  |  |  |         from_port="22", | 
					
						
							|  |  |  |         to_port="2222", | 
					
						
							|  |  |  |         src_group=other_security_group, | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2013-02-23 16:27:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     security_group = [ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         group for group in conn.get_all_security_groups() if group.name == "test" | 
					
						
							|  |  |  |     ][0] | 
					
						
							| 
									
										
										
										
											2013-02-23 16:27:43 -05:00
										 |  |  |     security_group.rules.should.have.length_of(0) | 
					
						
							| 
									
										
										
										
											2014-03-20 16:22:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-11 17:13:48 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-20 23:01:09 +02:00
										 |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_authorize_other_group_egress_and_revoke(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2016-04-20 23:01:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16") | 
					
						
							| 
									
										
										
										
											2016-04-20 23:01:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     sg01 = ec2.create_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         GroupName="sg01", Description="Test security group sg01", VpcId=vpc.id | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     sg02 = ec2.create_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         GroupName="sg02", Description="Test security group sg02", VpcId=vpc.id | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-04-20 23:01:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ip_permission = { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "IpProtocol": "tcp", | 
					
						
							|  |  |  |         "FromPort": 27017, | 
					
						
							|  |  |  |         "ToPort": 27017, | 
					
						
							|  |  |  |         "UserIdGroupPairs": [ | 
					
						
							|  |  |  |             {"GroupId": sg02.id, "GroupName": "sg02", "UserId": sg02.owner_id} | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |         "IpRanges": [], | 
					
						
							| 
									
										
										
										
											2016-04-20 23:01:09 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     sg01.authorize_egress(IpPermissions=[ip_permission]) | 
					
						
							|  |  |  |     sg01.ip_permissions_egress.should.have.length_of(2) | 
					
						
							|  |  |  |     sg01.ip_permissions_egress.should.contain(ip_permission) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     sg01.revoke_egress(IpPermissions=[ip_permission]) | 
					
						
							|  |  |  |     sg01.ip_permissions_egress.should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-11 17:37:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-03-20 17:26:08 -07:00
										 |  |  | def test_authorize_group_in_vpc(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_ec2("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2014-03-20 16:22:37 -07:00
										 |  |  |     vpc_id = "vpc-12345" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # create 2 groups in a vpc | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     security_group = conn.create_security_group("test1", "test1", vpc_id) | 
					
						
							|  |  |  |     other_security_group = conn.create_security_group("test2", "test2", vpc_id) | 
					
						
							| 
									
										
										
										
											2014-03-20 16:22:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     success = security_group.authorize( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         ip_protocol="tcp", | 
					
						
							|  |  |  |         from_port="22", | 
					
						
							|  |  |  |         to_port="2222", | 
					
						
							|  |  |  |         src_group=other_security_group, | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2014-03-21 13:31:00 -07:00
										 |  |  |     success.should.be.true | 
					
						
							| 
									
										
										
										
											2014-03-20 16:22:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-11 17:13:48 -04:00
										 |  |  |     # Check that the rule is accurate | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     security_group = [ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         group for group in conn.get_all_security_groups() if group.name == "test1" | 
					
						
							|  |  |  |     ][0] | 
					
						
							| 
									
										
										
										
											2014-05-11 17:13:48 -04:00
										 |  |  |     int(security_group.rules[0].to_port).should.equal(2222) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     security_group.rules[0].grants[0].group_id.should.equal(other_security_group.id) | 
					
						
							| 
									
										
										
										
											2014-03-20 16:22:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-20 23:01:09 +02:00
										 |  |  |     # Now remove the rule | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     success = security_group.revoke( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         ip_protocol="tcp", | 
					
						
							|  |  |  |         from_port="22", | 
					
						
							|  |  |  |         to_port="2222", | 
					
						
							|  |  |  |         src_group=other_security_group, | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2014-05-11 17:13:48 -04:00
										 |  |  |     success.should.be.true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # And check that it gets revoked | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     security_group = [ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         group for group in conn.get_all_security_groups() if group.name == "test1" | 
					
						
							|  |  |  |     ][0] | 
					
						
							| 
									
										
										
										
											2014-05-11 17:13:48 -04:00
										 |  |  |     security_group.rules.should.have.length_of(0) | 
					
						
							| 
									
										
										
										
											2014-08-21 19:04:48 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-08-21 19:04:48 -05:00
										 |  |  | def test_get_all_security_groups(): | 
					
						
							|  |  |  |     conn = boto.connect_ec2() | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     sg1 = conn.create_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         name="test1", description="test1", vpc_id="vpc-mjm05d27" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     conn.create_security_group(name="test2", description="test2") | 
					
						
							| 
									
										
										
										
											2014-08-21 19:04:48 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp = conn.get_all_security_groups(groupnames=["test1"]) | 
					
						
							| 
									
										
										
										
											2014-08-21 19:04:48 -05:00
										 |  |  |     resp.should.have.length_of(1) | 
					
						
							| 
									
										
										
										
											2014-09-10 09:42:38 -07:00
										 |  |  |     resp[0].id.should.equal(sg1.id) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         conn.get_all_security_groups(groupnames=["does_not_exist"]) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("InvalidGroup.NotFound") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2017-09-18 19:51:01 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |     resp.should.have.length_of(1) | 
					
						
							|  |  |  |     resp[0].id.should.equal(sg1.id) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp = conn.get_all_security_groups(filters={"vpc-id": ["vpc-mjm05d27"]}) | 
					
						
							| 
									
										
										
										
											2014-09-10 09:42:38 -07:00
										 |  |  |     resp.should.have.length_of(1) | 
					
						
							|  |  |  |     resp[0].id.should.equal(sg1.id) | 
					
						
							| 
									
										
										
										
											2014-08-21 19:04:48 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp = conn.get_all_security_groups(filters={"vpc_id": ["vpc-mjm05d27"]}) | 
					
						
							| 
									
										
										
										
											2014-08-21 19:04:48 -05:00
										 |  |  |     resp.should.have.length_of(1) | 
					
						
							| 
									
										
										
										
											2014-09-10 09:42:38 -07:00
										 |  |  |     resp[0].id.should.equal(sg1.id) | 
					
						
							| 
									
										
										
										
											2014-08-21 19:04:48 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp = conn.get_all_security_groups(filters={"description": ["test1"]}) | 
					
						
							| 
									
										
										
										
											2014-08-21 19:04:48 -05:00
										 |  |  |     resp.should.have.length_of(1) | 
					
						
							| 
									
										
										
										
											2014-09-10 09:42:38 -07:00
										 |  |  |     resp[0].id.should.equal(sg1.id) | 
					
						
							| 
									
										
										
										
											2014-08-21 19:04:48 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp = conn.get_all_security_groups() | 
					
						
							| 
									
										
										
										
											2017-01-11 17:37:57 -08:00
										 |  |  |     resp.should.have.length_of(4) | 
					
						
							| 
									
										
										
										
											2014-10-06 14:42:12 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-10-06 14:42:12 -04:00
										 |  |  | def test_authorize_bad_cidr_throws_invalid_parameter_value(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_ec2("the_key", "the_secret") | 
					
						
							|  |  |  |     security_group = conn.create_security_group("test", "test") | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |         security_group.authorize( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             ip_protocol="tcp", from_port="22", to_port="2222", cidr_ip="123.123.123.123" | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("InvalidParameterValue") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2015-02-14 08:57:14 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2015-02-14 08:57:14 -05:00
										 |  |  | def test_security_group_tagging(): | 
					
						
							|  |  |  |     conn = boto.connect_vpc() | 
					
						
							|  |  |  |     vpc = conn.create_vpc("10.0.0.0/16") | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-14 08:57:14 -05:00
										 |  |  |     sg = conn.create_security_group("test-sg", "Test SG", vpc.id) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as ex: | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  |         sg.add_tag("Test", "Tag", dry_run=True) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     ex.value.error_code.should.equal("DryRunOperation") | 
					
						
							|  |  |  |     ex.value.status.should.equal(400) | 
					
						
							|  |  |  |     ex.value.message.should.equal( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "An error occurred (DryRunOperation) when calling the CreateTags operation: Request would have succeeded, but DryRun flag is set" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-14 08:57:14 -05:00
										 |  |  |     sg.add_tag("Test", "Tag") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     tag = conn.get_all_tags()[0] | 
					
						
							|  |  |  |     tag.name.should.equal("Test") | 
					
						
							|  |  |  |     tag.value.should.equal("Tag") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     group = conn.get_all_security_groups("test-sg")[0] | 
					
						
							|  |  |  |     group.tags.should.have.length_of(1) | 
					
						
							|  |  |  |     group.tags["Test"].should.equal("Tag") | 
					
						
							| 
									
										
										
										
											2015-02-24 17:56:26 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2015-02-24 17:56:26 -05:00
										 |  |  | def test_security_group_tag_filtering(): | 
					
						
							|  |  |  |     conn = boto.connect_ec2() | 
					
						
							|  |  |  |     sg = conn.create_security_group("test-sg", "Test SG") | 
					
						
							|  |  |  |     sg.add_tag("test-tag", "test-value") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     groups = conn.get_all_security_groups(filters={"tag:test-tag": "test-value"}) | 
					
						
							| 
									
										
										
										
											2015-02-24 17:56:26 -05:00
										 |  |  |     groups.should.have.length_of(1) | 
					
						
							| 
									
										
										
										
											2016-04-26 10:53:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2016-04-26 10:53:18 +02:00
										 |  |  | def test_authorize_all_protocols_with_no_port_specification(): | 
					
						
							|  |  |  |     conn = boto.connect_ec2() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     sg = conn.create_security_group("test", "test") | 
					
						
							| 
									
										
										
										
											2016-04-26 10:53:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     success = sg.authorize(ip_protocol="-1", cidr_ip="0.0.0.0/0") | 
					
						
							| 
									
										
										
										
											2016-04-26 10:53:18 +02:00
										 |  |  |     success.should.be.true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     sg = conn.get_all_security_groups("test")[0] | 
					
						
							| 
									
										
										
										
											2016-04-26 10:53:18 +02:00
										 |  |  |     sg.rules[0].from_port.should.equal(None) | 
					
						
							|  |  |  |     sg.rules[0].to_port.should.equal(None) | 
					
						
							| 
									
										
										
										
											2016-06-23 12:03:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-11 17:37:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  | def test_sec_group_rule_limit(): | 
					
						
							|  |  |  |     ec2_conn = boto.connect_ec2() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     sg = ec2_conn.create_security_group("test", "test") | 
					
						
							|  |  |  |     other_sg = ec2_conn.create_security_group("test_2", "test_other") | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # INGRESS | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  |         ec2_conn.authorize_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             group_id=sg.id, | 
					
						
							|  |  |  |             ip_protocol="-1", | 
					
						
							|  |  |  |             cidr_ip=["{0}.0.0.0/0".format(i) for i in range(110)], | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.error_code.should.equal("RulesPerSecurityGroupLimitExceeded") | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     sg.rules.should.be.empty | 
					
						
							|  |  |  |     # authorize a rule targeting a different sec group (because this count too) | 
					
						
							|  |  |  |     success = ec2_conn.authorize_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         group_id=sg.id, ip_protocol="-1", src_security_group_group_id=other_sg.id | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  |     success.should.be.true | 
					
						
							|  |  |  |     # fill the rules up the limit | 
					
						
							|  |  |  |     success = ec2_conn.authorize_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         group_id=sg.id, | 
					
						
							|  |  |  |         ip_protocol="-1", | 
					
						
							|  |  |  |         cidr_ip=["{0}.0.0.0/0".format(i) for i in range(99)], | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  |     success.should.be.true | 
					
						
							|  |  |  |     # verify that we cannot authorize past the limit for a CIDR IP | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  |         ec2_conn.authorize_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             group_id=sg.id, ip_protocol="-1", cidr_ip=["100.0.0.0/0"] | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.error_code.should.equal("RulesPerSecurityGroupLimitExceeded") | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  |     # verify that we cannot authorize past the limit for a different sec group | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  |         ec2_conn.authorize_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             group_id=sg.id, ip_protocol="-1", src_security_group_group_id=other_sg.id | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.error_code.should.equal("RulesPerSecurityGroupLimitExceeded") | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # EGRESS | 
					
						
							|  |  |  |     # authorize a rule targeting a different sec group (because this count too) | 
					
						
							|  |  |  |     ec2_conn.authorize_security_group_egress( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         group_id=sg.id, ip_protocol="-1", src_group_id=other_sg.id | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  |     # fill the rules up the limit | 
					
						
							|  |  |  |     # remember that by default, when created a sec group contains 1 egress rule | 
					
						
							|  |  |  |     # so our other_sg rule + 98 CIDR IP rules + 1 by default == 100 the limit | 
					
						
							| 
									
										
										
										
											2021-03-26 04:23:07 -07:00
										 |  |  |     for i in range(1, 99): | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  |         ec2_conn.authorize_security_group_egress( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             group_id=sg.id, ip_protocol="-1", cidr_ip="{0}.0.0.0/0".format(i) | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  |     # verify that we cannot authorize past the limit for a CIDR IP | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  |         ec2_conn.authorize_security_group_egress( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             group_id=sg.id, ip_protocol="-1", cidr_ip="101.0.0.0/0" | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.error_code.should.equal("RulesPerSecurityGroupLimitExceeded") | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  |     # verify that we cannot authorize past the limit for a different sec group | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  |         ec2_conn.authorize_security_group_egress( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             group_id=sg.id, ip_protocol="-1", src_group_id=other_sg.id | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.error_code.should.equal("RulesPerSecurityGroupLimitExceeded") | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  | def test_sec_group_rule_limit_vpc(): | 
					
						
							|  |  |  |     ec2_conn = boto.connect_ec2() | 
					
						
							|  |  |  |     vpc_conn = boto.connect_vpc() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc = vpc_conn.create_vpc("10.0.0.0/16") | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     sg = ec2_conn.create_security_group("test", "test", vpc_id=vpc.id) | 
					
						
							|  |  |  |     other_sg = ec2_conn.create_security_group("test_2", "test", vpc_id=vpc.id) | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # INGRESS | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  |         ec2_conn.authorize_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             group_id=sg.id, | 
					
						
							|  |  |  |             ip_protocol="-1", | 
					
						
							|  |  |  |             cidr_ip=["{0}.0.0.0/0".format(i) for i in range(110)], | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.error_code.should.equal("RulesPerSecurityGroupLimitExceeded") | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     sg.rules.should.be.empty | 
					
						
							|  |  |  |     # authorize a rule targeting a different sec group (because this count too) | 
					
						
							|  |  |  |     success = ec2_conn.authorize_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         group_id=sg.id, ip_protocol="-1", src_security_group_group_id=other_sg.id | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  |     success.should.be.true | 
					
						
							|  |  |  |     # fill the rules up the limit | 
					
						
							|  |  |  |     success = ec2_conn.authorize_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         group_id=sg.id, | 
					
						
							|  |  |  |         ip_protocol="-1", | 
					
						
							|  |  |  |         cidr_ip=["{0}.0.0.0/0".format(i) for i in range(49)], | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  |     # verify that we cannot authorize past the limit for a CIDR IP | 
					
						
							|  |  |  |     success.should.be.true | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  |         ec2_conn.authorize_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             group_id=sg.id, ip_protocol="-1", cidr_ip=["100.0.0.0/0"] | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.error_code.should.equal("RulesPerSecurityGroupLimitExceeded") | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  |     # verify that we cannot authorize past the limit for a different sec group | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  |         ec2_conn.authorize_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             group_id=sg.id, ip_protocol="-1", src_security_group_group_id=other_sg.id | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.error_code.should.equal("RulesPerSecurityGroupLimitExceeded") | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # EGRESS | 
					
						
							|  |  |  |     # authorize a rule targeting a different sec group (because this count too) | 
					
						
							|  |  |  |     ec2_conn.authorize_security_group_egress( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         group_id=sg.id, ip_protocol="-1", src_group_id=other_sg.id | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  |     # fill the rules up the limit | 
					
						
							|  |  |  |     # remember that by default, when created a sec group contains 1 egress rule | 
					
						
							|  |  |  |     # so our other_sg rule + 48 CIDR IP rules + 1 by default == 50 the limit | 
					
						
							| 
									
										
										
										
											2021-03-26 04:23:07 -07:00
										 |  |  |     for i in range(1, 49): | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  |         ec2_conn.authorize_security_group_egress( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             group_id=sg.id, ip_protocol="-1", cidr_ip="{0}.0.0.0/0".format(i) | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  |     # verify that we cannot authorize past the limit for a CIDR IP | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  |         ec2_conn.authorize_security_group_egress( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             group_id=sg.id, ip_protocol="-1", cidr_ip="50.0.0.0/0" | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.error_code.should.equal("RulesPerSecurityGroupLimitExceeded") | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  |     # verify that we cannot authorize past the limit for a different sec group | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  |         ec2_conn.authorize_security_group_egress( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             group_id=sg.id, ip_protocol="-1", src_group_id=other_sg.id | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.error_code.should.equal("RulesPerSecurityGroupLimitExceeded") | 
					
						
							| 
									
										
										
										
											2017-01-19 02:37:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  | """
 | 
					
						
							| 
									
										
										
										
											2016-06-23 12:03:29 +01:00
										 |  |  | Boto3 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  | """
 | 
					
						
							| 
									
										
										
										
											2016-06-23 12:03:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-11 22:35:27 -05:00
										 |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_add_same_rule_twice_throws_error(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2017-01-11 22:35:27 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16") | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     sg = ec2.create_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         GroupName="sg1", Description="Test security group sg1", VpcId=vpc.id | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-01-11 22:35:27 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-26 04:23:07 -07:00
										 |  |  |     # Ingress | 
					
						
							| 
									
										
										
										
											2017-01-11 22:35:27 -05:00
										 |  |  |     ip_permissions = [ | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "IpProtocol": "tcp", | 
					
						
							|  |  |  |             "FromPort": 27017, | 
					
						
							|  |  |  |             "ToPort": 27017, | 
					
						
							|  |  |  |             "IpRanges": [{"CidrIp": "1.2.3.4/32"}], | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-01-11 22:35:27 -05:00
										 |  |  |     ] | 
					
						
							|  |  |  |     sg.authorize_ingress(IpPermissions=ip_permissions) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(ClientError) as ex: | 
					
						
							| 
									
										
										
										
											2017-01-11 22:35:27 -05:00
										 |  |  |         sg.authorize_ingress(IpPermissions=ip_permissions) | 
					
						
							| 
									
										
										
										
											2021-03-26 04:23:07 -07:00
										 |  |  |     ex.value.response["Error"]["Code"].should.equal("InvalidPermission.Duplicate") | 
					
						
							|  |  |  |     ex.value.response["Error"]["Message"].should.match( | 
					
						
							|  |  |  |         r"^.* specified rule.*already exists$" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Egress | 
					
						
							|  |  |  |     ip_permissions = [ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             "IpProtocol": "-1", | 
					
						
							|  |  |  |             "IpRanges": [{"CidrIp": "0.0.0.0/0"}], | 
					
						
							|  |  |  |             "Ipv6Ranges": [], | 
					
						
							|  |  |  |             "PrefixListIds": [], | 
					
						
							|  |  |  |             "UserIdGroupPairs": [], | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with pytest.raises(ClientError) as ex: | 
					
						
							|  |  |  |         sg.authorize_egress(IpPermissions=ip_permissions) | 
					
						
							|  |  |  |     ex.value.response["Error"]["Code"].should.equal("InvalidPermission.Duplicate") | 
					
						
							|  |  |  |     ex.value.response["Error"]["Message"].should.match( | 
					
						
							|  |  |  |         r"^.* specified rule.*already exists$" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-01-11 22:35:27 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-23 12:03:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-04 11:20:57 +05:30
										 |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_description_in_ip_permissions(): | 
					
						
							|  |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							|  |  |  |     conn = boto3.client("ec2", region_name="us-east-1") | 
					
						
							|  |  |  |     vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16") | 
					
						
							|  |  |  |     sg = conn.create_security_group( | 
					
						
							|  |  |  |         GroupName="sg1", Description="Test security group sg1", VpcId=vpc.id | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ip_permissions = [ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             "IpProtocol": "tcp", | 
					
						
							|  |  |  |             "FromPort": 27017, | 
					
						
							|  |  |  |             "ToPort": 27017, | 
					
						
							|  |  |  |             "IpRanges": [{"CidrIp": "1.2.3.4/32", "Description": "testDescription"}], | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  |     conn.authorize_security_group_ingress( | 
					
						
							|  |  |  |         GroupId=sg["GroupId"], IpPermissions=ip_permissions | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     result = conn.describe_security_groups(GroupIds=[sg["GroupId"]]) | 
					
						
							| 
									
										
										
										
											2021-01-13 13:36:56 +00:00
										 |  |  |     group = result["SecurityGroups"][0] | 
					
						
							| 
									
										
										
										
											2020-08-04 11:20:57 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 13:36:56 +00:00
										 |  |  |     assert group["IpPermissions"][0]["IpRanges"][0]["Description"] == "testDescription" | 
					
						
							|  |  |  |     assert group["IpPermissions"][0]["IpRanges"][0]["CidrIp"] == "1.2.3.4/32" | 
					
						
							| 
									
										
										
										
											2020-08-04 11:20:57 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |     sg = conn.create_security_group( | 
					
						
							|  |  |  |         GroupName="sg2", Description="Test security group sg1", VpcId=vpc.id | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ip_permissions = [ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             "IpProtocol": "tcp", | 
					
						
							|  |  |  |             "FromPort": 27017, | 
					
						
							|  |  |  |             "ToPort": 27017, | 
					
						
							|  |  |  |             "IpRanges": [{"CidrIp": "1.2.3.4/32"}], | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  |     conn.authorize_security_group_ingress( | 
					
						
							|  |  |  |         GroupId=sg["GroupId"], IpPermissions=ip_permissions | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     result = conn.describe_security_groups(GroupIds=[sg["GroupId"]]) | 
					
						
							| 
									
										
										
										
											2021-01-13 13:36:56 +00:00
										 |  |  |     group = result["SecurityGroups"][0] | 
					
						
							| 
									
										
										
										
											2020-08-04 11:20:57 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 13:36:56 +00:00
										 |  |  |     assert group["IpPermissions"][0]["IpRanges"][0].get("Description") is None | 
					
						
							|  |  |  |     assert group["IpPermissions"][0]["IpRanges"][0]["CidrIp"] == "1.2.3.4/32" | 
					
						
							| 
									
										
										
										
											2020-08-04 11:20:57 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-23 12:03:29 +01:00
										 |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_security_group_tagging_boto3(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto3.client("ec2", region_name="us-east-1") | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-23 12:03:29 +01:00
										 |  |  |     sg = conn.create_security_group(GroupName="test-sg", Description="Test SG") | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(ClientError) as ex: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         conn.create_tags( | 
					
						
							|  |  |  |             Resources=[sg["GroupId"]], | 
					
						
							|  |  |  |             Tags=[{"Key": "Test", "Value": "Tag"}], | 
					
						
							|  |  |  |             DryRun=True, | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     ex.value.response["Error"]["Code"].should.equal("DryRunOperation") | 
					
						
							|  |  |  |     ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) | 
					
						
							|  |  |  |     ex.value.response["Error"]["Message"].should.equal( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "An error occurred (DryRunOperation) when calling the CreateTags operation: Request would have succeeded, but DryRun flag is set" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     conn.create_tags(Resources=[sg["GroupId"]], Tags=[{"Key": "Test", "Value": "Tag"}]) | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     describe = conn.describe_security_groups( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         Filters=[{"Name": "tag-value", "Values": ["Tag"]}] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     tag = describe["SecurityGroups"][0]["Tags"][0] | 
					
						
							|  |  |  |     tag["Value"].should.equal("Tag") | 
					
						
							|  |  |  |     tag["Key"].should.equal("Test") | 
					
						
							| 
									
										
										
										
											2016-10-20 18:13:41 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-06 21:55:01 +02:00
										 |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_security_group_wildcard_tag_filter_boto3(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto3.client("ec2", region_name="us-east-1") | 
					
						
							| 
									
										
										
										
											2017-10-06 21:55:01 +02:00
										 |  |  |     sg = conn.create_security_group(GroupName="test-sg", Description="Test SG") | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn.create_tags(Resources=[sg["GroupId"]], Tags=[{"Key": "Test", "Value": "Tag"}]) | 
					
						
							| 
									
										
										
										
											2017-10-06 21:55:01 +02:00
										 |  |  |     describe = conn.describe_security_groups( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         Filters=[{"Name": "tag-value", "Values": ["*"]}] | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-10-06 21:55:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     tag = describe["SecurityGroups"][0]["Tags"][0] | 
					
						
							|  |  |  |     tag["Value"].should.equal("Tag") | 
					
						
							|  |  |  |     tag["Key"].should.equal("Test") | 
					
						
							| 
									
										
										
										
											2017-10-06 21:55:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-11 18:50:15 +01:00
										 |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_security_group_filter_ip_permission(): | 
					
						
							|  |  |  |     ec2 = boto3.resource("ec2", region_name="us-east-1") | 
					
						
							|  |  |  |     vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     conn = boto3.client("ec2", region_name="us-east-1") | 
					
						
							|  |  |  |     sg = ec2.create_security_group( | 
					
						
							|  |  |  |         GroupName="test-sg", Description="Test SG", VpcId=vpc.id | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ip_permissions = [ | 
					
						
							|  |  |  |         {"IpProtocol": "tcp", "FromPort": 27017, "ToPort": 27017, "IpRanges": [],}, | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     sg.authorize_ingress(IpPermissions=ip_permissions) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe = conn.describe_security_groups( | 
					
						
							|  |  |  |         Filters=[{"Name": "ip-permission.from-port", "Values": ["27017"]}] | 
					
						
							|  |  |  |     )["SecurityGroups"] | 
					
						
							|  |  |  |     describe.should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe[0]["GroupName"].should.equal("test-sg") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-20 18:13:41 +00:00
										 |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_authorize_and_revoke_in_bulk(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2016-10-20 18:13:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16") | 
					
						
							| 
									
										
										
										
											2016-10-20 18:13:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     sg01 = ec2.create_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         GroupName="sg01", Description="Test security group sg01", VpcId=vpc.id | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     sg02 = ec2.create_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         GroupName="sg02", Description="Test security group sg02", VpcId=vpc.id | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     sg03 = ec2.create_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         GroupName="sg03", Description="Test security group sg03" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2020-08-26 08:27:45 -05:00
										 |  |  |     sg04 = ec2.create_security_group( | 
					
						
							|  |  |  |         GroupName="sg04", Description="Test security group sg04" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-10-20 18:13:41 +00:00
										 |  |  |     ip_permissions = [ | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "IpProtocol": "tcp", | 
					
						
							|  |  |  |             "FromPort": 27017, | 
					
						
							|  |  |  |             "ToPort": 27017, | 
					
						
							|  |  |  |             "UserIdGroupPairs": [ | 
					
						
							|  |  |  |                 {"GroupId": sg02.id, "GroupName": "sg02", "UserId": sg02.owner_id} | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |             "IpRanges": [], | 
					
						
							| 
									
										
										
										
											2016-10-20 18:13:41 +00:00
										 |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "IpProtocol": "tcp", | 
					
						
							|  |  |  |             "FromPort": 27018, | 
					
						
							|  |  |  |             "ToPort": 27018, | 
					
						
							|  |  |  |             "UserIdGroupPairs": [{"GroupId": sg02.id, "UserId": sg02.owner_id}], | 
					
						
							|  |  |  |             "IpRanges": [], | 
					
						
							| 
									
										
										
										
											2016-10-20 17:25:54 +00:00
										 |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "IpProtocol": "tcp", | 
					
						
							|  |  |  |             "FromPort": 27017, | 
					
						
							|  |  |  |             "ToPort": 27017, | 
					
						
							|  |  |  |             "UserIdGroupPairs": [{"GroupName": "sg03", "UserId": sg03.owner_id}], | 
					
						
							|  |  |  |             "IpRanges": [], | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2020-08-26 08:27:45 -05:00
										 |  |  |         { | 
					
						
							|  |  |  |             "IpProtocol": "tcp", | 
					
						
							|  |  |  |             "FromPort": 27015, | 
					
						
							|  |  |  |             "ToPort": 27015, | 
					
						
							|  |  |  |             "UserIdGroupPairs": [{"GroupName": "sg04", "UserId": sg04.owner_id}], | 
					
						
							|  |  |  |             "IpRanges": [ | 
					
						
							|  |  |  |                 {"CidrIp": "10.10.10.0/24", "Description": "Some Description"} | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             "IpProtocol": "tcp", | 
					
						
							|  |  |  |             "FromPort": 27016, | 
					
						
							|  |  |  |             "ToPort": 27016, | 
					
						
							|  |  |  |             "UserIdGroupPairs": [{"GroupId": sg04.id, "UserId": sg04.owner_id}], | 
					
						
							|  |  |  |             "IpRanges": [{"CidrIp": "10.10.10.0/24"}], | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2016-10-20 18:13:41 +00:00
										 |  |  |     ] | 
					
						
							| 
									
										
										
										
											2016-10-20 17:25:54 +00:00
										 |  |  |     expected_ip_permissions = copy.deepcopy(ip_permissions) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     expected_ip_permissions[1]["UserIdGroupPairs"][0]["GroupName"] = "sg02" | 
					
						
							|  |  |  |     expected_ip_permissions[2]["UserIdGroupPairs"][0]["GroupId"] = sg03.id | 
					
						
							| 
									
										
										
										
											2020-08-26 08:27:45 -05:00
										 |  |  |     expected_ip_permissions[3]["UserIdGroupPairs"][0]["GroupId"] = sg04.id | 
					
						
							|  |  |  |     expected_ip_permissions[4]["UserIdGroupPairs"][0]["GroupName"] = "sg04" | 
					
						
							| 
									
										
										
										
											2016-10-20 18:13:41 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     sg01.authorize_ingress(IpPermissions=ip_permissions) | 
					
						
							| 
									
										
										
										
											2020-08-26 08:27:45 -05:00
										 |  |  |     sg01.ip_permissions.should.have.length_of(5) | 
					
						
							| 
									
										
										
										
											2016-10-20 17:25:54 +00:00
										 |  |  |     for ip_permission in expected_ip_permissions: | 
					
						
							| 
									
										
										
										
											2016-10-20 18:13:41 +00:00
										 |  |  |         sg01.ip_permissions.should.contain(ip_permission) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     sg01.revoke_ingress(IpPermissions=ip_permissions) | 
					
						
							|  |  |  |     sg01.ip_permissions.should.be.empty | 
					
						
							| 
									
										
										
										
											2016-10-20 17:25:54 +00:00
										 |  |  |     for ip_permission in expected_ip_permissions: | 
					
						
							| 
									
										
										
										
											2016-10-20 18:13:41 +00:00
										 |  |  |         sg01.ip_permissions.shouldnt.contain(ip_permission) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     sg01.authorize_egress(IpPermissions=ip_permissions) | 
					
						
							| 
									
										
										
										
											2020-08-26 08:27:45 -05:00
										 |  |  |     sg01.ip_permissions_egress.should.have.length_of(6) | 
					
						
							| 
									
										
										
										
											2016-10-20 17:25:54 +00:00
										 |  |  |     for ip_permission in expected_ip_permissions: | 
					
						
							| 
									
										
										
										
											2016-10-20 18:13:41 +00:00
										 |  |  |         sg01.ip_permissions_egress.should.contain(ip_permission) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     sg01.revoke_egress(IpPermissions=ip_permissions) | 
					
						
							|  |  |  |     sg01.ip_permissions_egress.should.have.length_of(1) | 
					
						
							| 
									
										
										
										
											2016-10-20 17:25:54 +00:00
										 |  |  |     for ip_permission in expected_ip_permissions: | 
					
						
							| 
									
										
										
										
											2016-10-20 18:13:41 +00:00
										 |  |  |         sg01.ip_permissions_egress.shouldnt.contain(ip_permission) | 
					
						
							| 
									
										
										
										
											2017-02-09 13:29:37 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 15:03:07 -04:00
										 |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_security_group_ingress_without_multirule(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2 = boto3.resource("ec2", "ca-central-1") | 
					
						
							|  |  |  |     sg = ec2.create_security_group(Description="Test SG", GroupName="test-sg") | 
					
						
							| 
									
										
										
										
											2018-04-13 15:03:07 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     assert len(sg.ip_permissions) == 0 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     sg.authorize_ingress( | 
					
						
							|  |  |  |         CidrIp="192.168.0.1/32", FromPort=22, ToPort=22, IpProtocol="tcp" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-04-13 15:03:07 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Fails | 
					
						
							|  |  |  |     assert len(sg.ip_permissions) == 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_security_group_ingress_without_multirule_after_reload(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2 = boto3.resource("ec2", "ca-central-1") | 
					
						
							|  |  |  |     sg = ec2.create_security_group(Description="Test SG", GroupName="test-sg") | 
					
						
							| 
									
										
										
										
											2018-04-13 15:03:07 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     assert len(sg.ip_permissions) == 0 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     sg.authorize_ingress( | 
					
						
							|  |  |  |         CidrIp="192.168.0.1/32", FromPort=22, ToPort=22, IpProtocol="tcp" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-04-13 15:03:07 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Also Fails | 
					
						
							|  |  |  |     sg_after = ec2.SecurityGroup(sg.id) | 
					
						
							|  |  |  |     assert len(sg_after.ip_permissions) == 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2017-02-09 13:29:37 +11:00
										 |  |  | def test_get_all_security_groups_filter_with_same_vpc_id(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_ec2("the_key", "the_secret") | 
					
						
							|  |  |  |     vpc_id = "vpc-5300000c" | 
					
						
							|  |  |  |     security_group = conn.create_security_group("test1", "test1", vpc_id=vpc_id) | 
					
						
							|  |  |  |     security_group2 = conn.create_security_group("test2", "test2", vpc_id=vpc_id) | 
					
						
							| 
									
										
										
										
											2017-02-09 13:29:37 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  |     security_group.vpc_id.should.equal(vpc_id) | 
					
						
							|  |  |  |     security_group2.vpc_id.should.equal(vpc_id) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     security_groups = conn.get_all_security_groups( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         group_ids=[security_group.id], filters={"vpc-id": [vpc_id]} | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-02-09 13:29:37 +11:00
										 |  |  |     security_groups.should.have.length_of(1) | 
					
						
							| 
									
										
										
										
											2017-09-18 19:51:01 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         conn.get_all_security_groups(group_ids=["does_not_exist"]) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("InvalidGroup.NotFound") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2019-11-24 17:17:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_revoke_security_group_egress(): | 
					
						
							|  |  |  |     ec2 = boto3.resource("ec2", "us-east-1") | 
					
						
							|  |  |  |     sg = ec2.create_security_group(Description="Test SG", GroupName="test-sg") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     sg.ip_permissions_egress.should.equal( | 
					
						
							|  |  |  |         [ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 "IpProtocol": "-1", | 
					
						
							|  |  |  |                 "IpRanges": [{"CidrIp": "0.0.0.0/0"}], | 
					
						
							|  |  |  |                 "UserIdGroupPairs": [], | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     sg.revoke_egress( | 
					
						
							|  |  |  |         IpPermissions=[ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 "IpProtocol": "-1", | 
					
						
							| 
									
										
										
										
											2020-08-04 11:20:57 +05:30
										 |  |  |                 "IpRanges": [{"CidrIp": "0.0.0.0/0"}], | 
					
						
							| 
									
										
										
										
											2020-08-26 08:27:45 -05:00
										 |  |  |                 "UserIdGroupPairs": [], | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-11-24 17:17:53 +01:00
										 |  |  |         ] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     sg.reload() | 
					
						
							|  |  |  |     sg.ip_permissions_egress.should.have.length_of(0) | 
					
						
							| 
									
										
										
										
											2021-03-26 06:00:51 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_non_existent_security_group_raises_error_on_authorize(): | 
					
						
							|  |  |  |     client = boto3.client("ec2", "us-east-1") | 
					
						
							|  |  |  |     non_existent_sg = "sg-123abc" | 
					
						
							|  |  |  |     expected_error = "The security group '{}' does not exist".format(non_existent_sg) | 
					
						
							|  |  |  |     authorize_funcs = [ | 
					
						
							|  |  |  |         client.authorize_security_group_egress, | 
					
						
							|  |  |  |         client.authorize_security_group_ingress, | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  |     for authorize_func in authorize_funcs: | 
					
						
							|  |  |  |         with pytest.raises(ClientError) as ex: | 
					
						
							|  |  |  |             authorize_func(GroupId=non_existent_sg, IpPermissions=[{}]) | 
					
						
							|  |  |  |         ex.value.response["Error"]["Code"].should.equal("InvalidGroup.NotFound") | 
					
						
							|  |  |  |         ex.value.response["Error"]["Message"].should.equal(expected_error) | 
					
						
							| 
									
										
										
										
											2021-03-31 11:33:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_security_group_rules_added_via_the_backend_can_be_revoked_via_the_api(): | 
					
						
							|  |  |  |     if settings.TEST_SERVER_MODE: | 
					
						
							|  |  |  |         raise unittest.SkipTest("Can't test backend directly in server mode.") | 
					
						
							|  |  |  |     ec2_resource = boto3.resource("ec2", region_name="us-east-1") | 
					
						
							|  |  |  |     ec2_client = boto3.client("ec2", region_name="us-east-1") | 
					
						
							|  |  |  |     vpc = ec2_resource.create_vpc(CidrBlock="10.0.0.0/16") | 
					
						
							|  |  |  |     group_name = "test-backend-authorize" | 
					
						
							|  |  |  |     sg = ec2_resource.create_security_group( | 
					
						
							|  |  |  |         GroupName=group_name, Description="test", VpcId=vpc.id | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     # Add an ingress/egress rule using the EC2 backend directly. | 
					
						
							|  |  |  |     rule_ingress = { | 
					
						
							|  |  |  |         "group_name_or_id": sg.id, | 
					
						
							|  |  |  |         "from_port": 0, | 
					
						
							|  |  |  |         "ip_protocol": "udp", | 
					
						
							|  |  |  |         "ip_ranges": [], | 
					
						
							|  |  |  |         "to_port": 65535, | 
					
						
							|  |  |  |         "source_group_ids": [sg.id], | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     ec2_backend.authorize_security_group_ingress(**rule_ingress) | 
					
						
							|  |  |  |     rule_egress = { | 
					
						
							|  |  |  |         "group_name_or_id": sg.id, | 
					
						
							|  |  |  |         "from_port": 8443, | 
					
						
							|  |  |  |         "ip_protocol": "tcp", | 
					
						
							|  |  |  |         "ip_ranges": [], | 
					
						
							|  |  |  |         "to_port": 8443, | 
					
						
							|  |  |  |         "source_group_ids": [sg.id], | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     ec2_backend.authorize_security_group_egress(**rule_egress) | 
					
						
							|  |  |  |     # Both rules (plus the default egress) should now be present. | 
					
						
							|  |  |  |     sg = ec2_client.describe_security_groups( | 
					
						
							|  |  |  |         Filters=[{"Name": "group-name", "Values": [group_name]}] | 
					
						
							|  |  |  |     ).get("SecurityGroups")[0] | 
					
						
							|  |  |  |     assert len(sg["IpPermissions"]) == 1 | 
					
						
							|  |  |  |     assert len(sg["IpPermissionsEgress"]) == 2 | 
					
						
							|  |  |  |     # Revoking via the API should work for all rules (even those we added directly). | 
					
						
							|  |  |  |     ec2_client.revoke_security_group_egress( | 
					
						
							|  |  |  |         GroupId=sg["GroupId"], IpPermissions=sg["IpPermissionsEgress"] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     ec2_client.revoke_security_group_ingress( | 
					
						
							|  |  |  |         GroupId=sg["GroupId"], IpPermissions=sg["IpPermissions"] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     sg = ec2_client.describe_security_groups( | 
					
						
							|  |  |  |         Filters=[{"Name": "group-name", "Values": [group_name]}] | 
					
						
							|  |  |  |     ).get("SecurityGroups")[0] | 
					
						
							|  |  |  |     assert len(sg["IpPermissions"]) == 0 | 
					
						
							|  |  |  |     assert len(sg["IpPermissionsEgress"]) == 0 |