policy/v2: fix host validation, consistent pattern (#2533)
This commit is contained in:
parent
c30e3a4762
commit
710d75367e
1 changed files with 9 additions and 10 deletions
|
@ -8,6 +8,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"slices"
|
||||||
|
|
||||||
"github.com/juanfont/headscale/hscontrol/types"
|
"github.com/juanfont/headscale/hscontrol/types"
|
||||||
"github.com/juanfont/headscale/hscontrol/util"
|
"github.com/juanfont/headscale/hscontrol/util"
|
||||||
"github.com/tailscale/hujson"
|
"github.com/tailscale/hujson"
|
||||||
|
@ -238,10 +240,10 @@ type Host string
|
||||||
|
|
||||||
func (h Host) Validate() error {
|
func (h Host) Validate() error {
|
||||||
if isHost(string(h)) {
|
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 {
|
func (h *Host) UnmarshalJSON(b []byte) error {
|
||||||
*h = Host(strings.Trim(string(b), `"`))
|
*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
|
type Prefix netip.Prefix
|
||||||
|
|
||||||
func (p Prefix) Validate() error {
|
func (p Prefix) Validate() error {
|
||||||
if !netip.Prefix(p).IsValid() {
|
if netip.Prefix(p).IsValid() {
|
||||||
return fmt.Errorf("Prefix %q is invalid", p)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
return fmt.Errorf("Prefix %q is invalid", p)
|
||||||
|
}
|
||||||
|
|
||||||
func (p Prefix) String() string {
|
func (p Prefix) String() string {
|
||||||
return netip.Prefix(p).String()
|
return netip.Prefix(p).String()
|
||||||
|
@ -379,11 +380,9 @@ const (
|
||||||
var autogroups = []string{AutoGroupInternet}
|
var autogroups = []string{AutoGroupInternet}
|
||||||
|
|
||||||
func (ag AutoGroup) Validate() error {
|
func (ag AutoGroup) Validate() error {
|
||||||
for _, valid := range autogroups {
|
if slices.Contains(autogroups, string(ag)) {
|
||||||
if valid == string(ag) {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Errorf("AutoGroup is invalid, got: %q, must be one of %v", ag, autogroups)
|
return fmt.Errorf("AutoGroup is invalid, got: %q, must be one of %v", ag, autogroups)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue