Skip to content

Commit aef405c

Browse files
Benoît Marcelinmattbostock
authored andcommitted
Add support for GRANT SCHEMA and ALL TABLES IN SCHEMA
1 parent 9278954 commit aef405c

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

manifests/server/grant.pp

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424
#'FOREIGN DATA WRAPPER',
2525
#'FUNCTION',
2626
#'PROCEDURAL LANGUAGE',
27-
#'SCHEMA',
27+
'SCHEMA',
2828
#'SEQUENCE',
2929
'TABLE',
30+
'ALL TABLES IN SCHEMA',
3031
#'TABLESPACE',
3132
#'VIEW',
3233
)
34+
# You can use ALL TABLES IN SCHEMA by passing schema_name to object_name
3335

3436
## Validate that the object type's privilege is acceptable
3537
# TODO: this is a terrible hack; if they pass "ALL" as the desired privilege,
@@ -43,14 +45,25 @@
4345
case $_object_type {
4446
'DATABASE': {
4547
$unless_privilege = $_privilege ? {
46-
'ALL' => 'CREATE',
47-
default => $_privilege,
48+
'ALL' => 'CREATE',
49+
'ALL PRIVILEGES' => 'CREATE',
50+
default => $_privilege,
4851
}
4952
validate_string($unless_privilege,'CREATE','CONNECT','TEMPORARY','TEMP',
5053
'ALL','ALL PRIVILEGES')
5154
$unless_function = 'has_database_privilege'
5255
$on_db = $psql_db
5356
}
57+
'SCHEMA': {
58+
$unless_privilege = $_privilege ? {
59+
'ALL' => 'CREATE',
60+
'ALL PRIVILEGES' => 'CREATE',
61+
default => $_privilege,
62+
}
63+
validate_string($_privilege, 'CREATE', 'USAGE', 'ALL', 'ALL PRIVILEGES')
64+
$unless_function = 'has_schema_privilege'
65+
$on_db = $db
66+
}
5467
'TABLE': {
5568
$unless_privilege = $_privilege ? {
5669
'ALL' => 'INSERT',
@@ -61,20 +74,50 @@
6174
$unless_function = 'has_table_privilege'
6275
$on_db = $db
6376
}
77+
'ALL TABLES IN SCHEMA': {
78+
validate_string($_privilege, 'SELECT', 'INSERT', 'UPDATE', 'REFERENCES',
79+
'ALL', 'ALL PRIVILEGES')
80+
$unless_function = false # There is no way to test it simply
81+
$on_db = $db
82+
}
6483
default: {
6584
fail("Missing privilege validation for object type ${_object_type}")
6685
}
6786
}
6887

69-
$grant_cmd = "GRANT ${_privilege} ON ${_object_type} \"${object_name}\" TO \"${role}\""
88+
# This is used to give grant to "schemaname"."tablename"
89+
# If you need such grant, use:
90+
# postgresql::grant { 'table:foo':
91+
# role => 'joe',
92+
#
93+
# object_type => 'TABLE',
94+
# object_name => [$schema, $table],
95+
# }
96+
if is_array($object_name) {
97+
$_togrant_object = join($object_name, '"."')
98+
# Never put double quotes into has_*_privilege function
99+
$_granted_object = join($object_name, '.')
100+
} else {
101+
$_granted_object = $object_name
102+
$_togrant_object = $object_name
103+
}
104+
105+
$_unless = $unless_function ? {
106+
false => undef,
107+
default => "SELECT 1 WHERE ${unless_function}('${role}',
108+
'${_granted_object}', '${unless_privilege}')",
109+
}
110+
111+
$grant_cmd = "GRANT ${_privilege} ON ${_object_type} \"${_togrant_object}\" TO
112+
\"${role}\""
70113
postgresql_psql { "grant:${name}":
71114
command => $grant_cmd,
72115
db => $on_db,
73116
port => $port,
74117
psql_user => $psql_user,
75118
psql_group => $group,
76119
psql_path => $psql_path,
77-
unless => "SELECT 1 WHERE ${unless_function}('${role}', '${object_name}', '${unless_privilege}')",
120+
unless => $_unless,
78121
require => Class['postgresql::server']
79122
}
80123

0 commit comments

Comments
 (0)