1
0
mirror of https://github.com/juanfont/headscale.git synced 2024-12-20 19:09:07 +01:00
juanfont.headscale/acls_types.go

136 lines
3.7 KiB
Go
Raw Normal View History

2021-07-03 11:55:32 +02:00
package headscale
import (
2021-11-05 08:24:00 +01:00
"encoding/json"
2022-09-02 00:05:43 +02:00
"net/netip"
2021-07-03 11:55:32 +02:00
"strings"
2021-07-03 17:31:32 +02:00
"github.com/tailscale/hujson"
2022-02-27 09:04:48 +01:00
"gopkg.in/yaml.v3"
2021-07-03 11:55:32 +02:00
)
2021-11-13 09:39:04 +01:00
// ACLPolicy represents a Tailscale ACL Policy.
2021-07-03 11:55:32 +02:00
type ACLPolicy struct {
Groups Groups `json:"groups" yaml:"groups"`
Hosts Hosts `json:"hosts" yaml:"hosts"`
TagOwners TagOwners `json:"tagOwners" yaml:"tagOwners"`
ACLs []ACL `json:"acls" yaml:"acls"`
Tests []ACLTest `json:"tests" yaml:"tests"`
AutoApprovers AutoApprovers `json:"autoApprovers" yaml:"autoApprovers"`
2021-07-03 11:55:32 +02:00
}
2021-11-13 09:39:04 +01:00
// ACL is a basic rule for the ACL Policy.
2021-07-03 11:55:32 +02:00
type ACL struct {
Action string `json:"action" yaml:"action"`
2022-08-04 10:47:00 +02:00
Protocol string `json:"proto" yaml:"proto"`
Sources []string `json:"src" yaml:"src"`
Destinations []string `json:"dst" yaml:"dst"`
2021-07-03 11:55:32 +02:00
}
2021-11-13 09:39:04 +01:00
// Groups references a series of alias in the ACL rules.
2021-07-03 11:55:32 +02:00
type Groups map[string][]string
2021-11-13 09:39:04 +01:00
// Hosts are alias for IP addresses or subnets.
2022-09-02 00:05:43 +02:00
type Hosts map[string]netip.Prefix
2021-07-03 11:55:32 +02:00
2021-11-13 09:39:04 +01:00
// TagOwners specify what users (namespaces?) are allow to use certain tags.
2021-07-03 17:31:32 +02:00
type TagOwners map[string][]string
2021-07-03 11:55:32 +02:00
2021-11-13 09:39:04 +01:00
// ACLTest is not implemented, but should be use to check if a certain rule is allowed.
2021-07-03 11:55:32 +02:00
type ACLTest struct {
2022-08-04 10:47:00 +02:00
Source string `json:"src" yaml:"src"`
Accept []string `json:"accept" yaml:"accept"`
Deny []string `json:"deny,omitempty" yaml:"deny,omitempty"`
2021-07-03 11:55:32 +02:00
}
// AutoApprovers specify which users (namespaces?), groups or tags have their advertised routes
2022-09-04 22:40:08 +02:00
// or exit node status automatically enabled.
type AutoApprovers struct {
Routes map[string][]string `json:"routes" yaml:"routes"`
ExitNode []string `json:"exitNode" yaml:"exitNode"`
}
2022-09-03 23:46:14 +02:00
// UnmarshalJSON allows to parse the Hosts directly into netip objects.
func (hosts *Hosts) UnmarshalJSON(data []byte) error {
newHosts := Hosts{}
hostIPPrefixMap := make(map[string]string)
2021-11-05 08:24:00 +01:00
ast, err := hujson.Parse(data)
if err != nil {
return err
}
ast.Standardize()
data = ast.Pack()
err = json.Unmarshal(data, &hostIPPrefixMap)
2021-07-03 17:31:32 +02:00
if err != nil {
return err
2021-07-03 11:55:32 +02:00
}
for host, prefixStr := range hostIPPrefixMap {
if !strings.Contains(prefixStr, "/") {
prefixStr += "/32"
2021-07-03 11:55:32 +02:00
}
2022-09-02 00:05:43 +02:00
prefix, err := netip.ParsePrefix(prefixStr)
2021-07-03 11:55:32 +02:00
if err != nil {
2021-07-03 17:31:32 +02:00
return err
2021-07-03 11:55:32 +02:00
}
newHosts[host] = prefix
2021-07-03 11:55:32 +02:00
}
*hosts = newHosts
2021-11-14 16:46:09 +01:00
2021-07-03 17:31:32 +02:00
return nil
}
2022-09-03 23:46:14 +02:00
// UnmarshalYAML allows to parse the Hosts directly into netip objects.
2022-02-27 09:04:48 +01:00
func (hosts *Hosts) UnmarshalYAML(data []byte) error {
newHosts := Hosts{}
hostIPPrefixMap := make(map[string]string)
err := yaml.Unmarshal(data, &hostIPPrefixMap)
if err != nil {
return err
}
for host, prefixStr := range hostIPPrefixMap {
2022-09-02 00:05:43 +02:00
prefix, err := netip.ParsePrefix(prefixStr)
2022-02-27 09:04:48 +01:00
if err != nil {
return err
}
newHosts[host] = prefix
}
*hosts = newHosts
return nil
}
2021-11-13 09:39:04 +01:00
// IsZero is perhaps a bit naive here.
func (policy ACLPolicy) IsZero() bool {
if len(policy.Groups) == 0 && len(policy.Hosts) == 0 && len(policy.ACLs) == 0 {
2021-07-03 17:31:32 +02:00
return true
}
2021-11-14 16:46:09 +01:00
2021-07-03 17:31:32 +02:00
return false
2021-07-03 11:55:32 +02:00
}
2022-09-07 13:39:56 +02:00
// Returns the list of autoApproving namespaces, groups or tags for a given IPPrefix.
func (autoApprovers *AutoApprovers) GetRouteApprovers(
2022-09-05 01:33:53 +02:00
prefix netip.Prefix,
) ([]string, error) {
if prefix.Bits() == 0 {
return autoApprovers.ExitNode, nil // 0.0.0.0/0, ::/0 or equivalent
}
approverAliases := []string{}
for autoApprovedPrefix, autoApproverAliases := range autoApprovers.Routes {
2022-09-05 01:33:53 +02:00
autoApprovedPrefix, err := netip.ParsePrefix(autoApprovedPrefix)
if err != nil {
return nil, err
}
if prefix.Bits() >= autoApprovedPrefix.Bits() &&
2022-09-05 01:33:53 +02:00
autoApprovedPrefix.Contains(prefix.Masked().Addr()) {
approverAliases = append(approverAliases, autoApproverAliases...)
}
}
return approverAliases, nil
}