Skip to content

Commit fa22a10

Browse files
committed
libnetwork/resolvconf: add new KeepHostSearches/Options
Using one KeepHostServers that controls the override of nameservers, search domains and options is not good enough. With netavark 1.15 we dropped the dns.podman search domain[1] as this always overwrote the host search domains which was not correct. However that in turn caused a new issue[2] that a container name might now get resolved to a search domain from the host first. To fix that we either need to revert the dns.podman change or add the ndots:0 option in resolv.conf. Whatever we end up doing we will need one of KeepHostSearches or KeepHostOptions in podman to populate resolv.conf correctly so that we don't overwrite the host domains/options but still can overwrite the nameservers as we want to force aardvark-dns only as nameserver so resolvers cannot bypass it. [1] containers/netavark#1214 [2] containers/podman#26198 Signed-off-by: Paul Holzinger <pholzing@redhat.com> (cherry picked from commit b4bf1f2) Signed-off-by: Paul Holzinger <pholzing@redhat.com>
1 parent 27569a9 commit fa22a10

File tree

2 files changed

+70
-25
lines changed

2 files changed

+70
-25
lines changed

libnetwork/resolvconf/resolv.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,28 @@ type Params struct {
3030
// IPv6Enabled will filter ipv6 nameservers when not set to true.
3131
IPv6Enabled bool
3232
// KeepHostServers can be set when it is required to still keep the
33-
// original resolv.conf content even when custom Nameserver/Searches/Options
33+
// original resolv.conf nameservers even when explicit Nameservers
3434
// are set. In this case they will be appended to the given values.
3535
KeepHostServers bool
36+
// KeepHostSearches can be set when it is required to still keep the
37+
// original resolv.conf search domains even when explicit search domains
38+
// are set in Searches.
39+
KeepHostSearches bool
40+
// KeepHostOptions can be set when it is required to still keep the
41+
// original resolv.conf options even when explicit options are set in
42+
// Options.
43+
KeepHostOptions bool
3644
// Nameservers is a list of nameservers the container should use,
37-
// instead of the default ones from the host.
45+
// instead of the default ones from the host. Set KeepHostServers
46+
// in order to also keep the hosts resolv.conf nameservers.
3847
Nameservers []string
3948
// Searches is a list of dns search domains the container should use,
40-
// instead of the default ones from the host.
49+
// instead of the default ones from the host. Set KeepHostSearches
50+
// in order to also keep the hosts resolv.conf search domains.
4151
Searches []string
4252
// Options is a list of dns options the container should use,
43-
// instead of the default ones from the host.
53+
// instead of the default ones from the host. Set KeepHostOptions
54+
// in order to also keep the hosts resolv.conf options.
4455
Options []string
4556

4657
// resolvConfPath is the path which should be used as base to get the dns
@@ -121,7 +132,8 @@ func unsetSearchDomainsIfNeeded(searches []string) []string {
121132
// New creates a new resolv.conf file with the given params.
122133
func New(params *Params) error {
123134
// short path, if everything is given there is no need to actually read the hosts /etc/resolv.conf
124-
if len(params.Nameservers) > 0 && len(params.Options) > 0 && len(params.Searches) > 0 && !params.KeepHostServers {
135+
if len(params.Nameservers) > 0 && len(params.Options) > 0 && len(params.Searches) > 0 &&
136+
!params.KeepHostServers && !params.KeepHostOptions && !params.KeepHostSearches {
125137
return build(params.Path, params.Nameservers, unsetSearchDomainsIfNeeded(params.Searches), params.Options)
126138
}
127139

@@ -140,12 +152,12 @@ func New(params *Params) error {
140152
searches := unsetSearchDomainsIfNeeded(params.Searches)
141153
// if no params.Searches then use host ones
142154
// otherwise make sure that they were no explicitly unset before adding host ones
143-
if len(params.Searches) == 0 || (params.KeepHostServers && len(searches) > 0) {
155+
if len(params.Searches) == 0 || (params.KeepHostSearches && len(searches) > 0) {
144156
searches = append(searches, getSearchDomains(content)...)
145157
}
146158

147159
options := params.Options
148-
if len(options) == 0 || params.KeepHostServers {
160+
if len(options) == 0 || params.KeepHostOptions {
149161
options = append(options, getOptions(content)...)
150162
}
151163

libnetwork/resolvconf/resolv_test.go

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,17 @@ options edns0
1919

2020
func TestNew(t *testing.T) {
2121
tests := []struct {
22-
name string
23-
baseContent string
24-
nameservers []string
25-
options []string
26-
searches []string
27-
ipv6 bool
28-
hostns bool
29-
keepHostServers bool
30-
want string
22+
name string
23+
baseContent string
24+
nameservers []string
25+
options []string
26+
searches []string
27+
ipv6 bool
28+
hostns bool
29+
keepHostServers bool
30+
keepHostSearches bool
31+
keepHostOptions bool
32+
want string
3133
}{
3234
{
3335
name: "simple resolv.conf",
@@ -98,7 +100,36 @@ func TestNew(t *testing.T) {
98100
options: []string{"ndots:2"},
99101
searches: []string{"test.com"},
100102
keepHostServers: true,
101-
want: "search test.com example.com\nnameserver 1.2.3.4\nnameserver 5.6.7.8\nnameserver 1.1.1.1\noptions ndots:2 edns0\n",
103+
want: "search test.com\nnameserver 1.2.3.4\nnameserver 5.6.7.8\nnameserver 1.1.1.1\noptions ndots:2\n",
104+
},
105+
{
106+
name: "set all and keep host searches",
107+
baseContent: resolv2,
108+
nameservers: []string{"1.2.3.4", "5.6.7.8"},
109+
options: []string{"ndots:2"},
110+
searches: []string{"test.com"},
111+
keepHostSearches: true,
112+
want: "search test.com example.com\nnameserver 1.2.3.4\nnameserver 5.6.7.8\noptions ndots:2\n",
113+
},
114+
{
115+
name: "set all and keep host options",
116+
baseContent: resolv2,
117+
nameservers: []string{"1.2.3.4", "5.6.7.8"},
118+
options: []string{"ndots:2"},
119+
searches: []string{"test.com"},
120+
keepHostOptions: true,
121+
want: "search test.com\nnameserver 1.2.3.4\nnameserver 5.6.7.8\noptions ndots:2 edns0\n",
122+
},
123+
{
124+
name: "set all and keep all options",
125+
baseContent: resolv2,
126+
nameservers: []string{"1.2.3.4", "5.6.7.8"},
127+
options: []string{"ndots:2"},
128+
searches: []string{"test.com"},
129+
keepHostServers: true,
130+
keepHostSearches: true,
131+
keepHostOptions: true,
132+
want: "search test.com example.com\nnameserver 1.2.3.4\nnameserver 5.6.7.8\nnameserver 1.1.1.1\noptions ndots:2 edns0\n",
102133
},
103134
{
104135
name: "localhost nameservers should be filtered and use defaults instead",
@@ -148,14 +179,16 @@ func TestNew(t *testing.T) {
148179
}
149180
}
150181
err = New(&Params{
151-
Path: target,
152-
Nameservers: tt.nameservers,
153-
Searches: tt.searches,
154-
Options: tt.options,
155-
IPv6Enabled: tt.ipv6,
156-
KeepHostServers: tt.keepHostServers,
157-
Namespaces: namespaces,
158-
resolvConfPath: base,
182+
Path: target,
183+
Nameservers: tt.nameservers,
184+
Searches: tt.searches,
185+
Options: tt.options,
186+
IPv6Enabled: tt.ipv6,
187+
KeepHostServers: tt.keepHostServers,
188+
KeepHostSearches: tt.keepHostSearches,
189+
KeepHostOptions: tt.keepHostOptions,
190+
Namespaces: namespaces,
191+
resolvConfPath: base,
159192
})
160193
assert.NoError(t, err, "New()")
161194
content, err := os.ReadFile(target)

0 commit comments

Comments
 (0)