2023-05-21 16:37:59 +00:00
|
|
|
package policy
|
2021-07-03 09:55:32 +00:00
|
|
|
|
|
|
|
import (
|
2022-02-05 16:18:39 +00:00
|
|
|
"errors"
|
2022-09-01 22:05:43 +00:00
|
|
|
"net/netip"
|
2022-02-07 15:12:05 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2022-02-05 16:18:39 +00:00
|
|
|
|
2023-04-26 12:04:12 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2023-05-21 16:37:59 +00:00
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
|
|
|
"github.com/juanfont/headscale/hscontrol/util"
|
2023-04-26 12:04:12 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2023-04-28 14:11:02 +00:00
|
|
|
"go4.org/netipx"
|
2021-07-03 09:55:32 +00:00
|
|
|
"gopkg.in/check.v1"
|
2022-02-07 15:12:05 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2021-07-03 09:55:32 +00:00
|
|
|
)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
func Test(t *testing.T) {
|
|
|
|
check.TestingT(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ = check.Suite(&Suite{})
|
|
|
|
|
|
|
|
type Suite struct{}
|
|
|
|
|
2021-07-03 09:55:32 +00:00
|
|
|
func (s *Suite) TestWrongPath(c *check.C) {
|
2023-05-21 16:37:59 +00:00
|
|
|
_, err := LoadACLPolicyFromPath("asdfg")
|
2021-07-03 09:55:32 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestBrokenHuJson(c *check.C) {
|
2023-05-10 08:19:16 +00:00
|
|
|
acl := []byte(`
|
|
|
|
{
|
|
|
|
`)
|
2023-05-21 16:37:59 +00:00
|
|
|
_, err := LoadACLPolicyFromBytes(acl, "hujson")
|
2021-07-03 09:55:32 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestInvalidPolicyHuson(c *check.C) {
|
2023-05-10 08:19:16 +00:00
|
|
|
acl := []byte(`
|
|
|
|
{
|
2023-05-11 07:09:18 +00:00
|
|
|
"valid_json": true,
|
|
|
|
"but_a_policy_though": false
|
2023-05-10 08:19:16 +00:00
|
|
|
}
|
|
|
|
`)
|
2023-05-21 16:37:59 +00:00
|
|
|
_, err := LoadACLPolicyFromBytes(acl, "hujson")
|
2021-07-03 09:55:32 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
2023-05-21 16:37:59 +00:00
|
|
|
c.Assert(err, check.Equals, ErrEmptyPolicy)
|
2021-07-03 09:55:32 +00:00
|
|
|
}
|
|
|
|
|
2021-07-03 15:31:32 +00:00
|
|
|
func (s *Suite) TestParseHosts(c *check.C) {
|
2021-11-15 16:16:04 +00:00
|
|
|
var hosts Hosts
|
|
|
|
err := hosts.UnmarshalJSON(
|
2021-11-13 08:36:45 +00:00
|
|
|
[]byte(
|
|
|
|
`{"example-host-1": "100.100.100.100","example-host-2": "100.100.101.100/24"}`,
|
|
|
|
),
|
|
|
|
)
|
2021-11-15 16:16:04 +00:00
|
|
|
c.Assert(hosts, check.NotNil)
|
2021-07-03 09:55:32 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-07-03 15:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestParseInvalidCIDR(c *check.C) {
|
2021-11-15 16:16:04 +00:00
|
|
|
var hosts Hosts
|
|
|
|
err := hosts.UnmarshalJSON([]byte(`{"example-host-1": "100.100.100.100/42"}`))
|
|
|
|
c.Assert(hosts, check.IsNil)
|
2021-07-03 15:31:32 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestRuleInvalidGeneration(c *check.C) {
|
2023-05-10 08:19:16 +00:00
|
|
|
acl := []byte(`
|
|
|
|
{
|
2023-05-11 07:09:18 +00:00
|
|
|
// Declare static groups of users beyond those in the identity service.
|
|
|
|
"groups": {
|
|
|
|
"group:example": [
|
|
|
|
"user1@example.com",
|
|
|
|
"user2@example.com",
|
|
|
|
],
|
|
|
|
},
|
|
|
|
// Declare hostname aliases to use in place of IP addresses or subnets.
|
|
|
|
"hosts": {
|
|
|
|
"example-host-1": "100.100.100.100",
|
|
|
|
"example-host-2": "100.100.101.100/24",
|
|
|
|
},
|
|
|
|
// Define who is allowed to use which tags.
|
|
|
|
"tagOwners": {
|
|
|
|
// Everyone in the montreal-admins or global-admins group are
|
|
|
|
// allowed to tag servers as montreal-webserver.
|
|
|
|
"tag:montreal-webserver": [
|
|
|
|
"group:montreal-admins",
|
|
|
|
"group:global-admins",
|
|
|
|
],
|
|
|
|
// Only a few admins are allowed to create API servers.
|
|
|
|
"tag:api-server": [
|
|
|
|
"group:global-admins",
|
|
|
|
"example-host-1",
|
|
|
|
],
|
|
|
|
},
|
|
|
|
// Access control lists.
|
|
|
|
"acls": [
|
|
|
|
// Engineering users, plus the president, can access port 22 (ssh)
|
|
|
|
// and port 3389 (remote desktop protocol) on all servers, and all
|
|
|
|
// ports on git-server or ci-server.
|
|
|
|
{
|
|
|
|
"action": "accept",
|
|
|
|
"src": [
|
|
|
|
"group:engineering",
|
|
|
|
"president@example.com"
|
|
|
|
],
|
|
|
|
"dst": [
|
|
|
|
"*:22,3389",
|
|
|
|
"git-server:*",
|
|
|
|
"ci-server:*"
|
|
|
|
],
|
|
|
|
},
|
|
|
|
// Allow engineer users to access any port on a device tagged with
|
|
|
|
// tag:production.
|
|
|
|
{
|
|
|
|
"action": "accept",
|
|
|
|
"src": [
|
|
|
|
"group:engineers"
|
|
|
|
],
|
|
|
|
"dst": [
|
|
|
|
"tag:production:*"
|
|
|
|
],
|
|
|
|
},
|
|
|
|
// Allow servers in the my-subnet host and 192.168.1.0/24 to access hosts
|
|
|
|
// on both networks.
|
|
|
|
{
|
|
|
|
"action": "accept",
|
|
|
|
"src": [
|
|
|
|
"my-subnet",
|
|
|
|
"192.168.1.0/24"
|
|
|
|
],
|
|
|
|
"dst": [
|
|
|
|
"my-subnet:*",
|
|
|
|
"192.168.1.0/24:*"
|
|
|
|
],
|
|
|
|
},
|
|
|
|
// Allow every user of your network to access anything on the network.
|
|
|
|
// Comment out this section if you want to define specific ACL
|
|
|
|
// restrictions above.
|
|
|
|
{
|
|
|
|
"action": "accept",
|
|
|
|
"src": [
|
|
|
|
"*"
|
|
|
|
],
|
|
|
|
"dst": [
|
|
|
|
"*:*"
|
|
|
|
],
|
|
|
|
},
|
|
|
|
// All users in Montreal are allowed to access the Montreal web
|
|
|
|
// servers.
|
|
|
|
{
|
|
|
|
"action": "accept",
|
|
|
|
"src": [
|
|
|
|
"group:montreal-users"
|
|
|
|
],
|
|
|
|
"dst": [
|
|
|
|
"tag:montreal-webserver:80,443"
|
|
|
|
],
|
|
|
|
},
|
|
|
|
// Montreal web servers are allowed to make outgoing connections to
|
|
|
|
// the API servers, but only on https port 443.
|
|
|
|
// In contrast, this doesn't grant API servers the right to initiate
|
|
|
|
// any connections.
|
|
|
|
{
|
|
|
|
"action": "accept",
|
|
|
|
"src": [
|
|
|
|
"tag:montreal-webserver"
|
|
|
|
],
|
|
|
|
"dst": [
|
|
|
|
"tag:api-server:443"
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
// Declare tests to check functionality of ACL rules
|
|
|
|
"tests": [
|
|
|
|
{
|
|
|
|
"src": "user1@example.com",
|
|
|
|
"accept": [
|
|
|
|
"example-host-1:22",
|
|
|
|
"example-host-2:80"
|
|
|
|
],
|
|
|
|
"deny": [
|
|
|
|
"exapmle-host-2:100"
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"src": "user2@example.com",
|
|
|
|
"accept": [
|
|
|
|
"100.60.3.4:22"
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
2023-05-10 08:19:16 +00:00
|
|
|
}
|
|
|
|
`)
|
2023-05-21 16:37:59 +00:00
|
|
|
pol, err := LoadACLPolicyFromBytes(acl, "hujson")
|
|
|
|
c.Assert(pol.ACLs, check.HasLen, 6)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
rules, err := pol.generateFilterRules(types.Machines{}, false)
|
2021-07-03 15:31:32 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
2023-05-21 16:37:59 +00:00
|
|
|
c.Assert(rules, check.IsNil)
|
2021-07-03 15:31:32 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 10:35:18 +00:00
|
|
|
func (s *Suite) TestBasicRule(c *check.C) {
|
2023-05-10 08:19:16 +00:00
|
|
|
acl := []byte(`
|
|
|
|
{
|
2023-05-11 07:09:18 +00:00
|
|
|
"hosts": {
|
|
|
|
"host-1": "100.100.100.100",
|
|
|
|
"subnet-1": "100.100.101.100/24",
|
|
|
|
},
|
|
|
|
|
|
|
|
"acls": [
|
|
|
|
{
|
|
|
|
"action": "accept",
|
|
|
|
"src": [
|
|
|
|
"subnet-1",
|
|
|
|
"192.168.1.0/24"
|
|
|
|
],
|
|
|
|
"dst": [
|
|
|
|
"*:22,3389",
|
|
|
|
"host-1:*",
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
2023-05-10 08:19:16 +00:00
|
|
|
}
|
|
|
|
`)
|
2023-05-21 16:37:59 +00:00
|
|
|
pol, err := LoadACLPolicyFromBytes(acl, "hujson")
|
2021-07-03 15:31:32 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
rules, err := pol.generateFilterRules(types.Machines{}, false)
|
2021-07-03 15:31:32 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-07-04 11:01:41 +00:00
|
|
|
c.Assert(rules, check.NotNil)
|
|
|
|
}
|
|
|
|
|
2022-02-21 20:48:05 +00:00
|
|
|
// TODO(kradalby): Make tests values safe, independent and descriptive.
|
2022-02-05 16:18:39 +00:00
|
|
|
func (s *Suite) TestInvalidAction(c *check.C) {
|
2023-05-21 16:37:59 +00:00
|
|
|
pol := &ACLPolicy{
|
2022-02-05 16:18:39 +00:00
|
|
|
ACLs: []ACL{
|
2022-08-04 08:47:00 +00:00
|
|
|
{
|
|
|
|
Action: "invalidAction",
|
|
|
|
Sources: []string{"*"},
|
|
|
|
Destinations: []string{"*:*"},
|
|
|
|
},
|
2022-02-05 16:18:39 +00:00
|
|
|
},
|
|
|
|
}
|
2023-05-21 16:37:59 +00:00
|
|
|
_, _, err := GenerateFilterRules(pol, types.Machines{}, false)
|
|
|
|
c.Assert(errors.Is(err, ErrInvalidAction), check.Equals, true)
|
2022-09-30 18:44:23 +00:00
|
|
|
}
|
|
|
|
|
2022-02-05 16:18:39 +00:00
|
|
|
func (s *Suite) TestInvalidGroupInGroup(c *check.C) {
|
2022-06-08 11:40:15 +00:00
|
|
|
// this ACL is wrong because the group in Sources sections doesn't exist
|
2023-05-21 16:37:59 +00:00
|
|
|
pol := &ACLPolicy{
|
2022-02-14 14:54:51 +00:00
|
|
|
Groups: Groups{
|
|
|
|
"group:test": []string{"foo"},
|
|
|
|
"group:error": []string{"foo", "group:test"},
|
|
|
|
},
|
2022-02-05 16:18:39 +00:00
|
|
|
ACLs: []ACL{
|
2022-08-04 08:47:00 +00:00
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"group:error"},
|
|
|
|
Destinations: []string{"*:*"},
|
|
|
|
},
|
2022-02-05 16:18:39 +00:00
|
|
|
},
|
|
|
|
}
|
2023-05-21 16:37:59 +00:00
|
|
|
_, _, err := GenerateFilterRules(pol, types.Machines{}, false)
|
|
|
|
c.Assert(errors.Is(err, ErrInvalidGroup), check.Equals, true)
|
2022-02-05 16:18:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestInvalidTagOwners(c *check.C) {
|
|
|
|
// this ACL is wrong because no tagOwners own the requested tag for the server
|
2023-05-21 16:37:59 +00:00
|
|
|
pol := &ACLPolicy{
|
2022-02-05 16:18:39 +00:00
|
|
|
ACLs: []ACL{
|
2022-08-04 08:47:00 +00:00
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"tag:foo"},
|
|
|
|
Destinations: []string{"*:*"},
|
|
|
|
},
|
2022-02-05 16:18:39 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
_, _, err := GenerateFilterRules(pol, types.Machines{}, false)
|
|
|
|
c.Assert(errors.Is(err, ErrInvalidTag), check.Equals, true)
|
2022-02-05 16:18:39 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 11:01:41 +00:00
|
|
|
func (s *Suite) TestPortRange(c *check.C) {
|
2023-05-10 08:19:16 +00:00
|
|
|
acl := []byte(`
|
|
|
|
{
|
2023-05-11 07:09:18 +00:00
|
|
|
"hosts": {
|
|
|
|
"host-1": "100.100.100.100",
|
|
|
|
"subnet-1": "100.100.101.100/24",
|
|
|
|
},
|
|
|
|
|
|
|
|
"acls": [
|
|
|
|
{
|
|
|
|
"action": "accept",
|
|
|
|
"src": [
|
|
|
|
"subnet-1",
|
|
|
|
],
|
|
|
|
"dst": [
|
|
|
|
"host-1:5400-5500",
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
2023-05-10 08:19:16 +00:00
|
|
|
}
|
|
|
|
`)
|
2023-05-21 16:37:59 +00:00
|
|
|
pol, err := LoadACLPolicyFromBytes(acl, "hujson")
|
2021-07-04 11:01:41 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-05-21 16:37:59 +00:00
|
|
|
c.Assert(pol, check.NotNil)
|
2021-07-04 11:01:41 +00:00
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
rules, err := pol.generateFilterRules(types.Machines{}, false)
|
2021-07-04 11:01:41 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(rules, check.NotNil)
|
|
|
|
|
2021-11-04 22:16:56 +00:00
|
|
|
c.Assert(rules, check.HasLen, 1)
|
2022-01-16 13:16:59 +00: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 11:01:41 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 16:06:25 +00:00
|
|
|
func (s *Suite) TestProtocolParsing(c *check.C) {
|
2023-05-10 08:19:16 +00:00
|
|
|
acl := []byte(`
|
|
|
|
{
|
2023-05-11 07:09:18 +00:00
|
|
|
"hosts": {
|
|
|
|
"host-1": "100.100.100.100",
|
|
|
|
"subnet-1": "100.100.101.100/24",
|
|
|
|
},
|
|
|
|
|
|
|
|
"acls": [
|
|
|
|
{
|
|
|
|
"Action": "accept",
|
|
|
|
"src": [
|
|
|
|
"*",
|
|
|
|
],
|
|
|
|
"proto": "tcp",
|
|
|
|
"dst": [
|
|
|
|
"host-1:*",
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Action": "accept",
|
|
|
|
"src": [
|
|
|
|
"*",
|
|
|
|
],
|
|
|
|
"proto": "udp",
|
|
|
|
"dst": [
|
|
|
|
"host-1:53",
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Action": "accept",
|
|
|
|
"src": [
|
|
|
|
"*",
|
|
|
|
],
|
|
|
|
"proto": "icmp",
|
|
|
|
"dst": [
|
|
|
|
"host-1:*",
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
2023-05-10 08:19:16 +00:00
|
|
|
}
|
|
|
|
`)
|
2023-05-21 16:37:59 +00:00
|
|
|
pol, err := LoadACLPolicyFromBytes(acl, "hujson")
|
2022-06-08 16:06:25 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-05-21 16:37:59 +00:00
|
|
|
c.Assert(pol, check.NotNil)
|
2022-06-08 16:06:25 +00:00
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
rules, err := pol.generateFilterRules(types.Machines{}, false)
|
2022-06-08 16:06:25 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(rules, check.NotNil)
|
|
|
|
|
|
|
|
c.Assert(rules, check.HasLen, 3)
|
2022-06-11 13:17:07 +00: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 16:06:25 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 11:01:41 +00:00
|
|
|
func (s *Suite) TestPortWildcard(c *check.C) {
|
2023-05-10 08:19:16 +00:00
|
|
|
acl := []byte(`
|
|
|
|
{
|
2023-05-11 07:09:18 +00:00
|
|
|
"hosts": {
|
|
|
|
"host-1": "100.100.100.100",
|
|
|
|
"subnet-1": "100.100.101.100/24",
|
|
|
|
},
|
|
|
|
|
|
|
|
"acls": [
|
|
|
|
{
|
|
|
|
"Action": "accept",
|
|
|
|
"src": [
|
|
|
|
"*",
|
|
|
|
],
|
|
|
|
"dst": [
|
|
|
|
"host-1:*",
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
2023-05-10 08:19:16 +00:00
|
|
|
}
|
|
|
|
`)
|
2023-05-21 16:37:59 +00:00
|
|
|
pol, err := LoadACLPolicyFromBytes(acl, "hujson")
|
2021-07-04 11:01:41 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-05-21 16:37:59 +00:00
|
|
|
c.Assert(pol, check.NotNil)
|
2021-07-04 11:01:41 +00:00
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
rules, err := pol.generateFilterRules(types.Machines{}, false)
|
2021-07-04 11:01:41 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(rules, check.NotNil)
|
|
|
|
|
2021-11-04 22:16:56 +00:00
|
|
|
c.Assert(rules, check.HasLen, 1)
|
2022-01-16 13:16:59 +00:00
|
|
|
c.Assert(rules[0].DstPorts, check.HasLen, 1)
|
2022-02-27 08:04:59 +00: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))
|
2023-04-28 14:11:02 +00:00
|
|
|
c.Assert(rules[0].SrcIPs, check.HasLen, 2)
|
|
|
|
c.Assert(rules[0].SrcIPs[0], check.Equals, "0.0.0.0/0")
|
2022-02-27 08:04:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestPortWildcardYAML(c *check.C) {
|
2023-05-11 07:09:18 +00:00
|
|
|
acl := []byte(`---
|
2023-05-10 08:19:16 +00:00
|
|
|
hosts:
|
|
|
|
host-1: 100.100.100.100/32
|
|
|
|
subnet-1: 100.100.101.100/24
|
|
|
|
acls:
|
|
|
|
- action: accept
|
|
|
|
src:
|
|
|
|
- "*"
|
|
|
|
dst:
|
2023-05-11 07:09:18 +00:00
|
|
|
- host-1:*`)
|
2023-05-21 16:37:59 +00:00
|
|
|
pol, err := LoadACLPolicyFromBytes(acl, "yaml")
|
2022-02-27 08:04:59 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-05-21 16:37:59 +00:00
|
|
|
c.Assert(pol, check.NotNil)
|
2022-02-27 08:04:59 +00:00
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
rules, err := pol.generateFilterRules(types.Machines{}, false)
|
2022-02-27 08:04:59 +00: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 13:16:59 +00: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))
|
2023-04-28 14:11:02 +00:00
|
|
|
c.Assert(rules[0].SrcIPs, check.HasLen, 2)
|
|
|
|
c.Assert(rules[0].SrcIPs[0], check.Equals, "0.0.0.0/0")
|
2021-07-04 11:01:41 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 15:12:05 +00:00
|
|
|
func Test_expandGroup(t *testing.T) {
|
2023-04-26 08:58:26 +00:00
|
|
|
type field struct {
|
|
|
|
pol ACLPolicy
|
|
|
|
}
|
2022-02-07 15:12:05 +00:00
|
|
|
type args struct {
|
2022-03-01 20:01:46 +00:00
|
|
|
group string
|
|
|
|
stripEmailDomain bool
|
2022-02-07 15:12:05 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2023-04-26 08:58:26 +00:00
|
|
|
field field
|
2022-02-07 15:12:05 +00:00
|
|
|
args args
|
|
|
|
want []string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "simple test",
|
2023-04-26 08:58:26 +00:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{
|
2022-02-14 14:54:51 +00:00
|
|
|
Groups: Groups{
|
2022-02-20 22:00:31 +00:00
|
|
|
"group:test": []string{"user1", "user2", "user3"},
|
|
|
|
"group:foo": []string{"user2", "user3"},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2023-04-26 08:58:26 +00:00
|
|
|
},
|
|
|
|
args: args{
|
2022-03-01 20:01:46 +00:00
|
|
|
group: "group:test",
|
|
|
|
stripEmailDomain: true,
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2022-02-20 22:00:31 +00:00
|
|
|
want: []string{"user1", "user2", "user3"},
|
2022-02-07 15:12:05 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "InexistantGroup",
|
2023-04-26 08:58:26 +00:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{
|
2022-02-14 14:54:51 +00:00
|
|
|
Groups: Groups{
|
2022-02-20 22:00:31 +00:00
|
|
|
"group:test": []string{"user1", "user2", "user3"},
|
|
|
|
"group:foo": []string{"user2", "user3"},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2023-04-26 08:58:26 +00:00
|
|
|
},
|
|
|
|
args: args{
|
2022-03-01 20:01:46 +00:00
|
|
|
group: "group:undefined",
|
|
|
|
stripEmailDomain: true,
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
|
|
|
want: []string{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2022-03-01 20:01:46 +00:00
|
|
|
{
|
|
|
|
name: "Expand emails in group",
|
2023-04-26 08:58:26 +00:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{
|
2022-03-01 20:01:46 +00:00
|
|
|
Groups: Groups{
|
|
|
|
"group:admin": []string{
|
|
|
|
"joe.bar@gmail.com",
|
|
|
|
"john.doe@yahoo.fr",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-04-26 08:58:26 +00:00
|
|
|
},
|
|
|
|
args: args{
|
2022-03-01 20:01:46 +00:00
|
|
|
group: "group:admin",
|
|
|
|
stripEmailDomain: true,
|
|
|
|
},
|
|
|
|
want: []string{"joe.bar", "john.doe"},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Expand emails in group",
|
2023-04-26 08:58:26 +00:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{
|
2022-03-01 20:01:46 +00:00
|
|
|
Groups: Groups{
|
|
|
|
"group:admin": []string{
|
|
|
|
"joe.bar@gmail.com",
|
|
|
|
"john.doe@yahoo.fr",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-04-26 08:58:26 +00:00
|
|
|
},
|
|
|
|
args: args{
|
2022-03-01 20:01:46 +00:00
|
|
|
group: "group:admin",
|
|
|
|
stripEmailDomain: false,
|
|
|
|
},
|
|
|
|
want: []string{"joe.bar.gmail.com", "john.doe.yahoo.fr"},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
2022-02-07 15:12:05 +00:00
|
|
|
}
|
2022-02-14 14:26:54 +00:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2023-04-26 08:58:26 +00:00
|
|
|
got, err := test.field.pol.getUsersInGroup(
|
2022-03-01 20:01:46 +00:00
|
|
|
test.args.group,
|
|
|
|
test.args.stripEmailDomain,
|
|
|
|
)
|
2022-02-14 14:26:54 +00:00
|
|
|
if (err != nil) != test.wantErr {
|
|
|
|
t.Errorf("expandGroup() error = %v, wantErr %v", err, test.wantErr)
|
|
|
|
|
2022-02-07 15:12:05 +00:00
|
|
|
return
|
|
|
|
}
|
2022-02-14 14:26:54 +00:00
|
|
|
if !reflect.DeepEqual(got, test.want) {
|
|
|
|
t.Errorf("expandGroup() = %v, want %v", got, test.want)
|
2022-02-07 15:12:05 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_expandTagOwners(t *testing.T) {
|
|
|
|
type args struct {
|
2023-04-26 08:58:26 +00:00
|
|
|
aclPolicy *ACLPolicy
|
2022-03-01 20:01:46 +00:00
|
|
|
tag string
|
|
|
|
stripEmailDomain bool
|
2022-02-07 15:12:05 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want []string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
2022-02-20 22:00:31 +00:00
|
|
|
name: "simple tag expansion",
|
2022-02-07 15:12:05 +00:00
|
|
|
args: args{
|
2023-04-26 08:58:26 +00:00
|
|
|
aclPolicy: &ACLPolicy{
|
2022-02-20 22:00:31 +00:00
|
|
|
TagOwners: TagOwners{"tag:test": []string{"user1"}},
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2022-03-01 20:01:46 +00:00
|
|
|
tag: "tag:test",
|
|
|
|
stripEmailDomain: true,
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2022-02-20 22:00:31 +00:00
|
|
|
want: []string{"user1"},
|
2022-02-07 15:12:05 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
2022-02-20 22:00:31 +00:00
|
|
|
name: "expand with tag and group",
|
2022-02-07 15:12:05 +00:00
|
|
|
args: args{
|
2023-04-26 08:58:26 +00:00
|
|
|
aclPolicy: &ACLPolicy{
|
2022-02-20 22:00:31 +00:00
|
|
|
Groups: Groups{"group:foo": []string{"user1", "user2"}},
|
2022-02-07 15:12:05 +00:00
|
|
|
TagOwners: TagOwners{"tag:test": []string{"group:foo"}},
|
|
|
|
},
|
2022-03-01 20:01:46 +00:00
|
|
|
tag: "tag:test",
|
|
|
|
stripEmailDomain: true,
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2022-02-20 22:00:31 +00:00
|
|
|
want: []string{"user1", "user2"},
|
2022-02-07 15:12:05 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
2023-01-17 16:43:44 +00:00
|
|
|
name: "expand with user and group",
|
2022-02-07 15:12:05 +00:00
|
|
|
args: args{
|
2023-04-26 08:58:26 +00:00
|
|
|
aclPolicy: &ACLPolicy{
|
2022-02-20 22:00:31 +00:00
|
|
|
Groups: Groups{"group:foo": []string{"user1", "user2"}},
|
|
|
|
TagOwners: TagOwners{"tag:test": []string{"group:foo", "user3"}},
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2022-03-01 20:01:46 +00:00
|
|
|
tag: "tag:test",
|
|
|
|
stripEmailDomain: true,
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2022-02-20 22:00:31 +00:00
|
|
|
want: []string{"user1", "user2", "user3"},
|
2022-02-07 15:12:05 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid tag",
|
|
|
|
args: args{
|
2023-04-26 08:58:26 +00:00
|
|
|
aclPolicy: &ACLPolicy{
|
2022-02-20 22:00:31 +00:00
|
|
|
TagOwners: TagOwners{"tag:foo": []string{"group:foo", "user1"}},
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2022-03-01 20:01:46 +00:00
|
|
|
tag: "tag:test",
|
|
|
|
stripEmailDomain: true,
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
|
|
|
want: []string{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid group",
|
|
|
|
args: args{
|
2023-04-26 08:58:26 +00:00
|
|
|
aclPolicy: &ACLPolicy{
|
2022-02-20 22:00:31 +00:00
|
|
|
Groups: Groups{"group:bar": []string{"user1", "user2"}},
|
|
|
|
TagOwners: TagOwners{"tag:test": []string{"group:foo", "user2"}},
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2022-03-01 20:01:46 +00:00
|
|
|
tag: "tag:test",
|
|
|
|
stripEmailDomain: true,
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
|
|
|
want: []string{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
2022-02-14 14:26:54 +00:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2023-04-26 08:58:26 +00:00
|
|
|
got, err := getTagOwners(
|
2022-03-01 20:01:46 +00:00
|
|
|
test.args.aclPolicy,
|
|
|
|
test.args.tag,
|
|
|
|
test.args.stripEmailDomain,
|
|
|
|
)
|
2022-02-14 14:26:54 +00:00
|
|
|
if (err != nil) != test.wantErr {
|
|
|
|
t.Errorf("expandTagOwners() error = %v, wantErr %v", err, test.wantErr)
|
|
|
|
|
2022-02-07 15:12:05 +00:00
|
|
|
return
|
|
|
|
}
|
2022-02-14 14:26:54 +00:00
|
|
|
if !reflect.DeepEqual(got, test.want) {
|
|
|
|
t.Errorf("expandTagOwners() = %v, want %v", got, test.want)
|
2022-02-07 15:12:05 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_expandPorts(t *testing.T) {
|
|
|
|
type args struct {
|
2022-06-08 15:43:59 +00:00
|
|
|
portsStr string
|
|
|
|
needsWildcard bool
|
2022-02-07 15:12:05 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *[]tailcfg.PortRange
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "wildcard",
|
2022-06-08 15:43:59 +00:00
|
|
|
args: args{portsStr: "*", needsWildcard: true},
|
2022-02-07 15:12:05 +00:00
|
|
|
want: &[]tailcfg.PortRange{
|
|
|
|
{First: portRangeBegin, Last: portRangeEnd},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
2022-06-08 15:43:59 +00: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 15:12:05 +00:00
|
|
|
{
|
2022-06-08 11:40:15 +00:00
|
|
|
name: "two Destinations",
|
2022-06-08 15:43:59 +00:00
|
|
|
args: args{portsStr: "80,443", needsWildcard: false},
|
2022-02-07 15:12:05 +00:00
|
|
|
want: &[]tailcfg.PortRange{
|
|
|
|
{First: 80, Last: 80},
|
|
|
|
{First: 443, Last: 443},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "a range and a port",
|
2022-06-08 15:43:59 +00:00
|
|
|
args: args{portsStr: "80-1024,443", needsWildcard: false},
|
2022-02-07 15:12:05 +00:00
|
|
|
want: &[]tailcfg.PortRange{
|
|
|
|
{First: 80, Last: 1024},
|
|
|
|
{First: 443, Last: 443},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "out of bounds",
|
2022-06-08 15:43:59 +00:00
|
|
|
args: args{portsStr: "854038", needsWildcard: false},
|
2022-02-07 15:12:05 +00:00
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wrong port",
|
2022-06-08 15:43:59 +00:00
|
|
|
args: args{portsStr: "85a38", needsWildcard: false},
|
2022-02-07 15:12:05 +00:00
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wrong port in first",
|
2022-06-08 15:43:59 +00:00
|
|
|
args: args{portsStr: "a-80", needsWildcard: false},
|
2022-02-07 15:12:05 +00:00
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wrong port in last",
|
2022-06-08 15:43:59 +00:00
|
|
|
args: args{portsStr: "80-85a38", needsWildcard: false},
|
2022-02-07 15:12:05 +00:00
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wrong port format",
|
2022-06-08 15:43:59 +00:00
|
|
|
args: args{portsStr: "80-85a38-3", needsWildcard: false},
|
2022-02-07 15:12:05 +00:00
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
2022-02-14 14:26:54 +00:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2022-06-08 15:43:59 +00:00
|
|
|
got, err := expandPorts(test.args.portsStr, test.args.needsWildcard)
|
2022-02-14 14:26:54 +00:00
|
|
|
if (err != nil) != test.wantErr {
|
|
|
|
t.Errorf("expandPorts() error = %v, wantErr %v", err, test.wantErr)
|
|
|
|
|
2022-02-07 15:12:05 +00:00
|
|
|
return
|
|
|
|
}
|
2022-02-14 14:26:54 +00:00
|
|
|
if !reflect.DeepEqual(got, test.want) {
|
|
|
|
t.Errorf("expandPorts() = %v, want %v", got, test.want)
|
2022-02-07 15:12:05 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-17 16:43:44 +00:00
|
|
|
func Test_listMachinesInUser(t *testing.T) {
|
2022-02-07 15:12:05 +00:00
|
|
|
type args struct {
|
2023-05-21 16:37:59 +00:00
|
|
|
machines types.Machines
|
2023-01-17 19:36:46 +00:00
|
|
|
user string
|
2022-02-07 15:12:05 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2023-05-21 16:37:59 +00:00
|
|
|
want types.Machines
|
2022-02-07 15:12:05 +00:00
|
|
|
}{
|
|
|
|
{
|
2023-01-17 16:43:44 +00:00
|
|
|
name: "1 machine in user",
|
2022-02-07 15:12:05 +00:00
|
|
|
args: args{
|
2023-05-21 16:37:59 +00:00
|
|
|
machines: types.Machines{
|
|
|
|
{User: types.User{Name: "joe"}},
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2023-01-17 16:43:44 +00:00
|
|
|
user: "joe",
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
want: types.Machines{
|
|
|
|
{User: types.User{Name: "joe"}},
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-01-17 16:43:44 +00:00
|
|
|
name: "3 machines, 2 in user",
|
2022-02-07 15:12:05 +00:00
|
|
|
args: args{
|
2023-05-21 16:37:59 +00:00
|
|
|
machines: types.Machines{
|
|
|
|
{ID: 1, User: types.User{Name: "joe"}},
|
|
|
|
{ID: 2, User: types.User{Name: "marc"}},
|
|
|
|
{ID: 3, User: types.User{Name: "marc"}},
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2023-01-17 16:43:44 +00:00
|
|
|
user: "marc",
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
want: types.Machines{
|
|
|
|
{ID: 2, User: types.User{Name: "marc"}},
|
|
|
|
{ID: 3, User: types.User{Name: "marc"}},
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-01-17 16:43:44 +00:00
|
|
|
name: "5 machines, 0 in user",
|
2022-02-07 15:12:05 +00:00
|
|
|
args: args{
|
2023-05-21 16:37:59 +00:00
|
|
|
machines: types.Machines{
|
|
|
|
{ID: 1, User: types.User{Name: "joe"}},
|
|
|
|
{ID: 2, User: types.User{Name: "marc"}},
|
|
|
|
{ID: 3, User: types.User{Name: "marc"}},
|
|
|
|
{ID: 4, User: types.User{Name: "marc"}},
|
|
|
|
{ID: 5, User: types.User{Name: "marc"}},
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2023-01-17 16:43:44 +00:00
|
|
|
user: "mickael",
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
want: types.Machines{},
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
|
|
|
}
|
2022-02-14 17:24:37 +00:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2023-01-17 16:43:44 +00:00
|
|
|
if got := filterMachinesByUser(test.args.machines, test.args.user); !reflect.DeepEqual(
|
2022-02-14 14:54:51 +00:00
|
|
|
got,
|
2022-02-14 17:24:37 +00:00
|
|
|
test.want,
|
2022-02-14 14:54:51 +00:00
|
|
|
) {
|
2023-01-17 16:43:44 +00:00
|
|
|
t.Errorf("listMachinesInUser() = %v, want %v", got, test.want)
|
2022-02-07 15:12:05 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_expandAlias(t *testing.T) {
|
2023-04-28 14:11:02 +00:00
|
|
|
set := func(ips []string, prefixes []string) *netipx.IPSet {
|
|
|
|
var builder netipx.IPSetBuilder
|
|
|
|
|
|
|
|
for _, ip := range ips {
|
|
|
|
builder.Add(netip.MustParseAddr(ip))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pre := range prefixes {
|
|
|
|
builder.AddPrefix(netip.MustParsePrefix(pre))
|
|
|
|
}
|
|
|
|
|
|
|
|
s, _ := builder.IPSet()
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2023-04-26 08:58:26 +00:00
|
|
|
type field struct {
|
|
|
|
pol ACLPolicy
|
|
|
|
}
|
2022-02-07 15:12:05 +00:00
|
|
|
type args struct {
|
2023-05-21 16:37:59 +00:00
|
|
|
machines types.Machines
|
2022-03-01 20:01:46 +00:00
|
|
|
aclPolicy ACLPolicy
|
|
|
|
alias string
|
|
|
|
stripEmailDomain bool
|
2022-02-07 15:12:05 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2023-04-26 08:58:26 +00:00
|
|
|
field field
|
2022-02-07 15:12:05 +00:00
|
|
|
args args
|
2023-04-28 14:11:02 +00:00
|
|
|
want *netipx.IPSet
|
2022-02-07 15:12:05 +00:00
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "wildcard",
|
2023-04-26 08:58:26 +00:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{},
|
|
|
|
},
|
2022-02-07 15:12:05 +00:00
|
|
|
args: args{
|
|
|
|
alias: "*",
|
2023-05-21 16:37:59 +00:00
|
|
|
machines: types.Machines{
|
|
|
|
{IPAddresses: types.MachineAddresses{netip.MustParseAddr("100.64.0.1")}},
|
2022-02-14 14:54:51 +00:00
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.78.84.227"),
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
|
|
|
},
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2022-03-01 20:01:46 +00:00
|
|
|
stripEmailDomain: true,
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2023-04-28 14:11:02 +00:00
|
|
|
want: set([]string{}, []string{
|
|
|
|
"0.0.0.0/0",
|
|
|
|
"::/0",
|
|
|
|
}),
|
2022-02-07 15:12:05 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple group",
|
2023-04-26 08:58:26 +00:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{
|
|
|
|
Groups: Groups{"group:accountant": []string{"joe", "marc"}},
|
|
|
|
},
|
|
|
|
},
|
2022-02-07 15:12:05 +00:00
|
|
|
args: args{
|
2022-02-20 22:00:31 +00:00
|
|
|
alias: "group:accountant",
|
2023-05-21 16:37:59 +00:00
|
|
|
machines: types.Machines{
|
2022-02-14 14:54:51 +00:00
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "marc"},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.4"),
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "mickael"},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2022-03-01 20:01:46 +00:00
|
|
|
stripEmailDomain: true,
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2023-04-28 14:11:02 +00:00
|
|
|
want: set([]string{
|
|
|
|
"100.64.0.1", "100.64.0.2", "100.64.0.3",
|
|
|
|
}, []string{}),
|
2022-02-07 15:12:05 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wrong group",
|
2023-04-26 08:58:26 +00:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{
|
|
|
|
Groups: Groups{"group:accountant": []string{"joe", "marc"}},
|
|
|
|
},
|
|
|
|
},
|
2022-02-07 15:12:05 +00:00
|
|
|
args: args{
|
2022-02-20 22:00:31 +00:00
|
|
|
alias: "group:hr",
|
2023-05-21 16:37:59 +00:00
|
|
|
machines: types.Machines{
|
2022-02-14 14:54:51 +00:00
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "marc"},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.4"),
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "mickael"},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2022-03-01 20:01:46 +00:00
|
|
|
stripEmailDomain: true,
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2023-04-28 14:11:02 +00:00
|
|
|
want: set([]string{}, []string{}),
|
2022-02-07 15:12:05 +00:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple ipaddress",
|
2023-04-26 08:58:26 +00:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{},
|
|
|
|
},
|
2022-02-07 15:12:05 +00:00
|
|
|
args: args{
|
2022-03-01 20:01:46 +00:00
|
|
|
alias: "10.0.0.3",
|
2023-05-21 16:37:59 +00:00
|
|
|
machines: types.Machines{},
|
2022-03-01 20:01:46 +00:00
|
|
|
stripEmailDomain: true,
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2023-04-28 14:11:02 +00:00
|
|
|
want: set([]string{
|
|
|
|
"10.0.0.3",
|
|
|
|
}, []string{}),
|
2022-02-07 15:12:05 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
2023-04-16 10:26:35 +00:00
|
|
|
name: "simple host by ip passed through",
|
2023-04-26 08:58:26 +00:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{},
|
|
|
|
},
|
2022-02-07 15:12:05 +00:00
|
|
|
args: args{
|
2023-04-16 10:26:35 +00:00
|
|
|
alias: "10.0.0.1",
|
2023-05-21 16:37:59 +00:00
|
|
|
machines: types.Machines{},
|
2023-04-16 10:26:35 +00:00
|
|
|
stripEmailDomain: true,
|
|
|
|
},
|
2023-04-28 14:11:02 +00:00
|
|
|
want: set([]string{
|
|
|
|
"10.0.0.1",
|
|
|
|
}, []string{}),
|
2023-04-16 10:26:35 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple host by ipv4 single ipv4",
|
2023-04-26 08:58:26 +00:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{},
|
|
|
|
},
|
2023-04-16 10:26:35 +00:00
|
|
|
args: args{
|
|
|
|
alias: "10.0.0.1",
|
2023-05-21 16:37:59 +00:00
|
|
|
machines: types.Machines{
|
2023-04-16 10:26:35 +00:00
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2023-04-16 10:26:35 +00:00
|
|
|
netip.MustParseAddr("10.0.0.1"),
|
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "mickael"},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2022-03-01 20:01:46 +00:00
|
|
|
stripEmailDomain: true,
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2023-04-28 14:11:02 +00:00
|
|
|
want: set([]string{
|
|
|
|
"10.0.0.1",
|
|
|
|
}, []string{}),
|
2022-02-07 15:12:05 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
2023-04-16 10:26:35 +00:00
|
|
|
name: "simple host by ipv4 single dual stack",
|
2023-04-26 08:58:26 +00:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{},
|
|
|
|
},
|
2022-02-07 15:12:05 +00:00
|
|
|
args: args{
|
2023-04-16 10:26:35 +00:00
|
|
|
alias: "10.0.0.1",
|
2023-05-21 16:37:59 +00:00
|
|
|
machines: types.Machines{
|
2023-04-16 10:26:35 +00:00
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2023-04-16 10:26:35 +00:00
|
|
|
netip.MustParseAddr("10.0.0.1"),
|
|
|
|
netip.MustParseAddr("fd7a:115c:a1e0:ab12:4843:2222:6273:2222"),
|
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "mickael"},
|
2023-04-16 10:26:35 +00:00
|
|
|
},
|
|
|
|
},
|
2022-03-01 20:01:46 +00:00
|
|
|
stripEmailDomain: true,
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2023-04-28 14:11:02 +00:00
|
|
|
want: set([]string{
|
|
|
|
"10.0.0.1", "fd7a:115c:a1e0:ab12:4843:2222:6273:2222",
|
|
|
|
}, []string{}),
|
2023-04-16 10:26:35 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple host by ipv6 single dual stack",
|
2023-04-26 08:58:26 +00:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{},
|
|
|
|
},
|
2023-04-16 10:26:35 +00:00
|
|
|
args: args{
|
|
|
|
alias: "fd7a:115c:a1e0:ab12:4843:2222:6273:2222",
|
2023-05-21 16:37:59 +00:00
|
|
|
machines: types.Machines{
|
2023-04-16 10:26:35 +00:00
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2023-04-16 10:26:35 +00:00
|
|
|
netip.MustParseAddr("10.0.0.1"),
|
|
|
|
netip.MustParseAddr("fd7a:115c:a1e0:ab12:4843:2222:6273:2222"),
|
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "mickael"},
|
2023-04-16 10:26:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
stripEmailDomain: true,
|
|
|
|
},
|
2023-04-28 14:11:02 +00:00
|
|
|
want: set([]string{
|
|
|
|
"fd7a:115c:a1e0:ab12:4843:2222:6273:2222", "10.0.0.1",
|
|
|
|
}, []string{}),
|
2022-02-07 15:12:05 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
2023-01-30 08:39:27 +00:00
|
|
|
{
|
|
|
|
name: "simple host by hostname alias",
|
2023-04-26 08:58:26 +00:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{
|
2023-01-30 08:39:27 +00:00
|
|
|
Hosts: Hosts{
|
|
|
|
"testy": netip.MustParsePrefix("10.0.0.132/32"),
|
|
|
|
},
|
|
|
|
},
|
2023-04-26 08:58:26 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
alias: "testy",
|
2023-05-21 16:37:59 +00:00
|
|
|
machines: types.Machines{},
|
2023-01-30 08:39:27 +00:00
|
|
|
stripEmailDomain: true,
|
|
|
|
},
|
2023-04-28 14:11:02 +00:00
|
|
|
want: set([]string{}, []string{"10.0.0.132/32"}),
|
2023-01-30 08:39:27 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
2023-04-16 10:26:35 +00:00
|
|
|
{
|
|
|
|
name: "private network",
|
2023-04-26 08:58:26 +00:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{
|
2023-04-16 10:26:35 +00:00
|
|
|
Hosts: Hosts{
|
|
|
|
"homeNetwork": netip.MustParsePrefix("192.168.1.0/24"),
|
|
|
|
},
|
|
|
|
},
|
2023-04-26 08:58:26 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
alias: "homeNetwork",
|
2023-05-21 16:37:59 +00:00
|
|
|
machines: types.Machines{},
|
2023-04-16 10:26:35 +00:00
|
|
|
stripEmailDomain: true,
|
|
|
|
},
|
2023-04-28 14:11:02 +00:00
|
|
|
want: set([]string{}, []string{"192.168.1.0/24"}),
|
2023-04-16 10:26:35 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
2022-02-07 15:12:05 +00:00
|
|
|
{
|
|
|
|
name: "simple CIDR",
|
2023-04-26 08:58:26 +00:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{},
|
|
|
|
},
|
2022-02-07 15:12:05 +00:00
|
|
|
args: args{
|
2022-03-01 20:01:46 +00:00
|
|
|
alias: "10.0.0.0/16",
|
2023-05-21 16:37:59 +00:00
|
|
|
machines: types.Machines{},
|
2022-03-01 20:01:46 +00:00
|
|
|
aclPolicy: ACLPolicy{},
|
|
|
|
stripEmailDomain: true,
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2023-04-28 14:11:02 +00:00
|
|
|
want: set([]string{}, []string{"10.0.0.0/16"}),
|
2022-02-07 15:12:05 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple tag",
|
2023-04-26 08:58:26 +00:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{
|
|
|
|
TagOwners: TagOwners{"tag:hr-webserver": []string{"joe"}},
|
|
|
|
},
|
|
|
|
},
|
2022-02-07 15:12:05 +00:00
|
|
|
args: args{
|
2022-02-20 22:00:31 +00:00
|
|
|
alias: "tag:hr-webserver",
|
2023-05-21 16:37:59 +00:00
|
|
|
machines: types.Machines{
|
2022-02-14 14:54:51 +00:00
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
|
|
|
HostInfo: types.HostInfo{
|
2022-03-01 16:34:35 +00:00
|
|
|
OS: "centos",
|
|
|
|
Hostname: "foo",
|
|
|
|
RequestTags: []string{"tag:hr-webserver"},
|
|
|
|
},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
|
|
|
HostInfo: types.HostInfo{
|
2022-03-01 16:34:35 +00:00
|
|
|
OS: "centos",
|
|
|
|
Hostname: "foo",
|
|
|
|
RequestTags: []string{"tag:hr-webserver"},
|
|
|
|
},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "marc"},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.4"),
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2022-03-01 20:01:46 +00:00
|
|
|
stripEmailDomain: true,
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2023-04-28 14:11:02 +00:00
|
|
|
want: set([]string{
|
|
|
|
"100.64.0.1", "100.64.0.2",
|
|
|
|
}, []string{}),
|
2022-02-07 15:12:05 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "No tag defined",
|
2023-04-26 08:58:26 +00:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{
|
|
|
|
Groups: Groups{"group:accountant": []string{"joe", "marc"}},
|
|
|
|
TagOwners: TagOwners{
|
|
|
|
"tag:accountant-webserver": []string{"group:accountant"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-02-07 15:12:05 +00:00
|
|
|
args: args{
|
2022-02-20 22:00:31 +00:00
|
|
|
alias: "tag:hr-webserver",
|
2023-05-21 16:37:59 +00:00
|
|
|
machines: types.Machines{
|
2022-02-14 14:54:51 +00:00
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "marc"},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.4"),
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "mickael"},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2022-03-01 20:01:46 +00:00
|
|
|
stripEmailDomain: true,
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2023-04-28 14:11:02 +00:00
|
|
|
want: set([]string{}, []string{}),
|
2022-02-07 15:12:05 +00:00
|
|
|
wantErr: true,
|
|
|
|
},
|
2022-04-15 16:01:13 +00:00
|
|
|
{
|
|
|
|
name: "Forced tag defined",
|
2023-04-26 08:58:26 +00:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{},
|
|
|
|
},
|
2022-04-15 16:01:13 +00:00
|
|
|
args: args{
|
|
|
|
alias: "tag:hr-webserver",
|
2023-05-21 16:37:59 +00:00
|
|
|
machines: types.Machines{
|
2022-04-15 16:01:13 +00:00
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-04-15 16:01:13 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
2022-04-15 16:01:13 +00:00
|
|
|
ForcedTags: []string{"tag:hr-webserver"},
|
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-04-15 16:01:13 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
2022-04-15 16:01:13 +00:00
|
|
|
ForcedTags: []string{"tag:hr-webserver"},
|
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
2022-04-15 16:01:13 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "marc"},
|
2022-04-15 16:01:13 +00:00
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.4"),
|
2022-04-15 16:01:13 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "mickael"},
|
2022-04-15 16:01:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
stripEmailDomain: true,
|
|
|
|
},
|
2023-04-28 14:11:02 +00:00
|
|
|
want: set([]string{"100.64.0.1", "100.64.0.2"}, []string{}),
|
2022-06-01 13:39:59 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Forced tag with legitimate tagOwner",
|
2023-04-26 08:58:26 +00:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{
|
|
|
|
TagOwners: TagOwners{
|
|
|
|
"tag:hr-webserver": []string{"joe"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-06-01 13:39:59 +00:00
|
|
|
args: args{
|
|
|
|
alias: "tag:hr-webserver",
|
2023-05-21 16:37:59 +00:00
|
|
|
machines: types.Machines{
|
2022-06-01 13:39:59 +00:00
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-06-01 13:39:59 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
2022-06-01 13:39:59 +00:00
|
|
|
ForcedTags: []string{"tag:hr-webserver"},
|
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-06-01 13:39:59 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
|
|
|
HostInfo: types.HostInfo{
|
2022-06-01 13:39:59 +00:00
|
|
|
OS: "centos",
|
|
|
|
Hostname: "foo",
|
|
|
|
RequestTags: []string{"tag:hr-webserver"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
2022-06-01 13:39:59 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "marc"},
|
2022-06-01 13:39:59 +00:00
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.4"),
|
2022-06-01 13:39:59 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "mickael"},
|
2022-06-01 13:39:59 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
stripEmailDomain: true,
|
|
|
|
},
|
2023-04-28 14:11:02 +00:00
|
|
|
want: set([]string{"100.64.0.1", "100.64.0.2"}, []string{}),
|
2022-04-15 16:01:13 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
2022-02-07 15:12:05 +00:00
|
|
|
{
|
2023-01-17 16:43:44 +00:00
|
|
|
name: "list host in user without correctly tagged servers",
|
2023-04-26 08:58:26 +00:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{
|
|
|
|
TagOwners: TagOwners{"tag:accountant-webserver": []string{"joe"}},
|
|
|
|
},
|
|
|
|
},
|
2022-02-07 15:12:05 +00:00
|
|
|
args: args{
|
2022-02-20 22:00:31 +00:00
|
|
|
alias: "joe",
|
2023-05-21 16:37:59 +00:00
|
|
|
machines: types.Machines{
|
2022-02-14 14:54:51 +00:00
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
|
|
|
HostInfo: types.HostInfo{
|
2022-03-01 16:34:35 +00:00
|
|
|
OS: "centos",
|
|
|
|
Hostname: "foo",
|
|
|
|
RequestTags: []string{"tag:accountant-webserver"},
|
|
|
|
},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
|
|
|
HostInfo: types.HostInfo{
|
2022-03-01 16:34:35 +00:00
|
|
|
OS: "centos",
|
|
|
|
Hostname: "foo",
|
|
|
|
RequestTags: []string{"tag:accountant-webserver"},
|
|
|
|
},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "marc"},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.4"),
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2022-03-01 20:01:46 +00:00
|
|
|
stripEmailDomain: true,
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2023-04-28 14:11:02 +00:00
|
|
|
want: set([]string{"100.64.0.4"}, []string{}),
|
2022-02-07 15:12:05 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
2022-02-14 14:26:54 +00:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2023-05-21 16:37:59 +00:00
|
|
|
got, err := test.field.pol.ExpandAlias(
|
2022-02-14 14:54:51 +00:00
|
|
|
test.args.machines,
|
|
|
|
test.args.alias,
|
2022-03-01 20:01:46 +00:00
|
|
|
test.args.stripEmailDomain,
|
2022-02-14 14:54:51 +00:00
|
|
|
)
|
2022-02-14 14:26:54 +00:00
|
|
|
if (err != nil) != test.wantErr {
|
|
|
|
t.Errorf("expandAlias() error = %v, wantErr %v", err, test.wantErr)
|
|
|
|
|
2022-02-07 15:12:05 +00:00
|
|
|
return
|
|
|
|
}
|
2023-04-28 14:11:02 +00:00
|
|
|
if diff := cmp.Diff(test.want, got); diff != "" {
|
2023-05-31 07:59:37 +00:00
|
|
|
t.Errorf("expandAlias() unexpected result (-want +got):\n%s", diff)
|
2022-02-07 15:12:05 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_excludeCorrectlyTaggedNodes(t *testing.T) {
|
|
|
|
type args struct {
|
2023-04-26 08:58:26 +00:00
|
|
|
aclPolicy *ACLPolicy
|
2023-05-21 16:37:59 +00:00
|
|
|
nodes types.Machines
|
2023-01-17 19:36:46 +00:00
|
|
|
user string
|
2022-08-04 08:42:47 +00:00
|
|
|
stripEmailDomain bool
|
2022-02-07 15:12:05 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2023-05-21 16:37:59 +00:00
|
|
|
want types.Machines
|
2022-02-07 15:12:05 +00:00
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "exclude nodes with valid tags",
|
|
|
|
args: args{
|
2023-04-26 08:58:26 +00:00
|
|
|
aclPolicy: &ACLPolicy{
|
2022-02-20 22:00:31 +00:00
|
|
|
TagOwners: TagOwners{"tag:accountant-webserver": []string{"joe"}},
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
nodes: types.Machines{
|
2022-02-14 14:54:51 +00:00
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
|
|
|
HostInfo: types.HostInfo{
|
2022-03-01 16:34:35 +00:00
|
|
|
OS: "centos",
|
|
|
|
Hostname: "foo",
|
|
|
|
RequestTags: []string{"tag:accountant-webserver"},
|
|
|
|
},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
|
|
|
HostInfo: types.HostInfo{
|
2022-03-01 16:34:35 +00:00
|
|
|
OS: "centos",
|
|
|
|
Hostname: "foo",
|
|
|
|
RequestTags: []string{"tag:accountant-webserver"},
|
|
|
|
},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.4"),
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2023-01-17 19:36:46 +00:00
|
|
|
user: "joe",
|
2022-08-04 08:42:47 +00:00
|
|
|
stripEmailDomain: true,
|
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
want: types.Machines{
|
2022-08-04 08:42:47 +00:00
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{netip.MustParseAddr("100.64.0.4")},
|
|
|
|
User: types.User{Name: "joe"},
|
2022-08-04 08:42:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "exclude nodes with valid tags, and owner is in a group",
|
|
|
|
args: args{
|
2023-04-26 08:58:26 +00:00
|
|
|
aclPolicy: &ACLPolicy{
|
2022-08-04 08:42:47 +00:00
|
|
|
Groups: Groups{
|
|
|
|
"group:accountant": []string{"joe", "bar"},
|
|
|
|
},
|
2022-08-04 08:47:00 +00:00
|
|
|
TagOwners: TagOwners{
|
|
|
|
"tag:accountant-webserver": []string{"group:accountant"},
|
|
|
|
},
|
2022-08-04 08:42:47 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
nodes: types.Machines{
|
2022-08-04 08:42:47 +00:00
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-08-04 08:42:47 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
|
|
|
HostInfo: types.HostInfo{
|
2022-08-04 08:42:47 +00:00
|
|
|
OS: "centos",
|
|
|
|
Hostname: "foo",
|
|
|
|
RequestTags: []string{"tag:accountant-webserver"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-08-04 08:42:47 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
|
|
|
HostInfo: types.HostInfo{
|
2022-08-04 08:42:47 +00:00
|
|
|
OS: "centos",
|
|
|
|
Hostname: "foo",
|
|
|
|
RequestTags: []string{"tag:accountant-webserver"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.4"),
|
2022-08-04 08:42:47 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
2022-08-04 08:42:47 +00:00
|
|
|
},
|
|
|
|
},
|
2023-01-17 19:36:46 +00:00
|
|
|
user: "joe",
|
2022-08-04 08:42:47 +00:00
|
|
|
stripEmailDomain: true,
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
want: types.Machines{
|
2022-02-14 14:54:51 +00:00
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{netip.MustParseAddr("100.64.0.4")},
|
|
|
|
User: types.User{Name: "joe"},
|
2022-04-15 16:01:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "exclude nodes with valid tags and with forced tags",
|
|
|
|
args: args{
|
2023-04-26 08:58:26 +00:00
|
|
|
aclPolicy: &ACLPolicy{
|
2022-04-15 16:01:13 +00:00
|
|
|
TagOwners: TagOwners{"tag:accountant-webserver": []string{"joe"}},
|
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
nodes: types.Machines{
|
2022-04-15 16:01:13 +00:00
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-04-15 16:01:13 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
|
|
|
HostInfo: types.HostInfo{
|
2022-04-15 16:01:13 +00:00
|
|
|
OS: "centos",
|
|
|
|
Hostname: "foo",
|
|
|
|
RequestTags: []string{"tag:accountant-webserver"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-04-15 16:01:13 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
2022-04-15 16:01:13 +00:00
|
|
|
ForcedTags: []string{"tag:accountant-webserver"},
|
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.4"),
|
2022-04-15 16:01:13 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
2022-04-15 16:01:13 +00:00
|
|
|
},
|
|
|
|
},
|
2023-01-17 19:36:46 +00:00
|
|
|
user: "joe",
|
2022-08-04 08:42:47 +00:00
|
|
|
stripEmailDomain: true,
|
2022-04-15 16:01:13 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
want: types.Machines{
|
2022-04-15 16:01:13 +00:00
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{netip.MustParseAddr("100.64.0.4")},
|
|
|
|
User: types.User{Name: "joe"},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "all nodes have invalid tags, don't exclude them",
|
|
|
|
args: args{
|
2023-04-26 08:58:26 +00:00
|
|
|
aclPolicy: &ACLPolicy{
|
2022-02-20 22:00:31 +00:00
|
|
|
TagOwners: TagOwners{"tag:accountant-webserver": []string{"joe"}},
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
nodes: types.Machines{
|
2022-02-14 14:54:51 +00:00
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
|
|
|
HostInfo: types.HostInfo{
|
2022-03-01 16:34:35 +00:00
|
|
|
OS: "centos",
|
|
|
|
Hostname: "hr-web1",
|
|
|
|
RequestTags: []string{"tag:hr-webserver"},
|
|
|
|
},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
|
|
|
HostInfo: types.HostInfo{
|
2022-03-01 16:34:35 +00:00
|
|
|
OS: "centos",
|
|
|
|
Hostname: "hr-web2",
|
|
|
|
RequestTags: []string{"tag:hr-webserver"},
|
|
|
|
},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.4"),
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2023-01-17 19:36:46 +00:00
|
|
|
user: "joe",
|
2022-08-04 08:42:47 +00:00
|
|
|
stripEmailDomain: true,
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
want: types.Machines{
|
2022-02-14 14:54:51 +00:00
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-02-20 22:00:31 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
|
|
|
HostInfo: types.HostInfo{
|
2022-03-01 16:34:35 +00:00
|
|
|
OS: "centos",
|
|
|
|
Hostname: "hr-web1",
|
|
|
|
RequestTags: []string{"tag:hr-webserver"},
|
|
|
|
},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-02-20 22:00:31 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
|
|
|
HostInfo: types.HostInfo{
|
2022-03-01 16:34:35 +00:00
|
|
|
OS: "centos",
|
|
|
|
Hostname: "hr-web2",
|
|
|
|
RequestTags: []string{"tag:hr-webserver"},
|
|
|
|
},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2022-09-01 22:05:43 +00:00
|
|
|
netip.MustParseAddr("100.64.0.4"),
|
2022-02-20 22:00:31 +00:00
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "joe"},
|
2022-02-14 14:54:51 +00:00
|
|
|
},
|
2022-02-07 15:12:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-02-14 14:26:54 +00:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2022-03-02 08:15:14 +00:00
|
|
|
got := excludeCorrectlyTaggedNodes(
|
2022-02-14 14:54:51 +00:00
|
|
|
test.args.aclPolicy,
|
|
|
|
test.args.nodes,
|
2023-01-17 16:43:44 +00:00
|
|
|
test.args.user,
|
2022-08-04 08:42:47 +00:00
|
|
|
test.args.stripEmailDomain,
|
2022-02-14 14:54:51 +00:00
|
|
|
)
|
2022-02-14 14:26:54 +00:00
|
|
|
if !reflect.DeepEqual(got, test.want) {
|
|
|
|
t.Errorf("excludeCorrectlyTaggedNodes() = %v, want %v", got, test.want)
|
2022-02-07 15:12:05 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-04-03 08:08:48 +00:00
|
|
|
|
2023-04-26 12:04:12 +00:00
|
|
|
func TestACLPolicy_generateFilterRules(t *testing.T) {
|
|
|
|
type field struct {
|
|
|
|
pol ACLPolicy
|
|
|
|
}
|
|
|
|
type args struct {
|
2023-05-21 16:37:59 +00:00
|
|
|
machines types.Machines
|
2023-04-26 12:04:12 +00:00
|
|
|
stripEmailDomain bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
field field
|
|
|
|
args args
|
|
|
|
want []tailcfg.FilterRule
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no-policy",
|
|
|
|
field: field{},
|
|
|
|
args: args{},
|
|
|
|
want: []tailcfg.FilterRule{},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
2023-04-26 15:27:51 +00:00
|
|
|
name: "allow-all",
|
2023-04-26 12:04:12 +00:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{
|
|
|
|
ACLs: []ACL{
|
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"*"},
|
|
|
|
Destinations: []string{"*:*"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
2023-05-21 16:37:59 +00:00
|
|
|
machines: types.Machines{},
|
2023-04-26 12:04:12 +00:00
|
|
|
stripEmailDomain: true,
|
|
|
|
},
|
|
|
|
want: []tailcfg.FilterRule{
|
|
|
|
{
|
2023-04-28 14:11:02 +00:00
|
|
|
SrcIPs: []string{"0.0.0.0/0", "::/0"},
|
2023-04-26 12:04:12 +00:00
|
|
|
DstPorts: []tailcfg.NetPortRange{
|
|
|
|
{
|
2023-04-28 14:11:02 +00:00
|
|
|
IP: "0.0.0.0/0",
|
|
|
|
Ports: tailcfg.PortRange{
|
|
|
|
First: 0,
|
|
|
|
Last: 65535,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IP: "::/0",
|
2023-04-26 12:04:12 +00:00
|
|
|
Ports: tailcfg.PortRange{
|
|
|
|
First: 0,
|
|
|
|
Last: 65535,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
2023-04-26 15:27:51 +00:00
|
|
|
name: "host1-can-reach-host2",
|
2023-04-26 12:04:12 +00:00
|
|
|
field: field{
|
|
|
|
pol: ACLPolicy{
|
|
|
|
ACLs: []ACL{
|
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"100.64.0.1"},
|
|
|
|
Destinations: []string{"100.64.0.2:*"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
2023-05-21 16:37:59 +00:00
|
|
|
machines: types.Machines{
|
2023-04-26 12:04:12 +00:00
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2023-04-26 15:27:51 +00:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2023-04-26 12:04:12 +00:00
|
|
|
netip.MustParseAddr("fd7a:115c:a1e0:ab12:4843:2222:6273:2221"),
|
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "mickael"},
|
2023-04-26 12:04:12 +00:00
|
|
|
},
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
IPAddresses: types.MachineAddresses{
|
2023-04-26 15:27:51 +00:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2023-04-26 12:04:12 +00:00
|
|
|
netip.MustParseAddr("fd7a:115c:a1e0:ab12:4843:2222:6273:2222"),
|
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
User: types.User{Name: "mickael"},
|
2023-04-26 12:04:12 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
stripEmailDomain: true,
|
|
|
|
},
|
|
|
|
want: []tailcfg.FilterRule{
|
|
|
|
{
|
2023-05-21 16:37:59 +00:00
|
|
|
SrcIPs: []string{
|
|
|
|
"100.64.0.1/32",
|
|
|
|
"fd7a:115c:a1e0:ab12:4843:2222:6273:2221/128",
|
|
|
|
},
|
2023-04-26 12:04:12 +00:00
|
|
|
DstPorts: []tailcfg.NetPortRange{
|
|
|
|
{
|
2023-04-28 14:11:02 +00:00
|
|
|
IP: "100.64.0.2/32",
|
2023-04-26 12:04:12 +00:00
|
|
|
Ports: tailcfg.PortRange{
|
|
|
|
First: 0,
|
|
|
|
Last: 65535,
|
|
|
|
},
|
|
|
|
},
|
2023-04-26 15:27:51 +00:00
|
|
|
{
|
2023-04-28 14:11:02 +00:00
|
|
|
IP: "fd7a:115c:a1e0:ab12:4843:2222:6273:2222/128",
|
2023-04-26 15:27:51 +00:00
|
|
|
Ports: tailcfg.PortRange{
|
|
|
|
First: 0,
|
|
|
|
Last: 65535,
|
|
|
|
},
|
|
|
|
},
|
2023-04-26 12:04:12 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := tt.field.pol.generateFilterRules(
|
|
|
|
tt.args.machines,
|
|
|
|
tt.args.stripEmailDomain,
|
|
|
|
)
|
|
|
|
if (err != nil) != tt.wantErr {
|
2023-05-21 16:37:59 +00:00
|
|
|
t.Errorf("ACLgenerateFilterRules() error = %v, wantErr %v", err, tt.wantErr)
|
2023-04-26 12:04:12 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff := cmp.Diff(tt.want, got); diff != "" {
|
|
|
|
log.Trace().Interface("got", got).Msg("result")
|
2023-05-31 07:59:37 +00:00
|
|
|
t.Errorf("ACLgenerateFilterRules() unexpected result (-want +got):\n%s", diff)
|
2023-05-21 16:37:59 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_getTags(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
aclPolicy *ACLPolicy
|
|
|
|
machine types.Machine
|
|
|
|
stripEmailDomain bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantInvalid []string
|
|
|
|
wantValid []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "valid tag one machine",
|
|
|
|
args: args{
|
|
|
|
aclPolicy: &ACLPolicy{
|
|
|
|
TagOwners: TagOwners{
|
|
|
|
"tag:valid": []string{"joe"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
machine: types.Machine{
|
|
|
|
User: types.User{
|
|
|
|
Name: "joe",
|
|
|
|
},
|
|
|
|
HostInfo: types.HostInfo{
|
|
|
|
RequestTags: []string{"tag:valid"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
stripEmailDomain: false,
|
|
|
|
},
|
|
|
|
wantValid: []string{"tag:valid"},
|
|
|
|
wantInvalid: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid tag and valid tag one machine",
|
|
|
|
args: args{
|
|
|
|
aclPolicy: &ACLPolicy{
|
|
|
|
TagOwners: TagOwners{
|
|
|
|
"tag:valid": []string{"joe"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
machine: types.Machine{
|
|
|
|
User: types.User{
|
|
|
|
Name: "joe",
|
|
|
|
},
|
|
|
|
HostInfo: types.HostInfo{
|
|
|
|
RequestTags: []string{"tag:valid", "tag:invalid"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
stripEmailDomain: false,
|
|
|
|
},
|
|
|
|
wantValid: []string{"tag:valid"},
|
|
|
|
wantInvalid: []string{"tag:invalid"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple invalid and identical tags, should return only one invalid tag",
|
|
|
|
args: args{
|
|
|
|
aclPolicy: &ACLPolicy{
|
|
|
|
TagOwners: TagOwners{
|
|
|
|
"tag:valid": []string{"joe"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
machine: types.Machine{
|
|
|
|
User: types.User{
|
|
|
|
Name: "joe",
|
|
|
|
},
|
|
|
|
HostInfo: types.HostInfo{
|
|
|
|
RequestTags: []string{
|
|
|
|
"tag:invalid",
|
|
|
|
"tag:valid",
|
|
|
|
"tag:invalid",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
stripEmailDomain: false,
|
|
|
|
},
|
|
|
|
wantValid: []string{"tag:valid"},
|
|
|
|
wantInvalid: []string{"tag:invalid"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "only invalid tags",
|
|
|
|
args: args{
|
|
|
|
aclPolicy: &ACLPolicy{
|
|
|
|
TagOwners: TagOwners{
|
|
|
|
"tag:valid": []string{"joe"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
machine: types.Machine{
|
|
|
|
User: types.User{
|
|
|
|
Name: "joe",
|
|
|
|
},
|
|
|
|
HostInfo: types.HostInfo{
|
|
|
|
RequestTags: []string{"tag:invalid", "very-invalid"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
stripEmailDomain: false,
|
|
|
|
},
|
|
|
|
wantValid: nil,
|
|
|
|
wantInvalid: []string{"tag:invalid", "very-invalid"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty ACLPolicy should return empty tags and should not panic",
|
|
|
|
args: args{
|
|
|
|
aclPolicy: &ACLPolicy{},
|
|
|
|
machine: types.Machine{
|
|
|
|
User: types.User{
|
|
|
|
Name: "joe",
|
|
|
|
},
|
|
|
|
HostInfo: types.HostInfo{
|
|
|
|
RequestTags: []string{"tag:invalid", "very-invalid"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
stripEmailDomain: false,
|
|
|
|
},
|
|
|
|
wantValid: nil,
|
|
|
|
wantInvalid: []string{"tag:invalid", "very-invalid"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
gotValid, gotInvalid := test.args.aclPolicy.GetTagsOfMachine(
|
|
|
|
test.args.machine,
|
|
|
|
test.args.stripEmailDomain,
|
|
|
|
)
|
|
|
|
for _, valid := range gotValid {
|
|
|
|
if !util.StringOrPrefixListContains(test.wantValid, valid) {
|
|
|
|
t.Errorf(
|
|
|
|
"valids: getTags() = %v, want %v",
|
|
|
|
gotValid,
|
|
|
|
test.wantValid,
|
|
|
|
)
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, invalid := range gotInvalid {
|
|
|
|
if !util.StringOrPrefixListContains(test.wantInvalid, invalid) {
|
|
|
|
t.Errorf(
|
|
|
|
"invalids: getTags() = %v, want %v",
|
|
|
|
gotInvalid,
|
|
|
|
test.wantInvalid,
|
|
|
|
)
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_getFilteredByACLPeers(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
machines types.Machines
|
|
|
|
rules []tailcfg.FilterRule
|
|
|
|
machine *types.Machine
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want types.Machines
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "all hosts can talk to each other",
|
|
|
|
args: args{
|
|
|
|
machines: types.Machines{ // list of all machines in the database
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "joe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 2,
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "marc"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 3,
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "mickael"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
rules: []tailcfg.FilterRule{ // list of all ACLRules registered
|
|
|
|
{
|
|
|
|
SrcIPs: []string{"100.64.0.1", "100.64.0.2", "100.64.0.3"},
|
|
|
|
DstPorts: []tailcfg.NetPortRange{
|
|
|
|
{IP: "*"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
machine: &types.Machine{ // current machine
|
|
|
|
ID: 1,
|
|
|
|
IPAddresses: types.MachineAddresses{netip.MustParseAddr("100.64.0.1")},
|
|
|
|
User: types.User{Name: "joe"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: types.Machines{
|
|
|
|
{
|
|
|
|
ID: 2,
|
|
|
|
IPAddresses: types.MachineAddresses{netip.MustParseAddr("100.64.0.2")},
|
|
|
|
User: types.User{Name: "marc"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 3,
|
|
|
|
IPAddresses: types.MachineAddresses{netip.MustParseAddr("100.64.0.3")},
|
|
|
|
User: types.User{Name: "mickael"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "One host can talk to another, but not all hosts",
|
|
|
|
args: args{
|
|
|
|
machines: types.Machines{ // list of all machines in the database
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "joe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 2,
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "marc"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 3,
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "mickael"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
rules: []tailcfg.FilterRule{ // list of all ACLRules registered
|
|
|
|
{
|
|
|
|
SrcIPs: []string{"100.64.0.1", "100.64.0.2", "100.64.0.3"},
|
|
|
|
DstPorts: []tailcfg.NetPortRange{
|
|
|
|
{IP: "100.64.0.2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
machine: &types.Machine{ // current machine
|
|
|
|
ID: 1,
|
|
|
|
IPAddresses: types.MachineAddresses{netip.MustParseAddr("100.64.0.1")},
|
|
|
|
User: types.User{Name: "joe"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: types.Machines{
|
|
|
|
{
|
|
|
|
ID: 2,
|
|
|
|
IPAddresses: types.MachineAddresses{netip.MustParseAddr("100.64.0.2")},
|
|
|
|
User: types.User{Name: "marc"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "host cannot directly talk to destination, but return path is authorized",
|
|
|
|
args: args{
|
|
|
|
machines: types.Machines{ // list of all machines in the database
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "joe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 2,
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "marc"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 3,
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "mickael"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
rules: []tailcfg.FilterRule{ // list of all ACLRules registered
|
|
|
|
{
|
|
|
|
SrcIPs: []string{"100.64.0.3"},
|
|
|
|
DstPorts: []tailcfg.NetPortRange{
|
|
|
|
{IP: "100.64.0.2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
machine: &types.Machine{ // current machine
|
|
|
|
ID: 2,
|
|
|
|
IPAddresses: types.MachineAddresses{netip.MustParseAddr("100.64.0.2")},
|
|
|
|
User: types.User{Name: "marc"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: types.Machines{
|
|
|
|
{
|
|
|
|
ID: 3,
|
|
|
|
IPAddresses: types.MachineAddresses{netip.MustParseAddr("100.64.0.3")},
|
|
|
|
User: types.User{Name: "mickael"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "rules allows all hosts to reach one destination",
|
|
|
|
args: args{
|
|
|
|
machines: types.Machines{ // list of all machines in the database
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "joe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 2,
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "marc"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 3,
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "mickael"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
rules: []tailcfg.FilterRule{ // list of all ACLRules registered
|
|
|
|
{
|
|
|
|
SrcIPs: []string{"*"},
|
|
|
|
DstPorts: []tailcfg.NetPortRange{
|
|
|
|
{IP: "100.64.0.2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
machine: &types.Machine{ // current machine
|
|
|
|
ID: 1,
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "joe"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: types.Machines{
|
|
|
|
{
|
|
|
|
ID: 2,
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "marc"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "rules allows all hosts to reach one destination, destination can reach all hosts",
|
|
|
|
args: args{
|
|
|
|
machines: types.Machines{ // list of all machines in the database
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "joe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 2,
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "marc"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 3,
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "mickael"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
rules: []tailcfg.FilterRule{ // list of all ACLRules registered
|
|
|
|
{
|
|
|
|
SrcIPs: []string{"*"},
|
|
|
|
DstPorts: []tailcfg.NetPortRange{
|
|
|
|
{IP: "100.64.0.2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
machine: &types.Machine{ // current machine
|
|
|
|
ID: 2,
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "marc"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: types.Machines{
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "joe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 3,
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "mickael"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "rule allows all hosts to reach all destinations",
|
|
|
|
args: args{
|
|
|
|
machines: types.Machines{ // list of all machines in the database
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "joe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 2,
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "marc"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 3,
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "mickael"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
rules: []tailcfg.FilterRule{ // list of all ACLRules registered
|
|
|
|
{
|
|
|
|
SrcIPs: []string{"*"},
|
|
|
|
DstPorts: []tailcfg.NetPortRange{
|
|
|
|
{IP: "*"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
machine: &types.Machine{ // current machine
|
|
|
|
ID: 2,
|
|
|
|
IPAddresses: types.MachineAddresses{netip.MustParseAddr("100.64.0.2")},
|
|
|
|
User: types.User{Name: "marc"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: types.Machines{
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "joe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 3,
|
|
|
|
IPAddresses: types.MachineAddresses{netip.MustParseAddr("100.64.0.3")},
|
|
|
|
User: types.User{Name: "mickael"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "without rule all communications are forbidden",
|
|
|
|
args: args{
|
|
|
|
machines: types.Machines{ // list of all machines in the database
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "joe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 2,
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "marc"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 3,
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "mickael"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
rules: []tailcfg.FilterRule{ // list of all ACLRules registered
|
|
|
|
},
|
|
|
|
machine: &types.Machine{ // current machine
|
|
|
|
ID: 2,
|
|
|
|
IPAddresses: types.MachineAddresses{netip.MustParseAddr("100.64.0.2")},
|
|
|
|
User: types.User{Name: "marc"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: types.Machines{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Investigating 699
|
|
|
|
// Found some machines: [ts-head-8w6paa ts-unstable-lys2ib ts-head-upcrmb ts-unstable-rlwpvr] machine=ts-head-8w6paa
|
|
|
|
// ACL rules generated ACL=[{"DstPorts":[{"Bits":null,"IP":"*","Ports":{"First":0,"Last":65535}}],"SrcIPs":["fd7a:115c:a1e0::3","100.64.0.3","fd7a:115c:a1e0::4","100.64.0.4"]}]
|
|
|
|
// ACL Cache Map={"100.64.0.3":{"*":{}},"100.64.0.4":{"*":{}},"fd7a:115c:a1e0::3":{"*":{}},"fd7a:115c:a1e0::4":{"*":{}}}
|
|
|
|
name: "issue-699-broken-star",
|
|
|
|
args: args{
|
|
|
|
machines: types.Machines{ //
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
Hostname: "ts-head-upcrmb",
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
|
|
|
netip.MustParseAddr("fd7a:115c:a1e0::3"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "user1"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 2,
|
|
|
|
Hostname: "ts-unstable-rlwpvr",
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.4"),
|
|
|
|
netip.MustParseAddr("fd7a:115c:a1e0::4"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "user1"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 3,
|
|
|
|
Hostname: "ts-head-8w6paa",
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
|
|
|
netip.MustParseAddr("fd7a:115c:a1e0::1"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "user2"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 4,
|
|
|
|
Hostname: "ts-unstable-lys2ib",
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
|
|
|
netip.MustParseAddr("fd7a:115c:a1e0::2"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "user2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
rules: []tailcfg.FilterRule{ // list of all ACLRules registered
|
|
|
|
{
|
|
|
|
DstPorts: []tailcfg.NetPortRange{
|
|
|
|
{
|
|
|
|
IP: "*",
|
|
|
|
Ports: tailcfg.PortRange{First: 0, Last: 65535},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
SrcIPs: []string{
|
|
|
|
"fd7a:115c:a1e0::3", "100.64.0.3",
|
|
|
|
"fd7a:115c:a1e0::4", "100.64.0.4",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
machine: &types.Machine{ // current machine
|
|
|
|
ID: 3,
|
|
|
|
Hostname: "ts-head-8w6paa",
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
|
|
|
netip.MustParseAddr("fd7a:115c:a1e0::1"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "user2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: types.Machines{
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
Hostname: "ts-head-upcrmb",
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
|
|
|
netip.MustParseAddr("fd7a:115c:a1e0::3"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "user1"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 2,
|
|
|
|
Hostname: "ts-unstable-rlwpvr",
|
|
|
|
IPAddresses: types.MachineAddresses{
|
|
|
|
netip.MustParseAddr("100.64.0.4"),
|
|
|
|
netip.MustParseAddr("fd7a:115c:a1e0::4"),
|
|
|
|
},
|
|
|
|
User: types.User{Name: "user1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got := FilterMachinesByACL(
|
|
|
|
tt.args.machine,
|
|
|
|
tt.args.machines,
|
|
|
|
tt.args.rules,
|
|
|
|
)
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("filterMachinesByACL() = %v, want %v", got, tt.want)
|
2023-04-26 12:04:12 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|