55Module to help with defining the network settings for the ECS Service based on the family services definitions.
66"""
77
8+ from __future__ import annotations
9+
10+ from typing import TYPE_CHECKING
11+
12+ if TYPE_CHECKING :
13+ from troposphere import AWSHelperFn
14+ from ecs_composex .common .settings import ComposeXSettings
15+
816import re
917from copy import deepcopy
1018from ipaddress import IPv4Interface
@@ -153,14 +161,8 @@ def set_service_ports(ports):
153161 return service_ports
154162
155163
156- def lookup_security_group (settings , lookup ):
157- """
158- Function to fetch the security group ID based on lookup details
159-
160- :param ecs_composex.common.settings.ComposeXSettings settings:
161- :param lookup:
162- :return:
163- """
164+ def lookup_security_group (settings : ComposeXSettings , lookup : dict | list ) -> str :
165+ """Function to fetch the security group ID based on lookup details"""
164166 sg_re = re .compile (
165167 r"^arn:aws(?:-[a-z]+)?:ec2:[a-z0-9-]+:\d{12}:security-group/([\S]+)$"
166168 )
@@ -239,14 +241,48 @@ def __init__(self, definition, ports):
239241 def __repr__ (self ):
240242 return dumps (self .definition , indent = 2 )
241243
242- def set_aws_sources_ingress (self , settings , destination_title , sg_ref ) -> None :
244+ def handle_security_group_source (
245+ self ,
246+ source ,
247+ common_args : dict ,
248+ destination_title : str ,
249+ target_port : int ,
250+ settings ,
251+ ) -> None :
243252 """
244- Method to define AWS Sources ingresses
245-
246- :param settings:
247- :param destination_title:
248- :param sg_ref:
253+ Method to handle SecurityGroup sources
254+ It updates the list of AWS sources ingress rules that will later be added to the stack template of the family
249255 """
256+ if keyisset ("Id" , source ):
257+ sg_id = source ["Id" ]
258+ elif keyisset ("Lookup" , source ):
259+ sg_id = lookup_security_group (settings , source ["Lookup" ])
260+ else :
261+ raise KeyError (
262+ "Information missing to identify the SecurityGroup. Requires either Id or Lookup"
263+ )
264+ common_args .update (
265+ {
266+ "Description" : Sub (
267+ f"From { sg_id } to { destination_title } on port { target_port } "
268+ )
269+ }
270+ )
271+ self .aws_ingress_rules .append (
272+ SecurityGroupIngress (
273+ f"From{ NONALPHANUM .sub ('' , sg_id )} ToServiceOn{ target_port } " ,
274+ SourceSecurityGroupId = sg_id ,
275+ SourceSecurityGroupOwnerId = set_else_none (
276+ "AccountOwner" , source , Ref (AWS_ACCOUNT_ID )
277+ ),
278+ ** common_args ,
279+ )
280+ )
281+
282+ def set_aws_sources_ingress (
283+ self , settings : ComposeXSettings , destination_title : str , sg_ref : AWSHelperFn
284+ ) -> None :
285+ """Method to define AWS Sources ingresses"""
250286 for source in self .aws_sources :
251287 for port in self .ports :
252288 if (
@@ -269,30 +305,8 @@ def set_aws_sources_ingress(self, settings, destination_title, sg_ref) -> None:
269305 "GroupId" : sg_ref ,
270306 }
271307 if source ["Type" ] == "SecurityGroup" :
272- if keyisset ("Id" , source ):
273- sg_id = source ["Id" ]
274- elif keyisset ("Lookup" , source ):
275- sg_id = lookup_security_group (settings , source ["Lookup" ])
276- else :
277- raise KeyError (
278- "Information missing to identify the SecurityGroup. Requires either Id or Lookup"
279- )
280- common_args .update (
281- {
282- "Description" : Sub (
283- f"From { sg_id } to { destination_title } on port { target_port } "
284- )
285- }
286- )
287- self .aws_ingress_rules .append (
288- SecurityGroupIngress (
289- f"From{ NONALPHANUM .sub ('' , sg_id )} ToServiceOn{ target_port } " ,
290- SourceSecurityGroupId = sg_id ,
291- SourceSecurityGroupOwnerId = set_else_none (
292- "AccountOwner" , source , Ref (AWS_ACCOUNT_ID )
293- ),
294- ** common_args ,
295- )
308+ self .handle_security_group_source (
309+ source , common_args , destination_title , target_port , settings
296310 )
297311 elif source ["Type" ] == "PrefixList" :
298312 self .aws_ingress_rules .append (
@@ -304,7 +318,7 @@ def set_aws_sources_ingress(self, settings, destination_title, sg_ref) -> None:
304318 )
305319
306320 def create_ext_sources_ingress_rule (
307- self , destination_title , allowed_source , security_group , ** props
321+ self , destination_title , allowed_source , security_group : AWSHelperFn , ** props
308322 ) -> None :
309323 """
310324 Creates the Security Ingress rule for a CIDR based rule
0 commit comments