From 0c005a6b012816cbf91e717abfd04af2b5170b33 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Mon, 15 Nov 2021 16:33:16 +0000 Subject: [PATCH] Add and fix errname --- .golangci.yaml | 1 - acls.go | 30 +++++++++++++++--------------- acls_test.go | 2 +- machine.go | 4 ++-- namespaces.go | 18 +++++++++--------- namespaces_test.go | 10 +++++----- preauth_keys.go | 8 ++++---- preauth_keys_test.go | 6 +++--- sharing.go | 14 +++++++------- sharing_test.go | 8 ++++---- 10 files changed, 50 insertions(+), 51 deletions(-) diff --git a/.golangci.yaml b/.golangci.yaml index 982c98bb..08528b8f 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -33,7 +33,6 @@ linters: - wrapcheck - goerr113 - forcetypeassert - - errname - gosec - forbidigo - dupl diff --git a/acls.go b/acls.go index 01de114e..3f94663c 100644 --- a/acls.go +++ b/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 } } diff --git a/acls_test.go b/acls_test.go index cca39ce8..3e051f5f 100644 --- a/acls_test.go +++ b/acls_test.go @@ -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) { diff --git a/machine.go b/machine.go index b0c003fd..8aee68e3 100644 --- a/machine.go +++ b/machine.go @@ -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 } diff --git a/namespaces.go b/namespaces.go index 858a7aaa..9ee7bf16 100644 --- a/namespaces.go +++ b/namespaces.go @@ -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 diff --git a/namespaces_test.go b/namespaces_test.go index 7ac1f063..bbae98f9 100644 --- a/namespaces_test.go +++ b/namespaces_test.go @@ -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) { diff --git a/preauth_keys.go b/preauth_keys.go index 742e06cf..5159a870 100644 --- a/preauth_keys.go +++ b/preauth_keys.go @@ -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 diff --git a/preauth_keys_test.go b/preauth_keys_test.go index 6a6a79b4..fd0feb03 100644 --- a/preauth_keys_test.go +++ b/preauth_keys_test.go @@ -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) } diff --git a/sharing.go b/sharing.go index 741deb61..be1689d5 100644 --- a/sharing.go +++ b/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) diff --git a/sharing_test.go b/sharing_test.go index 8d4ee2c9..7ec1b0ee 100644 --- a/sharing_test.go +++ b/sharing_test.go @@ -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) {