Skip to content

Commit e78d91d

Browse files
committed
Convert host names to fully qualified domain names
- Adds test cases for A records and Cnames - Move cname conversion out of yaml into dns package Signed-off-by: Nino Kodabande <[email protected]>
1 parent 38725f4 commit e78d91d

File tree

5 files changed

+51
-23
lines changed

5 files changed

+51
-23
lines changed

pkg/hostagent/dns/dns.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"runtime"
99
"strings"
1010

11-
"github.com/lima-vm/lima/pkg/limayaml"
1211
"github.com/miekg/dns"
1312
"github.com/sirupsen/logrus"
1413
)
@@ -132,10 +131,11 @@ func NewHandler(opts HandlerOptions) (dns.Handler, error) {
132131
hostToIP: make(map[string]net.IP),
133132
}
134133
for host, address := range opts.StaticHosts {
134+
cname := dns.CanonicalName(host)
135135
if ip := net.ParseIP(address); ip != nil {
136-
h.hostToIP[host] = ip
136+
h.hostToIP[cname] = ip
137137
} else {
138-
h.cnameToHost[host] = limayaml.Cname(address)
138+
h.cnameToHost[cname] = dns.CanonicalName(address)
139139
}
140140
}
141141
return h, nil

pkg/hostagent/dns/dns_test.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ func TestDNSRecords(t *testing.T) {
4747
options := HandlerOptions{
4848
IPv6: true,
4949
StaticHosts: map[string]string{
50-
"MY.Host": "host.lima.internal",
50+
"MY.DOMAIN.COM": "192.168.0.23",
51+
"host.lima.internal": "10.10.0.34",
52+
"my.host": "host.lima.internal",
53+
"default": "my.domain.com",
5154
},
5255
}
5356

@@ -81,6 +84,40 @@ func TestDNSRecords(t *testing.T) {
8184
assert.Assert(t, regexMatch(dnsResult.String(), tc.expectedTXTRecord))
8285
}
8386
})
87+
88+
t.Run("test A records", func(t *testing.T) {
89+
tests := []struct {
90+
testDomain string
91+
expectedARecord string
92+
}{
93+
{testDomain: "my.domain.com", expectedARecord: `my.domain.com.\s+5\s+IN\s+A\s+192.168.0.23`},
94+
{testDomain: "host.lima.internal", expectedARecord: `host.lima.internal.\s+5\s+IN\s+A\s+10.10.0.34`},
95+
}
96+
97+
for _, tc := range tests {
98+
req := new(dns.Msg)
99+
req.SetQuestion(dns.Fqdn(tc.testDomain), dns.TypeA)
100+
h.ServeDNS(w, req)
101+
assert.Assert(t, regexMatch(dnsResult.String(), tc.expectedARecord))
102+
}
103+
})
104+
105+
t.Run("test CNAME records", func(t *testing.T) {
106+
tests := []struct {
107+
testDomain string
108+
expectedCNAME string
109+
}{
110+
{testDomain: "my.host", expectedCNAME: `my.host.\s+5\s+IN\s+CNAME\s+host.lima.internal.`},
111+
{testDomain: "default", expectedCNAME: `default.\s+5\s+IN\s+CNAME\s+my.domain.com.`},
112+
}
113+
114+
for _, tc := range tests {
115+
req := new(dns.Msg)
116+
req.SetQuestion(dns.Fqdn(tc.testDomain), dns.TypeCNAME)
117+
h.ServeDNS(w, req)
118+
assert.Assert(t, regexMatch(dnsResult.String(), tc.expectedCNAME))
119+
}
120+
})
84121
}
85122

86123
type TestResponseWriter struct {

pkg/hostagent/hostagent.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,8 @@ func (a *HostAgent) Run(ctx context.Context) error {
307307

308308
if *a.y.HostResolver.Enabled {
309309
hosts := a.y.HostResolver.Hosts
310-
hosts["host.lima.internal."] = qemuconst.SlirpGateway
311-
hosts[fmt.Sprintf("lima-%s.", a.instName)] = qemuconst.SlirpIPAddress
310+
hosts["host.lima.internal"] = qemuconst.SlirpGateway
311+
hosts[fmt.Sprintf("lima-%s", a.instName)] = qemuconst.SlirpIPAddress
312312
srvOpts := dns.ServerOptions{
313313
UDPPort: a.udpDNSLocalPort,
314314
TCPPort: a.tcpDNSLocalPort,

pkg/limayaml/defaults.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"path/filepath"
1010
"runtime"
1111
"strconv"
12-
"strings"
1312
"text/template"
1413

1514
"github.com/lima-vm/lima/pkg/guestagent/api"
@@ -233,13 +232,13 @@ func FillDefault(y, d, o *LimaYAML, filePath string) {
233232
hosts := make(map[string]string)
234233
// Values can be either names or IP addresses. Name values are canonicalized in the hostResolver.
235234
for k, v := range d.HostResolver.Hosts {
236-
hosts[Cname(k)] = v
235+
hosts[k] = v
237236
}
238237
for k, v := range y.HostResolver.Hosts {
239-
hosts[Cname(k)] = v
238+
hosts[k] = v
240239
}
241240
for k, v := range o.HostResolver.Hosts {
242-
hosts[Cname(k)] = v
241+
hosts[k] = v
243242
}
244243
y.HostResolver.Hosts = hosts
245244

@@ -667,14 +666,6 @@ func IsNativeArch(arch Arch) bool {
667666
return nativeX8664 || nativeAARCH64 || nativeRISCV64
668667
}
669668

670-
func Cname(host string) string {
671-
host = strings.ToLower(host)
672-
if !strings.HasSuffix(host, ".") {
673-
host += "."
674-
}
675-
return host
676-
}
677-
678669
func unique(s []string) []string {
679670
keys := make(map[string]bool)
680671
list := []string{}

pkg/limayaml/defaults_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func TestFillDefault(t *testing.T) {
149149

150150
expect := builtin
151151
expect.HostResolver.Hosts = map[string]string{
152-
"my.host.": "host.lima.internal",
152+
"MY.Host": "host.lima.internal",
153153
}
154154

155155
expect.Mounts = y.Mounts
@@ -319,7 +319,7 @@ func TestFillDefault(t *testing.T) {
319319
expect.Mounts[0].NineP.Msize = pointer.String(Default9pMsize)
320320
expect.Mounts[0].NineP.Cache = pointer.String(Default9pCacheForRO)
321321
expect.HostResolver.Hosts = map[string]string{
322-
"default.": d.HostResolver.Hosts["default"],
322+
"default": d.HostResolver.Hosts["default"],
323323
}
324324
expect.MountType = pointer.String(REVSSHFS)
325325
expect.CACertificates.RemoveDefaults = pointer.Bool(true)
@@ -348,7 +348,7 @@ func TestFillDefault(t *testing.T) {
348348
expect.Mounts = append(d.Mounts, y.Mounts...)
349349
expect.Networks = append(d.Networks, y.Networks...)
350350

351-
expect.HostResolver.Hosts["default."] = d.HostResolver.Hosts["default"]
351+
expect.HostResolver.Hosts["default"] = d.HostResolver.Hosts["default"]
352352

353353
// d.DNS will be ignored, and not appended to y.DNS
354354

@@ -474,8 +474,8 @@ func TestFillDefault(t *testing.T) {
474474
expect.PortForwards = append(append(o.PortForwards, y.PortForwards...), d.PortForwards...)
475475
expect.Containerd.Archives = append(append(o.Containerd.Archives, y.Containerd.Archives...), d.Containerd.Archives...)
476476

477-
expect.HostResolver.Hosts["default."] = d.HostResolver.Hosts["default"]
478-
expect.HostResolver.Hosts["my.host."] = d.HostResolver.Hosts["host.lima.internal"]
477+
expect.HostResolver.Hosts["default"] = d.HostResolver.Hosts["default"]
478+
expect.HostResolver.Hosts["MY.Host"] = d.HostResolver.Hosts["host.lima.internal"]
479479

480480
// o.Mounts just makes d.Mounts[0] writable because the Location matches
481481
expect.Mounts = append(d.Mounts, y.Mounts...)

0 commit comments

Comments
 (0)