From 512b189808ea0d7b333587689d7e7eb52d16b189 Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Tue, 29 Jan 2019 11:11:38 +0100 Subject: [PATCH] Include aliases in the fqdns grains Add UT for "is_fqdn" Add "is_fqdn" check to the network utils Bugfix: include FQDNs aliases Deprecate UnitTest assertion in favour of built-in assert keyword Add UT for fqdns aliases Leverage cached interfaces, if any. --- salt/grains/core.py | 14 ++++++-------- salt/utils/network.py | 12 ++++++++++++ tests/unit/grains/test_core.py | 28 +++++++++++++++++++++++++--- tests/unit/utils/test_network.py | 24 ++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 11 deletions(-) diff --git a/salt/grains/core.py b/salt/grains/core.py index 7b7e328520..309e4c9c4a 100644 --- a/salt/grains/core.py +++ b/salt/grains/core.py @@ -2275,14 +2275,13 @@ def fqdns(): grains = {} fqdns = set() - addresses = salt.utils.network.ip_addrs(include_loopback=False, - interface_data=_INTERFACES) - addresses.extend(salt.utils.network.ip_addrs6(include_loopback=False, - interface_data=_INTERFACES)) - err_message = 'An exception occurred resolving address \'%s\': %s' + addresses = salt.utils.network.ip_addrs(include_loopback=False, interface_data=_get_interfaces()) + addresses.extend(salt.utils.network.ip_addrs6(include_loopback=False, interface_data=_get_interfaces())) + err_message = 'Exception during resolving address: %s' for ip in addresses: try: - fqdns.add(socket.getfqdn(socket.gethostbyaddr(ip)[0])) + name, aliaslist, addresslist = socket.gethostbyaddr(ip) + fqdns.update([socket.getfqdn(name)] + [als for als in aliaslist if salt.utils.network.is_fqdn(als)]) except socket.herror as err: if err.errno in (0, HOST_NOT_FOUND, NO_DATA): # No FQDN for this IP address, so we don't need to know this all the time. @@ -2292,8 +2291,7 @@ def fqdns(): except (socket.error, socket.gaierror, socket.timeout) as err: log.error(err_message, ip, err) - grains['fqdns'] = sorted(list(fqdns)) - return grains + return {"fqdns": sorted(list(fqdns))} def ip_fqdn(): diff --git a/salt/utils/network.py b/salt/utils/network.py index 906d1cb3bc..2ae2e213b7 100644 --- a/salt/utils/network.py +++ b/salt/utils/network.py @@ -1958,3 +1958,15 @@ def parse_host_port(host_port): raise ValueError('bad hostname: "{}"'.format(host)) return host, port + + +def is_fqdn(hostname): + """ + Verify if hostname conforms to be a FQDN. + + :param hostname: text string with the name of the host + :return: bool, True if hostname is correct FQDN, False otherwise + """ + + compliant = re.compile(r"(?!-)[A-Z\d\-\_]{1,63}(?