Skip to content

Commit 44e4a8f

Browse files
committed
Add test for autoApprovers feature
1 parent 088474e commit 44e4a8f

File tree

4 files changed

+83
-24
lines changed

4 files changed

+83
-24
lines changed

machine.go

+17-20
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,6 @@ func (h *Headscale) EnableAutoApprovedRoutes(machine *Machine) error {
945945
continue // Skip routes that are already enabled for the node
946946
}
947947

948-
approved := false
949948
routeApprovers, err := h.aclPolicy.AutoApprovers.GetRouteApprovers(advertisedRoute)
950949

951950
if err != nil {
@@ -957,26 +956,24 @@ func (h *Headscale) EnableAutoApprovedRoutes(machine *Machine) error {
957956
}
958957

959958
for _, approvedAlias := range routeApprovers {
960-
961-
approvedIps, err := expandAlias(thisMachine, *h.aclPolicy, approvedAlias, h.cfg.OIDC.StripEmaildomain)
962-
963-
if err != nil {
964-
log.Err(err).
965-
Str("alias", approvedAlias).
966-
Msg("Failed to expand alias when processing autoApprovers policy")
967-
return err
959+
if approvedAlias == machine.Namespace.Name {
960+
approvedRoutes = append(approvedRoutes, advertisedRoute)
961+
} else {
962+
approvedIps, err := expandAlias(thisMachine, *h.aclPolicy, approvedAlias, h.cfg.OIDC.StripEmaildomain)
963+
964+
if err != nil {
965+
log.Err(err).
966+
Str("alias", approvedAlias).
967+
Msg("Failed to expand alias when processing autoApprovers policy")
968+
return err
969+
}
970+
971+
// approvedIPs should contain all of machine's IPs if it matches the rule, so check for first
972+
if contains(approvedIps, machine.IPAddresses[0].String()) {
973+
approvedRoutes = append(approvedRoutes, advertisedRoute)
974+
975+
}
968976
}
969-
970-
// approvedIPs should contain all of machine's IPs if it matches the rule, so check for first
971-
approved = contains(approvedIps, machine.IPAddresses[0].String())
972-
973-
if approved {
974-
break
975-
}
976-
}
977-
978-
if approved {
979-
approvedRoutes = append(approvedRoutes, advertisedRoute)
980977
}
981978
}
982979

machine_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -1051,3 +1051,44 @@ func TestHeadscale_GenerateGivenName(t *testing.T) {
10511051
})
10521052
}
10531053
}
1054+
1055+
func (s *Suite) TestAutoApproveRoutes(c *check.C) {
1056+
err := app.LoadACLPolicy("./tests/acls/acl_policy_autoapprovers.hujson")
1057+
c.Assert(err, check.IsNil)
1058+
1059+
namespace, err := app.CreateNamespace("test")
1060+
c.Assert(err, check.IsNil)
1061+
1062+
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
1063+
c.Assert(err, check.IsNil)
1064+
1065+
nodeKey := key.NewNode()
1066+
1067+
defaultRoute := netaddr.MustParseIPPrefix("0.0.0.0/0")
1068+
route1 := netaddr.MustParseIPPrefix("10.10.0.0/16")
1069+
route2 := netaddr.MustParseIPPrefix("10.11.0.0/16")
1070+
1071+
machine := Machine{
1072+
ID: 0,
1073+
MachineKey: "foo",
1074+
NodeKey: NodePublicKeyStripPrefix(nodeKey.Public()),
1075+
DiscoKey: "faa",
1076+
Hostname: "test",
1077+
NamespaceID: namespace.ID,
1078+
RegisterMethod: RegisterMethodAuthKey,
1079+
AuthKeyID: uint(pak.ID),
1080+
HostInfo: HostInfo{
1081+
RequestTags: []string{"tag:exit"},
1082+
RoutableIPs: []netaddr.IPPrefix{defaultRoute, route1, route2},
1083+
},
1084+
IPAddresses: []netaddr.IP{netaddr.MustParseIP("100.64.0.1")},
1085+
}
1086+
1087+
app.db.Save(&machine)
1088+
1089+
machine0ByID, err := app.GetMachineByID(0)
1090+
c.Assert(err, check.IsNil)
1091+
1092+
app.EnableAutoApprovedRoutes(machine0ByID)
1093+
c.Assert(machine0ByID.GetEnabledRoutes(), check.HasLen, 3)
1094+
}

protocol_common_poll.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@ func (h *Headscale) handlePollCommon(
4444
}
4545

4646
// update routes with peer information
47-
err = h.EnableAutoApprovedRoutes(machine)
48-
if err != nil {
49-
//TODO
50-
}
47+
h.EnableAutoApprovedRoutes(machine)
5148
}
5249

5350
// From Tailscale client:
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// This ACL validates autoApprovers support for
2+
// exit nodes and advertised routes
3+
4+
{
5+
"tagOwners": {
6+
"tag:exit": ["test"],
7+
},
8+
9+
"groups": {
10+
"group:test": ["test"]
11+
},
12+
13+
"acls": [
14+
{"action": "accept", "users": ["*"], "ports": ["*:*"]},
15+
],
16+
17+
"autoApprovers": {
18+
"exitNode": ["tag:exit"],
19+
"routes": {
20+
"10.10.0.0/16": ["group:test"],
21+
"10.11.0.0/16": ["test"],
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)