How to use describe_security_groups method in localstack

Best Python code snippet using localstack_python

test_ec2.py

Source:test_ec2.py Github

copy

Full Screen

...44 barrel = EC2Barrel({}, clients=clients)45 tap_return = barrel.tap('describe_instances')46 describe_instances_return = barrel.describe_instances()47 self.assertEqual(describe_instances_return, tap_return)48 def test_tap_functions_with_describe_security_groups(self):49 clients = {50 'us-east-1': self.client_mock([])51 }52 barrel = EC2Barrel({}, clients=clients)53 tap_return = barrel.tap('describe_security_groups')54 describe_security_groups_return = barrel.describe_security_groups()55 self.assertEqual(describe_security_groups_return, tap_return)56 def test_tap_functions_with_high_threat_security_groups(self):57 clients = {58 'us-east-1': self.client_mock([])59 }60 barrel = EC2Barrel({}, clients=clients)61 tap_return = barrel.tap('describe_security_groups')62 describe_security_groups_return = barrel.describe_security_groups()63 self.assertEqual(describe_security_groups_return, tap_return)64 def test_tap_throws_error_with_unsupported_call(self):65 barrel = EC2Barrel({})66 with self.assertRaises(RuntimeError):67 barrel.tap('unsupported_call')68 def test_describe_instances_returns_only_instances(self):69 clients = {70 'us-east-1': self.client_mock(71 boto3_describe_instances_paginator_one_field72 )73 }74 barrel = EC2Barrel({}, clients=clients)75 results = barrel.describe_instances()76 results_from_region = results['us-east-1']77 expected = [78 {79 'InstanceId': 'instance1'80 },81 {82 'InstanceId': 'instance2'83 },84 {85 'InstanceId': 'instance3'86 },87 {88 'InstanceId': 'instance4'89 },90 {91 'InstanceId': 'instance5'92 },93 {94 'InstanceId': 'instance6'95 },96 {97 'InstanceId': 'instance7'98 },99 {100 'InstanceId': 'instance8'101 },102 ]103 self.assertEqual(results_from_region, expected)104 def test_describe_instances_empty_with_no_instances(self):105 fixture = [ # Multiple pages of empty106 {107 'Reservations': [108 {109 'Instances': []110 }111 ]112 }113 ]114 clients = {115 'us-east-1': self.client_mock(fixture)116 }117 barrel = EC2Barrel({}, clients=clients)118 results = barrel.describe_instances()119 expected = {120 'us-east-1': []121 }122 self.assertEqual(results, expected)123 def test_describe_instances_returns_empty_list_with_no_instances_key(self):124 fixture = [ # Multiple pages of empty125 {126 'Reservations': [127 {128 }129 ]130 }131 ]132 clients = {133 'us-east-1': self.client_mock(fixture)134 }135 barrel = EC2Barrel({}, clients=clients)136 results = barrel.describe_instances()137 expected = {138 'us-east-1': []139 }140 self.assertEqual(results, expected)141 def test_describe_security_groups_returns_only_security_groups(self):142 fixture = [143 {144 'SecurityGroups': [145 {146 'GroupName': 'group1'147 },148 {149 'GroupName': 'group2'150 }151 ]152 },153 {154 'SecurityGroups': [155 {156 'GroupName': 'group3'157 },158 {159 'GroupName': 'group4'160 },161 ]162 }163 ]164 clients = {165 'us-east-1': self.client_mock(fixture)166 }167 barrel = EC2Barrel({}, clients=clients)168 results = barrel.describe_security_groups()169 expected = {170 'us-east-1': [171 {172 'GroupName': 'group1'173 },174 {175 'GroupName': 'group2'176 },177 {178 'GroupName': 'group3'179 },180 {181 'GroupName': 'group4'182 }183 ]184 }185 self.assertEqual(results, expected)186 def test_describe_security_groups_empty(self):187 fixture = [188 {189 'SecurityGroups': [190 ]191 }192 ]193 clients = {194 'us-east-1': self.client_mock(fixture)195 }196 barrel = EC2Barrel({}, clients=clients)197 results = barrel.describe_security_groups()198 expected = {199 'us-east-1': []200 }201 self.assertEqual(results, expected)202 def test_describe_security_groups_returns_empty_list_with_missing_key(self):203 fixture = [204 {205 # Security groups key should be here206 }207 ]208 clients = {209 'us-east-1': self.client_mock(fixture)210 }211 barrel = EC2Barrel({}, clients=clients)212 results = barrel.describe_security_groups()213 expected = {214 'us-east-1': []215 }216 self.assertEqual(results, expected)217 def test_high_threat_security_groups_with_no_security_groups(self):218 fixture = [219 {220 }221 ]222 clients = {223 'us-east-1': self.client_mock(fixture)224 }225 barrel = EC2Barrel({}, clients=clients)226 results = barrel.high_threat_security_groups()...

Full Screen

Full Screen

ec2.py

Source:ec2.py Github

copy

Full Screen

...47 for reservation in page['Reservations']:48 instances.extend(reservation.get('Instances', []))49 instances_by_region[region] = instances50 return instances_by_region51 def describe_security_groups(self):52 if self.cache.get('describe_security_groups'):53 return self.cache['describe_security_groups']54 security_groups_by_region = {}55 for region, client in self.clients.items():56 paginator = client.get_paginator('describe_security_groups')57 response_iterator = paginator.paginate()58 groups = []59 for page in response_iterator:60 groups.extend(page.get('SecurityGroups', []))61 security_groups_by_region[region] = groups62 self.cache['describe_security_groups'] = security_groups_by_region63 return security_groups_by_region64 def high_threat_security_groups(self):65 high_threat_groups_by_region = {}66 for region, security_groups in self.describe_security_groups().items():67 high_threat_groups = []68 for security_group in security_groups:69 found_ports = self.find_high_threat_ports(security_group)70 if found_ports:71 high_threat_groups.append({72 'id': security_group['GroupId'],73 'ports': found_ports,74 })75 high_threat_groups_by_region[region] = high_threat_groups76 return high_threat_groups_by_region77 def find_high_threat_ports(self, security_group):78 ports = []79 for rule in security_group['IpPermissions']:80 if rule.get('FromPort') in self.high_threat_ports:...

Full Screen

Full Screen

test_sg_ingress.py

Source:test_sg_ingress.py Github

copy

Full Screen

...8@mock_ec29def setUpModule():10 public_sgs = CustomSG().main()11 client = boto3.client('ec2')12 response = client.describe_security_groups()13 all_sgs = response['SecurityGroups']14 Ec2Actions().evaluate_each_sg(public_sgs, all_sgs)15 16@mock_ec217class TestSGPortIngress(unittest.TestCase):18 def test_check_lowercase_public_sg_cidr(self):19 client = boto3.client('ec2')20 response = client.describe_security_groups(GroupNames=['SG2', 'SG7'])21 security_groups = response['SecurityGroups']22 for security_group in security_groups:23 cidr_ip = security_group['IpPermissions'][0]['IpRanges'][0]['CidrIp']24 self.assertEqual(cidr_ip, '0.0.0.0/0')25 def test_check_open_private_sg_cidr(self):26 client = boto3.client('ec2')27 response = client.describe_security_groups(GroupNames=['SG3', 'SG4', 'SG8', 'SG10'])28 security_groups = response['SecurityGroups']29 for security_group in security_groups:30 cidr_ip = security_group['IpPermissions'][1]['IpRanges'][0]['CidrIp']31 self.assertEqual(cidr_ip, '10.0.0.0/8')32 def test_open_public_sg_cidr(self):33 client = boto3.client('ec2')34 response = client.describe_security_groups(GroupNames=['SG1', 'SG2', 'SG6', 'SG7'])35 security_groups = response['SecurityGroups']36 for security_group in security_groups:37 ip_permissions = security_group['IpPermissions']38 self.assertEqual(len(ip_permissions), 1)39 40 cidr_ip = security_group['IpPermissions'][0]['IpRanges'][0]['CidrIp']...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful