replace netaddr usage with netip

This commit is contained in:
Benjamin George Roberts 2022-09-05 09:33:53 +10:00
parent 9810d84e2d
commit 842c28adff
3 changed files with 10 additions and 9 deletions

View File

@ -111,7 +111,7 @@ func (policy ACLPolicy) IsZero() bool {
// Returns the list of autoApproving namespaces, groups or tags for a given IPPrefix // Returns the list of autoApproving namespaces, groups or tags for a given IPPrefix
func (autoApprovers *AutoApprovers) GetRouteApprovers( func (autoApprovers *AutoApprovers) GetRouteApprovers(
prefix netaddr.IPPrefix, prefix netip.Prefix,
) ([]string, error) { ) ([]string, error) {
if prefix.Bits() == 0 { if prefix.Bits() == 0 {
return autoApprovers.ExitNode, nil // 0.0.0.0/0, ::/0 or equivalent return autoApprovers.ExitNode, nil // 0.0.0.0/0, ::/0 or equivalent
@ -120,13 +120,14 @@ func (autoApprovers *AutoApprovers) GetRouteApprovers(
approverAliases := []string{} approverAliases := []string{}
for autoApprovedPrefix, autoApproverAliases := range autoApprovers.Routes { for autoApprovedPrefix, autoApproverAliases := range autoApprovers.Routes {
autoApprovedPrefix, err := netaddr.ParseIPPrefix(autoApprovedPrefix) autoApprovedPrefix, err := netip.ParsePrefix(autoApprovedPrefix)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if autoApprovedPrefix.Bits() >= prefix.Bits() && if autoApprovedPrefix.Bits() >= prefix.Bits() &&
autoApprovedPrefix.Contains(prefix.IP()) { autoApprovedPrefix.Contains(prefix.Masked().Addr()) {
approverAliases = append(approverAliases, autoApproverAliases...) approverAliases = append(approverAliases, autoApproverAliases...)
} }
} }

View File

@ -936,7 +936,7 @@ func (h *Headscale) EnableAutoApprovedRoutes(machine *Machine) error {
return nil // This machine has no IPAddresses, so can't possibly match any autoApprovers ACLs return nil // This machine has no IPAddresses, so can't possibly match any autoApprovers ACLs
} }
approvedRoutes := make([]netaddr.IPPrefix, 0, len(machine.HostInfo.RoutableIPs)) approvedRoutes := make([]netip.Prefix, 0, len(machine.HostInfo.RoutableIPs))
thisMachine := []Machine{*machine} thisMachine := []Machine{*machine}
for _, advertisedRoute := range machine.HostInfo.RoutableIPs { for _, advertisedRoute := range machine.HostInfo.RoutableIPs {

View File

@ -1063,9 +1063,9 @@ func (s *Suite) TestAutoApproveRoutes(c *check.C) {
nodeKey := key.NewNode() nodeKey := key.NewNode()
defaultRoute := netaddr.MustParseIPPrefix("0.0.0.0/0") defaultRoute := netip.MustParsePrefix("0.0.0.0/0")
route1 := netaddr.MustParseIPPrefix("10.10.0.0/16") route1 := netip.MustParsePrefix("10.10.0.0/16")
route2 := netaddr.MustParseIPPrefix("10.11.0.0/16") route2 := netip.MustParsePrefix("10.11.0.0/16")
machine := Machine{ machine := Machine{
ID: 0, ID: 0,
@ -1078,9 +1078,9 @@ func (s *Suite) TestAutoApproveRoutes(c *check.C) {
AuthKeyID: uint(pak.ID), AuthKeyID: uint(pak.ID),
HostInfo: HostInfo{ HostInfo: HostInfo{
RequestTags: []string{"tag:exit"}, RequestTags: []string{"tag:exit"},
RoutableIPs: []netaddr.IPPrefix{defaultRoute, route1, route2}, RoutableIPs: []netip.Prefix{defaultRoute, route1, route2},
}, },
IPAddresses: []netaddr.IP{netaddr.MustParseIP("100.64.0.1")}, IPAddresses: []netip.Addr{netip.MustParseAddr("100.64.0.1")},
} }
app.db.Save(&machine) app.db.Save(&machine)