puppetlabs/facter

Wrong networking.fqdn in the presence of `search .` in `/etc/resolv.conf`

Geod24 opened this issue · 1 comments

Describe the Bug

$ /opt/puppetlabs/bin/facter networking
{
  dhcp => "xxx.xxx.xxx.xxx",
  domain => ".",
  fqdn => "xxx-xxx-prd-vm03..",
  hostname => "xxx-xxx-prd-vm03",
  [...]

This happens because:

$ ls -l /etc/resolv.conf
lrwxrwxrwx 1 root root 37 Mar 23 10:47 /etc/resolv.conf -> /run/systemd/resolve/stub-resolv.conf
$ cat /run/systemd/resolve/stub-resolv.conf
# This is /run/systemd/resolve/stub-resolv.conf managed by man:systemd-resolved(8).
# Do not edit.
#
# This file might be symlinked as /etc/resolv.conf. If you're looking at
# /etc/resolv.conf and seeing this text, you have followed the symlink.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "resolvectl status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs should typically not access this file directly, but only
# through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a
# different way, replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver 127.0.0.53
options edns0 trust-ad
search .

Commenting out the search entry fixes the issue. This happens because this machine is configured without a domain, but with a search path, leading to this being executed:
https://github.com/puppetlabs/facter/blame/7bfd75b1c76bb5d531c35131b717fb0697a54426/lib/facter/resolvers/linux/hostname.rb#L101-L103

And then a wrong concatenation:
https://github.com/puppetlabs/facter/blame/7bfd75b1c76bb5d531c35131b717fb0697a54426/lib/facter/resolvers/linux/hostname.rb#L110

Expected Behavior

facter should not read search in the first place. search is a client lookup option, and it is not intended to set the machine's hostname.

Steps to Reproduce

See resolv.conf configuration above. Make sure the machine has no domain configured.

Environment

# /opt/puppetlabs/bin/facter --version
4.6.1
 # uname -a
Linux xxx-xxx-prd-vm03 5.10.0-22-amd64 #1 SMP Debian 5.10.178-3 (2023-04-22) x86_64 GNU/Linux

Additional Context

Introduced in: #2296
CC @oanatmaria : I know it's been a while since you've made this contribution, but I couldn't find the reason that facter is reading search. Could you shed some light on this ?
EDIT: Tracking the git history seems to point to it being there since a long time, e.g. 0599a60

Migrated issue to FACT-3459