Skip to content

Commit a39a38a

Browse files
EC2: Fix unhandled error for nonexistent security groups (#7639)
1 parent 1988b91 commit a39a38a

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

moto/ec2/models/security_groups.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,9 @@ def revoke_security_group_ingress(
812812
group_name_or_id, vpc_id
813813
) # type: ignore[assignment]
814814

815+
if group is None:
816+
raise InvalidSecurityGroupNotFoundError(group_name_or_id)
817+
815818
if security_rule_ids:
816819
group.ingress_rules = [
817820
rule for rule in group.ingress_rules if rule.id not in security_rule_ids
@@ -983,6 +986,9 @@ def revoke_security_group_egress(
983986
group_name_or_id, vpc_id
984987
) # type: ignore[assignment]
985988

989+
if group is None:
990+
raise InvalidSecurityGroupNotFoundError(group_name_or_id)
991+
986992
if security_rule_ids:
987993
group.egress_rules = [
988994
rule for rule in group.egress_rules if rule.id not in security_rule_ids

tests/test_ec2/test_security_groups.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,13 @@ def test_authorize_ip_range_and_revoke():
304304
{"CidrIp": "123.123.123.123/32"}
305305
]
306306

307+
# Bad group must produce error
308+
with pytest.raises(ClientError) as ex:
309+
client.revoke_security_group_egress(GroupId="badcompany")
310+
assert ex.value.response["ResponseMetadata"]["HTTPStatusCode"] == 400
311+
assert "RequestId" in ex.value.response["ResponseMetadata"]
312+
assert ex.value.response["Error"]["Code"] == "InvalidGroup.NotFound"
313+
307314
# Wrong Cidr should throw error
308315
with pytest.raises(ClientError) as ex:
309316
wrong_permissions = copy.deepcopy(egress_permissions)
@@ -380,6 +387,13 @@ def test_authorize_other_group_and_revoke():
380387
assert "RequestId" in ex.value.response["ResponseMetadata"]
381388
assert ex.value.response["Error"]["Code"] == "InvalidGroup.NotFound"
382389

390+
# Bad group must produce error
391+
with pytest.raises(ClientError) as ex:
392+
client.revoke_security_group_ingress(GroupId="badcompany")
393+
assert ex.value.response["ResponseMetadata"]["HTTPStatusCode"] == 400
394+
assert "RequestId" in ex.value.response["ResponseMetadata"]
395+
assert ex.value.response["Error"]["Code"] == "InvalidGroup.NotFound"
396+
383397
# Actually revoke
384398
security_group.revoke_ingress(IpPermissions=permissions)
385399

0 commit comments

Comments
 (0)