kubelet: parseResolvConf: Handle "search ."

When parsing a resolv.conf file that has "search .", parseResolvConf should
accept the "." entry verbatim.  Before this commit, parseResolvConf
unconditionally trimmed the "." suffix, which in the case of "." resulted
in a "" entry (that is, the empty string).  This empty entry could lead
parseResolvConf to produce a resolv.conf file with "search ".  Resolvers
could fail to parse such a resolv.conf file from parseResolvConf, thus
breaking DNS resolution in pods.  After this commit, parseResolvConf
accepts a resolv.conf file with "search ." and passes the "." entry through
verbatim to produce a valid resolv.conf file.  The "." suffix is still
trimmed for any entry that does not solely comprise ".".

Follow-up to commit a215a88d91.

* pkg/kubelet/network/dns/dns.go (parseResolvConf): Handle a "." entry in
the search path by copying it verbatim.
* pkg/kubelet/network/dns/dns_test.go (TestParseResolvConf): Add a test
case for "search .".
This commit is contained in:
Miciah Masters 2022-04-12 13:52:22 -04:00 committed by Miciah Dashiel Butler Masters
parent 7380fc735a
commit 5832b84200
2 changed files with 5 additions and 1 deletions

View File

@ -267,7 +267,10 @@ func parseResolvConf(reader io.Reader) (nameservers []string, searches []string,
// Normalise search fields so the same domain with and without trailing dot will only count once, to avoid hitting search validation limits.
searches = []string{}
for _, s := range fields[1:] {
searches = append(searches, strings.TrimSuffix(s, "."))
if s != "." {
s = strings.TrimSuffix(s, ".")
}
searches = append(searches, s)
}
}
if fields[0] == "options" {

View File

@ -78,6 +78,7 @@ func TestParseResolvConf(t *testing.T) {
{"nameserver 1.2.3.4\nnameserver 5.6.7.8", []string{"1.2.3.4", "5.6.7.8"}, []string{}, []string{}, false},
{"nameserver 1.2.3.4 #comment", []string{"1.2.3.4"}, []string{}, []string{}, false},
{"search ", []string{}, []string{}, []string{}, false}, // search empty
{"search .", []string{}, []string{"."}, []string{}, false},
{"search foo", []string{}, []string{"foo"}, []string{}, false},
{"search foo bar", []string{}, []string{"foo", "bar"}, []string{}, false},
{"search foo. bar", []string{}, []string{"foo", "bar"}, []string{}, false},