Skip to content

Commit 655b987

Browse files
committed
Fixed the CIDR range match function to automatically fix any errors in CIDRs
1 parent 0ce715e commit 655b987

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

shuffle-tools/1.2.0/src/app.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2182,8 +2182,17 @@ def convert_json_to_tags(self, json_object, split_value=", ", include_key=True,
21822182

21832183
return fullstring
21842184

2185-
def cidr_ip_match(self, ip, networks):
2185+
def autofix_network(self, ip_with_cidr):
2186+
try:
2187+
# Parse the input as an IPv4 network object
2188+
network = ipaddress.IPv4Network(ip_with_cidr, strict=False)
2189+
# Return the corrected network address
2190+
return str(network)
2191+
except ValueError as e:
2192+
print(f"Error: {e}")
2193+
return None
21862194

2195+
def cidr_ip_match(self, ip, networks):
21872196
if isinstance(networks, str):
21882197
try:
21892198
networks = json.loads(networks)
@@ -2193,9 +2202,18 @@ def cidr_ip_match(self, ip, networks):
21932202
"reason": "Networks is not a valid list: {}".format(networks),
21942203
}
21952204

2205+
new_networks = []
2206+
for network in networks:
2207+
new_network = self.autofix_network(network)
2208+
if new_network:
2209+
new_networks.append(new_network)
2210+
2211+
networks = new_networks
2212+
21962213
try:
21972214
ip_networks = list(map(ipaddress.ip_network, networks))
2198-
ip_address = ipaddress.ip_address(ip, False)
2215+
#ip_address = ipaddress.ip_address(ip, False)
2216+
ip_address = ipaddress.ip_address(ip)
21992217
except ValueError as e:
22002218
return "IP or some networks are not in valid format.\nError: {}".format(e)
22012219

0 commit comments

Comments
 (0)