policy/v2: fix host validation, consistent pattern (#2533)

This commit is contained in:
Kristoffer Dalby 2025-04-18 11:35:04 +02:00 committed by GitHub
parent c30e3a4762
commit 710d75367e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -8,6 +8,8 @@ import (
"strings"
"time"
"slices"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/juanfont/headscale/hscontrol/util"
"github.com/tailscale/hujson"
@ -238,9 +240,9 @@ type Host string
func (h Host) Validate() error {
if isHost(string(h)) {
fmt.Errorf("Hostname %q is invalid", h)
return nil
}
return nil
return fmt.Errorf("Hostname %q is invalid", h)
}
func (h *Host) UnmarshalJSON(b []byte) error {
@ -288,11 +290,10 @@ func (h Host) Resolve(p *Policy, _ types.Users, nodes types.Nodes) (*netipx.IPSe
type Prefix netip.Prefix
func (p Prefix) Validate() error {
if !netip.Prefix(p).IsValid() {
return fmt.Errorf("Prefix %q is invalid", p)
if netip.Prefix(p).IsValid() {
return nil
}
return nil
return fmt.Errorf("Prefix %q is invalid", p)
}
func (p Prefix) String() string {
@ -379,10 +380,8 @@ const (
var autogroups = []string{AutoGroupInternet}
func (ag AutoGroup) Validate() error {
for _, valid := range autogroups {
if valid == string(ag) {
return nil
}
if slices.Contains(autogroups, string(ag)) {
return nil
}
return fmt.Errorf("AutoGroup is invalid, got: %q, must be one of %v", ag, autogroups)