Skip to content

Commit 7c520bd

Browse files
author
Morgan Haskel
committed
Merge pull request puppetlabs#538 from mattbostock/groupeseb-master
Allow per-schema grants and support for 'ALL TABLES IN SCHEMA'
2 parents c7ae24c + d655024 commit 7c520bd

File tree

2 files changed

+66
-9
lines changed

2 files changed

+66
-9
lines changed

manifests/server/grant.pp

Lines changed: 52 additions & 8 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,27 +74,58 @@
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}\""
70-
postgresql_psql { $grant_cmd:
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}\""
113+
postgresql_psql { "grant:${name}":
114+
command => $grant_cmd,
71115
db => $on_db,
72116
port => $port,
73117
psql_user => $psql_user,
74118
psql_group => $group,
75119
psql_path => $psql_path,
76-
unless => "SELECT 1 WHERE ${unless_function}('${role}', '${object_name}', '${unless_privilege}')",
120+
unless => $_unless,
77121
require => Class['postgresql::server']
78122
}
79123

80124
if($role != undef and defined(Postgresql::Server::Role[$role])) {
81-
Postgresql::Server::Role[$role]->Postgresql_psql[$grant_cmd]
125+
Postgresql::Server::Role[$role]->Postgresql_psql["grant:${name}"]
82126
}
83127

84128
if($db != undef and defined(Postgresql::Server::Database[$db])) {
85-
Postgresql::Server::Database[$db]->Postgresql_psql[$grant_cmd]
129+
Postgresql::Server::Database[$db]->Postgresql_psql["grant:${name}"]
86130
}
87131
}

manifests/server/schema.pp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1-
# This defined types creates database schemas. See README.md for more details.
1+
# = Type: postgresql::server::schema
2+
#
3+
# Create a new schema. See README.md for more details.
4+
#
5+
# == Requires:
6+
#
7+
# The database must exist and the PostgreSQL user should have enough privileges
8+
#
9+
# == Sample Usage:
10+
#
11+
# postgresql::server::schema {'private':
12+
# db => 'template1',
13+
# }
14+
#
215
define postgresql::server::schema(
316
$db,
417
$owner = undef,

0 commit comments

Comments
 (0)