Skip to content

Commit

Permalink
fix linting issues in preauthkey tags
Browse files Browse the repository at this point in the history
  • Loading branch information
tsujamin committed Sep 7, 2022
1 parent 470c493 commit 05d8794
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (h *Headscale) initDB() error {
return err
}

err = db.AutoMigrate(&PreAuthKeyAclTag{})
err = db.AutoMigrate(&PreAuthKeyACLTag{})
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion grpcv1.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// nolint
//nolint
package headscale

import (
Expand Down
28 changes: 15 additions & 13 deletions preauth_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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,
Expand All @@ -98,6 +99,7 @@ func (h *Headscale) CreatePreAuthKey(
}
}
}

return nil
})

Expand All @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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,
) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
}

Expand Down
2 changes: 1 addition & 1 deletion preauth_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 0 additions & 2 deletions protocol_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down Expand Up @@ -372,7 +371,6 @@ func (h *Headscale) handleAuthKeyCommon(

return
}

} else {
now := time.Now().UTC()

Expand Down

0 comments on commit 05d8794

Please sign in to comment.