diff --git a/plugins/module_utils/dnac.py b/plugins/module_utils/dnac.py index a12e7eaf47..8064cc5e76 100644 --- a/plugins/module_utils/dnac.py +++ b/plugins/module_utils/dnac.py @@ -27,6 +27,7 @@ # import datetime import inspect import re +import socket class DnacBase(): @@ -485,6 +486,30 @@ def update_site_type_key(self, config): return new_config + def is_valid_ipv4(self, ip_address): + """ + Validates an IPv4 address. + + Parameters: + ip_address - String denoting the IPv4 address passed. + + Returns: + bool - Returns true if the passed IP address value is correct or it returns + false if it is incorrect + """ + + try: + socket.inet_aton(ip_address) + octets = ip_address.split('.') + if len(octets) != 4: + return False + for octet in octets: + if not 0 <= int(octet) <= 255: + return False + return True + except socket.error: + return False + def is_list_complex(x): return isinstance(x[0], dict) or isinstance(x[0], list) diff --git a/plugins/modules/discovery_intent.py b/plugins/modules/discovery_intent.py index 96759bb9c6..bbe0634883 100644 --- a/plugins/modules/discovery_intent.py +++ b/plugins/modules/discovery_intent.py @@ -721,6 +721,43 @@ def validate_input(self, state=None): self.status = "success" return self + def validate_ip4_address_list(self): + """ + Validates each ip adress paased in the IP_address_list passed by the user before preprocessing it + """ + + ip_address_list = self.validated_config[0].get('ip_address_list') + for ip in ip_address_list: + if '/' in ip: + ip = ip.split("/")[0] + if '-' in ip: + if len(ip.split('-')) == 2: + ip1, ip2 = ip.split('-') + if self.is_valid_ipv4(ip1) is False: + msg = "IP address {0} is not valid".format(ip1) + self.log(msg, "CRITICAL") + self.module.fail_json(msg=msg) + if self.is_valid_ipv4(ip2) is False: + msg = "IP address {0} is not valid".format(ip2) + self.log(msg, "CRITICAL") + self.module.fail_json(msg=msg) + ip1_parts = list(map(int, ip1.split('.'))) + ip2_parts = list(map(int, ip2.split('.'))) + for part in range(4): + if ip1_parts[part] > ip2_parts[part]: + msg = "Incorrect range passed: {0}. Please pass correct IP address range".format(ip) + self.log(msg, "CRITICAL") + self.module.fail_json(msg=msg) + else: + msg = "Provided range '{0}' is incorrect. IP address range should have only upper and lower limit values".format(ip) + self.log(msg, "CRITICAL") + self.module.fail_json(msg=msg) + if self.is_valid_ipv4(ip) is False and '-' not in ip: + msg = "IP address {0} is not valid".format(ip) + self.log(msg, "CRITICAL") + self.module.fail_json(msg=msg) + self.log("All the IP addresses passed are correct", "INFO") + def get_creds_ids_list(self): """ Retrieve the list of credentials IDs associated with class instance. @@ -1513,6 +1550,7 @@ def get_diff_merged(self): - self: The instance of the class with updated attributes. """ + self.validate_ip4_address_list() devices_list_info = self.get_devices_list_info() ip_address_list = self.preprocess_device_discovery(devices_list_info) exist_discovery = self.get_exist_discovery() diff --git a/plugins/modules/discovery_workflow_manager.py b/plugins/modules/discovery_workflow_manager.py index 88ce124a39..66667260da 100644 --- a/plugins/modules/discovery_workflow_manager.py +++ b/plugins/modules/discovery_workflow_manager.py @@ -721,6 +721,43 @@ def validate_input(self, state=None): self.status = "success" return self + def validate_ip4_address_list(self): + """ + Validates each ip adress paased in the IP_address_list passed by the user before preprocessing it + """ + + ip_address_list = self.validated_config[0].get('ip_address_list') + for ip in ip_address_list: + if '/' in ip: + ip = ip.split("/")[0] + if '-' in ip: + if len(ip.split('-')) == 2: + ip1, ip2 = ip.split('-') + if self.is_valid_ipv4(ip1) is False: + msg = "IP address {0} is not valid".format(ip1) + self.log(msg, "CRITICAL") + self.module.fail_json(msg=msg) + if self.is_valid_ipv4(ip2) is False: + msg = "IP address {0} is not valid".format(ip2) + self.log(msg, "CRITICAL") + self.module.fail_json(msg=msg) + ip1_parts = list(map(int, ip1.split('.'))) + ip2_parts = list(map(int, ip2.split('.'))) + for part in range(4): + if ip1_parts[part] > ip2_parts[part]: + msg = "Incorrect range passed: {0}. Please pass correct IP address range".format(ip) + self.log(msg, "CRITICAL") + self.module.fail_json(msg=msg) + else: + msg = "Provided range '{0}' is incorrect. IP address range should have only upper and lower limit values".format(ip) + self.log(msg, "CRITICAL") + self.module.fail_json(msg=msg) + if self.is_valid_ipv4(ip) is False and '-' not in ip: + msg = "IP address {0} is not valid".format(ip) + self.log(msg, "CRITICAL") + self.module.fail_json(msg=msg) + self.log("All the IP addresses passed are correct", "INFO") + def get_creds_ids_list(self): """ Retrieve the list of credentials IDs associated with class instance. @@ -1513,6 +1550,7 @@ def get_diff_merged(self): - self: The instance of the class with updated attributes. """ + self.validate_ip4_address_list() devices_list_info = self.get_devices_list_info() ip_address_list = self.preprocess_device_discovery(devices_list_info) exist_discovery = self.get_exist_discovery()