PostgreSQL Source Code  git master
test_rls_hooks.c
Go to the documentation of this file.
1 /*--------------------------------------------------------------------------
2  *
3  * test_rls_hooks.c
4  * Code for testing RLS hooks.
5  *
6  * Copyright (c) 2015-2022, PostgreSQL Global Development Group
7  *
8  * IDENTIFICATION
9  * src/test/modules/test_rls_hooks/test_rls_hooks.c
10  *
11  * -------------------------------------------------------------------------
12  */
13 
14 #include "postgres.h"
15 
16 #include "catalog/pg_type.h"
17 #include "fmgr.h"
18 #include "miscadmin.h"
19 #include "nodes/makefuncs.h"
20 #include "parser/parse_clause.h"
21 #include "parser/parse_collate.h"
22 #include "parser/parse_node.h"
23 #include "parser/parse_relation.h"
24 #include "rewrite/rowsecurity.h"
25 #include "test_rls_hooks.h"
26 #include "utils/acl.h"
27 #include "utils/rel.h"
28 #include "utils/relcache.h"
29 
31 
32 /* Install hooks */
33 void
34 _PG_init(void)
35 {
36  /* Set our hooks */
39 }
40 
41 /*
42  * Return permissive policies to be added
43  */
44 List *
46 {
47  List *policies = NIL;
48  RowSecurityPolicy *policy = palloc0(sizeof(RowSecurityPolicy));
49  Datum role;
50  FuncCall *n;
51  Node *e;
52  ColumnRef *c;
53  ParseState *qual_pstate;
54  ParseNamespaceItem *nsitem;
55 
56  if (strcmp(RelationGetRelationName(relation), "rls_test_permissive") != 0 &&
57  strcmp(RelationGetRelationName(relation), "rls_test_both") != 0)
58  return NIL;
59 
60  qual_pstate = make_parsestate(NULL);
61 
62  nsitem = addRangeTableEntryForRelation(qual_pstate,
63  relation, AccessShareLock,
64  NULL, false, false);
65  addNSItemToQuery(qual_pstate, nsitem, false, true, true);
66 
68 
69  policy->policy_name = pstrdup("extension policy");
70  policy->polcmd = '*';
71  policy->roles = construct_array_builtin(&role, 1, OIDOID);
72 
73  /*
74  * policy->qual = (Expr *) makeConst(BOOLOID, -1, InvalidOid,
75  * sizeof(bool), BoolGetDatum(true), false, true);
76  */
77 
78  n = makeFuncCall(list_make2(makeString("pg_catalog"),
79  makeString("current_user")),
80  NIL,
82  -1);
83 
84  c = makeNode(ColumnRef);
85  c->fields = list_make1(makeString("username"));
86  c->location = 0;
87 
88  e = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", (Node *) n, (Node *) c, 0);
89 
90  policy->qual = (Expr *) transformWhereClause(qual_pstate, copyObject(e),
92  "POLICY");
93  /* Fix up collation information */
94  assign_expr_collations(qual_pstate, (Node *) policy->qual);
95 
96  policy->with_check_qual = copyObject(policy->qual);
97  policy->hassublinks = false;
98 
99  policies = list_make1(policy);
100 
101  return policies;
102 }
103 
104 /*
105  * Return restrictive policies to be added
106  *
107  * Note that a permissive policy must exist or the default-deny policy
108  * will be included and nothing will be visible. If no filtering should
109  * be done except for the restrictive policy, then a single "USING (true)"
110  * permissive policy can be used; see the regression tests.
111  */
112 List *
114 {
115  List *policies = NIL;
116  RowSecurityPolicy *policy = palloc0(sizeof(RowSecurityPolicy));
117  Datum role;
118  FuncCall *n;
119  Node *e;
120  ColumnRef *c;
121  ParseState *qual_pstate;
122  ParseNamespaceItem *nsitem;
123 
124  if (strcmp(RelationGetRelationName(relation), "rls_test_restrictive") != 0 &&
125  strcmp(RelationGetRelationName(relation), "rls_test_both") != 0)
126  return NIL;
127 
128  qual_pstate = make_parsestate(NULL);
129 
130  nsitem = addRangeTableEntryForRelation(qual_pstate,
131  relation, AccessShareLock,
132  NULL, false, false);
133  addNSItemToQuery(qual_pstate, nsitem, false, true, true);
134 
136 
137  policy->policy_name = pstrdup("extension policy");
138  policy->polcmd = '*';
139  policy->roles = construct_array_builtin(&role, 1, OIDOID);
140 
141  n = makeFuncCall(list_make2(makeString("pg_catalog"),
142  makeString("current_user")),
143  NIL,
145  -1);
146 
147  c = makeNode(ColumnRef);
148  c->fields = list_make1(makeString("supervisor"));
149  c->location = 0;
150 
151  e = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", (Node *) n, (Node *) c, 0);
152 
153  policy->qual = (Expr *) transformWhereClause(qual_pstate, copyObject(e),
155  "POLICY");
156  /* Fix up collation information */
157  assign_expr_collations(qual_pstate, (Node *) policy->qual);
158 
159  policy->with_check_qual = copyObject(policy->qual);
160  policy->hassublinks = false;
161 
162  policies = list_make1(policy);
163 
164  return policies;
165 }
#define ACL_ID_PUBLIC
Definition: acl.h:46
ArrayType * construct_array_builtin(Datum *elems, int nelems, Oid elmtype)
Definition: arrayfuncs.c:3338
#define AccessShareLock
Definition: lockdefs.h:36
FuncCall * makeFuncCall(List *name, List *args, CoercionForm funcformat, int location)
Definition: makefuncs.c:585
A_Expr * makeSimpleA_Expr(A_Expr_Kind kind, char *name, Node *lexpr, Node *rexpr, int location)
Definition: makefuncs.c:48
char * pstrdup(const char *in)
Definition: mcxt.c:1483
void * palloc0(Size size)
Definition: mcxt.c:1230
#define copyObject(obj)
Definition: nodes.h:233
CmdType
Definition: nodes.h:263
#define makeNode(_type_)
Definition: nodes.h:165
Node * transformWhereClause(ParseState *pstate, Node *clause, ParseExprKind exprKind, const char *constructName)
void assign_expr_collations(ParseState *pstate, Node *expr)
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:43
@ EXPR_KIND_POLICY
Definition: parse_node.h:77
ParseNamespaceItem * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, int lockmode, Alias *alias, bool inh, bool inFromCl)
void addNSItemToQuery(ParseState *pstate, ParseNamespaceItem *nsitem, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
@ AEXPR_OP
Definition: parsenodes.h:278
#define NIL
Definition: pg_list.h:66
#define list_make1(x1)
Definition: pg_list.h:210
#define list_make2(x1, x2)
Definition: pg_list.h:212
uintptr_t Datum
Definition: postgres.h:412
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:600
char * c
e
Definition: preproc-init.c:82
@ COERCE_EXPLICIT_CALL
Definition: primnodes.h:586
#define RelationGetRelationName(relation)
Definition: rel.h:535
row_security_policy_hook_type row_security_policy_hook_permissive
Definition: rowsecurity.c:96
row_security_policy_hook_type row_security_policy_hook_restrictive
Definition: rowsecurity.c:97
Definition: pg_list.h:52
Definition: nodes.h:118
ArrayType * roles
Definition: rowsecurity.h:24
Expr * with_check_qual
Definition: rowsecurity.h:27
void _PG_init(void)
PG_MODULE_MAGIC
List * test_rls_hooks_restrictive(CmdType cmdtype, Relation relation)
List * test_rls_hooks_permissive(CmdType cmdtype, Relation relation)
String * makeString(char *str)
Definition: value.c:63