diff --git a/lib/ansible/modules/network/f5/bigip_remote_syslog.py b/lib/ansible/modules/network/f5/bigip_remote_syslog.py index f789ffafed..0396b15560 100644 --- a/lib/ansible/modules/network/f5/bigip_remote_syslog.py +++ b/lib/ansible/modules/network/f5/bigip_remote_syslog.py @@ -43,6 +43,7 @@ notes: extends_documentation_fragment: f5 requirements: - f5-sdk >= 2.2.0 + - netaddr author: - Tim Rupp (@caphrim007) ''' @@ -81,6 +82,8 @@ local_ip: sample: 10.10.10.10 ''' +import re + try: import netaddr HAS_NETADDR = True @@ -163,13 +166,40 @@ class Parameters(AnsibleF5Parameters): @property def remote_host(self): try: - ip = netaddr.IPAddress(self._values['remote_host']) - return str(ip) + # Check for valid IPv4 or IPv6 entries + netaddr.IPAddress(self._values['remote_host']) + return self._values['remote_host'] except netaddr.core.AddrFormatError: + # else fallback to checking reasonably well formatted hostnames + if self.is_valid_hostname(self._values['remote_host']): + return str(self._values['remote_host']) raise F5ModuleError( - "The provided 'remote_host' is not a valid IP address" + "The provided 'remote_host' is not a valid IP or hostname" ) + def is_valid_hostname(self, host): + """Reasonable attempt at validating a hostname + + Compiled from various paragraphs outlined here + https://tools.ietf.org/html/rfc3696#section-2 + https://tools.ietf.org/html/rfc1123 + + Notably, + * Host software MUST handle host names of up to 63 characters and + SHOULD handle host names of up to 255 characters. + * The "LDH rule", after the characters that it permits. (letters, digits, hyphen) + * If the hyphen is used, it is not permitted to appear at + either the beginning or end of a label + + :param host: + :return: + """ + if len(host) > 255: + return False + host = host.rstrip(".") + allowed = re.compile(r'(?!-)[A-Z0-9-]{1,63}(?= 2.7") from ansible.compat.tests import unittest -from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes +from ansible.compat.tests.mock import Mock +from ansible.compat.tests.mock import patch from ansible.module_utils.f5_utils import AnsibleF5Client -from units.modules.utils import set_module_args try: from library.bigip_remote_syslog import Parameters @@ -28,6 +26,7 @@ try: from library.bigip_remote_syslog import HAS_F5SDK from library.bigip_remote_syslog import HAS_NETADDR from ansible.module_utils.f5_utils import iControlUnexpectedHTTPError + from test.unit.modules.utils import set_module_args except ImportError: try: from ansible.modules.network.f5.bigip_remote_syslog import Parameters @@ -36,6 +35,7 @@ except ImportError: from ansible.modules.network.f5.bigip_remote_syslog import HAS_F5SDK from ansible.modules.network.f5.bigip_remote_syslog import HAS_NETADDR from ansible.module_utils.f5_utils import iControlUnexpectedHTTPError + from units.modules.utils import set_module_args except ImportError: raise SkipTest("F5 Ansible modules require the f5-sdk Python library")