headscale/acls_test.go

161 lines
4.7 KiB
Go
Raw Normal View History

2021-07-03 11:55:32 +02:00
package headscale
import (
"gopkg.in/check.v1"
)
func (s *Suite) TestWrongPath(c *check.C) {
2021-07-04 13:33:00 +02:00
err := h.LoadACLPolicy("asdfg")
2021-07-03 11:55:32 +02:00
c.Assert(err, check.NotNil)
}
func (s *Suite) TestBrokenHuJson(c *check.C) {
2021-07-04 13:33:00 +02:00
err := h.LoadACLPolicy("./tests/acls/broken.hujson")
2021-07-03 11:55:32 +02:00
c.Assert(err, check.NotNil)
}
func (s *Suite) TestInvalidPolicyHuson(c *check.C) {
2021-07-04 13:33:00 +02:00
err := h.LoadACLPolicy("./tests/acls/invalid.hujson")
2021-07-03 11:55:32 +02:00
c.Assert(err, check.NotNil)
2021-07-03 17:31:32 +02:00
c.Assert(err, check.Equals, errorEmptyPolicy)
2021-07-03 11:55:32 +02:00
}
2021-07-03 17:31:32 +02:00
func (s *Suite) TestParseHosts(c *check.C) {
var hs Hosts
err := hs.UnmarshalJSON([]byte(`{"example-host-1": "100.100.100.100","example-host-2": "100.100.101.100/24"}`))
c.Assert(hs, check.NotNil)
2021-07-03 11:55:32 +02:00
c.Assert(err, check.IsNil)
2021-07-03 17:31:32 +02:00
}
func (s *Suite) TestParseInvalidCIDR(c *check.C) {
var hs Hosts
err := hs.UnmarshalJSON([]byte(`{"example-host-1": "100.100.100.100/42"}`))
c.Assert(hs, check.IsNil)
c.Assert(err, check.NotNil)
}
func (s *Suite) TestRuleInvalidGeneration(c *check.C) {
2021-07-04 13:33:00 +02:00
err := h.LoadACLPolicy("./tests/acls/acl_policy_invalid.hujson")
2021-07-03 17:31:32 +02:00
c.Assert(err, check.NotNil)
}
func (s *Suite) TestBasicRule(c *check.C) {
2021-07-04 13:33:00 +02:00
err := h.LoadACLPolicy("./tests/acls/acl_policy_basic_1.hujson")
2021-07-03 17:31:32 +02:00
c.Assert(err, check.IsNil)
rules, err := h.generateACLRules()
c.Assert(err, check.IsNil)
2021-07-04 13:01:41 +02:00
c.Assert(rules, check.NotNil)
}
func (s *Suite) TestPortRange(c *check.C) {
2021-07-04 13:33:00 +02:00
err := h.LoadACLPolicy("./tests/acls/acl_policy_basic_range.hujson")
2021-07-04 13:01:41 +02:00
c.Assert(err, check.IsNil)
rules, err := h.generateACLRules()
c.Assert(err, check.IsNil)
c.Assert(rules, check.NotNil)
c.Assert(*rules, check.HasLen, 1)
c.Assert((*rules)[0].DstPorts, check.HasLen, 1)
c.Assert((*rules)[0].DstPorts[0].Ports.First, check.Equals, uint16(5400))
c.Assert((*rules)[0].DstPorts[0].Ports.Last, check.Equals, uint16(5500))
}
func (s *Suite) TestPortWildcard(c *check.C) {
2021-07-04 13:33:00 +02:00
err := h.LoadACLPolicy("./tests/acls/acl_policy_basic_wildcards.hujson")
2021-07-04 13:01:41 +02:00
c.Assert(err, check.IsNil)
rules, err := h.generateACLRules()
c.Assert(err, check.IsNil)
c.Assert(rules, check.NotNil)
c.Assert(*rules, check.HasLen, 1)
c.Assert((*rules)[0].DstPorts, check.HasLen, 1)
c.Assert((*rules)[0].DstPorts[0].Ports.First, check.Equals, uint16(0))
c.Assert((*rules)[0].DstPorts[0].Ports.Last, check.Equals, uint16(65535))
c.Assert((*rules)[0].SrcIPs, check.HasLen, 1)
c.Assert((*rules)[0].SrcIPs[0], check.Equals, "*")
}
func (s *Suite) TestPortNamespace(c *check.C) {
n, err := h.CreateNamespace("testnamespace")
c.Assert(err, check.IsNil)
pak, err := h.CreatePreAuthKey(n.Name, false, false, nil)
c.Assert(err, check.IsNil)
_, err = h.GetMachine("testnamespace", "testmachine")
c.Assert(err, check.NotNil)
ip, _ := h.getAvailableIP()
m := Machine{
ID: 0,
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
Name: "testmachine",
NamespaceID: n.ID,
Registered: true,
RegisterMethod: "authKey",
IPAddress: ip.String(),
AuthKeyID: uint(pak.ID),
}
2021-07-04 21:56:13 +02:00
h.db.Save(&m)
2021-07-04 13:01:41 +02:00
2021-07-04 13:33:00 +02:00
err = h.LoadACLPolicy("./tests/acls/acl_policy_basic_namespace_as_user.hujson")
2021-07-04 13:01:41 +02:00
c.Assert(err, check.IsNil)
rules, err := h.generateACLRules()
c.Assert(err, check.IsNil)
c.Assert(rules, check.NotNil)
c.Assert(*rules, check.HasLen, 1)
c.Assert((*rules)[0].DstPorts, check.HasLen, 1)
c.Assert((*rules)[0].DstPorts[0].Ports.First, check.Equals, uint16(0))
c.Assert((*rules)[0].DstPorts[0].Ports.Last, check.Equals, uint16(65535))
c.Assert((*rules)[0].SrcIPs, check.HasLen, 1)
c.Assert((*rules)[0].SrcIPs[0], check.Not(check.Equals), "not an ip")
c.Assert((*rules)[0].SrcIPs[0], check.Equals, ip.String())
2021-07-03 11:55:32 +02:00
}
2021-07-04 13:23:31 +02:00
func (s *Suite) TestPortGroup(c *check.C) {
n, err := h.CreateNamespace("testnamespace")
c.Assert(err, check.IsNil)
pak, err := h.CreatePreAuthKey(n.Name, false, false, nil)
c.Assert(err, check.IsNil)
_, err = h.GetMachine("testnamespace", "testmachine")
c.Assert(err, check.NotNil)
ip, _ := h.getAvailableIP()
m := Machine{
ID: 0,
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
Name: "testmachine",
NamespaceID: n.ID,
Registered: true,
RegisterMethod: "authKey",
IPAddress: ip.String(),
AuthKeyID: uint(pak.ID),
}
2021-07-04 21:56:13 +02:00
h.db.Save(&m)
2021-07-04 13:33:00 +02:00
err = h.LoadACLPolicy("./tests/acls/acl_policy_basic_groups.hujson")
2021-07-04 13:23:31 +02:00
c.Assert(err, check.IsNil)
rules, err := h.generateACLRules()
c.Assert(err, check.IsNil)
c.Assert(rules, check.NotNil)
2021-07-04 13:23:31 +02:00
c.Assert(*rules, check.HasLen, 1)
c.Assert((*rules)[0].DstPorts, check.HasLen, 1)
c.Assert((*rules)[0].DstPorts[0].Ports.First, check.Equals, uint16(0))
c.Assert((*rules)[0].DstPorts[0].Ports.Last, check.Equals, uint16(65535))
c.Assert((*rules)[0].SrcIPs, check.HasLen, 1)
c.Assert((*rules)[0].SrcIPs[0], check.Not(check.Equals), "not an ip")
c.Assert((*rules)[0].SrcIPs[0], check.Equals, ip.String())
}