Skip to content

Commit e91fc92

Browse files
authored
Better IPv4 validation (#177)
* Using ipaddress lib to validate entry * Adding unittest
1 parent e4f8694 commit e91fc92

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

ecs_composex/ecs/ecs_service.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
Instance as SdInstance,
6161
)
6262

63+
from ipaddress import IPv4Interface
64+
6365
from ecs_composex.common import add_parameters
6466
from ecs_composex.common import keyisset, LOG, NONALPHANUM
6567
from ecs_composex.common.cfn_conditions import USE_STACK_NAME_CON_T
@@ -88,11 +90,6 @@
8890
from ecs_composex.vpc import vpc_params
8991
from ecs_composex.vpc.vpc_params import VPC_ID, PUBLIC_SUBNETS
9092

91-
CIDR_REG = r"""((((((([0-9]{1}\.))|([0-9]{2}\.)|
92-
(1[0-9]{2}\.)|(2[0-5]{2}\.)))){3})(((((([0-9]{1}))|
93-
([0-9]{2})|(1[0-9]{2})|(2[0-5]{2}))))){1,3})\/(([0-9])|([1-2][0-9])|((3[0-2])))$"""
94-
CIDR_PAT = re.compile(CIDR_REG)
95-
9693

9794
def flatten_ip(ip_str):
9895
"""
@@ -297,12 +294,14 @@ def generate_security_group_props(allowed_source, service_name):
297294
if (
298295
keyisset("CidrIp", props)
299296
and isinstance(props["CidrIp"], str)
300-
and not CIDR_PAT.match(props["CidrIp"])
301297
):
302-
LOG.error(f"Falty IP Address: {allowed_source} - ecs_service {service_name}")
303-
raise ValueError(
304-
"Not a valid IPv4 CIDR notation", props["CidrIp"], "Expected", CIDR_REG,
305-
)
298+
try:
299+
IPv4Interface(props["CidrIp"])
300+
except Exception as error:
301+
LOG.error(f"Falty IP Address: {allowed_source} - ecs_service {service_name}")
302+
raise ValueError(
303+
"Not a valid IPv4 CIDR notation", props["CidrIp"], error
304+
)
306305
return props
307306

308307

pytests/test_static_functions.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# -*- coding: utf-8 -*-
2+
# ECS ComposeX <https://github.com/lambda-my-aws/ecs_composex>
3+
# Copyright (C) 2020 John Mille <john@lambda-my-aws.io>
4+
# #
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
# #
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
# #
15+
# You should have received a copy of the GNU General Public License
16+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
18+
from pytest import raises
19+
20+
from ecs_composex.ecs.ecs_service import generate_security_group_props
21+
22+
23+
def test_cidr_validation():
24+
a = generate_security_group_props({"ipv4": "1.1.1.1/32"}, "abcd")
25+
with raises(ValueError):
26+
a = generate_security_group_props({"ipv4": "1.1.1.256/32"}, "abcd")
27+
a = generate_security_group_props({"ipv4": "1.1.1.1/33"}, "abcd")

0 commit comments

Comments
 (0)