|
| 1 | +define postgresql::server::default_privileges ( |
| 2 | + String $role, |
| 3 | + String $db, |
| 4 | + String $privilege, |
| 5 | + Pattern[ |
| 6 | + /(?i:^FUNCTIONS$)/, |
| 7 | + /(?i:^ROUTINES$)/, |
| 8 | + /(?i:^SEQUENCES$)/, |
| 9 | + /(?i:^TABLES$)/, |
| 10 | + /(?i:^TYPES$)/ |
| 11 | + ] $object_type, |
| 12 | + String $schema = 'public', |
| 13 | + String $psql_db = $postgresql::server::default_database, |
| 14 | + String $psql_user = $postgresql::server::user, |
| 15 | + Integer $port = $postgresql::server::port, |
| 16 | + Boolean $onlyif_exists = false, |
| 17 | + Hash $connect_settings = $postgresql::server::default_connect_settings, |
| 18 | + Enum['present', |
| 19 | + 'absent' |
| 20 | + ] $ensure = 'present', |
| 21 | + String $group = $postgresql::server::group, |
| 22 | + String $psql_path = $postgresql::server::psql_path, |
| 23 | +) { |
| 24 | + |
| 25 | + # If possible use the version of the remote database, otherwise |
| 26 | + # fallback to our local DB version |
| 27 | + if $connect_settings != undef and has_key( $connect_settings, 'DBVERSION') { |
| 28 | + $version = $connect_settings['DBVERSION'] |
| 29 | + } else { |
| 30 | + $version = $postgresql::server::_version |
| 31 | + } |
| 32 | + |
| 33 | + if (versioncmp($version, '9.6') == -1) { |
| 34 | + fail 'Default_privileges is only useable with PostgreSQL >= 9.6' |
| 35 | + } |
| 36 | + |
| 37 | + case $ensure { |
| 38 | + default: { |
| 39 | + # default is 'present' |
| 40 | + $sql_command = 'ALTER DEFAULT PRIVILEGES IN SCHEMA %s GRANT %s ON %s TO "%s"' |
| 41 | + $unless_is = true |
| 42 | + } |
| 43 | + 'absent': { |
| 44 | + $sql_command = 'ALTER DEFAULT PRIVILEGES IN SCHEMA %s REVOKE %s ON %s FROM "%s"' |
| 45 | + $unless_is = false |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + # |
| 50 | + # Port, order of precedence: $port parameter, $connect_settings[PGPORT], $postgresql::server::port |
| 51 | + # |
| 52 | + if $port != undef { |
| 53 | + $port_override = $port |
| 54 | + } elsif $connect_settings != undef and has_key( $connect_settings, 'PGPORT') { |
| 55 | + $port_override = undef |
| 56 | + } else { |
| 57 | + $port_override = $postgresql::server::port |
| 58 | + } |
| 59 | + |
| 60 | + ## Munge the input values |
| 61 | + $_object_type = upcase($object_type) |
| 62 | + $_privilege = upcase($privilege) |
| 63 | + |
| 64 | + case $_object_type { |
| 65 | + # Routines and functions ends up with the same definition |
| 66 | + Pattern[ |
| 67 | + /^ROUTINES$/, |
| 68 | + /^FUNCTIONS$/, |
| 69 | + ]: { |
| 70 | + case $_privilege { |
| 71 | + Pattern[ |
| 72 | + /^ALL$/, |
| 73 | + /^EXECUTE$/, |
| 74 | + ]: { |
| 75 | + $_check_privilege = 'X' |
| 76 | + } |
| 77 | + default: { fail('Illegal value for $privilege parameter') } |
| 78 | + } |
| 79 | + $_check_type = 'f' |
| 80 | + } |
| 81 | + 'SEQUENCES': { |
| 82 | + case $_privilege { |
| 83 | + /^(ALL)$/: { $_check_privilege = 'rwU' } |
| 84 | + /^SELECT$/: { $_check_privilege = 'r'} |
| 85 | + /^UPDATE$/: { $_check_privilege = 'w'} |
| 86 | + /^USAGE$/: { $_check_privilege = 'U'} |
| 87 | + default: { fail('Illegal value for $privilege parameter') } |
| 88 | + } |
| 89 | + $_check_type = 'S' |
| 90 | + } |
| 91 | + 'TABLES': { |
| 92 | + case $_privilege { |
| 93 | + /^ALL$/: { $_check_privilege = 'arwdDxt' } |
| 94 | + /^DELETE$/: { $_check_privilege = 'd' } |
| 95 | + /^INSERT$/: { $_check_privilege = 'a' } |
| 96 | + /^REFERENCES$/: { $_check_privilege = 'x' } |
| 97 | + /^SELECT$/: { $_check_privilege = 'r' } |
| 98 | + /^TRIGGER$/: { $_check_privilege = 'd' } |
| 99 | + /^TRUNCATE$/: { $_check_privilege = 'D' } |
| 100 | + /^UPDATE$/: { $_check_privilege = 'w' } |
| 101 | + default: { fail('Illegal value for $privilege parameter') } |
| 102 | + } |
| 103 | + $_check_type = 'r' |
| 104 | + } |
| 105 | + 'TYPES': { |
| 106 | + case $_privilege { |
| 107 | + /^(ALL|USAGE)$/: { $_check_privilege = 'U'} |
| 108 | + default: { fail('Illegal value for $privilege parameter') } |
| 109 | + } |
| 110 | + $_check_type = 'T' |
| 111 | + } |
| 112 | + default: { |
| 113 | + fail("Missing privilege validation for object type ${_object_type}") |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + $_unless = $ensure ? { |
| 118 | + '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')", |
| 119 | + 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')" |
| 120 | + } |
| 121 | + |
| 122 | + $unless_cmd = sprintf($_unless, $role, $_check_privilege, $schema, $_check_type) |
| 123 | + $grant_cmd = sprintf($sql_command, $schema, $_privilege, $_object_type, $role) |
| 124 | + |
| 125 | + postgresql_psql { "default_privileges:${name}": |
| 126 | + command => $grant_cmd, |
| 127 | + db => $db, |
| 128 | + port => $port_override, |
| 129 | + connect_settings => $connect_settings, |
| 130 | + psql_user => $psql_user, |
| 131 | + psql_group => $group, |
| 132 | + psql_path => $psql_path, |
| 133 | + unless => $unless_cmd, |
| 134 | + environment => "PGOPTIONS=--client-min-messages=error" |
| 135 | + } |
| 136 | + |
| 137 | + if($role != undef and defined(Postgresql::Server::Role[$role])) { |
| 138 | + Postgresql::Server::Role[$role]->Postgresql_psql["default_privileges:${name}"] |
| 139 | + } |
| 140 | + |
| 141 | + if($db != undef and defined(Postgresql::Server::Database[$db])) { |
| 142 | + Postgresql::Server::Database[$db]->Postgresql_psql["default_privileges:${name}"] |
| 143 | + } |
| 144 | +} |
0 commit comments