1
0
mirror of https://github.com/juanfont/headscale.git synced 2024-12-20 19:09:07 +01:00
juanfont.headscale/dns_test.go

358 lines
10 KiB
Go
Raw Normal View History

2021-10-09 12:22:21 +02:00
package headscale
import (
"fmt"
2021-10-09 12:22:21 +02:00
"gopkg.in/check.v1"
"inet.af/netaddr"
"tailscale.com/tailcfg"
"tailscale.com/types/dnstype"
2021-10-09 12:22:21 +02:00
)
func (s *Suite) TestMagicDNSRootDomains100(c *check.C) {
prefix := netaddr.MustParseIPPrefix("100.64.0.0/10")
2021-11-14 18:03:21 +01:00
domains := generateMagicDNSRootDomains(prefix)
2021-10-09 12:22:21 +02:00
found := false
2021-10-10 12:43:41 +02:00
for _, domain := range domains {
2021-10-09 12:22:21 +02:00
if domain == "64.100.in-addr.arpa." {
found = true
2021-11-14 16:46:09 +01:00
2021-10-09 12:22:21 +02:00
break
}
}
c.Assert(found, check.Equals, true)
found = false
2021-10-10 12:43:41 +02:00
for _, domain := range domains {
2021-10-09 12:22:21 +02:00
if domain == "100.100.in-addr.arpa." {
found = true
2021-11-14 16:46:09 +01:00
2021-10-09 12:22:21 +02:00
break
}
}
c.Assert(found, check.Equals, true)
found = false
2021-10-10 12:43:41 +02:00
for _, domain := range domains {
2021-10-09 12:22:21 +02:00
if domain == "127.100.in-addr.arpa." {
found = true
2021-11-14 16:46:09 +01:00
2021-10-09 12:22:21 +02:00
break
}
}
c.Assert(found, check.Equals, true)
}
func (s *Suite) TestMagicDNSRootDomains172(c *check.C) {
prefix := netaddr.MustParseIPPrefix("172.16.0.0/16")
2021-11-14 18:03:21 +01:00
domains := generateMagicDNSRootDomains(prefix)
2021-10-09 12:22:21 +02:00
found := false
2021-10-10 12:43:41 +02:00
for _, domain := range domains {
2021-10-09 12:22:21 +02:00
if domain == "0.16.172.in-addr.arpa." {
found = true
2021-11-14 16:46:09 +01:00
2021-10-09 12:22:21 +02:00
break
}
}
c.Assert(found, check.Equals, true)
found = false
2021-10-10 12:43:41 +02:00
for _, domain := range domains {
2021-10-09 12:22:21 +02:00
if domain == "255.16.172.in-addr.arpa." {
found = true
2021-11-14 16:46:09 +01:00
2021-10-09 12:22:21 +02:00
break
}
}
c.Assert(found, check.Equals, true)
}
func (s *Suite) TestDNSConfigMapResponseWithMagicDNS(c *check.C) {
2021-11-15 17:16:04 +01:00
namespaceShared1, err := app.CreateNamespace("shared1")
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
namespaceShared2, err := app.CreateNamespace("shared2")
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
namespaceShared3, err := app.CreateNamespace("shared3")
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
preAuthKeyInShared1, err := app.CreatePreAuthKey(
namespaceShared1.Name,
false,
false,
nil,
)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
preAuthKeyInShared2, err := app.CreatePreAuthKey(
namespaceShared2.Name,
false,
false,
nil,
)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
preAuthKeyInShared3, err := app.CreatePreAuthKey(
namespaceShared3.Name,
false,
false,
nil,
)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
PreAuthKey2InShared1, err := app.CreatePreAuthKey(
namespaceShared1.Name,
false,
false,
nil,
)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
_, err = app.GetMachine(namespaceShared1.Name, "test_get_shared_nodes_1")
c.Assert(err, check.NotNil)
2021-11-15 17:16:04 +01:00
machineInShared1 := &Machine{
ID: 1,
MachineKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
NodeKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
DiscoKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
Name: "test_get_shared_nodes_1",
2021-11-15 17:16:04 +01:00
NamespaceID: namespaceShared1.ID,
Namespace: *namespaceShared1,
Registered: true,
2021-11-18 09:49:55 +01:00
RegisterMethod: RegisterMethodAuthKey,
IPAddress: "100.64.0.1",
2021-11-15 17:16:04 +01:00
AuthKeyID: uint(preAuthKeyInShared1.ID),
}
2021-11-15 17:16:04 +01:00
app.db.Save(machineInShared1)
2021-11-15 17:16:04 +01:00
_, err = app.GetMachine(namespaceShared1.Name, machineInShared1.Name)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
machineInShared2 := &Machine{
ID: 2,
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
Name: "test_get_shared_nodes_2",
2021-11-15 17:16:04 +01:00
NamespaceID: namespaceShared2.ID,
Namespace: *namespaceShared2,
Registered: true,
2021-11-18 09:49:55 +01:00
RegisterMethod: RegisterMethodAuthKey,
IPAddress: "100.64.0.2",
2021-11-15 17:16:04 +01:00
AuthKeyID: uint(preAuthKeyInShared2.ID),
}
2021-11-15 17:16:04 +01:00
app.db.Save(machineInShared2)
2021-11-15 17:16:04 +01:00
_, err = app.GetMachine(namespaceShared2.Name, machineInShared2.Name)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
machineInShared3 := &Machine{
ID: 3,
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
Name: "test_get_shared_nodes_3",
2021-11-15 17:16:04 +01:00
NamespaceID: namespaceShared3.ID,
Namespace: *namespaceShared3,
Registered: true,
2021-11-18 09:49:55 +01:00
RegisterMethod: RegisterMethodAuthKey,
IPAddress: "100.64.0.3",
2021-11-15 17:16:04 +01:00
AuthKeyID: uint(preAuthKeyInShared3.ID),
}
2021-11-15 17:16:04 +01:00
app.db.Save(machineInShared3)
2021-11-15 17:16:04 +01:00
_, err = app.GetMachine(namespaceShared3.Name, machineInShared3.Name)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
machine2InShared1 := &Machine{
ID: 4,
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
Name: "test_get_shared_nodes_4",
2021-11-15 17:16:04 +01:00
NamespaceID: namespaceShared1.ID,
Namespace: *namespaceShared1,
Registered: true,
2021-11-18 09:49:55 +01:00
RegisterMethod: RegisterMethodAuthKey,
IPAddress: "100.64.0.4",
2021-11-15 17:16:04 +01:00
AuthKeyID: uint(PreAuthKey2InShared1.ID),
}
2021-11-15 17:16:04 +01:00
app.db.Save(machine2InShared1)
2021-11-15 17:16:04 +01:00
err = app.AddSharedMachineToNamespace(machineInShared2, namespaceShared1)
c.Assert(err, check.IsNil)
baseDomain := "foobar.headscale.net"
dnsConfigOrig := tailcfg.DNSConfig{
Routes: make(map[string][]dnstype.Resolver),
Domains: []string{baseDomain},
Proxied: true,
}
2021-11-15 17:16:04 +01:00
peersOfMachineInShared1, err := app.getPeers(machineInShared1)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
dnsConfig := getMapResponseDNSConfig(
&dnsConfigOrig,
baseDomain,
*machineInShared1,
peersOfMachineInShared1,
)
c.Assert(dnsConfig, check.NotNil)
c.Assert(len(dnsConfig.Routes), check.Equals, 2)
2021-11-15 17:16:04 +01:00
domainRouteShared1 := fmt.Sprintf("%s.%s", namespaceShared1.Name, baseDomain)
_, ok := dnsConfig.Routes[domainRouteShared1]
c.Assert(ok, check.Equals, true)
2021-11-15 17:16:04 +01:00
domainRouteShared2 := fmt.Sprintf("%s.%s", namespaceShared2.Name, baseDomain)
_, ok = dnsConfig.Routes[domainRouteShared2]
c.Assert(ok, check.Equals, true)
2021-11-15 17:16:04 +01:00
domainRouteShared3 := fmt.Sprintf("%s.%s", namespaceShared3.Name, baseDomain)
_, ok = dnsConfig.Routes[domainRouteShared3]
c.Assert(ok, check.Equals, false)
}
func (s *Suite) TestDNSConfigMapResponseWithoutMagicDNS(c *check.C) {
2021-11-15 17:16:04 +01:00
namespaceShared1, err := app.CreateNamespace("shared1")
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
namespaceShared2, err := app.CreateNamespace("shared2")
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
namespaceShared3, err := app.CreateNamespace("shared3")
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
preAuthKeyInShared1, err := app.CreatePreAuthKey(
namespaceShared1.Name,
false,
false,
nil,
)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
preAuthKeyInShared2, err := app.CreatePreAuthKey(
namespaceShared2.Name,
false,
false,
nil,
)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
preAuthKeyInShared3, err := app.CreatePreAuthKey(
namespaceShared3.Name,
false,
false,
nil,
)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
preAuthKey2InShared1, err := app.CreatePreAuthKey(
namespaceShared1.Name,
false,
false,
nil,
)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
_, err = app.GetMachine(namespaceShared1.Name, "test_get_shared_nodes_1")
c.Assert(err, check.NotNil)
2021-11-15 17:16:04 +01:00
machineInShared1 := &Machine{
ID: 1,
MachineKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
NodeKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
DiscoKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
Name: "test_get_shared_nodes_1",
2021-11-15 17:16:04 +01:00
NamespaceID: namespaceShared1.ID,
Namespace: *namespaceShared1,
Registered: true,
2021-11-18 09:49:55 +01:00
RegisterMethod: RegisterMethodAuthKey,
IPAddress: "100.64.0.1",
2021-11-15 17:16:04 +01:00
AuthKeyID: uint(preAuthKeyInShared1.ID),
}
2021-11-15 17:16:04 +01:00
app.db.Save(machineInShared1)
2021-11-15 17:16:04 +01:00
_, err = app.GetMachine(namespaceShared1.Name, machineInShared1.Name)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
machineInShared2 := &Machine{
ID: 2,
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
Name: "test_get_shared_nodes_2",
2021-11-15 17:16:04 +01:00
NamespaceID: namespaceShared2.ID,
Namespace: *namespaceShared2,
Registered: true,
2021-11-18 09:49:55 +01:00
RegisterMethod: RegisterMethodAuthKey,
IPAddress: "100.64.0.2",
2021-11-15 17:16:04 +01:00
AuthKeyID: uint(preAuthKeyInShared2.ID),
}
2021-11-15 17:16:04 +01:00
app.db.Save(machineInShared2)
2021-11-15 17:16:04 +01:00
_, err = app.GetMachine(namespaceShared2.Name, machineInShared2.Name)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
machineInShared3 := &Machine{
ID: 3,
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
Name: "test_get_shared_nodes_3",
2021-11-15 17:16:04 +01:00
NamespaceID: namespaceShared3.ID,
Namespace: *namespaceShared3,
Registered: true,
2021-11-18 09:49:55 +01:00
RegisterMethod: RegisterMethodAuthKey,
IPAddress: "100.64.0.3",
2021-11-15 17:16:04 +01:00
AuthKeyID: uint(preAuthKeyInShared3.ID),
}
2021-11-15 17:16:04 +01:00
app.db.Save(machineInShared3)
2021-11-15 17:16:04 +01:00
_, err = app.GetMachine(namespaceShared3.Name, machineInShared3.Name)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
machine2InShared1 := &Machine{
ID: 4,
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
Name: "test_get_shared_nodes_4",
2021-11-15 17:16:04 +01:00
NamespaceID: namespaceShared1.ID,
Namespace: *namespaceShared1,
Registered: true,
2021-11-18 09:49:55 +01:00
RegisterMethod: RegisterMethodAuthKey,
IPAddress: "100.64.0.4",
2021-11-15 17:16:04 +01:00
AuthKeyID: uint(preAuthKey2InShared1.ID),
}
2021-11-15 17:16:04 +01:00
app.db.Save(machine2InShared1)
2021-11-15 17:16:04 +01:00
err = app.AddSharedMachineToNamespace(machineInShared2, namespaceShared1)
c.Assert(err, check.IsNil)
baseDomain := "foobar.headscale.net"
dnsConfigOrig := tailcfg.DNSConfig{
Routes: make(map[string][]dnstype.Resolver),
Domains: []string{baseDomain},
Proxied: false,
}
2021-11-15 17:16:04 +01:00
peersOfMachine1Shared1, err := app.getPeers(machineInShared1)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
dnsConfig := getMapResponseDNSConfig(
&dnsConfigOrig,
baseDomain,
*machineInShared1,
peersOfMachine1Shared1,
)
c.Assert(dnsConfig, check.NotNil)
c.Assert(len(dnsConfig.Routes), check.Equals, 0)
c.Assert(len(dnsConfig.Domains), check.Equals, 1)
}