2021-07-03 05:55:32 -04:00
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
2022-02-05 11:18:39 -05:00
|
|
|
"errors"
|
2022-02-07 10:12:05 -05:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2022-02-05 11:18:39 -05:00
|
|
|
|
2021-07-03 05:55:32 -04:00
|
|
|
"gopkg.in/check.v1"
|
2022-02-05 11:18:39 -05:00
|
|
|
"gorm.io/datatypes"
|
|
|
|
"inet.af/netaddr"
|
2022-02-07 10:12:05 -05:00
|
|
|
"tailscale.com/tailcfg"
|
2021-07-03 05:55:32 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Suite) TestWrongPath(c *check.C) {
|
2021-11-15 11:16:04 -05:00
|
|
|
err := app.LoadACLPolicy("asdfg")
|
2021-07-03 05:55:32 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestBrokenHuJson(c *check.C) {
|
2021-11-15 11:16:04 -05:00
|
|
|
err := app.LoadACLPolicy("./tests/acls/broken.hujson")
|
2021-07-03 05:55:32 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestInvalidPolicyHuson(c *check.C) {
|
2021-11-15 11:16:04 -05:00
|
|
|
err := app.LoadACLPolicy("./tests/acls/invalid.hujson")
|
2021-07-03 05:55:32 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
2021-11-15 11:33:16 -05:00
|
|
|
c.Assert(err, check.Equals, errEmptyPolicy)
|
2021-07-03 05:55:32 -04:00
|
|
|
}
|
|
|
|
|
2021-07-03 11:31:32 -04:00
|
|
|
func (s *Suite) TestParseHosts(c *check.C) {
|
2021-11-15 11:16:04 -05:00
|
|
|
var hosts Hosts
|
|
|
|
err := hosts.UnmarshalJSON(
|
2021-11-13 03:36:45 -05:00
|
|
|
[]byte(
|
|
|
|
`{"example-host-1": "100.100.100.100","example-host-2": "100.100.101.100/24"}`,
|
|
|
|
),
|
|
|
|
)
|
2021-11-15 11:16:04 -05:00
|
|
|
c.Assert(hosts, check.NotNil)
|
2021-07-03 05:55:32 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-07-03 11:31:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestParseInvalidCIDR(c *check.C) {
|
2021-11-15 11:16:04 -05:00
|
|
|
var hosts Hosts
|
|
|
|
err := hosts.UnmarshalJSON([]byte(`{"example-host-1": "100.100.100.100/42"}`))
|
|
|
|
c.Assert(hosts, check.IsNil)
|
2021-07-03 11:31:32 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestRuleInvalidGeneration(c *check.C) {
|
2021-11-15 11:16:04 -05:00
|
|
|
err := app.LoadACLPolicy("./tests/acls/acl_policy_invalid.hujson")
|
2021-07-03 11:31:32 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
}
|
|
|
|
|
2021-07-04 06:35:18 -04:00
|
|
|
func (s *Suite) TestBasicRule(c *check.C) {
|
2021-11-15 11:16:04 -05:00
|
|
|
err := app.LoadACLPolicy("./tests/acls/acl_policy_basic_1.hujson")
|
2021-07-03 11:31:32 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
rules, err := app.generateACLRules()
|
2021-07-03 11:31:32 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-07-04 07:01:41 -04:00
|
|
|
c.Assert(rules, check.NotNil)
|
|
|
|
}
|
|
|
|
|
2022-02-05 11:18:39 -05:00
|
|
|
func (s *Suite) TestInvalidAction(c *check.C) {
|
|
|
|
app.aclPolicy = &ACLPolicy{
|
|
|
|
ACLs: []ACL{
|
|
|
|
{Action: "invalidAction", Users: []string{"*"}, Ports: []string{"*:*"}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := app.UpdateACLRules()
|
|
|
|
c.Assert(errors.Is(err, errInvalidAction), check.Equals, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestInvalidGroupInGroup(c *check.C) {
|
|
|
|
// this ACL is wrong because the group in users sections doesn't exist
|
|
|
|
app.aclPolicy = &ACLPolicy{
|
2022-02-14 09:54:51 -05:00
|
|
|
Groups: Groups{
|
|
|
|
"group:test": []string{"foo"},
|
|
|
|
"group:error": []string{"foo", "group:test"},
|
|
|
|
},
|
2022-02-05 11:18:39 -05:00
|
|
|
ACLs: []ACL{
|
|
|
|
{Action: "accept", Users: []string{"group:error"}, Ports: []string{"*:*"}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := app.UpdateACLRules()
|
|
|
|
c.Assert(errors.Is(err, errInvalidGroup), check.Equals, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestInvalidTagOwners(c *check.C) {
|
|
|
|
// this ACL is wrong because no tagOwners own the requested tag for the server
|
|
|
|
app.aclPolicy = &ACLPolicy{
|
|
|
|
ACLs: []ACL{
|
|
|
|
{Action: "accept", Users: []string{"tag:foo"}, Ports: []string{"*:*"}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := app.UpdateACLRules()
|
|
|
|
c.Assert(errors.Is(err, errInvalidTag), check.Equals, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// this test should validate that we can expand a group in a TagOWner section and
|
|
|
|
// match properly the IP's of the related hosts. The owner is valid and the tag is also valid.
|
2022-02-14 09:26:54 -05:00
|
|
|
// the tag is matched in the Users section.
|
2022-02-05 11:18:39 -05:00
|
|
|
func (s *Suite) TestValidExpandTagOwnersInUsers(c *check.C) {
|
2022-02-20 17:00:31 -05:00
|
|
|
namespace, err := app.CreateNamespace("user1")
|
2022-02-05 11:18:39 -05:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-02-20 17:00:31 -05:00
|
|
|
_, err = app.GetMachine("user1", "testmachine")
|
2022-02-05 11:18:39 -05:00
|
|
|
c.Assert(err, check.NotNil)
|
2022-02-14 09:54:51 -05:00
|
|
|
hostInfo := []byte(
|
2022-02-20 17:00:31 -05:00
|
|
|
"{\"OS\":\"centos\",\"Hostname\":\"testmachine\",\"RequestTags\":[\"tag:test\"]}",
|
2022-02-14 09:54:51 -05:00
|
|
|
)
|
2022-02-05 11:18:39 -05:00
|
|
|
machine := Machine{
|
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
|
|
|
Name: "testmachine",
|
|
|
|
IPAddresses: MachineAddresses{netaddr.MustParseIP("100.64.0.1")},
|
|
|
|
NamespaceID: namespace.ID,
|
|
|
|
Registered: true,
|
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
2022-02-14 09:26:54 -05:00
|
|
|
HostInfo: datatypes.JSON(hostInfo),
|
2022-02-05 11:18:39 -05:00
|
|
|
}
|
|
|
|
app.db.Save(&machine)
|
|
|
|
|
|
|
|
app.aclPolicy = &ACLPolicy{
|
2022-02-20 17:00:31 -05:00
|
|
|
Groups: Groups{"group:test": []string{"user1", "user2"}},
|
|
|
|
TagOwners: TagOwners{"tag:test": []string{"user3", "group:test"}},
|
2022-02-05 11:18:39 -05:00
|
|
|
ACLs: []ACL{
|
|
|
|
{Action: "accept", Users: []string{"tag:test"}, Ports: []string{"*:*"}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = app.UpdateACLRules()
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(app.aclRules, check.HasLen, 1)
|
|
|
|
c.Assert(app.aclRules[0].SrcIPs, check.HasLen, 1)
|
|
|
|
c.Assert(app.aclRules[0].SrcIPs[0], check.Equals, "100.64.0.1")
|
|
|
|
}
|
|
|
|
|
|
|
|
// this test should validate that we can expand a group in a TagOWner section and
|
|
|
|
// match properly the IP's of the related hosts. The owner is valid and the tag is also valid.
|
2022-02-14 09:26:54 -05:00
|
|
|
// the tag is matched in the Ports section.
|
2022-02-05 11:18:39 -05:00
|
|
|
func (s *Suite) TestValidExpandTagOwnersInPorts(c *check.C) {
|
2022-02-20 17:00:31 -05:00
|
|
|
namespace, err := app.CreateNamespace("user1")
|
2022-02-05 11:18:39 -05:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-02-20 17:00:31 -05:00
|
|
|
_, err = app.GetMachine("user1", "testmachine")
|
2022-02-05 11:18:39 -05:00
|
|
|
c.Assert(err, check.NotNil)
|
2022-02-14 09:54:51 -05:00
|
|
|
hostInfo := []byte(
|
2022-02-20 17:00:31 -05:00
|
|
|
"{\"OS\":\"centos\",\"Hostname\":\"testmachine\",\"RequestTags\":[\"tag:test\"]}",
|
2022-02-14 09:54:51 -05:00
|
|
|
)
|
2022-02-05 11:18:39 -05:00
|
|
|
machine := Machine{
|
|
|
|
ID: 1,
|
2022-02-20 17:00:31 -05:00
|
|
|
MachineKey: "12345",
|
2022-02-05 11:18:39 -05:00
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
|
|
|
Name: "testmachine",
|
|
|
|
IPAddresses: MachineAddresses{netaddr.MustParseIP("100.64.0.1")},
|
|
|
|
NamespaceID: namespace.ID,
|
|
|
|
Registered: true,
|
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
2022-02-14 09:26:54 -05:00
|
|
|
HostInfo: datatypes.JSON(hostInfo),
|
2022-02-05 11:18:39 -05:00
|
|
|
}
|
|
|
|
app.db.Save(&machine)
|
|
|
|
|
|
|
|
app.aclPolicy = &ACLPolicy{
|
2022-02-20 17:00:31 -05:00
|
|
|
Groups: Groups{"group:test": []string{"user1", "user2"}},
|
|
|
|
TagOwners: TagOwners{"tag:test": []string{"user3", "group:test"}},
|
2022-02-05 11:18:39 -05:00
|
|
|
ACLs: []ACL{
|
|
|
|
{Action: "accept", Users: []string{"*"}, Ports: []string{"tag:test:*"}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = app.UpdateACLRules()
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(app.aclRules, check.HasLen, 1)
|
|
|
|
c.Assert(app.aclRules[0].DstPorts, check.HasLen, 1)
|
|
|
|
c.Assert(app.aclRules[0].DstPorts[0].IP, check.Equals, "100.64.0.1")
|
|
|
|
}
|
|
|
|
|
|
|
|
// need a test with:
|
|
|
|
// tag on a host that isn't owned by a tag owners. So the namespace
|
2022-02-14 09:26:54 -05:00
|
|
|
// of the host should be valid.
|
2022-02-05 11:18:39 -05:00
|
|
|
func (s *Suite) TestInvalidTagValidNamespace(c *check.C) {
|
2022-02-20 17:00:31 -05:00
|
|
|
namespace, err := app.CreateNamespace("user1")
|
2022-02-05 11:18:39 -05:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-02-20 17:00:31 -05:00
|
|
|
_, err = app.GetMachine("user1", "testmachine")
|
2022-02-05 11:18:39 -05:00
|
|
|
c.Assert(err, check.NotNil)
|
2022-02-14 09:54:51 -05:00
|
|
|
hostInfo := []byte(
|
2022-02-20 17:00:31 -05:00
|
|
|
"{\"OS\":\"centos\",\"Hostname\":\"testmachine\",\"RequestTags\":[\"tag:foo\"]}",
|
2022-02-14 09:54:51 -05:00
|
|
|
)
|
2022-02-05 11:18:39 -05:00
|
|
|
machine := Machine{
|
|
|
|
ID: 1,
|
2022-02-20 17:00:31 -05:00
|
|
|
MachineKey: "12345",
|
2022-02-05 11:18:39 -05:00
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
|
|
|
Name: "testmachine",
|
|
|
|
IPAddresses: MachineAddresses{netaddr.MustParseIP("100.64.0.1")},
|
|
|
|
NamespaceID: namespace.ID,
|
|
|
|
Registered: true,
|
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
2022-02-14 09:26:54 -05:00
|
|
|
HostInfo: datatypes.JSON(hostInfo),
|
2022-02-05 11:18:39 -05:00
|
|
|
}
|
|
|
|
app.db.Save(&machine)
|
|
|
|
|
|
|
|
app.aclPolicy = &ACLPolicy{
|
2022-02-20 17:00:31 -05:00
|
|
|
TagOwners: TagOwners{"tag:test": []string{"user1"}},
|
2022-02-05 11:18:39 -05:00
|
|
|
ACLs: []ACL{
|
2022-02-20 17:00:31 -05:00
|
|
|
{Action: "accept", Users: []string{"user1"}, Ports: []string{"*:*"}},
|
2022-02-05 11:18:39 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
err = app.UpdateACLRules()
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(app.aclRules, check.HasLen, 1)
|
|
|
|
c.Assert(app.aclRules[0].SrcIPs, check.HasLen, 1)
|
|
|
|
c.Assert(app.aclRules[0].SrcIPs[0], check.Equals, "100.64.0.1")
|
|
|
|
}
|
|
|
|
|
|
|
|
// tag on a host is owned by a tag owner, the tag is valid.
|
|
|
|
// an ACL rule is matching the tag to a namespace. It should not be valid since the
|
|
|
|
// host should be tied to the tag now.
|
|
|
|
func (s *Suite) TestValidTagInvalidNamespace(c *check.C) {
|
2022-02-20 17:00:31 -05:00
|
|
|
namespace, err := app.CreateNamespace("user1")
|
2022-02-05 11:18:39 -05:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-02-20 17:00:31 -05:00
|
|
|
_, err = app.GetMachine("user1", "webserver")
|
2022-02-05 11:18:39 -05:00
|
|
|
c.Assert(err, check.NotNil)
|
2022-02-14 09:54:51 -05:00
|
|
|
hostInfo := []byte(
|
|
|
|
"{\"OS\":\"centos\",\"Hostname\":\"webserver\",\"RequestTags\":[\"tag:webapp\"]}",
|
|
|
|
)
|
2022-02-05 11:18:39 -05:00
|
|
|
machine := Machine{
|
|
|
|
ID: 1,
|
2022-02-20 17:00:31 -05:00
|
|
|
MachineKey: "12345",
|
2022-02-05 11:18:39 -05:00
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
|
|
|
Name: "webserver",
|
|
|
|
IPAddresses: MachineAddresses{netaddr.MustParseIP("100.64.0.1")},
|
|
|
|
NamespaceID: namespace.ID,
|
|
|
|
Registered: true,
|
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
2022-02-14 09:26:54 -05:00
|
|
|
HostInfo: datatypes.JSON(hostInfo),
|
2022-02-05 11:18:39 -05:00
|
|
|
}
|
|
|
|
app.db.Save(&machine)
|
2022-02-20 17:00:31 -05:00
|
|
|
_, err = app.GetMachine("user1", "user")
|
2022-02-14 09:26:54 -05:00
|
|
|
hostInfo = []byte("{\"OS\":\"debian\",\"Hostname\":\"user\"}")
|
2022-02-05 11:18:39 -05:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
machine = Machine{
|
|
|
|
ID: 2,
|
2022-02-20 17:00:31 -05:00
|
|
|
MachineKey: "56789",
|
2022-02-05 11:18:39 -05:00
|
|
|
NodeKey: "bar2",
|
|
|
|
DiscoKey: "faab",
|
|
|
|
Name: "user",
|
|
|
|
IPAddresses: MachineAddresses{netaddr.MustParseIP("100.64.0.2")},
|
|
|
|
NamespaceID: namespace.ID,
|
|
|
|
Registered: true,
|
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
2022-02-14 09:26:54 -05:00
|
|
|
HostInfo: datatypes.JSON(hostInfo),
|
2022-02-05 11:18:39 -05:00
|
|
|
}
|
|
|
|
app.db.Save(&machine)
|
|
|
|
|
|
|
|
app.aclPolicy = &ACLPolicy{
|
2022-02-20 17:00:31 -05:00
|
|
|
TagOwners: TagOwners{"tag:webapp": []string{"user1"}},
|
2022-02-05 11:18:39 -05:00
|
|
|
ACLs: []ACL{
|
2022-02-14 09:54:51 -05:00
|
|
|
{
|
|
|
|
Action: "accept",
|
2022-02-20 17:00:31 -05:00
|
|
|
Users: []string{"user1"},
|
2022-02-14 09:54:51 -05:00
|
|
|
Ports: []string{"tag:webapp:80,443"},
|
|
|
|
},
|
2022-02-05 11:18:39 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
err = app.UpdateACLRules()
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(app.aclRules, check.HasLen, 1)
|
2022-02-07 10:12:05 -05:00
|
|
|
c.Assert(app.aclRules[0].SrcIPs, check.HasLen, 1)
|
|
|
|
c.Assert(app.aclRules[0].SrcIPs[0], check.Equals, "100.64.0.2")
|
|
|
|
c.Assert(app.aclRules[0].DstPorts, check.HasLen, 2)
|
|
|
|
c.Assert(app.aclRules[0].DstPorts[0].Ports.First, check.Equals, uint16(80))
|
|
|
|
c.Assert(app.aclRules[0].DstPorts[0].Ports.Last, check.Equals, uint16(80))
|
|
|
|
c.Assert(app.aclRules[0].DstPorts[0].IP, check.Equals, "100.64.0.1")
|
|
|
|
c.Assert(app.aclRules[0].DstPorts[1].Ports.First, check.Equals, uint16(443))
|
|
|
|
c.Assert(app.aclRules[0].DstPorts[1].Ports.Last, check.Equals, uint16(443))
|
|
|
|
c.Assert(app.aclRules[0].DstPorts[1].IP, check.Equals, "100.64.0.1")
|
2022-02-05 11:18:39 -05:00
|
|
|
}
|
|
|
|
|
2021-07-04 07:01:41 -04:00
|
|
|
func (s *Suite) TestPortRange(c *check.C) {
|
2021-11-15 11:16:04 -05:00
|
|
|
err := app.LoadACLPolicy("./tests/acls/acl_policy_basic_range.hujson")
|
2021-07-04 07:01:41 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
rules, err := app.generateACLRules()
|
2021-07-04 07:01:41 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(rules, check.NotNil)
|
|
|
|
|
2021-11-04 18:16:56 -04:00
|
|
|
c.Assert(rules, check.HasLen, 1)
|
2022-01-16 08:16:59 -05:00
|
|
|
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))
|
2021-07-04 07:01:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestPortWildcard(c *check.C) {
|
2021-11-15 11:16:04 -05:00
|
|
|
err := app.LoadACLPolicy("./tests/acls/acl_policy_basic_wildcards.hujson")
|
2021-07-04 07:01:41 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
rules, err := app.generateACLRules()
|
2021-07-04 07:01:41 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(rules, check.NotNil)
|
|
|
|
|
2021-11-04 18:16:56 -04:00
|
|
|
c.Assert(rules, check.HasLen, 1)
|
2022-01-16 08:16:59 -05:00
|
|
|
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, "*")
|
2021-07-04 07:01:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestPortNamespace(c *check.C) {
|
2021-11-15 11:16:04 -05:00
|
|
|
namespace, err := app.CreateNamespace("testnamespace")
|
2021-07-04 07:01:41 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
|
2021-07-04 07:01:41 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
_, err = app.GetMachine("testnamespace", "testmachine")
|
2021-07-04 07:01:41 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
2022-01-16 08:16:59 -05:00
|
|
|
ips, _ := app.getAvailableIPs()
|
2021-11-15 11:16:04 -05:00
|
|
|
machine := Machine{
|
2021-07-04 07:01:41 -04:00
|
|
|
ID: 0,
|
2022-02-20 17:00:31 -05:00
|
|
|
MachineKey: "12345",
|
2021-07-04 07:01:41 -04:00
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
|
|
|
Name: "testmachine",
|
2021-11-15 11:16:04 -05:00
|
|
|
NamespaceID: namespace.ID,
|
2021-07-04 07:01:41 -04:00
|
|
|
Registered: true,
|
2021-11-18 03:49:55 -05:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2022-01-16 08:16:59 -05:00
|
|
|
IPAddresses: ips,
|
2021-07-04 07:01:41 -04:00
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
2021-11-15 11:16:04 -05:00
|
|
|
app.db.Save(&machine)
|
2021-07-04 07:01:41 -04:00
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
err = app.LoadACLPolicy(
|
|
|
|
"./tests/acls/acl_policy_basic_namespace_as_user.hujson",
|
|
|
|
)
|
2021-07-04 07:01:41 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
rules, err := app.generateACLRules()
|
2021-07-04 07:01:41 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(rules, check.NotNil)
|
|
|
|
|
2021-11-04 18:16:56 -04:00
|
|
|
c.Assert(rules, check.HasLen, 1)
|
2022-01-16 08:16:59 -05:00
|
|
|
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(len(ips), check.Equals, 1)
|
|
|
|
c.Assert(rules[0].SrcIPs[0], check.Equals, ips[0].String())
|
2021-07-03 05:55:32 -04:00
|
|
|
}
|
2021-07-04 06:35:18 -04:00
|
|
|
|
2021-07-04 07:23:31 -04:00
|
|
|
func (s *Suite) TestPortGroup(c *check.C) {
|
2021-11-15 11:16:04 -05:00
|
|
|
namespace, err := app.CreateNamespace("testnamespace")
|
2021-07-04 07:23:31 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
|
2021-07-04 07:23:31 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
_, err = app.GetMachine("testnamespace", "testmachine")
|
2021-07-04 07:23:31 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
2022-01-16 08:16:59 -05:00
|
|
|
ips, _ := app.getAvailableIPs()
|
2021-11-15 11:16:04 -05:00
|
|
|
machine := Machine{
|
2021-07-04 07:23:31 -04:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
|
|
|
Name: "testmachine",
|
2021-11-15 11:16:04 -05:00
|
|
|
NamespaceID: namespace.ID,
|
2021-07-04 07:23:31 -04:00
|
|
|
Registered: true,
|
2021-11-18 03:49:55 -05:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2022-01-16 08:16:59 -05:00
|
|
|
IPAddresses: ips,
|
2021-07-04 07:23:31 -04:00
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
2021-11-15 11:16:04 -05:00
|
|
|
app.db.Save(&machine)
|
2021-07-04 06:35:18 -04:00
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
err = app.LoadACLPolicy("./tests/acls/acl_policy_basic_groups.hujson")
|
2021-07-04 07:23:31 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
rules, err := app.generateACLRules()
|
2021-07-04 07:23:31 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(rules, check.NotNil)
|
2021-07-04 06:35:18 -04:00
|
|
|
|
2021-11-04 18:16:56 -04:00
|
|
|
c.Assert(rules, check.HasLen, 1)
|
2022-01-16 08:16:59 -05:00
|
|
|
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(len(ips), check.Equals, 1)
|
|
|
|
c.Assert(rules[0].SrcIPs[0], check.Equals, ips[0].String())
|
2021-07-04 07:23:31 -04:00
|
|
|
}
|
2022-02-07 10:12:05 -05:00
|
|
|
|
|
|
|
func Test_expandGroup(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
aclPolicy ACLPolicy
|
|
|
|
group string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want []string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "simple test",
|
|
|
|
args: args{
|
|
|
|
aclPolicy: ACLPolicy{
|
2022-02-14 09:54:51 -05:00
|
|
|
Groups: Groups{
|
2022-02-20 17:00:31 -05:00
|
|
|
"group:test": []string{"user1", "user2", "user3"},
|
|
|
|
"group:foo": []string{"user2", "user3"},
|
2022-02-14 09:54:51 -05:00
|
|
|
},
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
|
|
|
group: "group:test",
|
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
want: []string{"user1", "user2", "user3"},
|
2022-02-07 10:12:05 -05:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "InexistantGroup",
|
|
|
|
args: args{
|
|
|
|
aclPolicy: ACLPolicy{
|
2022-02-14 09:54:51 -05:00
|
|
|
Groups: Groups{
|
2022-02-20 17:00:31 -05:00
|
|
|
"group:test": []string{"user1", "user2", "user3"},
|
|
|
|
"group:foo": []string{"user2", "user3"},
|
2022-02-14 09:54:51 -05:00
|
|
|
},
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
group: "group:undefined",
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
|
|
|
want: []string{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
2022-02-14 09:26:54 -05:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
got, err := expandGroup(test.args.aclPolicy, test.args.group)
|
|
|
|
if (err != nil) != test.wantErr {
|
|
|
|
t.Errorf("expandGroup() error = %v, wantErr %v", err, test.wantErr)
|
|
|
|
|
2022-02-07 10:12:05 -05:00
|
|
|
return
|
|
|
|
}
|
2022-02-14 09:26:54 -05:00
|
|
|
if !reflect.DeepEqual(got, test.want) {
|
|
|
|
t.Errorf("expandGroup() = %v, want %v", got, test.want)
|
2022-02-07 10:12:05 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_expandTagOwners(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
aclPolicy ACLPolicy
|
|
|
|
tag string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want []string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
2022-02-20 17:00:31 -05:00
|
|
|
name: "simple tag expansion",
|
2022-02-07 10:12:05 -05:00
|
|
|
args: args{
|
|
|
|
aclPolicy: ACLPolicy{
|
2022-02-20 17:00:31 -05:00
|
|
|
TagOwners: TagOwners{"tag:test": []string{"user1"}},
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
|
|
|
tag: "tag:test",
|
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
want: []string{"user1"},
|
2022-02-07 10:12:05 -05:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
2022-02-20 17:00:31 -05:00
|
|
|
name: "expand with tag and group",
|
2022-02-07 10:12:05 -05:00
|
|
|
args: args{
|
|
|
|
aclPolicy: ACLPolicy{
|
2022-02-20 17:00:31 -05:00
|
|
|
Groups: Groups{"group:foo": []string{"user1", "user2"}},
|
2022-02-07 10:12:05 -05:00
|
|
|
TagOwners: TagOwners{"tag:test": []string{"group:foo"}},
|
|
|
|
},
|
|
|
|
tag: "tag:test",
|
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
want: []string{"user1", "user2"},
|
2022-02-07 10:12:05 -05:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
2022-02-20 17:00:31 -05:00
|
|
|
name: "expand with namespace and group",
|
2022-02-07 10:12:05 -05:00
|
|
|
args: args{
|
|
|
|
aclPolicy: ACLPolicy{
|
2022-02-20 17:00:31 -05:00
|
|
|
Groups: Groups{"group:foo": []string{"user1", "user2"}},
|
|
|
|
TagOwners: TagOwners{"tag:test": []string{"group:foo", "user3"}},
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
|
|
|
tag: "tag:test",
|
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
want: []string{"user1", "user2", "user3"},
|
2022-02-07 10:12:05 -05:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid tag",
|
|
|
|
args: args{
|
|
|
|
aclPolicy: ACLPolicy{
|
2022-02-20 17:00:31 -05:00
|
|
|
TagOwners: TagOwners{"tag:foo": []string{"group:foo", "user1"}},
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
|
|
|
tag: "tag:test",
|
|
|
|
},
|
|
|
|
want: []string{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid group",
|
|
|
|
args: args{
|
|
|
|
aclPolicy: ACLPolicy{
|
2022-02-20 17:00:31 -05:00
|
|
|
Groups: Groups{"group:bar": []string{"user1", "user2"}},
|
|
|
|
TagOwners: TagOwners{"tag:test": []string{"group:foo", "user2"}},
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
|
|
|
tag: "tag:test",
|
|
|
|
},
|
|
|
|
want: []string{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
2022-02-14 09:26:54 -05:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
got, err := expandTagOwners(test.args.aclPolicy, test.args.tag)
|
|
|
|
if (err != nil) != test.wantErr {
|
|
|
|
t.Errorf("expandTagOwners() error = %v, wantErr %v", err, test.wantErr)
|
|
|
|
|
2022-02-07 10:12:05 -05:00
|
|
|
return
|
|
|
|
}
|
2022-02-14 09:26:54 -05:00
|
|
|
if !reflect.DeepEqual(got, test.want) {
|
|
|
|
t.Errorf("expandTagOwners() = %v, want %v", got, test.want)
|
2022-02-07 10:12:05 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_expandPorts(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
portsStr string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *[]tailcfg.PortRange
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "wildcard",
|
|
|
|
args: args{portsStr: "*"},
|
|
|
|
want: &[]tailcfg.PortRange{
|
|
|
|
{First: portRangeBegin, Last: portRangeEnd},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "two ports",
|
|
|
|
args: args{portsStr: "80,443"},
|
|
|
|
want: &[]tailcfg.PortRange{
|
|
|
|
{First: 80, Last: 80},
|
|
|
|
{First: 443, Last: 443},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "a range and a port",
|
|
|
|
args: args{portsStr: "80-1024,443"},
|
|
|
|
want: &[]tailcfg.PortRange{
|
|
|
|
{First: 80, Last: 1024},
|
|
|
|
{First: 443, Last: 443},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "out of bounds",
|
|
|
|
args: args{portsStr: "854038"},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wrong port",
|
|
|
|
args: args{portsStr: "85a38"},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wrong port in first",
|
|
|
|
args: args{portsStr: "a-80"},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wrong port in last",
|
|
|
|
args: args{portsStr: "80-85a38"},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wrong port format",
|
|
|
|
args: args{portsStr: "80-85a38-3"},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
2022-02-14 09:26:54 -05:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
got, err := expandPorts(test.args.portsStr)
|
|
|
|
if (err != nil) != test.wantErr {
|
|
|
|
t.Errorf("expandPorts() error = %v, wantErr %v", err, test.wantErr)
|
|
|
|
|
2022-02-07 10:12:05 -05:00
|
|
|
return
|
|
|
|
}
|
2022-02-14 09:26:54 -05:00
|
|
|
if !reflect.DeepEqual(got, test.want) {
|
|
|
|
t.Errorf("expandPorts() = %v, want %v", got, test.want)
|
2022-02-07 10:12:05 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_listMachinesInNamespace(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
machines []Machine
|
|
|
|
namespace string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want []Machine
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "1 machine in namespace",
|
|
|
|
args: args{
|
|
|
|
machines: []Machine{
|
2022-02-20 17:00:31 -05:00
|
|
|
{Namespace: Namespace{Name: "joe"}},
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
namespace: "joe",
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
|
|
|
want: []Machine{
|
2022-02-20 17:00:31 -05:00
|
|
|
{Namespace: Namespace{Name: "joe"}},
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "3 machines, 2 in namespace",
|
|
|
|
args: args{
|
|
|
|
machines: []Machine{
|
2022-02-20 17:00:31 -05:00
|
|
|
{ID: 1, Namespace: Namespace{Name: "joe"}},
|
|
|
|
{ID: 2, Namespace: Namespace{Name: "marc"}},
|
|
|
|
{ID: 3, Namespace: Namespace{Name: "marc"}},
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
namespace: "marc",
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
|
|
|
want: []Machine{
|
2022-02-20 17:00:31 -05:00
|
|
|
{ID: 2, Namespace: Namespace{Name: "marc"}},
|
|
|
|
{ID: 3, Namespace: Namespace{Name: "marc"}},
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "5 machines, 0 in namespace",
|
|
|
|
args: args{
|
|
|
|
machines: []Machine{
|
2022-02-20 17:00:31 -05:00
|
|
|
{ID: 1, Namespace: Namespace{Name: "joe"}},
|
|
|
|
{ID: 2, Namespace: Namespace{Name: "marc"}},
|
|
|
|
{ID: 3, Namespace: Namespace{Name: "marc"}},
|
|
|
|
{ID: 4, Namespace: Namespace{Name: "marc"}},
|
|
|
|
{ID: 5, Namespace: Namespace{Name: "marc"}},
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
namespace: "mickael",
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
|
|
|
want: []Machine{},
|
|
|
|
},
|
|
|
|
}
|
2022-02-14 12:24:37 -05:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2022-02-20 15:24:02 -05:00
|
|
|
if got := filterMachinesByNamespace(test.args.machines, test.args.namespace); !reflect.DeepEqual(
|
2022-02-14 09:54:51 -05:00
|
|
|
got,
|
2022-02-14 12:24:37 -05:00
|
|
|
test.want,
|
2022-02-14 09:54:51 -05:00
|
|
|
) {
|
2022-02-14 12:24:37 -05:00
|
|
|
t.Errorf("listMachinesInNamespace() = %v, want %v", got, test.want)
|
2022-02-07 10:12:05 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 12:32:20 -05:00
|
|
|
// nolint
|
2022-02-07 10:12:05 -05:00
|
|
|
func Test_expandAlias(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
machines []Machine
|
|
|
|
aclPolicy ACLPolicy
|
|
|
|
alias string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want []string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "wildcard",
|
|
|
|
args: args{
|
|
|
|
alias: "*",
|
|
|
|
machines: []Machine{
|
|
|
|
{IPAddresses: MachineAddresses{netaddr.MustParseIP("100.64.0.1")}},
|
2022-02-14 09:54:51 -05:00
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.78.84.227"),
|
|
|
|
},
|
|
|
|
},
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
|
|
|
aclPolicy: ACLPolicy{},
|
|
|
|
},
|
|
|
|
want: []string{"*"},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple group",
|
|
|
|
args: args{
|
2022-02-20 17:00:31 -05:00
|
|
|
alias: "group:accountant",
|
2022-02-07 10:12:05 -05:00
|
|
|
machines: []Machine{
|
2022-02-14 09:54:51 -05:00
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.1"),
|
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
Namespace: Namespace{Name: "joe"},
|
2022-02-14 09:54:51 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.2"),
|
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
Namespace: Namespace{Name: "joe"},
|
2022-02-14 09:54:51 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.3"),
|
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
Namespace: Namespace{Name: "marc"},
|
2022-02-14 09:54:51 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.4"),
|
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
Namespace: Namespace{Name: "mickael"},
|
2022-02-14 09:54:51 -05:00
|
|
|
},
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
|
|
|
aclPolicy: ACLPolicy{
|
2022-02-20 17:00:31 -05:00
|
|
|
Groups: Groups{"group:accountant": []string{"joe", "marc"}},
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []string{"100.64.0.1", "100.64.0.2", "100.64.0.3"},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wrong group",
|
|
|
|
args: args{
|
2022-02-20 17:00:31 -05:00
|
|
|
alias: "group:hr",
|
2022-02-07 10:12:05 -05:00
|
|
|
machines: []Machine{
|
2022-02-14 09:54:51 -05:00
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.1"),
|
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
Namespace: Namespace{Name: "joe"},
|
2022-02-14 09:54:51 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.2"),
|
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
Namespace: Namespace{Name: "joe"},
|
2022-02-14 09:54:51 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.3"),
|
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
Namespace: Namespace{Name: "marc"},
|
2022-02-14 09:54:51 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.4"),
|
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
Namespace: Namespace{Name: "mickael"},
|
2022-02-14 09:54:51 -05:00
|
|
|
},
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
|
|
|
aclPolicy: ACLPolicy{
|
2022-02-20 17:00:31 -05:00
|
|
|
Groups: Groups{"group:accountant": []string{"joe", "marc"}},
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []string{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple ipaddress",
|
|
|
|
args: args{
|
|
|
|
alias: "10.0.0.3",
|
|
|
|
machines: []Machine{},
|
|
|
|
aclPolicy: ACLPolicy{},
|
|
|
|
},
|
|
|
|
want: []string{"10.0.0.3"},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "private network",
|
|
|
|
args: args{
|
|
|
|
alias: "homeNetwork",
|
|
|
|
machines: []Machine{},
|
|
|
|
aclPolicy: ACLPolicy{
|
2022-02-14 09:54:51 -05:00
|
|
|
Hosts: Hosts{
|
|
|
|
"homeNetwork": netaddr.MustParseIPPrefix("192.168.1.0/24"),
|
|
|
|
},
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []string{"192.168.1.0/24"},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple host",
|
|
|
|
args: args{
|
|
|
|
alias: "10.0.0.1",
|
|
|
|
machines: []Machine{},
|
|
|
|
aclPolicy: ACLPolicy{},
|
|
|
|
},
|
|
|
|
want: []string{"10.0.0.1"},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple CIDR",
|
|
|
|
args: args{
|
|
|
|
alias: "10.0.0.0/16",
|
|
|
|
machines: []Machine{},
|
|
|
|
aclPolicy: ACLPolicy{},
|
|
|
|
},
|
|
|
|
want: []string{"10.0.0.0/16"},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple tag",
|
|
|
|
args: args{
|
2022-02-20 17:00:31 -05:00
|
|
|
alias: "tag:hr-webserver",
|
2022-02-07 10:12:05 -05:00
|
|
|
machines: []Machine{
|
2022-02-14 09:54:51 -05:00
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.1"),
|
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
Namespace: Namespace{Name: "joe"},
|
2022-02-14 09:54:51 -05:00
|
|
|
HostInfo: []byte(
|
2022-02-20 17:00:31 -05:00
|
|
|
"{\"OS\":\"centos\",\"Hostname\":\"foo\",\"RequestTags\":[\"tag:hr-webserver\"]}",
|
2022-02-14 09:54:51 -05:00
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.2"),
|
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
Namespace: Namespace{Name: "joe"},
|
2022-02-14 09:54:51 -05:00
|
|
|
HostInfo: []byte(
|
2022-02-20 17:00:31 -05:00
|
|
|
"{\"OS\":\"centos\",\"Hostname\":\"foo\",\"RequestTags\":[\"tag:hr-webserver\"]}",
|
2022-02-14 09:54:51 -05:00
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.3"),
|
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
Namespace: Namespace{Name: "marc"},
|
2022-02-14 09:54:51 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.4"),
|
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
Namespace: Namespace{Name: "joe"},
|
2022-02-14 09:54:51 -05:00
|
|
|
},
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
|
|
|
aclPolicy: ACLPolicy{
|
2022-02-20 17:00:31 -05:00
|
|
|
TagOwners: TagOwners{"tag:hr-webserver": []string{"joe"}},
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []string{"100.64.0.1", "100.64.0.2"},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "No tag defined",
|
|
|
|
args: args{
|
2022-02-20 17:00:31 -05:00
|
|
|
alias: "tag:hr-webserver",
|
2022-02-07 10:12:05 -05:00
|
|
|
machines: []Machine{
|
2022-02-14 09:54:51 -05:00
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.1"),
|
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
Namespace: Namespace{Name: "joe"},
|
2022-02-14 09:54:51 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.2"),
|
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
Namespace: Namespace{Name: "joe"},
|
2022-02-14 09:54:51 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.3"),
|
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
Namespace: Namespace{Name: "marc"},
|
2022-02-14 09:54:51 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.4"),
|
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
Namespace: Namespace{Name: "mickael"},
|
2022-02-14 09:54:51 -05:00
|
|
|
},
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
|
|
|
aclPolicy: ACLPolicy{
|
2022-02-20 17:00:31 -05:00
|
|
|
Groups: Groups{"group:accountant": []string{"joe", "marc"}},
|
|
|
|
TagOwners: TagOwners{"tag:accountant-webserver": []string{"group:accountant"}},
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []string{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "list host in namespace without correctly tagged servers",
|
|
|
|
args: args{
|
2022-02-20 17:00:31 -05:00
|
|
|
alias: "joe",
|
2022-02-07 10:12:05 -05:00
|
|
|
machines: []Machine{
|
2022-02-14 09:54:51 -05:00
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.1"),
|
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
Namespace: Namespace{Name: "joe"},
|
2022-02-14 09:54:51 -05:00
|
|
|
HostInfo: []byte(
|
2022-02-20 17:00:31 -05:00
|
|
|
"{\"OS\":\"centos\",\"Hostname\":\"foo\",\"RequestTags\":[\"tag:accountant-webserver\"]}",
|
2022-02-14 09:54:51 -05:00
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.2"),
|
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
Namespace: Namespace{Name: "joe"},
|
2022-02-14 09:54:51 -05:00
|
|
|
HostInfo: []byte(
|
2022-02-20 17:00:31 -05:00
|
|
|
"{\"OS\":\"centos\",\"Hostname\":\"foo\",\"RequestTags\":[\"tag:accountant-webserver\"]}",
|
2022-02-14 09:54:51 -05:00
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.3"),
|
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
Namespace: Namespace{Name: "marc"},
|
2022-02-14 09:54:51 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.4"),
|
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
Namespace: Namespace{Name: "joe"},
|
2022-02-14 09:54:51 -05:00
|
|
|
},
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
|
|
|
aclPolicy: ACLPolicy{
|
2022-02-20 17:00:31 -05:00
|
|
|
TagOwners: TagOwners{"tag:accountant-webserver": []string{"joe"}},
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []string{"100.64.0.4"},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
2022-02-14 09:26:54 -05:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2022-02-14 09:54:51 -05:00
|
|
|
got, err := expandAlias(
|
|
|
|
test.args.machines,
|
|
|
|
test.args.aclPolicy,
|
|
|
|
test.args.alias,
|
|
|
|
)
|
2022-02-14 09:26:54 -05:00
|
|
|
if (err != nil) != test.wantErr {
|
|
|
|
t.Errorf("expandAlias() error = %v, wantErr %v", err, test.wantErr)
|
|
|
|
|
2022-02-07 10:12:05 -05:00
|
|
|
return
|
|
|
|
}
|
2022-02-14 09:26:54 -05:00
|
|
|
if !reflect.DeepEqual(got, test.want) {
|
|
|
|
t.Errorf("expandAlias() = %v, want %v", got, test.want)
|
2022-02-07 10:12:05 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_excludeCorrectlyTaggedNodes(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
aclPolicy ACLPolicy
|
|
|
|
nodes []Machine
|
|
|
|
namespace string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want []Machine
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "exclude nodes with valid tags",
|
|
|
|
args: args{
|
|
|
|
aclPolicy: ACLPolicy{
|
2022-02-20 17:00:31 -05:00
|
|
|
TagOwners: TagOwners{"tag:accountant-webserver": []string{"joe"}},
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
|
|
|
nodes: []Machine{
|
2022-02-14 09:54:51 -05:00
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.1"),
|
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
Namespace: Namespace{Name: "joe"},
|
2022-02-14 09:54:51 -05:00
|
|
|
HostInfo: []byte(
|
2022-02-20 17:00:31 -05:00
|
|
|
"{\"OS\":\"centos\",\"Hostname\":\"foo\",\"RequestTags\":[\"tag:accountant-webserver\"]}",
|
2022-02-14 09:54:51 -05:00
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.2"),
|
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
Namespace: Namespace{Name: "joe"},
|
2022-02-14 09:54:51 -05:00
|
|
|
HostInfo: []byte(
|
2022-02-20 17:00:31 -05:00
|
|
|
"{\"OS\":\"centos\",\"Hostname\":\"foo\",\"RequestTags\":[\"tag:accountant-webserver\"]}",
|
2022-02-14 09:54:51 -05:00
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.4"),
|
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
Namespace: Namespace{Name: "joe"},
|
2022-02-14 09:54:51 -05:00
|
|
|
},
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
namespace: "joe",
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
|
|
|
want: []Machine{
|
2022-02-14 09:54:51 -05:00
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{netaddr.MustParseIP("100.64.0.4")},
|
2022-02-20 17:00:31 -05:00
|
|
|
Namespace: Namespace{Name: "joe"},
|
2022-02-14 09:54:51 -05:00
|
|
|
},
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "all nodes have invalid tags, don't exclude them",
|
|
|
|
args: args{
|
|
|
|
aclPolicy: ACLPolicy{
|
2022-02-20 17:00:31 -05:00
|
|
|
TagOwners: TagOwners{"tag:accountant-webserver": []string{"joe"}},
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
|
|
|
nodes: []Machine{
|
2022-02-14 09:54:51 -05:00
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.1"),
|
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
Namespace: Namespace{Name: "joe"},
|
2022-02-14 09:54:51 -05:00
|
|
|
HostInfo: []byte(
|
2022-02-20 17:00:31 -05:00
|
|
|
"{\"OS\":\"centos\",\"Hostname\":\"hr-web1\",\"RequestTags\":[\"tag:hr-webserver\"]}",
|
2022-02-14 09:54:51 -05:00
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.2"),
|
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
Namespace: Namespace{Name: "joe"},
|
2022-02-14 09:54:51 -05:00
|
|
|
HostInfo: []byte(
|
2022-02-20 17:00:31 -05:00
|
|
|
"{\"OS\":\"centos\",\"Hostname\":\"hr-web2\",\"RequestTags\":[\"tag:hr-webserver\"]}",
|
2022-02-14 09:54:51 -05:00
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.4"),
|
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
Namespace: Namespace{Name: "joe"},
|
2022-02-14 09:54:51 -05:00
|
|
|
},
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
2022-02-20 17:00:31 -05:00
|
|
|
namespace: "joe",
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
|
|
|
want: []Machine{
|
2022-02-14 09:54:51 -05:00
|
|
|
{
|
2022-02-20 17:00:31 -05:00
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.1"),
|
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "joe"},
|
2022-02-14 09:54:51 -05:00
|
|
|
HostInfo: []byte(
|
2022-02-20 17:00:31 -05:00
|
|
|
"{\"OS\":\"centos\",\"Hostname\":\"hr-web1\",\"RequestTags\":[\"tag:hr-webserver\"]}",
|
2022-02-14 09:54:51 -05:00
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
2022-02-20 17:00:31 -05:00
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.2"),
|
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "joe"},
|
2022-02-14 09:54:51 -05:00
|
|
|
HostInfo: []byte(
|
2022-02-20 17:00:31 -05:00
|
|
|
"{\"OS\":\"centos\",\"Hostname\":\"hr-web2\",\"RequestTags\":[\"tag:hr-webserver\"]}",
|
2022-02-14 09:54:51 -05:00
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
2022-02-20 17:00:31 -05:00
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.4"),
|
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "joe"},
|
2022-02-14 09:54:51 -05:00
|
|
|
},
|
2022-02-07 10:12:05 -05:00
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
2022-02-14 09:26:54 -05:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2022-02-14 09:54:51 -05:00
|
|
|
got, err := excludeCorrectlyTaggedNodes(
|
|
|
|
test.args.aclPolicy,
|
|
|
|
test.args.nodes,
|
|
|
|
test.args.namespace,
|
|
|
|
)
|
2022-02-14 09:26:54 -05:00
|
|
|
if (err != nil) != test.wantErr {
|
2022-02-14 09:54:51 -05:00
|
|
|
t.Errorf(
|
|
|
|
"excludeCorrectlyTaggedNodes() error = %v, wantErr %v",
|
|
|
|
err,
|
|
|
|
test.wantErr,
|
|
|
|
)
|
2022-02-14 09:26:54 -05:00
|
|
|
|
2022-02-07 10:12:05 -05:00
|
|
|
return
|
|
|
|
}
|
2022-02-14 09:26:54 -05:00
|
|
|
if !reflect.DeepEqual(got, test.want) {
|
|
|
|
t.Errorf("excludeCorrectlyTaggedNodes() = %v, want %v", got, test.want)
|
2022-02-07 10:12:05 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|