Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Improve security group fixture for EC2 #12607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions localstack-core/localstack/testing/pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from werkzeug import Request, Response

from localstack import config
from localstack.aws.api.ec2 import CreateSecurityGroupRequest
from localstack.aws.connect import ServiceLevelClientFactory
from localstack.services.stores import (
AccountRegionBundle,
Expand All @@ -42,7 +43,7 @@
from localstack.utils.aws.client import SigningHttpClient
from localstack.utils.aws.resources import create_dynamodb_table
from localstack.utils.bootstrap import is_api_enabled
from localstack.utils.collections import ensure_list
from localstack.utils.collections import ensure_list, select_from_typed_dict
from localstack.utils.functions import call_safe, run_safe
from localstack.utils.http import safe_requests as requests
from localstack.utils.id_generator import ResourceIdentifier, localstack_id_manager
Expand Down Expand Up @@ -2001,26 +2002,33 @@ def inner(sender_email_address: Optional[str] = None) -> str:
def ec2_create_security_group(aws_client):
ec2_sgs = []

def factory(ports=None, **kwargs):
def factory(ports=None, ip_protocol: str = "tcp", **kwargs):
"""
Create the target group and authorize the security group ingress.
:param ports: list of ports to be authorized for the ingress rule.
:param ip_protocol: the ip protocol for the permissions (tcp by default)
"""
if "GroupName" not in kwargs:
kwargs["GroupName"] = f"test-sg-{short_uid()}"
security_group = aws_client.ec2.create_security_group(**kwargs)

kwargs["GroupName"] = f"sg-{short_uid()}"
# Making sure the call to CreateSecurityGroup gets the right arguments
_args = select_from_typed_dict(CreateSecurityGroupRequest, kwargs)
security_group = aws_client.ec2.create_security_group(**_args)
security_group_id = security_group["GroupId"]
permissions = [
{
"FromPort": port,
"IpProtocol": "tcp",
"IpProtocol": ip_protocol,
"IpRanges": [{"CidrIp": "0.0.0.0/0"}],
"ToPort": port,
}
for port in ports or []
]
aws_client.ec2.authorize_security_group_ingress(
GroupName=kwargs["GroupName"],
GroupId=security_group_id,
IpPermissions=permissions,
)

ec2_sgs.append(security_group["GroupId"])
ec2_sgs.append(security_group_id)
return security_group

yield factory
Expand Down
Loading