database_grant.pp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. # TODO: in mysql module, the grant resource name might look like this: 'user@host/dbname';
  19. # I think that the API for the resource type should split these up, because it's
  20. # easier / safer to recombine them for mysql than it is to parse them for other
  21. # databases. Also, in the mysql module, the hostname portion of that string
  22. # affects the user's ability to connect from remote hosts. In postgres this is
  23. # managed via pg_hba.conf; not sure if we want to try to reconcile that difference
  24. # in the modules or not.
  25. define postgresql::database_grant(
  26. # TODO: mysql supports an array of privileges here. We should do that if we
  27. # port this to ruby.
  28. $privilege,
  29. $db,
  30. $role,
  31. $psql_db = 'postgres',
  32. $psql_user ='postgres'
  33. ) {
  34. include postgresql::params
  35. # TODO: FIXME: only works on databases, due to using has_database_privilege
  36. # TODO: this is a terrible hack; if they pass "ALL" as the desired privilege,
  37. # we need a way to test for it--and has_database_privilege does not recognize
  38. # 'ALL' as a valid privilege name. So we probably need to hard-code a mapping
  39. # between 'ALL' and the list of actual privileges that it entails, and loop
  40. # over them to check them. That sort of thing will probably need to wait until
  41. # we port this over to ruby, so, for now, we're just going to assume that if
  42. # they have "CREATE" privileges on a database, then they have "ALL". (I told
  43. # you that it was terrible!)
  44. $unless_privilege = $privilege ? {
  45. 'ALL' => 'CREATE',
  46. default => $privilege,
  47. }
  48. postgresql_psql {"GRANT ${privilege} ON database ${db} TO ${role}":
  49. db => $psql_db,
  50. psql_user => $psql_user,
  51. unless => "SELECT 1 WHERE has_database_privilege('${role}', '${db}', '${unless_privilege}')",
  52. cwd => $postgresql::params::datadir,
  53. }
  54. }