We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
When using ec2.VpnTunnelOptionsSpecification, report error:compile() missing required argument 'filename' (pos 2)
I am using troposphere version 4.3.2
The issue occurs in this code section:
```
  vpn_tunnel_option_props = self.filter_parameters(
      vpn_option.items(), ec2.VpnTunnelOptionsSpecification
  )
```
after I ran it an error occured: compile() missing required argument 'filename' (pos 2)
But it works in troposphere version 3.2.2
I find that troposphere uses re.complile() in the old versions(v3), but the Python system function compile() is used in the new versions(v4), which caused this problem. Is this a bug?
```
def vpn_tunnel_inside_cidr(cidr):
    """
    Property: VpnTunnelOptionsSpecification.TunnelInsideCidr
    """
    cidr_match_re = compile(
        r"^169\.254\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)"
        r"\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\/30$"
    )
```

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `troposphere/validators/ec2.py`
Content:
```
1 # Copyright (c) 2012-2022, Mark Peek <mark@peek.org>
2 # All rights reserved.
3 #
4 # See LICENSE file for full license.
5 
6 
7 from .. import AWSProperty
8 from ..compat import validate_policytype
9 from ..type_defs.compat import Final
10 from . import exactly_one, integer_range, network_port, tags_or_list
11 
12 PERMISSION_INSTANCE_ATTACH: Final = "INSTANCE-ATTACH"
13 PERMISSION_EIP_ASSOCIATE: Final = "EIP-ASSOCIATE"
14 
15 NO_DEVICE: Final = {}
16 
17 
18 def Ipv6Addresses(ipv6_address):
19     """
20     Export:
21     """
22     from ..ec2 import InstanceIpv6Address
23 
24     return InstanceIpv6Address(Ipv6Address=ipv6_address)
25 
26 
27 class Tag(AWSProperty):
28     """
29     Export:
30     """
31 
32     props = {
33         "Key": (str, True),
34         "Value": (str, True),
35     }
36 
37     def __init__(self, key=None, value=None, **kwargs):
38         # provided for backward compatibility
39         if key is not None:
40             kwargs["Key"] = key
41         if value is not None:
42             kwargs["Value"] = value
43         super().__init__(**kwargs)
44 
45 
46 def validate_int_to_str(x):
47     """
48     Backward compatibility - field was int and now str.
49     Property: NetworkInterfaceProperty.DeviceIndex
50     Property: NetworkInterfaceAttachment.DeviceIndex
51     """
52 
53     if isinstance(x, int):
54         return str(x)
55     if isinstance(x, str):
56         return str(int(x))
57 
58     raise TypeError(f"Value {x} of type {type(x)} must be either int or str")
59 
60 
61 def policytypes(policy):
62     """
63     Property: VPCEndpoint.PolicyDocument
64     """
65     return validate_policytype(policy)
66 
67 
68 def validate_networkaclentry_rulenumber(x):
69     """
70     Property: NetworkAclEntry.RuleNumber
71     """
72     return integer_range(1, 32766)(x)
73 
74 
75 def validate_network_port(x):
76     """
77     Property: NetworkAclEntry.Protocol
78     Property: PortRange.From
79     Property: PortRange.To
80     Property: SecurityGroupRule.FromPort
81     Property: SecurityGroupRule.ToPort
82     Property: SecurityGroupEgress.FromPort
83     Property: SecurityGroupEgress.ToPort
84     Property: SecurityGroupIngress.FromPort
85     Property: SecurityGroupIngress.ToPort
86     """
87     return network_port(x)
88 
89 
90 def validate_tags_or_list(x):
91     """
92     Property: TagSpecifications.Tags
93     Property: CustomerGateway.Tags
94     Property: DHCPOptions.Tags
95     Property: Instance.Tags
96     Property: InternetGateway.Tags
97     Property: LocalGatewayRouteTableVPCAssociation.Tags
98     Property: NatGateway.Tags
99     Property: NetworkAcl.Tags
100     Property: NetworkInterface.Tags
101     Property: RouteTable.Tags
102     Property: SecurityGroup.Tags
103     Property: SpotFleetTagSpecification.Tags
104     Property: Subnet.Tags
105     Property: TransitGateway.Tags
106     Property: TransitGatewayAttachment.Tags
107     Property: TransitGatewayRouteTable.Tags
108     Property: VPC.Tags
109     Property: VPCPeeringConnection.Tags
110     Property: VPNConnection.Tags
111     Property: VPNGateway.Tags
112     Property: Volume.Tags
113     Property: VPNGateway.Tags
114     Property: VPNGateway.Tags
115     """
116     return tags_or_list(x)
117 
118 
119 def instance_tenancy(value):
120     """
121     Property: VPC.InstanceTenancy
122     """
123     valid = ["default", "dedicated"]
124     if value not in valid:
125         raise ValueError("InstanceTenancy needs to be one of %r" % valid)
126     return value
127 
128 
129 def check_ports(props):
130     # IpProtocol is a required field but not all values allowed require
131     # ToPort and FromPort. The ones that don't need these ports are:
132     ports_optional = [
133         "-1",  # all protocols
134         "58",  # ICMPv6
135     ]
136     proto = props["IpProtocol"]
137 
138     if proto not in ports_optional:
139         if not ("ToPort" in props and "FromPort" in props):
140             raise ValueError("ToPort/FromPort must be specified for proto %s" % proto)
141 
142 
143 def validate_elasticinferenceaccelerator_type(elasticinferenceaccelerator_type):
144     """
145     Validate ElasticInferenceAccelerator for Instance
146     Property: ElasticInferenceAccelerator.Type
147     Property: LaunchTemplateElasticInferenceAccelerator.Type
148     """
149 
150     VALID_ELASTICINFERENCEACCELERATOR_TYPES = (
151         "eia1.medium",
152         "eia1.large",
153         "eia1.xlarge",
154     )
155     if elasticinferenceaccelerator_type not in VALID_ELASTICINFERENCEACCELERATOR_TYPES:
156         raise ValueError(
157             "Elastic Inference Accelerator Type must be one of: %s"
158             % ", ".join(VALID_ELASTICINFERENCEACCELERATOR_TYPES)
159         )
160     return elasticinferenceaccelerator_type
161 
162 
163 def validate_clientvpnendpoint_selfserviceportal(value):
164     """
165     Validate SelfServicePortal for ClientVpnEndpoint.
166     Property: ClientVpnEndpoint.SelfServicePortal
167     """
168 
169     VALID_CLIENTVPNENDPOINT_SELFSERVICEPORTAL = ("disabled", "enabled")
170 
171     if value not in VALID_CLIENTVPNENDPOINT_SELFSERVICEPORTAL:
172         raise ValueError(
173             "ClientVpnEndpoint.SelfServicePortal must be one of: {}".format(
174                 ", ".join(VALID_CLIENTVPNENDPOINT_SELFSERVICEPORTAL)
175             )
176         )
177     return value
178 
179 
180 def validate_clientvpnendpoint_vpnport(vpnport):
181     """
182     Validate VpnPort for ClientVpnEndpoint
183     Property: ClientVpnEndpoint.VpnPort
184     """
185 
186     VALID_CLIENTVPNENDPOINT_VPNPORT = (443, 1194)
187     if vpnport not in VALID_CLIENTVPNENDPOINT_VPNPORT:
188         raise ValueError(
189             "ClientVpnEndpoint VpnPort must be one of: %s"
190             % ", ".join(VALID_CLIENTVPNENDPOINT_VPNPORT)
191         )
192     return vpnport
193 
194 
195 def vpn_pre_shared_key(key):
196     """
197     Property: VpnTunnelOptionsSpecification.PreSharedKey
198     """
199     pre_shared_key_match_re = compile(r"^(?!0)([A-Za-z0-9]|\_|\.){8,64}$")
200     if not pre_shared_key_match_re.match(key):
201         raise ValueError(
202             "%s is not a valid key."
203             " Allowed characters are alphanumeric characters and ._. Must"
204             " be between 8 and 64 characters in length and cannot"
205             " start with zero (0)." % key
206         )
207     return key
208 
209 
210 def vpn_tunnel_inside_cidr(cidr):
211     """
212     Property: VpnTunnelOptionsSpecification.TunnelInsideCidr
213     """
214     reserved_cidrs = [
215         "169.254.0.0/30",
216         "169.254.1.0/30",
217         "169.254.2.0/30",
218         "169.254.3.0/30",
219         "169.254.4.0/30",
220         "169.254.5.0/30",
221         "169.254.169.252/30",
222     ]
223     cidr_match_re = compile(
224         r"^169\.254\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)"
225         r"\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\/30$"
226     )
227     if cidr in reserved_cidrs:
228         raise ValueError(
229             'The following CIDR blocks are reserved and cannot be used: "%s"'
230             % (", ".join(reserved_cidrs))
231         )
232     elif not cidr_match_re.match(cidr):
233         raise ValueError(
234             "%s is not a valid CIDR."
235             " A size /30 CIDR block from the 169.254.0.0/16 must be specified." % cidr
236         )
237     return cidr
238 
239 
240 def vpc_endpoint_type(endpoint_type):
241     """
242     Property: VPCEndpoint.VpcEndpointType
243     """
244     valid_types = ["Interface", "Gateway", "GatewayLoadBalancer"]
245     if endpoint_type not in valid_types:
246         raise ValueError(
247             'VpcEndpointType must be one of: "%s"' % (", ".join(valid_types))
248         )
249     return endpoint_type
250 
251 
252 def validate_network_acl_entry(self):
253     """
254     Class: NetworkAclEntry
255     """
256     conds = [
257         "CidrBlock",
258         "Ipv6CidrBlock",
259     ]
260     exactly_one(self.__class__.__name__, self.properties, conds)
261 
262 
263 def validate_route(self):
264     """
265     Class: Route
266     """
267     cidr_conds = [
268         "DestinationCidrBlock",
269         "DestinationIpv6CidrBlock",
270     ]
271     gateway_conds = [
272         "CarrierGatewayId",
273         "EgressOnlyInternetGatewayId",
274         "GatewayId",
275         "InstanceId",
276         "LocalGatewayId",
277         "NatGatewayId",
278         "NetworkInterfaceId",
279         "TransitGatewayId",
280         "VpcEndpointId",
281         "VpcPeeringConnectionId",
282     ]
283     exactly_one(self.__class__.__name__, self.properties, cidr_conds)
284     exactly_one(self.__class__.__name__, self.properties, gateway_conds)
285 
286 
287 def validate_security_group_egress(self):
288     """
289     Class: SecurityGroupEgress
290     """
291     conds = [
292         "CidrIp",
293         "CidrIpv6",
294         "DestinationPrefixListId",
295         "DestinationSecurityGroupId",
296     ]
297     exactly_one(self.__class__.__name__, self.properties, conds)
298     check_ports(self.properties)
299 
300 
301 def validate_security_group_ingress(self):
302     """
303     Class: SecurityGroupIngress
304     """
305     conds = [
306         "CidrIp",
307         "CidrIpv6",
308         "SourcePrefixListId",
309         "SourceSecurityGroupName",
310         "SourceSecurityGroupId",
311     ]
312     exactly_one(self.__class__.__name__, self.properties, conds)
313     check_ports(self.properties)
314 
315 
316 def validate_spot_fleet_request_config_data(self):
317     """
318     Class: SpotFleetRequestConfigData
319     """
320     conds = ["LaunchSpecifications", "LaunchTemplateConfigs"]
321     exactly_one(self.__class__.__name__, self.properties, conds)
322 
323 
324 def validate_subnet(self):
325     """
326     Class: Subnet
327     """
328     if "Ipv6CidrBlock" in self.properties:
329         if not self.properties.get("AssignIpv6AddressOnCreation"):
330             raise ValueError(
331                 "If Ipv6CidrBlock is present, "
332                 "AssignIpv6AddressOnCreation must be set to True"
333             )
334 
335 
336 def validate_vpn_connection(self):
337     """
338     Class: VPNConnection
339     """
340     conds = [
341         "VpnGatewayId",
342         "TransitGatewayId",
343     ]
344     exactly_one(self.__class__.__name__, self.properties, conds)
345
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/troposphere/validators/ec2.py b/troposphere/validators/ec2.py
--- a/troposphere/validators/ec2.py
+++ b/troposphere/validators/ec2.py
@@ -3,6 +3,7 @@
 #
 # See LICENSE file for full license.
 
+import re
 
 from .. import AWSProperty
 from ..compat import validate_policytype
@@ -196,7 +197,7 @@
     """
     Property: VpnTunnelOptionsSpecification.PreSharedKey
     """
-    pre_shared_key_match_re = compile(r"^(?!0)([A-Za-z0-9]|\_|\.){8,64}$")
+    pre_shared_key_match_re = re.compile(r"^(?!0)([A-Za-z0-9]|\_|\.){8,64}$")
     if not pre_shared_key_match_re.match(key):
         raise ValueError(
             "%s is not a valid key."
@@ -220,7 +221,7 @@
         "169.254.5.0/30",
         "169.254.169.252/30",
     ]
-    cidr_match_re = compile(
+    cidr_match_re = re.compile(
         r"^169\.254\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)"
         r"\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\/30$"
     )

{"golden_diff": "diff --git a/troposphere/validators/ec2.py b/troposphere/validators/ec2.py\n--- a/troposphere/validators/ec2.py\n+++ b/troposphere/validators/ec2.py\n@@ -3,6 +3,7 @@\n #\n # See LICENSE file for full license.\n \n+import re\n \n from .. import AWSProperty\n from ..compat import validate_policytype\n@@ -196,7 +197,7 @@\n     \"\"\"\n     Property: VpnTunnelOptionsSpecification.PreSharedKey\n     \"\"\"\n-    pre_shared_key_match_re = compile(r\"^(?!0)([A-Za-z0-9]|\\_|\\.){8,64}$\")\n+    pre_shared_key_match_re = re.compile(r\"^(?!0)([A-Za-z0-9]|\\_|\\.){8,64}$\")\n     if not pre_shared_key_match_re.match(key):\n         raise ValueError(\n             \"%s is not a valid key.\"\n@@ -220,7 +221,7 @@\n         \"169.254.5.0/30\",\n         \"169.254.169.252/30\",\n     ]\n-    cidr_match_re = compile(\n+    cidr_match_re = re.compile(\n         r\"^169\\.254\\.(?:25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\"\n         r\"\\.(?:25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\/30$\"\n     )\n", "issue": "When using ec2.VpnTunnelOptionsSpecification, report error:compile() missing required argument 'filename' (pos 2)\nI am using troposphere version 4.3.2\r\nThe issue occurs in this code section:\r\n```\r\n  vpn_tunnel_option_props = self.filter_parameters(\r\n      vpn_option.items(), ec2.VpnTunnelOptionsSpecification\r\n  )\r\n```\r\nafter I ran it an error occured: compile() missing required argument 'filename' (pos 2)\r\nBut it works in troposphere version 3.2.2\r\nI find that troposphere uses re.complile() in the old versions(v3), but the Python system function compile() is used in the new versions(v4), which caused this problem. Is this a bug?\r\n```\r\ndef vpn_tunnel_inside_cidr(cidr):\r\n    \"\"\"\r\n    Property: VpnTunnelOptionsSpecification.TunnelInsideCidr\r\n    \"\"\"\r\n    cidr_match_re = compile(\r\n        r\"^169\\.254\\.(?:25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\"\r\n        r\"\\.(?:25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\/30$\"\r\n    )\r\n```\n", "before_files": [{"content": "# Copyright (c) 2012-2022, Mark Peek <mark@peek.org>\n# All rights reserved.\n#\n# See LICENSE file for full license.\n\n\nfrom .. import AWSProperty\nfrom ..compat import validate_policytype\nfrom ..type_defs.compat import Final\nfrom . import exactly_one, integer_range, network_port, tags_or_list\n\nPERMISSION_INSTANCE_ATTACH: Final = \"INSTANCE-ATTACH\"\nPERMISSION_EIP_ASSOCIATE: Final = \"EIP-ASSOCIATE\"\n\nNO_DEVICE: Final = {}\n\n\ndef Ipv6Addresses(ipv6_address):\n    \"\"\"\n    Export:\n    \"\"\"\n    from ..ec2 import InstanceIpv6Address\n\n    return InstanceIpv6Address(Ipv6Address=ipv6_address)\n\n\nclass Tag(AWSProperty):\n    \"\"\"\n    Export:\n    \"\"\"\n\n    props = {\n        \"Key\": (str, True),\n        \"Value\": (str, True),\n    }\n\n    def __init__(self, key=None, value=None, **kwargs):\n        # provided for backward compatibility\n        if key is not None:\n            kwargs[\"Key\"] = key\n        if value is not None:\n            kwargs[\"Value\"] = value\n        super().__init__(**kwargs)\n\n\ndef validate_int_to_str(x):\n    \"\"\"\n    Backward compatibility - field was int and now str.\n    Property: NetworkInterfaceProperty.DeviceIndex\n    Property: NetworkInterfaceAttachment.DeviceIndex\n    \"\"\"\n\n    if isinstance(x, int):\n        return str(x)\n    if isinstance(x, str):\n        return str(int(x))\n\n    raise TypeError(f\"Value {x} of type {type(x)} must be either int or str\")\n\n\ndef policytypes(policy):\n    \"\"\"\n    Property: VPCEndpoint.PolicyDocument\n    \"\"\"\n    return validate_policytype(policy)\n\n\ndef validate_networkaclentry_rulenumber(x):\n    \"\"\"\n    Property: NetworkAclEntry.RuleNumber\n    \"\"\"\n    return integer_range(1, 32766)(x)\n\n\ndef validate_network_port(x):\n    \"\"\"\n    Property: NetworkAclEntry.Protocol\n    Property: PortRange.From\n    Property: PortRange.To\n    Property: SecurityGroupRule.FromPort\n    Property: SecurityGroupRule.ToPort\n    Property: SecurityGroupEgress.FromPort\n    Property: SecurityGroupEgress.ToPort\n    Property: SecurityGroupIngress.FromPort\n    Property: SecurityGroupIngress.ToPort\n    \"\"\"\n    return network_port(x)\n\n\ndef validate_tags_or_list(x):\n    \"\"\"\n    Property: TagSpecifications.Tags\n    Property: CustomerGateway.Tags\n    Property: DHCPOptions.Tags\n    Property: Instance.Tags\n    Property: InternetGateway.Tags\n    Property: LocalGatewayRouteTableVPCAssociation.Tags\n    Property: NatGateway.Tags\n    Property: NetworkAcl.Tags\n    Property: NetworkInterface.Tags\n    Property: RouteTable.Tags\n    Property: SecurityGroup.Tags\n    Property: SpotFleetTagSpecification.Tags\n    Property: Subnet.Tags\n    Property: TransitGateway.Tags\n    Property: TransitGatewayAttachment.Tags\n    Property: TransitGatewayRouteTable.Tags\n    Property: VPC.Tags\n    Property: VPCPeeringConnection.Tags\n    Property: VPNConnection.Tags\n    Property: VPNGateway.Tags\n    Property: Volume.Tags\n    Property: VPNGateway.Tags\n    Property: VPNGateway.Tags\n    \"\"\"\n    return tags_or_list(x)\n\n\ndef instance_tenancy(value):\n    \"\"\"\n    Property: VPC.InstanceTenancy\n    \"\"\"\n    valid = [\"default\", \"dedicated\"]\n    if value not in valid:\n        raise ValueError(\"InstanceTenancy needs to be one of %r\" % valid)\n    return value\n\n\ndef check_ports(props):\n    # IpProtocol is a required field but not all values allowed require\n    # ToPort and FromPort. The ones that don't need these ports are:\n    ports_optional = [\n        \"-1\",  # all protocols\n        \"58\",  # ICMPv6\n    ]\n    proto = props[\"IpProtocol\"]\n\n    if proto not in ports_optional:\n        if not (\"ToPort\" in props and \"FromPort\" in props):\n            raise ValueError(\"ToPort/FromPort must be specified for proto %s\" % proto)\n\n\ndef validate_elasticinferenceaccelerator_type(elasticinferenceaccelerator_type):\n    \"\"\"\n    Validate ElasticInferenceAccelerator for Instance\n    Property: ElasticInferenceAccelerator.Type\n    Property: LaunchTemplateElasticInferenceAccelerator.Type\n    \"\"\"\n\n    VALID_ELASTICINFERENCEACCELERATOR_TYPES = (\n        \"eia1.medium\",\n        \"eia1.large\",\n        \"eia1.xlarge\",\n    )\n    if elasticinferenceaccelerator_type not in VALID_ELASTICINFERENCEACCELERATOR_TYPES:\n        raise ValueError(\n            \"Elastic Inference Accelerator Type must be one of: %s\"\n            % \", \".join(VALID_ELASTICINFERENCEACCELERATOR_TYPES)\n        )\n    return elasticinferenceaccelerator_type\n\n\ndef validate_clientvpnendpoint_selfserviceportal(value):\n    \"\"\"\n    Validate SelfServicePortal for ClientVpnEndpoint.\n    Property: ClientVpnEndpoint.SelfServicePortal\n    \"\"\"\n\n    VALID_CLIENTVPNENDPOINT_SELFSERVICEPORTAL = (\"disabled\", \"enabled\")\n\n    if value not in VALID_CLIENTVPNENDPOINT_SELFSERVICEPORTAL:\n        raise ValueError(\n            \"ClientVpnEndpoint.SelfServicePortal must be one of: {}\".format(\n                \", \".join(VALID_CLIENTVPNENDPOINT_SELFSERVICEPORTAL)\n            )\n        )\n    return value\n\n\ndef validate_clientvpnendpoint_vpnport(vpnport):\n    \"\"\"\n    Validate VpnPort for ClientVpnEndpoint\n    Property: ClientVpnEndpoint.VpnPort\n    \"\"\"\n\n    VALID_CLIENTVPNENDPOINT_VPNPORT = (443, 1194)\n    if vpnport not in VALID_CLIENTVPNENDPOINT_VPNPORT:\n        raise ValueError(\n            \"ClientVpnEndpoint VpnPort must be one of: %s\"\n            % \", \".join(VALID_CLIENTVPNENDPOINT_VPNPORT)\n        )\n    return vpnport\n\n\ndef vpn_pre_shared_key(key):\n    \"\"\"\n    Property: VpnTunnelOptionsSpecification.PreSharedKey\n    \"\"\"\n    pre_shared_key_match_re = compile(r\"^(?!0)([A-Za-z0-9]|\\_|\\.){8,64}$\")\n    if not pre_shared_key_match_re.match(key):\n        raise ValueError(\n            \"%s is not a valid key.\"\n            \" Allowed characters are alphanumeric characters and ._. Must\"\n            \" be between 8 and 64 characters in length and cannot\"\n            \" start with zero (0).\" % key\n        )\n    return key\n\n\ndef vpn_tunnel_inside_cidr(cidr):\n    \"\"\"\n    Property: VpnTunnelOptionsSpecification.TunnelInsideCidr\n    \"\"\"\n    reserved_cidrs = [\n        \"169.254.0.0/30\",\n        \"169.254.1.0/30\",\n        \"169.254.2.0/30\",\n        \"169.254.3.0/30\",\n        \"169.254.4.0/30\",\n        \"169.254.5.0/30\",\n        \"169.254.169.252/30\",\n    ]\n    cidr_match_re = compile(\n        r\"^169\\.254\\.(?:25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\"\n        r\"\\.(?:25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\/30$\"\n    )\n    if cidr in reserved_cidrs:\n        raise ValueError(\n            'The following CIDR blocks are reserved and cannot be used: \"%s\"'\n            % (\", \".join(reserved_cidrs))\n        )\n    elif not cidr_match_re.match(cidr):\n        raise ValueError(\n            \"%s is not a valid CIDR.\"\n            \" A size /30 CIDR block from the 169.254.0.0/16 must be specified.\" % cidr\n        )\n    return cidr\n\n\ndef vpc_endpoint_type(endpoint_type):\n    \"\"\"\n    Property: VPCEndpoint.VpcEndpointType\n    \"\"\"\n    valid_types = [\"Interface\", \"Gateway\", \"GatewayLoadBalancer\"]\n    if endpoint_type not in valid_types:\n        raise ValueError(\n            'VpcEndpointType must be one of: \"%s\"' % (\", \".join(valid_types))\n        )\n    return endpoint_type\n\n\ndef validate_network_acl_entry(self):\n    \"\"\"\n    Class: NetworkAclEntry\n    \"\"\"\n    conds = [\n        \"CidrBlock\",\n        \"Ipv6CidrBlock\",\n    ]\n    exactly_one(self.__class__.__name__, self.properties, conds)\n\n\ndef validate_route(self):\n    \"\"\"\n    Class: Route\n    \"\"\"\n    cidr_conds = [\n        \"DestinationCidrBlock\",\n        \"DestinationIpv6CidrBlock\",\n    ]\n    gateway_conds = [\n        \"CarrierGatewayId\",\n        \"EgressOnlyInternetGatewayId\",\n        \"GatewayId\",\n        \"InstanceId\",\n        \"LocalGatewayId\",\n        \"NatGatewayId\",\n        \"NetworkInterfaceId\",\n        \"TransitGatewayId\",\n        \"VpcEndpointId\",\n        \"VpcPeeringConnectionId\",\n    ]\n    exactly_one(self.__class__.__name__, self.properties, cidr_conds)\n    exactly_one(self.__class__.__name__, self.properties, gateway_conds)\n\n\ndef validate_security_group_egress(self):\n    \"\"\"\n    Class: SecurityGroupEgress\n    \"\"\"\n    conds = [\n        \"CidrIp\",\n        \"CidrIpv6\",\n        \"DestinationPrefixListId\",\n        \"DestinationSecurityGroupId\",\n    ]\n    exactly_one(self.__class__.__name__, self.properties, conds)\n    check_ports(self.properties)\n\n\ndef validate_security_group_ingress(self):\n    \"\"\"\n    Class: SecurityGroupIngress\n    \"\"\"\n    conds = [\n        \"CidrIp\",\n        \"CidrIpv6\",\n        \"SourcePrefixListId\",\n        \"SourceSecurityGroupName\",\n        \"SourceSecurityGroupId\",\n    ]\n    exactly_one(self.__class__.__name__, self.properties, conds)\n    check_ports(self.properties)\n\n\ndef validate_spot_fleet_request_config_data(self):\n    \"\"\"\n    Class: SpotFleetRequestConfigData\n    \"\"\"\n    conds = [\"LaunchSpecifications\", \"LaunchTemplateConfigs\"]\n    exactly_one(self.__class__.__name__, self.properties, conds)\n\n\ndef validate_subnet(self):\n    \"\"\"\n    Class: Subnet\n    \"\"\"\n    if \"Ipv6CidrBlock\" in self.properties:\n        if not self.properties.get(\"AssignIpv6AddressOnCreation\"):\n            raise ValueError(\n                \"If Ipv6CidrBlock is present, \"\n                \"AssignIpv6AddressOnCreation must be set to True\"\n            )\n\n\ndef validate_vpn_connection(self):\n    \"\"\"\n    Class: VPNConnection\n    \"\"\"\n    conds = [\n        \"VpnGatewayId\",\n        \"TransitGatewayId\",\n    ]\n    exactly_one(self.__class__.__name__, self.properties, conds)\n", "path": "troposphere/validators/ec2.py"}], "after_files": [{"content": "# Copyright (c) 2012-2022, Mark Peek <mark@peek.org>\n# All rights reserved.\n#\n# See LICENSE file for full license.\n\nimport re\n\nfrom .. import AWSProperty\nfrom ..compat import validate_policytype\nfrom ..type_defs.compat import Final\nfrom . import exactly_one, integer_range, network_port, tags_or_list\n\nPERMISSION_INSTANCE_ATTACH: Final = \"INSTANCE-ATTACH\"\nPERMISSION_EIP_ASSOCIATE: Final = \"EIP-ASSOCIATE\"\n\nNO_DEVICE: Final = {}\n\n\ndef Ipv6Addresses(ipv6_address):\n    \"\"\"\n    Export:\n    \"\"\"\n    from ..ec2 import InstanceIpv6Address\n\n    return InstanceIpv6Address(Ipv6Address=ipv6_address)\n\n\nclass Tag(AWSProperty):\n    \"\"\"\n    Export:\n    \"\"\"\n\n    props = {\n        \"Key\": (str, True),\n        \"Value\": (str, True),\n    }\n\n    def __init__(self, key=None, value=None, **kwargs):\n        # provided for backward compatibility\n        if key is not None:\n            kwargs[\"Key\"] = key\n        if value is not None:\n            kwargs[\"Value\"] = value\n        super().__init__(**kwargs)\n\n\ndef validate_int_to_str(x):\n    \"\"\"\n    Backward compatibility - field was int and now str.\n    Property: NetworkInterfaceProperty.DeviceIndex\n    Property: NetworkInterfaceAttachment.DeviceIndex\n    \"\"\"\n\n    if isinstance(x, int):\n        return str(x)\n    if isinstance(x, str):\n        return str(int(x))\n\n    raise TypeError(f\"Value {x} of type {type(x)} must be either int or str\")\n\n\ndef policytypes(policy):\n    \"\"\"\n    Property: VPCEndpoint.PolicyDocument\n    \"\"\"\n    return validate_policytype(policy)\n\n\ndef validate_networkaclentry_rulenumber(x):\n    \"\"\"\n    Property: NetworkAclEntry.RuleNumber\n    \"\"\"\n    return integer_range(1, 32766)(x)\n\n\ndef validate_network_port(x):\n    \"\"\"\n    Property: NetworkAclEntry.Protocol\n    Property: PortRange.From\n    Property: PortRange.To\n    Property: SecurityGroupRule.FromPort\n    Property: SecurityGroupRule.ToPort\n    Property: SecurityGroupEgress.FromPort\n    Property: SecurityGroupEgress.ToPort\n    Property: SecurityGroupIngress.FromPort\n    Property: SecurityGroupIngress.ToPort\n    \"\"\"\n    return network_port(x)\n\n\ndef validate_tags_or_list(x):\n    \"\"\"\n    Property: TagSpecifications.Tags\n    Property: CustomerGateway.Tags\n    Property: DHCPOptions.Tags\n    Property: Instance.Tags\n    Property: InternetGateway.Tags\n    Property: LocalGatewayRouteTableVPCAssociation.Tags\n    Property: NatGateway.Tags\n    Property: NetworkAcl.Tags\n    Property: NetworkInterface.Tags\n    Property: RouteTable.Tags\n    Property: SecurityGroup.Tags\n    Property: SpotFleetTagSpecification.Tags\n    Property: Subnet.Tags\n    Property: TransitGateway.Tags\n    Property: TransitGatewayAttachment.Tags\n    Property: TransitGatewayRouteTable.Tags\n    Property: VPC.Tags\n    Property: VPCPeeringConnection.Tags\n    Property: VPNConnection.Tags\n    Property: VPNGateway.Tags\n    Property: Volume.Tags\n    Property: VPNGateway.Tags\n    Property: VPNGateway.Tags\n    \"\"\"\n    return tags_or_list(x)\n\n\ndef instance_tenancy(value):\n    \"\"\"\n    Property: VPC.InstanceTenancy\n    \"\"\"\n    valid = [\"default\", \"dedicated\"]\n    if value not in valid:\n        raise ValueError(\"InstanceTenancy needs to be one of %r\" % valid)\n    return value\n\n\ndef check_ports(props):\n    # IpProtocol is a required field but not all values allowed require\n    # ToPort and FromPort. The ones that don't need these ports are:\n    ports_optional = [\n        \"-1\",  # all protocols\n        \"58\",  # ICMPv6\n    ]\n    proto = props[\"IpProtocol\"]\n\n    if proto not in ports_optional:\n        if not (\"ToPort\" in props and \"FromPort\" in props):\n            raise ValueError(\"ToPort/FromPort must be specified for proto %s\" % proto)\n\n\ndef validate_elasticinferenceaccelerator_type(elasticinferenceaccelerator_type):\n    \"\"\"\n    Validate ElasticInferenceAccelerator for Instance\n    Property: ElasticInferenceAccelerator.Type\n    Property: LaunchTemplateElasticInferenceAccelerator.Type\n    \"\"\"\n\n    VALID_ELASTICINFERENCEACCELERATOR_TYPES = (\n        \"eia1.medium\",\n        \"eia1.large\",\n        \"eia1.xlarge\",\n    )\n    if elasticinferenceaccelerator_type not in VALID_ELASTICINFERENCEACCELERATOR_TYPES:\n        raise ValueError(\n            \"Elastic Inference Accelerator Type must be one of: %s\"\n            % \", \".join(VALID_ELASTICINFERENCEACCELERATOR_TYPES)\n        )\n    return elasticinferenceaccelerator_type\n\n\ndef validate_clientvpnendpoint_selfserviceportal(value):\n    \"\"\"\n    Validate SelfServicePortal for ClientVpnEndpoint.\n    Property: ClientVpnEndpoint.SelfServicePortal\n    \"\"\"\n\n    VALID_CLIENTVPNENDPOINT_SELFSERVICEPORTAL = (\"disabled\", \"enabled\")\n\n    if value not in VALID_CLIENTVPNENDPOINT_SELFSERVICEPORTAL:\n        raise ValueError(\n            \"ClientVpnEndpoint.SelfServicePortal must be one of: {}\".format(\n                \", \".join(VALID_CLIENTVPNENDPOINT_SELFSERVICEPORTAL)\n            )\n        )\n    return value\n\n\ndef validate_clientvpnendpoint_vpnport(vpnport):\n    \"\"\"\n    Validate VpnPort for ClientVpnEndpoint\n    Property: ClientVpnEndpoint.VpnPort\n    \"\"\"\n\n    VALID_CLIENTVPNENDPOINT_VPNPORT = (443, 1194)\n    if vpnport not in VALID_CLIENTVPNENDPOINT_VPNPORT:\n        raise ValueError(\n            \"ClientVpnEndpoint VpnPort must be one of: %s\"\n            % \", \".join(VALID_CLIENTVPNENDPOINT_VPNPORT)\n        )\n    return vpnport\n\n\ndef vpn_pre_shared_key(key):\n    \"\"\"\n    Property: VpnTunnelOptionsSpecification.PreSharedKey\n    \"\"\"\n    pre_shared_key_match_re = re.compile(r\"^(?!0)([A-Za-z0-9]|\\_|\\.){8,64}$\")\n    if not pre_shared_key_match_re.match(key):\n        raise ValueError(\n            \"%s is not a valid key.\"\n            \" Allowed characters are alphanumeric characters and ._. Must\"\n            \" be between 8 and 64 characters in length and cannot\"\n            \" start with zero (0).\" % key\n        )\n    return key\n\n\ndef vpn_tunnel_inside_cidr(cidr):\n    \"\"\"\n    Property: VpnTunnelOptionsSpecification.TunnelInsideCidr\n    \"\"\"\n    reserved_cidrs = [\n        \"169.254.0.0/30\",\n        \"169.254.1.0/30\",\n        \"169.254.2.0/30\",\n        \"169.254.3.0/30\",\n        \"169.254.4.0/30\",\n        \"169.254.5.0/30\",\n        \"169.254.169.252/30\",\n    ]\n    cidr_match_re = re.compile(\n        r\"^169\\.254\\.(?:25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\"\n        r\"\\.(?:25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\/30$\"\n    )\n    if cidr in reserved_cidrs:\n        raise ValueError(\n            'The following CIDR blocks are reserved and cannot be used: \"%s\"'\n            % (\", \".join(reserved_cidrs))\n        )\n    elif not cidr_match_re.match(cidr):\n        raise ValueError(\n            \"%s is not a valid CIDR.\"\n            \" A size /30 CIDR block from the 169.254.0.0/16 must be specified.\" % cidr\n        )\n    return cidr\n\n\ndef vpc_endpoint_type(endpoint_type):\n    \"\"\"\n    Property: VPCEndpoint.VpcEndpointType\n    \"\"\"\n    valid_types = [\"Interface\", \"Gateway\", \"GatewayLoadBalancer\"]\n    if endpoint_type not in valid_types:\n        raise ValueError(\n            'VpcEndpointType must be one of: \"%s\"' % (\", \".join(valid_types))\n        )\n    return endpoint_type\n\n\ndef validate_network_acl_entry(self):\n    \"\"\"\n    Class: NetworkAclEntry\n    \"\"\"\n    conds = [\n        \"CidrBlock\",\n        \"Ipv6CidrBlock\",\n    ]\n    exactly_one(self.__class__.__name__, self.properties, conds)\n\n\ndef validate_route(self):\n    \"\"\"\n    Class: Route\n    \"\"\"\n    cidr_conds = [\n        \"DestinationCidrBlock\",\n        \"DestinationIpv6CidrBlock\",\n    ]\n    gateway_conds = [\n        \"CarrierGatewayId\",\n        \"EgressOnlyInternetGatewayId\",\n        \"GatewayId\",\n        \"InstanceId\",\n        \"LocalGatewayId\",\n        \"NatGatewayId\",\n        \"NetworkInterfaceId\",\n        \"TransitGatewayId\",\n        \"VpcEndpointId\",\n        \"VpcPeeringConnectionId\",\n    ]\n    exactly_one(self.__class__.__name__, self.properties, cidr_conds)\n    exactly_one(self.__class__.__name__, self.properties, gateway_conds)\n\n\ndef validate_security_group_egress(self):\n    \"\"\"\n    Class: SecurityGroupEgress\n    \"\"\"\n    conds = [\n        \"CidrIp\",\n        \"CidrIpv6\",\n        \"DestinationPrefixListId\",\n        \"DestinationSecurityGroupId\",\n    ]\n    exactly_one(self.__class__.__name__, self.properties, conds)\n    check_ports(self.properties)\n\n\ndef validate_security_group_ingress(self):\n    \"\"\"\n    Class: SecurityGroupIngress\n    \"\"\"\n    conds = [\n        \"CidrIp\",\n        \"CidrIpv6\",\n        \"SourcePrefixListId\",\n        \"SourceSecurityGroupName\",\n        \"SourceSecurityGroupId\",\n    ]\n    exactly_one(self.__class__.__name__, self.properties, conds)\n    check_ports(self.properties)\n\n\ndef validate_spot_fleet_request_config_data(self):\n    \"\"\"\n    Class: SpotFleetRequestConfigData\n    \"\"\"\n    conds = [\"LaunchSpecifications\", \"LaunchTemplateConfigs\"]\n    exactly_one(self.__class__.__name__, self.properties, conds)\n\n\ndef validate_subnet(self):\n    \"\"\"\n    Class: Subnet\n    \"\"\"\n    if \"Ipv6CidrBlock\" in self.properties:\n        if not self.properties.get(\"AssignIpv6AddressOnCreation\"):\n            raise ValueError(\n                \"If Ipv6CidrBlock is present, \"\n                \"AssignIpv6AddressOnCreation must be set to True\"\n            )\n\n\ndef validate_vpn_connection(self):\n    \"\"\"\n    Class: VPNConnection\n    \"\"\"\n    conds = [\n        \"VpnGatewayId\",\n        \"TransitGatewayId\",\n    ]\n    exactly_one(self.__class__.__name__, self.properties, conds)\n", "path": "troposphere/validators/ec2.py"}]}