mirror of
https://github.com/juanfont/headscale.git
synced 2024-11-21 15:05:23 +00:00
Add and fix errname
This commit is contained in:
parent
0c45f8d252
commit
0c005a6b01
@ -33,7 +33,6 @@ linters:
|
||||
- wrapcheck
|
||||
- goerr113
|
||||
- forcetypeassert
|
||||
- errname
|
||||
- gosec
|
||||
- forbidigo
|
||||
- dupl
|
||||
|
30
acls.go
30
acls.go
@ -15,13 +15,13 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
errorEmptyPolicy = Error("empty policy")
|
||||
errorInvalidAction = Error("invalid action")
|
||||
errorInvalidUserSection = Error("invalid user section")
|
||||
errorInvalidGroup = Error("invalid group")
|
||||
errorInvalidTag = Error("invalid tag")
|
||||
errorInvalidNamespace = Error("invalid namespace")
|
||||
errorInvalidPortFormat = Error("invalid port format")
|
||||
errEmptyPolicy = Error("empty policy")
|
||||
errInvalidAction = Error("invalid action")
|
||||
errInvalidUserSection = Error("invalid user section")
|
||||
errInvalidGroup = Error("invalid group")
|
||||
errInvalidTag = Error("invalid tag")
|
||||
errInvalidNamespace = Error("invalid namespace")
|
||||
errInvalidPortFormat = Error("invalid port format")
|
||||
)
|
||||
|
||||
const (
|
||||
@ -57,7 +57,7 @@ func (h *Headscale) LoadACLPolicy(path string) error {
|
||||
return err
|
||||
}
|
||||
if policy.IsZero() {
|
||||
return errorEmptyPolicy
|
||||
return errEmptyPolicy
|
||||
}
|
||||
|
||||
h.aclPolicy = &policy
|
||||
@ -75,7 +75,7 @@ func (h *Headscale) generateACLRules() ([]tailcfg.FilterRule, error) {
|
||||
|
||||
for index, acl := range h.aclPolicy.ACLs {
|
||||
if acl.Action != "accept" {
|
||||
return nil, errorInvalidAction
|
||||
return nil, errInvalidAction
|
||||
}
|
||||
|
||||
filterRule := tailcfg.FilterRule{}
|
||||
@ -123,7 +123,7 @@ func (h *Headscale) generateACLPolicyDestPorts(
|
||||
) ([]tailcfg.NetPortRange, error) {
|
||||
tokens := strings.Split(d, ":")
|
||||
if len(tokens) < EXPECTED_TOKEN_ITEMS || len(tokens) > 3 {
|
||||
return nil, errorInvalidPortFormat
|
||||
return nil, errInvalidPortFormat
|
||||
}
|
||||
|
||||
var alias string
|
||||
@ -169,13 +169,13 @@ func (h *Headscale) expandAlias(alias string) ([]string, error) {
|
||||
|
||||
if strings.HasPrefix(alias, "group:") {
|
||||
if _, ok := h.aclPolicy.Groups[alias]; !ok {
|
||||
return nil, errorInvalidGroup
|
||||
return nil, errInvalidGroup
|
||||
}
|
||||
ips := []string{}
|
||||
for _, n := range h.aclPolicy.Groups[alias] {
|
||||
nodes, err := h.ListMachinesInNamespace(n)
|
||||
if err != nil {
|
||||
return nil, errorInvalidNamespace
|
||||
return nil, errInvalidNamespace
|
||||
}
|
||||
for _, node := range nodes {
|
||||
ips = append(ips, node.IPAddress)
|
||||
@ -187,7 +187,7 @@ func (h *Headscale) expandAlias(alias string) ([]string, error) {
|
||||
|
||||
if strings.HasPrefix(alias, "tag:") {
|
||||
if _, ok := h.aclPolicy.TagOwners[alias]; !ok {
|
||||
return nil, errorInvalidTag
|
||||
return nil, errInvalidTag
|
||||
}
|
||||
|
||||
// This will have HORRIBLE performance.
|
||||
@ -251,7 +251,7 @@ func (h *Headscale) expandAlias(alias string) ([]string, error) {
|
||||
return []string{cidr.String()}, nil
|
||||
}
|
||||
|
||||
return nil, errorInvalidUserSection
|
||||
return nil, errInvalidUserSection
|
||||
}
|
||||
|
||||
func (h *Headscale) expandPorts(portsStr string) (*[]tailcfg.PortRange, error) {
|
||||
@ -290,7 +290,7 @@ func (h *Headscale) expandPorts(portsStr string) (*[]tailcfg.PortRange, error) {
|
||||
})
|
||||
|
||||
default:
|
||||
return nil, errorInvalidPortFormat
|
||||
return nil, errInvalidPortFormat
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -17,7 +17,7 @@ func (s *Suite) TestBrokenHuJson(c *check.C) {
|
||||
func (s *Suite) TestInvalidPolicyHuson(c *check.C) {
|
||||
err := app.LoadACLPolicy("./tests/acls/invalid.hujson")
|
||||
c.Assert(err, check.NotNil)
|
||||
c.Assert(err, check.Equals, errorEmptyPolicy)
|
||||
c.Assert(err, check.Equals, errEmptyPolicy)
|
||||
}
|
||||
|
||||
func (s *Suite) TestParseHosts(c *check.C) {
|
||||
|
@ -284,7 +284,7 @@ func (h *Headscale) UpdateMachine(machine *Machine) error {
|
||||
// DeleteMachine softs deletes a Machine from the database.
|
||||
func (h *Headscale) DeleteMachine(machine *Machine) error {
|
||||
err := h.RemoveSharedMachineFromAllNamespaces(machine)
|
||||
if err != nil && errors.Is(err, errorMachineNotShared) {
|
||||
if err != nil && errors.Is(err, errMachineNotShared) {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -301,7 +301,7 @@ func (h *Headscale) DeleteMachine(machine *Machine) error {
|
||||
// HardDeleteMachine hard deletes a Machine from the database.
|
||||
func (h *Headscale) HardDeleteMachine(machine *Machine) error {
|
||||
err := h.RemoveSharedMachineFromAllNamespaces(machine)
|
||||
if err != nil && errors.Is(err, errorMachineNotShared) {
|
||||
if err != nil && errors.Is(err, errMachineNotShared) {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -15,9 +15,9 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
errorNamespaceExists = Error("Namespace already exists")
|
||||
errorNamespaceNotFound = Error("Namespace not found")
|
||||
errorNamespaceNotEmptyOfNodes = Error("Namespace not empty: node(s) found")
|
||||
errNamespaceExists = Error("Namespace already exists")
|
||||
errNamespaceNotFound = Error("Namespace not found")
|
||||
errNamespaceNotEmptyOfNodes = Error("Namespace not empty: node(s) found")
|
||||
)
|
||||
|
||||
// Namespace is the way Headscale implements the concept of users in Tailscale
|
||||
@ -34,7 +34,7 @@ type Namespace struct {
|
||||
func (h *Headscale) CreateNamespace(name string) (*Namespace, error) {
|
||||
namespace := Namespace{}
|
||||
if err := h.db.Where("name = ?", name).First(&namespace).Error; err == nil {
|
||||
return nil, errorNamespaceExists
|
||||
return nil, errNamespaceExists
|
||||
}
|
||||
namespace.Name = name
|
||||
if err := h.db.Create(&namespace).Error; err != nil {
|
||||
@ -54,7 +54,7 @@ func (h *Headscale) CreateNamespace(name string) (*Namespace, error) {
|
||||
func (h *Headscale) DestroyNamespace(name string) error {
|
||||
namespace, err := h.GetNamespace(name)
|
||||
if err != nil {
|
||||
return errorNamespaceNotFound
|
||||
return errNamespaceNotFound
|
||||
}
|
||||
|
||||
machines, err := h.ListMachinesInNamespace(name)
|
||||
@ -62,7 +62,7 @@ func (h *Headscale) DestroyNamespace(name string) error {
|
||||
return err
|
||||
}
|
||||
if len(machines) > 0 {
|
||||
return errorNamespaceNotEmptyOfNodes
|
||||
return errNamespaceNotEmptyOfNodes
|
||||
}
|
||||
|
||||
keys, err := h.ListPreAuthKeys(name)
|
||||
@ -92,9 +92,9 @@ func (h *Headscale) RenameNamespace(oldName, newName string) error {
|
||||
}
|
||||
_, err = h.GetNamespace(newName)
|
||||
if err == nil {
|
||||
return errorNamespaceExists
|
||||
return errNamespaceExists
|
||||
}
|
||||
if !errors.Is(err, errorNamespaceNotFound) {
|
||||
if !errors.Is(err, errNamespaceNotFound) {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -119,7 +119,7 @@ func (h *Headscale) GetNamespace(name string) (*Namespace, error) {
|
||||
result.Error,
|
||||
gorm.ErrRecordNotFound,
|
||||
) {
|
||||
return nil, errorNamespaceNotFound
|
||||
return nil, errNamespaceNotFound
|
||||
}
|
||||
|
||||
return &namespace, nil
|
||||
|
@ -24,7 +24,7 @@ func (s *Suite) TestCreateAndDestroyNamespace(c *check.C) {
|
||||
|
||||
func (s *Suite) TestDestroyNamespaceErrors(c *check.C) {
|
||||
err := app.DestroyNamespace("test")
|
||||
c.Assert(err, check.Equals, errorNamespaceNotFound)
|
||||
c.Assert(err, check.Equals, errNamespaceNotFound)
|
||||
|
||||
namespace, err := app.CreateNamespace("test")
|
||||
c.Assert(err, check.IsNil)
|
||||
@ -59,7 +59,7 @@ func (s *Suite) TestDestroyNamespaceErrors(c *check.C) {
|
||||
app.db.Save(&machine)
|
||||
|
||||
err = app.DestroyNamespace("test")
|
||||
c.Assert(err, check.Equals, errorNamespaceNotEmptyOfNodes)
|
||||
c.Assert(err, check.Equals, errNamespaceNotEmptyOfNodes)
|
||||
}
|
||||
|
||||
func (s *Suite) TestRenameNamespace(c *check.C) {
|
||||
@ -75,20 +75,20 @@ func (s *Suite) TestRenameNamespace(c *check.C) {
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
_, err = app.GetNamespace("test")
|
||||
c.Assert(err, check.Equals, errorNamespaceNotFound)
|
||||
c.Assert(err, check.Equals, errNamespaceNotFound)
|
||||
|
||||
_, err = app.GetNamespace("test_renamed")
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
err = app.RenameNamespace("test_does_not_exit", "test")
|
||||
c.Assert(err, check.Equals, errorNamespaceNotFound)
|
||||
c.Assert(err, check.Equals, errNamespaceNotFound)
|
||||
|
||||
namespaceTest2, err := app.CreateNamespace("test2")
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Assert(namespaceTest2.Name, check.Equals, "test2")
|
||||
|
||||
err = app.RenameNamespace("test2", "test_renamed")
|
||||
c.Assert(err, check.Equals, errorNamespaceExists)
|
||||
c.Assert(err, check.Equals, errNamespaceExists)
|
||||
}
|
||||
|
||||
func (s *Suite) TestGetMapResponseUserProfiles(c *check.C) {
|
||||
|
@ -13,8 +13,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
errorAuthKeyNotFound = Error("AuthKey not found")
|
||||
errorAuthKeyExpired = Error("AuthKey expired")
|
||||
errPreAuthKeyNotFound = Error("AuthKey not found")
|
||||
errPreAuthKeyExpired = Error("AuthKey expired")
|
||||
errSingleUseAuthKeyHasBeenUsed = Error("AuthKey has already been used")
|
||||
)
|
||||
|
||||
@ -120,11 +120,11 @@ func (h *Headscale) checkKeyValidity(k string) (*PreAuthKey, error) {
|
||||
result.Error,
|
||||
gorm.ErrRecordNotFound,
|
||||
) {
|
||||
return nil, errorAuthKeyNotFound
|
||||
return nil, errPreAuthKeyNotFound
|
||||
}
|
||||
|
||||
if pak.Expiration != nil && pak.Expiration.Before(time.Now()) {
|
||||
return nil, errorAuthKeyExpired
|
||||
return nil, errPreAuthKeyExpired
|
||||
}
|
||||
|
||||
if pak.Reusable || pak.Ephemeral { // we don't need to check if has been used before
|
||||
|
@ -44,13 +44,13 @@ func (*Suite) TestExpiredPreAuthKey(c *check.C) {
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
key, err := app.checkKeyValidity(pak.Key)
|
||||
c.Assert(err, check.Equals, errorAuthKeyExpired)
|
||||
c.Assert(err, check.Equals, errPreAuthKeyExpired)
|
||||
c.Assert(key, check.IsNil)
|
||||
}
|
||||
|
||||
func (*Suite) TestPreAuthKeyDoesNotExist(c *check.C) {
|
||||
key, err := app.checkKeyValidity("potatoKey")
|
||||
c.Assert(err, check.Equals, errorAuthKeyNotFound)
|
||||
c.Assert(err, check.Equals, errPreAuthKeyNotFound)
|
||||
c.Assert(key, check.IsNil)
|
||||
}
|
||||
|
||||
@ -177,7 +177,7 @@ func (*Suite) TestExpirePreauthKey(c *check.C) {
|
||||
c.Assert(pak.Expiration, check.NotNil)
|
||||
|
||||
key, err := app.checkKeyValidity(pak.Key)
|
||||
c.Assert(err, check.Equals, errorAuthKeyExpired)
|
||||
c.Assert(err, check.Equals, errPreAuthKeyExpired)
|
||||
c.Assert(key, check.IsNil)
|
||||
}
|
||||
|
||||
|
14
sharing.go
14
sharing.go
@ -3,9 +3,9 @@ package headscale
|
||||
import "gorm.io/gorm"
|
||||
|
||||
const (
|
||||
errorSameNamespace = Error("Destination namespace same as origin")
|
||||
errorMachineAlreadyShared = Error("Node already shared to this namespace")
|
||||
errorMachineNotShared = Error("Machine not shared to this namespace")
|
||||
errSameNamespace = Error("Destination namespace same as origin")
|
||||
errMachineAlreadyShared = Error("Node already shared to this namespace")
|
||||
errMachineNotShared = Error("Machine not shared to this namespace")
|
||||
)
|
||||
|
||||
// SharedMachine is a join table to support sharing nodes between namespaces.
|
||||
@ -23,7 +23,7 @@ func (h *Headscale) AddSharedMachineToNamespace(
|
||||
namespace *Namespace,
|
||||
) error {
|
||||
if machine.NamespaceID == namespace.ID {
|
||||
return errorSameNamespace
|
||||
return errSameNamespace
|
||||
}
|
||||
|
||||
sharedMachines := []SharedMachine{}
|
||||
@ -31,7 +31,7 @@ func (h *Headscale) AddSharedMachineToNamespace(
|
||||
return err
|
||||
}
|
||||
if len(sharedMachines) > 0 {
|
||||
return errorMachineAlreadyShared
|
||||
return errMachineAlreadyShared
|
||||
}
|
||||
|
||||
sharedMachine := SharedMachine{
|
||||
@ -52,7 +52,7 @@ func (h *Headscale) RemoveSharedMachineFromNamespace(
|
||||
) error {
|
||||
if machine.NamespaceID == namespace.ID {
|
||||
// Can't unshare from primary namespace
|
||||
return errorMachineNotShared
|
||||
return errMachineNotShared
|
||||
}
|
||||
|
||||
sharedMachine := SharedMachine{}
|
||||
@ -64,7 +64,7 @@ func (h *Headscale) RemoveSharedMachineFromNamespace(
|
||||
}
|
||||
|
||||
if result.RowsAffected == 0 {
|
||||
return errorMachineNotShared
|
||||
return errMachineNotShared
|
||||
}
|
||||
|
||||
err := h.RequestMapUpdates(namespace.ID)
|
||||
|
@ -80,7 +80,7 @@ func (s *Suite) TestSameNamespace(c *check.C) {
|
||||
c.Assert(len(peersOfMachine1BeforeShare), check.Equals, 0)
|
||||
|
||||
err = app.AddSharedMachineToNamespace(machine1, namespace1)
|
||||
c.Assert(err, check.Equals, errorSameNamespace)
|
||||
c.Assert(err, check.Equals, errSameNamespace)
|
||||
}
|
||||
|
||||
func (s *Suite) TestUnshare(c *check.C) {
|
||||
@ -118,10 +118,10 @@ func (s *Suite) TestUnshare(c *check.C) {
|
||||
c.Assert(len(peersOfMachine1BeforeShare), check.Equals, 0)
|
||||
|
||||
err = app.RemoveSharedMachineFromNamespace(machine2, namespace1)
|
||||
c.Assert(err, check.Equals, errorMachineNotShared)
|
||||
c.Assert(err, check.Equals, errMachineNotShared)
|
||||
|
||||
err = app.RemoveSharedMachineFromNamespace(machine1, namespace1)
|
||||
c.Assert(err, check.Equals, errorMachineNotShared)
|
||||
c.Assert(err, check.Equals, errMachineNotShared)
|
||||
}
|
||||
|
||||
func (s *Suite) TestAlreadyShared(c *check.C) {
|
||||
@ -147,7 +147,7 @@ func (s *Suite) TestAlreadyShared(c *check.C) {
|
||||
err = app.AddSharedMachineToNamespace(machine2, namespace1)
|
||||
c.Assert(err, check.IsNil)
|
||||
err = app.AddSharedMachineToNamespace(machine2, namespace1)
|
||||
c.Assert(err, check.Equals, errorMachineAlreadyShared)
|
||||
c.Assert(err, check.Equals, errMachineAlreadyShared)
|
||||
}
|
||||
|
||||
func (s *Suite) TestDoNotIncludeRoutesOnShared(c *check.C) {
|
||||
|
Loading…
Reference in New Issue
Block a user