2023-05-26 11:26:34 +01:00
|
|
|
package mapper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-05-31 17:26:19 +02:00
|
|
|
"net/netip"
|
2023-05-26 11:26:34 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
2023-05-31 17:26:19 +02:00
|
|
|
"github.com/juanfont/headscale/hscontrol/policy"
|
2025-05-27 16:27:16 +02:00
|
|
|
"github.com/juanfont/headscale/hscontrol/policy/matcher"
|
2025-02-26 07:22:55 -08:00
|
|
|
"github.com/juanfont/headscale/hscontrol/routes"
|
2023-05-26 11:26:34 +01:00
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
"tailscale.com/types/dnstype"
|
|
|
|
)
|
|
|
|
|
2024-04-17 07:03:06 +02:00
|
|
|
var iap = func(ipStr string) *netip.Addr {
|
|
|
|
ip := netip.MustParseAddr(ipStr)
|
|
|
|
return &ip
|
|
|
|
}
|
|
|
|
|
2023-05-26 11:26:34 +01:00
|
|
|
func TestDNSConfigMapResponse(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
magicDNS bool
|
|
|
|
want *tailcfg.DNSConfig
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
magicDNS: true,
|
|
|
|
want: &tailcfg.DNSConfig{
|
Redo OIDC configuration (#2020)
expand user, add claims to user
This commit expands the user table with additional fields that
can be retrieved from OIDC providers (and other places) and
uses this data in various tailscale response objects if it is
available.
This is the beginning of implementing
https://docs.google.com/document/d/1X85PMxIaVWDF6T_UPji3OeeUqVBcGj_uHRM5CI-AwlY/edit
trying to make OIDC more coherant and maintainable in addition
to giving the user a better experience and integration with a
provider.
remove usernames in magic dns, normalisation of emails
this commit removes the option to have usernames as part of MagicDNS
domains and headscale will now align with Tailscale, where there is a
root domain, and the machine name.
In addition, the various normalisation functions for dns names has been
made lighter not caring about username and special character that wont
occur.
Email are no longer normalised as part of the policy processing.
untagle oidc and regcache, use typed cache
This commits stops reusing the registration cache for oidc
purposes and switches the cache to be types and not use any
allowing the removal of a bunch of casting.
try to make reauth/register branches clearer in oidc
Currently there was a function that did a bunch of stuff,
finding the machine key, trying to find the node, reauthing
the node, returning some status, and it was called validate
which was very confusing.
This commit tries to split this into what to do if the node
exists, if it needs to register etc.
Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
2024-10-02 14:50:17 +02:00
|
|
|
Routes: map[string][]*dnstype.Resolver{},
|
2023-05-26 11:26:34 +01:00
|
|
|
Domains: []string{
|
|
|
|
"foobar.headscale.net",
|
|
|
|
},
|
|
|
|
Proxied: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
magicDNS: false,
|
|
|
|
want: &tailcfg.DNSConfig{
|
|
|
|
Domains: []string{"foobar.headscale.net"},
|
|
|
|
Proxied: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(fmt.Sprintf("with-magicdns-%v", tt.magicDNS), func(t *testing.T) {
|
2023-09-24 13:42:05 +02:00
|
|
|
mach := func(hostname, username string, userid uint) *types.Node {
|
|
|
|
return &types.Node{
|
2023-05-26 11:26:34 +01:00
|
|
|
Hostname: hostname,
|
|
|
|
UserID: userid,
|
|
|
|
User: types.User{
|
|
|
|
Name: username,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
baseDomain := "foobar.headscale.net"
|
|
|
|
|
|
|
|
dnsConfigOrig := tailcfg.DNSConfig{
|
|
|
|
Routes: make(map[string][]*dnstype.Resolver),
|
|
|
|
Domains: []string{baseDomain},
|
|
|
|
Proxied: tt.magicDNS,
|
|
|
|
}
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
nodeInShared1 := mach("test_get_shared_nodes_1", "shared1", 1)
|
2023-05-26 11:26:34 +01:00
|
|
|
|
|
|
|
got := generateDNSConfig(
|
2024-06-26 13:44:40 +02:00
|
|
|
&types.Config{
|
2024-12-13 07:52:40 +00:00
|
|
|
TailcfgDNSConfig: &dnsConfigOrig,
|
2024-06-26 13:44:40 +02:00
|
|
|
},
|
2025-07-05 23:31:13 +02:00
|
|
|
nodeInShared1.View(),
|
2023-05-26 11:26:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
if diff := cmp.Diff(tt.want, got, cmpopts.EquateEmpty()); diff != "" {
|
2023-05-31 09:59:37 +02:00
|
|
|
t.Errorf("expandAlias() unexpected result (-want +got):\n%s", diff)
|
2023-05-26 11:26:34 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-05-31 17:26:19 +02:00
|
|
|
|
2025-07-10 23:38:55 +02:00
|
|
|
// mockState is a mock implementation that provides the required methods.
|
2025-05-27 16:27:16 +02:00
|
|
|
type mockState struct {
|
|
|
|
polMan policy.PolicyManager
|
|
|
|
derpMap *tailcfg.DERPMap
|
|
|
|
primary *routes.PrimaryRoutes
|
|
|
|
nodes types.Nodes
|
|
|
|
peers types.Nodes
|
|
|
|
}
|
2023-05-31 17:26:19 +02:00
|
|
|
|
2025-05-27 16:27:16 +02:00
|
|
|
func (m *mockState) DERPMap() *tailcfg.DERPMap {
|
|
|
|
return m.derpMap
|
|
|
|
}
|
2023-05-31 17:26:19 +02:00
|
|
|
|
2025-05-27 16:27:16 +02:00
|
|
|
func (m *mockState) Filter() ([]tailcfg.FilterRule, []matcher.Match) {
|
|
|
|
if m.polMan == nil {
|
|
|
|
return tailcfg.FilterAllowAll, nil
|
2023-05-31 17:26:19 +02:00
|
|
|
}
|
2025-05-27 16:27:16 +02:00
|
|
|
return m.polMan.Filter()
|
|
|
|
}
|
2023-05-31 17:26:19 +02:00
|
|
|
|
2025-07-05 23:31:13 +02:00
|
|
|
func (m *mockState) SSHPolicy(node types.NodeView) (*tailcfg.SSHPolicy, error) {
|
2025-05-27 16:27:16 +02:00
|
|
|
if m.polMan == nil {
|
|
|
|
return nil, nil
|
2023-05-31 17:26:19 +02:00
|
|
|
}
|
2025-05-27 16:27:16 +02:00
|
|
|
return m.polMan.SSHPolicy(node)
|
|
|
|
}
|
2023-05-31 17:26:19 +02:00
|
|
|
|
2025-07-05 23:31:13 +02:00
|
|
|
func (m *mockState) NodeCanHaveTag(node types.NodeView, tag string) bool {
|
2025-05-27 16:27:16 +02:00
|
|
|
if m.polMan == nil {
|
|
|
|
return false
|
2023-05-31 18:45:04 +02:00
|
|
|
}
|
2025-05-27 16:27:16 +02:00
|
|
|
return m.polMan.NodeCanHaveTag(node, tag)
|
|
|
|
}
|
2023-05-31 18:45:04 +02:00
|
|
|
|
2025-05-27 16:27:16 +02:00
|
|
|
func (m *mockState) GetNodePrimaryRoutes(nodeID types.NodeID) []netip.Prefix {
|
|
|
|
if m.primary == nil {
|
|
|
|
return nil
|
2023-05-31 18:45:04 +02:00
|
|
|
}
|
2025-05-27 16:27:16 +02:00
|
|
|
return m.primary.PrimaryRoutes(nodeID)
|
|
|
|
}
|
2023-05-31 18:45:04 +02:00
|
|
|
|
2025-05-27 16:27:16 +02:00
|
|
|
func (m *mockState) ListPeers(nodeID types.NodeID, peerIDs ...types.NodeID) (types.Nodes, error) {
|
|
|
|
if len(peerIDs) > 0 {
|
|
|
|
// Filter peers by the provided IDs
|
|
|
|
var filtered types.Nodes
|
|
|
|
for _, peer := range m.peers {
|
|
|
|
for _, id := range peerIDs {
|
|
|
|
if peer.ID == id {
|
|
|
|
filtered = append(filtered, peer)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-07-10 23:38:55 +02:00
|
|
|
|
2025-05-27 16:27:16 +02:00
|
|
|
return filtered, nil
|
2023-05-31 18:45:04 +02:00
|
|
|
}
|
2025-05-27 16:27:16 +02:00
|
|
|
// Return all peers except the node itself
|
|
|
|
var filtered types.Nodes
|
|
|
|
for _, peer := range m.peers {
|
|
|
|
if peer.ID != nodeID {
|
|
|
|
filtered = append(filtered, peer)
|
|
|
|
}
|
2023-05-31 18:45:04 +02:00
|
|
|
}
|
2025-07-10 23:38:55 +02:00
|
|
|
|
2025-05-27 16:27:16 +02:00
|
|
|
return filtered, nil
|
|
|
|
}
|
2023-05-31 18:45:04 +02:00
|
|
|
|
2025-05-27 16:27:16 +02:00
|
|
|
func (m *mockState) ListNodes(nodeIDs ...types.NodeID) (types.Nodes, error) {
|
|
|
|
if len(nodeIDs) > 0 {
|
|
|
|
// Filter nodes by the provided IDs
|
|
|
|
var filtered types.Nodes
|
|
|
|
for _, node := range m.nodes {
|
|
|
|
for _, id := range nodeIDs {
|
|
|
|
if node.ID == id {
|
|
|
|
filtered = append(filtered, node)
|
|
|
|
break
|
2025-03-10 16:20:29 +01:00
|
|
|
}
|
2023-05-31 17:26:19 +02:00
|
|
|
}
|
2025-05-27 16:27:16 +02:00
|
|
|
}
|
2025-07-10 23:38:55 +02:00
|
|
|
|
2025-05-27 16:27:16 +02:00
|
|
|
return filtered, nil
|
2023-05-31 17:26:19 +02:00
|
|
|
}
|
2025-07-10 23:38:55 +02:00
|
|
|
|
2025-05-27 16:27:16 +02:00
|
|
|
return m.nodes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_fullMapResponse(t *testing.T) {
|
|
|
|
t.Skip("Test needs to be refactored for new state-based architecture")
|
|
|
|
// TODO: Refactor this test to work with the new state-based mapper
|
|
|
|
// The test architecture needs to be updated to work with the state interface
|
|
|
|
// instead of the old direct dependency injection pattern
|
2023-05-31 17:26:19 +02:00
|
|
|
}
|