|
1 | 1 | # @summary Manage a database defaults privileges. Only works with PostgreSQL version 9.6 and above.
|
2 | 2 | #
|
| 3 | +# @param target_role Target role whose created objects will receive the default privileges. Defaults to the current user. |
3 | 4 | # @param ensure Specifies whether to grant or revoke the privilege.
|
4 | 5 | # @param role Specifies the role or user whom you are granting access to.
|
5 | 6 | # @param db Specifies the database to which you are granting access.
|
|
13 | 14 | # @param connect_settings Specifies a hash of environment variables used when connecting to a remote server.
|
14 | 15 | # @param psql_path Specifies the path to the psql command.
|
15 | 16 | define postgresql::server::default_privileges (
|
| 17 | + Optional[String] $target_role = undef, |
16 | 18 | String $role,
|
17 | 19 | String $db,
|
18 | 20 | String $privilege,
|
|
50 | 52 | case $ensure {
|
51 | 53 | default: {
|
52 | 54 | # default is 'present'
|
53 |
| - $sql_command = 'ALTER DEFAULT PRIVILEGES IN SCHEMA %s GRANT %s ON %s TO "%s"' |
| 55 | + $sql_command = 'ALTER DEFAULT PRIVILEGES%s IN SCHEMA %s GRANT %s ON %s TO "%s"' |
54 | 56 | $unless_is = true
|
55 | 57 | }
|
56 | 58 | 'absent': {
|
57 |
| - $sql_command = 'ALTER DEFAULT PRIVILEGES IN SCHEMA %s REVOKE %s ON %s FROM "%s"' |
| 59 | + $sql_command = 'ALTER DEFAULT PRIVILEGES%s IN SCHEMA %s REVOKE %s ON %s FROM "%s"' |
58 | 60 | $unless_is = false
|
59 | 61 | }
|
60 | 62 | }
|
|
70 | 72 | $port_override = $postgresql::server::port
|
71 | 73 | }
|
72 | 74 |
|
| 75 | + if $target_role != undef { |
| 76 | + $_target_role = " FOR ROLE $target_role" |
| 77 | + $_check_target_role = "/$target_role" |
| 78 | + } else { |
| 79 | + $_target_role = '' |
| 80 | + $_check_target_role = '' |
| 81 | + } |
| 82 | + |
73 | 83 | ## Munge the input values
|
74 | 84 | $_object_type = upcase($object_type)
|
75 | 85 | $_privilege = upcase($privilege)
|
|
128 | 138 | }
|
129 | 139 |
|
130 | 140 | $_unless = $ensure ? {
|
131 |
| - 'absent' => "SELECT 1 WHERE NOT EXISTS (SELECT * FROM pg_default_acl AS da JOIN pg_namespace AS n ON da.defaclnamespace = n.oid WHERE '%s=%s' = ANY (defaclacl) AND nspname = '%s' and defaclobjtype = '%s')", |
132 |
| - default => "SELECT 1 WHERE EXISTS (SELECT * FROM pg_default_acl AS da JOIN pg_namespace AS n ON da.defaclnamespace = n.oid WHERE '%s=%s' = ANY (defaclacl) AND nspname = '%s' and defaclobjtype = '%s')" |
| 141 | + 'absent' => "SELECT 1 WHERE NOT EXISTS (SELECT * FROM pg_default_acl AS da JOIN pg_namespace AS n ON da.defaclnamespace = n.oid WHERE '%s=%s%s' = ANY (defaclacl) AND nspname = '%s' and defaclobjtype = '%s')", |
| 142 | + default => "SELECT 1 WHERE EXISTS (SELECT * FROM pg_default_acl AS da JOIN pg_namespace AS n ON da.defaclnamespace = n.oid WHERE '%s=%s%s' = ANY (defaclacl) AND nspname = '%s' and defaclobjtype = '%s')" |
133 | 143 | }
|
134 | 144 |
|
135 |
| - $unless_cmd = sprintf($_unless, $role, $_check_privilege, $schema, $_check_type) |
136 |
| - $grant_cmd = sprintf($sql_command, $schema, $_privilege, $_object_type, $role) |
| 145 | + $unless_cmd = sprintf($_unless, $role, $_check_privilege, $_check_target_role, $schema, $_check_type) |
| 146 | + $grant_cmd = sprintf($sql_command, $_target_role, $schema, $_privilege, $_object_type, $role) |
137 | 147 |
|
138 | 148 | postgresql_psql { "default_privileges:${name}":
|
139 | 149 | command => $grant_cmd,
|
|
0 commit comments