salsa: more mail setup
[mirror/dsa-puppet.git] / 3rdparty / modules / postgresql / manifests / grant.pp
1 # Resource postgresql::grant
2 #
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 (
11   $role,
12   $db,
13   # TODO: mysql supports an array of privileges here.  We should do that if we
14   #  port this to ruby.
15   $privilege   = undef,
16   $object_type = 'database',
17   $object_name = $db,
18   $psql_db     = $postgresql::params::user,
19   $psql_user   = $postgresql::params::user
20 ) {
21
22   ## Munge the input values
23   $_object_type = upcase($object_type)
24   $_privilege   = upcase($privilege)
25
26   ## Validate that the object type is known
27   validate_string($_object_type,
28     #'COLUMN',
29     'DATABASE',
30     #'FOREIGN SERVER',
31     #'FOREIGN DATA WRAPPER',
32     #'FUNCTION',
33     #'PROCEDURAL LANGUAGE',
34     #'SCHEMA',
35     #'SEQUENCE',
36     'TABLE',
37     #'TABLESPACE',
38     #'VIEW',
39   )
40
41   ## Validate that the object type's privilege is acceptable
42   case $_object_type {
43     'DATABASE': {
44       validate_string($_privilege,'CREATE','CONNECT','TEMPORARY','TEMP','ALL','ALL PRIVILEGES')
45       $unless_function = 'has_database_privilege'
46       $on_db = $psql_db
47     }
48     'TABLE': {
49       validate_string($_privilege,'SELECT','INSERT','UPDATE','REFERENCES','ALL','ALL PRIVILEGES')
50       $unless_function = 'has_table_privilege'
51       $on_db = $db
52     }
53     default: {
54       fail("Missing privilege validation for object type ${_object_type}")
55     }
56   }
57
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 ? {
67     'ALL'   => 'CREATE',
68     default => $_privilege,
69   }
70   postgresql_psql { "GRANT ${_privilege} ON ${_object_type} \"${object_name}\" TO \"${role}\"":
71     db         => $on_db,
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}')",
76   }
77 }