Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hscontrol/db: add migration setting non existing pak on nodes to null #2412

Merged
merged 1 commit into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions hscontrol/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ COMMIT;

err := tx.AutoMigrate(&types.User{})
if err != nil {
return err
return fmt.Errorf("automigrating types.User: %w", err)
}

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

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

err := tx.AutoMigrate(&types.Route{})
if err != nil {
return err
return fmt.Errorf("automigrating types.Route: %w", err)
}

return nil
Expand All @@ -589,11 +589,33 @@ COMMIT;
Migrate: func(tx *gorm.DB) error {
err := tx.AutoMigrate(&types.PreAuthKey{})
if err != nil {
return err
return fmt.Errorf("automigrating types.PreAuthKey: %w", err)
}
err = tx.AutoMigrate(&types.Node{})
if err != nil {
return err
return fmt.Errorf("automigrating types.Node: %w", err)
}

return nil
},
Rollback: func(db *gorm.DB) error { return nil },
},
// Ensure there are no nodes refering to a deleted preauthkey.
{
ID: "202502070949",
Migrate: func(tx *gorm.DB) error {
if tx.Migrator().HasTable(&types.PreAuthKey{}) {
err := tx.Exec(`
UPDATE nodes
SET auth_key_id = NULL
WHERE auth_key_id IS NOT NULL
AND auth_key_id NOT IN (
SELECT id FROM pre_auth_keys
);
`).Error
if err != nil {
return fmt.Errorf("setting auth_key to null on nodes with non-existing keys: %w", err)
}
}

return nil
Expand Down
20 changes: 20 additions & 0 deletions hscontrol/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,26 @@ func TestMigrationsSQLite(t *testing.T) {
}
},
},
{
dbPath: "testdata/failing-node-preauth-constraint.sqlite",
wantFunc: func(t *testing.T, h *HSDatabase) {
nodes, err := Read(h.DB, func(rx *gorm.DB) (types.Nodes, error) {
return ListNodes(rx)
})
require.NoError(t, err)

for _, node := range nodes {
assert.Falsef(t, node.MachineKey.IsZero(), "expected non zero machinekey")
assert.Contains(t, node.MachineKey.String(), "mkey:")
assert.Falsef(t, node.NodeKey.IsZero(), "expected non zero nodekey")
assert.Contains(t, node.NodeKey.String(), "nodekey:")
assert.Falsef(t, node.DiscoKey.IsZero(), "expected non zero discokey")
assert.Contains(t, node.DiscoKey.String(), "discokey:")
assert.Nil(t, node.AuthKey)
assert.Nil(t, node.AuthKeyID)
}
},
},
}

for _, tt := range tests {
Expand Down
Binary file not shown.
Loading