mirror of
https://github.com/juanfont/headscale.git
synced 2026-11-24 22:26:29 +09:00
db: translate driver errors to gorm sentinels
Unique and foreign-key violations reached callers as raw SQLite or Postgres text, so no layer could tell a taken name from a server fault.
This commit is contained in:
1 parent
6c89a68ec8
commit
1cf3b4796d
4 files changed
+43
-11
No files matched your search
+4
-1
@@ -545,6 +545,8 @@ func openDB(cfg types.DatabaseConfig, sqlitePath string) (*gorm.DB, error) {
|
||||
sqlite.Open(connectionURL),
|
||||
&gorm.Config{
|
||||
Logger: dbLogger,
|
||||
// Callers match gorm.ErrDuplicatedKey, not driver text.
|
||||
TranslateError: true,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
@@ -583,7 +585,8 @@ func openDB(cfg types.DatabaseConfig, sqlitePath string) (*gorm.DB, error) {
|
||||
pg.Pass = types.Secret(pass)
|
||||
|
||||
db, err := gorm.Open(postgres.Open(pg.DSN()), &gorm.Config{
|
||||
Logger: dbLogger,
|
||||
Logger: dbLogger,
|
||||
TranslateError: true,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -321,15 +321,10 @@ func createSQLiteFromSQLFile(sqlFilePath, dbPath string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// requireConstraintFailed checks if the error is a constraint failure with
|
||||
// either SQLite and PostgreSQL error messages.
|
||||
// requireConstraintFailed checks that a unique index refused the write.
|
||||
func requireConstraintFailed(t *testing.T, err error) {
|
||||
t.Helper()
|
||||
require.Error(t, err)
|
||||
|
||||
if !strings.Contains(err.Error(), "UNIQUE constraint failed:") && !strings.Contains(err.Error(), "violates unique constraint") {
|
||||
require.Failf(t, "expected error to contain a constraint failure, got: %s", err.Error())
|
||||
}
|
||||
require.ErrorIs(t, err, gorm.ErrDuplicatedKey)
|
||||
}
|
||||
|
||||
func TestConstraints(t *testing.T) {
|
||||
|
||||
@@ -207,7 +207,7 @@ func TestCannotDeleteAssignedPreAuthKey(t *testing.T) {
|
||||
|
||||
err = db.DB.Where("kind = ? AND id = ?", types.CredentialPreAuthKey, key.ID).
|
||||
Delete(&types.Credential{}).Error
|
||||
require.ErrorContains(t, err, "constraint failed: FOREIGN KEY constraint failed")
|
||||
require.ErrorIs(t, err, gorm.ErrForeignKeyViolated)
|
||||
}
|
||||
|
||||
func TestPreAuthKeyAuthentication(t *testing.T) {
|
||||
|
||||
@@ -331,6 +331,41 @@ func TestGetUserErrorPropagation(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestDuplicateUserNameIsTyped pins the root cause: a unique-index violation
|
||||
// must reach callers as a typed error on every backend, not as driver text.
|
||||
func TestDuplicateUserNameIsTyped(t *testing.T) {
|
||||
backends := map[string]func(t *testing.T) *HSDatabase{
|
||||
"sqlite": func(t *testing.T) *HSDatabase {
|
||||
t.Helper()
|
||||
|
||||
db, err := newSQLiteTestDB()
|
||||
require.NoError(t, err)
|
||||
|
||||
return db
|
||||
},
|
||||
"postgres": newPostgresTestDB,
|
||||
}
|
||||
|
||||
for name, open := range backends {
|
||||
t.Run(name+"/create", func(t *testing.T) {
|
||||
db := open(t)
|
||||
db.CreateUserForTest("dup")
|
||||
|
||||
_, err := db.CreateUser(types.User{Name: "dup"})
|
||||
require.ErrorIs(t, err, gorm.ErrDuplicatedKey)
|
||||
})
|
||||
|
||||
t.Run(name+"/rename", func(t *testing.T) {
|
||||
db := open(t)
|
||||
db.CreateUserForTest("dup")
|
||||
other := db.CreateUserForTest("other")
|
||||
|
||||
err := db.RenameUser(types.UserID(other.ID), "dup")
|
||||
require.ErrorIs(t, err, gorm.ErrDuplicatedKey)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenameUser(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -381,8 +416,7 @@ func TestRenameUser(t *testing.T) {
|
||||
assert.Equal(t, "test2", userTest2.Name)
|
||||
|
||||
err := db.RenameUser(types.UserID(userTest2.ID), "test")
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "UNIQUE constraint failed")
|
||||
require.ErrorIs(t, err, gorm.ErrDuplicatedKey)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user