You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ALTER DEFAULT PRIVILEGES supports the FOR ROLE <target_role> argument,
without which the statement applies only to objects created by the
*current* role, which may not be most useful.
Support specifying the target role.
Copy file name to clipboardExpand all lines: manifests/server/default_privileges.pp
+16-6Lines changed: 16 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,6 @@
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,6 +14,7 @@
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
definepostgresql::server::default_privileges (
17
+
Optional[String] $target_role=undef,
16
18
String $role,
17
19
String $db,
18
20
String $privilege,
@@ -50,11 +52,11 @@
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,6 +72,14 @@
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,12 +138,12 @@
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')"
Copy file name to clipboardExpand all lines: spec/acceptance/server/default_privileges_spec.rb
+122Lines changed: 122 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -68,6 +68,105 @@ class { 'postgresql::server': }
68
68
MANIFEST
69
69
end
70
70
71
+
let(:target_user){'target_role_user'}
72
+
let(:target_password){'target_role_password'}
73
+
74
+
let(:target_check_command)do
75
+
"SELECT 1 FROM pg_default_acl a LEFT JOIN pg_namespace AS b ON a.defaclnamespace = b.oid WHERE '#{user}=arwdDxt/#{target_user}' = ANY (defaclacl) AND nspname = 'public' AND defaclobjtype = 'r';"
76
+
end
77
+
78
+
let(:pp_target_role)do
79
+
<<-MANIFEST.unindent
80
+
$db = #{db}
81
+
$user = #{user}
82
+
$group = #{group}
83
+
$password = #{password}
84
+
$target_user = #{target_user}
85
+
$target_password = #{target_password}
86
+
87
+
user {$user:
88
+
ensure => present,
89
+
}
90
+
postgresql::server::database_grant { "allow connect for ${user}":
.with_command('ALTER DEFAULT PRIVILEGES FOR ROLE target IN SCHEMA public GRANT ALL ON TABLES TO "test"')
281
+
.with_unless("SELECT 1 WHERE EXISTS (SELECT * FROM pg_default_acl AS da JOIN pg_namespace AS n ON da.defaclnamespace = n.oid WHERE 'test=arwdDxt/target' = ANY (defaclacl) AND nspname = 'public' and defaclobjtype = 'r')")
0 commit comments