Skip to content

Commit ffe2f29

Browse files
committed
do not allow preauth keys to be deleted if assigned to node (#2396)
* do not allow preauth keys to be deleted if assigned to node Signed-off-by: Kristoffer Dalby <[email protected]> * update changelog Signed-off-by: Kristoffer Dalby <[email protected]> --------- Signed-off-by: Kristoffer Dalby <[email protected]>
1 parent 341a3d3 commit ffe2f29

File tree

5 files changed

+57
-4
lines changed

5 files changed

+57
-4
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# CHANGELOG
22

3+
## 0.24.3 (2025-02-07)
4+
5+
### Changes
6+
- Pre auth keys belonging to a user are no longer deleted with the user
7+
[#2396](https://github.com/juanfont/headscale/pull/2396)
8+
- Pre auth keys that are used by a node can no longer be deleted
9+
[#2396](https://github.com/juanfont/headscale/pull/2396)
310

411
## 0.24.2 (2025-01-30)
512

hscontrol/db/db.go

+18
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,24 @@ COMMIT;
582582
},
583583
Rollback: func(db *gorm.DB) error { return nil },
584584
},
585+
// Add back constraint so you cannot delete preauth keys that
586+
// is still used by a node.
587+
{
588+
ID: "202501311657",
589+
Migrate: func(tx *gorm.DB) error {
590+
err := tx.AutoMigrate(&types.PreAuthKey{})
591+
if err != nil {
592+
return err
593+
}
594+
err = tx.AutoMigrate(&types.Node{})
595+
if err != nil {
596+
return err
597+
}
598+
599+
return nil
600+
},
601+
Rollback: func(db *gorm.DB) error { return nil },
602+
},
585603
},
586604
)
587605

hscontrol/db/preauth_keys_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ package db
22

33
import (
44
"sort"
5+
"testing"
56
"time"
67

78
"github.com/juanfont/headscale/hscontrol/types"
89
"github.com/juanfont/headscale/hscontrol/util"
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
912
"gopkg.in/check.v1"
1013
"tailscale.com/types/ptr"
1114
)
@@ -175,3 +178,25 @@ func (*Suite) TestPreAuthKeyACLTags(c *check.C) {
175178
sort.Sort(sort.StringSlice(gotTags))
176179
c.Assert(gotTags, check.DeepEquals, tags)
177180
}
181+
182+
func TestCannotDeleteAssignedPreAuthKey(t *testing.T) {
183+
db, err := newSQLiteTestDB()
184+
require.NoError(t, err)
185+
user, err := db.CreateUser(types.User{Name: "test8"})
186+
assert.NoError(t, err)
187+
188+
key, err := db.CreatePreAuthKey(types.UserID(user.ID), false, false, nil, []string{"tag:good"})
189+
assert.NoError(t, err)
190+
191+
node := types.Node{
192+
ID: 0,
193+
Hostname: "testest",
194+
UserID: user.ID,
195+
RegisterMethod: util.RegisterMethodAuthKey,
196+
AuthKeyID: ptr.To(key.ID),
197+
}
198+
db.DB.Save(&node)
199+
200+
err = db.DB.Delete(key).Error
201+
require.ErrorContains(t, err, "constraint failed: FOREIGN KEY constraint failed")
202+
}

hscontrol/types/node.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,12 @@ type Node struct {
7777

7878
ForcedTags []string `gorm:"serializer:json"`
7979

80-
// TODO(kradalby): This seems like irrelevant information?
81-
AuthKeyID *uint64 `sql:"DEFAULT:NULL"`
82-
AuthKey *PreAuthKey `gorm:"constraint:OnDelete:SET NULL;"`
80+
// When a node has been created with a PreAuthKey, we need to
81+
// prevent the preauthkey from being deleted before the node.
82+
// The preauthkey can define "tags" of the node so we need it
83+
// around.
84+
AuthKeyID *uint64 `sql:"DEFAULT:NULL"`
85+
AuthKey *PreAuthKey
8386

8487
LastSeen *time.Time
8588
Expiry *time.Time

hscontrol/types/preauth_key.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type PreAuthKey struct {
1414
ID uint64 `gorm:"primary_key"`
1515
Key string
1616
UserID uint
17-
User User `gorm:"constraint:OnDelete:CASCADE;"`
17+
User User `gorm:"constraint:OnDelete:SET NULL;"`
1818
Reusable bool
1919
Ephemeral bool `gorm:"default:false"`
2020
Used bool `gorm:"default:false"`

0 commit comments

Comments
 (0)