PostgreSQL backup tool with Point-In-Time-Recovery and restore verification
TypeScript backupbackupsdatabasedatabase-backupdevopsdockergolangkubernetesmariadbmongodbmysqlpgpostgrespostgresqls3self-hostedsystem-administrationtoolsweb-ui
FIX (databases): Verify minimum dump privileges in TestConnection for all logical engines (issue #749)
Rostislav Dugin07c76ea8cd21
diff --git a/backend/internal/features/backups/backups/usecases/logical/mariadb/create_backup_uc.go b/backend/internal/features/backups/backups/usecases/logical/mariadb/create_backup_uc.goindex 5afeb73c..a6e30606 100644--- a/backend/internal/features/backups/backups/usecases/logical/mariadb/create_backup_uc.go+++ b/backend/internal/features/backups/backups/usecases/logical/mariadb/create_backup_uc.go@@ -135,10 +135,18 @@ func (uc *CreateMariadbBackupUsecase) buildMariadbDumpArgs("--quick","--skip-add-locks","--verbose",+ // Dumping tablespace definitions makes mariadb-dump read INFORMATION_SCHEMA.FILES, which costs+ // a global PROCESS privilege that a backup role has no other use for and that managed+ // providers often refuse to grant.+ "--no-tablespaces",}+ // Triggers are on by default, so a role without the privilege has to be opted out explicitly+ // or mariadb-dump fails on SHOW TRIGGERS instead of dumping without them.if mdb.HasPrivilege("TRIGGER") {args = append(args, "--triggers")+ } else {+ args = append(args, "--skip-triggers")}if mdb.HasPrivilege("EVENT") && !mdb.IsExcludeEvents {diff --git a/backend/internal/features/backups/backups/usecases/logical/mariadb/create_backup_uc_test.go b/backend/internal/features/backups/backups/usecases/logical/mariadb/create_backup_uc_test.goindex ba01bc99..147e9b3a 100644--- a/backend/internal/features/backups/backups/usecases/logical/mariadb/create_backup_uc_test.go+++ b/backend/internal/features/backups/backups/usecases/logical/mariadb/create_backup_uc_test.go@@ -77,6 +77,57 @@ func Test_BuildMariadbDumpArgs_WhenExcludedTablesArePastedMultiline_TrimsAndSpli}}+// Dumping tablespace definitions reads INFORMATION_SCHEMA.FILES, which costs a global PROCESS+// privilege the connection test no longer requires, so the flag has to stay in every dump.+func Test_BuildMariadbDumpArgs_ForAnyDatabase_AlwaysSkipsTablespaces(t *testing.T) {+ uc := &CreateMariadbBackupUsecase{}+ databaseName := "oa_db"+ database := &mariadbtypes.MariadbDatabase{+ Version: tools.MariadbVersion1011,+ Database: &databaseName,+ }++ args := uc.buildMariadbDumpArgs(database)++ if !slices.Contains(args, "--no-tablespaces") {+ t.Fatalf("expected --no-tablespaces, got %v", args)+ }+}++func Test_BuildMariadbDumpArgs_WithTriggerPrivilege_KeepsTriggers(t *testing.T) {+ uc := &CreateMariadbBackupUsecase{}+ databaseName := "oa_db"+ database := &mariadbtypes.MariadbDatabase{+ Version: tools.MariadbVersion1011,+ Database: &databaseName,+ Privileges: "SELECT,SHOW VIEW,TRIGGER",+ }++ args := uc.buildMariadbDumpArgs(database)++ if !slices.Contains(args, "--triggers") || slices.Contains(args, "--skip-triggers") {+ t.Fatalf("expected --triggers and no --skip-triggers, got %v", args)+ }+}++// Triggers are on by default, so without the explicit opt-out mariadb-dump fails on SHOW TRIGGERS+// for a role that lacks the privilege instead of dumping without them.+func Test_BuildMariadbDumpArgs_WithoutTriggerPrivilege_SkipsTriggers(t *testing.T) {+ uc := &CreateMariadbBackupUsecase{}+ databaseName := "oa_db"+ database := &mariadbtypes.MariadbDatabase{+ Version: tools.MariadbVersion1011,+ Database: &databaseName,+ Privileges: "SELECT,SHOW VIEW",+ }++ args := uc.buildMariadbDumpArgs(database)++ if !slices.Contains(args, "--skip-triggers") || slices.Contains(args, "--triggers") {+ t.Fatalf("expected --skip-triggers and no --triggers, got %v", args)+ }+}+func Test_BuildMariadbDumpArgs_WithoutExcludedTables_OmitsIgnoreTableArgs(t *testing.T) {uc := &CreateMariadbBackupUsecase{}databaseName := "oa_db"diff --git a/backend/internal/features/backups/backups/usecases/logical/mysql/create_backup_uc.go b/backend/internal/features/backups/backups/usecases/logical/mysql/create_backup_uc.goindex 6a97b235..96b47d50 100644--- a/backend/internal/features/backups/backups/usecases/logical/mysql/create_backup_uc.go+++ b/backend/internal/features/backups/backups/usecases/logical/mysql/create_backup_uc.go@@ -134,11 +134,20 @@ func (uc *CreateMysqlBackupUsecase) buildMysqldumpArgs(my *mysqltypes.MysqlDatab"--quick","--skip-add-locks","--verbose",+ // Dumping tablespace definitions makes mysqldump read INFORMATION_SCHEMA.FILES, which costs+ // a global PROCESS privilege that a backup role has no other use for and that managed+ // providers often refuse to grant.+ "--no-tablespaces",}+ // Triggers are on by default, so a role without the privilege has to be opted out explicitly+ // or mysqldump fails on SHOW TRIGGERS instead of dumping without them.if my.HasPrivilege("TRIGGER") {args = append(args, "--triggers")+ } else {+ args = append(args, "--skip-triggers")}+if my.HasPrivilege("EVENT") {args = append(args, "--events")}diff --git a/backend/internal/features/backups/backups/usecases/logical/mysql/create_backup_uc_test.go b/backend/internal/features/backups/backups/usecases/logical/mysql/create_backup_uc_test.goindex b9f71970..db1817ee 100644--- a/backend/internal/features/backups/backups/usecases/logical/mysql/create_backup_uc_test.go+++ b/backend/internal/features/backups/backups/usecases/logical/mysql/create_backup_uc_test.go@@ -77,6 +77,57 @@ func Test_BuildMysqldumpArgs_WhenExcludedTablesArePastedMultiline_TrimsAndSplits}}+// Dumping tablespace definitions reads INFORMATION_SCHEMA.FILES, which costs a global PROCESS+// privilege the connection test no longer requires, so the flag has to stay in every dump.+func Test_BuildMysqldumpArgs_ForAnyDatabase_AlwaysSkipsTablespaces(t *testing.T) {+ uc := &CreateMysqlBackupUsecase{}+ databaseName := "oa_db"+ database := &mysqltypes.MysqlDatabase{+ Version: tools.MysqlVersion80,+ Database: &databaseName,+ }++ args := uc.buildMysqldumpArgs(database)++ if !slices.Contains(args, "--no-tablespaces") {+ t.Fatalf("expected --no-tablespaces, got %v", args)+ }+}++func Test_BuildMysqldumpArgs_WithTriggerPrivilege_KeepsTriggers(t *testing.T) {+ uc := &CreateMysqlBackupUsecase{}+ databaseName := "oa_db"+ database := &mysqltypes.MysqlDatabase{+ Version: tools.MysqlVersion80,+ Database: &databaseName,+ Privileges: "SELECT,SHOW VIEW,TRIGGER",+ }++ args := uc.buildMysqldumpArgs(database)++ if !slices.Contains(args, "--triggers") || slices.Contains(args, "--skip-triggers") {+ t.Fatalf("expected --triggers and no --skip-triggers, got %v", args)+ }+}++// Triggers are on by default, so without the explicit opt-out mysqldump fails on SHOW TRIGGERS+// for a role that lacks the privilege instead of dumping without them.+func Test_BuildMysqldumpArgs_WithoutTriggerPrivilege_SkipsTriggers(t *testing.T) {+ uc := &CreateMysqlBackupUsecase{}+ databaseName := "oa_db"+ database := &mysqltypes.MysqlDatabase{+ Version: tools.MysqlVersion80,+ Database: &databaseName,+ Privileges: "SELECT,SHOW VIEW",+ }++ args := uc.buildMysqldumpArgs(database)++ if !slices.Contains(args, "--skip-triggers") || slices.Contains(args, "--triggers") {+ t.Fatalf("expected --skip-triggers and no --triggers, got %v", args)+ }+}+func Test_BuildMysqldumpArgs_WithoutExcludedTables_OmitsIgnoreTableArgs(t *testing.T) {uc := &CreateMysqlBackupUsecase{}databaseName := "oa_db"diff --git a/backend/internal/features/databases/databases/mariadb/model.go b/backend/internal/features/databases/databases/mariadb/model.goindex 99e4f4dc..0bc07ee6 100644--- a/backend/internal/features/databases/databases/mariadb/model.go+++ b/backend/internal/features/databases/databases/mariadb/model.go@@ -9,7 +9,6 @@ import ("log/slog""regexp""slices"- "sort""strings""time"@@ -17,6 +16,7 @@ import ("github.com/google/uuid""gorm.io/gorm"+ "databasus-backend/internal/features/databases/databases/mysqlfamily""databasus-backend/internal/features/sshtunnel""databasus-backend/internal/util/encryption""databasus-backend/internal/util/namelist"@@ -123,14 +123,20 @@ func (m *MariadbDatabase) TestConnection(}m.Version = detectedVersion- privileges, err := detectPrivileges(ctx, db, *m.Database)+ backupPrivileges, err := readBackupPrivileges(ctx, backupPrivilegesQuery{+ DB: db,+ Logger: logger,+ Version: m.Version,+ SchemaName: *m.Database,+ ExcludeTables: m.ExcludeTables,+ })if err != nil {return err}- m.Privileges = privileges+ m.Privileges = backupPrivileges.GetEffectivePrivilegesCsv()- if err := checkBackupPermissions(m.Privileges); err != nil {- return err+ if !backupPrivileges.IsSufficientForDump() {+ return backupPrivileges.NewInsufficiencyError()}return nil@@ -271,11 +277,17 @@ func (m *MariadbDatabase) PopulateDbData(}m.Version = detectedVersion- privileges, err := detectPrivileges(ctx, db, *m.Database)+ backupPrivileges, err := readBackupPrivileges(ctx, backupPrivilegesQuery{+ DB: db,+ Logger: logger,+ Version: m.Version,+ SchemaName: *m.Database,+ ExcludeTables: m.ExcludeTables,+ })if err != nil {return err}- m.Privileges = privileges+ m.Privileges = backupPrivileges.GetEffectivePrivilegesCsv()return nil}@@ -376,9 +388,14 @@ func (m *MariadbDatabase) ShouldSuggestReadOnlyUser(return false, nil, fmt.Errorf("failed to scan grant: %w", err)}- for _, priv := range parseGrantPrivileges(grant) {- if writePrivileges[priv] {- detectedPrivileges[priv] = true+ parsedGrant := mysqlfamily.ParseGrantLine(grant)+ if parsedGrant == nil {+ continue+ }++ for _, privilege := range parsedGrant.Privileges {+ if writePrivileges[privilege] {+ detectedPrivileges[privilege] = true}}}@@ -460,14 +477,6 @@ func (m *MariadbDatabase) CreateReadOnlyUser(return "", "", fmt.Errorf("failed to grant database privileges: %w", err)}- _, err = tx.ExecContext(ctx, fmt.Sprintf(- "GRANT PROCESS ON *.* TO '%s'@'%%'",- newUsername,- ))- if err != nil {- return "", "", fmt.Errorf("failed to grant PROCESS privilege: %w", err)- }-_, err = tx.ExecContext(ctx, "FLUSH PRIVILEGES")if err != nil {return "", "", fmt.Errorf("failed to flush privileges: %w", err)@@ -489,19 +498,6 @@ func (m *MariadbDatabase) CreateReadOnlyUser(return "", "", errors.New("failed to generate unique username after 3 attempts")}-func (m *MariadbDatabase) HasPrivilege(priv string) bool {- return HasPrivilege(m.Privileges, priv)-}--func HasPrivilege(privileges, priv string) bool {- for p := range strings.SplitSeq(privileges, ",") {- if strings.TrimSpace(p) == priv {- return true- }- }- return false-}-func (m *MariadbDatabase) buildDSN(password, database string) string {tlsConfig := "false"@@ -604,139 +600,6 @@ func mapMariadb11xVersion(minor string) (tools.MariadbVersion, error) {}}-// parseGrantPrivileges extracts comma-separated privilege names from a single-// SHOW GRANTS line. Returns uppercased privilege tokens with column-level-// qualifiers like "(col1, col2)" stripped. Returns nil for role grants and-// other lines without an ON clause.-//-// Parsing the privilege list is necessary because a naive substring search-// over the whole grant string falsely matches privilege keywords that appear-// inside other privilege names — e.g. "SHOW CREATE ROUTINE" contains both-// "CREATE" and "CREATE ROUTINE" as substrings.-func parseGrantPrivileges(grant string) []string {- headRe := regexp.MustCompile(`(?is)^\s*GRANT\s+(.+?)\s+ON\s+`)- m := headRe.FindStringSubmatch(grant)- if m == nil {- return nil- }-- colRe := regexp.MustCompile(`\s*\([^)]*\)`)- privsStr := colRe.ReplaceAllString(m[1], "")-- parts := strings.Split(privsStr, ",")- privs := make([]string, 0, len(parts))- for _, p := range parts {- p = strings.TrimSpace(strings.ToUpper(p))- if p != "" {- privs = append(privs, p)- }- }-- return privs-}--// detectPrivileges detects backup-related privileges and returns them as comma-separated string-func detectPrivileges(ctx context.Context, db *sql.DB, database string) (string, error) {- rows, err := db.QueryContext(ctx, "SHOW GRANTS FOR CURRENT_USER()")- if err != nil {- return "", fmt.Errorf("failed to check grants: %w", err)- }- defer func() { _ = rows.Close() }()-- backupPrivileges := []string{- "SELECT", "SHOW VIEW", "LOCK TABLES", "TRIGGER", "EVENT",- }-- detectedPrivileges := make(map[string]bool)- hasProcess := false- hasAllPrivileges := false-- // Escape underscores to match MariaDB's grant output format- // MariaDB escapes _ as \_ in SHOW GRANTS output- // Pattern matches either literal _ or escaped \_- escapedDbName := strings.ReplaceAll(regexp.QuoteMeta(database), "_", `(_|\\_)`)- dbPatternStr := fmt.Sprintf(- `(?i)ON\s+[\x60'"]?%s[\x60'"]?\s*\.\s*\*`,- escapedDbName,- )- dbPattern := regexp.MustCompile(dbPatternStr)- globalPattern := regexp.MustCompile(`(?i)ON\s+\*\s*\.\s*\*`)- allPrivilegesPattern := regexp.MustCompile(`(?i)\bALL\s+PRIVILEGES\b`)-- for rows.Next() {- var grant string- if err := rows.Scan(&grant); err != nil {- return "", fmt.Errorf("failed to scan grant: %w", err)- }-- isRelevantGrant := globalPattern.MatchString(grant) || dbPattern.MatchString(grant)-- if allPrivilegesPattern.MatchString(grant) && isRelevantGrant {- hasAllPrivileges = true- }-- if isRelevantGrant {- for _, priv := range backupPrivileges {- privPattern := regexp.MustCompile(`(?i)\b` + regexp.QuoteMeta(priv) + `\b`)- if privPattern.MatchString(grant) {- detectedPrivileges[priv] = true- }- }- }-- if globalPattern.MatchString(grant) {- processPattern := regexp.MustCompile(`(?i)\bPROCESS\b`)- if processPattern.MatchString(grant) {- hasProcess = true- }- }- }-- if err := rows.Err(); err != nil {- return "", fmt.Errorf("error iterating grants: %w", err)- }-- if hasAllPrivileges {- for _, priv := range backupPrivileges {- detectedPrivileges[priv] = true- }- hasProcess = true- }-- privileges := make([]string, 0, len(detectedPrivileges)+1)- for priv := range detectedPrivileges {- privileges = append(privileges, priv)- }- if hasProcess {- privileges = append(privileges, "PROCESS")- }-- sort.Strings(privileges)- return strings.Join(privileges, ","), nil-}--// checkBackupPermissions verifies the user has sufficient privileges for mariadb-dump backup.-// Required: SELECT, SHOW VIEW-func checkBackupPermissions(privileges string) error {- requiredPrivileges := []string{"SELECT", "SHOW VIEW"}-- var missingPrivileges []string- for _, priv := range requiredPrivileges {- if !HasPrivilege(privileges, priv) {- missingPrivileges = append(missingPrivileges, priv)- }- }-- if len(missingPrivileges) > 0 {- return fmt.Errorf(- "insufficient permissions for backup. Missing: %s. Required: SELECT, SHOW VIEW",- strings.Join(missingPrivileges, ", "),- )- }-- return nil-}-func decryptPasswordIfNeeded(password string,encryptor encryption.FieldEncryptor,diff --git a/backend/internal/features/databases/databases/mariadb/model_test.go b/backend/internal/features/databases/databases/mariadb/model_test.goindex 3169099b..92836f01 100644--- a/backend/internal/features/databases/databases/mariadb/model_test.go+++ b/backend/internal/features/databases/databases/mariadb/model_test.go@@ -62,6 +62,34 @@ func Test_MariadbModel_AcrossSupportedVersions(t *testing.T) {testTestConnectionDatabaseWithUnderscoresAndAllPrivileges(t, endpoint, dbVersion.version)})+ t.Run("Test_TestConnection_WhenBackupPrivilegesComeFromActiveRole_Success", func(t *testing.T) {+ testTestConnectionBackupPrivilegesFromActiveRole(t, endpoint, dbVersion.version)+ })++ t.Run("Test_TestConnection_WhenBackupPrivilegesComeFromNestedRole_Success", func(t *testing.T) {+ testTestConnectionBackupPrivilegesFromNestedRole(t, endpoint, dbVersion.version)+ })++ t.Run("Test_TestConnection_WhenRoleIsGrantedButNotActivated_ReturnsError", func(t *testing.T) {+ testTestConnectionRoleGrantedButNotActivated(t, endpoint, dbVersion.version)+ })++ t.Run("Test_TestConnection_WhenTableGrantsCoverEveryVisibleTable_Success", func(t *testing.T) {+ testTestConnectionTableGrantsCoverEveryVisibleTable(t, endpoint, dbVersion.version)+ })++ t.Run("Test_TestConnection_WhenVisibleTableLacksSelect_ReturnsError", func(t *testing.T) {+ testTestConnectionVisibleTableLacksSelect(t, endpoint, dbVersion.version)+ })++ t.Run("Test_TestConnection_WhenVisibleTableLackingSelectIsExcluded_Success", func(t *testing.T) {+ testTestConnectionVisibleTableLackingSelectIsExcluded(t, endpoint, dbVersion.version)+ })++ t.Run("Test_TestConnection_WhenViewLacksShowView_ReturnsErrorUntilShowViewIsGranted", func(t *testing.T) {+ testTestConnectionViewLacksShowView(t, endpoint, dbVersion.version)+ })+if dbVersion.runsRoutineGrantTest {t.Run("Test_ShouldSuggestReadOnlyUser_WithShowCreateRoutineGrant_ReturnsFalse", func(t *testing.T) {testShowCreateRoutineGrant(t, endpoint, dbVersion.version)@@ -101,8 +129,10 @@ func testTestConnectionInsufficientPermissions())assert.NoError(t, err)+ // INSERT makes the schema and its tables visible without making them readable, which is what+ // the dump tool aborts on._, err = container.DB.Exec(fmt.Sprintf(- "GRANT SELECT ON `%s`.* TO '%s'@'%%'",+ "GRANT INSERT ON `%s`.* TO '%s'@'%%'",container.Database,limitedUsername,))@@ -126,8 +156,10 @@ func testTestConnectionInsufficientPermissions(logger := slog.New(slog.NewTextHandler(os.Stdout, nil))err = mariadbModel.TestConnection(logger, nil)- assert.Error(t, err)- assert.Contains(t, err.Error(), "insufficient permissions")+ if assert.Error(t, err) {+ assert.Contains(t, err.Error(), "insufficient permissions")+ assert.Contains(t, err.Error(), "SELECT")+ }}func testTestConnectionSufficientPermissions(@@ -167,12 +199,6 @@ func testTestConnectionSufficientPermissions())assert.NoError(t, err)- _, err = container.DB.Exec(fmt.Sprintf(- "GRANT PROCESS ON *.* TO '%s'@'%%'",- backupUsername,- ))- assert.NoError(t, err)-_, err = container.DB.Exec("FLUSH PRIVILEGES")assert.NoError(t, err)@@ -831,72 +857,6 @@ func testShowCreateRoutineGrant(assert.Empty(t, privileges)}-func Test_ParseGrantPrivileges_ReturnsExpectedTokens(t *testing.T) {- cases := []struct {- name string- grant string- want []string- }{- {- "issue-568 SHOW CREATE ROUTINE not split into CREATE",- "GRANT SELECT, SHOW VIEW, SHOW CREATE ROUTINE ON *.* TO 'backup'@'%'",- []string{"SELECT", "SHOW VIEW", "SHOW CREATE ROUTINE"},- },- {- "standard write privs",- "GRANT SELECT, INSERT, UPDATE ON *.* TO 'x'@'%'",- []string{"SELECT", "INSERT", "UPDATE"},- },- {- "ALL PRIVILEGES",- "GRANT ALL PRIVILEGES ON db.* TO 'x'@'%'",- []string{"ALL PRIVILEGES"},- },- {- "USAGE-only line",- "GRANT USAGE ON *.* TO 'x'@'%'",- []string{"USAGE"},- },- {- "column-level qualifiers stripped",- "GRANT SELECT (col1, col2), UPDATE (col3) ON db.t TO 'x'@'%'",- []string{"SELECT", "UPDATE"},- },- {- "role grant (no ON clause) returns nil",- "GRANT my_role TO 'u'@'%'",- nil,- },- {- "PROXY grant",- "GRANT PROXY ON 'other'@'%' TO 'u'@'%'",- []string{"PROXY"},- },- {- "WITH GRANT OPTION trailer ignored",- "GRANT SELECT, INSERT ON *.* TO 'x'@'%' WITH GRANT OPTION",- []string{"SELECT", "INSERT"},- },- {- "mixed case GRANT/ON",- "grant Select, Update on *.* to 'x'@'%'",- []string{"SELECT", "UPDATE"},- },- {- "column literally named ON inside parens",- "GRANT SELECT (on) ON db.t TO 'x'@'%'",- []string{"SELECT"},- },- }-- for _, tc := range cases {- t.Run(tc.name, func(t *testing.T) {- got := parseGrantPrivileges(tc.grant)- assert.Equal(t, tc.want, got)- })- }-}-func Test_HideSensitiveData_WhenCalled_ClearsPasswordAndPreservesOtherFields(t *testing.T) {databaseName := "appdb"mariadbModel := &MariadbDatabase{diff --git a/backend/internal/features/databases/databases/mariadb/privileges.go b/backend/internal/features/databases/databases/mariadb/privileges.gonew file mode 100644index 00000000..b1ec7e5f--- /dev/null+++ b/backend/internal/features/databases/databases/mariadb/privileges.go@@ -0,0 +1,96 @@+package mariadb++import (+ "context"+ "database/sql"+ "log/slog"+ "strings"++ "databasus-backend/internal/features/databases/databases/mysqlfamily"+ "databasus-backend/internal/util/tools"+)++type backupPrivilegesQuery struct {+ DB *sql.DB+ Logger *slog.Logger+ Version tools.MariadbVersion+ SchemaName string+ ExcludeTables []string+}++func readBackupPrivileges(+ ctx context.Context,+ query backupPrivilegesQuery,+) (mysqlfamily.BackupPrivileges, error) {+ grantLines, err := readGrantLines(ctx, query)+ if err != nil {+ return mysqlfamily.BackupPrivileges{}, err+ }++ return mysqlfamily.ReadBackupPrivileges(ctx, mysqlfamily.BackupPrivilegesQuery{+ DB: query.DB,+ SchemaName: query.SchemaName,+ ExcludeTables: query.ExcludeTables,+ GrantLines: grantLines,+ })+}++func readGrantLines(ctx context.Context, query backupPrivilegesQuery) ([]string, error) {+ grantLines, err := mysqlfamily.QueryGrantLines(ctx, query.DB, "SHOW GRANTS FOR CURRENT_USER")+ if err != nil {+ return nil, err+ }++ if query.Version == tools.MariadbVersion55 {+ return grantLines, nil+ }++ activeRole, err := readActiveRole(ctx, query.DB)+ if err != nil {+ query.Logger.WarnContext(ctx, "failed to read the active role", "error", err)++ return grantLines, nil+ }++ if activeRole == "" {+ return grantLines, nil+ }++ // SHOW GRANTS FOR a role already includes every role granted to it, transitively, so one+ // query covers the whole hierarchy behind the active role.+ activeRoleGrantLines, err := mysqlfamily.QueryGrantLines(+ ctx,+ query.DB,+ "SHOW GRANTS FOR "+quoteRoleName(activeRole),+ )+ if err != nil {+ query.Logger.WarnContext(ctx, "failed to read role grants", "error", err)++ return grantLines, nil+ }++ return append(grantLines, activeRoleGrantLines...), nil+}++// MariaDB activates a single role at a time and reports it without a host part, unlike MySQL,+// which can hold several and needs them spliced into a USING clause.+func readActiveRole(ctx context.Context, db *sql.DB) (string, error) {+ var activeRole sql.NullString+ if err := db.QueryRowContext(ctx, "SELECT CURRENT_ROLE()").Scan(&activeRole); err != nil {+ return "", err+ }++ if !activeRole.Valid {+ return "", nil+ }++ return activeRole.String, nil+}++func quoteRoleName(roleName string) string {+ return "`" + strings.ReplaceAll(roleName, "`", "``") + "`"+}++func (m *MariadbDatabase) HasPrivilege(privilege string) bool {+ return mysqlfamily.HasPrivilege(m.Privileges, privilege)+}diff --git a/backend/internal/features/databases/databases/mariadb/privileges_test.go b/backend/internal/features/databases/databases/mariadb/privileges_test.gonew file mode 100644index 00000000..948c5124--- /dev/null+++ b/backend/internal/features/databases/databases/mariadb/privileges_test.go@@ -0,0 +1,353 @@+package mariadb++import (+ "fmt"+ "log/slog"+ "os"+ "testing"++ "github.com/google/uuid"+ "github.com/stretchr/testify/assert"+ "github.com/stretchr/testify/require"++ "databasus-backend/internal/util/testing/containers"+ "databasus-backend/internal/util/tools"+)++// The version's subtests share one server (ADR-0013), so every name carries a suffix and the+// tables a case creates are dropped again before the next one runs.+type backupPrivilegeFixture struct {+ Container *MariadbContainer+ Suffix string+ Username string+ RoleName string+ Model *MariadbDatabase+}++func createBackupPrivilegeFixture(+ t *testing.T,+ endpoint containers.Endpoint,+ version tools.MariadbVersion,+) backupPrivilegeFixture {+ t.Helper()++ container := connectToMariadbEndpoint(t, endpoint, version)+ t.Cleanup(func() { container.DB.Close() })++ suffix := uuid.New().String()[:8]+ username := fmt.Sprintf("backup_%s", suffix)+ password := "backuprolepassword123"++ _, err := container.DB.Exec(fmt.Sprintf(+ "CREATE USER '%s'@'%%' IDENTIFIED BY '%s'",+ username,+ password,+ ))+ require.NoError(t, err)++ t.Cleanup(func() {+ _, _ = container.DB.Exec(fmt.Sprintf("DROP USER IF EXISTS '%s'@'%%'", username))+ })++ return backupPrivilegeFixture{+ Container: container,+ Suffix: suffix,+ Username: username,+ RoleName: fmt.Sprintf("backup_role_%s", suffix),+ Model: &MariadbDatabase{+ Version: version,+ Host: container.Host,+ Port: container.Port,+ Username: username,+ Password: password,+ Database: &container.Database,+ },+ }+}++func (f backupPrivilegeFixture) executeStatements(t *testing.T, statements ...string) {+ t.Helper()++ for _, statement := range statements {+ _, err := f.Container.DB.Exec(statement)+ require.NoError(t, err)+ }+}++// A MariaDB role carries no host part, unlike a MySQL one.+func (f backupPrivilegeFixture) createRole(t *testing.T) {+ t.Helper()++ f.executeStatements(t, fmt.Sprintf("CREATE ROLE %s", f.RoleName))++ t.Cleanup(func() {+ _, _ = f.Container.DB.Exec(fmt.Sprintf("DROP ROLE IF EXISTS %s", f.RoleName))+ })+}++// Every table in the schema joins the dump set, so a case granting per table has to drop the+// tables it created or it would decide the outcome of the cases that follow.+func (f backupPrivilegeFixture) createTables(t *testing.T, tableNames ...string) {+ t.Helper()++ for _, tableName := range tableNames {+ f.executeStatements(t, fmt.Sprintf("CREATE TABLE `%s` (id INT PRIMARY KEY)", tableName))++ t.Cleanup(func() {+ _, _ = f.Container.DB.Exec(fmt.Sprintf("DROP TABLE IF EXISTS `%s`", tableName))+ })+ }+}++func (f backupPrivilegeFixture) createView(t *testing.T, viewName, baseTableName string) {+ t.Helper()++ f.executeStatements(t, fmt.Sprintf(+ "CREATE VIEW `%s` AS SELECT id FROM `%s`",+ viewName,+ baseTableName,+ ))++ t.Cleanup(func() {+ _, _ = f.Container.DB.Exec(fmt.Sprintf("DROP VIEW IF EXISTS `%s`", viewName))+ })+}++func (f backupPrivilegeFixture) testConnection() error {+ return f.Model.TestConnection(slog.New(slog.NewTextHandler(os.Stdout, nil)), nil)+}++func Test_QuoteRoleName_WithEmbeddedBackquote_EscapesIt(t *testing.T) {+ assert.Equal(t, "`plain_role`", quoteRoleName("plain_role"))+ assert.Equal(t, "`odd``role`", quoteRoleName("odd`role"))+}++func testTestConnectionBackupPrivilegesFromActiveRole(+ t *testing.T,+ endpoint containers.Endpoint,+ version tools.MariadbVersion,+) {+ fixture := createBackupPrivilegeFixture(t, endpoint, version)+ fixture.createRole(t)++ fixture.executeStatements(t,+ fmt.Sprintf(+ "GRANT SELECT, SHOW VIEW, LOCK TABLES, TRIGGER, EVENT ON `%s`.* TO %s",+ fixture.Container.Database,+ fixture.RoleName,+ ),+ fmt.Sprintf("GRANT %s TO '%s'@'%%'", fixture.RoleName, fixture.Username),+ fmt.Sprintf("SET DEFAULT ROLE %s FOR '%s'@'%%'", fixture.RoleName, fixture.Username),+ "FLUSH PRIVILEGES",+ )++ require.NoError(t, fixture.testConnection())+ assert.Contains(t, fixture.Model.Privileges, "SELECT")+ assert.Contains(t, fixture.Model.Privileges, "SHOW VIEW")+}++// The account's SELECT arrives only through a role granted to its active role, so this is what+// pins that a single SHOW GRANTS FOR the active role really carries the transitively inherited+// grants: read only the user's own grants and the check falsely reports missing privileges.+func testTestConnectionBackupPrivilegesFromNestedRole(+ t *testing.T,+ endpoint containers.Endpoint,+ version tools.MariadbVersion,+) {+ fixture := createBackupPrivilegeFixture(t, endpoint, version)+ fixture.createRole(t)+ nestedRoleName := "nested_role_" + fixture.Suffix++ fixture.executeStatements(t, fmt.Sprintf("CREATE ROLE %s", nestedRoleName))+ t.Cleanup(func() {+ _, _ = fixture.Container.DB.Exec(fmt.Sprintf("DROP ROLE IF EXISTS %s", nestedRoleName))+ })++ fixture.executeStatements(t,+ fmt.Sprintf(+ "GRANT SELECT, SHOW VIEW ON `%s`.* TO %s",+ fixture.Container.Database,+ nestedRoleName,+ ),+ fmt.Sprintf("GRANT %s TO %s", nestedRoleName, fixture.RoleName),+ fmt.Sprintf("GRANT %s TO '%s'@'%%'", fixture.RoleName, fixture.Username),+ fmt.Sprintf("SET DEFAULT ROLE %s FOR '%s'@'%%'", fixture.RoleName, fixture.Username),+ "FLUSH PRIVILEGES",+ )++ require.NoError(t, fixture.testConnection())+ assert.Contains(t, fixture.Model.Privileges, "SELECT")+ assert.Contains(t, fixture.Model.Privileges, "SHOW VIEW")+}++// mariadb-dump logs in fresh and a granted-but-inactive role gives it nothing, so reading only the+// active role is what matches what the dump will have. The direct INSERT grant is what lets the+// account reach the schema at all and puts the table in the dump set.+func testTestConnectionRoleGrantedButNotActivated(+ t *testing.T,+ endpoint containers.Endpoint,+ version tools.MariadbVersion,+) {+ fixture := createBackupPrivilegeFixture(t, endpoint, version)+ fixture.createRole(t)+ blockedTableName := "blocked_" + fixture.Suffix++ fixture.createTables(t, blockedTableName)++ fixture.executeStatements(t,+ fmt.Sprintf(+ "GRANT INSERT ON `%s`.`%s` TO '%s'@'%%'",+ fixture.Container.Database,+ blockedTableName,+ fixture.Username,+ ),+ fmt.Sprintf(+ "GRANT SELECT, SHOW VIEW ON `%s`.* TO %s",+ fixture.Container.Database,+ fixture.RoleName,+ ),+ fmt.Sprintf("GRANT %s TO '%s'@'%%'", fixture.RoleName, fixture.Username),+ "FLUSH PRIVILEGES",+ )++ err := fixture.testConnection()++ require.Error(t, err)+ assert.Contains(t, err.Error(), "insufficient permissions")+ assert.Contains(+ t,+ err.Error(),+ fmt.Sprintf("SELECT on `%s`.*", fixture.Container.Database),+ )+}++func testTestConnectionTableGrantsCoverEveryVisibleTable(+ t *testing.T,+ endpoint containers.Endpoint,+ version tools.MariadbVersion,+) {+ fixture := createBackupPrivilegeFixture(t, endpoint, version)+ readableTableName := "readable_" + fixture.Suffix++ fixture.createTables(t, readableTableName)++ fixture.executeStatements(t,+ fmt.Sprintf(+ "GRANT SELECT, SHOW VIEW ON `%s`.`%s` TO '%s'@'%%'",+ fixture.Container.Database,+ readableTableName,+ fixture.Username,+ ),+ "FLUSH PRIVILEGES",+ )++ assert.NoError(t, fixture.testConnection())+}++func testTestConnectionVisibleTableLacksSelect(+ t *testing.T,+ endpoint containers.Endpoint,+ version tools.MariadbVersion,+) {+ fixture := createBackupPrivilegeFixture(t, endpoint, version)+ readableTableName := "readable_" + fixture.Suffix+ blockedTableName := "blocked_" + fixture.Suffix++ fixture.createTables(t, readableTableName, blockedTableName)++ fixture.executeStatements(t,+ fmt.Sprintf(+ "GRANT SELECT, SHOW VIEW ON `%s`.`%s` TO '%s'@'%%'",+ fixture.Container.Database,+ readableTableName,+ fixture.Username,+ ),+ fmt.Sprintf(+ "GRANT INSERT ON `%s`.`%s` TO '%s'@'%%'",+ fixture.Container.Database,+ blockedTableName,+ fixture.Username,+ ),+ "FLUSH PRIVILEGES",+ )++ err := fixture.testConnection()++ require.Error(t, err)+ assert.Contains(t, err.Error(), "insufficient permissions")+ assert.Contains(t, err.Error(), blockedTableName)+ assert.NotContains(t, err.Error(), readableTableName)+}++func testTestConnectionVisibleTableLackingSelectIsExcluded(+ t *testing.T,+ endpoint containers.Endpoint,+ version tools.MariadbVersion,+) {+ fixture := createBackupPrivilegeFixture(t, endpoint, version)+ readableTableName := "readable_" + fixture.Suffix+ blockedTableName := "blocked_" + fixture.Suffix++ fixture.createTables(t, readableTableName, blockedTableName)++ fixture.executeStatements(t,+ fmt.Sprintf(+ "GRANT SELECT, SHOW VIEW ON `%s`.`%s` TO '%s'@'%%'",+ fixture.Container.Database,+ readableTableName,+ fixture.Username,+ ),+ fmt.Sprintf(+ "GRANT INSERT ON `%s`.`%s` TO '%s'@'%%'",+ fixture.Container.Database,+ blockedTableName,+ fixture.Username,+ ),+ "FLUSH PRIVILEGES",+ )++ fixture.Model.ExcludeTables = []string{blockedTableName}++ assert.NoError(t, fixture.testConnection())+}++// Nothing else in this package creates a view, so this is what pins readDumpedTables' TABLE_TYPE+// mapping: misclassify a view as a base table and SHOW VIEW silently stops being required.+func testTestConnectionViewLacksShowView(+ t *testing.T,+ endpoint containers.Endpoint,+ version tools.MariadbVersion,+) {+ fixture := createBackupPrivilegeFixture(t, endpoint, version)+ baseTableName := "viewbase_" + fixture.Suffix+ viewName := "viewonly_" + fixture.Suffix++ fixture.createTables(t, baseTableName)+ fixture.createView(t, viewName, baseTableName)++ fixture.executeStatements(t,+ fmt.Sprintf(+ "GRANT SELECT ON `%s`.* TO '%s'@'%%'",+ fixture.Container.Database,+ fixture.Username,+ ),+ "FLUSH PRIVILEGES",+ )++ err := fixture.testConnection()++ require.Error(t, err)+ assert.Contains(t, err.Error(), "insufficient permissions")+ assert.Contains(t, err.Error(), "SHOW VIEW")++ fixture.executeStatements(t,+ fmt.Sprintf(+ "GRANT SHOW VIEW ON `%s`.* TO '%s'@'%%'",+ fixture.Container.Database,+ fixture.Username,+ ),+ "FLUSH PRIVILEGES",+ )++ assert.NoError(t, fixture.testConnection())+}diff --git a/backend/internal/features/databases/databases/mongodb/model.go b/backend/internal/features/databases/databases/mongodb/model.goindex 20d1090b..36181c7c 100644--- a/backend/internal/features/databases/databases/mongodb/model.go+++ b/backend/internal/features/databases/databases/mongodb/model.go@@ -8,7 +8,6 @@ import ("net/url""regexp""slices"- "strings""time""github.com/google/uuid"@@ -139,13 +138,12 @@ func (m *MongodbDatabase) TestConnection(}m.Version = detectedVersion- if err := checkBackupPermissions(- ctx,- client,- m.Username,- m.Database,- m.AuthDatabase,- ); err != nil {+ if err := checkDumpReadPrivileges(ctx, client, backupPrivilegeScope{+ Username: m.Username,+ Database: m.Database,+ AuthDatabase: m.AuthDatabase,+ ExcludeCollections: m.ExcludeCollections,+ }); err != nil {return err}@@ -665,128 +663,6 @@ func mapMongodbVersion(major, minor string) (tools.MongodbVersion, error) {}}-// checkBackupPermissions verifies the user has sufficient privileges for mongodump backup.-// Required: 'read' role on target database OR 'backup' role on admin OR 'readAnyDatabase' role.-func checkBackupPermissions(- ctx context.Context,- client *mongo.Client,- username, database, authDatabase string,-) error {- authDB := authDatabase- if authDB == "" {- authDB = "admin"- }-- adminDB := client.Database(authDB)- var result bson.M- err := adminDB.RunCommand(ctx, bson.D{- {Key: "usersInfo", Value: bson.D{- {Key: "user", Value: username},- {Key: "db", Value: authDB},- }},- {Key: "showPrivileges", Value: true},- }).Decode(&result)- if err != nil {- return fmt.Errorf("failed to get user info: %w", err)- }-- users, ok := result["users"].(bson.A)- if !ok || len(users) == 0 {- return errors.New("insufficient permissions for backup. User not found")- }-- user, ok := users[0].(bson.M)- if !ok {- return errors.New("insufficient permissions for backup. Could not parse user info")- }-- // Check roles for backup permissions- roles, ok := user["roles"].(bson.A)- if !ok {- return errors.New("insufficient permissions for backup. No roles assigned")- }-- backupRoles := map[string]bool{- "backup": true,- "root": true,- "readAnyDatabase": true,- "dbOwner": true,- "__system": true,- "clusterAdmin": true,- "readWriteAnyDatabase": true,- }-- var userRoles []string- hasBackupRole := false- hasReadOnTargetDB := false-- for _, roleDoc := range roles {- role, ok := roleDoc.(bson.M)- if !ok {- continue- }- roleName, _ := role["role"].(string)- roleDB, _ := role["db"].(string)-- if roleName != "" {- userRoles = append(userRoles, roleName)- }-- if backupRoles[roleName] {- hasBackupRole = true- }-- if roleName == "read" && (roleDB == database || roleDB == "") {- hasReadOnTargetDB = true- }- if roleName == "readWrite" && (roleDB == database || roleDB == "") {- hasReadOnTargetDB = true- }- }-- if hasBackupRole || hasReadOnTargetDB {- return nil- }-- // Check inherited privileges for 'find' action on target database- inheritedPrivileges, ok := user["inheritedPrivileges"].(bson.A)- if ok {- for _, privDoc := range inheritedPrivileges {- priv, ok := privDoc.(bson.M)- if !ok {- continue- }- resource, ok := priv["resource"].(bson.M)- if !ok {- continue- }-- resourceDB, _ := resource["db"].(string)- resourceCluster, _ := resource["cluster"].(bool)-- isTargetDB := resourceDB == database || resourceDB == "" || resourceCluster-- actions, ok := priv["actions"].(bson.A)- if !ok {- continue- }-- for _, action := range actions {- actionStr, ok := action.(string)- if ok && actionStr == "find" && isTargetDB {- return nil- }- }- }- }-- return fmt.Errorf(- "insufficient permissions for backup. Current roles: %s. Required: 'read' role on database '%s' OR 'backup' role on admin OR 'readAnyDatabase' role",- strings.Join(userRoles, ", "),- database,- )-}-func decryptPasswordIfNeeded(password string,encryptor encryption.FieldEncryptor,diff --git a/backend/internal/features/databases/databases/mongodb/model_test.go b/backend/internal/features/databases/databases/mongodb/model_test.goindex fcecd6db..906725a9 100644--- a/backend/internal/features/databases/databases/mongodb/model_test.go+++ b/backend/internal/features/databases/databases/mongodb/model_test.go@@ -56,6 +56,20 @@ func Test_MongodbModel_AcrossSupportedVersions(t *testing.T) {t.Run("Test_CreateReadOnlyUser_UserCanReadButNotWrite", func(t *testing.T) {testCreateReadOnlyUserCanReadButNotWrite(t, endpoint, dbVersion.version)})++ t.Run(+ "Test_TestConnection_WhenFindIsGrantedOnSingleCollection_ReturnsErrorUntilCollectionIsExcluded",+ func(t *testing.T) {+ testTestConnectionFindGrantedOnSingleCollection(t, endpoint, dbVersion.version)+ },+ )++ t.Run(+ "Test_TestConnection_WhenClusterRoleNameLivesOnAnotherDatabase_ReturnsError",+ func(t *testing.T) {+ testTestConnectionClusterRoleNameOnAnotherDatabase(t, endpoint, dbVersion.version)+ },+ )})}}diff --git a/backend/internal/features/databases/databases/mongodb/privileges.go b/backend/internal/features/databases/databases/mongodb/privileges.gonew file mode 100644index 00000000..f362ca65--- /dev/null+++ b/backend/internal/features/databases/databases/mongodb/privileges.go@@ -0,0 +1,316 @@+package mongodb++import (+ "context"+ "errors"+ "fmt"+ "slices"+ "strings"++ "go.mongodb.org/mongo-driver/bson"+ "go.mongodb.org/mongo-driver/mongo"++ "databasus-backend/internal/util/namelist"+)++const findAction = "find"++// MongoDB's Unauthorized error code, as returned by listCollections for a role without it.+const unauthorizedErrorCode = 13++// These are defined on admin and read every database, so they cover the dump whatever it targets.+var clusterWideBackupRoles = map[string]bool{+ "backup": true,+ "root": true,+ "readAnyDatabase": true,+ "readWriteAnyDatabase": true,+ "clusterAdmin": true,+ "__system": true,+}++// These read one database, so they only count when they are scoped to the one being dumped.+var databaseScopedBackupRoles = map[string]bool{+ "read": true,+ "readWrite": true,+ "dbOwner": true,+}++type backupPrivilegeScope struct {+ Username string+ Database string+ AuthDatabase string+ ExcludeCollections []string+}++func (s backupPrivilegeScope) getAuthDatabaseName() string {+ if s.AuthDatabase == "" {+ return "admin"+ }++ return s.AuthDatabase+}++func checkDumpReadPrivileges(+ ctx context.Context,+ client *mongo.Client,+ scope backupPrivilegeScope,+) error {+ authDatabaseName := scope.getAuthDatabaseName()++ var userInfo bson.M+ err := client.Database(authDatabaseName).RunCommand(ctx, bson.D{+ {Key: "usersInfo", Value: bson.D{+ {Key: "user", Value: scope.Username},+ {Key: "db", Value: authDatabaseName},+ }},+ {Key: "showPrivileges", Value: true},+ }).Decode(&userInfo)+ if err != nil {+ return fmt.Errorf("failed to get user info: %w", err)+ }++ users, isUserList := userInfo["users"].(bson.A)+ if !isUserList || len(users) == 0 {+ return errors.New("insufficient permissions for backup. User not found")+ }++ user, isUserDocument := users[0].(bson.M)+ if !isUserDocument {+ return errors.New("insufficient permissions for backup. Could not parse user info")+ }++ account := grantedAccount{Scope: scope, UsersInfo: user}++ if account.hasBackupRole() {+ return nil+ }++ if account.isFindGrantedOnWholeDatabase() {+ return nil+ }++ return account.checkPerCollectionFindPrivileges(ctx, client)+}++type grantedAccount struct {+ Scope backupPrivilegeScope+ UsersInfo bson.M+}++func (a grantedAccount) getGrantedRoleNames() []string {+ var grantedRoleNames []string++ for _, role := range a.getRoles() {+ if roleName, _ := role["role"].(string); roleName != "" {+ grantedRoleNames = append(grantedRoleNames, roleName)+ }+ }++ return grantedRoleNames+}++func (a grantedAccount) hasBackupRole() bool {+ for _, role := range a.getRoles() {+ roleName, _ := role["role"].(string)+ roleDatabaseName, _ := role["db"].(string)++ // The all-database built-ins exist only on admin; a custom role that merely shares+ // their name on another database grants none of their reach.+ if clusterWideBackupRoles[roleName] && roleDatabaseName == "admin" {+ return true+ }++ if databaseScopedBackupRoles[roleName] &&+ (roleDatabaseName == a.Scope.Database || roleDatabaseName == "") {+ return true+ }+ }++ return false+}++func (a grantedAccount) getRoles() []bson.M {+ roles, isRoleList := a.UsersInfo["roles"].(bson.A)+ if !isRoleList {+ return nil+ }++ roleDocuments := make([]bson.M, 0, len(roles))++ for _, roleEntry := range roles {+ if role, isRoleDocument := roleEntry.(bson.M); isRoleDocument {+ roleDocuments = append(roleDocuments, role)+ }+ }++ return roleDocuments+}++// A find privilege counts for the whole dump only when its resource is the database itself, the+// cluster, or any resource. A privilege naming one collection covers just that collection, which+// is what the per-collection pass below has to work out.+func (a grantedAccount) isFindGrantedOnWholeDatabase() bool {+ for _, privilege := range a.getInheritedPrivileges() {+ resource, isResourceDocument := privilege["resource"].(bson.M)+ if !isResourceDocument || !hasFindAction(privilege) {+ continue+ }++ if isCluster, _ := resource["cluster"].(bool); isCluster {+ return true+ }++ resourceCollectionName, _ := resource["collection"].(string)+ if resourceCollectionName != "" {+ continue+ }++ resourceDatabaseName, _ := resource["db"].(string)+ if resourceDatabaseName == a.Scope.Database || resourceDatabaseName == "" {+ return true+ }+ }++ return false+}++func (a grantedAccount) checkPerCollectionFindPrivileges(+ ctx context.Context,+ client *mongo.Client,+) error {+ dumpedCollectionNames, err := readDumpedCollectionNames(ctx, client, a.Scope)+ if err != nil {+ // A role that cannot list the collections certainly cannot dump them, but a transport+ // failure must keep saying what it was rather than pose as a permission problem.+ if !isUnauthorizedError(err) {+ return err+ }++ return a.newInsufficientPrivilegesError(nil)+ }++ readableCollectionNames := a.getCollectionsGrantingFind()++ var unreadableCollectionNames []string++ for _, collectionName := range dumpedCollectionNames {+ if !slices.Contains(readableCollectionNames, collectionName) {+ unreadableCollectionNames = append(unreadableCollectionNames, collectionName)+ }+ }++ if len(unreadableCollectionNames) == 0 {+ return nil+ }++ return a.newInsufficientPrivilegesError(unreadableCollectionNames)+}++func (a grantedAccount) getCollectionsGrantingFind() []string {+ var readableCollectionNames []string++ for _, privilege := range a.getInheritedPrivileges() {+ resource, isResourceDocument := privilege["resource"].(bson.M)+ if !isResourceDocument || !hasFindAction(privilege) {+ continue+ }++ resourceCollectionName, _ := resource["collection"].(string)+ if resourceCollectionName == "" {+ continue+ }++ resourceDatabaseName, _ := resource["db"].(string)+ if resourceDatabaseName == a.Scope.Database || resourceDatabaseName == "" {+ readableCollectionNames = append(readableCollectionNames, resourceCollectionName)+ }+ }++ return readableCollectionNames+}++func (a grantedAccount) getInheritedPrivileges() []bson.M {+ inheritedPrivileges, isPrivilegeList := a.UsersInfo["inheritedPrivileges"].(bson.A)+ if !isPrivilegeList {+ return nil+ }++ privileges := make([]bson.M, 0, len(inheritedPrivileges))++ for _, privilegeEntry := range inheritedPrivileges {+ if privilege, isPrivilegeDocument := privilegeEntry.(bson.M); isPrivilegeDocument {+ privileges = append(privileges, privilege)+ }+ }++ return privileges+}++func hasFindAction(privilege bson.M) bool {+ actions, isActionList := privilege["actions"].(bson.A)+ if !isActionList {+ return false+ }++ for _, actionEntry := range actions {+ if action, isActionName := actionEntry.(string); isActionName && action == findAction {+ return true+ }+ }++ return false+}++func isUnauthorizedError(err error) bool {+ var commandError mongo.CommandError+ if errors.As(err, &commandError) {+ return commandError.Code == unauthorizedErrorCode+ }++ return false+}++func readDumpedCollectionNames(+ ctx context.Context,+ client *mongo.Client,+ scope backupPrivilegeScope,+) ([]string, error) {+ collectionNames, err := client.Database(scope.Database).+ ListCollectionNames(ctx, bson.D{})+ if err != nil {+ return nil, fmt.Errorf("failed to list collections: %w", err)+ }++ excludedCollectionNames := namelist.NormalizeUniqueNames(scope.ExcludeCollections)+ dumpedCollectionNames := make([]string, 0, len(collectionNames))++ for _, collectionName := range collectionNames {+ if slices.Contains(excludedCollectionNames, collectionName) {+ continue+ }++ dumpedCollectionNames = append(dumpedCollectionNames, collectionName)+ }++ return dumpedCollectionNames, nil+}++func (a grantedAccount) newInsufficientPrivilegesError(+ unreadableCollectionNames []string,+) error {+ if len(unreadableCollectionNames) > 0 {+ return fmt.Errorf(+ "insufficient permissions for backup. Cannot read collections: %s. "++ "Required: 'read' role on database '%s' OR 'backup' role on admin OR 'readAnyDatabase' role",+ namelist.FormatTruncatedNames(unreadableCollectionNames),+ a.Scope.Database,+ )+ }++ return fmt.Errorf(+ "insufficient permissions for backup. Current roles: %s. "++ "Required: 'read' role on database '%s' OR 'backup' role on admin OR 'readAnyDatabase' role",+ strings.Join(a.getGrantedRoleNames(), ", "),+ a.Scope.Database,+ )+}diff --git a/backend/internal/features/databases/databases/mongodb/privileges_test.go b/backend/internal/features/databases/databases/mongodb/privileges_test.gonew file mode 100644index 00000000..77de2c33--- /dev/null+++ b/backend/internal/features/databases/databases/mongodb/privileges_test.go@@ -0,0 +1,188 @@+package mongodb++import (+ "context"+ "fmt"+ "log/slog"+ "os"+ "testing"++ "github.com/google/uuid"+ "github.com/stretchr/testify/assert"+ "github.com/stretchr/testify/require"+ "go.mongodb.org/mongo-driver/bson"++ "databasus-backend/internal/util/testing/containers"+ "databasus-backend/internal/util/tools"+)++// The all-database built-ins live on admin, so a custom role that merely shares their name on+// another database must not short-circuit the privilege scan.+func testTestConnectionClusterRoleNameOnAnotherDatabase(+ t *testing.T,+ endpoint containers.Endpoint,+ version tools.MongodbVersion,+) {+ container := connectToMongodbEndpoint(t, endpoint, version)+ t.Cleanup(func() { _ = container.Client.Disconnect(context.Background()) })++ ctx := t.Context()+ suffix := uuid.New().String()[:8]+ databaseName := "rolename_scope_" + suffix+ username := "rolename_reader_" + suffix+ password := "rolenamereaderpassword123"++ database := container.Client.Database(databaseName)++ _, err := database.Collection("orders_"+suffix).InsertOne(ctx, bson.M{"data": "row1"})+ require.NoError(t, err)++ t.Cleanup(func() { _ = database.Drop(context.Background()) })++ require.NoError(t, database.RunCommand(ctx, bson.D{+ {Key: "createRole", Value: "backup"},+ {Key: "privileges", Value: bson.A{}},+ {Key: "roles", Value: bson.A{}},+ }).Err())++ t.Cleanup(func() {+ _ = database.RunCommand(+ context.Background(),+ bson.D{{Key: "dropRole", Value: "backup"}},+ ).Err()+ })++ adminDatabase := container.Client.Database(container.AuthDatabase)++ require.NoError(t, adminDatabase.RunCommand(ctx, bson.D{+ {Key: "createUser", Value: username},+ {Key: "pwd", Value: password},+ {Key: "roles", Value: bson.A{+ bson.D{{Key: "role", Value: "backup"}, {Key: "db", Value: databaseName}},+ }},+ }).Err())++ t.Cleanup(func() { dropUserSafe(container.Client, username, container.AuthDatabase) })++ port := container.Port+ mongodbModel := &MongodbDatabase{+ Version: version,+ Host: container.Host,+ Port: &port,+ Username: username,+ Password: password,+ Database: databaseName,+ AuthDatabase: container.AuthDatabase,+ CpuCount: 1,+ }++ err = mongodbModel.TestConnection(slog.New(slog.NewTextHandler(os.Stdout, nil)), nil)++ require.Error(t, err)+ assert.Contains(t, err.Error(), "insufficient permissions")+}++// A privilege naming one collection covers only that collection, so it cannot stand in for+// permission to read the whole database.+func testTestConnectionFindGrantedOnSingleCollection(+ t *testing.T,+ endpoint containers.Endpoint,+ version tools.MongodbVersion,+) {+ // t.Context() is already canceled when cleanups run, so they carry their own context, and+ // the disconnect registers first so it runs after every drop below.+ container := connectToMongodbEndpoint(t, endpoint, version)+ t.Cleanup(func() { _ = container.Client.Disconnect(context.Background()) })++ ctx := t.Context()+ suffix := uuid.New().String()[:8]+ readableCollectionName := "readable_" + suffix+ blockedCollectionName := "blocked_" + suffix+ // The version's subtests share one server (ADR-0013), so an own database keeps the dump set+ // free of the collections the other subtests leave behind.+ databaseName := "collection_scope_" + suffix+ roleName := "single_collection_reader_" + suffix+ username := "collection_reader_" + suffix+ password := "collectionreaderpassword123"++ database := container.Client.Database(databaseName)++ for _, collectionName := range []string{readableCollectionName, blockedCollectionName} {+ _, err := database.Collection(collectionName).InsertOne(ctx, bson.M{"data": "row1"})+ require.NoError(t, err)++ t.Cleanup(func() { _ = database.Collection(collectionName).Drop(context.Background()) })+ }++ adminDatabase := container.Client.Database(container.AuthDatabase)++ require.NoError(t, adminDatabase.RunCommand(ctx, bson.D{+ {Key: "createRole", Value: roleName},+ {Key: "privileges", Value: bson.A{+ bson.D{+ {Key: "resource", Value: bson.D{+ {Key: "db", Value: databaseName},+ {Key: "collection", Value: readableCollectionName},+ }},+ {Key: "actions", Value: bson.A{"find"}},+ },+ // Listing collections is what lets the check see the whole dump set, so the role has+ // it: without it the role could not even reach the interesting comparison.+ bson.D{+ {Key: "resource", Value: bson.D{+ {Key: "db", Value: databaseName},+ {Key: "collection", Value: ""},+ }},+ {Key: "actions", Value: bson.A{"listCollections"}},+ },+ }},+ {Key: "roles", Value: bson.A{}},+ }).Err())++ t.Cleanup(func() {+ _ = adminDatabase.RunCommand(+ context.Background(),+ bson.D{{Key: "dropRole", Value: roleName}},+ ).Err()+ })++ require.NoError(t, adminDatabase.RunCommand(ctx, bson.D{+ {Key: "createUser", Value: username},+ {Key: "pwd", Value: password},+ {Key: "roles", Value: bson.A{+ bson.D{{Key: "role", Value: roleName}, {Key: "db", Value: container.AuthDatabase}},+ }},+ }).Err())++ t.Cleanup(func() { dropUserSafe(container.Client, username, container.AuthDatabase) })+ t.Cleanup(func() { _ = database.Drop(context.Background()) })++ port := container.Port+ mongodbModel := &MongodbDatabase{+ Version: version,+ Host: container.Host,+ Port: &port,+ Username: username,+ Password: password,+ Database: databaseName,+ AuthDatabase: container.AuthDatabase,+ CpuCount: 1,+ }++ logger := slog.New(slog.NewTextHandler(os.Stdout, nil))++ err := mongodbModel.TestConnection(logger, nil)++ require.Error(t, err)+ assert.Contains(t, err.Error(), "insufficient permissions")+ assert.Contains(t, err.Error(), blockedCollectionName)+ assert.NotContains(t, err.Error(), readableCollectionName)++ mongodbModel.ExcludeCollections = []string{blockedCollectionName}++ assert.NoError(+ t,+ mongodbModel.TestConnection(logger, nil),+ fmt.Sprintf("excluding %s should leave only readable collections", blockedCollectionName),+ )+}diff --git a/backend/internal/features/databases/databases/mysql/model.go b/backend/internal/features/databases/databases/mysql/model.goindex 72f49747..157d6bb4 100644--- a/backend/internal/features/databases/databases/mysql/model.go+++ b/backend/internal/features/databases/databases/mysql/model.go@@ -9,14 +9,13 @@ import ("log/slog""regexp""slices"- "sort"- "strings""time""github.com/go-sql-driver/mysql""github.com/google/uuid""gorm.io/gorm"+ "databasus-backend/internal/features/databases/databases/mysqlfamily""databasus-backend/internal/features/sshtunnel""databasus-backend/internal/util/encryption""databasus-backend/internal/util/namelist"@@ -124,14 +123,20 @@ func (m *MysqlDatabase) TestConnection(}m.Version = detectedVersion- privileges, err := detectPrivileges(ctx, db, *m.Database)+ backupPrivileges, err := readBackupPrivileges(ctx, backupPrivilegesQuery{+ DB: db,+ Logger: logger,+ Version: m.Version,+ SchemaName: *m.Database,+ ExcludeTables: m.ExcludeTables,+ })if err != nil {return err}- m.Privileges = privileges+ m.Privileges = backupPrivileges.GetEffectivePrivilegesCsv()- if err := checkBackupPermissions(m.Privileges); err != nil {- return err+ if !backupPrivileges.IsSufficientForDump() {+ return backupPrivileges.NewInsufficiencyError()}return nil@@ -270,11 +275,17 @@ func (m *MysqlDatabase) PopulateDbData(}m.Version = detectedVersion- privileges, err := detectPrivileges(ctx, db, *m.Database)+ backupPrivileges, err := readBackupPrivileges(ctx, backupPrivilegesQuery{+ DB: db,+ Logger: logger,+ Version: m.Version,+ SchemaName: *m.Database,+ ExcludeTables: m.ExcludeTables,+ })if err != nil {return err}- m.Privileges = privileges+ m.Privileges = backupPrivileges.GetEffectivePrivilegesCsv()return nil}@@ -374,9 +385,14 @@ func (m *MysqlDatabase) ShouldSuggestReadOnlyUser(return false, nil, fmt.Errorf("failed to scan grant: %w", err)}- for _, priv := range parseGrantPrivileges(grant) {- if writePrivileges[priv] {- detectedPrivileges[priv] = true+ parsedGrant := mysqlfamily.ParseGrantLine(grant)+ if parsedGrant == nil {+ continue+ }++ for _, privilege := range parsedGrant.Privileges {+ if writePrivileges[privilege] {+ detectedPrivileges[privilege] = true}}}@@ -457,14 +473,6 @@ func (m *MysqlDatabase) CreateReadOnlyUser(return "", "", fmt.Errorf("failed to grant database privileges: %w", err)}- _, err = tx.ExecContext(ctx, fmt.Sprintf(- "GRANT PROCESS ON *.* TO '%s'@'%%'",- newUsername,- ))- if err != nil {- return "", "", fmt.Errorf("failed to grant PROCESS privilege: %w", err)- }-_, err = tx.ExecContext(ctx, "FLUSH PRIVILEGES")if err != nil {return "", "", fmt.Errorf("failed to flush privileges: %w", err)@@ -487,19 +495,6 @@ func (m *MysqlDatabase) CreateReadOnlyUser(return "", "", errors.New("failed to generate unique username after 3 attempts")}-func (m *MysqlDatabase) HasPrivilege(priv string) bool {- return HasPrivilege(m.Privileges, priv)-}--func HasPrivilege(privileges, priv string) bool {- for p := range strings.SplitSeq(privileges, ",") {- if strings.TrimSpace(p) == priv {- return true- }- }- return false-}-func (m *MysqlDatabase) buildDSN(password, database string) string {tlsConfig := "false"allowCleartext := ""@@ -575,139 +570,6 @@ func mapMysql8xVersion(minor string) tools.MysqlVersion {}}-// parseGrantPrivileges extracts comma-separated privilege names from a single-// SHOW GRANTS line. Returns uppercased privilege tokens with column-level-// qualifiers like "(col1, col2)" stripped. Returns nil for role grants and-// other lines without an ON clause.-//-// Parsing the privilege list is necessary because a naive substring search-// over the whole grant string falsely matches privilege keywords that appear-// inside other privilege names — e.g. "CREATE VIEW" contains "CREATE" as a-// substring.-func parseGrantPrivileges(grant string) []string {- headRe := regexp.MustCompile(`(?is)^\s*GRANT\s+(.+?)\s+ON\s+`)- m := headRe.FindStringSubmatch(grant)- if m == nil {- return nil- }-- colRe := regexp.MustCompile(`\s*\([^)]*\)`)- privsStr := colRe.ReplaceAllString(m[1], "")-- parts := strings.Split(privsStr, ",")- privs := make([]string, 0, len(parts))- for _, p := range parts {- p = strings.TrimSpace(strings.ToUpper(p))- if p != "" {- privs = append(privs, p)- }- }-- return privs-}--// detectPrivileges detects backup-related privileges and returns them as comma-separated string-func detectPrivileges(ctx context.Context, db *sql.DB, database string) (string, error) {- rows, err := db.QueryContext(ctx, "SHOW GRANTS FOR CURRENT_USER()")- if err != nil {- return "", fmt.Errorf("failed to check grants: %w", err)- }- defer func() { _ = rows.Close() }()-- backupPrivileges := []string{- "SELECT", "SHOW VIEW", "LOCK TABLES", "TRIGGER", "EVENT",- }-- detectedPrivileges := make(map[string]bool)- hasProcess := false- hasAllPrivileges := false-- // Escape underscores to match MySQL's grant output format- // MySQL escapes _ as \_ in SHOW GRANTS output- // Pattern matches either literal _ or escaped \_- escapedDbName := strings.ReplaceAll(regexp.QuoteMeta(database), "_", `(_|\\_)`)- dbPatternStr := fmt.Sprintf(- `(?i)ON\s+[\x60'"]?%s[\x60'"]?\s*\.\s*\*`,- escapedDbName,- )- dbPattern := regexp.MustCompile(dbPatternStr)- globalPattern := regexp.MustCompile(`(?i)ON\s+\*\s*\.\s*\*`)- allPrivilegesPattern := regexp.MustCompile(`(?i)\bALL\s+PRIVILEGES\b`)-- for rows.Next() {- var grant string- if err := rows.Scan(&grant); err != nil {- return "", fmt.Errorf("failed to scan grant: %w", err)- }-- isRelevantGrant := globalPattern.MatchString(grant) || dbPattern.MatchString(grant)-- if allPrivilegesPattern.MatchString(grant) && isRelevantGrant {- hasAllPrivileges = true- }-- if isRelevantGrant {- for _, priv := range backupPrivileges {- privPattern := regexp.MustCompile(`(?i)\b` + regexp.QuoteMeta(priv) + `\b`)- if privPattern.MatchString(grant) {- detectedPrivileges[priv] = true- }- }- }-- if globalPattern.MatchString(grant) {- processPattern := regexp.MustCompile(`(?i)\bPROCESS\b`)- if processPattern.MatchString(grant) {- hasProcess = true- }- }- }-- if err := rows.Err(); err != nil {- return "", fmt.Errorf("error iterating grants: %w", err)- }-- if hasAllPrivileges {- for _, priv := range backupPrivileges {- detectedPrivileges[priv] = true- }- hasProcess = true- }-- privileges := make([]string, 0, len(detectedPrivileges)+1)- for priv := range detectedPrivileges {- privileges = append(privileges, priv)- }- if hasProcess {- privileges = append(privileges, "PROCESS")- }-- sort.Strings(privileges)- return strings.Join(privileges, ","), nil-}--// checkBackupPermissions verifies the user has sufficient privileges for mysqldump backup.-// Required: SELECT, SHOW VIEW-func checkBackupPermissions(privileges string) error {- requiredPrivileges := []string{"SELECT", "SHOW VIEW"}-- var missingPrivileges []string- for _, priv := range requiredPrivileges {- if !HasPrivilege(privileges, priv) {- missingPrivileges = append(missingPrivileges, priv)- }- }-- if len(missingPrivileges) > 0 {- return fmt.Errorf(- "insufficient permissions for backup. Missing: %s. Required: SELECT, SHOW VIEW",- strings.Join(missingPrivileges, ", "),- )- }-- return nil-}-func decryptPasswordIfNeeded(password string,encryptor encryption.FieldEncryptor,diff --git a/backend/internal/features/databases/databases/mysql/model_test.go b/backend/internal/features/databases/databases/mysql/model_test.goindex 7e792357..d6cac6d9 100644--- a/backend/internal/features/databases/databases/mysql/model_test.go+++ b/backend/internal/features/databases/databases/mysql/model_test.go@@ -17,16 +17,17 @@ import ()type mysqlModelVersion struct {- name string- version tools.MysqlVersion- image string+ name string+ version tools.MysqlVersion+ image string+ hasRoles bool}var mysqlModelVersions = []mysqlModelVersion{- {"MySQL 5.7", tools.MysqlVersion57, "mysql:5.7"},- {"MySQL 8.0", tools.MysqlVersion80, "mysql:8.0"},- {"MySQL 8.4", tools.MysqlVersion84, "mysql:8.4"},- {"MySQL 9", tools.MysqlVersion9, "mysql:9"},+ {"MySQL 5.7", tools.MysqlVersion57, "mysql:5.7", false},+ {"MySQL 8.0", tools.MysqlVersion80, "mysql:8.0", true},+ {"MySQL 8.4", tools.MysqlVersion84, "mysql:8.4", true},+ {"MySQL 9", tools.MysqlVersion9, "mysql:9", true},}// Test_MysqlModel_AcrossSupportedVersions boots each MySQL version once and runs every matrix model@@ -59,6 +60,32 @@ func Test_MysqlModel_AcrossSupportedVersions(t *testing.T) {t.Run("Test_TestConnection_DatabaseWithUnderscoresAndAllPrivileges_Success", func(t *testing.T) {testTestConnectionDatabaseWithUnderscoresAndAllPrivileges(t, endpoint, dbVersion.version)})++ t.Run("Test_TestConnection_WhenTableGrantsCoverEveryVisibleTable_Success", func(t *testing.T) {+ testTestConnectionTableGrantsCoverEveryVisibleTable(t, endpoint, dbVersion.version)+ })++ t.Run("Test_TestConnection_WhenVisibleTableLacksSelect_ReturnsError", func(t *testing.T) {+ testTestConnectionVisibleTableLacksSelect(t, endpoint, dbVersion.version)+ })++ t.Run("Test_TestConnection_WhenVisibleTableLackingSelectIsExcluded_Success", func(t *testing.T) {+ testTestConnectionVisibleTableLackingSelectIsExcluded(t, endpoint, dbVersion.version)+ })++ t.Run("Test_TestConnection_WhenViewLacksShowView_ReturnsErrorUntilShowViewIsGranted", func(t *testing.T) {+ testTestConnectionViewLacksShowView(t, endpoint, dbVersion.version)+ })++ if dbVersion.hasRoles {+ t.Run("Test_TestConnection_WhenBackupPrivilegesComeFromActiveRole_Success", func(t *testing.T) {+ testTestConnectionBackupPrivilegesFromActiveRole(t, endpoint, dbVersion.version)+ })++ t.Run("Test_TestConnection_WhenRoleIsGrantedButNotActivated_ReturnsError", func(t *testing.T) {+ testTestConnectionRoleGrantedButNotActivated(t, endpoint, dbVersion.version)+ })+ }})}}@@ -93,8 +120,10 @@ func testTestConnectionInsufficientPermissions())assert.NoError(t, err)+ // INSERT makes the schema and its tables visible without making them readable, which is what+ // the dump tool aborts on._, err = container.DB.Exec(fmt.Sprintf(- "GRANT SELECT ON `%s`.* TO '%s'@'%%'",+ "GRANT INSERT ON `%s`.* TO '%s'@'%%'",container.Database,limitedUsername,))@@ -122,8 +151,10 @@ func testTestConnectionInsufficientPermissions(logger := slog.New(slog.NewTextHandler(os.Stdout, nil))err = mysqlModel.TestConnection(logger, nil)- assert.Error(t, err)- assert.Contains(t, err.Error(), "insufficient permissions")+ if assert.Error(t, err) {+ assert.Contains(t, err.Error(), "insufficient permissions")+ assert.Contains(t, err.Error(), "SELECT")+ }}func testTestConnectionSufficientPermissions(@@ -163,12 +194,6 @@ func testTestConnectionSufficientPermissions())assert.NoError(t, err)- _, err = container.DB.Exec(fmt.Sprintf(- "GRANT PROCESS ON *.* TO '%s'@'%%'",- backupUsername,- ))- assert.NoError(t, err)-_, err = container.DB.Exec("FLUSH PRIVILEGES")assert.NoError(t, err)@@ -779,72 +804,6 @@ func Test_GetRawDbSizeMb_Mysql_ReturnsPositiveSize(t *testing.T) {assert.Greater(t, sizeMB, 0.0, "raw db size should be > 0 after inserting data")}-func Test_ParseGrantPrivileges_ReturnsExpectedTokens(t *testing.T) {- cases := []struct {- name string- grant string- want []string- }{- {- "issue-568 SHOW CREATE ROUTINE not split into CREATE",- "GRANT SELECT, SHOW VIEW, SHOW CREATE ROUTINE ON *.* TO 'backup'@'%'",- []string{"SELECT", "SHOW VIEW", "SHOW CREATE ROUTINE"},- },- {- "standard write privs",- "GRANT SELECT, INSERT, UPDATE ON *.* TO 'x'@'%'",- []string{"SELECT", "INSERT", "UPDATE"},- },- {- "ALL PRIVILEGES",- "GRANT ALL PRIVILEGES ON db.* TO 'x'@'%'",- []string{"ALL PRIVILEGES"},- },- {- "USAGE-only line",- "GRANT USAGE ON *.* TO 'x'@'%'",- []string{"USAGE"},- },- {- "column-level qualifiers stripped",- "GRANT SELECT (col1, col2), UPDATE (col3) ON db.t TO 'x'@'%'",- []string{"SELECT", "UPDATE"},- },- {- "role grant (no ON clause) returns nil",- "GRANT my_role TO 'u'@'%'",- nil,- },- {- "PROXY grant",- "GRANT PROXY ON 'other'@'%' TO 'u'@'%'",- []string{"PROXY"},- },- {- "WITH GRANT OPTION trailer ignored",- "GRANT SELECT, INSERT ON *.* TO 'x'@'%' WITH GRANT OPTION",- []string{"SELECT", "INSERT"},- },- {- "mixed case GRANT/ON",- "grant Select, Update on *.* to 'x'@'%'",- []string{"SELECT", "UPDATE"},- },- {- "column literally named ON inside parens",- "GRANT SELECT (on) ON db.t TO 'x'@'%'",- []string{"SELECT"},- },- }-- for _, tc := range cases {- t.Run(tc.name, func(t *testing.T) {- got := parseGrantPrivileges(tc.grant)- assert.Equal(t, tc.want, got)- })- }-}-func Test_HideSensitiveData_WhenCalled_ClearsPasswordAndPreservesOtherFields(t *testing.T) {databaseName := "appdb"mysqlModel := &MysqlDatabase{diff --git a/backend/internal/features/databases/databases/mysql/privileges.go b/backend/internal/features/databases/databases/mysql/privileges.gonew file mode 100644index 00000000..7915564a--- /dev/null+++ b/backend/internal/features/databases/databases/mysql/privileges.go@@ -0,0 +1,99 @@+package mysql++import (+ "context"+ "database/sql"+ "log/slog"++ "databasus-backend/internal/features/databases/databases/mysqlfamily"+ "databasus-backend/internal/util/tools"+)++const noActiveRolesMarker = "NONE"++type backupPrivilegesQuery struct {+ DB *sql.DB+ Logger *slog.Logger+ Version tools.MysqlVersion+ SchemaName string+ ExcludeTables []string+}++func readBackupPrivileges(+ ctx context.Context,+ query backupPrivilegesQuery,+) (mysqlfamily.BackupPrivileges, error) {+ grantLines, err := readGrantLines(ctx, query)+ if err != nil {+ return mysqlfamily.BackupPrivileges{}, err+ }++ return mysqlfamily.ReadBackupPrivileges(ctx, mysqlfamily.BackupPrivilegesQuery{+ DB: query.DB,+ SchemaName: query.SchemaName,+ ExcludeTables: query.ExcludeTables,+ GrantLines: grantLines,+ })+}++// SHOW GRANTS reports role membership without expanding it, so a role's privileges have to be+// asked for separately or a role-based backup account looks like it holds nothing.+func readGrantLines(ctx context.Context, query backupPrivilegesQuery) ([]string, error) {+ grantLines, err := mysqlfamily.QueryGrantLines(+ ctx,+ query.DB,+ "SHOW GRANTS FOR CURRENT_USER()",+ )+ if err != nil {+ return nil, err+ }++ if query.Version == tools.MysqlVersion57 {+ return grantLines, nil+ }++ // Only the roles activated for this session count: mysqldump logs in the same way and, with+ // activate_all_roles_on_login off, a granted but inactive role gives it nothing.+ activeRoles, err := readActiveRoles(ctx, query.DB)+ if err != nil {+ query.Logger.WarnContext(ctx, "failed to read active roles", "error", err)++ return grantLines, nil+ }++ if activeRoles == "" {+ return grantLines, nil+ }++ // The server returns the roles already quoted, so re-quoting them here would corrupt a role+ // name that contains a backquote. The value never comes from user input.+ roleGrantLines, err := mysqlfamily.QueryGrantLines(+ ctx,+ query.DB,+ "SHOW GRANTS FOR CURRENT_USER() USING "+activeRoles,+ )+ if err != nil {+ query.Logger.WarnContext(ctx, "failed to expand active roles", "error", err)++ return grantLines, nil+ }++ return append(grantLines, roleGrantLines...), nil+}++func readActiveRoles(ctx context.Context, db *sql.DB) (string, error) {+ var activeRoles sql.NullString+ if err := db.QueryRowContext(ctx, "SELECT CURRENT_ROLE()").Scan(&activeRoles); err != nil {+ return "", err+ }++ if !activeRoles.Valid || activeRoles.String == noActiveRolesMarker {+ return "", nil+ }++ return activeRoles.String, nil+}++func (m *MysqlDatabase) HasPrivilege(privilege string) bool {+ return mysqlfamily.HasPrivilege(m.Privileges, privilege)+}diff --git a/backend/internal/features/databases/databases/mysql/privileges_test.go b/backend/internal/features/databases/databases/mysql/privileges_test.gonew file mode 100644index 00000000..0c25080c--- /dev/null+++ b/backend/internal/features/databases/databases/mysql/privileges_test.go@@ -0,0 +1,314 @@+package mysql++import (+ "fmt"+ "log/slog"+ "os"+ "testing"++ "github.com/google/uuid"+ "github.com/stretchr/testify/assert"+ "github.com/stretchr/testify/require"++ "databasus-backend/internal/util/testing/containers"+ "databasus-backend/internal/util/tools"+)++// The version's subtests share one server (ADR-0013), so every name carries a suffix and the+// tables a case creates are dropped again before the next one runs.+type backupPrivilegeFixture struct {+ Container *MysqlContainer+ Suffix string+ Username string+ RoleName string+ Model *MysqlDatabase+}++func createBackupPrivilegeFixture(+ t *testing.T,+ endpoint containers.Endpoint,+ version tools.MysqlVersion,+) backupPrivilegeFixture {+ t.Helper()++ container := connectToMysqlEndpoint(t, endpoint, version)+ t.Cleanup(func() { container.DB.Close() })++ suffix := uuid.New().String()[:8]+ username := fmt.Sprintf("backup_%s", suffix)+ password := "backuprolepassword123"++ _, err := container.DB.Exec(fmt.Sprintf(+ "CREATE USER '%s'@'%%' IDENTIFIED BY '%s'",+ username,+ password,+ ))+ require.NoError(t, err)++ t.Cleanup(func() {+ _, _ = container.DB.Exec(fmt.Sprintf("DROP USER IF EXISTS '%s'@'%%'", username))+ })++ return backupPrivilegeFixture{+ Container: container,+ Suffix: suffix,+ Username: username,+ RoleName: fmt.Sprintf("backup_role_%s", suffix),+ Model: &MysqlDatabase{+ Version: version,+ Host: container.Host,+ Port: container.Port,+ Username: username,+ Password: password,+ Database: &container.Database,+ },+ }+}++func (f backupPrivilegeFixture) executeStatements(t *testing.T, statements ...string) {+ t.Helper()++ for _, statement := range statements {+ _, err := f.Container.DB.Exec(statement)+ require.NoError(t, err)+ }+}++func (f backupPrivilegeFixture) createRole(t *testing.T) {+ t.Helper()++ f.executeStatements(t, fmt.Sprintf("CREATE ROLE '%s'", f.RoleName))++ t.Cleanup(func() {+ _, _ = f.Container.DB.Exec(fmt.Sprintf("DROP ROLE IF EXISTS '%s'", f.RoleName))+ })+}++// Every table in the schema joins the dump set, so a case granting per table has to drop the+// tables it created or it would decide the outcome of the cases that follow.+func (f backupPrivilegeFixture) createTables(t *testing.T, tableNames ...string) {+ t.Helper()++ for _, tableName := range tableNames {+ f.executeStatements(t, fmt.Sprintf("CREATE TABLE `%s` (id INT PRIMARY KEY)", tableName))++ t.Cleanup(func() {+ _, _ = f.Container.DB.Exec(fmt.Sprintf("DROP TABLE IF EXISTS `%s`", tableName))+ })+ }+}++func (f backupPrivilegeFixture) createView(t *testing.T, viewName, baseTableName string) {+ t.Helper()++ f.executeStatements(t, fmt.Sprintf(+ "CREATE VIEW `%s` AS SELECT id FROM `%s`",+ viewName,+ baseTableName,+ ))++ t.Cleanup(func() {+ _, _ = f.Container.DB.Exec(fmt.Sprintf("DROP VIEW IF EXISTS `%s`", viewName))+ })+}++func (f backupPrivilegeFixture) testConnection() error {+ return f.Model.TestConnection(slog.New(slog.NewTextHandler(os.Stdout, nil)), nil)+}++func testTestConnectionBackupPrivilegesFromActiveRole(+ t *testing.T,+ endpoint containers.Endpoint,+ version tools.MysqlVersion,+) {+ fixture := createBackupPrivilegeFixture(t, endpoint, version)+ fixture.createRole(t)++ fixture.executeStatements(t,+ fmt.Sprintf(+ "GRANT SELECT, SHOW VIEW, LOCK TABLES, TRIGGER, EVENT ON `%s`.* TO '%s'",+ fixture.Container.Database,+ fixture.RoleName,+ ),+ fmt.Sprintf("GRANT '%s' TO '%s'@'%%'", fixture.RoleName, fixture.Username),+ fmt.Sprintf("SET DEFAULT ROLE ALL TO '%s'@'%%'", fixture.Username),+ "FLUSH PRIVILEGES",+ )++ require.NoError(t, fixture.testConnection())+ assert.Contains(t, fixture.Model.Privileges, "SELECT")+ assert.Contains(t, fixture.Model.Privileges, "SHOW VIEW")+}++// mysqldump logs in fresh, and with activate_all_roles_on_login off a granted-but-inactive role+// gives it nothing, so reading only the active roles is what matches what the dump will have.+// The direct INSERT grant is what lets the account reach the schema at all and puts the table in+// the dump set; everything it would need to read that table sits in the unactivated role.+func testTestConnectionRoleGrantedButNotActivated(+ t *testing.T,+ endpoint containers.Endpoint,+ version tools.MysqlVersion,+) {+ fixture := createBackupPrivilegeFixture(t, endpoint, version)+ fixture.createRole(t)+ blockedTableName := "blocked_" + fixture.Suffix++ fixture.createTables(t, blockedTableName)++ fixture.executeStatements(t,+ fmt.Sprintf(+ "GRANT INSERT ON `%s`.`%s` TO '%s'@'%%'",+ fixture.Container.Database,+ blockedTableName,+ fixture.Username,+ ),+ fmt.Sprintf(+ "GRANT SELECT, SHOW VIEW ON `%s`.* TO '%s'",+ fixture.Container.Database,+ fixture.RoleName,+ ),+ fmt.Sprintf("GRANT '%s' TO '%s'@'%%'", fixture.RoleName, fixture.Username),+ "FLUSH PRIVILEGES",+ )++ err := fixture.testConnection()++ require.Error(t, err)+ assert.Contains(t, err.Error(), "insufficient permissions")+ assert.Contains(+ t,+ err.Error(),+ fmt.Sprintf("SELECT on `%s`.*", fixture.Container.Database),+ )+}++func testTestConnectionTableGrantsCoverEveryVisibleTable(+ t *testing.T,+ endpoint containers.Endpoint,+ version tools.MysqlVersion,+) {+ fixture := createBackupPrivilegeFixture(t, endpoint, version)+ readableTableName := "readable_" + fixture.Suffix++ fixture.createTables(t, readableTableName)++ fixture.executeStatements(t,+ fmt.Sprintf(+ "GRANT SELECT, SHOW VIEW ON `%s`.`%s` TO '%s'@'%%'",+ fixture.Container.Database,+ readableTableName,+ fixture.Username,+ ),+ "FLUSH PRIVILEGES",+ )++ assert.NoError(t, fixture.testConnection())+}++func testTestConnectionVisibleTableLacksSelect(+ t *testing.T,+ endpoint containers.Endpoint,+ version tools.MysqlVersion,+) {+ fixture := createBackupPrivilegeFixture(t, endpoint, version)+ readableTableName := "readable_" + fixture.Suffix+ blockedTableName := "blocked_" + fixture.Suffix++ fixture.createTables(t, readableTableName, blockedTableName)++ fixture.executeStatements(t,+ fmt.Sprintf(+ "GRANT SELECT, SHOW VIEW ON `%s`.`%s` TO '%s'@'%%'",+ fixture.Container.Database,+ readableTableName,+ fixture.Username,+ ),+ fmt.Sprintf(+ "GRANT INSERT ON `%s`.`%s` TO '%s'@'%%'",+ fixture.Container.Database,+ blockedTableName,+ fixture.Username,+ ),+ "FLUSH PRIVILEGES",+ )++ err := fixture.testConnection()++ require.Error(t, err)+ assert.Contains(t, err.Error(), "insufficient permissions")+ assert.Contains(t, err.Error(), blockedTableName)+ assert.NotContains(t, err.Error(), readableTableName)+}++func testTestConnectionVisibleTableLackingSelectIsExcluded(+ t *testing.T,+ endpoint containers.Endpoint,+ version tools.MysqlVersion,+) {+ fixture := createBackupPrivilegeFixture(t, endpoint, version)+ readableTableName := "readable_" + fixture.Suffix+ blockedTableName := "blocked_" + fixture.Suffix++ fixture.createTables(t, readableTableName, blockedTableName)++ fixture.executeStatements(t,+ fmt.Sprintf(+ "GRANT SELECT, SHOW VIEW ON `%s`.`%s` TO '%s'@'%%'",+ fixture.Container.Database,+ readableTableName,+ fixture.Username,+ ),+ fmt.Sprintf(+ "GRANT INSERT ON `%s`.`%s` TO '%s'@'%%'",+ fixture.Container.Database,+ blockedTableName,+ fixture.Username,+ ),+ "FLUSH PRIVILEGES",+ )++ fixture.Model.ExcludeTables = []string{blockedTableName}++ assert.NoError(t, fixture.testConnection())+}++// Nothing else in this package creates a view, so this is what pins readDumpedTables' TABLE_TYPE+// mapping: misclassify a view as a base table and SHOW VIEW silently stops being required.+func testTestConnectionViewLacksShowView(+ t *testing.T,+ endpoint containers.Endpoint,+ version tools.MysqlVersion,+) {+ fixture := createBackupPrivilegeFixture(t, endpoint, version)+ baseTableName := "viewbase_" + fixture.Suffix+ viewName := "viewonly_" + fixture.Suffix++ fixture.createTables(t, baseTableName)+ fixture.createView(t, viewName, baseTableName)++ fixture.executeStatements(t,+ fmt.Sprintf(+ "GRANT SELECT ON `%s`.* TO '%s'@'%%'",+ fixture.Container.Database,+ fixture.Username,+ ),+ "FLUSH PRIVILEGES",+ )++ err := fixture.testConnection()++ require.Error(t, err)+ assert.Contains(t, err.Error(), "insufficient permissions")+ assert.Contains(t, err.Error(), "SHOW VIEW")++ fixture.executeStatements(t,+ fmt.Sprintf(+ "GRANT SHOW VIEW ON `%s`.* TO '%s'@'%%'",+ fixture.Container.Database,+ fixture.Username,+ ),+ "FLUSH PRIVILEGES",+ )++ assert.NoError(t, fixture.testConnection())+}diff --git a/backend/internal/features/databases/databases/mysqlfamily/doc.go b/backend/internal/features/databases/databases/mysqlfamily/doc.gonew file mode 100644index 00000000..fa32031d--- /dev/null+++ b/backend/internal/features/databases/databases/mysqlfamily/doc.go@@ -0,0 +1,8 @@+// Package mysqlfamily holds the grant reading and backup-privilege rules that MySQL and MariaDB+// share. Both speak the same GRANT syntax and both dump through the same tool family, so keeping+// one copy is what keeps the two engine packages from drifting apart.+//+// What stays in the engine packages is what genuinely differs: how active roles are read (MySQL+// expands them with a USING clause, MariaDB has a single active role and no such clause) and the+// version enums, which are distinct types.+package mysqlfamilydiff --git a/backend/internal/features/databases/databases/mysqlfamily/enums.go b/backend/internal/features/databases/databases/mysqlfamily/enums.gonew file mode 100644index 00000000..dfe8bd0e--- /dev/null+++ b/backend/internal/features/databases/databases/mysqlfamily/enums.go@@ -0,0 +1,9 @@+package mysqlfamily++type GrantScope string++const (+ GrantScopeGlobal GrantScope = "global"+ GrantScopeSchema GrantScope = "schema"+ GrantScopeTable GrantScope = "table"+)diff --git a/backend/internal/features/databases/databases/mysqlfamily/grants.go b/backend/internal/features/databases/databases/mysqlfamily/grants.gonew file mode 100644index 00000000..39916d73--- /dev/null+++ b/backend/internal/features/databases/databases/mysqlfamily/grants.go@@ -0,0 +1,298 @@+package mysqlfamily++import (+ "regexp"+ "strings"+)++type Grant struct {+ Privileges []string+ Scope GrantScope+ SchemaPattern string+ TableName string+}++var (+ grantHeadPattern = regexp.MustCompile(`(?is)^\s*GRANT\s+(.+?)\s+ON\s+(.+?)\s+TO\s`)+ revokeHeadPattern = regexp.MustCompile(`(?is)^\s*REVOKE\s+(.+?)\s+ON\s+(.+?)\s+FROM\s`)+)++// A naive substring search over the whole grant line falsely matches privilege keywords that+// appear inside other privilege names, so the list has to be split before it is read: "SHOW+// CREATE ROUTINE" would otherwise register as "CREATE".+func ParseGrantLine(grantLine string) *Grant {+ return parseGrantHead(grantHeadPattern, grantLine)+}++// SHOW GRANTS reports a partial revoke (partial_revokes=ON, the managed-MySQL default shape) as+// a REVOKE line; ignoring it would count a globally granted privilege the server refuses on+// this schema.+func parseRevokeLine(grantLine string) *Grant {+ return parseGrantHead(revokeHeadPattern, grantLine)+}++func parseGrantHead(headPattern *regexp.Regexp, grantLine string) *Grant {+ matches := headPattern.FindStringSubmatch(grantLine)+ if matches == nil {+ return nil+ }++ privileges := parseGrantedPrivileges(matches[1])+ if len(privileges) == 0 {+ return nil+ }++ grant := parseGrantTarget(strings.TrimSpace(matches[2]))+ if grant == nil {+ return nil+ }++ grant.Privileges = privileges++ return grant+}++func parseGrantedPrivileges(privilegeList string) []string {+ parts := splitOutsideParentheses(privilegeList)++ privileges := make([]string, 0, len(parts))++ for _, part := range parts {+ // A column-qualified privilege reaches only the listed columns, so counting it as+ // table-wide would pass an account whose dump later fails on the unlisted ones.+ if strings.Contains(part, "(") {+ continue+ }++ privilege := strings.TrimSpace(strings.ToUpper(part))+ if privilege != "" {+ privileges = append(privileges, privilege)+ }+ }++ return privileges+}++// A column list carries its own commas, so the privilege separator is only the comma that+// sits outside parentheses: "SELECT (col1, col2), UPDATE (col3)" is two privileges, not four.+func splitOutsideParentheses(privilegeList string) []string {+ var parts []string++ parenthesisDepth := 0+ partStart := 0++ for index, character := range privilegeList {+ switch character {+ case '(':+ parenthesisDepth+++ case ')':+ if parenthesisDepth > 0 {+ parenthesisDepth--+ }+ case ',':+ if parenthesisDepth == 0 {+ parts = append(parts, privilegeList[partStart:index])+ partStart = index + 1+ }+ }+ }++ return append(parts, privilegeList[partStart:])+}++// A PROXY grant names an account rather than a schema, and its target is quoted with single+// quotes, which is how it is told apart from the schema forms.+func parseGrantTarget(target string) *Grant {+ if strings.HasPrefix(target, "'") {+ return nil+ }++ schemaPart, tablePart, hasTablePart := cutOnScopeSeparator(target)+ if !hasTablePart {+ return nil+ }++ schemaPattern := unquoteGrantIdentifier(schemaPart)+ tableName := unquoteGrantIdentifier(tablePart)++ if schemaPattern == "*" && tableName == "*" {+ return &Grant{Scope: GrantScopeGlobal}+ }++ if tableName == "*" {+ return &Grant{Scope: GrantScopeSchema, SchemaPattern: schemaPattern}+ }++ return &Grant{+ Scope: GrantScopeTable,+ SchemaPattern: schemaPattern,+ TableName: tableName,+ }+}++// A backquoted schema name may itself contain a dot, so the split has to happen on the separator+// that sits outside the quotes rather than on the first dot in the string.+func cutOnScopeSeparator(target string) (string, string, bool) {+ isInsideQuotes := false++ for index, character := range target {+ switch character {+ case '`':+ isInsideQuotes = !isInsideQuotes+ case '.':+ if !isInsideQuotes {+ return target[:index], target[index+1:], true+ }+ }+ }++ return target, "", false+}++func unquoteGrantIdentifier(identifier string) string {+ return strings.Trim(strings.TrimSpace(identifier), "`\"")+}++// A schema grant is written as a LIKE pattern, so "app%" and "test\_db" have to be matched the way+// the server matches them rather than compared literally.+func isMatchingSchemaPattern(schemaPattern, schemaName string) bool {+ var translatedPattern strings.Builder+ translatedPattern.WriteString("^")++ for index := 0; index < len(schemaPattern); index++ {+ character := schemaPattern[index]++ if character == '\\' && index+1 < len(schemaPattern) {+ index+++ translatedPattern.WriteString(regexp.QuoteMeta(string(schemaPattern[index])))++ continue+ }++ switch character {+ case '%':+ translatedPattern.WriteString(".*")+ case '_':+ translatedPattern.WriteString(".")+ default:+ translatedPattern.WriteString(regexp.QuoteMeta(string(character)))+ }+ }++ translatedPattern.WriteString("$")++ compiledPattern, err := regexp.Compile(translatedPattern.String())+ if err != nil {+ return schemaPattern == schemaName+ }++ return compiledPattern.MatchString(schemaName)+}++type SchemaGrants struct {+ Global map[string]bool+ Schema map[string]bool+ Tables map[string]map[string]bool+ Revoked map[string]bool+}++// ALL PRIVILEGES has to be expanded into the privileges this package consults, and a grant on a+// single table cannot carry the ones that only exist schema-wide.+var (+ schemaWideGrantablePrivileges = []string{"SELECT", "SHOW VIEW", "TRIGGER", "EVENT"}+ tableGrantablePrivileges = []string{"SELECT", "SHOW VIEW", "TRIGGER"}+)++func CollectSchemaGrants(grantLines []string, schemaName string) SchemaGrants {+ collectedGrants := SchemaGrants{+ Global: map[string]bool{},+ Schema: map[string]bool{},+ Tables: map[string]map[string]bool{},+ Revoked: map[string]bool{},+ }++ for _, grantLine := range grantLines {+ if revokedGrant := parseRevokeLine(grantLine); revokedGrant != nil {+ if revokedGrant.Scope == GrantScopeSchema &&+ isMatchingSchemaPattern(revokedGrant.SchemaPattern, schemaName) {+ addGrantedPrivileges(+ collectedGrants.Revoked,+ revokedGrant.Privileges,+ schemaWideGrantablePrivileges,+ )+ }++ continue+ }++ grant := ParseGrantLine(grantLine)+ if grant == nil {+ continue+ }++ switch grant.Scope {+ case GrantScopeGlobal:+ addGrantedPrivileges(collectedGrants.Global, grant.Privileges, schemaWideGrantablePrivileges)+ case GrantScopeSchema:+ if isMatchingSchemaPattern(grant.SchemaPattern, schemaName) {+ addGrantedPrivileges(+ collectedGrants.Schema,+ grant.Privileges,+ schemaWideGrantablePrivileges,+ )+ }+ case GrantScopeTable:+ if !isMatchingSchemaPattern(grant.SchemaPattern, schemaName) {+ continue+ }++ if collectedGrants.Tables[grant.TableName] == nil {+ collectedGrants.Tables[grant.TableName] = map[string]bool{}+ }++ addGrantedPrivileges(+ collectedGrants.Tables[grant.TableName],+ grant.Privileges,+ tableGrantablePrivileges,+ )+ }+ }++ return collectedGrants+}++func addGrantedPrivileges(+ grantedPrivileges map[string]bool,+ privileges []string,+ grantableAtThisScope []string,+) {+ for _, privilege := range privileges {+ if privilege == "ALL PRIVILEGES" || privilege == "ALL" {+ for _, grantablePrivilege := range grantableAtThisScope {+ grantedPrivileges[grantablePrivilege] = true+ }++ continue+ }++ grantedPrivileges[privilege] = true+ }+}++// A partial revoke subtracts only from the global level: the server lifts it again the moment+// the same privilege is granted on the schema explicitly.+func (g SchemaGrants) HasForSchema(privilege string) bool {+ if g.Schema[privilege] {+ return true+ }++ return g.Global[privilege] && !g.Revoked[privilege]+}++func (g SchemaGrants) HasForTable(privilege, tableName string) bool {+ if g.HasForSchema(privilege) {+ return true+ }++ return g.Tables[tableName][privilege]+}diff --git a/backend/internal/features/databases/databases/mysqlfamily/grants_test.go b/backend/internal/features/databases/databases/mysqlfamily/grants_test.gonew file mode 100644index 00000000..77bb3e46--- /dev/null+++ b/backend/internal/features/databases/databases/mysqlfamily/grants_test.go@@ -0,0 +1,243 @@+package mysqlfamily++import (+ "testing"++ "github.com/stretchr/testify/assert"+ "github.com/stretchr/testify/require"+)++func Test_ParseGrantLine_WithGlobalSchemaAndTableScopes_ReturnsScopeAndPrivileges(t *testing.T) {+ grantCases := []struct {+ name string+ grantLine string+ privileges []string+ scope GrantScope+ schemaPattern string+ tableName string+ }{+ {+ // issue #568: the privilege list must be split before it is read, or "SHOW CREATE+ // ROUTINE" registers as a CREATE grant.+ name: "SHOW CREATE ROUTINE is not split into CREATE",+ grantLine: "GRANT SELECT, SHOW VIEW, SHOW CREATE ROUTINE ON *.* TO 'backup'@'%'",+ privileges: []string{"SELECT", "SHOW VIEW", "SHOW CREATE ROUTINE"},+ scope: GrantScopeGlobal,+ },+ {+ name: "global write privileges",+ grantLine: "GRANT SELECT, INSERT, UPDATE ON *.* TO 'x'@'%'",+ privileges: []string{"SELECT", "INSERT", "UPDATE"},+ scope: GrantScopeGlobal,+ },+ {+ name: "schema scoped all privileges",+ grantLine: "GRANT ALL PRIVILEGES ON `appdb`.* TO 'x'@'%'",+ privileges: []string{"ALL PRIVILEGES"},+ scope: GrantScopeSchema,+ schemaPattern: "appdb",+ },+ {+ name: "usage only line",+ grantLine: "GRANT USAGE ON *.* TO 'x'@'%'",+ privileges: []string{"USAGE"},+ scope: GrantScopeGlobal,+ },+ {+ name: "column level privilege does not count as table wide",+ grantLine: "GRANT SELECT (col1, col2), SHOW VIEW ON `appdb`.`orders` TO 'x'@'%'",+ privileges: []string{"SHOW VIEW"},+ scope: GrantScopeTable,+ schemaPattern: "appdb",+ tableName: "orders",+ },+ {+ name: "table scoped grant",+ grantLine: "GRANT SELECT, SHOW VIEW ON `appdb`.`orders` TO 'backup'@'%'",+ privileges: []string{"SELECT", "SHOW VIEW"},+ scope: GrantScopeTable,+ schemaPattern: "appdb",+ tableName: "orders",+ },+ {+ name: "wildcard schema pattern is kept as written",+ grantLine: "GRANT SELECT ON `app%`.* TO 'x'@'%'",+ privileges: []string{"SELECT"},+ scope: GrantScopeSchema,+ schemaPattern: "app%",+ },+ {+ name: "escaped underscore in the schema pattern",+ grantLine: "GRANT SELECT ON `test\\_db`.* TO 'x'@'%'",+ privileges: []string{"SELECT"},+ scope: GrantScopeSchema,+ schemaPattern: `test\_db`,+ },+ {+ name: "schema name containing a dot",+ grantLine: "GRANT SELECT ON `my.db`.`orders` TO 'x'@'%'",+ privileges: []string{"SELECT"},+ scope: GrantScopeTable,+ schemaPattern: "my.db",+ tableName: "orders",+ },+ {+ name: "with grant option trailer is ignored",+ grantLine: "GRANT SELECT, INSERT ON *.* TO 'x'@'%' WITH GRANT OPTION",+ privileges: []string{"SELECT", "INSERT"},+ scope: GrantScopeGlobal,+ },+ {+ name: "mixed case keywords",+ grantLine: "grant Select, Update on *.* to 'x'@'%'",+ privileges: []string{"SELECT", "UPDATE"},+ scope: GrantScopeGlobal,+ },+ }++ for _, grantCase := range grantCases {+ t.Run(grantCase.name, func(t *testing.T) {+ grant := ParseGrantLine(grantCase.grantLine)++ require.NotNil(t, grant)+ assert.Equal(t, grantCase.privileges, grant.Privileges)+ assert.Equal(t, grantCase.scope, grant.Scope)+ assert.Equal(t, grantCase.schemaPattern, grant.SchemaPattern)+ assert.Equal(t, grantCase.tableName, grant.TableName)+ })+ }+}++func Test_ParseGrantLine_WithoutAGrantedScope_ReturnsNil(t *testing.T) {+ ignoredGrantLines := []struct {+ name string+ grantLine string+ }{+ {"role grant has no ON clause", "GRANT `reporting_role`@`%` TO 'u'@'%'"},+ {"proxy grant names an account", "GRANT PROXY ON 'other'@'%' TO 'u'@'%'"},+ {"revoke line is not a grant", "REVOKE SELECT ON `appdb`.* FROM 'u'@'%'"},+ {+ "every privilege is column qualified",+ "GRANT SELECT (on) ON `appdb`.`orders` TO 'x'@'%'",+ },+ }++ for _, ignoredGrantLine := range ignoredGrantLines {+ t.Run(ignoredGrantLine.name, func(t *testing.T) {+ assert.Nil(t, ParseGrantLine(ignoredGrantLine.grantLine))+ })+ }+}++func Test_IsMatchingSchemaPattern_WithWildcardsAndEscapes_MatchesServerRule(t *testing.T) {+ matchCases := []struct {+ name string+ schemaPattern string+ schemaName string+ isMatching bool+ }{+ {"plain name matches itself", "appdb", "appdb", true},+ {"plain name does not match another", "appdb", "otherdb", false},+ {"percent matches a suffix", "app%", "appdb", true},+ {"percent matches everything", "%", "anything", true},+ {"underscore matches one character", "app_b", "appXb", true},+ {"escaped underscore is literal", `test\_db`, "test_db", true},+ {"escaped underscore rejects another character", `test\_db`, "testXdb", false},+ {"pattern must match the whole name", "app", "appdb", false},+ }++ for _, matchCase := range matchCases {+ t.Run(matchCase.name, func(t *testing.T) {+ assert.Equal(+ t,+ matchCase.isMatching,+ isMatchingSchemaPattern(matchCase.schemaPattern, matchCase.schemaName),+ )+ })+ }+}++func Test_CollectSchemaGrants_WhenAllPrivilegesIsTableScoped_DoesNotGrantEvent(t *testing.T) {+ collectedGrants := CollectSchemaGrants(+ []string{"GRANT ALL PRIVILEGES ON `appdb`.`orders` TO 'x'@'%'"},+ "appdb",+ )++ assert.True(t, collectedGrants.HasForTable("SELECT", "orders"))+ assert.True(t, collectedGrants.HasForTable("SHOW VIEW", "orders"))+ assert.False(t, collectedGrants.HasForTable("EVENT", "orders"))+}++func Test_CollectSchemaGrants_WhenGrantTargetsAnotherSchema_IsIgnored(t *testing.T) {+ collectedGrants := CollectSchemaGrants(+ []string{"GRANT SELECT ON `otherdb`.* TO 'x'@'%'"},+ "appdb",+ )++ assert.False(t, collectedGrants.HasForSchema("SELECT"))+}++func Test_CollectSchemaGrants_WhenSchemaIsPartiallyRevoked_SubtractsFromGlobal(t *testing.T) {+ collectedGrants := CollectSchemaGrants(+ []string{+ "GRANT SELECT, SHOW VIEW ON *.* TO 'x'@'%'",+ "REVOKE SELECT ON `appdb`.* FROM 'x'@'%'",+ },+ "appdb",+ )++ assert.False(t, collectedGrants.HasForSchema("SELECT"))+ assert.True(t, collectedGrants.HasForSchema("SHOW VIEW"))+}++func Test_CollectSchemaGrants_WhenRevokeUsesSchemaPattern_SubtractsFromGlobal(t *testing.T) {+ collectedGrants := CollectSchemaGrants(+ []string{+ "GRANT SELECT ON *.* TO 'x'@'%'",+ "REVOKE SELECT ON `app%`.* FROM 'x'@'%'",+ },+ "appdb",+ )++ assert.False(t, collectedGrants.HasForSchema("SELECT"))+}++func Test_CollectSchemaGrants_WhenAnotherSchemaIsRevoked_GlobalGrantStillCounts(t *testing.T) {+ collectedGrants := CollectSchemaGrants(+ []string{+ "GRANT SELECT ON *.* TO 'x'@'%'",+ "REVOKE SELECT ON `otherdb`.* FROM 'x'@'%'",+ },+ "appdb",+ )++ assert.True(t, collectedGrants.HasForSchema("SELECT"))+}++func Test_CollectSchemaGrants_WhenRevokedSchemaIsGrantedExplicitly_SchemaGrantWins(t *testing.T) {+ collectedGrants := CollectSchemaGrants(+ []string{+ "GRANT SELECT ON *.* TO 'x'@'%'",+ "REVOKE SELECT ON `appdb`.* FROM 'x'@'%'",+ "GRANT SELECT ON `appdb`.* TO 'x'@'%'",+ },+ "appdb",+ )++ assert.True(t, collectedGrants.HasForSchema("SELECT"))+}++func Test_CollectSchemaGrants_WhenAllPrivilegesAreRevoked_EveryRequiredOneIsGone(t *testing.T) {+ collectedGrants := CollectSchemaGrants(+ []string{+ "GRANT ALL PRIVILEGES ON *.* TO 'x'@'%'",+ "REVOKE ALL PRIVILEGES ON `appdb`.* FROM 'x'@'%'",+ },+ "appdb",+ )++ assert.False(t, collectedGrants.HasForSchema("SELECT"))+ assert.False(t, collectedGrants.HasForSchema("SHOW VIEW"))+ assert.False(t, collectedGrants.HasForSchema("TRIGGER"))+ assert.False(t, collectedGrants.HasForSchema("EVENT"))+}diff --git a/backend/internal/features/databases/databases/mysqlfamily/privileges.go b/backend/internal/features/databases/databases/mysqlfamily/privileges.gonew file mode 100644index 00000000..376f5de1--- /dev/null+++ b/backend/internal/features/databases/databases/mysqlfamily/privileges.go@@ -0,0 +1,199 @@+package mysqlfamily++import (+ "fmt"+ "slices"+ "strings"++ "databasus-backend/internal/util/namelist"+)++const viewTableType = "VIEW"++// A view is dumped by its definition, which is what SHOW VIEW unlocks; a base table is dumped by+// its rows. Requiring SHOW VIEW on a schema that holds no views would reject an account whose dump+// would have succeeded.+type DumpedTable struct {+ Name string+ IsView bool+}++func (t DumpedTable) getRequiredPrivileges() []string {+ if t.IsView {+ return []string{"SELECT", "SHOW VIEW"}+ }++ return []string{"SELECT"}+}++// The dump tool aborts on an object it can list but cannot read, so these decide whether a dump can+// run at all. Which of them a given object actually needs is DumpedTable's business.+var requiredBackupPrivileges = []string{"SELECT", "SHOW VIEW"}++// TRIGGER and EVENT are not required: without them the dump tool is told to leave those objects+// out rather than fail, and the stored set is what the backup consults to decide that.+var recordedBackupPrivileges = []string{"SELECT", "SHOW VIEW", "TRIGGER", "EVENT"}++type BackupPrivilegesSpec struct {+ SchemaName string+ Grantee string+ DumpedTables []DumpedTable+ Grants SchemaGrants+}++type MissingBackupPrivilege struct {+ Privilege string+ Scope GrantScope+ Tables []string+}++type BackupPrivileges struct {+ EffectivePrivileges []string+ Missing []MissingBackupPrivilege+ SchemaName string+ Grantee string+}++func GetBackupPrivileges(spec BackupPrivilegesSpec) BackupPrivileges {+ backupPrivileges := BackupPrivileges{+ SchemaName: spec.SchemaName,+ Grantee: spec.Grantee,+ }++ for _, privilege := range recordedBackupPrivileges {+ if isPrivilegeCoveringDump(spec, privilege) {+ backupPrivileges.EffectivePrivileges = append(backupPrivileges.EffectivePrivileges, privilege)+ }+ }++ slices.Sort(backupPrivileges.EffectivePrivileges)++ for _, privilege := range requiredBackupPrivileges {+ if missingPrivilege, isMissing := getMissingPrivilege(spec, privilege); isMissing {+ backupPrivileges.Missing = append(backupPrivileges.Missing, missingPrivilege)+ }+ }++ return backupPrivileges+}++func isPrivilegeCoveringDump(spec BackupPrivilegesSpec, privilege string) bool {+ if len(spec.DumpedTables) == 0 {+ return spec.Grants.HasForSchema(privilege)+ }++ for _, dumpedTable := range spec.DumpedTables {+ if !spec.Grants.HasForTable(privilege, dumpedTable.Name) {+ return false+ }+ }++ return true+}++// A privilege absent everywhere is reported once against the schema; listing every table would+// bury the fix under noise when the role simply holds nothing.+func getMissingPrivilege(+ spec BackupPrivilegesSpec,+ privilege string,+) (MissingBackupPrivilege, bool) {+ if len(spec.DumpedTables) == 0 {+ if spec.Grants.HasForSchema(privilege) {+ return MissingBackupPrivilege{}, false+ }++ return MissingBackupPrivilege{Privilege: privilege, Scope: GrantScopeSchema}, true+ }++ var tablesMissingPrivilege []string+ requiringTableCount := 0++ for _, dumpedTable := range spec.DumpedTables {+ if !slices.Contains(dumpedTable.getRequiredPrivileges(), privilege) {+ continue+ }++ requiringTableCount++++ if !spec.Grants.HasForTable(privilege, dumpedTable.Name) {+ tablesMissingPrivilege = append(tablesMissingPrivilege, dumpedTable.Name)+ }+ }++ if len(tablesMissingPrivilege) == 0 {+ return MissingBackupPrivilege{}, false+ }++ if len(tablesMissingPrivilege) == requiringTableCount {+ return MissingBackupPrivilege{Privilege: privilege, Scope: GrantScopeSchema}, true+ }++ return MissingBackupPrivilege{+ Privilege: privilege,+ Scope: GrantScopeTable,+ Tables: tablesMissingPrivilege,+ }, true+}++func (p BackupPrivileges) IsSufficientForDump() bool {+ return len(p.Missing) == 0+}++func (p BackupPrivileges) GetEffectivePrivilegesCsv() string {+ return strings.Join(p.EffectivePrivileges, ",")+}++func (p BackupPrivileges) NewInsufficiencyError() error {+ missingDescriptions := make([]string, 0, len(p.Missing))+ missingPrivilegeNames := make([]string, 0, len(p.Missing))++ for _, missingPrivilege := range p.Missing {+ missingPrivilegeNames = append(missingPrivilegeNames, missingPrivilege.Privilege)++ if missingPrivilege.Scope == GrantScopeTable {+ missingDescriptions = append(missingDescriptions, fmt.Sprintf(+ "%s on %s",+ missingPrivilege.Privilege,+ namelist.FormatTruncatedNames(qualifyTableNames(p.SchemaName, missingPrivilege.Tables)),+ ))++ continue+ }++ missingDescriptions = append(missingDescriptions, fmt.Sprintf(+ "%s on `%s`.*",+ missingPrivilege.Privilege,+ p.SchemaName,+ ))+ }++ // The suggested grant is always schema-wide: it repairs every listed table at once and keeps+ // covering the tables the schema gains later.+ return fmt.Errorf(+ "insufficient permissions for backup. Missing: %s. Run: GRANT %s ON `%s`.* TO %s;",+ strings.Join(missingDescriptions, ", "),+ strings.Join(missingPrivilegeNames, ", "),+ p.SchemaName,+ p.Grantee,+ )+}++func HasPrivilege(grantedPrivileges, privilege string) bool {+ for grantedPrivilege := range strings.SplitSeq(grantedPrivileges, ",") {+ if strings.TrimSpace(grantedPrivilege) == privilege {+ return true+ }+ }++ return false+}++func qualifyTableNames(schemaName string, tableNames []string) []string {+ qualifiedNames := make([]string, 0, len(tableNames))++ for _, tableName := range tableNames {+ qualifiedNames = append(qualifiedNames, fmt.Sprintf("`%s`.`%s`", schemaName, tableName))+ }++ return qualifiedNames+}diff --git a/backend/internal/features/databases/databases/mysqlfamily/privileges_test.go b/backend/internal/features/databases/databases/mysqlfamily/privileges_test.gonew file mode 100644index 00000000..f7d3ddeb--- /dev/null+++ b/backend/internal/features/databases/databases/mysqlfamily/privileges_test.go@@ -0,0 +1,208 @@+package mysqlfamily++import (+ "fmt"+ "testing"++ "github.com/stretchr/testify/assert"+ "github.com/stretchr/testify/require"+)++func newBackupPrivilegesSpec(grantLines, dumpedTableNames []string) BackupPrivilegesSpec {+ dumpedTables := make([]DumpedTable, 0, len(dumpedTableNames))+ for _, tableName := range dumpedTableNames {+ dumpedTables = append(dumpedTables, DumpedTable{Name: tableName})+ }++ return BackupPrivilegesSpec{+ SchemaName: "appdb",+ Grantee: "'backup'@'%'",+ DumpedTables: dumpedTables,+ Grants: CollectSchemaGrants(grantLines, "appdb"),+ }+}++func Test_GetBackupPrivileges_WhenSchemaGrantCoversDumpSet_ReportsSufficient(t *testing.T) {+ backupPrivileges := GetBackupPrivileges(newBackupPrivilegesSpec(+ []string{"GRANT SELECT, SHOW VIEW, TRIGGER ON `appdb`.* TO 'backup'@'%'"},+ []string{"orders", "payments"},+ ))++ assert.True(t, backupPrivileges.IsSufficientForDump())+ assert.Equal(t, "SELECT,SHOW VIEW,TRIGGER", backupPrivileges.GetEffectivePrivilegesCsv())+}++func Test_GetBackupPrivileges_WhenTableGrantsCoverEveryDumpedTable_ReportsSufficient(t *testing.T) {+ backupPrivileges := GetBackupPrivileges(newBackupPrivilegesSpec(+ []string{+ "GRANT SELECT, SHOW VIEW ON `appdb`.`orders` TO 'backup'@'%'",+ "GRANT SELECT, SHOW VIEW ON `appdb`.`payments` TO 'backup'@'%'",+ },+ []string{"orders", "payments"},+ ))++ assert.True(t, backupPrivileges.IsSufficientForDump())+ assert.Equal(t, "SELECT,SHOW VIEW", backupPrivileges.GetEffectivePrivilegesCsv())+}++// mysqldump needs SHOW VIEW only to emit a view's definition, so a schema of plain tables must not+// be rejected for lacking it.+func Test_GetBackupPrivileges_WhenSchemaHoldsNoViews_DoesNotRequireShowView(t *testing.T) {+ backupPrivileges := GetBackupPrivileges(newBackupPrivilegesSpec(+ []string{"GRANT SELECT ON `appdb`.* TO 'backup'@'%'"},+ []string{"orders", "payments"},+ ))++ assert.True(t, backupPrivileges.IsSufficientForDump())+}++// The base table sits in the fixture to show it is not implicated by a view's missing privilege.+func Test_GetBackupPrivileges_WhenEveryViewLacksShowView_ReportsSchemaScope(t *testing.T) {+ backupPrivileges := GetBackupPrivileges(BackupPrivilegesSpec{+ SchemaName: "appdb",+ Grantee: "'backup'@'%'",+ DumpedTables: []DumpedTable{+ {Name: "orders"},+ {Name: "orders_view", IsView: true},+ },+ Grants: CollectSchemaGrants(+ []string{"GRANT SELECT ON `appdb`.* TO 'backup'@'%'"},+ "appdb",+ ),+ })++ require.Len(t, backupPrivileges.Missing, 1)+ assert.Equal(t, "SHOW VIEW", backupPrivileges.Missing[0].Privilege)+ assert.Equal(t, GrantScopeSchema, backupPrivileges.Missing[0].Scope)+}++func Test_GetBackupPrivileges_WhenOneOfTwoViewsLacksShowView_ListsThatView(t *testing.T) {+ backupPrivileges := GetBackupPrivileges(BackupPrivilegesSpec{+ SchemaName: "appdb",+ Grantee: "'backup'@'%'",+ DumpedTables: []DumpedTable{+ {Name: "orders"},+ {Name: "granted_view", IsView: true},+ {Name: "ungranted_view", IsView: true},+ },+ Grants: CollectSchemaGrants(+ []string{+ "GRANT SELECT ON `appdb`.* TO 'backup'@'%'",+ "GRANT SHOW VIEW ON `appdb`.`granted_view` TO 'backup'@'%'",+ },+ "appdb",+ ),+ })++ require.Len(t, backupPrivileges.Missing, 1)+ assert.Equal(t, "SHOW VIEW", backupPrivileges.Missing[0].Privilege)+ assert.Equal(t, GrantScopeTable, backupPrivileges.Missing[0].Scope)+ assert.Equal(t, []string{"ungranted_view"}, backupPrivileges.Missing[0].Tables)+}++func Test_GetBackupPrivileges_WhenPrivilegeIsMissingOnSomeTables_ListsThoseTables(t *testing.T) {+ backupPrivileges := GetBackupPrivileges(newBackupPrivilegesSpec(+ []string{+ "GRANT SHOW VIEW ON `appdb`.* TO 'backup'@'%'",+ "GRANT SELECT ON `appdb`.`orders` TO 'backup'@'%'",+ },+ []string{"orders", "payments"},+ ))++ require.Len(t, backupPrivileges.Missing, 1)+ assert.Equal(t, "SELECT", backupPrivileges.Missing[0].Privilege)+ assert.Equal(t, GrantScopeTable, backupPrivileges.Missing[0].Scope)+ assert.Equal(t, []string{"payments"}, backupPrivileges.Missing[0].Tables)+ assert.False(t, backupPrivileges.IsSufficientForDump())+}++// A privilege is recorded only when it covers the whole dump, because the dump flags it gates+// are all-or-nothing for the run.+func Test_GetBackupPrivileges_WhenTriggerCoversOnlySomeTables_OmitsThePrivilege(t *testing.T) {+ backupPrivileges := GetBackupPrivileges(newBackupPrivilegesSpec(+ []string{+ "GRANT SELECT, SHOW VIEW ON `appdb`.* TO 'backup'@'%'",+ "GRANT TRIGGER ON `appdb`.`orders` TO 'backup'@'%'",+ },+ []string{"orders", "payments"},+ ))++ assert.True(t, backupPrivileges.IsSufficientForDump())+ assert.False(t, HasPrivilege(backupPrivileges.GetEffectivePrivilegesCsv(), "TRIGGER"))+}++func Test_GetBackupPrivileges_WhenSchemaHasNoTables_FallsBackToSchemaGrants(t *testing.T) {+ backupPrivileges := GetBackupPrivileges(newBackupPrivilegesSpec(+ []string{"GRANT SELECT, SHOW VIEW ON `appdb`.* TO 'backup'@'%'"},+ nil,+ ))++ assert.True(t, backupPrivileges.IsSufficientForDump())+}++func Test_GetBackupPrivileges_WhenRoleHoldsNothing_ReportsTheMissingSelect(t *testing.T) {+ backupPrivileges := GetBackupPrivileges(newBackupPrivilegesSpec(+ []string{"GRANT USAGE ON *.* TO 'backup'@'%'"},+ []string{"orders"},+ ))++ require.Len(t, backupPrivileges.Missing, 1)+ assert.Equal(t, "SELECT", backupPrivileges.Missing[0].Privilege)+ assert.Equal(t, GrantScopeSchema, backupPrivileges.Missing[0].Scope)+ assert.False(t, backupPrivileges.IsSufficientForDump())+}++func Test_GetBackupPrivileges_WhenAllPrivilegesIsTableScoped_OmitsEvent(t *testing.T) {+ backupPrivileges := GetBackupPrivileges(newBackupPrivilegesSpec(+ []string{"GRANT ALL PRIVILEGES ON `appdb`.`orders` TO 'backup'@'%'"},+ []string{"orders"},+ ))++ assert.True(t, backupPrivileges.IsSufficientForDump())+ assert.False(t, HasPrivilege(backupPrivileges.GetEffectivePrivilegesCsv(), "EVENT"))+}++func Test_NewInsufficiencyError_WhenPrivilegeIsMissingOnSomeTables_SuggestsSchemaWideGrant(t *testing.T) {+ backupPrivileges := GetBackupPrivileges(newBackupPrivilegesSpec(+ []string{+ "GRANT SHOW VIEW ON `appdb`.* TO 'backup'@'%'",+ "GRANT SELECT ON `appdb`.`orders` TO 'backup'@'%'",+ },+ []string{"orders", "payments"},+ ))++ err := backupPrivileges.NewInsufficiencyError()++ require.Error(t, err)+ assert.Contains(t, err.Error(), "insufficient permissions")+ assert.Contains(t, err.Error(), "`appdb`.`payments`")+ assert.NotContains(t, err.Error(), "`appdb`.`orders`")+ assert.Contains(t, err.Error(), "GRANT SELECT ON `appdb`.* TO 'backup'@'%';")+}++func Test_NewInsufficiencyError_WhenManyTablesAreMissingSelect_CapsTheTableList(t *testing.T) {+ var dumpedTables []string+ for tableIndex := range 9 {+ dumpedTables = append(dumpedTables, fmt.Sprintf("table_%d", tableIndex))+ }++ backupPrivileges := GetBackupPrivileges(newBackupPrivilegesSpec(+ []string{+ "GRANT SHOW VIEW ON `appdb`.* TO 'backup'@'%'",+ "GRANT SELECT ON `appdb`.`table_0` TO 'backup'@'%'",+ },+ dumpedTables,+ ))++ err := backupPrivileges.NewInsufficiencyError()++ require.Error(t, err)+ assert.Contains(t, err.Error(), "and 3 more")+}++func Test_HasPrivilege_WithCommaSeparatedTokens_MatchesWholeTokensOnly(t *testing.T) {+ assert.True(t, HasPrivilege("SELECT,SHOW VIEW,TRIGGER", "SHOW VIEW"))+ assert.False(t, HasPrivilege("SELECT,SHOW VIEW", "TRIGGER"))+ assert.False(t, HasPrivilege("SHOW CREATE ROUTINE", "CREATE"))+ assert.False(t, HasPrivilege("", "SELECT"))+}diff --git a/backend/internal/features/databases/databases/mysqlfamily/reader.go b/backend/internal/features/databases/databases/mysqlfamily/reader.gonew file mode 100644index 00000000..2add225f--- /dev/null+++ b/backend/internal/features/databases/databases/mysqlfamily/reader.go@@ -0,0 +1,129 @@+package mysqlfamily++import (+ "context"+ "database/sql"+ "fmt"+ "slices"+ "strings"++ "databasus-backend/internal/util/namelist"+)++type BackupPrivilegesQuery struct {+ DB *sql.DB+ SchemaName string+ ExcludeTables []string++ // Queried by the caller, because that is where the engines differ: MySQL expands its active+ // roles with a USING clause, MariaDB walks a single active role instead.+ GrantLines []string+}++func ReadBackupPrivileges(+ ctx context.Context,+ query BackupPrivilegesQuery,+) (BackupPrivileges, error) {+ grantee, err := readGrantee(ctx, query.DB)+ if err != nil {+ return BackupPrivileges{}, err+ }++ dumpedTables, err := readDumpedTables(ctx, query)+ if err != nil {+ return BackupPrivileges{}, err+ }++ return GetBackupPrivileges(BackupPrivilegesSpec{+ SchemaName: query.SchemaName,+ Grantee: grantee,+ DumpedTables: dumpedTables,+ Grants: CollectSchemaGrants(query.GrantLines, query.SchemaName),+ }), nil+}++// CURRENT_USER() returns "name@host". A username may itself contain an @ while a hostname cannot,+// so the account splits on the last one.+func readGrantee(ctx context.Context, db *sql.DB) (string, error) {+ var currentUser string+ if err := db.QueryRowContext(ctx, "SELECT CURRENT_USER()").Scan(¤tUser); err != nil {+ return "", fmt.Errorf("failed to read the current user: %w", err)+ }++ separatorIndex := strings.LastIndex(currentUser, "@")+ if separatorIndex < 0 {+ return fmt.Sprintf("'%s'", currentUser), nil+ }++ return fmt.Sprintf(+ "'%s'@'%s'",+ currentUser[:separatorIndex],+ currentUser[separatorIndex+1:],+ ), nil+}++func QueryGrantLines(ctx context.Context, db *sql.DB, query string) ([]string, error) {+ rows, err := db.QueryContext(ctx, query)+ if err != nil {+ return nil, fmt.Errorf("failed to check grants: %w", err)+ }+ defer func() { _ = rows.Close() }()++ var grantLines []string++ for rows.Next() {+ var grantLine string+ if err := rows.Scan(&grantLine); err != nil {+ return nil, fmt.Errorf("failed to scan grant: %w", err)+ }++ grantLines = append(grantLines, grantLine)+ }++ if err := rows.Err(); err != nil {+ return nil, fmt.Errorf("error iterating grants: %w", err)+ }++ return grantLines, nil+}++// information_schema.TABLES is itself filtered by privilege, so this is the set the dump tool will+// enumerate: a table the account cannot see at all is skipped by both it and us.+func readDumpedTables(ctx context.Context, query BackupPrivilegesQuery) ([]DumpedTable, error) {+ rows, err := query.DB.QueryContext(ctx, `+ SELECT TABLE_NAME, TABLE_TYPE+ FROM information_schema.TABLES+ WHERE TABLE_SCHEMA = ? AND TABLE_TYPE IN ('BASE TABLE', 'VIEW')+ `, query.SchemaName)+ if err != nil {+ return nil, fmt.Errorf("failed to list tables: %w", err)+ }+ defer func() { _ = rows.Close() }()++ excludedTableNames := namelist.NormalizeUniqueNames(query.ExcludeTables)+ var dumpedTables []DumpedTable++ for rows.Next() {+ var tableName string+ var tableType string+ if err := rows.Scan(&tableName, &tableType); err != nil {+ return nil, fmt.Errorf("failed to scan dumped table: %w", err)+ }++ // --ignore-table matches the name exactly, and table names are case sensitive on Linux.+ if slices.Contains(excludedTableNames, tableName) {+ continue+ }++ dumpedTables = append(dumpedTables, DumpedTable{+ Name: tableName,+ IsView: tableType == viewTableType,+ })+ }++ if err := rows.Err(); err != nil {+ return nil, fmt.Errorf("error iterating table names: %w", err)+ }++ return dumpedTables, nil+}diff --git a/backend/internal/features/databases/databases/postgresql/logical/dumpscope.go b/backend/internal/features/databases/databases/postgresql/logical/dumpscope.gonew file mode 100644index 00000000..92fcd3d1--- /dev/null+++ b/backend/internal/features/databases/databases/postgresql/logical/dumpscope.go@@ -0,0 +1,131 @@+package postgresql_logical++import (+ "path"+ "strings"++ "databasus-backend/internal/util/namelist"+)++// pg_dump emits no data for a relation that belongs to an extension, so such a relation+// needs no read privilege and adds nothing to the restored size. The exception is a table+// the extension registered through pg_extension_config_dump: its contents are dumped like+// any other, which is how TimescaleDB's catalog reaches the archive.+const dumpedRelationScope = `+ FROM pg_class c+ JOIN pg_namespace n ON n.oid = c.relnamespace+ WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')+ AND n.nspname NOT LIKE 'pg\_toast%'+ AND n.nspname NOT LIKE 'pg\_temp%'+ AND (cardinality($1::text[]) = 0 OR n.nspname = ANY($1::text[]))+ AND (+ NOT EXISTS (+ SELECT 1+ FROM pg_depend d+ WHERE d.classid = 'pg_class'::regclass+ AND d.objid = c.oid+ AND d.objsubid = 0+ AND d.deptype = 'e'+ )+ OR EXISTS (SELECT 1 FROM pg_extension e WHERE c.oid = ANY(e.extconfig))+ )+`++type DumpFilter struct {+ IncludeSchemas []string+ ExcludeTablePatterns []string+}++type relationName struct {+ SchemaName string+ TableName string+}++// The dump arguments are built from normalized names, so a pasted multiline value must be+// normalized here too or the scope query would silently match nothing.+func (f DumpFilter) getIncludedSchemaNames() []string {+ return namelist.NormalizeUniqueNames(f.IncludeSchemas)+}++// Same parity requirement as getIncludedSchemaNames: pg_dump receives the exclude patterns+// normalized, so an unnormalized pattern here would demand SELECT on a table the dump skips.+func (f DumpFilter) getExcludeTablePatterns() []string {+ return namelist.NormalizeUniqueNames(f.ExcludeTablePatterns)+}++func isRelationExcluded(relation relationName, excludeTablePatterns []string) bool {+ for _, excludeTablePattern := range excludeTablePatterns {+ if parseDumpTablePattern(excludeTablePattern).isMatchingRelation(relation) {+ return true+ }+ }++ return false+}++type dumpTablePattern struct {+ SchemaGlob globPattern+ TableGlob globPattern+ IsSchemaQualified bool+}++func (p dumpTablePattern) isMatchingRelation(relation relationName) bool {+ if p.IsSchemaQualified && !p.SchemaGlob.isMatchingName(relation.SchemaName) {+ return false+ }++ return p.TableGlob.isMatchingName(relation.TableName)+}++// Mirrors pg_dump's --exclude-table: an unqualified pattern hits the table name in+// every schema, and the split happens on the first dot outside double quotes.+func parseDumpTablePattern(excludeTablePattern string) dumpTablePattern {+ isInsideQuotes := false++ for index, character := range excludeTablePattern {+ switch character {+ case '"':+ isInsideQuotes = !isInsideQuotes+ case '.':+ if !isInsideQuotes {+ return dumpTablePattern{+ SchemaGlob: parseGlobPattern(excludeTablePattern[:index]),+ TableGlob: parseGlobPattern(excludeTablePattern[index+1:]),+ IsSchemaQualified: true,+ }+ }+ }+ }++ return dumpTablePattern{TableGlob: parseGlobPattern(excludeTablePattern)}+}++type globPattern struct {+ Value string+ IsLiteral bool+}++func parseGlobPattern(patternPart string) globPattern {+ if len(patternPart) >= 2 &&+ strings.HasPrefix(patternPart, `"`) &&+ strings.HasSuffix(patternPart, `"`) {+ unquoted := strings.ReplaceAll(patternPart[1:len(patternPart)-1], `""`, `"`)++ return globPattern{Value: unquoted, IsLiteral: true}+ }++ return globPattern{Value: patternPart}+}++func (g globPattern) isMatchingName(name string) bool {+ if g.IsLiteral {+ return g.Value == name+ }++ isMatching, err := path.Match(g.Value, name)+ if err != nil {+ return g.Value == name+ }++ return isMatching+}diff --git a/backend/internal/features/databases/databases/postgresql/logical/size_test.go b/backend/internal/features/databases/databases/postgresql/logical/dumpscope_test.gosimilarity index 90%rename from backend/internal/features/databases/databases/postgresql/logical/size_test.gorename to backend/internal/features/databases/databases/postgresql/logical/dumpscope_test.goindex 441e28f3..bbecd7de 100644--- a/backend/internal/features/databases/databases/postgresql/logical/size_test.go+++ b/backend/internal/features/databases/databases/postgresql/logical/dumpscope_test.go@@ -6,6 +6,19 @@ import ("github.com/stretchr/testify/assert")+func Test_GetIncludedSchemaNames_WhenNamesArePastedMultiline_ReturnsTrimmedUniqueNames(t *testing.T) {+ filter := DumpFilter{IncludeSchemas: []string{"\npublic", " reporting ", "public"}}++ assert.Equal(t, []string{"public", "reporting"}, filter.getIncludedSchemaNames())+}++func Test_GetIncludedSchemaNames_WhenNoSchemasAreConfigured_ReturnsEmptySlice(t *testing.T) {+ filter := DumpFilter{}++ assert.Empty(t, filter.getIncludedSchemaNames())+ assert.NotNil(t, filter.getIncludedSchemaNames())+}+func Test_IsRelationExcluded_WithVariousPatterns_MatchesPgDumpSemantics(t *testing.T) {exclusionCases := []struct {name stringdiff --git a/backend/internal/features/databases/databases/postgresql/logical/model.go b/backend/internal/features/databases/databases/postgresql/logical/model.goindex c86e6177..034ccc32 100644--- a/backend/internal/features/databases/databases/postgresql/logical/model.go+++ b/backend/internal/features/databases/databases/postgresql/logical/model.go@@ -12,7 +12,6 @@ import ("github.com/google/uuid""github.com/jackc/pgx/v5"- "github.com/jackc/pgx/v5/pgconn""gorm.io/gorm"postgresql_shared "databasus-backend/internal/features/databases/databases/postgresql/shared"@@ -447,57 +446,6 @@ func (p *PostgresqlLogicalDatabase) ShouldSuggestReadOnlyUser(return shouldSuggestReadOnlyUser, privileges, nil}-// pg_dump runs with row_security = off and aborts rather than emit a partial dump, so a role-// without BYPASSRLS can never back these tables up.-func getRowLevelSecurityTables(- ctx context.Context,- conn *pgx.Conn,- includeSchemas []string,-) ([]string, error) {- query := `- SELECT n.nspname || '.' || c.relname- FROM pg_class c- JOIN pg_namespace n ON n.oid = c.relnamespace- WHERE c.relrowsecurity- AND c.relkind = 'r'- AND n.nspname NOT IN ('pg_catalog', 'information_schema', 'pg_toast')- AND n.nspname NOT LIKE 'pg_temp_%'- AND n.nspname NOT LIKE 'pg_toast_temp_%'- `-- var rows pgx.Rows- var err error-- if len(includeSchemas) > 0 {- rows, err = conn.Query(ctx, query+" AND n.nspname = ANY($1::text[]) ORDER BY 1", includeSchemas)- } else {- rows, err = conn.Query(ctx, query+" ORDER BY 1")- }-- if err != nil {- return nil, fmt.Errorf("failed to check row-level security: %w", err)- }-- defer rows.Close()-- var rowLevelSecurityTables []string-- for rows.Next() {- var table string- if err := rows.Scan(&table); err != nil {- return nil, fmt.Errorf("failed to scan row-level security table: %w", err)- }-- rowLevelSecurityTables = append(rowLevelSecurityTables, table)- }-- if err := rows.Err(); err != nil {- return nil, fmt.Errorf("error iterating row-level security tables: %w", err)- }-- return rowLevelSecurityTables, nil-}-func (p *PostgresqlLogicalDatabase) CreateReadOnlyUser(ctx context.Context,logger *slog.Logger,@@ -536,7 +484,7 @@ func (p *PostgresqlLogicalDatabase) CreateReadOnlyUser(if len(rowLevelSecurityTables) > 0 {return "", "", fmt.Errorf("row-level security is enabled on %s; grant BYPASSRLS to a role you create yourself, or back up with a role that owns these tables",- formatTruncatedTableList(rowLevelSecurityTables),+ namelist.FormatTruncatedNames(rowLevelSecurityTables),)}@@ -957,19 +905,16 @@ func (p *PostgresqlLogicalDatabase) validateSslConfig() error {)}-// testSingleDatabaseConnection tests connection to a specific database for pg_dumpfunc testSingleDatabaseConnection(logger *slog.Logger,ctx context.Context,postgresDb *PostgresqlLogicalDatabase,encryptor encryption.FieldEncryptor,) error {- // For single database backup, we need to connect to the specific databaseif postgresDb.Database == nil || *postgresDb.Database == "" {return errors.New("database name is required for single database backup (pg_dump)")}- // Test connectionconn, err := openPgConn(ctx, postgresDb, *postgresDb.Database, encryptor)if err != nil {// TODO make more readable errors:@@ -984,15 +929,16 @@ func testSingleDatabaseConnection(}}()- // Detect and set the database version automaticallydetectedVersion, err := detectDatabaseVersion(ctx, conn)if err != nil {return err}postgresDb.Version = detectedVersion- // Verify user has sufficient permissions for backup operations- if err := checkBackupPermissions(ctx, conn, postgresDb.IncludeSchemas); err != nil {+ if err := checkDumpReadPrivileges(ctx, conn, DumpFilter{+ IncludeSchemas: postgresDb.IncludeSchemas,+ ExcludeTablePatterns: postgresDb.ExcludeTables,+ }); err != nil {return err}@@ -1005,31 +951,6 @@ func testSingleDatabaseConnection(return nil}-// PostgreSQL masks pg_user_mappings.umoptions (credentials) from any role that is not a superuser,-// the foreign server owner, or the mapping's own user. Such a role's pg_dump emits a bare CREATE-// USER MAPPING that loses the credentials and breaks restore for FDWs that require them (e.g.-// oracle_fdw), so refuse the backup when any mapping's options are hidden.-func checkUserMappingsReadable(ctx context.Context, conn *pgx.Conn) error {- var unreadableCount int- err := conn.QueryRow(ctx, "SELECT count(*) FROM pg_user_mappings WHERE umoptions IS NULL").- Scan(&unreadableCount)- if err != nil {- return fmt.Errorf("cannot check user mapping options: %w", err)- }-- if unreadableCount > 0 {- return fmt.Errorf(- "database has %d user mapping(s) whose options this role cannot read; their "+- "credentials would be lost on restore — back up as a superuser or the foreign "+- "server/mapping owner, or enable 'skip user mappings'",- unreadableCount,- )- }-- return nil-}--// detectDatabaseVersion queries and returns the PostgreSQL major versionfunc detectDatabaseVersion(ctx context.Context, conn *pgx.Conn) (tools.PostgresqlVersion, error) {var versionStr stringerr := conn.QueryRow(ctx, "SELECT version()").Scan(&versionStr)@@ -1056,141 +977,6 @@ func detectDatabaseVersion(ctx context.Context, conn *pgx.Conn) (tools.Postgresq}}-// checkBackupPermissions verifies the user has sufficient privileges for pg_dump backup.-// Required privileges: CONNECT on database, USAGE on schemas, SELECT on tables.-// If includeSchemas is specified, only checks permissions on those schemas.-func checkBackupPermissions(- ctx context.Context,- conn *pgx.Conn,- includeSchemas []string,-) error {- var missingPrivileges []string-- // Check CONNECT privilege on database- var hasConnect bool- err := conn.QueryRow(ctx, "SELECT has_database_privilege(current_user, current_database(), 'CONNECT')").- Scan(&hasConnect)- if err != nil {- return fmt.Errorf("cannot check database privileges: %w", err)- }- if !hasConnect {- missingPrivileges = append(missingPrivileges, "CONNECT on database")- }-- // Check USAGE privilege on at least one non-system schema- var schemaCount int- if len(includeSchemas) > 0 {- // Check only the specified schemas- err = conn.QueryRow(ctx, `- SELECT COUNT(*)- FROM pg_namespace n- WHERE has_schema_privilege(current_user, n.nspname, 'USAGE')- AND n.nspname NOT IN ('pg_catalog', 'information_schema', 'pg_toast')- AND n.nspname NOT LIKE 'pg_temp_%'- AND n.nspname NOT LIKE 'pg_toast_temp_%'- AND n.nspname = ANY($1::text[])- `, includeSchemas).Scan(&schemaCount)- } else {- // Check all non-system schemas- err = conn.QueryRow(ctx, `- SELECT COUNT(*)- FROM pg_namespace n- WHERE has_schema_privilege(current_user, n.nspname, 'USAGE')- AND n.nspname NOT IN ('pg_catalog', 'information_schema', 'pg_toast')- AND n.nspname NOT LIKE 'pg_temp_%'- AND n.nspname NOT LIKE 'pg_toast_temp_%'- `).Scan(&schemaCount)- }-- if err != nil {- return fmt.Errorf("cannot check schema privileges: %w", err)- }- if schemaCount == 0 {- missingPrivileges = append(missingPrivileges, "USAGE on at least one schema")- }-- // Check SELECT privilege on at least one table (if tables exist)- // Use pg_tables from pg_catalog which shows all tables regardless of user privileges- var tableCount int-- if len(includeSchemas) > 0 {- // Check only tables in the specified schemas- err = conn.QueryRow(ctx, `- SELECT COUNT(*)- FROM pg_catalog.pg_tables t- WHERE t.schemaname NOT IN ('pg_catalog', 'information_schema')- AND t.schemaname NOT LIKE 'pg_temp_%'- AND t.schemaname NOT LIKE 'pg_toast_temp_%'- AND t.schemaname = ANY($1::text[])- `, includeSchemas).Scan(&tableCount)- } else {- // Check all tables in non-system schemas- err = conn.QueryRow(ctx, `- SELECT COUNT(*)- FROM pg_catalog.pg_tables t- WHERE t.schemaname NOT IN ('pg_catalog', 'information_schema')- AND t.schemaname NOT LIKE 'pg_temp_%'- AND t.schemaname NOT LIKE 'pg_toast_temp_%'- `).Scan(&tableCount)- }-- if err != nil {- return fmt.Errorf("cannot check table count: %w", err)- }-- if tableCount > 0 {- // Check if user has SELECT on at least one of these tables- var selectableTableCount int-- if len(includeSchemas) > 0 {- // Check only tables in the specified schemas- err = conn.QueryRow(ctx, `- SELECT COUNT(*)- FROM pg_catalog.pg_tables t- WHERE t.schemaname NOT IN ('pg_catalog', 'information_schema')- AND t.schemaname NOT LIKE 'pg_temp_%'- AND t.schemaname NOT LIKE 'pg_toast_temp_%'- AND t.schemaname = ANY($1::text[])- AND has_table_privilege(current_user, quote_ident(t.schemaname) || '.' || quote_ident(t.tablename), 'SELECT')- `, includeSchemas).Scan(&selectableTableCount)- } else {- // Check all tables in non-system schemas- err = conn.QueryRow(ctx, `- SELECT COUNT(*)- FROM pg_catalog.pg_tables t- WHERE t.schemaname NOT IN ('pg_catalog', 'information_schema')- AND t.schemaname NOT LIKE 'pg_temp_%'- AND t.schemaname NOT LIKE 'pg_toast_temp_%'- AND has_table_privilege(current_user, quote_ident(t.schemaname) || '.' || quote_ident(t.tablename), 'SELECT')- `).Scan(&selectableTableCount)- }-- if err != nil {- // If the user doesn't have USAGE on the schema, has_table_privilege will fail- // with "permission denied for schema". This means they definitely don't have- // SELECT privileges, so treat this as missing permissions rather than an error.- var pgErr *pgconn.PgError- if errors.As(err, &pgErr) && pgErr.Code == "42501" { // insufficient_privilege- selectableTableCount = 0- } else {- return fmt.Errorf("cannot check SELECT privileges: %w", err)- }- }- if selectableTableCount == 0 {- missingPrivileges = append(missingPrivileges, "SELECT on tables")- }- }-- if len(missingPrivileges) > 0 {- return fmt.Errorf(- "insufficient permissions for backup. Missing: %s. Required: CONNECT on database, USAGE on schemas, SELECT on tables",- strings.Join(missingPrivileges, ", "),- )- }-- return nil-}-func isSupabaseConnection(host, username string) bool {return strings.Contains(strings.ToLower(host), "supabase") ||strings.Contains(strings.ToLower(username), "supabase")@@ -1227,16 +1013,3 @@ func isLocalhostAddress(host string) bool {// The entire 127.0.0.0/8 loopback range, not just 127.0.0.1return strings.HasPrefix(host, "127.")}--func formatTruncatedTableList(tables []string) string {- const maxListedTables = 5- if len(tables) <= maxListedTables {- return strings.Join(tables, ", ")- }-- return fmt.Sprintf(- "%s and %d more",- strings.Join(tables[:maxListedTables], ", "),- len(tables)-maxListedTables,- )-}diff --git a/backend/internal/features/databases/databases/postgresql/logical/model_test.go b/backend/internal/features/databases/databases/postgresql/logical/model_test.goindex 14dd6bb1..1daa265c 100644--- a/backend/internal/features/databases/databases/postgresql/logical/model_test.go+++ b/backend/internal/features/databases/databases/postgresql/logical/model_test.go@@ -64,6 +64,12 @@ func Test_TestConnection_PasswordContainingSpaces_TestedSuccessfully(t *testing.))assert.NoError(t, err)+ _, err = container.DB.Exec(fmt.Sprintf(+ `GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO "%s"`,+ usernameWithSpaces,+ ))+ assert.NoError(t, err)+defer func() {_, _ = container.DB.Exec(fmt.Sprintf(`DROP USER IF EXISTS "%s"`, usernameWithSpaces))}()@@ -190,6 +196,60 @@ func Test_PostgresqlModel_AcrossSupportedVersions(t *testing.T) {testCreateReadOnlyUserWhenRlsTableInScope(t, endpoint, dbVersion.tag)})+ t.Run(+ "Test_TestConnection_WhenSelectIsMissingOnSomeTables_ReturnsErrorNamingOnlyUnreadableTable",+ func(t *testing.T) {+ testConnectionSelectMissingOnSomeTables(t, endpoint, dbVersion.tag)+ },+ )++ t.Run("Test_TestConnection_WhenEveryTableAndSequenceIsReadable_Success", func(t *testing.T) {+ testConnectionEveryTableAndSequenceReadable(t, endpoint, dbVersion.tag)+ })++ t.Run("Test_TestConnection_WhenSelectIsMissingOnSequence_ReturnsError", func(t *testing.T) {+ testConnectionSelectMissingOnSequence(t, endpoint, dbVersion.tag)+ })++ t.Run("Test_TestConnection_WhenUnreadableTableIsExcluded_Success", func(t *testing.T) {+ testConnectionUnreadableTableExcluded(t, endpoint, dbVersion.tag)+ })++ t.Run(+ "Test_TestConnection_WhenExcludedEntryIsUnnormalized_Success",+ func(t *testing.T) {+ testConnectionUnreadableTableExcludedWithUnnormalizedEntry(+ t,+ endpoint,+ dbVersion.tag,+ )+ },+ )++ t.Run("Test_TestConnection_WhenUnreadableTableIsOutsideIncludedSchemas_Success", func(t *testing.T) {+ testConnectionUnreadableTableOutsideIncludedSchemas(t, endpoint, dbVersion.tag)+ })++ t.Run("Test_TestConnection_WhenSchemaUsageIsMissing_ReturnsError", func(t *testing.T) {+ testConnectionSchemaUsageMissing(t, endpoint, dbVersion.tag)+ })++ t.Run("Test_TestConnection_WhenRoleOwnsEveryTable_Success", func(t *testing.T) {+ testConnectionRoleOwnsEveryTable(t, endpoint, dbVersion.tag)+ })++ t.Run("Test_TestConnection_WhenNoRelationIsInScope_Success", func(t *testing.T) {+ testConnectionNoRelationInScope(t, endpoint, dbVersion.tag)+ })++ t.Run("Test_TestConnection_WhenPartitionIsUnreadable_ReturnsError", func(t *testing.T) {+ testConnectionPartitionUnreadable(t, endpoint, dbVersion.tag)+ })++ t.Run("Test_TestConnection_WhenUnreadableTableBelongsToExtension_Success", func(t *testing.T) {+ testConnectionUnreadableTableBelongsToExtension(t, endpoint, dbVersion.tag)+ })+t.Run("Test_TestConnection_WhenUserMappingUnreadableAndFlagFalse_ReturnsError", func(t *testing.T) {testConnectionUserMappingUnreadableFlagFalse(t, endpoint, dbVersion.tag)})@@ -213,11 +273,14 @@ func setupUnreadableUserMappingModel(limitedUsername := fmt.Sprintf("um_limited_%s", suffix)limitedPassword := "limitedpassword123"serverName := fmt.Sprintf("um_test_srv_%s", suffix)- tableName := fmt.Sprintf("um_test_table_%s", suffix)+ // The version's subtests share one server (ADR-0013), so an own schema keeps the dump scope+ // free of the tables other subtests leave behind in public.+ schemaName := fmt.Sprintf("um_test_schema_%s", suffix)setupStatements := []string{- fmt.Sprintf(`CREATE TABLE %s (id SERIAL PRIMARY KEY, data TEXT NOT NULL)`, tableName),- fmt.Sprintf(`INSERT INTO %s (data) VALUES ('row1')`, tableName),+ fmt.Sprintf(`CREATE SCHEMA %s`, schemaName),+ fmt.Sprintf(`CREATE TABLE %s.um_test_table (id SERIAL PRIMARY KEY, data TEXT NOT NULL)`, schemaName),+ fmt.Sprintf(`INSERT INTO %s.um_test_table (data) VALUES ('row1')`, schemaName),`CREATE EXTENSION IF NOT EXISTS postgres_fdw`,fmt.Sprintf(`CREATE SERVER %s FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'localhost', dbname 'postgres')`,@@ -229,8 +292,9 @@ func setupUnreadableUserMappingModel(),fmt.Sprintf(`CREATE USER "%s" WITH PASSWORD '%s' LOGIN`, limitedUsername, limitedPassword),fmt.Sprintf(`GRANT CONNECT ON DATABASE "%s" TO "%s"`, container.Database, limitedUsername),- fmt.Sprintf(`GRANT USAGE ON SCHEMA public TO "%s"`, limitedUsername),- fmt.Sprintf(`GRANT SELECT ON %s TO "%s"`, tableName, limitedUsername),+ fmt.Sprintf(`GRANT USAGE ON SCHEMA %s TO "%s"`, schemaName, limitedUsername),+ fmt.Sprintf(`GRANT SELECT ON ALL TABLES IN SCHEMA %s TO "%s"`, schemaName, limitedUsername),+ fmt.Sprintf(`GRANT SELECT ON ALL SEQUENCES IN SCHEMA %s TO "%s"`, schemaName, limitedUsername),}for _, statement := range setupStatements {@@ -240,20 +304,21 @@ func setupUnreadableUserMappingModel(t.Cleanup(func() {_, _ = container.DB.Exec(fmt.Sprintf(`DROP SERVER IF EXISTS %s CASCADE`, serverName))- _, _ = container.DB.Exec(fmt.Sprintf(`DROP TABLE IF EXISTS %s CASCADE`, tableName))+ _, _ = container.DB.Exec(fmt.Sprintf(`DROP SCHEMA IF EXISTS %s CASCADE`, schemaName))_, _ = container.DB.Exec(fmt.Sprintf(`DROP OWNED BY "%s"`, limitedUsername))_, _ = container.DB.Exec(fmt.Sprintf(`DROP USER IF EXISTS "%s"`, limitedUsername))})return &PostgresqlLogicalDatabase{- Version: tools.GetPostgresqlVersionEnum(version),- Host: container.Host,- Port: container.Port,- Username: limitedUsername,- Password: limitedPassword,- Database: &container.Database,- SslMode: postgresql_shared.PostgresSslModeDisable,- CpuCount: 1,+ Version: tools.GetPostgresqlVersionEnum(version),+ Host: container.Host,+ Port: container.Port,+ Username: limitedUsername,+ Password: limitedPassword,+ Database: &container.Database,+ SslMode: postgresql_shared.PostgresSslModeDisable,+ IncludeSchemas: []string{schemaName},+ CpuCount: 1,}}@@ -384,6 +449,12 @@ func testConnectionSufficientPermissions(t *testing.T, endpoint containers.Endpo))assert.NoError(t, err)+ _, err = container.DB.Exec(fmt.Sprintf(+ `GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO "%s"`,+ backupUsername,+ ))+ assert.NoError(t, err)+defer func() {_, _ = container.DB.Exec(fmt.Sprintf(`DROP USER IF EXISTS "%s"`, backupUsername))}()diff --git a/backend/internal/features/databases/databases/postgresql/logical/privileges.go b/backend/internal/features/databases/databases/postgresql/logical/privileges.gonew file mode 100644index 00000000..1ef5ddb2--- /dev/null+++ b/backend/internal/features/databases/databases/postgresql/logical/privileges.go@@ -0,0 +1,235 @@+package postgresql_logical++import (+ "context"+ "fmt"+ "strings"++ "github.com/jackc/pgx/v5"++ "databasus-backend/internal/util/namelist"+)++// has_table_privilege is asked by oid rather than by name so it reaches pg_class_aclcheck+// directly: the name form resolves through the schema and raises insufficient_privilege for+// a role that lacks USAGE, which would hide the very gap this looks for. One function covers+// sequences too, unlike has_sequence_privilege, which errors on a non-sequence.+const unreadableRelationsQuery = `+ SELECT n.nspname,+ c.relname,+ NOT COALESCE(has_schema_privilege(current_user, n.oid, 'USAGE'), false)+` + dumpedRelationScope + `+ AND c.relkind IN ('r', 'p', 'S')+ AND NOT (+ COALESCE(has_schema_privilege(current_user, n.oid, 'USAGE'), false)+ AND COALESCE(has_table_privilege(current_user, c.oid, 'SELECT'), false)+ )+ ORDER BY 1, 2+`++type unreadableRelation struct {+ relationName+ IsSchemaUsageMissing bool+}++func checkDumpReadPrivileges(ctx context.Context, conn *pgx.Conn, filter DumpFilter) error {+ unreadableRelations, err := getUnreadableDumpedRelations(ctx, conn, filter)+ if err != nil {+ return err+ }++ if len(unreadableRelations) == 0 {+ return nil+ }++ var schemasMissingUsage []string+ var relationsMissingSelect []string+ seenSchemas := make(map[string]struct{})++ for _, relation := range unreadableRelations {+ if !relation.IsSchemaUsageMissing {+ relationsMissingSelect = append(+ relationsMissingSelect,+ relation.SchemaName+"."+relation.TableName,+ )++ continue+ }++ if _, isSeen := seenSchemas[relation.SchemaName]; isSeen {+ continue+ }++ seenSchemas[relation.SchemaName] = struct{}{}+ schemasMissingUsage = append(schemasMissingUsage, relation.SchemaName)+ }++ return newInsufficientDumpPrivilegesError(ctx, conn, missingDumpPrivileges{+ SchemasMissingUsage: schemasMissingUsage,+ RelationsMissingSelect: relationsMissingSelect,+ })+}++type missingDumpPrivileges struct {+ SchemasMissingUsage []string+ RelationsMissingSelect []string+}++func getUnreadableDumpedRelations(+ ctx context.Context,+ conn *pgx.Conn,+ filter DumpFilter,+) ([]unreadableRelation, error) {+ rows, err := conn.Query(ctx, unreadableRelationsQuery, filter.getIncludedSchemaNames())+ if err != nil {+ return nil, fmt.Errorf("failed to check dump read privileges: %w", err)+ }++ defer rows.Close()++ excludeTablePatterns := filter.getExcludeTablePatterns()++ var unreadableRelations []unreadableRelation++ for rows.Next() {+ var relation unreadableRelation++ if err := rows.Scan(+ &relation.SchemaName,+ &relation.TableName,+ &relation.IsSchemaUsageMissing,+ ); err != nil {+ return nil, fmt.Errorf("failed to scan unreadable relation: %w", err)+ }++ if isRelationExcluded(relation.relationName, excludeTablePatterns) {+ continue+ }++ unreadableRelations = append(unreadableRelations, relation)+ }++ if err := rows.Err(); err != nil {+ return nil, fmt.Errorf("failed to read unreadable relations: %w", err)+ }++ return unreadableRelations, nil+}++func newInsufficientDumpPrivilegesError(+ ctx context.Context,+ conn *pgx.Conn,+ missing missingDumpPrivileges,+) error {+ // A pooled Supabase login carries a "postgres.<project>" username that no GRANT can name,+ // so the role has to come from the server rather than from the stored credentials.+ var roleName string+ if err := conn.QueryRow(ctx, "SELECT current_user").Scan(&roleName); err != nil {+ return fmt.Errorf("failed to read the current role: %w", err)+ }++ messageParts := []string{+ fmt.Sprintf(+ "insufficient permissions for backup: role %q cannot read everything pg_dump would dump.",+ roleName,+ ),+ }++ if len(missing.SchemasMissingUsage) > 0 {+ messageParts = append(messageParts, fmt.Sprintf(+ "Missing USAGE on schemas: %s.",+ namelist.FormatTruncatedNames(missing.SchemasMissingUsage),+ ))+ }++ if len(missing.RelationsMissingSelect) > 0 {+ messageParts = append(messageParts, fmt.Sprintf(+ "Missing SELECT on: %s.",+ namelist.FormatTruncatedNames(missing.RelationsMissingSelect),+ ))+ }++ messageParts = append(messageParts, fmt.Sprintf(+ "Grant USAGE ON SCHEMA, SELECT ON ALL TABLES IN SCHEMA and SELECT ON ALL SEQUENCES IN SCHEMA "++ "to %q for every schema listed above, or narrow the backup with include schemas and exclude tables. "++ "Note that excluding a table does not exclude the sequences it owns.",+ roleName,+ ))++ return fmt.Errorf("%s", strings.Join(messageParts, " "))+}++// PostgreSQL masks pg_user_mappings.umoptions (credentials) from any role that is not a superuser,+// the foreign server owner, or the mapping's own user. Such a role's pg_dump emits a bare CREATE+// USER MAPPING that loses the credentials and breaks restore for FDWs that require them (e.g.+// oracle_fdw), so refuse the backup when any mapping's options are hidden.+func checkUserMappingsReadable(ctx context.Context, conn *pgx.Conn) error {+ var unreadableCount int+ err := conn.QueryRow(ctx, "SELECT count(*) FROM pg_user_mappings WHERE umoptions IS NULL").+ Scan(&unreadableCount)+ if err != nil {+ return fmt.Errorf("cannot check user mapping options: %w", err)+ }++ if unreadableCount > 0 {+ return fmt.Errorf(+ "database has %d user mapping(s) whose options this role cannot read; their "++ "credentials would be lost on restore — back up as a superuser or the foreign "++ "server/mapping owner, or enable 'skip user mappings'",+ unreadableCount,+ )+ }++ return nil+}++// pg_dump runs with row_security = off and aborts rather than emit a partial dump, so a role+// without BYPASSRLS can never back these tables up.+func getRowLevelSecurityTables(+ ctx context.Context,+ conn *pgx.Conn,+ includeSchemas []string,+) ([]string, error) {+ query := `+ SELECT n.nspname || '.' || c.relname+ FROM pg_class c+ JOIN pg_namespace n ON n.oid = c.relnamespace+ WHERE c.relrowsecurity+ AND c.relkind = 'r'+ AND n.nspname NOT IN ('pg_catalog', 'information_schema', 'pg_toast')+ AND n.nspname NOT LIKE 'pg_temp_%'+ AND n.nspname NOT LIKE 'pg_toast_temp_%'+ `++ var rows pgx.Rows+ var err error++ if len(includeSchemas) > 0 {+ rows, err = conn.Query(ctx, query+" AND n.nspname = ANY($1::text[]) ORDER BY 1", includeSchemas)+ } else {+ rows, err = conn.Query(ctx, query+" ORDER BY 1")+ }++ if err != nil {+ return nil, fmt.Errorf("failed to check row-level security: %w", err)+ }++ defer rows.Close()++ var rowLevelSecurityTables []string++ for rows.Next() {+ var table string+ if err := rows.Scan(&table); err != nil {+ return nil, fmt.Errorf("failed to scan row-level security table: %w", err)+ }++ rowLevelSecurityTables = append(rowLevelSecurityTables, table)+ }++ if err := rows.Err(); err != nil {+ return nil, fmt.Errorf("error iterating row-level security tables: %w", err)+ }++ return rowLevelSecurityTables, nil+}diff --git a/backend/internal/features/databases/databases/postgresql/logical/privileges_test.go b/backend/internal/features/databases/databases/postgresql/logical/privileges_test.gonew file mode 100644index 00000000..5dfec162--- /dev/null+++ b/backend/internal/features/databases/databases/postgresql/logical/privileges_test.go@@ -0,0 +1,328 @@+package postgresql_logical++import (+ "fmt"+ "log/slog"+ "os"+ "testing"++ "github.com/google/uuid"+ "github.com/stretchr/testify/assert"+ "github.com/stretchr/testify/require"++ postgresql_shared "databasus-backend/internal/features/databases/databases/postgresql/shared"+ "databasus-backend/internal/util/testing/containers"+ "databasus-backend/internal/util/tools"+)++// The version's subtests share one server (ADR-0013), so every case works in a schema of its own+// and scopes the model to it. Without that, tables another subtest left in public would join the+// dump scope and decide the outcome.+type backupPrivilegeFixture struct {+ Container *PostgresContainer+ SchemaName string+ RoleName string+ Model *PostgresqlLogicalDatabase+}++func createBackupPrivilegeFixture(+ t *testing.T,+ endpoint containers.Endpoint,+ version string,+ schemaPrefix string,+) backupPrivilegeFixture {+ t.Helper()++ container := connectToPostgresEndpoint(t, endpoint)+ t.Cleanup(func() { container.DB.Close() })++ suffix := uuid.New().String()[:8]+ schemaName := fmt.Sprintf("%s_%s", schemaPrefix, suffix)+ roleName := fmt.Sprintf("backup_role_%s", suffix)+ rolePassword := "backuprolepassword123"++ for _, statement := range []string{+ fmt.Sprintf(`CREATE SCHEMA %s`, schemaName),+ fmt.Sprintf(`CREATE USER "%s" WITH PASSWORD '%s' LOGIN`, roleName, rolePassword),+ fmt.Sprintf(`GRANT CONNECT ON DATABASE "%s" TO "%s"`, container.Database, roleName),+ } {+ _, err := container.DB.Exec(statement)+ require.NoError(t, err)+ }++ t.Cleanup(func() {+ _, _ = container.DB.Exec(fmt.Sprintf(`DROP SCHEMA IF EXISTS %s CASCADE`, schemaName))+ _, _ = container.DB.Exec(fmt.Sprintf(`DROP OWNED BY "%s"`, roleName))+ _, _ = container.DB.Exec(fmt.Sprintf(`DROP USER IF EXISTS "%s"`, roleName))+ })++ return backupPrivilegeFixture{+ Container: container,+ SchemaName: schemaName,+ RoleName: roleName,+ Model: &PostgresqlLogicalDatabase{+ Version: tools.GetPostgresqlVersionEnum(version),+ Host: container.Host,+ Port: container.Port,+ Username: roleName,+ Password: rolePassword,+ Database: &container.Database,+ SslMode: postgresql_shared.PostgresSslModeDisable,+ IncludeSchemas: []string{schemaName},+ CpuCount: 1,+ },+ }+}++func (f backupPrivilegeFixture) executeStatements(t *testing.T, statements ...string) {+ t.Helper()++ for _, statement := range statements {+ _, err := f.Container.DB.Exec(statement)+ require.NoError(t, err)+ }+}++func (f backupPrivilegeFixture) testConnection() error {+ return f.Model.TestConnection(slog.New(slog.NewTextHandler(os.Stdout, nil)), nil)+}++// Pins issue #749: a role holding SELECT on part of the dump scope used to pass the connection+// test and only fail once pg_dump reached the first unreadable table.+func testConnectionSelectMissingOnSomeTables(+ t *testing.T,+ endpoint containers.Endpoint,+ version string,+) {+ fixture := createBackupPrivilegeFixture(t, endpoint, version, "perm_partial")++ fixture.executeStatements(t,+ fmt.Sprintf(`CREATE TABLE %s.granted_orders (id INT)`, fixture.SchemaName),+ fmt.Sprintf(`CREATE TABLE %s.ungranted_payments (id INT)`, fixture.SchemaName),+ fmt.Sprintf(`GRANT USAGE ON SCHEMA %s TO "%s"`, fixture.SchemaName, fixture.RoleName),+ fmt.Sprintf(+ `GRANT SELECT ON %s.granted_orders TO "%s"`,+ fixture.SchemaName,+ fixture.RoleName,+ ),+ )++ err := fixture.testConnection()++ require.Error(t, err)+ assert.Contains(t, err.Error(), "insufficient permissions")+ assert.Contains(t, err.Error(), fixture.SchemaName+".ungranted_payments")+ assert.NotContains(t, err.Error(), fixture.SchemaName+".granted_orders")+}++func testConnectionEveryTableAndSequenceReadable(+ t *testing.T,+ endpoint containers.Endpoint,+ version string,+) {+ fixture := createBackupPrivilegeFixture(t, endpoint, version, "perm_full")++ fixture.executeStatements(t,+ fmt.Sprintf(`CREATE TABLE %s.orders (id SERIAL PRIMARY KEY)`, fixture.SchemaName),+ fmt.Sprintf(`CREATE SEQUENCE %s.standalone_counter`, fixture.SchemaName),+ fmt.Sprintf(`GRANT USAGE ON SCHEMA %s TO "%s"`, fixture.SchemaName, fixture.RoleName),+ fmt.Sprintf(+ `GRANT SELECT ON ALL TABLES IN SCHEMA %s TO "%s"`,+ fixture.SchemaName,+ fixture.RoleName,+ ),+ fmt.Sprintf(+ `GRANT SELECT ON ALL SEQUENCES IN SCHEMA %s TO "%s"`,+ fixture.SchemaName,+ fixture.RoleName,+ ),+ )++ assert.NoError(t, fixture.testConnection())+}++func testConnectionSelectMissingOnSequence(+ t *testing.T,+ endpoint containers.Endpoint,+ version string,+) {+ fixture := createBackupPrivilegeFixture(t, endpoint, version, "perm_sequence")++ fixture.executeStatements(t,+ fmt.Sprintf(`CREATE TABLE %s.orders (id SERIAL PRIMARY KEY)`, fixture.SchemaName),+ fmt.Sprintf(`GRANT USAGE ON SCHEMA %s TO "%s"`, fixture.SchemaName, fixture.RoleName),+ fmt.Sprintf(+ `GRANT SELECT ON ALL TABLES IN SCHEMA %s TO "%s"`,+ fixture.SchemaName,+ fixture.RoleName,+ ),+ )++ err := fixture.testConnection()++ require.Error(t, err)+ assert.Contains(t, err.Error(), fixture.SchemaName+".orders_id_seq")+}++func testConnectionUnreadableTableExcluded(+ t *testing.T,+ endpoint containers.Endpoint,+ version string,+) {+ fixture := createBackupPrivilegeFixture(t, endpoint, version, "perm_excluded")++ fixture.executeStatements(t,+ fmt.Sprintf(`CREATE TABLE %s.kept (id INT)`, fixture.SchemaName),+ fmt.Sprintf(`CREATE TABLE %s.skipped_audit (id INT)`, fixture.SchemaName),+ fmt.Sprintf(`GRANT USAGE ON SCHEMA %s TO "%s"`, fixture.SchemaName, fixture.RoleName),+ fmt.Sprintf(`GRANT SELECT ON %s.kept TO "%s"`, fixture.SchemaName, fixture.RoleName),+ )++ fixture.Model.ExcludeTables = []string{fixture.SchemaName + ".skipped_audit"}++ assert.NoError(t, fixture.testConnection())+}++// pg_dump receives the exclude list normalized, so an entry pasted with a leading newline still+// excludes the table and must not be re-demanded by the privilege check.+func testConnectionUnreadableTableExcludedWithUnnormalizedEntry(+ t *testing.T,+ endpoint containers.Endpoint,+ version string,+) {+ fixture := createBackupPrivilegeFixture(t, endpoint, version, "perm_dirty_excl")++ fixture.executeStatements(t,+ fmt.Sprintf(`CREATE TABLE %s.kept (id INT)`, fixture.SchemaName),+ fmt.Sprintf(`CREATE TABLE %s.skipped_audit (id INT)`, fixture.SchemaName),+ fmt.Sprintf(`GRANT USAGE ON SCHEMA %s TO "%s"`, fixture.SchemaName, fixture.RoleName),+ fmt.Sprintf(`GRANT SELECT ON %s.kept TO "%s"`, fixture.SchemaName, fixture.RoleName),+ )++ fixture.Model.ExcludeTables = []string{"\n" + fixture.SchemaName + ".skipped_audit"}++ assert.NoError(t, fixture.testConnection())+}++func testConnectionUnreadableTableOutsideIncludedSchemas(+ t *testing.T,+ endpoint containers.Endpoint,+ version string,+) {+ fixture := createBackupPrivilegeFixture(t, endpoint, version, "perm_scoped_in")+ unscopedSchemaName := fixture.SchemaName + "_unscoped"++ fixture.executeStatements(t,+ fmt.Sprintf(`CREATE SCHEMA %s`, unscopedSchemaName),+ fmt.Sprintf(`CREATE TABLE %s.unreadable (id INT)`, unscopedSchemaName),+ fmt.Sprintf(`CREATE TABLE %s.readable (id INT)`, fixture.SchemaName),+ fmt.Sprintf(`GRANT USAGE ON SCHEMA %s TO "%s"`, fixture.SchemaName, fixture.RoleName),+ fmt.Sprintf(`GRANT SELECT ON %s.readable TO "%s"`, fixture.SchemaName, fixture.RoleName),+ )++ t.Cleanup(func() {+ _, _ = fixture.Container.DB.Exec(+ fmt.Sprintf(`DROP SCHEMA IF EXISTS %s CASCADE`, unscopedSchemaName),+ )+ })++ assert.NoError(t, fixture.testConnection())+}++func testConnectionSchemaUsageMissing(t *testing.T, endpoint containers.Endpoint, version string) {+ fixture := createBackupPrivilegeFixture(t, endpoint, version, "perm_no_usage")++ fixture.executeStatements(t,+ fmt.Sprintf(`CREATE TABLE %s.orders (id INT)`, fixture.SchemaName),+ fmt.Sprintf(`GRANT SELECT ON %s.orders TO "%s"`, fixture.SchemaName, fixture.RoleName),+ )++ err := fixture.testConnection()++ require.Error(t, err)+ assert.Contains(t, err.Error(), "USAGE")+ assert.Contains(t, err.Error(), fixture.SchemaName)+}++func testConnectionRoleOwnsEveryTable(t *testing.T, endpoint containers.Endpoint, version string) {+ fixture := createBackupPrivilegeFixture(t, endpoint, version, "perm_owned")++ fixture.executeStatements(t,+ fmt.Sprintf(+ `ALTER SCHEMA %s OWNER TO "%s"`,+ fixture.SchemaName,+ fixture.RoleName,+ ),+ fmt.Sprintf(`CREATE TABLE %s.orders (id SERIAL PRIMARY KEY)`, fixture.SchemaName),+ fmt.Sprintf(+ `ALTER TABLE %s.orders OWNER TO "%s"`,+ fixture.SchemaName,+ fixture.RoleName,+ ),+ fmt.Sprintf(+ `ALTER SEQUENCE %s.orders_id_seq OWNER TO "%s"`,+ fixture.SchemaName,+ fixture.RoleName,+ ),+ )++ assert.NoError(t, fixture.testConnection())+}++func testConnectionNoRelationInScope(t *testing.T, endpoint containers.Endpoint, version string) {+ fixture := createBackupPrivilegeFixture(t, endpoint, version, "perm_empty")++ assert.NoError(t, fixture.testConnection())+}++func testConnectionPartitionUnreadable(t *testing.T, endpoint containers.Endpoint, version string) {+ fixture := createBackupPrivilegeFixture(t, endpoint, version, "perm_partitioned")++ fixture.executeStatements(t,+ fmt.Sprintf(+ `CREATE TABLE %s.events (id INT) PARTITION BY RANGE (id)`,+ fixture.SchemaName,+ ),+ fmt.Sprintf(+ `CREATE TABLE %s.events_2026 PARTITION OF %s.events FOR VALUES FROM (0) TO (1000)`,+ fixture.SchemaName,+ fixture.SchemaName,+ ),+ fmt.Sprintf(`GRANT USAGE ON SCHEMA %s TO "%s"`, fixture.SchemaName, fixture.RoleName),+ fmt.Sprintf(`GRANT SELECT ON %s.events TO "%s"`, fixture.SchemaName, fixture.RoleName),+ )++ err := fixture.testConnection()++ require.Error(t, err)+ assert.Contains(t, err.Error(), fixture.SchemaName+".events_2026")+}++// pg_dump emits no data for an extension member, so requiring SELECT on one would reject+// databases that back up fine today (pg_cron, pg_partman, PostGIS topology).+func testConnectionUnreadableTableBelongsToExtension(+ t *testing.T,+ endpoint containers.Endpoint,+ version string,+) {+ fixture := createBackupPrivilegeFixture(t, endpoint, version, "perm_extension")++ fixture.executeStatements(t,+ `CREATE EXTENSION IF NOT EXISTS hstore`,+ fmt.Sprintf(`CREATE TABLE %s.extension_owned (id INT)`, fixture.SchemaName),+ fmt.Sprintf(+ `ALTER EXTENSION hstore ADD TABLE %s.extension_owned`,+ fixture.SchemaName,+ ),+ fmt.Sprintf(`GRANT USAGE ON SCHEMA %s TO "%s"`, fixture.SchemaName, fixture.RoleName),+ )++ t.Cleanup(func() {+ _, _ = fixture.Container.DB.Exec(fmt.Sprintf(+ `ALTER EXTENSION hstore DROP TABLE %s.extension_owned`,+ fixture.SchemaName,+ ))+ })++ assert.NoError(t, fixture.testConnection())+}diff --git a/backend/internal/features/databases/databases/postgresql/logical/size.go b/backend/internal/features/databases/databases/postgresql/logical/size.goindex c82fd58f..6ddd22d7 100644--- a/backend/internal/features/databases/databases/postgresql/logical/size.go+++ b/backend/internal/features/databases/databases/postgresql/logical/size.go@@ -3,8 +3,6 @@ package postgresql_logicalimport ("context""fmt"- "path"- "strings""github.com/jackc/pgx/v5")@@ -13,42 +11,24 @@ import (// not their contents, so counting them would overstate what a restore recreates.const dumpedRelationsSizeQuery = `SELECT n.nspname, c.relname, COALESCE(pg_total_relation_size(c.oid), 0)- FROM pg_class c- JOIN pg_namespace n ON n.oid = c.relnamespace- WHERE c.relkind IN ('r', 'p')- AND n.nspname NOT IN ('pg_catalog', 'information_schema')- AND n.nspname NOT LIKE 'pg\_toast%'- AND n.nspname NOT LIKE 'pg\_temp%'- AND (cardinality($1::text[]) = 0 OR n.nspname = ANY($1::text[]))+` + dumpedRelationScope + `+ AND c.relkind IN ('r', 'p')`-type DumpFilter struct {- IncludeSchemas []string- ExcludeTablePatterns []string-}--type relationName struct {- SchemaName string- TableName string-}-func getDumpedRelationsSizeBytes(ctx context.Context,conn *pgx.Conn,filter DumpFilter,) (int64, error) {- includeSchemas := filter.IncludeSchemas- if includeSchemas == nil {- includeSchemas = []string{}- }-- rows, err := conn.Query(ctx, dumpedRelationsSizeQuery, includeSchemas)+ rows, err := conn.Query(ctx, dumpedRelationsSizeQuery, filter.getIncludedSchemaNames())if err != nil {return 0, fmt.Errorf("failed to query dumped relations size: %w", err)}defer rows.Close()+ excludeTablePatterns := filter.getExcludeTablePatterns()+var totalSizeBytes int64for rows.Next() {@@ -59,7 +39,7 @@ func getDumpedRelationsSizeBytes(return 0, fmt.Errorf("failed to scan dumped relation size: %w", err)}- if isRelationExcluded(relation, filter.ExcludeTablePatterns) {+ if isRelationExcluded(relation, excludeTablePatterns) {continue}@@ -72,80 +52,3 @@ func getDumpedRelationsSizeBytes(return totalSizeBytes, nil}--func isRelationExcluded(relation relationName, excludeTablePatterns []string) bool {- for _, excludeTablePattern := range excludeTablePatterns {- if parseDumpTablePattern(excludeTablePattern).isMatchingRelation(relation) {- return true- }- }-- return false-}--type dumpTablePattern struct {- SchemaGlob globPattern- TableGlob globPattern- IsSchemaQualified bool-}--func (p dumpTablePattern) isMatchingRelation(relation relationName) bool {- if p.IsSchemaQualified && !p.SchemaGlob.isMatchingName(relation.SchemaName) {- return false- }-- return p.TableGlob.isMatchingName(relation.TableName)-}--// Mirrors pg_dump's --exclude-table: an unqualified pattern hits the table name in-// every schema, and the split happens on the first dot outside double quotes.-func parseDumpTablePattern(excludeTablePattern string) dumpTablePattern {- isInsideQuotes := false-- for index, character := range excludeTablePattern {- switch character {- case '"':- isInsideQuotes = !isInsideQuotes- case '.':- if !isInsideQuotes {- return dumpTablePattern{- SchemaGlob: parseGlobPattern(excludeTablePattern[:index]),- TableGlob: parseGlobPattern(excludeTablePattern[index+1:]),- IsSchemaQualified: true,- }- }- }- }-- return dumpTablePattern{TableGlob: parseGlobPattern(excludeTablePattern)}-}--type globPattern struct {- Value string- IsLiteral bool-}--func parseGlobPattern(patternPart string) globPattern {- if len(patternPart) >= 2 &&- strings.HasPrefix(patternPart, `"`) &&- strings.HasSuffix(patternPart, `"`) {- unquoted := strings.ReplaceAll(patternPart[1:len(patternPart)-1], `""`, `"`)-- return globPattern{Value: unquoted, IsLiteral: true}- }-- return globPattern{Value: patternPart}-}--func (g globPattern) isMatchingName(name string) bool {- if g.IsLiteral {- return g.Value == name- }-- isMatching, err := path.Match(g.Value, name)- if err != nil {- return g.Value == name- }-- return isMatching-}diff --git a/backend/internal/features/tests/logical/postgresql/backup_restore_test.go b/backend/internal/features/tests/logical/postgresql/backup_restore_test.goindex b6a47b9b..8617c1c1 100644--- a/backend/internal/features/tests/logical/postgresql/backup_restore_test.go+++ b/backend/internal/features/tests/logical/postgresql/backup_restore_test.go@@ -577,7 +577,10 @@ func testBackupRestoreSkipUserMappingsForVersion(t *testing.T, endpoint containefmt.Sprintf(`CREATE USER "%s" WITH PASSWORD '%s' LOGIN`, limitedUsername, limitedPassword),fmt.Sprintf(`GRANT CONNECT ON DATABASE "%s" TO "%s"`, container.Database, limitedUsername),fmt.Sprintf(`GRANT USAGE ON SCHEMA public TO "%s"`, limitedUsername),- `GRANT SELECT ON skip_um_data TO "` + limitedUsername + `"`,+ // The backup covers the whole database, and the version's subtests share this server+ // (ADR-0013), so the role needs every relation in public, not just this test's table.+ fmt.Sprintf(`GRANT SELECT ON ALL TABLES IN SCHEMA public TO "%s"`, limitedUsername),+ fmt.Sprintf(`GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO "%s"`, limitedUsername),}for _, statement := range setupStatements {@@ -1196,9 +1199,9 @@ func testBackupFailsWhenUserCannotReadSequencesForVersion(CREATE ROLE %[1]s LOGIN PASSWORD '%[2]s';GRANT CONNECT ON DATABASE %[3]s TO %[1]s;GRANT USAGE ON SCHEMA public TO %[1]s;- GRANT SELECT ON TABLE %[4]s TO %[1]s;- REVOKE ALL ON SEQUENCE %[5]s FROM PUBLIC, %[1]s;- `, restrictedUsername, restrictedPassword, container.Database, tableName, sequenceName))+ GRANT SELECT ON ALL TABLES IN SCHEMA public TO %[1]s;+ GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO %[1]s;+ `, restrictedUsername, restrictedPassword, container.Database))assert.NoError(t, err)defer func() {@@ -1229,6 +1232,15 @@ func testBackupFailsWhenUserCannotReadSequencesForVersion(user.Token,)+ // The sequence is revoked only after the database is saved, because privileges drift on a+ // live server and a backup must fail loudly when they do, rather than emit a partial dump.+ _, err = container.DB.Exec(fmt.Sprintf(+ `REVOKE ALL ON SEQUENCE %s FROM PUBLIC, %s`,+ sequenceName,+ restrictedUsername,+ ))+ assert.NoError(t, err)+logicaltesting.EnableBackupsViaAPI(t, router, restrictedDatabase.ID, storage.ID,backups_core_enums.BackupEncryptionNone, user.Token,diff --git a/backend/internal/util/namelist/namelist.go b/backend/internal/util/namelist/namelist.goindex d6a2a957..712ae12c 100644--- a/backend/internal/util/namelist/namelist.go+++ b/backend/internal/util/namelist/namelist.go@@ -1,13 +1,14 @@-// Package namelist normalizes user-supplied lists of database object names.+// Package namelist normalizes and formats user-supplied lists of database object names.//// The UI collects these lists with an AntD tags input that splits pasted text on its// token separators without trimming, so a list pasted across several lines reaches us// with leading newlines glued to every entry but the first. Such an entry silently// matches nothing in mysqldump / pg_dump / mongodump, so the object it names ends up in-// the backup anyway (issue #690).+// the backup anyway.package namelistimport (+ "fmt""strings")@@ -37,6 +38,22 @@ func FormatUniqueNames(names []string) string {return strings.Join(NormalizeUniqueNames(names), ",")}+// FormatTruncatedNames keeps an error message readable when the caller found hundreds of offending+// objects: the reader needs a few examples and the scale, not the whole list.+func FormatTruncatedNames(names []string) string {+ const maxListedNames = 5++ if len(names) <= maxListedNames {+ return strings.Join(names, ", ")+ }++ return fmt.Sprintf(+ "%s and %d more",+ strings.Join(names[:maxListedNames], ", "),+ len(names)-maxListedNames,+ )+}+func splitOnSeparators(rawNames string) []string {splitNames := make([]string, 0, 1)diff --git a/backend/internal/util/namelist/namelist_test.go b/backend/internal/util/namelist/namelist_test.goindex ff00bf7e..e6b18bd7 100644--- a/backend/internal/util/namelist/namelist_test.go+++ b/backend/internal/util/namelist/namelist_test.go@@ -37,3 +37,14 @@ func Test_FormatUniqueNames_WithDirtyNames_JoinsNormalizedNamesWithCommas(t *tesassert.Equal(t, "orders,users", FormatUniqueNames([]string{" orders ", "\nusers", "orders", ""}))assert.Equal(t, "", FormatUniqueNames(nil))}++func Test_FormatTruncatedNames_WhenNamesFitTheLimit_ListsThemAll(t *testing.T) {+ assert.Equal(t, "orders, users", FormatTruncatedNames([]string{"orders", "users"}))+ assert.Equal(t, "", FormatTruncatedNames(nil))+}++func Test_FormatTruncatedNames_WhenNamesExceedTheLimit_AppendsTheRemainingCount(t *testing.T) {+ manyNames := []string{"a", "b", "c", "d", "e", "f", "g"}++ assert.Equal(t, "a, b, c, d, e and 2 more", FormatTruncatedNames(manyNames))+}