Skip to content

Commit

Permalink
In pg_dump, include pg_catalog and extension ACLs, if changed
Browse files Browse the repository at this point in the history
Now that all of the infrastructure exists, add in the ability to
dump out the ACLs of the objects inside of pg_catalog or the ACLs
for objects which are members of extensions, but only if they have
been changed from their original values.

The original values are tracked in pg_init_privs.  When pg_dump'ing
9.6-and-above databases, we will dump out the ACLs for all objects
in pg_catalog and the ACLs for all extension members, where the ACL
has been changed from the original value which was set during either
initdb or CREATE EXTENSION.

This should not change dumps against pre-9.6 databases.

Reviews by Alexander Korotkov, Jose Luis Tallon
  • Loading branch information
sfrost committed Apr 7, 2016
1 parent d217b2c commit 23f34fa
Show file tree
Hide file tree
Showing 15 changed files with 1,268 additions and 209 deletions.
21 changes: 21 additions & 0 deletions doc/src/sgml/extend.sgml
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,27 @@
data; see below.)
</para>

<para>
The extension script may set privileges on objects which are part of the
extension via <command>GRANT</command> and <command>REVOKE</command>
statements. The final set of privileges for each object (if any are set)
will be stored in the
<link linkend="catalog-pg-init-privs"><structname>pg_init_privs</structname></link>
system catalog. When <application>pg_dump</> is used, the
<command>CREATE EXTENSION</> command will be included in the dump, followed
by the set of <command>GRANT</command> and <command>REVOKE</command>
statements necessary to set the privileges on the objects to what they were
at the time the dump was taken.
</para>

<para>
<productname>PostgreSQL</> does not currently support extension scripts
issuing <command>CREATE POLICY</command> or <command>SECURITY LABEL</command>
statements. These are expected to be set after the extension has been
created. All RLS policies and security labels on extension objects will be
included in dumps created by <application>pg_dump</>.
</para>

<para>
The extension mechanism also has provisions for packaging modification
scripts that adjust the definitions of the SQL objects contained in an
Expand Down
17 changes: 16 additions & 1 deletion src/backend/catalog/aclchk.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "access/htup_details.h"
#include "access/sysattr.h"
#include "access/xact.h"
#include "catalog/binary_upgrade.h"
#include "catalog/catalog.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
Expand Down Expand Up @@ -85,6 +86,12 @@ typedef struct
DropBehavior behavior;
} InternalDefaultACL;

/*
* When performing a binary-upgrade, pg_dump will call a function to set
* this variable to let us know that we need to populate the pg_init_privs
* table for the GRANT/REVOKE commands while this variable is set to true.
*/
bool binary_upgrade_record_init_privs = false;

static void ExecGrantStmt_oids(InternalGrant *istmt);
static void ExecGrant_Relation(InternalGrant *grantStmt);
Expand Down Expand Up @@ -5237,7 +5244,15 @@ recordExtensionInitPriv(Oid objoid, Oid classoid, int objsubid, Acl *new_acl)
HeapTuple tuple;
HeapTuple oldtuple;

if (!creating_extension)
/*
* Generally, we only record the initial privileges when an extension is
* being created, but because we don't actually use CREATE EXTENSION
* during binary upgrades with pg_upgrade, there is a variable to let us
* know that the GRANT and REVOKE statements being issued, while this
* variable is true, are for the initial privileges of the extension
* object and therefore we need to record them.
*/
if (!creating_extension && !binary_upgrade_record_init_privs)
return;

relation = heap_open(InitPrivsRelationId, RowExclusiveLock);
Expand Down
12 changes: 12 additions & 0 deletions src/backend/utils/adt/pg_upgrade_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Datum binary_upgrade_set_next_toast_pg_class_oid(PG_FUNCTION_ARGS);
Datum binary_upgrade_set_next_pg_enum_oid(PG_FUNCTION_ARGS);
Datum binary_upgrade_set_next_pg_authid_oid(PG_FUNCTION_ARGS);
Datum binary_upgrade_create_empty_extension(PG_FUNCTION_ARGS);
Datum binary_upgrade_set_record_init_privs(PG_FUNCTION_ARGS);


#define CHECK_IS_BINARY_UPGRADE \
Expand Down Expand Up @@ -193,3 +194,14 @@ binary_upgrade_create_empty_extension(PG_FUNCTION_ARGS)

PG_RETURN_VOID();
}

Datum
binary_upgrade_set_record_init_privs(PG_FUNCTION_ARGS)
{
bool record_init_privs = PG_GETARG_BOOL(0);

CHECK_IS_BINARY_UPGRADE;
binary_upgrade_record_init_privs = record_init_privs;

PG_RETURN_VOID();
}
6 changes: 5 additions & 1 deletion src/bin/initdb/initdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -2002,7 +2002,11 @@ setup_privileges(FILE *cmdfd)
char **priv_lines;
static char *privileges_setup[] = {
"UPDATE pg_class "
" SET relacl = E'{\"=r/\\\\\"$POSTGRES_SUPERUSERNAME\\\\\"\"}' "
" SET relacl = (SELECT array_agg(a.acl) FROM "
" (SELECT E'=r/\"$POSTGRES_SUPERUSERNAME\"' as acl "
" UNION SELECT unnest(pg_catalog.acldefault("
" CASE WHEN relkind = 'S' THEN 's' ELSE 'r' END::\"char\",10::oid))"
" ) as a) "
" WHERE relkind IN ('r', 'v', 'm', 'S') AND relacl IS NULL;\n\n",
"GRANT USAGE ON SCHEMA pg_catalog TO PUBLIC;\n\n",
"GRANT CREATE, USAGE ON SCHEMA public TO PUBLIC;\n\n",
Expand Down
Loading

0 comments on commit 23f34fa

Please sign in to comment.