|
| 1 | +# puppet-postgresql |
| 2 | +# For all details and documentation: |
| 3 | +# http://github.com/inkling/puppet-postgresql |
| 4 | +# |
| 5 | +# Copyright 2012- Inkling Systems, Inc. |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | + |
| 19 | +# TODO: in mysql module, the grant resource name might look like this: 'user@host/dbname'; |
| 20 | +# I think that the API for the resource type should split these up, because it's |
| 21 | +# easier / safer to recombine them for mysql than it is to parse them for other |
| 22 | +# databases. Also, in the mysql module, the hostname portion of that string |
| 23 | +# affects the user's ability to connect from remote hosts. In postgres this is |
| 24 | +# managed via pg_hba.conf; not sure if we want to try to reconcile that difference |
| 25 | +# in the modules or not. |
| 26 | + |
| 27 | +define postgresql::database_grant( |
| 28 | + # TODO: mysql supports an array of privileges here. We should do that if we |
| 29 | + # port this to ruby. |
| 30 | + $privilege, |
| 31 | + $db, |
| 32 | + $role, |
| 33 | + $psql_db = 'postgres', |
| 34 | + $psql_user='postgres', |
| 35 | +) { |
| 36 | + |
| 37 | + # TODO: FIXME: only works on databases, due to using has_database_privilege |
| 38 | + |
| 39 | + # TODO: this is a terrible hack; if they pass "ALL" as the desired privilege, |
| 40 | + # we need a way to test for it--and has_database_privilege does not recognize |
| 41 | + # 'ALL' as a valid privelege name. So we probably need to hard-code a mapping |
| 42 | + # between 'ALL' and the list of actual privileges that it entails, and loop |
| 43 | + # over them to check them. That sort of thing will probably need to wait until |
| 44 | + # we port this over to ruby, so, for now, we're just going to assume that if |
| 45 | + # they have "CREATE" privileges on a database, then they have "ALL". (I told |
| 46 | + # you that it was terrible!) |
| 47 | + $unless_privilege = $privilege ? { |
| 48 | + 'ALL' => 'CREATE', |
| 49 | + default => $privilege, |
| 50 | + } |
| 51 | + |
| 52 | + postgresql::psql {"GRANT $privilege ON database $db TO $role": |
| 53 | + db => $psql_db, |
| 54 | + user => $psql_user, |
| 55 | + unless => "SELECT 1 WHERE has_database_privilege('$role', '$db', '$unless_privilege')", |
| 56 | + } |
| 57 | +} |
| 58 | + |
0 commit comments