Skip to content

Commit 0181bb7

Browse files
Geod24AriaXLi
authored andcommitted
networking: Workaround 2693 by ignoring resolv.conf entries starting with dot
Currently, systems without a FQDN can be mishandled by facter for certain `/etc/resolv.conf` content. This was initially noticed when systemd-resolved was installed on a host without domain. systemd-resolved stub resolver sets 'search .' as a search domain, which results in the following hostname/domain/fqdn triplet: foo, ., foo... See: https://github.com/systemd/systemd/blob/v255/src/resolve/resolv.conf#L19 This is wrong on multiple levels: first, facter does not seem to handle '.' (the root level) well, and there have been PRs to remove the trailing dot in FQDN as far back as 2012 (PR 200), leaving user with a convenient, but sometimes ambiguous string that is not fully qualified. This commit implements a a middle ground solution to support top-level/domainless servers without making a breaking change. Additionally, it adds more coverage for various search cases.
1 parent 92a6484 commit 0181bb7

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

lib/facter/resolvers/hostname.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ def hostname_and_no_domain?(hostname, domain)
6868

6969
def read_domain
7070
file_content = Facter::Util::FileHelper.safe_read('/etc/resolv.conf')
71-
if file_content =~ /^domain\s+(\S+)/
71+
if file_content =~ /^domain\s+([^.]\S+)/
7272
Regexp.last_match(1)
73-
elsif file_content =~ /^search\s+(\S+)/
73+
elsif file_content =~ /^search\s+([^.]\S+)/
7474
Regexp.last_match(1)
7575
end
7676
end

lib/facter/resolvers/linux/hostname.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ def hostname_and_no_domain?(hostname, domain)
9696

9797
def read_domain
9898
file_content = Facter::Util::FileHelper.safe_read('/etc/resolv.conf')
99-
if file_content =~ /^domain\s+(\S+)/
99+
if file_content =~ /^domain\s+([^.]\S+)/
100100
Regexp.last_match(1)
101-
elsif file_content =~ /^search\s+(\S+)/
101+
elsif file_content =~ /^search\s+([^.]\S+)/
102102
Regexp.last_match(1)
103103
end
104104
end

spec/facter/resolvers/linux/hostname_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,30 @@
7979

8080
it_behaves_like 'detects values'
8181
end
82+
83+
context 'when /etc/resolv.conf has "search ."' do
84+
let(:resolv_conf) { "search .\n" }
85+
let(:domain) { nil }
86+
let(:fqdn) { hostname }
87+
88+
it_behaves_like 'detects values'
89+
end
90+
91+
context 'when /etc/resolv.conf has "search ." with multiple entires' do
92+
let(:resolv_conf) { 'search . foo.bar' }
93+
let(:domain) { nil }
94+
let(:fqdn) { hostname }
95+
96+
it_behaves_like 'detects values'
97+
end
98+
99+
context 'when /etc/resolv.conf has "search" with multiple entires' do
100+
let(:resolv_conf) { 'search foo.bar example.com' }
101+
let(:domain) { 'foo.bar' }
102+
let(:fqdn) { "#{hostname}.#{domain}" }
103+
104+
it_behaves_like 'detects values'
105+
end
82106
end
83107

84108
context 'when FFI is not installed' do

0 commit comments

Comments
 (0)