1 # Resource postgresql::grant
3 # TODO: in mysql module, the grant resource name might look like this: 'user@host/dbname';
4 # I think that the API for the resource type should split these up, because it's
5 # easier / safer to recombine them for mysql than it is to parse them for other
6 # databases. Also, in the mysql module, the hostname portion of that string
7 # affects the user's ability to connect from remote hosts. In postgres this is
8 # managed via pg_hba.conf; not sure if we want to try to reconcile that difference
9 # in the modules or not.
10 define postgresql::grant (
13 # TODO: mysql supports an array of privileges here. We should do that if we
16 $object_type = 'database',
18 $psql_db = $postgresql::params::user,
19 $psql_user = $postgresql::params::user
22 ## Munge the input values
23 $_object_type = upcase($object_type)
24 $_privilege = upcase($privilege)
26 ## Validate that the object type is known
27 validate_string($_object_type,
31 #'FOREIGN DATA WRAPPER',
33 #'PROCEDURAL LANGUAGE',
41 ## Validate that the object type's privilege is acceptable
44 validate_string($_privilege,'CREATE','CONNECT','TEMPORARY','TEMP','ALL','ALL PRIVILEGES')
45 $unless_function = 'has_database_privilege'
49 validate_string($_privilege,'SELECT','INSERT','UPDATE','REFERENCES','ALL','ALL PRIVILEGES')
50 $unless_function = 'has_table_privilege'
54 fail("Missing privilege validation for object type ${_object_type}")
58 # TODO: this is a terrible hack; if they pass "ALL" as the desired privilege,
59 # we need a way to test for it--and has_database_privilege does not recognize
60 # 'ALL' as a valid privilege name. So we probably need to hard-code a mapping
61 # between 'ALL' and the list of actual privileges that it entails, and loop
62 # over them to check them. That sort of thing will probably need to wait until
63 # we port this over to ruby, so, for now, we're just going to assume that if
64 # they have "CREATE" privileges on a database, then they have "ALL". (I told
65 # you that it was terrible!)
66 $unless_privilege = $_privilege ? {
68 default => $_privilege,
70 postgresql_psql { "GRANT ${_privilege} ON ${_object_type} \"${object_name}\" TO \"${role}\"":
72 psql_user => $psql_user,
73 psql_group => $postgresql::params::group,
74 psql_path => $postgresql::params::psql_path,
75 unless => "SELECT 1 WHERE ${unless_function}('${role}', '${object_name}', '${unless_privilege}')",