]> git.openstreetmap.org Git - chef.git/blob - cookbooks/postgresql/resources/user.rb
imagery: restart titiler service to workaround resource leak
[chef.git] / cookbooks / postgresql / resources / user.rb
1 #
2 # Cookbook:: postgresql
3 # Resource:: postgresql_user
4 #
5 # Copyright:: 2012, OpenStreetMap Foundation
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 # https://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
20 require "shellwords"
21
22 unified_mode true
23
24 default_action :create
25
26 property :user, :kind_of => String, :name_property => true
27 property :cluster, :kind_of => String, :required => true
28 property :password, :kind_of => String
29 property :superuser, :kind_of => [TrueClass, FalseClass], :default => false
30 property :createdb, :kind_of => [TrueClass, FalseClass], :default => false
31 property :createrole, :kind_of => [TrueClass, FalseClass], :default => false
32 property :replication, :kind_of => [TrueClass, FalseClass], :default => false
33 property :roles, :kind_of => [String, Array]
34
35 action :create do
36   password = new_resource.password ? "ENCRYPTED PASSWORD '#{new_resource.password.shellescape}'" : ""
37   superuser = new_resource.superuser ? "SUPERUSER" : "NOSUPERUSER"
38   createdb = new_resource.createdb ? "CREATEDB" : "NOCREATEDB"
39   createrole = new_resource.createrole ? "CREATEROLE" : "NOCREATEROLE"
40   replication = new_resource.replication ? "REPLICATION" : "NOREPLICATION"
41
42   if !cluster.users.include?(new_resource.user)
43     converge_by "create role #{new_resource.user}" do
44       cluster.execute(:command => "CREATE ROLE \"#{new_resource.user}\" LOGIN #{password} #{superuser} #{createdb} #{createrole}")
45     end
46
47     Array(new_resource.roles).each do |role|
48       converge_by "grant #{role} to #{new_resource.user}" do
49         cluster.execute(:command => "GRANT \"#{role}\" TO \"#{new_resource.user}\"")
50       end
51     end
52   else
53     current_user = cluster.users[new_resource.user]
54
55     if new_resource.superuser != current_user[:superuser]
56       converge_by "alter role #{new_resource.user}" do
57         cluster.execute(:command => "ALTER ROLE \"#{new_resource.user}\" #{superuser}")
58       end
59     end
60
61     unless new_resource.superuser
62       if new_resource.createdb != current_user[:createdb]
63         converge_by "alter role #{new_resource.user}" do
64           cluster.execute(:command => "ALTER ROLE \"#{new_resource.user}\" #{createdb}")
65         end
66       end
67
68       if new_resource.createrole != current_user[:createrole]
69         converge_by "alter role #{new_resource.user}" do
70           cluster.execute(:command => "ALTER ROLE \"#{new_resource.user}\" #{createrole}")
71         end
72       end
73
74       if new_resource.replication != current_user[:replication]
75         converge_by "alter role #{new_resource.user}" do
76           cluster.execute(:command => "ALTER ROLE \"#{new_resource.user}\" #{replication}")
77         end
78       end
79     end
80
81     roles = Array(new_resource.roles)
82
83     roles.each do |role|
84       next if current_user[:roles].include?(role)
85
86       converge_by "grant #{role} to #{new_resource.user}" do
87         cluster.execute(:command => "GRANT \"#{role}\" TO \"#{new_resource.user}\"")
88       end
89     end
90
91     current_user[:roles].each do |role|
92       next if roles.include?(role)
93
94       converge_by "revoke #{role} from #{new_resource.user}" do
95         cluster.execute(:command => "REVOKE \"#{role}\" FROM \"#{new_resource.user}\"")
96       end
97     end
98   end
99 end
100
101 action :drop do
102   if cluster.users.include?(new_resource.user)
103     converge_by "drop role #{new_resource.user}" do
104       cluster.execute(:command => "DROP ROLE \"#{new_resource.user}\"")
105     end
106   end
107 end
108
109 action_class do
110   def cluster
111     @cluster ||= OpenStreetMap::PostgreSQL.new(new_resource.cluster)
112   end
113 end