Remove code duplication for permission checks with replication slots
Two functions, both named check_permissions(), used the same checks to verify if a user had required privileges to work on replication slots. This commit removes the duplication, and moves the function doing the checks to slot.c to be centralized. Author: Bharath Rupireddy Reviewed-by: Nathan Bossart, Euler Taveira Discussion: https://postgr.es/m/CALj2ACUPpVw1u7sQocFVWrSs0n10pt_G_4NPZKSxXK6cW1dErw@mail.gmail.com
This commit is contained in:
parent
138531f1bb
commit
026ed8efd6
@ -95,15 +95,6 @@ LogicalOutputWrite(LogicalDecodingContext *ctx, XLogRecPtr lsn, TransactionId xi
|
|||||||
p->returned_rows++;
|
p->returned_rows++;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
|
||||||
check_permissions(void)
|
|
||||||
{
|
|
||||||
if (!superuser() && !has_rolreplication(GetUserId()))
|
|
||||||
ereport(ERROR,
|
|
||||||
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
||||||
errmsg("must be superuser or replication role to use replication slots")));
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Helper function for the various SQL callable logical decoding functions.
|
* Helper function for the various SQL callable logical decoding functions.
|
||||||
*/
|
*/
|
||||||
@ -124,7 +115,7 @@ pg_logical_slot_get_changes_guts(FunctionCallInfo fcinfo, bool confirm, bool bin
|
|||||||
List *options = NIL;
|
List *options = NIL;
|
||||||
DecodingOutputState *p;
|
DecodingOutputState *p;
|
||||||
|
|
||||||
check_permissions();
|
CheckSlotPermissions();
|
||||||
|
|
||||||
CheckLogicalDecodingRequirements();
|
CheckLogicalDecodingRequirements();
|
||||||
|
|
||||||
|
@ -1059,6 +1059,18 @@ CheckSlotRequirements(void)
|
|||||||
errmsg("replication slots can only be used if wal_level >= replica")));
|
errmsg("replication slots can only be used if wal_level >= replica")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Check whether the user has privilege to use replication slots.
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
CheckSlotPermissions(void)
|
||||||
|
{
|
||||||
|
if (!superuser() && !has_rolreplication(GetUserId()))
|
||||||
|
ereport(ERROR,
|
||||||
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
||||||
|
errmsg("must be superuser or replication role to use replication slots")));
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Reserve WAL for the currently active slot.
|
* Reserve WAL for the currently active slot.
|
||||||
*
|
*
|
||||||
|
@ -25,15 +25,6 @@
|
|||||||
#include "utils/pg_lsn.h"
|
#include "utils/pg_lsn.h"
|
||||||
#include "utils/resowner.h"
|
#include "utils/resowner.h"
|
||||||
|
|
||||||
static void
|
|
||||||
check_permissions(void)
|
|
||||||
{
|
|
||||||
if (!superuser() && !has_rolreplication(GetUserId()))
|
|
||||||
ereport(ERROR,
|
|
||||||
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
||||||
errmsg("must be superuser or replication role to use replication slots")));
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Helper function for creating a new physical replication slot with
|
* Helper function for creating a new physical replication slot with
|
||||||
* given arguments. Note that this function doesn't release the created
|
* given arguments. Note that this function doesn't release the created
|
||||||
@ -85,7 +76,7 @@ pg_create_physical_replication_slot(PG_FUNCTION_ARGS)
|
|||||||
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
|
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
|
||||||
elog(ERROR, "return type must be a row type");
|
elog(ERROR, "return type must be a row type");
|
||||||
|
|
||||||
check_permissions();
|
CheckSlotPermissions();
|
||||||
|
|
||||||
CheckSlotRequirements();
|
CheckSlotRequirements();
|
||||||
|
|
||||||
@ -188,7 +179,7 @@ pg_create_logical_replication_slot(PG_FUNCTION_ARGS)
|
|||||||
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
|
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
|
||||||
elog(ERROR, "return type must be a row type");
|
elog(ERROR, "return type must be a row type");
|
||||||
|
|
||||||
check_permissions();
|
CheckSlotPermissions();
|
||||||
|
|
||||||
CheckLogicalDecodingRequirements();
|
CheckLogicalDecodingRequirements();
|
||||||
|
|
||||||
@ -224,7 +215,7 @@ pg_drop_replication_slot(PG_FUNCTION_ARGS)
|
|||||||
{
|
{
|
||||||
Name name = PG_GETARG_NAME(0);
|
Name name = PG_GETARG_NAME(0);
|
||||||
|
|
||||||
check_permissions();
|
CheckSlotPermissions();
|
||||||
|
|
||||||
CheckSlotRequirements();
|
CheckSlotRequirements();
|
||||||
|
|
||||||
@ -619,7 +610,7 @@ pg_replication_slot_advance(PG_FUNCTION_ARGS)
|
|||||||
|
|
||||||
Assert(!MyReplicationSlot);
|
Assert(!MyReplicationSlot);
|
||||||
|
|
||||||
check_permissions();
|
CheckSlotPermissions();
|
||||||
|
|
||||||
if (XLogRecPtrIsInvalid(moveto))
|
if (XLogRecPtrIsInvalid(moveto))
|
||||||
ereport(ERROR,
|
ereport(ERROR,
|
||||||
@ -718,7 +709,7 @@ copy_replication_slot(FunctionCallInfo fcinfo, bool logical_slot)
|
|||||||
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
|
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
|
||||||
elog(ERROR, "return type must be a row type");
|
elog(ERROR, "return type must be a row type");
|
||||||
|
|
||||||
check_permissions();
|
CheckSlotPermissions();
|
||||||
|
|
||||||
if (logical_slot)
|
if (logical_slot)
|
||||||
CheckLogicalDecodingRequirements();
|
CheckLogicalDecodingRequirements();
|
||||||
|
@ -222,5 +222,6 @@ extern void StartupReplicationSlots(void);
|
|||||||
extern void CheckPointReplicationSlots(void);
|
extern void CheckPointReplicationSlots(void);
|
||||||
|
|
||||||
extern void CheckSlotRequirements(void);
|
extern void CheckSlotRequirements(void);
|
||||||
|
extern void CheckSlotPermissions(void);
|
||||||
|
|
||||||
#endif /* SLOT_H */
|
#endif /* SLOT_H */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user