Skip to content

PostgreSQL backup tool with Point-In-Time-Recovery and restore verification

FEATURE (databases): Grant WAL switch privilege to replication users

Rostislav Duginf05e2b8400e1
diff --git a/backend/internal/features/databases/controller.go b/backend/internal/features/databases/controller.go
index 6571b1be..66b81fcc 100644
--- a/backend/internal/features/databases/controller.go
+++ b/backend/internal/features/databases/controller.go
@@ -215,11 +215,12 @@ func (c *DatabaseController) GetDatabases(ctx *gin.Context) {
// TestDatabaseConnection
// @Summary Test database connection
-// @Description Test connection to an existing database configuration
+// @Description Test connection to an existing database configuration. A physical PostgreSQL source is refused with a machine-readable code, including no_wal_switch_privilege when the backup type is FULL_INCREMENTAL_WAL_STREAM and the credentials cannot execute pg_switch_wal().
// @Tags databases
+// @Produce json
// @Param id path string true "Database ID"
// @Success 200
-// @Failure 400
+// @Failure 400 {object} map[string]string
// @Failure 401
// @Failure 500
// @Router /databases/{id}/test-connection [post]
@@ -246,12 +247,13 @@ func (c *DatabaseController) TestDatabaseConnection(ctx *gin.Context) {
// TestDatabaseConnectionDirect
// @Summary Test database connection directly
-// @Description Test connection to a database configuration without saving it
+// @Description Test connection to a database configuration without saving it. A physical PostgreSQL source is refused with a machine-readable code, including no_wal_switch_privilege when the backup type is FULL_INCREMENTAL_WAL_STREAM and the credentials cannot execute pg_switch_wal().
// @Tags databases
// @Accept json
+// @Produce json
// @Param request body Database true "Database configuration to test"
// @Success 200
-// @Failure 400
+// @Failure 400 {object} map[string]string
// @Failure 401
// @Router /databases/test-connection-direct [post]
func (c *DatabaseController) TestDatabaseConnectionDirect(ctx *gin.Context) {
@@ -469,13 +471,13 @@ func (c *DatabaseController) CreateReadOnlyUser(ctx *gin.Context) {
// CreateReplicationOnlyUser
// @Summary Create replication-only database user (PostgreSQL physical only)
-// @Description Provision a fresh PostgreSQL role with LOGIN + REPLICATION (or its cloud equivalent on RDS / Azure / GCP) and nothing more. Refuses for database types other than POSTGRES_PHYSICAL.
+// @Description Provision a fresh PostgreSQL role with LOGIN + REPLICATION (or its cloud equivalent on RDS / Azure / GCP) plus EXECUTE on pg_switch_wal() where the source allows it, and nothing more. isForcedWalRotationAvailable reports whether the created role can force a WAL segment switch, which the FULL_INCREMENTAL_WAL_STREAM backup type requires. Refuses for database types other than POSTGRES_PHYSICAL.
// @Tags databases
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param request body Database true "Database configuration (must be POSTGRES_PHYSICAL)"
-// @Success 200 {object} CreateReadOnlyUserResponse
+// @Success 200 {object} CreateReplicationOnlyUserResponse
// @Failure 400 {object} map[string]string
// @Failure 401 {object} map[string]string
// @Failure 403 {object} map[string]string
@@ -493,14 +495,15 @@ func (c *DatabaseController) CreateReplicationOnlyUser(ctx *gin.Context) {
return
}
- username, password, err := c.databaseService.CreateReplicationOnlyUser(ctx.Request.Context(), user, &request)
+ createdUser, err := c.databaseService.CreateReplicationOnlyUser(ctx.Request.Context(), user, &request)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
- ctx.JSON(http.StatusOK, CreateReadOnlyUserResponse{
- Username: username,
- Password: password,
+ ctx.JSON(http.StatusOK, CreateReplicationOnlyUserResponse{
+ Username: createdUser.Username,
+ Password: createdUser.Password,
+ IsForcedWalRotationAvailable: createdUser.IsForcedWalRotationAvailable,
})
}
diff --git a/backend/internal/features/databases/controller_test.go b/backend/internal/features/databases/controller_test.go
index 396edabc..06acf508 100644
--- a/backend/internal/features/databases/controller_test.go
+++ b/backend/internal/features/databases/controller_test.go
@@ -15,6 +15,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/google/uuid"
+ "github.com/jackc/pgx/v5"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -2068,3 +2069,247 @@ func Test_CreateDatabase_WhenThePhysicalClusterIsBehindABastion_PassesTheReplica
"the cluster identity must be carried back too, or the manifest records a zero identifier")
assert.NotNil(t, response.PostgresqlPhysical.WalSegmentSizeBytes)
}
+
+func openFixtureConn(t *testing.T, fixture physicalFixture) *pgx.Conn {
+ t.Helper()
+
+ dsn := fmt.Sprintf(
+ "host=%s port=%s user=testuser password=testpassword dbname=postgres sslmode=disable",
+ config.GetEnv().TestLocalhost, fixture.port(),
+ )
+
+ conn, err := pgx.Connect(t.Context(), dsn)
+ require.NoError(t, err)
+
+ t.Cleanup(func() { _ = conn.Close(context.Background()) })
+
+ return conn
+}
+
+func createReplicationRole(t *testing.T, conn *pgx.Conn, canForceWalRotation bool) (string, string) {
+ t.Helper()
+
+ username := "databasus-ct-" + uuid.New().String()[:8]
+ password := "Temp1234!Pwd"
+
+ _, err := conn.Exec(t.Context(),
+ fmt.Sprintf(`CREATE USER "%s" WITH PASSWORD '%s' LOGIN REPLICATION`, username, password))
+ require.NoError(t, err)
+
+ t.Cleanup(func() {
+ _, _ = conn.Exec(context.Background(), fmt.Sprintf(`DROP USER IF EXISTS "%s"`, username))
+ })
+
+ if canForceWalRotation {
+ _, err := conn.Exec(t.Context(),
+ fmt.Sprintf(`GRANT EXECUTE ON FUNCTION pg_switch_wal() TO "%s"`, username))
+ require.NoError(t, err)
+ }
+
+ return username, password
+}
+
+type physicalDatabaseRequestSpec struct {
+ workspaceID uuid.UUID
+ fixture physicalFixture
+ username string
+ password string
+ backupType postgresql_physical.BackupType
+}
+
+func buildPhysicalDatabaseRequestFor(t *testing.T, spec physicalDatabaseRequestSpec) Database {
+ t.Helper()
+
+ request := buildPhysicalDatabaseRequest(t, spec.workspaceID, spec.fixture)
+ request.PostgresqlPhysical.Username = spec.username
+ request.PostgresqlPhysical.Password = spec.password
+ request.PostgresqlPhysical.BackupType = spec.backupType
+
+ return request
+}
+
+func getConnectionErrorCode(t *testing.T, body []byte) postgresql_shared.ConnectionErrorCode {
+ t.Helper()
+
+ var payload struct {
+ Code postgresql_shared.ConnectionErrorCode `json:"code"`
+ }
+ require.NoError(t, json.Unmarshal(body, &payload))
+
+ return payload.Code
+}
+
+func Test_TestDatabaseConnectionDirect_ForWalStreamWhenRoleCannotSwitchWal_ReturnsNoWalSwitchPrivilegeCode(
+ t *testing.T,
+) {
+ for _, fixture := range physicalFixtures() {
+ t.Run(fixture.name, func(t *testing.T) {
+ router := createTestRouter()
+ owner := users_testing.CreateTestUser(t.Context(), users_enums.UserRoleMember)
+ workspace := workspaces_testing.CreateTestWorkspace(t.Context(), "Test Workspace", owner, router)
+ defer workspaces_testing.RemoveTestWorkspace(t.Context(), workspace, router)
+
+ username, password := createReplicationRole(t, openFixtureConn(t, fixture), false)
+
+ request := buildPhysicalDatabaseRequestFor(t, physicalDatabaseRequestSpec{
+ workspaceID: workspace.ID,
+ fixture: fixture,
+ username: username,
+ password: password,
+ backupType: postgresql_physical.BackupTypeFullIncrementalAndWalStream,
+ })
+
+ connectionTestResponse := test_utils.MakePostRequest(
+ t, router,
+ "/api/v1/databases/test-connection-direct",
+ "Bearer "+owner.Token,
+ request,
+ http.StatusBadRequest,
+ )
+
+ assert.Equal(t,
+ postgresql_shared.ConnErrNoWalSwitchPrivilege,
+ getConnectionErrorCode(t, connectionTestResponse.Body))
+ })
+ }
+}
+
+func Test_TestDatabaseConnectionDirect_ForWalStreamWhenRoleCanSwitchWal_ReturnsSuccess(t *testing.T) {
+ for _, fixture := range physicalFixtures() {
+ t.Run(fixture.name, func(t *testing.T) {
+ router := createTestRouter()
+ owner := users_testing.CreateTestUser(t.Context(), users_enums.UserRoleMember)
+ workspace := workspaces_testing.CreateTestWorkspace(t.Context(), "Test Workspace", owner, router)
+ defer workspaces_testing.RemoveTestWorkspace(t.Context(), workspace, router)
+
+ username, password := createReplicationRole(t, openFixtureConn(t, fixture), true)
+
+ request := buildPhysicalDatabaseRequestFor(t, physicalDatabaseRequestSpec{
+ workspaceID: workspace.ID,
+ fixture: fixture,
+ username: username,
+ password: password,
+ backupType: postgresql_physical.BackupTypeFullIncrementalAndWalStream,
+ })
+
+ test_utils.MakePostRequest(
+ t, router,
+ "/api/v1/databases/test-connection-direct",
+ "Bearer "+owner.Token,
+ request,
+ http.StatusOK,
+ )
+ })
+ }
+}
+
+// A saved streaming database whose source revoked the privilege afterwards is the only way
+// this state reaches an existing row, since creating it now is refused.
+func Test_TestDatabaseConnection_WhenTheSourceRevokedTheWalSwitchPrivilege_ReturnsNoWalSwitchPrivilegeCode(
+ t *testing.T,
+) {
+ for _, fixture := range physicalFixtures() {
+ t.Run(fixture.name, func(t *testing.T) {
+ router := createTestRouter()
+ owner := users_testing.CreateTestUser(t.Context(), users_enums.UserRoleMember)
+ workspace := workspaces_testing.CreateTestWorkspace(t.Context(), "Test Workspace", owner, router)
+
+ conn := openFixtureConn(t, fixture)
+ username, password := createReplicationRole(t, conn, true)
+
+ request := buildPhysicalDatabaseRequestFor(t, physicalDatabaseRequestSpec{
+ workspaceID: workspace.ID,
+ fixture: fixture,
+ username: username,
+ password: password,
+ backupType: postgresql_physical.BackupTypeFullIncrementalAndWalStream,
+ })
+
+ createDatabaseResponse := workspaces_testing.MakeAPIRequest(
+ router, "POST", "/api/v1/databases/create", "Bearer "+owner.Token, request,
+ )
+ require.Equal(t, http.StatusCreated, createDatabaseResponse.Code,
+ "create failed: %s", createDatabaseResponse.Body.String())
+
+ var database Database
+ require.NoError(t, json.Unmarshal(createDatabaseResponse.Body.Bytes(), &database))
+ defer func() {
+ RemoveTestDatabase(t.Context(), &database)
+ workspaces_testing.RemoveTestWorkspace(t.Context(), workspace, router)
+ }()
+
+ _, err := conn.Exec(t.Context(),
+ fmt.Sprintf(`REVOKE EXECUTE ON FUNCTION pg_switch_wal() FROM "%s"`, username))
+ require.NoError(t, err)
+
+ connectionTestResponse := test_utils.MakePostRequest(
+ t, router,
+ "/api/v1/databases/"+database.ID.String()+"/test-connection",
+ "Bearer "+owner.Token,
+ nil,
+ http.StatusBadRequest,
+ )
+
+ assert.Equal(t,
+ postgresql_shared.ConnErrNoWalSwitchPrivilege,
+ getConnectionErrorCode(t, connectionTestResponse.Body))
+ })
+ }
+}
+
+func Test_UpdateDatabase_RaisingToWalStreamWhenRoleCannotSwitchWal_ReturnsNoWalSwitchPrivilegeCode(
+ t *testing.T,
+) {
+ for _, fixture := range physicalFixtures() {
+ t.Run(fixture.name, func(t *testing.T) {
+ router := createTestRouter()
+ owner := users_testing.CreateTestUser(t.Context(), users_enums.UserRoleMember)
+ workspace := workspaces_testing.CreateTestWorkspace(t.Context(), "Test Workspace", owner, router)
+
+ username, password := createReplicationRole(t, openFixtureConn(t, fixture), false)
+
+ request := buildPhysicalDatabaseRequestFor(t, physicalDatabaseRequestSpec{
+ workspaceID: workspace.ID,
+ fixture: fixture,
+ username: username,
+ password: password,
+ backupType: postgresql_physical.BackupTypeFullAndIncremental,
+ })
+
+ createDatabaseResponse := workspaces_testing.MakeAPIRequest(
+ router, "POST", "/api/v1/databases/create", "Bearer "+owner.Token, request,
+ )
+ require.Equal(t, http.StatusCreated, createDatabaseResponse.Code,
+ "create failed: %s", createDatabaseResponse.Body.String())
+
+ var database Database
+ require.NoError(t, json.Unmarshal(createDatabaseResponse.Body.Bytes(), &database))
+ defer func() {
+ RemoveTestDatabase(t.Context(), &database)
+ workspaces_testing.RemoveTestWorkspace(t.Context(), workspace, router)
+ }()
+
+ database.PostgresqlPhysical.Password = password
+ database.PostgresqlPhysical.BackupType = postgresql_physical.BackupTypeFullIncrementalAndWalStream
+
+ updateDatabaseResponse := workspaces_testing.MakeAPIRequest(
+ router, "POST", "/api/v1/databases/update", "Bearer "+owner.Token, database,
+ )
+
+ require.Equal(t, http.StatusBadRequest, updateDatabaseResponse.Code,
+ "expected refusal, got: %s", updateDatabaseResponse.Body.String())
+
+ var updateDatabaseErrorResponse struct {
+ Error string `json:"error"`
+ }
+ require.NoError(t, json.Unmarshal(updateDatabaseResponse.Body.Bytes(), &updateDatabaseErrorResponse))
+
+ // Create and update surface only Error(), so this path shows the remedy sentence
+ // rather than the machine-readable code the test-connection endpoints return.
+ assert.Contains(t, updateDatabaseErrorResponse.Error,
+ "GRANT EXECUTE ON FUNCTION pg_switch_wal()")
+ assert.Contains(t, updateDatabaseErrorResponse.Error,
+ pgx.Identifier{username}.Sanitize())
+ })
+ }
+}
diff --git a/backend/internal/features/databases/databases/postgresql/physical/dto.go b/backend/internal/features/databases/databases/postgresql/physical/dto.go
index 136ebb02..1d3c7cfe 100644
--- a/backend/internal/features/databases/databases/postgresql/physical/dto.go
+++ b/backend/internal/features/databases/databases/postgresql/physical/dto.go
@@ -6,3 +6,9 @@ type replicationSettings struct {
maxWalSenders int
maxReplicationSlots int
}
+
+type ReplicationOnlyUser struct {
+ Username string
+ Password string
+ IsForcedWalRotationAvailable bool
+}
diff --git a/backend/internal/features/databases/databases/postgresql/physical/forced_wal_rotation.go b/backend/internal/features/databases/databases/postgresql/physical/forced_wal_rotation.go
new file mode 100644
index 00000000..5a8d903d
--- /dev/null
+++ b/backend/internal/features/databases/databases/postgresql/physical/forced_wal_rotation.go
@@ -0,0 +1,70 @@
+package postgresql_physical
+
+import (
+ "context"
+ "errors"
+ "fmt"
+
+ "github.com/jackc/pgx/v5"
+ "github.com/jackc/pgx/v5/pgconn"
+)
+
+// pgRowQuerier is satisfied by both *pgx.Conn and pgx.Tx, so a probe can run on a
+// plain connection or inside the provisioning transaction that just created the role.
+type pgRowQuerier interface {
+ QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
+}
+
+// Reading the privilege instead of calling pg_switch_wal() keeps this safe to run on
+// every connection test: a successful call would archive a padded WAL segment.
+func canForceWalRotation(ctx context.Context, querier pgRowQuerier, roleName string) (bool, error) {
+ var canExecute bool
+
+ if err := querier.QueryRow(
+ ctx,
+ `SELECT has_function_privilege($1, 'pg_switch_wal()', 'EXECUTE')`,
+ roleName,
+ ).Scan(&canExecute); err != nil {
+ return false, fmt.Errorf("failed to check pg_switch_wal privilege: %w", err)
+ }
+
+ return canExecute, nil
+}
+
+const pgErrorCodeInsufficientPrivilege = "42501"
+
+// A source that will not confer this cannot be used for continuous WAL streaming, but
+// it still serves FULL and incremental backups, so a refusal is an outcome rather than a
+// failure. The GRANT runs in a savepoint because a refused statement would otherwise
+// abort the transaction that created the role.
+func grantWalSwitchIfPermitted(ctx context.Context, tx pgx.Tx, username string) (isGranted bool, err error) {
+ savepoint, err := tx.Begin(ctx)
+ if err != nil {
+ return false, fmt.Errorf("failed to open savepoint for the pg_switch_wal grant: %w", err)
+ }
+
+ quotedUsername := pgx.Identifier{username}.Sanitize()
+
+ _, err = savepoint.Exec(ctx,
+ fmt.Sprintf(`GRANT EXECUTE ON FUNCTION pg_switch_wal() TO %s`, quotedUsername))
+ if err != nil {
+ if rollbackErr := savepoint.Rollback(ctx); rollbackErr != nil {
+ return false, fmt.Errorf("failed to roll back the refused pg_switch_wal grant: %w", rollbackErr)
+ }
+
+ var pgErr *pgconn.PgError
+ if errors.As(err, &pgErr) && pgErr.Code == pgErrorCodeInsufficientPrivilege {
+ return false, nil
+ }
+
+ return false, fmt.Errorf("failed to grant EXECUTE on pg_switch_wal: %w", err)
+ }
+
+ if err := savepoint.Commit(ctx); err != nil {
+ return false, fmt.Errorf("failed to commit the pg_switch_wal grant: %w", err)
+ }
+
+ // The catalog, not the statement's exit status, is what the connection test later
+ // reads, so provisioning and testing cannot disagree about the same role.
+ return canForceWalRotation(ctx, tx, username)
+}
diff --git a/backend/internal/features/databases/databases/postgresql/physical/forced_wal_rotation_test.go b/backend/internal/features/databases/databases/postgresql/physical/forced_wal_rotation_test.go
new file mode 100644
index 00000000..b8ce1203
--- /dev/null
+++ b/backend/internal/features/databases/databases/postgresql/physical/forced_wal_rotation_test.go
@@ -0,0 +1,226 @@
+package postgresql_physical
+
+import (
+ "context"
+ "fmt"
+ "testing"
+
+ "github.com/jackc/pgx/v5"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+
+ postgresql_shared "databasus-backend/internal/features/databases/databases/postgresql/shared"
+)
+
+func grantWalSwitchTo(t *testing.T, conn *pgx.Conn, username string) {
+ t.Helper()
+
+ _, err := conn.Exec(
+ context.Background(),
+ fmt.Sprintf(`GRANT EXECUTE ON FUNCTION pg_switch_wal() TO "%s"`, username),
+ )
+ require.NoError(t, err)
+}
+
+func Test_CanForceWalRotation_ForReplicationOnlyRole_ReturnsFalse(t *testing.T) {
+ for _, fixture := range physicalFixtures() {
+ t.Run(fixture.name, func(t *testing.T) {
+ conn := openTestConn(t, fixture.port())
+ username, _ := createTempUser(t, conn, "REPLICATION")
+
+ canRotate, err := canForceWalRotation(context.Background(), conn, username)
+
+ require.NoError(t, err)
+ assert.False(t, canRotate)
+ })
+ }
+}
+
+func Test_CanForceWalRotation_AfterExecuteIsGranted_ReturnsTrue(t *testing.T) {
+ for _, fixture := range physicalFixtures() {
+ t.Run(fixture.name, func(t *testing.T) {
+ conn := openTestConn(t, fixture.port())
+ username, _ := createTempUser(t, conn, "REPLICATION")
+ grantWalSwitchTo(t, conn, username)
+
+ canRotate, err := canForceWalRotation(context.Background(), conn, username)
+
+ require.NoError(t, err)
+ assert.True(t, canRotate)
+ })
+ }
+}
+
+// The connected administrator can always execute pg_switch_wal, so a probe that ignored
+// its role argument would report every source as capable.
+func Test_CanForceWalRotation_WhenAnAdminProbesAnotherRole_AnswersForThatRole(t *testing.T) {
+ for _, fixture := range physicalFixtures() {
+ t.Run(fixture.name, func(t *testing.T) {
+ conn := openTestConn(t, fixture.port())
+ username, _ := createTempUser(t, conn, "REPLICATION")
+
+ canAdminRotate, err := canForceWalRotation(context.Background(), conn, "testuser")
+ require.NoError(t, err)
+ require.True(t, canAdminRotate, "the test administrator must be able to rotate")
+
+ canRoleRotate, err := canForceWalRotation(context.Background(), conn, username)
+
+ require.NoError(t, err)
+ assert.False(t, canRoleRotate)
+ })
+ }
+}
+
+func Test_TestReplicationConnection_ForWalStreamWhenRoleCannotSwitchWal_ReturnsNoWalSwitchPrivilegeCode(
+ t *testing.T,
+) {
+ for _, fixture := range physicalFixtures() {
+ t.Run(fixture.name, func(t *testing.T) {
+ conn := openTestConn(t, fixture.port())
+ username, password := createTempUser(t, conn, "REPLICATION")
+
+ physicalDatabase := newTestModel(t, fixture.port())
+ physicalDatabase.Username = username
+ physicalDatabase.Password = password
+ physicalDatabase.BackupType = BackupTypeFullIncrementalAndWalStream
+
+ err := physicalDatabase.TestReplicationConnection(testLogger(), nil)
+
+ assert.Equal(t, postgresql_shared.ConnErrNoWalSwitchPrivilege, connTestErrorCode(t, err))
+ })
+ }
+}
+
+func Test_TestReplicationConnection_ForWalStreamAfterExecuteIsGranted_Succeeds(t *testing.T) {
+ for _, fixture := range physicalFixtures() {
+ t.Run(fixture.name, func(t *testing.T) {
+ conn := openTestConn(t, fixture.port())
+ username, password := createTempUser(t, conn, "REPLICATION")
+ grantWalSwitchTo(t, conn, username)
+
+ physicalDatabase := newTestModel(t, fixture.port())
+ physicalDatabase.Username = username
+ physicalDatabase.Password = password
+ physicalDatabase.BackupType = BackupTypeFullIncrementalAndWalStream
+
+ assert.NoError(t, physicalDatabase.TestReplicationConnection(testLogger(), nil))
+ })
+ }
+}
+
+// FULL and incremental restores replay no archived WAL, so demanding the privilege for
+// them would refuse sources that back up correctly.
+func Test_TestReplicationConnection_WhenRoleCannotSwitchWal_SucceedsForBackupTypesWithoutWalReplay(
+ t *testing.T,
+) {
+ for _, fixture := range physicalFixtures() {
+ t.Run(fixture.name, func(t *testing.T) {
+ conn := openTestConn(t, fixture.port())
+ username, password := createTempUser(t, conn, "REPLICATION")
+
+ for _, backupType := range []BackupType{BackupTypeFullOnly, BackupTypeFullAndIncremental} {
+ physicalDatabase := newTestModel(t, fixture.port())
+ physicalDatabase.Username = username
+ physicalDatabase.Password = password
+ physicalDatabase.BackupType = backupType
+
+ assert.NoErrorf(t, physicalDatabase.TestReplicationConnection(testLogger(), nil),
+ "backup type %s must not require the pg_switch_wal privilege", backupType)
+ }
+ })
+ }
+}
+
+func dropUserAfterTest(t *testing.T, conn *pgx.Conn, username string) {
+ t.Helper()
+
+ t.Cleanup(func() {
+ _, _ = conn.Exec(context.Background(), fmt.Sprintf(`DROP USER IF EXISTS "%s"`, username))
+ })
+}
+
+func Test_CreateReplicationOnlyUser_OnSelfManagedSource_GrantsForcedWalRotation(t *testing.T) {
+ for _, fixture := range physicalFixtures() {
+ t.Run(fixture.name, func(t *testing.T) {
+ conn := openTestConn(t, fixture.port())
+
+ createdUser, err := newTestModel(t, fixture.port()).
+ CreateReplicationOnlyUser(context.Background(), testLogger(), nil)
+ require.NoError(t, err)
+ dropUserAfterTest(t, conn, createdUser.Username)
+
+ assert.True(t, createdUser.IsForcedWalRotationAvailable)
+
+ physicalDatabase := newTestModel(t, fixture.port())
+ physicalDatabase.Username = createdUser.Username
+ physicalDatabase.Password = createdUser.Password
+ physicalDatabase.BackupType = BackupTypeFullIncrementalAndWalStream
+
+ assert.NoError(t, physicalDatabase.TestReplicationConnection(testLogger(), nil))
+ })
+ }
+}
+
+func Test_CreateReplicationOnlyUser_WhenTheSourceRefusesTheGrant_StillReturnsWorkingCredentials(
+ t *testing.T,
+) {
+ for _, fixture := range physicalFixtures() {
+ t.Run(fixture.name, func(t *testing.T) {
+ conn := openTestConn(t, fixture.port())
+
+ // A role that may create roles and confer REPLICATION, but owns none of the
+ // pg_catalog functions, is what a managed platform's administrator looks like.
+ adminName, adminPassword := createTempUser(t, conn, "CREATEROLE REPLICATION")
+
+ provisioner := newTestModel(t, fixture.port())
+ provisioner.Username = adminName
+ provisioner.Password = adminPassword
+
+ createdUser, err := provisioner.CreateReplicationOnlyUser(
+ context.Background(), testLogger(), nil,
+ )
+ require.NoError(t, err)
+ dropUserAfterTest(t, conn, createdUser.Username)
+
+ assert.False(t, createdUser.IsForcedWalRotationAvailable)
+
+ physicalDatabase := newTestModel(t, fixture.port())
+ physicalDatabase.Username = createdUser.Username
+ physicalDatabase.Password = createdUser.Password
+ physicalDatabase.BackupType = BackupTypeFullAndIncremental
+
+ assert.NoError(t, physicalDatabase.TestReplicationConnection(testLogger(), nil))
+ })
+ }
+}
+
+func Test_CreateReplicationOnlyUser_ConfersForcedWalRotationOnTheCreatedRoleAlone(t *testing.T) {
+ for _, fixture := range physicalFixtures() {
+ t.Run(fixture.name, func(t *testing.T) {
+ conn := openTestConn(t, fixture.port())
+
+ bystanderName, _ := createTempUser(t, conn, "REPLICATION")
+
+ createdUser, err := newTestModel(t, fixture.port()).
+ CreateReplicationOnlyUser(context.Background(), testLogger(), nil)
+ require.NoError(t, err)
+ dropUserAfterTest(t, conn, createdUser.Username)
+
+ canBystanderRotate, err := canForceWalRotation(context.Background(), conn, bystanderName)
+ require.NoError(t, err)
+ assert.False(t, canBystanderRotate, "the grant must not reach any other role")
+
+ var isSuper, canCreateRole, canCreateDB, canBypassRLS bool
+ require.NoError(t, conn.QueryRow(context.Background(), `
+ SELECT rolsuper, rolcreaterole, rolcreatedb, rolbypassrls
+ FROM pg_roles
+ WHERE rolname = $1
+ `, createdUser.Username).Scan(&isSuper, &canCreateRole, &canCreateDB, &canBypassRLS))
+
+ assert.False(t, isSuper)
+ assert.False(t, canCreateRole)
+ assert.False(t, canCreateDB)
+ assert.False(t, canBypassRLS)
+ })
+ }
+}
diff --git a/backend/internal/features/databases/databases/postgresql/physical/model.go b/backend/internal/features/databases/databases/postgresql/physical/model.go
index d44ba68f..0c16453a 100644
--- a/backend/internal/features/databases/databases/postgresql/physical/model.go
+++ b/backend/internal/features/databases/databases/postgresql/physical/model.go
@@ -715,24 +715,21 @@ func (p *PostgresqlPhysicalDatabase) ShouldSuggestReplicationOnlyUser(
return len(excessive) > 0, excessive, nil
}
-// CreateReplicationOnlyUser provisions a fresh role with exactly
-// LOGIN + REPLICATION (or its cloud equivalent). Mirrors logical's
-// CreateReadOnlyUser but trivially smaller — physical doesn't need schema /
-// table grants. Cloud-aware: each platform grants replication differently
-// and some (Azure / GCP) require operator action in the console first.
+// Managed platforms grant replication through platform roles and may withhold EXECUTE on
+// pg_switch_wal(), so the two capabilities are negotiated independently.
func (p *PostgresqlPhysicalDatabase) CreateReplicationOnlyUser(
ctx context.Context,
logger *slog.Logger,
encryptor encryption.FieldEncryptor,
-) (string, string, error) {
+) (*ReplicationOnlyUser, error) {
conn, err := openConn(ctx, p, encryptor)
if err != nil {
- return "", "", fmt.Errorf("failed to connect to cluster: %w", err)
+ return nil, fmt.Errorf("failed to connect to cluster: %w", err)
}
defer closeConnQuietly(ctx, conn, logger)
if err := assertCanCreateRole(ctx, conn); err != nil {
- return "", "", err
+ return nil, err
}
platform := detectPlatform(ctx, conn)
@@ -744,7 +741,7 @@ func (p *PostgresqlPhysicalDatabase) CreateReplicationOnlyUser(
tx, err := conn.Begin(ctx)
if err != nil {
- return "", "", fmt.Errorf("failed to begin transaction: %w", err)
+ return nil, fmt.Errorf("failed to begin transaction: %w", err)
}
isCommitted := false
@@ -764,11 +761,16 @@ func (p *PostgresqlPhysicalDatabase) CreateReplicationOnlyUser(
if strings.Contains(err.Error(), "already exists") && attempt < maxRetries-1 {
continue
}
- return "", "", fmt.Errorf("failed to create user: %w", err)
+ return nil, fmt.Errorf("failed to create user: %w", err)
}
if err := grantReplication(ctx, tx, baseUsername, platform); err != nil {
- return "", "", err
+ return nil, err
+ }
+
+ isWalSwitchGranted, err := grantWalSwitchIfPermitted(ctx, tx, baseUsername)
+ if err != nil {
+ return nil, err
}
var verifyName string
@@ -777,19 +779,35 @@ func (p *PostgresqlPhysicalDatabase) CreateReplicationOnlyUser(
`SELECT rolname FROM pg_roles WHERE rolname = $1`,
baseUsername,
).Scan(&verifyName); err != nil {
- return "", "", fmt.Errorf("failed to verify user creation: %w", err)
+ return nil, fmt.Errorf("failed to verify user creation: %w", err)
}
if err := tx.Commit(ctx); err != nil {
- return "", "", fmt.Errorf("failed to commit transaction: %w", err)
+ return nil, fmt.Errorf("failed to commit transaction: %w", err)
}
isCommitted = true
logger.InfoContext(ctx, "replication-only user created", "username", baseUsername, "platform", platform)
- return baseUsername, newPassword, nil
+
+ if !isWalSwitchGranted {
+ logger.WarnContext(
+ ctx,
+ "source refused the pg_switch_wal grant; continuous wal streaming is unavailable for this user",
+ "username",
+ baseUsername,
+ "platform",
+ platform,
+ )
+ }
+
+ return &ReplicationOnlyUser{
+ Username: baseUsername,
+ Password: newPassword,
+ IsForcedWalRotationAvailable: isWalSwitchGranted,
+ }, nil
}
- return "", "", errors.New("failed to generate unique username after 3 attempts")
+ return nil, errors.New("failed to generate unique username after 3 attempts")
}
// OpenInspectionConn opens a regular (non-replication) connection to the
@@ -853,6 +871,30 @@ func (p *PostgresqlPhysicalDatabase) checkReplicationReadiness(
return &postgresql_shared.ConnectionTestError{Code: postgresql_shared.ConnErrWalSummaryDisabled}
}
+ // Continuous streaming promises a recovery point between backups, and only a
+ // finalized segment is uploaded. Without pg_switch_wal a quiet source leaves its
+ // newest WAL local until the segment fills, so the promise cannot be kept.
+ if p.BackupType.IsWalStreaming() {
+ canRotate, err := canForceWalRotation(ctx, conn, p.Username)
+ if err != nil {
+ return err
+ }
+
+ if !canRotate {
+ quotedUsername := pgx.Identifier{p.Username}.Sanitize()
+
+ return &postgresql_shared.ConnectionTestError{
+ Code: postgresql_shared.ConnErrNoWalSwitchPrivilege,
+ Message: fmt.Sprintf(
+ "user %s cannot force a WAL segment switch, so the recovery point between backups "+
+ "would advance only as segments fill. Run GRANT EXECUTE ON FUNCTION pg_switch_wal() TO %s "+
+ "on the source, or choose a backup type that does not replay archived WAL",
+ quotedUsername, quotedUsername,
+ ),
+ }
+ }
+ }
+
if p.SystemIdentifier != nil {
var currentSysID string
if err := conn.QueryRow(ctx, "SELECT system_identifier::text FROM pg_control_system()").
diff --git a/backend/internal/features/databases/databases/postgresql/physical/model_test.go b/backend/internal/features/databases/databases/postgresql/physical/model_test.go
index 00db2896..686521a2 100644
--- a/backend/internal/features/databases/databases/postgresql/physical/model_test.go
+++ b/backend/internal/features/databases/databases/postgresql/physical/model_test.go
@@ -701,7 +701,7 @@ func Test_ShouldSuggestReplicationOnlyUser_FalseForFreshlyCreatedReplicationUser
}
provisioner := newTestModel(t, fx.port())
- username, password, err := provisioner.CreateReplicationOnlyUser(
+ createdUser, err := provisioner.CreateReplicationOnlyUser(
context.Background(), testLogger(), nil,
)
require.NoError(t, err)
@@ -711,13 +711,13 @@ func Test_ShouldSuggestReplicationOnlyUser_FalseForFreshlyCreatedReplicationUser
defer setupConn.Close(context.Background())
_, _ = setupConn.Exec(
context.Background(),
- fmt.Sprintf(`DROP USER IF EXISTS "%s"`, username),
+ fmt.Sprintf(`DROP USER IF EXISTS "%s"`, createdUser.Username),
)
})
m := newTestModel(t, fx.port())
- m.Username = username
- m.Password = password
+ m.Username = createdUser.Username
+ m.Password = createdUser.Password
shouldSuggestReplicationOnlyUser, excessive, err := m.ShouldSuggestReplicationOnlyUser(
context.Background(),
@@ -782,14 +782,16 @@ func Test_CreateReplicationOnlyUser_HappyPath(t *testing.T) {
m := newTestModel(t, fx.port())
- username, password, err := m.CreateReplicationOnlyUser(
+ createdUser, err := m.CreateReplicationOnlyUser(
context.Background(), testLogger(), nil,
)
require.NoError(t, err)
+
+ username := createdUser.Username
assert.True(t,
len(username) > len("databasus-") && username[:len("databasus-")] == "databasus-",
"username=%s", username)
- assert.NotEmpty(t, password)
+ assert.NotEmpty(t, createdUser.Password)
t.Cleanup(func() {
setupConn := openTestConn(t, fx.port())
@@ -827,7 +829,7 @@ func Test_CreateReplicationOnlyUser_NewUserCanOpenReplicationConnection(t *testi
}
provisioner := newTestModel(t, fx.port())
- username, password, err := provisioner.CreateReplicationOnlyUser(
+ createdUser, err := provisioner.CreateReplicationOnlyUser(
context.Background(), testLogger(), nil,
)
require.NoError(t, err)
@@ -837,7 +839,7 @@ func Test_CreateReplicationOnlyUser_NewUserCanOpenReplicationConnection(t *testi
defer setupConn.Close(context.Background())
_, _ = setupConn.Exec(
context.Background(),
- fmt.Sprintf(`DROP USER IF EXISTS "%s"`, username),
+ fmt.Sprintf(`DROP USER IF EXISTS "%s"`, createdUser.Username),
)
})
@@ -846,7 +848,7 @@ func Test_CreateReplicationOnlyUser_NewUserCanOpenReplicationConnection(t *testi
dsn := fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=postgres sslmode=disable replication=true",
- config.GetEnv().TestLocalhost, portInt, username, password,
+ config.GetEnv().TestLocalhost, portInt, createdUser.Username, createdUser.Password,
)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
@@ -874,7 +876,7 @@ func Test_CreateReplicationOnlyUser_FailsWhenCurrentUserCannotCreateRole(t *test
m.Username = username
m.Password = password
- _, _, err := m.CreateReplicationOnlyUser(context.Background(), testLogger(), nil)
+ _, err := m.CreateReplicationOnlyUser(context.Background(), testLogger(), nil)
require.Error(t, err)
assert.Contains(t, err.Error(), "create roles")
})
@@ -982,11 +984,13 @@ func Test_CreateReplicationOnlyUser_OnSimulatedRds_GrantsRdsReplicationMembershi
createMarkerRole(t, setupConn, "rds_replication")
m := newTestModel(t, fx.port())
- username, _, err := m.CreateReplicationOnlyUser(
+ createdUser, err := m.CreateReplicationOnlyUser(
context.Background(), testLogger(), nil,
)
require.NoError(t, err)
+ username := createdUser.Username
+
t.Cleanup(func() {
_, _ = setupConn.Exec(
context.Background(),
diff --git a/backend/internal/features/databases/databases/postgresql/shared/connection_error.go b/backend/internal/features/databases/databases/postgresql/shared/connection_error.go
index 1492387f..40711b79 100644
--- a/backend/internal/features/databases/databases/postgresql/shared/connection_error.go
+++ b/backend/internal/features/databases/databases/postgresql/shared/connection_error.go
@@ -14,6 +14,7 @@ const (
ConnErrWalSummaryDisabled ConnectionErrorCode = "wal_summary_disabled"
ConnErrCustomTablespaces ConnectionErrorCode = "custom_tablespaces"
ConnErrSystemIdentifierMismatch ConnectionErrorCode = "system_identifier_mismatch"
+ ConnErrNoWalSwitchPrivilege ConnectionErrorCode = "no_wal_switch_privilege"
)
// ConnectionTestError carries a machine-readable Code for a classified connection failure.
diff --git a/backend/internal/features/databases/dto.go b/backend/internal/features/databases/dto.go
index f5dcc6fa..6f15a85d 100644
--- a/backend/internal/features/databases/dto.go
+++ b/backend/internal/features/databases/dto.go
@@ -9,3 +9,9 @@ type ShouldSuggestReadOnlyUserResponse struct {
ShouldSuggestReadOnlyUser bool `json:"shouldSuggestReadOnlyUser"`
Privileges []string `json:"privileges"`
}
+
+type CreateReplicationOnlyUserResponse struct {
+ Username string `json:"username"`
+ Password string `json:"password"`
+ IsForcedWalRotationAvailable bool `json:"isForcedWalRotationAvailable"`
+}
diff --git a/backend/internal/features/databases/physical_model_test.go b/backend/internal/features/databases/physical_model_test.go
index 626f144c..78e0bc5a 100644
--- a/backend/internal/features/databases/physical_model_test.go
+++ b/backend/internal/features/databases/physical_model_test.go
@@ -102,31 +102,32 @@ func Test_TestConnection_OnPhysicalDatabase_DispatchesToReplicationConnection(t
}
func Test_CreateReplicationOnlyUser_OnPhysicalDatabase_ReturnsCredentials(t *testing.T) {
- for _, fx := range physicalFixtures() {
- t.Run(fx.name, func(t *testing.T) {
+ for _, fixture := range physicalFixtures() {
+ t.Run(fixture.name, func(t *testing.T) {
router := createTestRouter()
owner := users_testing.CreateTestUser(t.Context(), users_enums.UserRoleMember)
workspace := workspaces_testing.CreateTestWorkspace(t.Context(), "Test Workspace", owner, router)
- database := createPhysicalDatabaseInternalAPI(t, router, workspace.ID, owner.Token, fx)
+ database := createPhysicalDatabaseInternalAPI(t, router, workspace.ID, owner.Token, fixture)
defer func() {
RemoveTestDatabase(t.Context(), database)
workspaces_testing.RemoveTestWorkspace(t.Context(), workspace, router)
}()
- var response CreateReadOnlyUserResponse
+ var createReplicationOnlyUserResponse CreateReplicationOnlyUserResponse
test_utils.MakePostRequestAndUnmarshal(
t, router,
"/api/v1/databases/create-replication-only-user",
"Bearer "+owner.Token,
database,
http.StatusOK,
- &response,
+ &createReplicationOnlyUserResponse,
)
- assert.NotEmpty(t, response.Username)
- assert.NotEmpty(t, response.Password)
- assert.Contains(t, response.Username, "databasus-")
+ assert.NotEmpty(t, createReplicationOnlyUserResponse.Username)
+ assert.NotEmpty(t, createReplicationOnlyUserResponse.Password)
+ assert.Contains(t, createReplicationOnlyUserResponse.Username, "databasus-")
+ assert.True(t, createReplicationOnlyUserResponse.IsForcedWalRotationAvailable)
})
}
}
diff --git a/backend/internal/features/databases/service.go b/backend/internal/features/databases/service.go
index 61449dc5..c27b23f4 100644
--- a/backend/internal/features/databases/service.go
+++ b/backend/internal/features/databases/service.go
@@ -875,35 +875,35 @@ func (s *DatabaseService) CreateReplicationOnlyUser(
ctx context.Context,
user *users_models.User,
database *Database,
-) (string, string, error) {
+) (*postgresql_physical.ReplicationOnlyUser, error) {
var usingDatabase *Database
if database.ID != uuid.Nil {
existingDatabase, err := s.dbRepository.FindByID(database.ID)
if err != nil {
- return "", "", err
+ return nil, err
}
if existingDatabase.WorkspaceID == nil {
- return "", "", errors.New("cannot create user for database without workspace")
+ return nil, errors.New("cannot create user for database without workspace")
}
canManage, err := s.workspaceService.CanUserManageDBs(ctx, *existingDatabase.WorkspaceID, user)
if err != nil {
- return "", "", err
+ return nil, err
}
if !canManage {
- return "", "", errors.New("insufficient permissions to manage this database")
+ return nil, errors.New("insufficient permissions to manage this database")
}
if database.WorkspaceID != nil && *existingDatabase.WorkspaceID != *database.WorkspaceID {
- return "", "", errors.New("database does not belong to this workspace")
+ return nil, errors.New("database does not belong to this workspace")
}
existingDatabase.Update(database)
if err := existingDatabase.Validate(); err != nil {
- return "", "", err
+ return nil, err
}
usingDatabase = existingDatabase
@@ -911,10 +911,10 @@ func (s *DatabaseService) CreateReplicationOnlyUser(
if database.WorkspaceID != nil {
canManage, err := s.workspaceService.CanUserManageDBs(ctx, *database.WorkspaceID, user)
if err != nil {
- return "", "", err
+ return nil, err
}
if !canManage {
- return "", "", errors.New("insufficient permissions to manage this workspace")
+ return nil, errors.New("insufficient permissions to manage this workspace")
}
}
@@ -922,13 +922,13 @@ func (s *DatabaseService) CreateReplicationOnlyUser(
}
if usingDatabase.Type != DatabaseTypePostgresPhysical {
- return "", "", errors.New(
+ return nil, errors.New(
"replication-only user creation is only supported for POSTGRES_PHYSICAL databases",
)
}
if usingDatabase.PostgresqlPhysical == nil {
- return "", "", errors.New("physical database details are missing")
+ return nil, errors.New("physical database details are missing")
}
tunnelCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
@@ -942,15 +942,15 @@ func (s *DatabaseService) CreateReplicationOnlyUser(
Encryptor: s.fieldEncryptor,
})
if err != nil {
- return "", "", err
+ return nil, err
}
defer tunneledDatabase.Close()
- username, password, err := tunneledDatabase.GetDatabaseThroughTunnel().
+ createdUser, err := tunneledDatabase.GetDatabaseThroughTunnel().
PostgresqlPhysical.CreateReplicationOnlyUser(tunnelCtx, logger, s.fieldEncryptor)
if err != nil {
- return "", "", err
+ return nil, err
}
if usingDatabase.WorkspaceID != nil {
@@ -958,14 +958,14 @@ func (s *DatabaseService) CreateReplicationOnlyUser(
Message: fmt.Sprintf(
"Replication-only user created for database: %s (username: %s)",
usingDatabase.Name,
- username,
+ createdUser.Username,
),
UserID: &user.ID,
WorkspaceID: usingDatabase.WorkspaceID,
})
}
- return username, password, nil
+ return createdUser, nil
}
// fillPhysicalLastBackupTimes populates LastBackupTime for physical databases,
diff --git a/frontend/src/entity/databases/api/databaseApi.ts b/frontend/src/entity/databases/api/databaseApi.ts
index 9bcab71d..3f0c0c14 100644
--- a/frontend/src/entity/databases/api/databaseApi.ts
+++ b/frontend/src/entity/databases/api/databaseApi.ts
@@ -2,6 +2,7 @@ import { getApplicationServer } from '../../../constants';
import RequestOptions from '../../../shared/api/RequestOptions';
import { apiHelper } from '../../../shared/api/apiHelper';
import type { CreateReadOnlyUserResponse } from '../model/CreateReadOnlyUserResponse';
+import type { CreateReplicationOnlyUserResponse } from '../model/CreateReplicationOnlyUserResponse';
import type { Database } from '../model/Database';
import type { ShouldSuggestReadOnlyUserResponse } from '../model/ShouldSuggestReadOnlyUserResponse';
@@ -122,7 +123,7 @@ export const databaseApi = {
async createReplicationOnlyUser(database: Database) {
const requestOptions: RequestOptions = new RequestOptions();
requestOptions.setBody(JSON.stringify(database));
- return apiHelper.fetchPostJson<CreateReadOnlyUserResponse>(
+ return apiHelper.fetchPostJson<CreateReplicationOnlyUserResponse>(
`${getApplicationServer()}/api/v1/databases/create-replication-only-user`,
requestOptions,
);
diff --git a/frontend/src/entity/databases/index.ts b/frontend/src/entity/databases/index.ts
index 82eedbea..4fdef8bf 100644
--- a/frontend/src/entity/databases/index.ts
+++ b/frontend/src/entity/databases/index.ts
@@ -7,6 +7,7 @@ export { isPostgresType } from './model/isPostgresType';
export { initializeDatabaseTypeData } from './model/initializeDatabaseTypeData';
export { Period } from './model/Period';
export { PostgresSslMode } from './model/postgresql/PostgresSslMode';
+export { quotePostgresqlIdentifier } from './model/postgresql/quotePostgresqlIdentifier';
export { type PostgresqlLogicalDatabase } from './model/postgresql/PostgresqlLogicalDatabase';
export { type PostgresqlPhysicalDatabase } from './model/postgresql/physical/PostgresqlPhysicalDatabase';
export { PhysicalDatabaseBackupType } from './model/postgresql/physical/PhysicalDatabaseBackupType';
@@ -37,3 +38,4 @@ export { MongodbVersion } from './model/mongodb/MongodbVersion';
export { disableSrvWhenTunneled } from './model/mongodb/disableSrvWhenTunneled';
export { type ShouldSuggestReadOnlyUserResponse } from './model/ShouldSuggestReadOnlyUserResponse';
export { type CreateReadOnlyUserResponse } from './model/CreateReadOnlyUserResponse';
+export { type CreateReplicationOnlyUserResponse } from './model/CreateReplicationOnlyUserResponse';
diff --git a/frontend/src/entity/databases/model/CreateReplicationOnlyUserResponse.ts b/frontend/src/entity/databases/model/CreateReplicationOnlyUserResponse.ts
new file mode 100644
index 00000000..b01b282b
--- /dev/null
+++ b/frontend/src/entity/databases/model/CreateReplicationOnlyUserResponse.ts
@@ -0,0 +1,5 @@
+export interface CreateReplicationOnlyUserResponse {
+ username: string;
+ password: string;
+ isForcedWalRotationAvailable: boolean;
+}
diff --git a/frontend/src/entity/databases/model/postgresql/physical/ConnectionErrorCode.ts b/frontend/src/entity/databases/model/postgresql/physical/ConnectionErrorCode.ts
index 70ad4cac..05a15c32 100644
--- a/frontend/src/entity/databases/model/postgresql/physical/ConnectionErrorCode.ts
+++ b/frontend/src/entity/databases/model/postgresql/physical/ConnectionErrorCode.ts
@@ -9,6 +9,7 @@ export enum ConnectionErrorCode {
NoWalSenders = 'no_wal_senders',
NoReplicationSlots = 'no_replication_slots',
WalSummaryDisabled = 'wal_summary_disabled',
+ NoWalSwitchPrivilege = 'no_wal_switch_privilege',
CustomTablespaces = 'custom_tablespaces',
SystemIdentifierMismatch = 'system_identifier_mismatch',
}
diff --git a/frontend/src/entity/databases/model/postgresql/physical/physicalConnectionErrorContent.test.ts b/frontend/src/entity/databases/model/postgresql/physical/physicalConnectionErrorContent.test.ts
new file mode 100644
index 00000000..01db3203
--- /dev/null
+++ b/frontend/src/entity/databases/model/postgresql/physical/physicalConnectionErrorContent.test.ts
@@ -0,0 +1,16 @@
+import { describe, expect, it } from 'vitest';
+
+import { ConnectionErrorCode } from './ConnectionErrorCode';
+import { physicalConnectionErrorContent } from './physicalConnectionErrorContent';
+
+describe('physicalConnectionErrorContent', () => {
+ it('quotes a generated replication role in the WAL switch grant command', () => {
+ const buildSteps =
+ physicalConnectionErrorContent[ConnectionErrorCode.NoWalSwitchPrivilege].buildSteps;
+
+ expect(buildSteps?.({ username: 'databasus-ab12cd34' })).toContainEqual({
+ type: 'command',
+ command: 'GRANT EXECUTE ON FUNCTION pg_switch_wal() TO "databasus-ab12cd34";',
+ });
+ });
+});
diff --git a/frontend/src/entity/databases/model/postgresql/physical/physicalConnectionErrorContent.ts b/frontend/src/entity/databases/model/postgresql/physical/physicalConnectionErrorContent.ts
index ce9bc434..311dd962 100644
--- a/frontend/src/entity/databases/model/postgresql/physical/physicalConnectionErrorContent.ts
+++ b/frontend/src/entity/databases/model/postgresql/physical/physicalConnectionErrorContent.ts
@@ -1,3 +1,4 @@
+import { quotePostgresqlIdentifier } from '../quotePostgresqlIdentifier';
import { ConnectionErrorCode } from './ConnectionErrorCode';
export interface PhysicalConnectionErrorTextRun {
@@ -65,12 +66,42 @@ export const physicalConnectionErrorContent: Record<
title: 'User cannot run replication',
summary:
'The user connected but lacks the REPLICATION privilege that physical backups require.',
- buildSteps: ({ username }) => [
- { type: 'note', runs: [{ text: 'Grant it with the command below (run as a superuser).' }] },
- { type: 'command', command: `ALTER ROLE ${username} REPLICATION;` },
- ],
+ buildSteps: ({ username }) => {
+ const quotedUsername = quotePostgresqlIdentifier(username);
+
+ return [
+ { type: 'note', runs: [{ text: 'Grant it with the command below (run as a superuser).' }] },
+ { type: 'command', command: `ALTER ROLE ${quotedUsername} REPLICATION;` },
+ ];
+ },
managedNote: ({ username }) =>
- `On AWS RDS use GRANT rds_replication TO ${username}; on Azure / GCP enable replication for the role in the provider console.`,
+ `On AWS RDS use GRANT rds_replication TO ${quotePostgresqlIdentifier(username)}; on Azure / GCP enable replication for the role in the provider console.`,
+ },
+ [ConnectionErrorCode.NoWalSwitchPrivilege]: {
+ title: 'User cannot force a WAL segment switch',
+ summary:
+ 'Continuous WAL streaming only uploads a segment once the source closes it, so on a rarely-written database the newest WAL stays on the source until the 16 MB segment fills. Forcing a switch on a timer is what keeps the recovery point between backups close to the present, and it needs EXECUTE on pg_switch_wal(). Full and incremental backups do not need it: they restore to the point each backup finished and replay no archived WAL.',
+ buildSteps: ({ username }) => {
+ const quotedUsername = quotePostgresqlIdentifier(username);
+
+ return [
+ { type: 'note', runs: [{ text: 'Grant it with the command below (run as a superuser).' }] },
+ {
+ type: 'command',
+ command: `GRANT EXECUTE ON FUNCTION pg_switch_wal() TO ${quotedUsername};`,
+ },
+ {
+ type: 'note',
+ runs: [
+ { text: 'Or switch the backup type to ' },
+ { text: 'Full + incremental', isBold: true },
+ { text: ', which does not replay archived WAL.' },
+ ],
+ },
+ ];
+ },
+ managedNote:
+ 'On managed PostgreSQL (RDS / Azure / GCP) the function belongs to the platform superuser and a customer role cannot be granted EXECUTE on it, so continuous WAL streaming is unavailable there - use full + incremental backups instead.',
},
[ConnectionErrorCode.WalLevelInvalid]: {
title: 'wal_level is too low',
diff --git a/frontend/src/entity/databases/model/postgresql/quotePostgresqlIdentifier.test.ts b/frontend/src/entity/databases/model/postgresql/quotePostgresqlIdentifier.test.ts
new file mode 100644
index 00000000..74001c4c
--- /dev/null
+++ b/frontend/src/entity/databases/model/postgresql/quotePostgresqlIdentifier.test.ts
@@ -0,0 +1,13 @@
+import { describe, expect, it } from 'vitest';
+
+import { quotePostgresqlIdentifier } from './quotePostgresqlIdentifier';
+
+describe('quotePostgresqlIdentifier', () => {
+ it('quotes a Databasus-generated role name containing a hyphen', () => {
+ expect(quotePostgresqlIdentifier('databasus-ab12cd34')).toBe('"databasus-ab12cd34"');
+ });
+
+ it('escapes embedded double quotes', () => {
+ expect(quotePostgresqlIdentifier('backup"role')).toBe('"backup""role"');
+ });
+});
diff --git a/frontend/src/entity/databases/model/postgresql/quotePostgresqlIdentifier.ts b/frontend/src/entity/databases/model/postgresql/quotePostgresqlIdentifier.ts
new file mode 100644
index 00000000..eb6c72d5
--- /dev/null
+++ b/frontend/src/entity/databases/model/postgresql/quotePostgresqlIdentifier.ts
@@ -0,0 +1,2 @@
+export const quotePostgresqlIdentifier = (identifier: string): string =>
+ `"${identifier.replace(/"/g, '""')}"`;
diff --git a/frontend/src/features/databases/ui/edit/CreateReadOnlyComponent.tsx b/frontend/src/features/databases/ui/edit/CreateReadOnlyComponent.tsx
index e4317bcb..802447fa 100644
--- a/frontend/src/features/databases/ui/edit/CreateReadOnlyComponent.tsx
+++ b/frontend/src/features/databases/ui/edit/CreateReadOnlyComponent.tsx
@@ -20,6 +20,8 @@ interface Props {
const PRIVILEGES_TRUNCATE_LENGTH = 50;
+const FORCED_WAL_ROTATION_WARNING_SECONDS = 15;
+
export const CreateReadOnlyComponent = ({
database,
onReadOnlyUserUpdated,
@@ -69,29 +71,50 @@ export const CreateReadOnlyComponent = ({
return fullText.length > PRIVILEGES_TRUNCATE_LENGTH;
};
- const createReadOnlyUser = async () => {
+ const provisionAndApplyReplicationOnlyUserCredentials = async () => {
+ const response = await databaseApi.createReplicationOnlyUser(database);
+
+ if (database.postgresqlPhysical) {
+ database.postgresqlPhysical.username = response.username;
+ database.postgresqlPhysical.password = response.password;
+ }
+
+ if (!response.isForcedWalRotationAvailable) {
+ message.warning(
+ 'This source would not grant EXECUTE on pg_switch_wal() to the new user, which ' +
+ 'continuous WAL streaming needs to keep the recovery point close to the present. ' +
+ 'Full and incremental backups work normally with these credentials.',
+ FORCED_WAL_ROTATION_WARNING_SECONDS,
+ );
+ }
+ };
+
+ const provisionAndApplyReadOnlyUserCredentials = async () => {
+ const response = await databaseApi.createReadOnlyUser(database);
+
+ if (isLogicalPostgres && database.postgresqlLogical) {
+ database.postgresqlLogical.username = response.username;
+ database.postgresqlLogical.password = response.password;
+ } else if (isMysql && database.mysql) {
+ database.mysql.username = response.username;
+ database.mysql.password = response.password;
+ } else if (isMariadb && database.mariadb) {
+ database.mariadb.username = response.username;
+ database.mariadb.password = response.password;
+ } else if (isMongodb && database.mongodb) {
+ database.mongodb.username = response.username;
+ database.mongodb.password = response.password;
+ }
+ };
+
+ const provisionRestrictedUser = async () => {
setIsCreatingReadOnlyUser(true);
try {
- const response = isPhysicalPostgres
- ? await databaseApi.createReplicationOnlyUser(database)
- : await databaseApi.createReadOnlyUser(database);
-
- if (isPhysicalPostgres && database.postgresqlPhysical) {
- database.postgresqlPhysical.username = response.username;
- database.postgresqlPhysical.password = response.password;
- } else if (isLogicalPostgres && database.postgresqlLogical) {
- database.postgresqlLogical.username = response.username;
- database.postgresqlLogical.password = response.password;
- } else if (isMysql && database.mysql) {
- database.mysql.username = response.username;
- database.mysql.password = response.password;
- } else if (isMariadb && database.mariadb) {
- database.mariadb.username = response.username;
- database.mariadb.password = response.password;
- } else if (isMongodb && database.mongodb) {
- database.mongodb.username = response.username;
- database.mongodb.password = response.password;
+ if (isPhysicalPostgres) {
+ await provisionAndApplyReplicationOnlyUserCredentials();
+ } else {
+ await provisionAndApplyReadOnlyUserCredentials();
}
onReadOnlyUserUpdated(database);
@@ -209,7 +232,7 @@ export const CreateReadOnlyComponent = ({
<Button
type="primary"
- onClick={createReadOnlyUser}
+ onClick={provisionRestrictedUser}
loading={isCreatingReadOnlyUser}
disabled={isCreatingReadOnlyUser}
>