Add and fix errname

This commit is contained in:
Kristoffer Dalby 2021-11-15 16:33:16 +00:00
parent 0c45f8d252
commit 0c005a6b01
No known key found for this signature in database
GPG key ID: 09F62DC067465735
10 changed files with 50 additions and 51 deletions

View file

@ -33,7 +33,6 @@ linters:
- wrapcheck - wrapcheck
- goerr113 - goerr113
- forcetypeassert - forcetypeassert
- errname
- gosec - gosec
- forbidigo - forbidigo
- dupl - dupl

30
acls.go
View file

@ -15,13 +15,13 @@ import (
) )
const ( const (
errorEmptyPolicy = Error("empty policy") errEmptyPolicy = Error("empty policy")
errorInvalidAction = Error("invalid action") errInvalidAction = Error("invalid action")
errorInvalidUserSection = Error("invalid user section") errInvalidUserSection = Error("invalid user section")
errorInvalidGroup = Error("invalid group") errInvalidGroup = Error("invalid group")
errorInvalidTag = Error("invalid tag") errInvalidTag = Error("invalid tag")
errorInvalidNamespace = Error("invalid namespace") errInvalidNamespace = Error("invalid namespace")
errorInvalidPortFormat = Error("invalid port format") errInvalidPortFormat = Error("invalid port format")
) )
const ( const (
@ -57,7 +57,7 @@ func (h *Headscale) LoadACLPolicy(path string) error {
return err return err
} }
if policy.IsZero() { if policy.IsZero() {
return errorEmptyPolicy return errEmptyPolicy
} }
h.aclPolicy = &policy h.aclPolicy = &policy
@ -75,7 +75,7 @@ func (h *Headscale) generateACLRules() ([]tailcfg.FilterRule, error) {
for index, acl := range h.aclPolicy.ACLs { for index, acl := range h.aclPolicy.ACLs {
if acl.Action != "accept" { if acl.Action != "accept" {
return nil, errorInvalidAction return nil, errInvalidAction
} }
filterRule := tailcfg.FilterRule{} filterRule := tailcfg.FilterRule{}
@ -123,7 +123,7 @@ func (h *Headscale) generateACLPolicyDestPorts(
) ([]tailcfg.NetPortRange, error) { ) ([]tailcfg.NetPortRange, error) {
tokens := strings.Split(d, ":") tokens := strings.Split(d, ":")
if len(tokens) < EXPECTED_TOKEN_ITEMS || len(tokens) > 3 { if len(tokens) < EXPECTED_TOKEN_ITEMS || len(tokens) > 3 {
return nil, errorInvalidPortFormat return nil, errInvalidPortFormat
} }
var alias string var alias string
@ -169,13 +169,13 @@ func (h *Headscale) expandAlias(alias string) ([]string, error) {
if strings.HasPrefix(alias, "group:") { if strings.HasPrefix(alias, "group:") {
if _, ok := h.aclPolicy.Groups[alias]; !ok { if _, ok := h.aclPolicy.Groups[alias]; !ok {
return nil, errorInvalidGroup return nil, errInvalidGroup
} }
ips := []string{} ips := []string{}
for _, n := range h.aclPolicy.Groups[alias] { for _, n := range h.aclPolicy.Groups[alias] {
nodes, err := h.ListMachinesInNamespace(n) nodes, err := h.ListMachinesInNamespace(n)
if err != nil { if err != nil {
return nil, errorInvalidNamespace return nil, errInvalidNamespace
} }
for _, node := range nodes { for _, node := range nodes {
ips = append(ips, node.IPAddress) ips = append(ips, node.IPAddress)
@ -187,7 +187,7 @@ func (h *Headscale) expandAlias(alias string) ([]string, error) {
if strings.HasPrefix(alias, "tag:") { if strings.HasPrefix(alias, "tag:") {
if _, ok := h.aclPolicy.TagOwners[alias]; !ok { if _, ok := h.aclPolicy.TagOwners[alias]; !ok {
return nil, errorInvalidTag return nil, errInvalidTag
} }
// This will have HORRIBLE performance. // This will have HORRIBLE performance.
@ -251,7 +251,7 @@ func (h *Headscale) expandAlias(alias string) ([]string, error) {
return []string{cidr.String()}, nil return []string{cidr.String()}, nil
} }
return nil, errorInvalidUserSection return nil, errInvalidUserSection
} }
func (h *Headscale) expandPorts(portsStr string) (*[]tailcfg.PortRange, error) { func (h *Headscale) expandPorts(portsStr string) (*[]tailcfg.PortRange, error) {
@ -290,7 +290,7 @@ func (h *Headscale) expandPorts(portsStr string) (*[]tailcfg.PortRange, error) {
}) })
default: default:
return nil, errorInvalidPortFormat return nil, errInvalidPortFormat
} }
} }

View file

@ -17,7 +17,7 @@ func (s *Suite) TestBrokenHuJson(c *check.C) {
func (s *Suite) TestInvalidPolicyHuson(c *check.C) { func (s *Suite) TestInvalidPolicyHuson(c *check.C) {
err := app.LoadACLPolicy("./tests/acls/invalid.hujson") err := app.LoadACLPolicy("./tests/acls/invalid.hujson")
c.Assert(err, check.NotNil) c.Assert(err, check.NotNil)
c.Assert(err, check.Equals, errorEmptyPolicy) c.Assert(err, check.Equals, errEmptyPolicy)
} }
func (s *Suite) TestParseHosts(c *check.C) { func (s *Suite) TestParseHosts(c *check.C) {

View file

@ -284,7 +284,7 @@ func (h *Headscale) UpdateMachine(machine *Machine) error {
// DeleteMachine softs deletes a Machine from the database. // DeleteMachine softs deletes a Machine from the database.
func (h *Headscale) DeleteMachine(machine *Machine) error { func (h *Headscale) DeleteMachine(machine *Machine) error {
err := h.RemoveSharedMachineFromAllNamespaces(machine) err := h.RemoveSharedMachineFromAllNamespaces(machine)
if err != nil && errors.Is(err, errorMachineNotShared) { if err != nil && errors.Is(err, errMachineNotShared) {
return err return err
} }
@ -301,7 +301,7 @@ func (h *Headscale) DeleteMachine(machine *Machine) error {
// HardDeleteMachine hard deletes a Machine from the database. // HardDeleteMachine hard deletes a Machine from the database.
func (h *Headscale) HardDeleteMachine(machine *Machine) error { func (h *Headscale) HardDeleteMachine(machine *Machine) error {
err := h.RemoveSharedMachineFromAllNamespaces(machine) err := h.RemoveSharedMachineFromAllNamespaces(machine)
if err != nil && errors.Is(err, errorMachineNotShared) { if err != nil && errors.Is(err, errMachineNotShared) {
return err return err
} }

View file

@ -15,9 +15,9 @@ import (
) )
const ( const (
errorNamespaceExists = Error("Namespace already exists") errNamespaceExists = Error("Namespace already exists")
errorNamespaceNotFound = Error("Namespace not found") errNamespaceNotFound = Error("Namespace not found")
errorNamespaceNotEmptyOfNodes = Error("Namespace not empty: node(s) found") errNamespaceNotEmptyOfNodes = Error("Namespace not empty: node(s) found")
) )
// Namespace is the way Headscale implements the concept of users in Tailscale // 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) { func (h *Headscale) CreateNamespace(name string) (*Namespace, error) {
namespace := Namespace{} namespace := Namespace{}
if err := h.db.Where("name = ?", name).First(&namespace).Error; err == nil { if err := h.db.Where("name = ?", name).First(&namespace).Error; err == nil {
return nil, errorNamespaceExists return nil, errNamespaceExists
} }
namespace.Name = name namespace.Name = name
if err := h.db.Create(&namespace).Error; err != nil { 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 { func (h *Headscale) DestroyNamespace(name string) error {
namespace, err := h.GetNamespace(name) namespace, err := h.GetNamespace(name)
if err != nil { if err != nil {
return errorNamespaceNotFound return errNamespaceNotFound
} }
machines, err := h.ListMachinesInNamespace(name) machines, err := h.ListMachinesInNamespace(name)
@ -62,7 +62,7 @@ func (h *Headscale) DestroyNamespace(name string) error {
return err return err
} }
if len(machines) > 0 { if len(machines) > 0 {
return errorNamespaceNotEmptyOfNodes return errNamespaceNotEmptyOfNodes
} }
keys, err := h.ListPreAuthKeys(name) keys, err := h.ListPreAuthKeys(name)
@ -92,9 +92,9 @@ func (h *Headscale) RenameNamespace(oldName, newName string) error {
} }
_, err = h.GetNamespace(newName) _, err = h.GetNamespace(newName)
if err == nil { if err == nil {
return errorNamespaceExists return errNamespaceExists
} }
if !errors.Is(err, errorNamespaceNotFound) { if !errors.Is(err, errNamespaceNotFound) {
return err return err
} }
@ -119,7 +119,7 @@ func (h *Headscale) GetNamespace(name string) (*Namespace, error) {
result.Error, result.Error,
gorm.ErrRecordNotFound, gorm.ErrRecordNotFound,
) { ) {
return nil, errorNamespaceNotFound return nil, errNamespaceNotFound
} }
return &namespace, nil return &namespace, nil

View file

@ -24,7 +24,7 @@ func (s *Suite) TestCreateAndDestroyNamespace(c *check.C) {
func (s *Suite) TestDestroyNamespaceErrors(c *check.C) { func (s *Suite) TestDestroyNamespaceErrors(c *check.C) {
err := app.DestroyNamespace("test") err := app.DestroyNamespace("test")
c.Assert(err, check.Equals, errorNamespaceNotFound) c.Assert(err, check.Equals, errNamespaceNotFound)
namespace, err := app.CreateNamespace("test") namespace, err := app.CreateNamespace("test")
c.Assert(err, check.IsNil) c.Assert(err, check.IsNil)
@ -59,7 +59,7 @@ func (s *Suite) TestDestroyNamespaceErrors(c *check.C) {
app.db.Save(&machine) app.db.Save(&machine)
err = app.DestroyNamespace("test") err = app.DestroyNamespace("test")
c.Assert(err, check.Equals, errorNamespaceNotEmptyOfNodes) c.Assert(err, check.Equals, errNamespaceNotEmptyOfNodes)
} }
func (s *Suite) TestRenameNamespace(c *check.C) { func (s *Suite) TestRenameNamespace(c *check.C) {
@ -75,20 +75,20 @@ func (s *Suite) TestRenameNamespace(c *check.C) {
c.Assert(err, check.IsNil) c.Assert(err, check.IsNil)
_, err = app.GetNamespace("test") _, err = app.GetNamespace("test")
c.Assert(err, check.Equals, errorNamespaceNotFound) c.Assert(err, check.Equals, errNamespaceNotFound)
_, err = app.GetNamespace("test_renamed") _, err = app.GetNamespace("test_renamed")
c.Assert(err, check.IsNil) c.Assert(err, check.IsNil)
err = app.RenameNamespace("test_does_not_exit", "test") 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") namespaceTest2, err := app.CreateNamespace("test2")
c.Assert(err, check.IsNil) c.Assert(err, check.IsNil)
c.Assert(namespaceTest2.Name, check.Equals, "test2") c.Assert(namespaceTest2.Name, check.Equals, "test2")
err = app.RenameNamespace("test2", "test_renamed") 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) { func (s *Suite) TestGetMapResponseUserProfiles(c *check.C) {

View file

@ -13,8 +13,8 @@ import (
) )
const ( const (
errorAuthKeyNotFound = Error("AuthKey not found") errPreAuthKeyNotFound = Error("AuthKey not found")
errorAuthKeyExpired = Error("AuthKey expired") errPreAuthKeyExpired = Error("AuthKey expired")
errSingleUseAuthKeyHasBeenUsed = Error("AuthKey has already been used") errSingleUseAuthKeyHasBeenUsed = Error("AuthKey has already been used")
) )
@ -120,11 +120,11 @@ func (h *Headscale) checkKeyValidity(k string) (*PreAuthKey, error) {
result.Error, result.Error,
gorm.ErrRecordNotFound, gorm.ErrRecordNotFound,
) { ) {
return nil, errorAuthKeyNotFound return nil, errPreAuthKeyNotFound
} }
if pak.Expiration != nil && pak.Expiration.Before(time.Now()) { 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 if pak.Reusable || pak.Ephemeral { // we don't need to check if has been used before

View file

@ -44,13 +44,13 @@ func (*Suite) TestExpiredPreAuthKey(c *check.C) {
c.Assert(err, check.IsNil) c.Assert(err, check.IsNil)
key, err := app.checkKeyValidity(pak.Key) key, err := app.checkKeyValidity(pak.Key)
c.Assert(err, check.Equals, errorAuthKeyExpired) c.Assert(err, check.Equals, errPreAuthKeyExpired)
c.Assert(key, check.IsNil) c.Assert(key, check.IsNil)
} }
func (*Suite) TestPreAuthKeyDoesNotExist(c *check.C) { func (*Suite) TestPreAuthKeyDoesNotExist(c *check.C) {
key, err := app.checkKeyValidity("potatoKey") key, err := app.checkKeyValidity("potatoKey")
c.Assert(err, check.Equals, errorAuthKeyNotFound) c.Assert(err, check.Equals, errPreAuthKeyNotFound)
c.Assert(key, check.IsNil) c.Assert(key, check.IsNil)
} }
@ -177,7 +177,7 @@ func (*Suite) TestExpirePreauthKey(c *check.C) {
c.Assert(pak.Expiration, check.NotNil) c.Assert(pak.Expiration, check.NotNil)
key, err := app.checkKeyValidity(pak.Key) key, err := app.checkKeyValidity(pak.Key)
c.Assert(err, check.Equals, errorAuthKeyExpired) c.Assert(err, check.Equals, errPreAuthKeyExpired)
c.Assert(key, check.IsNil) c.Assert(key, check.IsNil)
} }

View file

@ -3,9 +3,9 @@ package headscale
import "gorm.io/gorm" import "gorm.io/gorm"
const ( const (
errorSameNamespace = Error("Destination namespace same as origin") errSameNamespace = Error("Destination namespace same as origin")
errorMachineAlreadyShared = Error("Node already shared to this namespace") errMachineAlreadyShared = Error("Node already shared to this namespace")
errorMachineNotShared = Error("Machine not shared to this namespace") errMachineNotShared = Error("Machine not shared to this namespace")
) )
// SharedMachine is a join table to support sharing nodes between namespaces. // SharedMachine is a join table to support sharing nodes between namespaces.
@ -23,7 +23,7 @@ func (h *Headscale) AddSharedMachineToNamespace(
namespace *Namespace, namespace *Namespace,
) error { ) error {
if machine.NamespaceID == namespace.ID { if machine.NamespaceID == namespace.ID {
return errorSameNamespace return errSameNamespace
} }
sharedMachines := []SharedMachine{} sharedMachines := []SharedMachine{}
@ -31,7 +31,7 @@ func (h *Headscale) AddSharedMachineToNamespace(
return err return err
} }
if len(sharedMachines) > 0 { if len(sharedMachines) > 0 {
return errorMachineAlreadyShared return errMachineAlreadyShared
} }
sharedMachine := SharedMachine{ sharedMachine := SharedMachine{
@ -52,7 +52,7 @@ func (h *Headscale) RemoveSharedMachineFromNamespace(
) error { ) error {
if machine.NamespaceID == namespace.ID { if machine.NamespaceID == namespace.ID {
// Can't unshare from primary namespace // Can't unshare from primary namespace
return errorMachineNotShared return errMachineNotShared
} }
sharedMachine := SharedMachine{} sharedMachine := SharedMachine{}
@ -64,7 +64,7 @@ func (h *Headscale) RemoveSharedMachineFromNamespace(
} }
if result.RowsAffected == 0 { if result.RowsAffected == 0 {
return errorMachineNotShared return errMachineNotShared
} }
err := h.RequestMapUpdates(namespace.ID) err := h.RequestMapUpdates(namespace.ID)

View file

@ -80,7 +80,7 @@ func (s *Suite) TestSameNamespace(c *check.C) {
c.Assert(len(peersOfMachine1BeforeShare), check.Equals, 0) c.Assert(len(peersOfMachine1BeforeShare), check.Equals, 0)
err = app.AddSharedMachineToNamespace(machine1, namespace1) err = app.AddSharedMachineToNamespace(machine1, namespace1)
c.Assert(err, check.Equals, errorSameNamespace) c.Assert(err, check.Equals, errSameNamespace)
} }
func (s *Suite) TestUnshare(c *check.C) { 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) c.Assert(len(peersOfMachine1BeforeShare), check.Equals, 0)
err = app.RemoveSharedMachineFromNamespace(machine2, namespace1) err = app.RemoveSharedMachineFromNamespace(machine2, namespace1)
c.Assert(err, check.Equals, errorMachineNotShared) c.Assert(err, check.Equals, errMachineNotShared)
err = app.RemoveSharedMachineFromNamespace(machine1, namespace1) err = app.RemoveSharedMachineFromNamespace(machine1, namespace1)
c.Assert(err, check.Equals, errorMachineNotShared) c.Assert(err, check.Equals, errMachineNotShared)
} }
func (s *Suite) TestAlreadyShared(c *check.C) { func (s *Suite) TestAlreadyShared(c *check.C) {
@ -147,7 +147,7 @@ func (s *Suite) TestAlreadyShared(c *check.C) {
err = app.AddSharedMachineToNamespace(machine2, namespace1) err = app.AddSharedMachineToNamespace(machine2, namespace1)
c.Assert(err, check.IsNil) c.Assert(err, check.IsNil)
err = app.AddSharedMachineToNamespace(machine2, namespace1) err = app.AddSharedMachineToNamespace(machine2, namespace1)
c.Assert(err, check.Equals, errorMachineAlreadyShared) c.Assert(err, check.Equals, errMachineAlreadyShared)
} }
func (s *Suite) TestDoNotIncludeRoutesOnShared(c *check.C) { func (s *Suite) TestDoNotIncludeRoutesOnShared(c *check.C) {