2021-07-03 03:55:32 -06:00
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
2022-02-05 09:18:39 -07:00
|
|
|
"errors"
|
2022-09-01 16:05:43 -06:00
|
|
|
"net/netip"
|
2022-02-07 08:12:05 -07:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2022-02-05 09:18:39 -07:00
|
|
|
|
2021-07-03 03:55:32 -06:00
|
|
|
"gopkg.in/check.v1"
|
2022-11-25 06:46:34 -07:00
|
|
|
"tailscale.com/envknob"
|
2022-02-07 08:12:05 -07:00
|
|
|
"tailscale.com/tailcfg"
|
2021-07-03 03:55:32 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Suite) TestWrongPath(c *check.C) {
|
2021-11-15 09:16:04 -07:00
|
|
|
err := app.LoadACLPolicy("asdfg")
|
2021-07-03 03:55:32 -06:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestBrokenHuJson(c *check.C) {
|
2021-11-15 09:16:04 -07:00
|
|
|
err := app.LoadACLPolicy("./tests/acls/broken.hujson")
|
2021-07-03 03:55:32 -06:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestInvalidPolicyHuson(c *check.C) {
|
2021-11-15 09:16:04 -07:00
|
|
|
err := app.LoadACLPolicy("./tests/acls/invalid.hujson")
|
2021-07-03 03:55:32 -06:00
|
|
|
c.Assert(err, check.NotNil)
|
2021-11-15 09:33:16 -07:00
|
|
|
c.Assert(err, check.Equals, errEmptyPolicy)
|
2021-07-03 03:55:32 -06:00
|
|
|
}
|
|
|
|
|
2021-07-03 09:31:32 -06:00
|
|
|
func (s *Suite) TestParseHosts(c *check.C) {
|
2021-11-15 09:16:04 -07:00
|
|
|
var hosts Hosts
|
|
|
|
err := hosts.UnmarshalJSON(
|
2021-11-13 01:36:45 -07:00
|
|
|
[]byte(
|
|
|
|
`{"example-host-1": "100.100.100.100","example-host-2": "100.100.101.100/24"}`,
|
|
|
|
),
|
|
|
|
)
|
2021-11-15 09:16:04 -07:00
|
|
|
c.Assert(hosts, check.NotNil)
|
2021-07-03 03:55:32 -06:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-07-03 09:31:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestParseInvalidCIDR(c *check.C) {
|
2021-11-15 09:16:04 -07:00
|
|
|
var hosts Hosts
|
|
|
|
err := hosts.UnmarshalJSON([]byte(`{"example-host-1": "100.100.100.100/42"}`))
|
|
|
|
c.Assert(hosts, check.IsNil)
|
2021-07-03 09:31:32 -06:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestRuleInvalidGeneration(c *check.C) {
|
2021-11-15 09:16:04 -07:00
|
|
|
err := app.LoadACLPolicy("./tests/acls/acl_policy_invalid.hujson")
|
2021-07-03 09:31:32 -06:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
}
|
|
|
|
|
2021-07-04 04:35:18 -06:00
|
|
|
func (s *Suite) TestBasicRule(c *check.C) {
|
2021-11-15 09:16:04 -07:00
|
|
|
err := app.LoadACLPolicy("./tests/acls/acl_policy_basic_1.hujson")
|
2021-07-03 09:31:32 -06:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-04-26 03:24:44 -06:00
|
|
|
rules, err := app.aclPolicy.generateFilterRules([]Machine{}, false)
|
2021-07-03 09:31:32 -06:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-07-04 05:01:41 -06:00
|
|
|
c.Assert(rules, check.NotNil)
|
|
|
|
}
|
|
|
|
|
2022-02-21 13:48:05 -07:00
|
|
|
// TODO(kradalby): Make tests values safe, independent and descriptive.
|
2022-02-05 09:18:39 -07:00
|
|
|
func (s *Suite) TestInvalidAction(c *check.C) {
|
|
|
|
app.aclPolicy = &ACLPolicy{
|
|
|
|
ACLs: []ACL{
|
2022-08-04 02:47:00 -06:00
|
|
|
{
|
|
|
|
Action: "invalidAction",
|
|
|
|
Sources: []string{"*"},
|
|
|
|
Destinations: []string{"*:*"},
|
|
|
|
},
|
2022-02-05 09:18:39 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
err := app.UpdateACLRules()
|
|
|
|
c.Assert(errors.Is(err, errInvalidAction), check.Equals, true)
|
|
|
|
}
|
|
|
|
|
2022-09-30 12:44:23 -06:00
|
|
|
func (s *Suite) TestSshRules(c *check.C) {
|
2022-11-25 06:46:34 -07:00
|
|
|
envknob.Setenv("HEADSCALE_EXPERIMENTAL_FEATURE_SSH", "1")
|
|
|
|
|
2023-01-17 09:43:44 -07:00
|
|
|
user, err := app.CreateUser("user1")
|
2022-09-30 12:44:23 -06:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-01-17 09:43:44 -07:00
|
|
|
pak, err := app.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
2022-09-30 12:44:23 -06:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
_, err = app.GetMachine("user1", "testmachine")
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
hostInfo := tailcfg.Hostinfo{
|
|
|
|
OS: "centos",
|
|
|
|
Hostname: "testmachine",
|
|
|
|
RequestTags: []string{"tag:test"},
|
|
|
|
}
|
|
|
|
|
|
|
|
machine := Machine{
|
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
|
|
|
Hostname: "testmachine",
|
|
|
|
IPAddresses: MachineAddresses{netip.MustParseAddr("100.64.0.1")},
|
2023-01-17 12:36:46 -07:00
|
|
|
UserID: user.ID,
|
2022-09-30 12:44:23 -06:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
HostInfo: HostInfo(hostInfo),
|
|
|
|
}
|
|
|
|
app.db.Save(&machine)
|
|
|
|
|
|
|
|
app.aclPolicy = &ACLPolicy{
|
|
|
|
Groups: Groups{
|
|
|
|
"group:test": []string{"user1"},
|
|
|
|
},
|
|
|
|
Hosts: Hosts{
|
|
|
|
"client": netip.PrefixFrom(netip.MustParseAddr("100.64.99.42"), 32),
|
|
|
|
},
|
|
|
|
ACLs: []ACL{
|
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"*"},
|
|
|
|
Destinations: []string{"*:*"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
SSHs: []SSH{
|
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"group:test"},
|
|
|
|
Destinations: []string{"client"},
|
|
|
|
Users: []string{"autogroup:nonroot"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"*"},
|
|
|
|
Destinations: []string{"client"},
|
|
|
|
Users: []string{"autogroup:nonroot"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = app.UpdateACLRules()
|
|
|
|
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(app.sshPolicy, check.NotNil)
|
|
|
|
c.Assert(app.sshPolicy.Rules, check.HasLen, 2)
|
|
|
|
c.Assert(app.sshPolicy.Rules[0].SSHUsers, check.HasLen, 1)
|
|
|
|
c.Assert(app.sshPolicy.Rules[0].Principals, check.HasLen, 1)
|
|
|
|
c.Assert(app.sshPolicy.Rules[0].Principals[0].NodeIP, check.Matches, "100.64.0.1")
|
|
|
|
|
|
|
|
c.Assert(app.sshPolicy.Rules[1].SSHUsers, check.HasLen, 1)
|
|
|
|
c.Assert(app.sshPolicy.Rules[1].Principals, check.HasLen, 1)
|
|
|
|
c.Assert(app.sshPolicy.Rules[1].Principals[0].NodeIP, check.Matches, "*")
|
|
|
|
}
|
|
|
|
|
2022-02-05 09:18:39 -07:00
|
|
|
func (s *Suite) TestInvalidGroupInGroup(c *check.C) {
|
2022-06-08 05:40:15 -06:00
|
|
|
// this ACL is wrong because the group in Sources sections doesn't exist
|
2022-02-05 09:18:39 -07:00
|
|
|
app.aclPolicy = &ACLPolicy{
|
2022-02-14 07:54:51 -07:00
|
|
|
Groups: Groups{
|
|
|
|
"group:test": []string{"foo"},
|
|
|
|
"group:error": []string{"foo", "group:test"},
|
|
|
|
},
|
2022-02-05 09:18:39 -07:00
|
|
|
ACLs: []ACL{
|
2022-08-04 02:47:00 -06:00
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"group:error"},
|
|
|
|
Destinations: []string{"*:*"},
|
|
|
|
},
|
2022-02-05 09:18:39 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
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{
|
2022-08-04 02:47:00 -06:00
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"tag:foo"},
|
|
|
|
Destinations: []string{"*:*"},
|
|
|
|
},
|
2022-02-05 09:18:39 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
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-06-08 05:40:15 -06:00
|
|
|
// the tag is matched in the Sources section.
|
|
|
|
func (s *Suite) TestValidExpandTagOwnersInSources(c *check.C) {
|
2023-01-17 09:43:44 -07:00
|
|
|
user, err := app.CreateUser("user1")
|
2022-02-05 09:18:39 -07:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-01-17 09:43:44 -07:00
|
|
|
pak, err := app.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
2022-02-05 09:18:39 -07:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-02-20 15:00:31 -07:00
|
|
|
_, err = app.GetMachine("user1", "testmachine")
|
2022-02-05 09:18:39 -07:00
|
|
|
c.Assert(err, check.NotNil)
|
2022-03-01 09:34:35 -07:00
|
|
|
hostInfo := tailcfg.Hostinfo{
|
|
|
|
OS: "centos",
|
|
|
|
Hostname: "testmachine",
|
|
|
|
RequestTags: []string{"tag:test"},
|
|
|
|
}
|
|
|
|
|
2022-02-05 09:18:39 -07:00
|
|
|
machine := Machine{
|
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
2022-04-24 13:54:38 -06:00
|
|
|
Hostname: "testmachine",
|
2022-09-01 16:05:43 -06:00
|
|
|
IPAddresses: MachineAddresses{netip.MustParseAddr("100.64.0.1")},
|
2023-01-17 12:36:46 -07:00
|
|
|
UserID: user.ID,
|
2022-02-05 09:18:39 -07:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
2022-03-01 09:34:35 -07:00
|
|
|
HostInfo: HostInfo(hostInfo),
|
2022-02-05 09:18:39 -07:00
|
|
|
}
|
|
|
|
app.db.Save(&machine)
|
|
|
|
|
|
|
|
app.aclPolicy = &ACLPolicy{
|
2022-02-20 15:00:31 -07:00
|
|
|
Groups: Groups{"group:test": []string{"user1", "user2"}},
|
|
|
|
TagOwners: TagOwners{"tag:test": []string{"user3", "group:test"}},
|
2022-02-05 09:18:39 -07:00
|
|
|
ACLs: []ACL{
|
2022-08-04 02:47:00 -06:00
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"tag:test"},
|
|
|
|
Destinations: []string{"*:*"},
|
|
|
|
},
|
2022-02-05 09:18:39 -07: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")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-06-08 05:40:15 -06:00
|
|
|
// the tag is matched in the Destinations section.
|
2022-06-11 06:12:53 -06:00
|
|
|
func (s *Suite) TestValidExpandTagOwnersInDestinations(c *check.C) {
|
2023-01-17 09:43:44 -07:00
|
|
|
user, err := app.CreateUser("user1")
|
2022-02-05 09:18:39 -07:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-01-17 09:43:44 -07:00
|
|
|
pak, err := app.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
2022-02-05 09:18:39 -07:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-02-20 15:00:31 -07:00
|
|
|
_, err = app.GetMachine("user1", "testmachine")
|
2022-02-05 09:18:39 -07:00
|
|
|
c.Assert(err, check.NotNil)
|
2022-03-01 09:34:35 -07:00
|
|
|
hostInfo := tailcfg.Hostinfo{
|
|
|
|
OS: "centos",
|
|
|
|
Hostname: "testmachine",
|
|
|
|
RequestTags: []string{"tag:test"},
|
|
|
|
}
|
|
|
|
|
2022-02-05 09:18:39 -07:00
|
|
|
machine := Machine{
|
|
|
|
ID: 1,
|
2022-02-20 15:00:31 -07:00
|
|
|
MachineKey: "12345",
|
2022-02-05 09:18:39 -07:00
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
2022-04-24 13:54:38 -06:00
|
|
|
Hostname: "testmachine",
|
2022-09-01 16:05:43 -06:00
|
|
|
IPAddresses: MachineAddresses{netip.MustParseAddr("100.64.0.1")},
|
2023-01-17 12:36:46 -07:00
|
|
|
UserID: user.ID,
|
2022-02-05 09:18:39 -07:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
2022-03-01 09:34:35 -07:00
|
|
|
HostInfo: HostInfo(hostInfo),
|
2022-02-05 09:18:39 -07:00
|
|
|
}
|
|
|
|
app.db.Save(&machine)
|
|
|
|
|
|
|
|
app.aclPolicy = &ACLPolicy{
|
2022-02-20 15:00:31 -07:00
|
|
|
Groups: Groups{"group:test": []string{"user1", "user2"}},
|
|
|
|
TagOwners: TagOwners{"tag:test": []string{"user3", "group:test"}},
|
2022-02-05 09:18:39 -07:00
|
|
|
ACLs: []ACL{
|
2022-08-04 02:47:00 -06:00
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"*"},
|
|
|
|
Destinations: []string{"tag:test:*"},
|
|
|
|
},
|
2022-02-05 09:18:39 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
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:
|
2023-01-17 09:43:44 -07:00
|
|
|
// tag on a host that isn't owned by a tag owners. So the user
|
2022-02-14 07:26:54 -07:00
|
|
|
// of the host should be valid.
|
2023-01-17 09:43:44 -07:00
|
|
|
func (s *Suite) TestInvalidTagValidUser(c *check.C) {
|
|
|
|
user, err := app.CreateUser("user1")
|
2022-02-05 09:18:39 -07:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-01-17 09:43:44 -07:00
|
|
|
pak, err := app.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
2022-02-05 09:18:39 -07:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-02-20 15:00:31 -07:00
|
|
|
_, err = app.GetMachine("user1", "testmachine")
|
2022-02-05 09:18:39 -07:00
|
|
|
c.Assert(err, check.NotNil)
|
2022-03-01 09:34:35 -07:00
|
|
|
hostInfo := tailcfg.Hostinfo{
|
|
|
|
OS: "centos",
|
|
|
|
Hostname: "testmachine",
|
|
|
|
RequestTags: []string{"tag:foo"},
|
|
|
|
}
|
|
|
|
|
2022-02-05 09:18:39 -07:00
|
|
|
machine := Machine{
|
|
|
|
ID: 1,
|
2022-02-20 15:00:31 -07:00
|
|
|
MachineKey: "12345",
|
2022-02-05 09:18:39 -07:00
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
2022-04-24 13:54:38 -06:00
|
|
|
Hostname: "testmachine",
|
2022-09-01 16:05:43 -06:00
|
|
|
IPAddresses: MachineAddresses{netip.MustParseAddr("100.64.0.1")},
|
2023-01-17 12:36:46 -07:00
|
|
|
UserID: user.ID,
|
2022-02-05 09:18:39 -07:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
2022-03-01 09:34:35 -07:00
|
|
|
HostInfo: HostInfo(hostInfo),
|
2022-02-05 09:18:39 -07:00
|
|
|
}
|
|
|
|
app.db.Save(&machine)
|
|
|
|
|
|
|
|
app.aclPolicy = &ACLPolicy{
|
2022-02-20 15:00:31 -07:00
|
|
|
TagOwners: TagOwners{"tag:test": []string{"user1"}},
|
2022-02-05 09:18:39 -07:00
|
|
|
ACLs: []ACL{
|
2022-08-04 02:47:00 -06:00
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"user1"},
|
|
|
|
Destinations: []string{"*:*"},
|
|
|
|
},
|
2022-02-05 09:18:39 -07: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.
|
2023-01-17 09:43:44 -07:00
|
|
|
// an ACL rule is matching the tag to a user. It should not be valid since the
|
2022-02-05 09:18:39 -07:00
|
|
|
// host should be tied to the tag now.
|
2023-01-17 09:43:44 -07:00
|
|
|
func (s *Suite) TestValidTagInvalidUser(c *check.C) {
|
|
|
|
user, err := app.CreateUser("user1")
|
2022-02-05 09:18:39 -07:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-01-17 09:43:44 -07:00
|
|
|
pak, err := app.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
2022-02-05 09:18:39 -07:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-02-20 15:00:31 -07:00
|
|
|
_, err = app.GetMachine("user1", "webserver")
|
2022-02-05 09:18:39 -07:00
|
|
|
c.Assert(err, check.NotNil)
|
2022-03-01 09:34:35 -07:00
|
|
|
hostInfo := tailcfg.Hostinfo{
|
|
|
|
OS: "centos",
|
|
|
|
Hostname: "webserver",
|
|
|
|
RequestTags: []string{"tag:webapp"},
|
|
|
|
}
|
|
|
|
|
2022-02-05 09:18:39 -07:00
|
|
|
machine := Machine{
|
|
|
|
ID: 1,
|
2022-02-20 15:00:31 -07:00
|
|
|
MachineKey: "12345",
|
2022-02-05 09:18:39 -07:00
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
2022-04-24 13:54:38 -06:00
|
|
|
Hostname: "webserver",
|
2022-09-01 16:05:43 -06:00
|
|
|
IPAddresses: MachineAddresses{netip.MustParseAddr("100.64.0.1")},
|
2023-01-17 12:36:46 -07:00
|
|
|
UserID: user.ID,
|
2022-02-05 09:18:39 -07:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
2022-03-01 09:34:35 -07:00
|
|
|
HostInfo: HostInfo(hostInfo),
|
2022-02-05 09:18:39 -07:00
|
|
|
}
|
|
|
|
app.db.Save(&machine)
|
2022-02-20 15:00:31 -07:00
|
|
|
_, err = app.GetMachine("user1", "user")
|
2022-03-01 09:34:35 -07:00
|
|
|
hostInfo2 := tailcfg.Hostinfo{
|
|
|
|
OS: "debian",
|
|
|
|
Hostname: "Hostname",
|
|
|
|
}
|
2022-02-05 09:18:39 -07:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
machine = Machine{
|
|
|
|
ID: 2,
|
2022-02-20 15:00:31 -07:00
|
|
|
MachineKey: "56789",
|
2022-02-05 09:18:39 -07:00
|
|
|
NodeKey: "bar2",
|
|
|
|
DiscoKey: "faab",
|
2022-04-24 13:54:38 -06:00
|
|
|
Hostname: "user",
|
2022-09-01 16:05:43 -06:00
|
|
|
IPAddresses: MachineAddresses{netip.MustParseAddr("100.64.0.2")},
|
2023-01-17 12:36:46 -07:00
|
|
|
UserID: user.ID,
|
2022-02-05 09:18:39 -07:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
2022-03-01 09:34:35 -07:00
|
|
|
HostInfo: HostInfo(hostInfo2),
|
2022-02-05 09:18:39 -07:00
|
|
|
}
|
|
|
|
app.db.Save(&machine)
|
|
|
|
|
|
|
|
app.aclPolicy = &ACLPolicy{
|
2022-02-20 15:00:31 -07:00
|
|
|
TagOwners: TagOwners{"tag:webapp": []string{"user1"}},
|
2022-02-05 09:18:39 -07:00
|
|
|
ACLs: []ACL{
|
2022-02-14 07:54:51 -07:00
|
|
|
{
|
2022-06-08 05:40:15 -06:00
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"user1"},
|
|
|
|
Destinations: []string{"tag:webapp:80,443"},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2022-02-05 09:18:39 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
err = app.UpdateACLRules()
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(app.aclRules, check.HasLen, 1)
|
2022-02-07 08:12:05 -07: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 09:18:39 -07:00
|
|
|
}
|
|
|
|
|
2021-07-04 05:01:41 -06:00
|
|
|
func (s *Suite) TestPortRange(c *check.C) {
|
2021-11-15 09:16:04 -07:00
|
|
|
err := app.LoadACLPolicy("./tests/acls/acl_policy_basic_range.hujson")
|
2021-07-04 05:01:41 -06:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-04-26 03:24:44 -06:00
|
|
|
rules, err := app.aclPolicy.generateFilterRules([]Machine{}, false)
|
2021-07-04 05:01:41 -06:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(rules, check.NotNil)
|
|
|
|
|
2021-11-04 16:16:56 -06:00
|
|
|
c.Assert(rules, check.HasLen, 1)
|
2022-01-16 06:16:59 -07: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 05:01:41 -06:00
|
|
|
}
|
|
|
|
|
2022-06-08 10:06:25 -06:00
|
|
|
func (s *Suite) TestProtocolParsing(c *check.C) {
|
|
|
|
err := app.LoadACLPolicy("./tests/acls/acl_policy_basic_protocols.hujson")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-04-26 03:24:44 -06:00
|
|
|
rules, err := app.aclPolicy.generateFilterRules([]Machine{}, false)
|
2022-06-08 10:06:25 -06:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(rules, check.NotNil)
|
|
|
|
|
|
|
|
c.Assert(rules, check.HasLen, 3)
|
2022-06-11 07:17:07 -06:00
|
|
|
c.Assert(rules[0].IPProto[0], check.Equals, protocolTCP)
|
|
|
|
c.Assert(rules[1].IPProto[0], check.Equals, protocolUDP)
|
|
|
|
c.Assert(rules[2].IPProto[1], check.Equals, protocolIPv6ICMP)
|
2022-06-08 10:06:25 -06:00
|
|
|
}
|
|
|
|
|
2021-07-04 05:01:41 -06:00
|
|
|
func (s *Suite) TestPortWildcard(c *check.C) {
|
2021-11-15 09:16:04 -07:00
|
|
|
err := app.LoadACLPolicy("./tests/acls/acl_policy_basic_wildcards.hujson")
|
2021-07-04 05:01:41 -06:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-04-26 03:24:44 -06:00
|
|
|
rules, err := app.aclPolicy.generateFilterRules([]Machine{}, false)
|
2021-07-04 05:01:41 -06:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(rules, check.NotNil)
|
|
|
|
|
2021-11-04 16:16:56 -06:00
|
|
|
c.Assert(rules, check.HasLen, 1)
|
2022-01-16 06:16:59 -07:00
|
|
|
c.Assert(rules[0].DstPorts, check.HasLen, 1)
|
2022-02-27 01:04:59 -07:00
|
|
|
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) TestPortWildcardYAML(c *check.C) {
|
|
|
|
err := app.LoadACLPolicy("./tests/acls/acl_policy_basic_wildcards.yaml")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-04-26 03:24:44 -06:00
|
|
|
rules, err := app.aclPolicy.generateFilterRules([]Machine{}, false)
|
2022-02-27 01:04:59 -07:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(rules, check.NotNil)
|
|
|
|
|
|
|
|
c.Assert(rules, check.HasLen, 1)
|
|
|
|
c.Assert(rules[0].DstPorts, check.HasLen, 1)
|
2022-01-16 06:16:59 -07:00
|
|
|
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 05:01:41 -06:00
|
|
|
}
|
|
|
|
|
2023-01-17 09:43:44 -07:00
|
|
|
func (s *Suite) TestPortUser(c *check.C) {
|
|
|
|
user, err := app.CreateUser("testuser")
|
2021-07-04 05:01:41 -06:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-01-17 09:43:44 -07:00
|
|
|
pak, err := app.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
2021-07-04 05:01:41 -06:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-01-17 09:43:44 -07:00
|
|
|
_, err = app.GetMachine("testuser", "testmachine")
|
2021-07-04 05:01:41 -06:00
|
|
|
c.Assert(err, check.NotNil)
|
2022-01-16 06:16:59 -07:00
|
|
|
ips, _ := app.getAvailableIPs()
|
2021-11-15 09:16:04 -07:00
|
|
|
machine := Machine{
|
2021-07-04 05:01:41 -06:00
|
|
|
ID: 0,
|
2022-02-20 15:00:31 -07:00
|
|
|
MachineKey: "12345",
|
2021-07-04 05:01:41 -06:00
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
2022-04-24 13:54:38 -06:00
|
|
|
Hostname: "testmachine",
|
2023-01-17 12:36:46 -07:00
|
|
|
UserID: user.ID,
|
2021-11-18 01:49:55 -07:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2022-01-16 06:16:59 -07:00
|
|
|
IPAddresses: ips,
|
2021-07-04 05:01:41 -06:00
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
2021-11-15 09:16:04 -07:00
|
|
|
app.db.Save(&machine)
|
2021-07-04 05:01:41 -06:00
|
|
|
|
2021-11-15 09:16:04 -07:00
|
|
|
err = app.LoadACLPolicy(
|
2023-01-17 09:43:44 -07:00
|
|
|
"./tests/acls/acl_policy_basic_user_as_user.hujson",
|
2021-11-15 09:16:04 -07:00
|
|
|
)
|
2021-07-04 05:01:41 -06:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-11-30 16:37:58 -07:00
|
|
|
machines, err := app.ListMachines()
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-04-26 03:24:44 -06:00
|
|
|
rules, err := app.aclPolicy.generateFilterRules(machines, false)
|
2021-07-04 05:01:41 -06:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(rules, check.NotNil)
|
|
|
|
|
2021-11-04 16:16:56 -06:00
|
|
|
c.Assert(rules, check.HasLen, 1)
|
2022-01-16 06:16:59 -07: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 03:55:32 -06:00
|
|
|
}
|
2021-07-04 04:35:18 -06:00
|
|
|
|
2021-07-04 05:23:31 -06:00
|
|
|
func (s *Suite) TestPortGroup(c *check.C) {
|
2023-01-17 09:43:44 -07:00
|
|
|
user, err := app.CreateUser("testuser")
|
2021-07-04 05:23:31 -06:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-01-17 09:43:44 -07:00
|
|
|
pak, err := app.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
2021-07-04 05:23:31 -06:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-01-17 09:43:44 -07:00
|
|
|
_, err = app.GetMachine("testuser", "testmachine")
|
2021-07-04 05:23:31 -06:00
|
|
|
c.Assert(err, check.NotNil)
|
2022-01-16 06:16:59 -07:00
|
|
|
ips, _ := app.getAvailableIPs()
|
2021-11-15 09:16:04 -07:00
|
|
|
machine := Machine{
|
2021-07-04 05:23:31 -06:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
2022-04-24 13:54:38 -06:00
|
|
|
Hostname: "testmachine",
|
2023-01-17 12:36:46 -07:00
|
|
|
UserID: user.ID,
|
2021-11-18 01:49:55 -07:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2022-01-16 06:16:59 -07:00
|
|
|
IPAddresses: ips,
|
2021-07-04 05:23:31 -06:00
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
2021-11-15 09:16:04 -07:00
|
|
|
app.db.Save(&machine)
|
2021-07-04 04:35:18 -06:00
|
|
|
|
2021-11-15 09:16:04 -07:00
|
|
|
err = app.LoadACLPolicy("./tests/acls/acl_policy_basic_groups.hujson")
|
2021-07-04 05:23:31 -06:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-11-30 16:37:58 -07:00
|
|
|
machines, err := app.ListMachines()
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-04-26 03:24:44 -06:00
|
|
|
rules, err := app.aclPolicy.generateFilterRules(machines, false)
|
2021-07-04 05:23:31 -06:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(rules, check.NotNil)
|
2021-07-04 04:35:18 -06:00
|
|
|
|
2021-11-04 16:16:56 -06:00
|
|
|
c.Assert(rules, check.HasLen, 1)
|
2022-01-16 06:16:59 -07: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 05:23:31 -06:00
|
|
|
}
|
2022-02-07 08:12:05 -07:00
|
|
|
|
|
|
|
func Test_expandGroup(t *testing.T) {
|
2023-04-26 02:58:26 -06:00
|
|
|
type field struct {
|
|
|
|
pol ACLPolicy
|
|
|
|
}
|
2022-02-07 08:12:05 -07:00
|
|
|
type args struct {
|
2022-03-01 13:01:46 -07:00
|
|
|
group string
|
|
|
|
stripEmailDomain bool
|
2022-02-07 08:12:05 -07:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2023-04-26 02:58:26 -06:00
|
|
|
field field
|
2022-02-07 08:12:05 -07:00
|
|
|
args args
|
|
|
|
want []string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "simple test",
|
2023-04-26 02:58:26 -06:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{
|
2022-02-14 07:54:51 -07:00
|
|
|
Groups: Groups{
|
2022-02-20 15:00:31 -07:00
|
|
|
"group:test": []string{"user1", "user2", "user3"},
|
|
|
|
"group:foo": []string{"user2", "user3"},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
2023-04-26 02:58:26 -06:00
|
|
|
},
|
|
|
|
args: args{
|
2022-03-01 13:01:46 -07:00
|
|
|
group: "group:test",
|
|
|
|
stripEmailDomain: true,
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
2022-02-20 15:00:31 -07:00
|
|
|
want: []string{"user1", "user2", "user3"},
|
2022-02-07 08:12:05 -07:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "InexistantGroup",
|
2023-04-26 02:58:26 -06:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{
|
2022-02-14 07:54:51 -07:00
|
|
|
Groups: Groups{
|
2022-02-20 15:00:31 -07:00
|
|
|
"group:test": []string{"user1", "user2", "user3"},
|
|
|
|
"group:foo": []string{"user2", "user3"},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
2023-04-26 02:58:26 -06:00
|
|
|
},
|
|
|
|
args: args{
|
2022-03-01 13:01:46 -07:00
|
|
|
group: "group:undefined",
|
|
|
|
stripEmailDomain: true,
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
|
|
|
want: []string{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2022-03-01 13:01:46 -07:00
|
|
|
{
|
|
|
|
name: "Expand emails in group",
|
2023-04-26 02:58:26 -06:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{
|
2022-03-01 13:01:46 -07:00
|
|
|
Groups: Groups{
|
|
|
|
"group:admin": []string{
|
|
|
|
"joe.bar@gmail.com",
|
|
|
|
"john.doe@yahoo.fr",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-04-26 02:58:26 -06:00
|
|
|
},
|
|
|
|
args: args{
|
2022-03-01 13:01:46 -07:00
|
|
|
group: "group:admin",
|
|
|
|
stripEmailDomain: true,
|
|
|
|
},
|
|
|
|
want: []string{"joe.bar", "john.doe"},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Expand emails in group",
|
2023-04-26 02:58:26 -06:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{
|
2022-03-01 13:01:46 -07:00
|
|
|
Groups: Groups{
|
|
|
|
"group:admin": []string{
|
|
|
|
"joe.bar@gmail.com",
|
|
|
|
"john.doe@yahoo.fr",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-04-26 02:58:26 -06:00
|
|
|
},
|
|
|
|
args: args{
|
2022-03-01 13:01:46 -07:00
|
|
|
group: "group:admin",
|
|
|
|
stripEmailDomain: false,
|
|
|
|
},
|
|
|
|
want: []string{"joe.bar.gmail.com", "john.doe.yahoo.fr"},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
2022-02-07 08:12:05 -07:00
|
|
|
}
|
2022-02-14 07:26:54 -07:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2023-04-26 02:58:26 -06:00
|
|
|
got, err := test.field.pol.getUsersInGroup(
|
2022-03-01 13:01:46 -07:00
|
|
|
test.args.group,
|
|
|
|
test.args.stripEmailDomain,
|
|
|
|
)
|
2022-02-14 07:26:54 -07:00
|
|
|
if (err != nil) != test.wantErr {
|
|
|
|
t.Errorf("expandGroup() error = %v, wantErr %v", err, test.wantErr)
|
|
|
|
|
2022-02-07 08:12:05 -07:00
|
|
|
return
|
|
|
|
}
|
2022-02-14 07:26:54 -07:00
|
|
|
if !reflect.DeepEqual(got, test.want) {
|
|
|
|
t.Errorf("expandGroup() = %v, want %v", got, test.want)
|
2022-02-07 08:12:05 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_expandTagOwners(t *testing.T) {
|
|
|
|
type args struct {
|
2023-04-26 02:58:26 -06:00
|
|
|
aclPolicy *ACLPolicy
|
2022-03-01 13:01:46 -07:00
|
|
|
tag string
|
|
|
|
stripEmailDomain bool
|
2022-02-07 08:12:05 -07:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want []string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
2022-02-20 15:00:31 -07:00
|
|
|
name: "simple tag expansion",
|
2022-02-07 08:12:05 -07:00
|
|
|
args: args{
|
2023-04-26 02:58:26 -06:00
|
|
|
aclPolicy: &ACLPolicy{
|
2022-02-20 15:00:31 -07:00
|
|
|
TagOwners: TagOwners{"tag:test": []string{"user1"}},
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
2022-03-01 13:01:46 -07:00
|
|
|
tag: "tag:test",
|
|
|
|
stripEmailDomain: true,
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
2022-02-20 15:00:31 -07:00
|
|
|
want: []string{"user1"},
|
2022-02-07 08:12:05 -07:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
2022-02-20 15:00:31 -07:00
|
|
|
name: "expand with tag and group",
|
2022-02-07 08:12:05 -07:00
|
|
|
args: args{
|
2023-04-26 02:58:26 -06:00
|
|
|
aclPolicy: &ACLPolicy{
|
2022-02-20 15:00:31 -07:00
|
|
|
Groups: Groups{"group:foo": []string{"user1", "user2"}},
|
2022-02-07 08:12:05 -07:00
|
|
|
TagOwners: TagOwners{"tag:test": []string{"group:foo"}},
|
|
|
|
},
|
2022-03-01 13:01:46 -07:00
|
|
|
tag: "tag:test",
|
|
|
|
stripEmailDomain: true,
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
2022-02-20 15:00:31 -07:00
|
|
|
want: []string{"user1", "user2"},
|
2022-02-07 08:12:05 -07:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
2023-01-17 09:43:44 -07:00
|
|
|
name: "expand with user and group",
|
2022-02-07 08:12:05 -07:00
|
|
|
args: args{
|
2023-04-26 02:58:26 -06:00
|
|
|
aclPolicy: &ACLPolicy{
|
2022-02-20 15:00:31 -07:00
|
|
|
Groups: Groups{"group:foo": []string{"user1", "user2"}},
|
|
|
|
TagOwners: TagOwners{"tag:test": []string{"group:foo", "user3"}},
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
2022-03-01 13:01:46 -07:00
|
|
|
tag: "tag:test",
|
|
|
|
stripEmailDomain: true,
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
2022-02-20 15:00:31 -07:00
|
|
|
want: []string{"user1", "user2", "user3"},
|
2022-02-07 08:12:05 -07:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid tag",
|
|
|
|
args: args{
|
2023-04-26 02:58:26 -06:00
|
|
|
aclPolicy: &ACLPolicy{
|
2022-02-20 15:00:31 -07:00
|
|
|
TagOwners: TagOwners{"tag:foo": []string{"group:foo", "user1"}},
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
2022-03-01 13:01:46 -07:00
|
|
|
tag: "tag:test",
|
|
|
|
stripEmailDomain: true,
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
|
|
|
want: []string{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid group",
|
|
|
|
args: args{
|
2023-04-26 02:58:26 -06:00
|
|
|
aclPolicy: &ACLPolicy{
|
2022-02-20 15:00:31 -07:00
|
|
|
Groups: Groups{"group:bar": []string{"user1", "user2"}},
|
|
|
|
TagOwners: TagOwners{"tag:test": []string{"group:foo", "user2"}},
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
2022-03-01 13:01:46 -07:00
|
|
|
tag: "tag:test",
|
|
|
|
stripEmailDomain: true,
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
|
|
|
want: []string{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
2022-02-14 07:26:54 -07:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2023-04-26 02:58:26 -06:00
|
|
|
got, err := getTagOwners(
|
2022-03-01 13:01:46 -07:00
|
|
|
test.args.aclPolicy,
|
|
|
|
test.args.tag,
|
|
|
|
test.args.stripEmailDomain,
|
|
|
|
)
|
2022-02-14 07:26:54 -07:00
|
|
|
if (err != nil) != test.wantErr {
|
|
|
|
t.Errorf("expandTagOwners() error = %v, wantErr %v", err, test.wantErr)
|
|
|
|
|
2022-02-07 08:12:05 -07:00
|
|
|
return
|
|
|
|
}
|
2022-02-14 07:26:54 -07:00
|
|
|
if !reflect.DeepEqual(got, test.want) {
|
|
|
|
t.Errorf("expandTagOwners() = %v, want %v", got, test.want)
|
2022-02-07 08:12:05 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_expandPorts(t *testing.T) {
|
|
|
|
type args struct {
|
2022-06-08 09:43:59 -06:00
|
|
|
portsStr string
|
|
|
|
needsWildcard bool
|
2022-02-07 08:12:05 -07:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *[]tailcfg.PortRange
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "wildcard",
|
2022-06-08 09:43:59 -06:00
|
|
|
args: args{portsStr: "*", needsWildcard: true},
|
2022-02-07 08:12:05 -07:00
|
|
|
want: &[]tailcfg.PortRange{
|
|
|
|
{First: portRangeBegin, Last: portRangeEnd},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
2022-06-08 09:43:59 -06:00
|
|
|
{
|
|
|
|
name: "needs wildcard but does not require it",
|
|
|
|
args: args{portsStr: "*", needsWildcard: false},
|
|
|
|
want: &[]tailcfg.PortRange{
|
|
|
|
{First: portRangeBegin, Last: portRangeEnd},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "needs wildcard but gets port",
|
|
|
|
args: args{portsStr: "80,443", needsWildcard: true},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2022-02-07 08:12:05 -07:00
|
|
|
{
|
2022-06-08 05:40:15 -06:00
|
|
|
name: "two Destinations",
|
2022-06-08 09:43:59 -06:00
|
|
|
args: args{portsStr: "80,443", needsWildcard: false},
|
2022-02-07 08:12:05 -07:00
|
|
|
want: &[]tailcfg.PortRange{
|
|
|
|
{First: 80, Last: 80},
|
|
|
|
{First: 443, Last: 443},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "a range and a port",
|
2022-06-08 09:43:59 -06:00
|
|
|
args: args{portsStr: "80-1024,443", needsWildcard: false},
|
2022-02-07 08:12:05 -07:00
|
|
|
want: &[]tailcfg.PortRange{
|
|
|
|
{First: 80, Last: 1024},
|
|
|
|
{First: 443, Last: 443},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "out of bounds",
|
2022-06-08 09:43:59 -06:00
|
|
|
args: args{portsStr: "854038", needsWildcard: false},
|
2022-02-07 08:12:05 -07:00
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wrong port",
|
2022-06-08 09:43:59 -06:00
|
|
|
args: args{portsStr: "85a38", needsWildcard: false},
|
2022-02-07 08:12:05 -07:00
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wrong port in first",
|
2022-06-08 09:43:59 -06:00
|
|
|
args: args{portsStr: "a-80", needsWildcard: false},
|
2022-02-07 08:12:05 -07:00
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wrong port in last",
|
2022-06-08 09:43:59 -06:00
|
|
|
args: args{portsStr: "80-85a38", needsWildcard: false},
|
2022-02-07 08:12:05 -07:00
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wrong port format",
|
2022-06-08 09:43:59 -06:00
|
|
|
args: args{portsStr: "80-85a38-3", needsWildcard: false},
|
2022-02-07 08:12:05 -07:00
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
2022-02-14 07:26:54 -07:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2022-06-08 09:43:59 -06:00
|
|
|
got, err := expandPorts(test.args.portsStr, test.args.needsWildcard)
|
2022-02-14 07:26:54 -07:00
|
|
|
if (err != nil) != test.wantErr {
|
|
|
|
t.Errorf("expandPorts() error = %v, wantErr %v", err, test.wantErr)
|
|
|
|
|
2022-02-07 08:12:05 -07:00
|
|
|
return
|
|
|
|
}
|
2022-02-14 07:26:54 -07:00
|
|
|
if !reflect.DeepEqual(got, test.want) {
|
|
|
|
t.Errorf("expandPorts() = %v, want %v", got, test.want)
|
2022-02-07 08:12:05 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-17 09:43:44 -07:00
|
|
|
func Test_listMachinesInUser(t *testing.T) {
|
2022-02-07 08:12:05 -07:00
|
|
|
type args struct {
|
2023-01-17 12:36:46 -07:00
|
|
|
machines []Machine
|
|
|
|
user string
|
2022-02-07 08:12:05 -07:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want []Machine
|
|
|
|
}{
|
|
|
|
{
|
2023-01-17 09:43:44 -07:00
|
|
|
name: "1 machine in user",
|
2022-02-07 08:12:05 -07:00
|
|
|
args: args{
|
|
|
|
machines: []Machine{
|
2023-01-17 09:43:44 -07:00
|
|
|
{User: User{Name: "joe"}},
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
user: "joe",
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
|
|
|
want: []Machine{
|
2023-01-17 09:43:44 -07:00
|
|
|
{User: User{Name: "joe"}},
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-01-17 09:43:44 -07:00
|
|
|
name: "3 machines, 2 in user",
|
2022-02-07 08:12:05 -07:00
|
|
|
args: args{
|
|
|
|
machines: []Machine{
|
2023-01-17 09:43:44 -07:00
|
|
|
{ID: 1, User: User{Name: "joe"}},
|
|
|
|
{ID: 2, User: User{Name: "marc"}},
|
|
|
|
{ID: 3, User: User{Name: "marc"}},
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
user: "marc",
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
|
|
|
want: []Machine{
|
2023-01-17 09:43:44 -07:00
|
|
|
{ID: 2, User: User{Name: "marc"}},
|
|
|
|
{ID: 3, User: User{Name: "marc"}},
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-01-17 09:43:44 -07:00
|
|
|
name: "5 machines, 0 in user",
|
2022-02-07 08:12:05 -07:00
|
|
|
args: args{
|
|
|
|
machines: []Machine{
|
2023-01-17 09:43:44 -07:00
|
|
|
{ID: 1, User: User{Name: "joe"}},
|
|
|
|
{ID: 2, User: User{Name: "marc"}},
|
|
|
|
{ID: 3, User: User{Name: "marc"}},
|
|
|
|
{ID: 4, User: User{Name: "marc"}},
|
|
|
|
{ID: 5, User: User{Name: "marc"}},
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
user: "mickael",
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
|
|
|
want: []Machine{},
|
|
|
|
},
|
|
|
|
}
|
2022-02-14 10:24:37 -07:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2023-01-17 09:43:44 -07:00
|
|
|
if got := filterMachinesByUser(test.args.machines, test.args.user); !reflect.DeepEqual(
|
2022-02-14 07:54:51 -07:00
|
|
|
got,
|
2022-02-14 10:24:37 -07:00
|
|
|
test.want,
|
2022-02-14 07:54:51 -07:00
|
|
|
) {
|
2023-01-17 09:43:44 -07:00
|
|
|
t.Errorf("listMachinesInUser() = %v, want %v", got, test.want)
|
2022-02-07 08:12:05 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_expandAlias(t *testing.T) {
|
2023-04-26 02:58:26 -06:00
|
|
|
type field struct {
|
|
|
|
pol ACLPolicy
|
|
|
|
}
|
2022-02-07 08:12:05 -07:00
|
|
|
type args struct {
|
2022-03-01 13:01:46 -07:00
|
|
|
machines []Machine
|
|
|
|
aclPolicy ACLPolicy
|
|
|
|
alias string
|
|
|
|
stripEmailDomain bool
|
2022-02-07 08:12:05 -07:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2023-04-26 02:58:26 -06:00
|
|
|
field field
|
2022-02-07 08:12:05 -07:00
|
|
|
args args
|
|
|
|
want []string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "wildcard",
|
2023-04-26 02:58:26 -06:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{},
|
|
|
|
},
|
2022-02-07 08:12:05 -07:00
|
|
|
args: args{
|
|
|
|
alias: "*",
|
|
|
|
machines: []Machine{
|
2022-09-01 16:05:43 -06:00
|
|
|
{IPAddresses: MachineAddresses{netip.MustParseAddr("100.64.0.1")}},
|
2022-02-14 07:54:51 -07:00
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.78.84.227"),
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
|
|
|
},
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
2022-03-01 13:01:46 -07:00
|
|
|
stripEmailDomain: true,
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
|
|
|
want: []string{"*"},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple group",
|
2023-04-26 02:58:26 -06:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{
|
|
|
|
Groups: Groups{"group:accountant": []string{"joe", "marc"}},
|
|
|
|
},
|
|
|
|
},
|
2022-02-07 08:12:05 -07:00
|
|
|
args: args{
|
2022-02-20 15:00:31 -07:00
|
|
|
alias: "group:accountant",
|
2022-02-07 08:12:05 -07:00
|
|
|
machines: []Machine{
|
2022-02-14 07:54:51 -07:00
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "marc"},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.4"),
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "mickael"},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
2022-03-01 13:01:46 -07:00
|
|
|
stripEmailDomain: true,
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
|
|
|
want: []string{"100.64.0.1", "100.64.0.2", "100.64.0.3"},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wrong group",
|
2023-04-26 02:58:26 -06:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{
|
|
|
|
Groups: Groups{"group:accountant": []string{"joe", "marc"}},
|
|
|
|
},
|
|
|
|
},
|
2022-02-07 08:12:05 -07:00
|
|
|
args: args{
|
2022-02-20 15:00:31 -07:00
|
|
|
alias: "group:hr",
|
2022-02-07 08:12:05 -07:00
|
|
|
machines: []Machine{
|
2022-02-14 07:54:51 -07:00
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "marc"},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.4"),
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "mickael"},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
2022-03-01 13:01:46 -07:00
|
|
|
stripEmailDomain: true,
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
|
|
|
want: []string{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple ipaddress",
|
2023-04-26 02:58:26 -06:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{},
|
|
|
|
},
|
2022-02-07 08:12:05 -07:00
|
|
|
args: args{
|
2022-03-01 13:01:46 -07:00
|
|
|
alias: "10.0.0.3",
|
|
|
|
machines: []Machine{},
|
|
|
|
stripEmailDomain: true,
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
|
|
|
want: []string{"10.0.0.3"},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
2023-04-16 04:26:35 -06:00
|
|
|
name: "simple host by ip passed through",
|
2023-04-26 02:58:26 -06:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{},
|
|
|
|
},
|
2022-02-07 08:12:05 -07:00
|
|
|
args: args{
|
2023-04-16 04:26:35 -06:00
|
|
|
alias: "10.0.0.1",
|
|
|
|
machines: []Machine{},
|
|
|
|
stripEmailDomain: true,
|
|
|
|
},
|
|
|
|
want: []string{"10.0.0.1"},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple host by ipv4 single ipv4",
|
2023-04-26 02:58:26 -06:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{},
|
|
|
|
},
|
2023-04-16 04:26:35 -06:00
|
|
|
args: args{
|
|
|
|
alias: "10.0.0.1",
|
|
|
|
machines: []Machine{
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netip.MustParseAddr("10.0.0.1"),
|
|
|
|
},
|
|
|
|
User: User{Name: "mickael"},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
2022-03-01 13:01:46 -07:00
|
|
|
stripEmailDomain: true,
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
2023-04-16 04:26:35 -06:00
|
|
|
want: []string{"10.0.0.1"},
|
2022-02-07 08:12:05 -07:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
2023-04-16 04:26:35 -06:00
|
|
|
name: "simple host by ipv4 single dual stack",
|
2023-04-26 02:58:26 -06:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{},
|
|
|
|
},
|
2022-02-07 08:12:05 -07:00
|
|
|
args: args{
|
2023-04-16 04:26:35 -06:00
|
|
|
alias: "10.0.0.1",
|
|
|
|
machines: []Machine{
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netip.MustParseAddr("10.0.0.1"),
|
|
|
|
netip.MustParseAddr("fd7a:115c:a1e0:ab12:4843:2222:6273:2222"),
|
|
|
|
},
|
|
|
|
User: User{Name: "mickael"},
|
|
|
|
},
|
|
|
|
},
|
2022-03-01 13:01:46 -07:00
|
|
|
stripEmailDomain: true,
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
2023-04-16 04:26:35 -06:00
|
|
|
want: []string{"10.0.0.1", "fd7a:115c:a1e0:ab12:4843:2222:6273:2222"},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple host by ipv6 single dual stack",
|
2023-04-26 02:58:26 -06:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{},
|
|
|
|
},
|
2023-04-16 04:26:35 -06:00
|
|
|
args: args{
|
|
|
|
alias: "fd7a:115c:a1e0:ab12:4843:2222:6273:2222",
|
|
|
|
machines: []Machine{
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netip.MustParseAddr("10.0.0.1"),
|
|
|
|
netip.MustParseAddr("fd7a:115c:a1e0:ab12:4843:2222:6273:2222"),
|
|
|
|
},
|
|
|
|
User: User{Name: "mickael"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
stripEmailDomain: true,
|
|
|
|
},
|
|
|
|
want: []string{"fd7a:115c:a1e0:ab12:4843:2222:6273:2222", "10.0.0.1"},
|
2022-02-07 08:12:05 -07:00
|
|
|
wantErr: false,
|
|
|
|
},
|
2023-01-30 01:39:27 -07:00
|
|
|
{
|
|
|
|
name: "simple host by hostname alias",
|
2023-04-26 02:58:26 -06:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{
|
2023-01-30 01:39:27 -07:00
|
|
|
Hosts: Hosts{
|
|
|
|
"testy": netip.MustParsePrefix("10.0.0.132/32"),
|
|
|
|
},
|
|
|
|
},
|
2023-04-26 02:58:26 -06:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
alias: "testy",
|
|
|
|
machines: []Machine{},
|
2023-01-30 01:39:27 -07:00
|
|
|
stripEmailDomain: true,
|
|
|
|
},
|
|
|
|
want: []string{"10.0.0.132/32"},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
2023-04-16 04:26:35 -06:00
|
|
|
{
|
|
|
|
name: "private network",
|
2023-04-26 02:58:26 -06:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{
|
2023-04-16 04:26:35 -06:00
|
|
|
Hosts: Hosts{
|
|
|
|
"homeNetwork": netip.MustParsePrefix("192.168.1.0/24"),
|
|
|
|
},
|
|
|
|
},
|
2023-04-26 02:58:26 -06:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
alias: "homeNetwork",
|
|
|
|
machines: []Machine{},
|
2023-04-16 04:26:35 -06:00
|
|
|
stripEmailDomain: true,
|
|
|
|
},
|
|
|
|
want: []string{"192.168.1.0/24"},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
2022-02-07 08:12:05 -07:00
|
|
|
{
|
|
|
|
name: "simple CIDR",
|
2023-04-26 02:58:26 -06:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{},
|
|
|
|
},
|
2022-02-07 08:12:05 -07:00
|
|
|
args: args{
|
2022-03-01 13:01:46 -07:00
|
|
|
alias: "10.0.0.0/16",
|
|
|
|
machines: []Machine{},
|
|
|
|
aclPolicy: ACLPolicy{},
|
|
|
|
stripEmailDomain: true,
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
|
|
|
want: []string{"10.0.0.0/16"},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple tag",
|
2023-04-26 02:58:26 -06:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{
|
|
|
|
TagOwners: TagOwners{"tag:hr-webserver": []string{"joe"}},
|
|
|
|
},
|
|
|
|
},
|
2022-02-07 08:12:05 -07:00
|
|
|
args: args{
|
2022-02-20 15:00:31 -07:00
|
|
|
alias: "tag:hr-webserver",
|
2022-02-07 08:12:05 -07:00
|
|
|
machines: []Machine{
|
2022-02-14 07:54:51 -07:00
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-03-01 09:34:35 -07:00
|
|
|
HostInfo: HostInfo{
|
|
|
|
OS: "centos",
|
|
|
|
Hostname: "foo",
|
|
|
|
RequestTags: []string{"tag:hr-webserver"},
|
|
|
|
},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-03-01 09:34:35 -07:00
|
|
|
HostInfo: HostInfo{
|
|
|
|
OS: "centos",
|
|
|
|
Hostname: "foo",
|
|
|
|
RequestTags: []string{"tag:hr-webserver"},
|
|
|
|
},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "marc"},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.4"),
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
2022-03-01 13:01:46 -07:00
|
|
|
stripEmailDomain: true,
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
|
|
|
want: []string{"100.64.0.1", "100.64.0.2"},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "No tag defined",
|
2023-04-26 02:58:26 -06:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{
|
|
|
|
Groups: Groups{"group:accountant": []string{"joe", "marc"}},
|
|
|
|
TagOwners: TagOwners{
|
|
|
|
"tag:accountant-webserver": []string{"group:accountant"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-02-07 08:12:05 -07:00
|
|
|
args: args{
|
2022-02-20 15:00:31 -07:00
|
|
|
alias: "tag:hr-webserver",
|
2022-02-07 08:12:05 -07:00
|
|
|
machines: []Machine{
|
2022-02-14 07:54:51 -07:00
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "marc"},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.4"),
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "mickael"},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
2022-03-01 13:01:46 -07:00
|
|
|
stripEmailDomain: true,
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
|
|
|
want: []string{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2022-04-15 10:01:13 -06:00
|
|
|
{
|
|
|
|
name: "Forced tag defined",
|
2023-04-26 02:58:26 -06:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{},
|
|
|
|
},
|
2022-04-15 10:01:13 -06:00
|
|
|
args: args{
|
|
|
|
alias: "tag:hr-webserver",
|
|
|
|
machines: []Machine{
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-04-15 10:01:13 -06:00
|
|
|
},
|
2023-01-17 12:36:46 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-04-15 10:01:13 -06:00
|
|
|
ForcedTags: []string{"tag:hr-webserver"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-04-15 10:01:13 -06:00
|
|
|
},
|
2023-01-17 12:36:46 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-04-15 10:01:13 -06:00
|
|
|
ForcedTags: []string{"tag:hr-webserver"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
2022-04-15 10:01:13 -06:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "marc"},
|
2022-04-15 10:01:13 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.4"),
|
2022-04-15 10:01:13 -06:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "mickael"},
|
2022-04-15 10:01:13 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
stripEmailDomain: true,
|
|
|
|
},
|
2022-06-01 07:39:59 -06:00
|
|
|
want: []string{"100.64.0.1", "100.64.0.2"},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Forced tag with legitimate tagOwner",
|
2023-04-26 02:58:26 -06:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{
|
|
|
|
TagOwners: TagOwners{
|
|
|
|
"tag:hr-webserver": []string{"joe"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-06-01 07:39:59 -06:00
|
|
|
args: args{
|
|
|
|
alias: "tag:hr-webserver",
|
|
|
|
machines: []Machine{
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-06-01 07:39:59 -06:00
|
|
|
},
|
2023-01-17 12:36:46 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-06-01 07:39:59 -06:00
|
|
|
ForcedTags: []string{"tag:hr-webserver"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-06-01 07:39:59 -06:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-06-01 07:39:59 -06:00
|
|
|
HostInfo: HostInfo{
|
|
|
|
OS: "centos",
|
|
|
|
Hostname: "foo",
|
|
|
|
RequestTags: []string{"tag:hr-webserver"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
2022-06-01 07:39:59 -06:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "marc"},
|
2022-06-01 07:39:59 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.4"),
|
2022-06-01 07:39:59 -06:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "mickael"},
|
2022-06-01 07:39:59 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
stripEmailDomain: true,
|
|
|
|
},
|
2022-04-15 10:01:13 -06:00
|
|
|
want: []string{"100.64.0.1", "100.64.0.2"},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
2022-02-07 08:12:05 -07:00
|
|
|
{
|
2023-01-17 09:43:44 -07:00
|
|
|
name: "list host in user without correctly tagged servers",
|
2023-04-26 02:58:26 -06:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{
|
|
|
|
TagOwners: TagOwners{"tag:accountant-webserver": []string{"joe"}},
|
|
|
|
},
|
|
|
|
},
|
2022-02-07 08:12:05 -07:00
|
|
|
args: args{
|
2022-02-20 15:00:31 -07:00
|
|
|
alias: "joe",
|
2022-02-07 08:12:05 -07:00
|
|
|
machines: []Machine{
|
2022-02-14 07:54:51 -07:00
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-03-01 09:34:35 -07:00
|
|
|
HostInfo: HostInfo{
|
|
|
|
OS: "centos",
|
|
|
|
Hostname: "foo",
|
|
|
|
RequestTags: []string{"tag:accountant-webserver"},
|
|
|
|
},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-03-01 09:34:35 -07:00
|
|
|
HostInfo: HostInfo{
|
|
|
|
OS: "centos",
|
|
|
|
Hostname: "foo",
|
|
|
|
RequestTags: []string{"tag:accountant-webserver"},
|
|
|
|
},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "marc"},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.4"),
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
2022-03-01 13:01:46 -07:00
|
|
|
stripEmailDomain: true,
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
|
|
|
want: []string{"100.64.0.4"},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
2022-02-14 07:26:54 -07:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2023-04-26 02:58:26 -06:00
|
|
|
got, err := test.field.pol.expandAlias(
|
2022-02-14 07:54:51 -07:00
|
|
|
test.args.machines,
|
|
|
|
test.args.alias,
|
2022-03-01 13:01:46 -07:00
|
|
|
test.args.stripEmailDomain,
|
2022-02-14 07:54:51 -07:00
|
|
|
)
|
2022-02-14 07:26:54 -07:00
|
|
|
if (err != nil) != test.wantErr {
|
|
|
|
t.Errorf("expandAlias() error = %v, wantErr %v", err, test.wantErr)
|
|
|
|
|
2022-02-07 08:12:05 -07:00
|
|
|
return
|
|
|
|
}
|
2022-02-14 07:26:54 -07:00
|
|
|
if !reflect.DeepEqual(got, test.want) {
|
|
|
|
t.Errorf("expandAlias() = %v, want %v", got, test.want)
|
2022-02-07 08:12:05 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_excludeCorrectlyTaggedNodes(t *testing.T) {
|
|
|
|
type args struct {
|
2023-04-26 02:58:26 -06:00
|
|
|
aclPolicy *ACLPolicy
|
2022-08-04 02:42:47 -06:00
|
|
|
nodes []Machine
|
2023-01-17 12:36:46 -07:00
|
|
|
user string
|
2022-08-04 02:42:47 -06:00
|
|
|
stripEmailDomain bool
|
2022-02-07 08:12:05 -07:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want []Machine
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "exclude nodes with valid tags",
|
|
|
|
args: args{
|
2023-04-26 02:58:26 -06:00
|
|
|
aclPolicy: &ACLPolicy{
|
2022-02-20 15:00:31 -07:00
|
|
|
TagOwners: TagOwners{"tag:accountant-webserver": []string{"joe"}},
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
|
|
|
nodes: []Machine{
|
2022-02-14 07:54:51 -07:00
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-03-01 09:34:35 -07:00
|
|
|
HostInfo: HostInfo{
|
|
|
|
OS: "centos",
|
|
|
|
Hostname: "foo",
|
|
|
|
RequestTags: []string{"tag:accountant-webserver"},
|
|
|
|
},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-03-01 09:34:35 -07:00
|
|
|
HostInfo: HostInfo{
|
|
|
|
OS: "centos",
|
|
|
|
Hostname: "foo",
|
|
|
|
RequestTags: []string{"tag:accountant-webserver"},
|
|
|
|
},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.4"),
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
2023-01-17 12:36:46 -07:00
|
|
|
user: "joe",
|
2022-08-04 02:42:47 -06:00
|
|
|
stripEmailDomain: true,
|
|
|
|
},
|
|
|
|
want: []Machine{
|
|
|
|
{
|
2022-09-01 16:05:43 -06:00
|
|
|
IPAddresses: MachineAddresses{netip.MustParseAddr("100.64.0.4")},
|
2023-01-17 12:36:46 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-08-04 02:42:47 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "exclude nodes with valid tags, and owner is in a group",
|
|
|
|
args: args{
|
2023-04-26 02:58:26 -06:00
|
|
|
aclPolicy: &ACLPolicy{
|
2022-08-04 02:42:47 -06:00
|
|
|
Groups: Groups{
|
|
|
|
"group:accountant": []string{"joe", "bar"},
|
|
|
|
},
|
2022-08-04 02:47:00 -06:00
|
|
|
TagOwners: TagOwners{
|
|
|
|
"tag:accountant-webserver": []string{"group:accountant"},
|
|
|
|
},
|
2022-08-04 02:42:47 -06:00
|
|
|
},
|
|
|
|
nodes: []Machine{
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-08-04 02:42:47 -06:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-08-04 02:42:47 -06:00
|
|
|
HostInfo: HostInfo{
|
|
|
|
OS: "centos",
|
|
|
|
Hostname: "foo",
|
|
|
|
RequestTags: []string{"tag:accountant-webserver"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-08-04 02:42:47 -06:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-08-04 02:42:47 -06:00
|
|
|
HostInfo: HostInfo{
|
|
|
|
OS: "centos",
|
|
|
|
Hostname: "foo",
|
|
|
|
RequestTags: []string{"tag:accountant-webserver"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.4"),
|
2022-08-04 02:42:47 -06:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-08-04 02:42:47 -06:00
|
|
|
},
|
|
|
|
},
|
2023-01-17 12:36:46 -07:00
|
|
|
user: "joe",
|
2022-08-04 02:42:47 -06:00
|
|
|
stripEmailDomain: true,
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
|
|
|
want: []Machine{
|
2022-02-14 07:54:51 -07:00
|
|
|
{
|
2022-09-01 16:05:43 -06:00
|
|
|
IPAddresses: MachineAddresses{netip.MustParseAddr("100.64.0.4")},
|
2023-01-17 12:36:46 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-04-15 10:01:13 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "exclude nodes with valid tags and with forced tags",
|
|
|
|
args: args{
|
2023-04-26 02:58:26 -06:00
|
|
|
aclPolicy: &ACLPolicy{
|
2022-04-15 10:01:13 -06:00
|
|
|
TagOwners: TagOwners{"tag:accountant-webserver": []string{"joe"}},
|
|
|
|
},
|
|
|
|
nodes: []Machine{
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-04-15 10:01:13 -06:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-04-15 10:01:13 -06:00
|
|
|
HostInfo: HostInfo{
|
|
|
|
OS: "centos",
|
|
|
|
Hostname: "foo",
|
|
|
|
RequestTags: []string{"tag:accountant-webserver"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-04-15 10:01:13 -06:00
|
|
|
},
|
2023-01-17 12:36:46 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-04-15 10:01:13 -06:00
|
|
|
ForcedTags: []string{"tag:accountant-webserver"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.4"),
|
2022-04-15 10:01:13 -06:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-04-15 10:01:13 -06:00
|
|
|
},
|
|
|
|
},
|
2023-01-17 12:36:46 -07:00
|
|
|
user: "joe",
|
2022-08-04 02:42:47 -06:00
|
|
|
stripEmailDomain: true,
|
2022-04-15 10:01:13 -06:00
|
|
|
},
|
|
|
|
want: []Machine{
|
|
|
|
{
|
2022-09-01 16:05:43 -06:00
|
|
|
IPAddresses: MachineAddresses{netip.MustParseAddr("100.64.0.4")},
|
2023-01-17 12:36:46 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "all nodes have invalid tags, don't exclude them",
|
|
|
|
args: args{
|
2023-04-26 02:58:26 -06:00
|
|
|
aclPolicy: &ACLPolicy{
|
2022-02-20 15:00:31 -07:00
|
|
|
TagOwners: TagOwners{"tag:accountant-webserver": []string{"joe"}},
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
|
|
|
nodes: []Machine{
|
2022-02-14 07:54:51 -07:00
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-03-01 09:34:35 -07:00
|
|
|
HostInfo: HostInfo{
|
|
|
|
OS: "centos",
|
|
|
|
Hostname: "hr-web1",
|
|
|
|
RequestTags: []string{"tag:hr-webserver"},
|
|
|
|
},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-03-01 09:34:35 -07:00
|
|
|
HostInfo: HostInfo{
|
|
|
|
OS: "centos",
|
|
|
|
Hostname: "hr-web2",
|
|
|
|
RequestTags: []string{"tag:hr-webserver"},
|
|
|
|
},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.4"),
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
2023-01-17 12:36:46 -07:00
|
|
|
user: "joe",
|
2022-08-04 02:42:47 -06:00
|
|
|
stripEmailDomain: true,
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
|
|
|
want: []Machine{
|
2022-02-14 07:54:51 -07:00
|
|
|
{
|
2022-02-20 15:00:31 -07:00
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-02-20 15:00:31 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-03-01 09:34:35 -07:00
|
|
|
HostInfo: HostInfo{
|
|
|
|
OS: "centos",
|
|
|
|
Hostname: "hr-web1",
|
|
|
|
RequestTags: []string{"tag:hr-webserver"},
|
|
|
|
},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
|
|
|
{
|
2022-02-20 15:00:31 -07:00
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-02-20 15:00:31 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-03-01 09:34:35 -07:00
|
|
|
HostInfo: HostInfo{
|
|
|
|
OS: "centos",
|
|
|
|
Hostname: "hr-web2",
|
|
|
|
RequestTags: []string{"tag:hr-webserver"},
|
|
|
|
},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
|
|
|
{
|
2022-02-20 15:00:31 -07:00
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-01 16:05:43 -06:00
|
|
|
netip.MustParseAddr("100.64.0.4"),
|
2022-02-20 15:00:31 -07:00
|
|
|
},
|
2023-01-17 09:43:44 -07:00
|
|
|
User: User{Name: "joe"},
|
2022-02-14 07:54:51 -07:00
|
|
|
},
|
2022-02-07 08:12:05 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-02-14 07:26:54 -07:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2022-03-02 01:15:14 -07:00
|
|
|
got := excludeCorrectlyTaggedNodes(
|
2022-02-14 07:54:51 -07:00
|
|
|
test.args.aclPolicy,
|
|
|
|
test.args.nodes,
|
2023-01-17 09:43:44 -07:00
|
|
|
test.args.user,
|
2022-08-04 02:42:47 -06:00
|
|
|
test.args.stripEmailDomain,
|
2022-02-14 07:54:51 -07:00
|
|
|
)
|
2022-02-14 07:26:54 -07:00
|
|
|
if !reflect.DeepEqual(got, test.want) {
|
|
|
|
t.Errorf("excludeCorrectlyTaggedNodes() = %v, want %v", got, test.want)
|
2022-02-07 08:12:05 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-04-03 02:08:48 -06:00
|
|
|
|
|
|
|
func Test_expandACLPeerAddr(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
srcIP string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "asterix",
|
|
|
|
args: args{
|
|
|
|
srcIP: "*",
|
|
|
|
},
|
|
|
|
want: []string{"*"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ip",
|
|
|
|
args: args{
|
|
|
|
srcIP: "10.0.0.1",
|
|
|
|
},
|
|
|
|
want: []string{"10.0.0.1"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ip/32",
|
|
|
|
args: args{
|
|
|
|
srcIP: "10.0.0.1/32",
|
|
|
|
},
|
|
|
|
want: []string{"10.0.0.1"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ip/30",
|
|
|
|
args: args{
|
|
|
|
srcIP: "10.0.0.1/30",
|
|
|
|
},
|
|
|
|
want: []string{
|
|
|
|
"10.0.0.0",
|
|
|
|
"10.0.0.1",
|
|
|
|
"10.0.0.2",
|
2023-04-04 16:41:00 -06:00
|
|
|
"10.0.0.3",
|
2023-04-03 02:08:48 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ip/28",
|
|
|
|
args: args{
|
|
|
|
srcIP: "192.168.0.128/28",
|
|
|
|
},
|
|
|
|
want: []string{
|
|
|
|
"192.168.0.128", "192.168.0.129", "192.168.0.130",
|
|
|
|
"192.168.0.131", "192.168.0.132", "192.168.0.133",
|
|
|
|
"192.168.0.134", "192.168.0.135", "192.168.0.136",
|
|
|
|
"192.168.0.137", "192.168.0.138", "192.168.0.139",
|
|
|
|
"192.168.0.140", "192.168.0.141", "192.168.0.142",
|
2023-04-04 16:41:00 -06:00
|
|
|
"192.168.0.143",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got := expandACLPeerAddr(tt.args.srcIP); !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("expandACLPeerAddr() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_expandACLPeerAddrV6(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
srcIP string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "asterix",
|
|
|
|
args: args{
|
|
|
|
srcIP: "*",
|
|
|
|
},
|
|
|
|
want: []string{"*"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ipfull",
|
|
|
|
args: args{
|
|
|
|
srcIP: "fd7a:115c:a1e0:ab12:4943:cd96:624c:3166",
|
|
|
|
},
|
|
|
|
want: []string{"fd7a:115c:a1e0:ab12:4943:cd96:624c:3166"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ipzerocompression",
|
|
|
|
args: args{
|
|
|
|
srcIP: "fd7a:115c:a1e0:ab12:4943:cd96:624c::",
|
|
|
|
},
|
|
|
|
want: []string{"fd7a:115c:a1e0:ab12:4943:cd96:624c:0"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ip/128",
|
|
|
|
args: args{
|
|
|
|
srcIP: "fd7a:115c:a1e0:ab12:4943:cd96:624c:3166/128",
|
|
|
|
},
|
|
|
|
want: []string{"fd7a:115c:a1e0:ab12:4943:cd96:624c:3166"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ip/127",
|
|
|
|
args: args{
|
|
|
|
srcIP: "fd7a:115c:a1e0:ab12:4943:cd96:624c:0000/127",
|
|
|
|
},
|
|
|
|
want: []string{
|
|
|
|
"fd7a:115c:a1e0:ab12:4943:cd96:624c:0",
|
|
|
|
"fd7a:115c:a1e0:ab12:4943:cd96:624c:1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ip/126",
|
|
|
|
args: args{
|
|
|
|
srcIP: "fd7a:115c:a1e0:ab12:4943:cd96:624c:0000/126",
|
|
|
|
},
|
|
|
|
want: []string{
|
|
|
|
"fd7a:115c:a1e0:ab12:4943:cd96:624c:0",
|
|
|
|
"fd7a:115c:a1e0:ab12:4943:cd96:624c:1",
|
|
|
|
"fd7a:115c:a1e0:ab12:4943:cd96:624c:2",
|
|
|
|
"fd7a:115c:a1e0:ab12:4943:cd96:624c:3",
|
2023-04-03 02:08:48 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got := expandACLPeerAddr(tt.args.srcIP); !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("expandACLPeerAddr() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|