Skip to content

Commit b4ac8cd

Browse files
authored
hscontrol/db: add migration setting non existing pak on nodes to null (#2412)
Signed-off-by: Kristoffer Dalby <[email protected]>
1 parent 22277d1 commit b4ac8cd

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

hscontrol/db/db.go

+27-5
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ COMMIT;
512512

513513
err := tx.AutoMigrate(&types.User{})
514514
if err != nil {
515-
return err
515+
return fmt.Errorf("automigrating types.User: %w", err)
516516
}
517517

518518
return nil
@@ -527,7 +527,7 @@ COMMIT;
527527
Migrate: func(tx *gorm.DB) error {
528528
err := tx.AutoMigrate(&types.User{})
529529
if err != nil {
530-
return err
530+
return fmt.Errorf("automigrating types.User: %w", err)
531531
}
532532

533533
// Set up indexes and unique constraints outside of GORM, it does not support
@@ -575,7 +575,7 @@ COMMIT;
575575

576576
err := tx.AutoMigrate(&types.Route{})
577577
if err != nil {
578-
return err
578+
return fmt.Errorf("automigrating types.Route: %w", err)
579579
}
580580

581581
return nil
@@ -589,11 +589,33 @@ COMMIT;
589589
Migrate: func(tx *gorm.DB) error {
590590
err := tx.AutoMigrate(&types.PreAuthKey{})
591591
if err != nil {
592-
return err
592+
return fmt.Errorf("automigrating types.PreAuthKey: %w", err)
593593
}
594594
err = tx.AutoMigrate(&types.Node{})
595595
if err != nil {
596-
return err
596+
return fmt.Errorf("automigrating types.Node: %w", err)
597+
}
598+
599+
return nil
600+
},
601+
Rollback: func(db *gorm.DB) error { return nil },
602+
},
603+
// Ensure there are no nodes refering to a deleted preauthkey.
604+
{
605+
ID: "202502070949",
606+
Migrate: func(tx *gorm.DB) error {
607+
if tx.Migrator().HasTable(&types.PreAuthKey{}) {
608+
err := tx.Exec(`
609+
UPDATE nodes
610+
SET auth_key_id = NULL
611+
WHERE auth_key_id IS NOT NULL
612+
AND auth_key_id NOT IN (
613+
SELECT id FROM pre_auth_keys
614+
);
615+
`).Error
616+
if err != nil {
617+
return fmt.Errorf("setting auth_key to null on nodes with non-existing keys: %w", err)
618+
}
597619
}
598620

599621
return nil

hscontrol/db/db_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,26 @@ func TestMigrationsSQLite(t *testing.T) {
201201
}
202202
},
203203
},
204+
{
205+
dbPath: "testdata/failing-node-preauth-constraint.sqlite",
206+
wantFunc: func(t *testing.T, h *HSDatabase) {
207+
nodes, err := Read(h.DB, func(rx *gorm.DB) (types.Nodes, error) {
208+
return ListNodes(rx)
209+
})
210+
require.NoError(t, err)
211+
212+
for _, node := range nodes {
213+
assert.Falsef(t, node.MachineKey.IsZero(), "expected non zero machinekey")
214+
assert.Contains(t, node.MachineKey.String(), "mkey:")
215+
assert.Falsef(t, node.NodeKey.IsZero(), "expected non zero nodekey")
216+
assert.Contains(t, node.NodeKey.String(), "nodekey:")
217+
assert.Falsef(t, node.DiscoKey.IsZero(), "expected non zero discokey")
218+
assert.Contains(t, node.DiscoKey.String(), "discokey:")
219+
assert.Nil(t, node.AuthKey)
220+
assert.Nil(t, node.AuthKeyID)
221+
}
222+
},
223+
},
204224
}
205225

206226
for _, tt := range tests {
Binary file not shown.

0 commit comments

Comments
 (0)