-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql.rb
380 lines (278 loc) · 10.5 KB
/
mysql.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
require 'mysql2'
class Mysql
# Initialize our Mysql object with some instance variables
def initialize
@run = true
@test = true
@debug = true
@host = 'localhost'
end
# Create a connection to a database server with given credentials for
# user, password, host, and type
def connect(user, password, host="localhost")
begin
# Set up our instance variables so we know where and what we are connected to
@host = host
# Set up the connection string
connection_string = sprintf("host=%s",@host)
display_command("attempting to connect with credentials: #{connection_string} user: #{user} passwd: #{password}") if @show
# Attempt to connect to the database
@dbh = Mysql2::Client.new(:host => @host, :username => user, :password => password)
# Assign user host restrictions for this server based
# on whether or not this is a master or slave
@user_host_restriction = set_user_host_restriction
return true
rescue => e
display_error(e) if @debug
return false
end
end
# Create a new database on the server with specified name
# this must be a unique name
def create_database(database)
sql = "CREATE DATABASE `#{database}`"
display_command(sql) if @show
begin
@dbh.query(sql) if @run
return true
rescue => e
display_error(e) if @debug
return false
end
end
# Drop given database and remove any grants it may have associated with it
def drop_database(database)
sql = "DROP DATABASE `#{database}`"
display_command(sql) if @show
begin
@dbh.query(sql) if @run
return true
rescue => e
display_error(e) if @debug
return false
end
end
# Dump the specified database and return a sql file
def dump_database(host, database)
begin
# Redirect output to stdout so we can see if it failed or succeeded
result = %x(mysqldump --opt --user=root --host=#{host} #{database} > #{database}.sql 2>&1)
unless $? == 0 #check if the child process exited cleanly.
# Clean up our poor empty file
%x(rm #{database}.sql)
return false
end
return true
rescue => e
display_error(e) if @debug
return false
end
end
# Duplicate a database onto another host
def clone_database
end
# Move a database from one host to another
def move_database
end
# Create database user with specified name on the server
def create_user(user, password)
sql = "CREATE USER #{full_name(user)} IDENTIFIED BY '#{password}'"
display_command(sql) if @show
begin
@dbh.query(sql) if @run
flush_privileges
return true
rescue => e
display_error(e) if @debug
return false
end
end
# Delete an existing database user. Before deleting the user remove any/all grants
# they have been assigned to any databases on this server
def drop_user(user)
sql = "DROP USER #{full_name(user)}"
display_command(sql) if @show
begin
@dbh.query(sql) if @run
flush_privileges
return true
rescue => e
display_error(e) if @debug
return false
end
end
# Create an entry in the grant table for the specified combination of
# user, database, and rights. We used to have hard coded role logic in
# here to the effect of:
# ADMIN = 'ALL PRIVILEGES'
# WEB = 'SELECT,INSERT,UPDATE,DELETE,LOCK TABLES,EXECUTE'
# READONLY = 'SELECT'
def create_user_grant(database, user, permissions)
sql = "GRANT #{permissions} ON `#{database}`.* TO #{full_name(user)}"
display_command(sql) if @show
begin
@dbh.query(sql) if @run
flush_privileges
return true
rescue => e
display_error(e) if @debug
return false
end
end
# Revoke a specific grant entry for a user. This will remove a users permission
# to an individual specified database
def revoke_user_grant(database, user)
sql = "REVOKE ALL PRIVILEGES ON `#{database}`.* FROM #{full_name(user)}"
display_command(sql) if @show
begin
@dbh.query(sql) if @run
flush_privileges
return true
rescue => e
display_error(e) if @debug
return false
end
end
# Update a users password with specified username and password pair
def update_user_password(user, password)
sql = "UPDATE mysql.user SET Password=PASSWORD('#{password}') WHERE User='#{user}' AND Host='#{@user_host_restriction}'"
display_command(sql) if @show
begin
@dbh.query(sql) if @run
flush_privileges
return true
rescue => e
display_error(e) if @debug
return false
end
end
# Update a users password with specified username and password pair
def rename_user(user, new_user_name)
sql = "RENAME USER #{full_name(user)} TO #{full_name(new_user_name)}"
display_command(sql) if @show
begin
@dbh.query(sql) if @run
flush_privileges
return true
rescue => e
display_error(e) if @debug
return false
end
end
# Verify whether a database is present or not on the server by
# attempting to use it...if it is there it will execute successfully
# if it is not it will throw an error which we catch and return as false
def verify_database_present(database)
begin
# Attempt to use the database with given name
result = @dbh.query("USE #{database}")
# If we did not just throw an error the database is present
return true
rescue => e
display_error(e) if @debug
return false
end
end
# Verify that given user is present on server
def verify_user_present(user)
begin
# See if we can find the user...a valid user will return one or more rows...and invalid will return 0
result = @dbh.query("SELECT User FROM mysql.user WHERE User='#{user}' AND Host='#{@user_host_restriction}'")
# If we got a result the user is present...otherwise they are not
return result.count >= 1 ? true : false
rescue => e
display_error(e) if @debug
end
end
# Verify whether a given grant exists for the given user on the given database.
# If the user is nonexistant it will throw an error on the show command which is why
# we trap the error and return false as this is still consistent
def verify_user_grant_present(database, user)
begin
grant_present = false
begin
# This block will throw error and fail if user is not valid...
results = @dbh.query("SHOW GRANTS FOR #{full_name(user)}")
results.each do |row|
# Convert result to a string and look to see if we have a match
if current_database = row.to_s().index(database)
grant_present = true
break
end
end
rescue
grant_present = false
end
return grant_present
rescue => e
display_error(e) if @debug
end
end
# Verify a given set of user credentials is valid...used primarily after a user name change
# or password change to verify the changes have taken place and are valid
def verify_user_credentials(user, password)
begin
# Set up the connection string
connection_string = sprintf("host=%s",@host)
display_command("attempting to verify credentials: #{connection_string} user: #{user} passwd: #{password}") if @show
# Attempt to connect to the database
return true if Mysql2::Client.new(:host => @host, :username => user, :password => password)
rescue => e
display_error(e) if @debug
return false
end
end
# Verify that all grants for a database have been purged. This is primarily used
# to ensure that after dropping a database we have really removed all the grants
# associtated with it.
def verify_database_grants_purged(database)
#TODO: create actual test
return true
end
private
# Assign a host restriction to all users created on this database server
# based on whether or not it is a master or slave
def set_user_host_restriction
# For testing purposes we need to restrict this to localhost or something
# else that makes sense in future
host = (@test == true) ? 'localhost' : '%.cws.oregonstate.edu'
# If we are on a database host that is a slave we assign new users
# a different host restriction than if we are on a master
is_slave? ? '%' : host
end
# This is a mysql specific status check...we will need to broaden our scope
# to deal with postgres status...and if we are mysqlite we should just
# always answer false as the concept is not applicable
def is_slave?
begin
# Query our slave running status
result = @dbh.query("SHOW SLAVE STATUS")
# If its not a slave we wont get anything (or its off) otherwise its a master
return result.count > 0 ? true : false
rescue => e
display_error(e) if @debug
end
end
# Flush the datbase privileges (after you have created or modified a users grants)
def flush_privileges
begin
@dbh.query("FLUSH PRIVILEGES")
return true
rescue => e
display_error(e) if @debug
return false
end
end
# Format our errors for display
def display_error(error)
puts "Error: #{error.message}"
end
# Format our sql for display if they have @show enabled
def display_command(cmd)
print "\t#{cmd}\n"
end
# Format our user name to its full form including the host restriction
def full_name(user)
return "'#{user}'@'#{@user_host_restriction}'"
end
end