From 8a8ec7476d2ca2c03e0b9f9fe026fbedd7327eb1 Mon Sep 17 00:00:00 2001 From: Benjamin George Roberts Date: Wed, 7 Sep 2022 22:12:29 +1000 Subject: [PATCH] fix linting issues in preauthkey tags --- db.go | 2 +- grpcv1.go | 2 +- integration_cli_test.go | 2 +- preauth_keys.go | 28 +++++++++++++++------------- preauth_keys_test.go | 2 +- protocol_common.go | 2 -- 6 files changed, 19 insertions(+), 19 deletions(-) diff --git a/db.go b/db.go index 6edb19a2..fc539cc3 100644 --- a/db.go +++ b/db.go @@ -131,7 +131,7 @@ func (h *Headscale) initDB() error { return err } - err = db.AutoMigrate(&PreAuthKeyAclTag{}) + err = db.AutoMigrate(&PreAuthKeyACLTag{}) if err != nil { return err } diff --git a/grpcv1.go b/grpcv1.go index cadd6304..917c2a14 100644 --- a/grpcv1.go +++ b/grpcv1.go @@ -1,4 +1,4 @@ -// nolint +//nolint package headscale import ( diff --git a/integration_cli_test.go b/integration_cli_test.go index 284dd485..d3b31507 100644 --- a/integration_cli_test.go +++ b/integration_cli_test.go @@ -337,7 +337,7 @@ func (s *IntegrationCLITestSuite) TestPreAuthKeyCommand() { // Test that tags are present for i := 0; i < count; i++ { - assert.Equals(listedPreAuthKeys[i].AclTags, []string{"tag:test1,", "tag:test2"}) + assert.Equal(s.T(), listedPreAuthKeys[i].AclTags, []string{"tag:test1", "tag:test2"}) } // Expire three keys diff --git a/preauth_keys.go b/preauth_keys.go index 6ebd6560..5206c43c 100644 --- a/preauth_keys.go +++ b/preauth_keys.go @@ -19,6 +19,7 @@ const ( ErrPreAuthKeyExpired = Error("AuthKey expired") ErrSingleUseAuthKeyHasBeenUsed = Error("AuthKey has already been used") ErrNamespaceMismatch = Error("namespace mismatch") + ErrPreAuthKeyACLTagInvalid = Error("AuthKey tag is invalid") ) // PreAuthKey describes a pre-authorization key usable in a particular namespace. @@ -30,14 +31,14 @@ type PreAuthKey struct { Reusable bool Ephemeral bool `gorm:"default:false"` Used bool `gorm:"default:false"` - AclTags []PreAuthKeyAclTag + ACLTags []PreAuthKeyACLTag CreatedAt *time.Time Expiration *time.Time } -// PreAuthKeyAclTag describes an autmatic tag applied to a node when registered with the associated PreAuthKey -type PreAuthKeyAclTag struct { +// PreAuthKeyACLTag describes an autmatic tag applied to a node when registered with the associated PreAuthKey. +type PreAuthKeyACLTag struct { ID uint64 `gorm:"primary_key"` PreAuthKeyID uint64 Tag string @@ -58,7 +59,7 @@ func (h *Headscale) CreatePreAuthKey( for _, tag := range aclTags { if !strings.HasPrefix(tag, "tag:") { - return nil, fmt.Errorf("aclTag '%s' did not begin with 'tag:'", tag) + return nil, fmt.Errorf("%w: '%s' did not begin with 'tag:'", ErrPreAuthKeyACLTagInvalid, tag) } } @@ -87,8 +88,8 @@ func (h *Headscale) CreatePreAuthKey( seenTags := map[string]bool{} for _, tag := range aclTags { - if seenTags[tag] == false { - if err := db.Save(&PreAuthKeyAclTag{PreAuthKeyID: key.ID, Tag: tag}).Error; err != nil { + if !seenTags[tag] { + if err := db.Save(&PreAuthKeyACLTag{PreAuthKeyID: key.ID, Tag: tag}).Error; err != nil { return fmt.Errorf( "failed to ceate key tag in the database: %w", err, @@ -98,6 +99,7 @@ func (h *Headscale) CreatePreAuthKey( } } } + return nil }) @@ -116,7 +118,7 @@ func (h *Headscale) ListPreAuthKeys(namespaceName string) ([]PreAuthKey, error) } keys := []PreAuthKey{} - if err := h.db.Preload("Namespace").Preload("AclTags").Where(&PreAuthKey{NamespaceID: namespace.ID}).Find(&keys).Error; err != nil { + if err := h.db.Preload("Namespace").Preload("ACLTags").Where(&PreAuthKey{NamespaceID: namespace.ID}).Find(&keys).Error; err != nil { return nil, err } @@ -141,7 +143,7 @@ func (h *Headscale) GetPreAuthKey(namespace string, key string) (*PreAuthKey, er // does not exist. func (h *Headscale) DestroyPreAuthKey(pak PreAuthKey) error { return h.db.Transaction(func(db *gorm.DB) error { - if result := db.Unscoped().Where(PreAuthKeyAclTag{PreAuthKeyID: pak.ID}).Delete(&PreAuthKeyAclTag{}); result.Error != nil { + if result := db.Unscoped().Where(PreAuthKeyACLTag{PreAuthKeyID: pak.ID}).Delete(&PreAuthKeyACLTag{}); result.Error != nil { return result.Error } @@ -176,7 +178,7 @@ func (h *Headscale) UsePreAuthKey(k *PreAuthKey) error { // If returns no error and a PreAuthKey, it can be used. func (h *Headscale) checkKeyValidity(k string) (*PreAuthKey, error) { pak := PreAuthKey{} - if result := h.db.Preload("Namespace").Preload("AclTags").First(&pak, "key = ?", k); errors.Is( + if result := h.db.Preload("Namespace").Preload("ACLTags").First(&pak, "key = ?", k); errors.Is( result.Error, gorm.ErrRecordNotFound, ) { @@ -221,7 +223,7 @@ func (key *PreAuthKey) toProto() *v1.PreAuthKey { Ephemeral: key.Ephemeral, Reusable: key.Reusable, Used: key.Used, - AclTags: make([]string, len(key.AclTags)), + AclTags: make([]string, len(key.ACLTags)), } if key.Expiration != nil { @@ -232,9 +234,9 @@ func (key *PreAuthKey) toProto() *v1.PreAuthKey { protoKey.CreatedAt = timestamppb.New(*key.CreatedAt) } - if len(key.AclTags) > 0 { - for idx := range key.AclTags { - protoKey.AclTags[idx] = key.AclTags[idx].Tag + if len(key.ACLTags) > 0 { + for idx := range key.ACLTags { + protoKey.AclTags[idx] = key.ACLTags[idx].Tag } } diff --git a/preauth_keys_test.go b/preauth_keys_test.go index ffcaf9a4..84977dbd 100644 --- a/preauth_keys_test.go +++ b/preauth_keys_test.go @@ -191,7 +191,7 @@ func (*Suite) TestNotReusableMarkedAsUsed(c *check.C) { c.Assert(err, check.Equals, ErrSingleUseAuthKeyHasBeenUsed) } -func (*Suite) TestPreAuthKeyAclTags(c *check.C) { +func (*Suite) TestPreAuthKeyACLTags(c *check.C) { namespace, err := app.CreateNamespace("test8") c.Assert(err, check.IsNil) diff --git a/protocol_common.go b/protocol_common.go index cbb35be8..9cb7c67f 100644 --- a/protocol_common.go +++ b/protocol_common.go @@ -343,7 +343,6 @@ func (h *Headscale) handleAuthKeyCommon( machine.NodeKey = nodeKey machine.AuthKeyID = uint(pak.ID) err := h.RefreshMachine(machine, registerRequest.Expiry) - if err != nil { log.Error(). Caller(). @@ -372,7 +371,6 @@ func (h *Headscale) handleAuthKeyCommon( return } - } else { now := time.Now().UTC()