]> git.openstreetmap.org Git - chef.git/blob - cookbooks/mysql/resources/user.rb
Use an equality match for TOTP validation
[chef.git] / cookbooks / mysql / resources / user.rb
1 #
2 # Cookbook:: mysql
3 # Resource:: mysql_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 unified_mode true
21
22 default_action :create
23
24 property :user, :kind_of => String, :name_property => true
25 property :password, :kind_of => String
26
27 OpenStreetMap::MySQL::USER_PRIVILEGES.each do |privilege|
28   property privilege, :default => false
29 end
30
31 action :create do
32   user = mysql_canonicalise_user(new_resource.user)
33   password = new_resource.password ? "IDENTIFIED BY '#{new_resource.password}'" : ""
34
35   unless mysql_users.include?(user)
36     converge_by("create #{new_resource}") do
37       Chef::Log.info("Creating #{new_resource}")
38       mysql_execute(:command => "CREATE USER #{user} #{password}")
39     end
40   end
41
42   current_privileges = mysql_users.fetch(new_resource.user, {})
43
44   new_privileges = Hash[OpenStreetMap::MySQL::USER_PRIVILEGES.collect do |privilege|
45     [privilege, new_resource.send(privilege)]
46   end]
47
48   new_privileges.each do |privilege, new_enabled|
49     old_enabled = current_privileges.fetch(privilege, false)
50
51     if new_enabled && !old_enabled
52       converge_by("grant #{privilege} for #{new_resource}") do
53         Chef::Log.info("Granting #{privilege} for #{new_resource}")
54         mysql_execute(:command => "GRANT #{mysql_privilege_name(privilege)} ON *.* TO #{user}")
55       end
56     elsif old_enabled && !new_enabled
57       converge_by("revoke #{privilege} for #{new_resource}") do
58         Chef::Log.info("Revoking #{privilege} for #{new_resource}")
59         mysql_execute(:command => "REVOKE #{mysql_privilege_name(privilege)} ON *.* FROM #{user}")
60       end
61     end
62   end
63 end
64
65 action :drop do
66   user = mysql_canonicalise_user(new_resource.user)
67
68   if mysql_users.include?(user)
69     converge_by("drop #{new_resource}") do
70       Chef::Log.info("Dropping #{new_resource}")
71       mysql_execute(:command => "DROP USER #{user}")
72     end
73   end
74 end
75
76 action_class do
77   include OpenStreetMap::MySQL
78 end