1
0
mirror of https://github.com/juanfont/headscale.git synced 2025-05-27 01:18:48 +02:00

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,10 +240,10 @@ type Host string
func (h Host) Validate() error {
if isHost(string(h)) {
fmt.Errorf("Hostname %q is invalid", h)
}
return nil
}
return fmt.Errorf("Hostname %q is invalid", h)
}
func (h *Host) UnmarshalJSON(b []byte) error {
*h = Host(strings.Trim(string(b), `"`))
@ -288,12 +290,11 @@ 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 fmt.Errorf("Prefix %q is invalid", p)
}
func (p Prefix) String() string {
return netip.Prefix(p).String()
@ -379,11 +380,9 @@ const (
var autogroups = []string{AutoGroupInternet}
func (ag AutoGroup) Validate() error {
for _, valid := range autogroups {
if valid == string(ag) {
if slices.Contains(autogroups, string(ag)) {
return nil
}
}
return fmt.Errorf("AutoGroup is invalid, got: %q, must be one of %v", ag, autogroups)
}