Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit de4d456

Browse files
committed
Improve several permission-related error messages.
Mainly move some detail from errmsg to errdetail, remove explicit mention of superuser where appropriate, since that is implied in most permission checks, and make messages more uniform. Author: Nathan Bossart <nathandbossart@gmail.com> Discussion: https://www.postgresql.org/message-id/20230316234701.GA903298@nathanxps13
1 parent 39a3bdc commit de4d456

File tree

18 files changed

+282
-109
lines changed

18 files changed

+282
-109
lines changed

contrib/file_fdw/expected/file_fdw.out

+2-1
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,8 @@ ALTER FOREIGN TABLE agg_text OWNER TO regress_file_fdw_user;
491491
ALTER FOREIGN TABLE agg_text OPTIONS (SET format 'text');
492492
SET ROLE regress_file_fdw_user;
493493
ALTER FOREIGN TABLE agg_text OPTIONS (SET format 'text');
494-
ERROR: only superuser or a role with privileges of the pg_read_server_files role may specify the filename option of a file_fdw foreign table
494+
ERROR: permission denied to set the "filename" option of a file_fdw foreign table
495+
DETAIL: Only roles with privileges of the "pg_read_server_files" role may set this option.
495496
SET ROLE regress_file_fdw_superuser;
496497
-- cleanup
497498
RESET ROLE;

contrib/file_fdw/file_fdw.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,19 @@ file_fdw_validator(PG_FUNCTION_ARGS)
279279
!has_privs_of_role(GetUserId(), ROLE_PG_READ_SERVER_FILES))
280280
ereport(ERROR,
281281
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
282-
errmsg("only superuser or a role with privileges of the pg_read_server_files role may specify the filename option of a file_fdw foreign table")));
282+
errmsg("permission denied to set the \"%s\" option of a file_fdw foreign table",
283+
"filename"),
284+
errdetail("Only roles with privileges of the \"%s\" role may set this option.",
285+
"pg_read_server_files")));
283286

284287
if (strcmp(def->defname, "program") == 0 &&
285288
!has_privs_of_role(GetUserId(), ROLE_PG_EXECUTE_SERVER_PROGRAM))
286289
ereport(ERROR,
287290
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
288-
errmsg("only superuser or a role with privileges of the pg_execute_server_program role may specify the program option of a file_fdw foreign table")));
291+
errmsg("permission denied to set the \"%s\" option of a file_fdw foreign table",
292+
"program"),
293+
errdetail("Only roles with privileges of the \"%s\" role may set this option.",
294+
"pg_execute_server_program")));
289295

290296
filename = defGetString(def);
291297
}

contrib/test_decoding/expected/permissions.out

+8-4
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,16 @@ RESET ROLE;
5454
-- plain user *can't* can control replication
5555
SET ROLE regress_lr_normal;
5656
SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot', 'test_decoding');
57-
ERROR: must be superuser or replication role to use replication slots
57+
ERROR: permission denied to use replication slots
58+
DETAIL: Only roles with the REPLICATION attribute may use replication slots.
5859
INSERT INTO lr_test VALUES('lr_superuser_init');
5960
ERROR: permission denied for table lr_test
6061
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
61-
ERROR: must be superuser or replication role to use replication slots
62+
ERROR: permission denied to use replication slots
63+
DETAIL: Only roles with the REPLICATION attribute may use replication slots.
6264
SELECT pg_drop_replication_slot('regression_slot');
63-
ERROR: must be superuser or replication role to use replication slots
65+
ERROR: permission denied to use replication slots
66+
DETAIL: Only roles with the REPLICATION attribute may use replication slots.
6467
RESET ROLE;
6568
-- replication users can drop superuser created slots
6669
SET ROLE regress_lr_superuser;
@@ -90,7 +93,8 @@ SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot', 'test_d
9093
RESET ROLE;
9194
SET ROLE regress_lr_normal;
9295
SELECT pg_drop_replication_slot('regression_slot');
93-
ERROR: must be superuser or replication role to use replication slots
96+
ERROR: permission denied to use replication slots
97+
DETAIL: Only roles with the REPLICATION attribute may use replication slots.
9498
RESET ROLE;
9599
-- all users can see existing slots
96100
SET ROLE regress_lr_superuser;

src/backend/backup/basebackup_server.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ bbsink_server_new(bbsink *next, char *pathname)
7272
if (!has_privs_of_role(GetUserId(), ROLE_PG_WRITE_SERVER_FILES))
7373
ereport(ERROR,
7474
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
75-
errmsg("must be superuser or a role with privileges of the pg_write_server_files role to create backup stored on server")));
75+
errmsg("permission denied to create backup stored on server"),
76+
errdetail("Only roles with privileges of the \"%s\" role may create a backup stored on the server.",
77+
"pg_write_server_files")));
7678
CommitTransactionCommand();
7779

7880
/*

src/backend/catalog/objectaddress.c

+11-5
Original file line numberDiff line numberDiff line change
@@ -2547,20 +2547,26 @@ check_object_ownership(Oid roleid, ObjectType objtype, ObjectAddress address,
25472547
if (!superuser_arg(roleid))
25482548
ereport(ERROR,
25492549
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
2550-
errmsg("must be superuser")));
2550+
errmsg("permission denied"),
2551+
errdetail("The current user must have the %s attribute.",
2552+
"SUPERUSER")));
25512553
}
25522554
else
25532555
{
25542556
if (!has_createrole_privilege(roleid))
25552557
ereport(ERROR,
25562558
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
2557-
errmsg("must have CREATEROLE privilege")));
2559+
errmsg("permission denied"),
2560+
errdetail("The current user must have the %s attribute.",
2561+
"CREATEROLE")));
25582562
if (!is_admin_of_role(roleid, address.objectId))
25592563
ereport(ERROR,
25602564
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
2561-
errmsg("must have admin option on role \"%s\"",
2562-
GetUserNameFromId(address.objectId,
2563-
true))));
2565+
errmsg("permission denied"),
2566+
errdetail("The current user must have the %s option on role \"%s\".",
2567+
"ADMIN",
2568+
GetUserNameFromId(address.objectId,
2569+
true))));
25642570
}
25652571
break;
25662572
case OBJECT_TSPARSER:

src/backend/commands/copy.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt,
8383
if (!has_privs_of_role(GetUserId(), ROLE_PG_EXECUTE_SERVER_PROGRAM))
8484
ereport(ERROR,
8585
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
86-
errmsg("must be superuser or have privileges of the pg_execute_server_program role to COPY to or from an external program"),
86+
errmsg("permission denied to COPY to or from an external program"),
87+
errdetail("Only roles with privileges of the \"%s\" role may COPY to or from an external program.",
88+
"pg_execute_server_program"),
8789
errhint("Anyone can COPY to stdout or from stdin. "
8890
"psql's \\copy command also works for anyone.")));
8991
}
@@ -92,14 +94,18 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt,
9294
if (is_from && !has_privs_of_role(GetUserId(), ROLE_PG_READ_SERVER_FILES))
9395
ereport(ERROR,
9496
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
95-
errmsg("must be superuser or have privileges of the pg_read_server_files role to COPY from a file"),
97+
errmsg("permission denied to COPY from a file"),
98+
errdetail("Only roles with privileges of the \"%s\" role may COPY from a file.",
99+
"pg_read_server_files"),
96100
errhint("Anyone can COPY to stdout or from stdin. "
97101
"psql's \\copy command also works for anyone.")));
98102

99103
if (!is_from && !has_privs_of_role(GetUserId(), ROLE_PG_WRITE_SERVER_FILES))
100104
ereport(ERROR,
101105
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
102-
errmsg("must be superuser or have privileges of the pg_write_server_files role to COPY to a file"),
106+
errmsg("permission denied to COPY to a file"),
107+
errdetail("Only roles with privileges of the \"%s\" role may COPY to a file.",
108+
"pg_write_server_files"),
103109
errhint("Anyone can COPY to stdout or from stdin. "
104110
"psql's \\copy command also works for anyone.")));
105111
}

0 commit comments

Comments
 (0)