-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add autogroup:internet, fix reduce filter rules #1917
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,38 @@ const ( | |
expectedTokenItems = 2 | ||
) | ||
|
||
var theInternetSet *netipx.IPSet | ||
|
||
// theInternet returns the IPSet for the Internet. | ||
// https://www.youtube.com/watch?v=iDbyYGrswtg | ||
func theInternet() *netipx.IPSet { | ||
if theInternetSet != nil { | ||
return theInternetSet | ||
} | ||
|
||
var internetBuilder netipx.IPSetBuilder | ||
internetBuilder.AddPrefix(netip.MustParsePrefix("2000::/3")) | ||
internetBuilder.AddPrefix(netip.MustParsePrefix("0.0.0.0/0")) | ||
|
||
// Delete Private network addresses | ||
// https://datatracker.ietf.org/doc/html/rfc1918 | ||
internetBuilder.RemovePrefix(netip.MustParsePrefix("fc00::/7")) | ||
internetBuilder.RemovePrefix(netip.MustParsePrefix("10.0.0.0/8")) | ||
internetBuilder.RemovePrefix(netip.MustParsePrefix("172.16.0.0/12")) | ||
internetBuilder.RemovePrefix(netip.MustParsePrefix("192.168.0.0/16")) | ||
|
||
// Delete Tailscale networks | ||
internetBuilder.RemovePrefix(netip.MustParsePrefix("fd7a:115c:a1e0::/48")) | ||
internetBuilder.RemovePrefix(netip.MustParsePrefix("100.64.0.0/10")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you can define custom ip prefixes is it a good practice to assume the user is using the default tailscale network? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tailscale client does not support other up ranges, so while it is configurable in Headscale you cannot use anything else. If that changes we could read it from the config. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks to make it clear. didn't knew that and just read through the code and commented with my thoughts. 😁 |
||
|
||
// Delete "cant find DHCP networks" | ||
internetBuilder.RemovePrefix(netip.MustParsePrefix("fe80::/10")) // link-loca | ||
internetBuilder.RemovePrefix(netip.MustParsePrefix("169.254.0.0/16")) | ||
|
||
theInternetSet, _ := internetBuilder.IPSet() | ||
return theInternetSet | ||
} | ||
|
||
// For some reason golang.org/x/net/internal/iana is an internal package. | ||
const ( | ||
protocolICMP = 1 // Internet Control Message | ||
|
@@ -221,28 +253,28 @@ func ReduceFilterRules(node *types.Node, rules []tailcfg.FilterRule) []tailcfg.F | |
// record if the rule is actually relevant for the given node. | ||
dests := []tailcfg.NetPortRange{} | ||
|
||
DEST_LOOP: | ||
for _, dest := range rule.DstPorts { | ||
expanded, err := util.ParseIPSet(dest.IP, nil) | ||
// Fail closed, if we cant parse it, then we should not allow | ||
// access. | ||
if err != nil { | ||
continue | ||
continue DEST_LOOP | ||
} | ||
|
||
if node.InIPSet(expanded) { | ||
dests = append(dests, dest) | ||
continue DEST_LOOP | ||
} | ||
|
||
// If the node exposes routes, ensure they are note removed | ||
// when the filters are reduced. | ||
if node.Hostinfo != nil { | ||
// TODO(kradalby): Evaluate if we should only keep | ||
// the routes if the route is enabled. This will | ||
// require database access in this part of the code. | ||
if len(node.Hostinfo.RoutableIPs) > 0 { | ||
for _, routableIP := range node.Hostinfo.RoutableIPs { | ||
if expanded.ContainsPrefix(routableIP) { | ||
if expanded.OverlapsPrefix(routableIP) { | ||
dests = append(dests, dest) | ||
continue DEST_LOOP | ||
} | ||
} | ||
} | ||
|
@@ -517,6 +549,7 @@ func (pol *ACLPolicy) expandSource( | |
// - a host | ||
// - an ip | ||
// - a cidr | ||
// - an autogroup | ||
// and transform these in IPAddresses. | ||
func (pol *ACLPolicy) ExpandAlias( | ||
nodes types.Nodes, | ||
|
@@ -542,6 +575,10 @@ func (pol *ACLPolicy) ExpandAlias( | |
return pol.expandIPsFromTag(alias, nodes) | ||
} | ||
|
||
if isAutoGroup(alias) { | ||
return expandAutoGroup(alias) | ||
} | ||
|
||
// if alias is a user | ||
if ips, err := pol.expandIPsFromUser(alias, nodes); ips != nil { | ||
return ips, err | ||
|
@@ -862,6 +899,16 @@ func (pol *ACLPolicy) expandIPsFromIPPrefix( | |
return build.IPSet() | ||
} | ||
|
||
func expandAutoGroup(alias string) (*netipx.IPSet, error) { | ||
switch { | ||
case strings.HasPrefix(alias, "autogroup:internet"): | ||
return theInternet(), nil | ||
|
||
default: | ||
return nil, fmt.Errorf("unknown autogroup %q", alias) | ||
} | ||
} | ||
|
||
func isWildcard(str string) bool { | ||
return str == "*" | ||
} | ||
|
@@ -874,6 +921,10 @@ func isTag(str string) bool { | |
return strings.HasPrefix(str, "tag:") | ||
} | ||
|
||
func isAutoGroup(str string) bool { | ||
return strings.HasPrefix(str, "autogroup:") | ||
} | ||
|
||
// TagsOfNode will return the tags of the current node. | ||
// Invalid tags are tags added by a user on a node, and that user doesn't have authority to add this tag. | ||
// Valid tags are tags added by a user that is allowed in the ACL policy to add this tag. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hahahaha