Skip to content
This repository was archived by the owner on May 31, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions lib/gratan/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def each_user

def show_grants(user, host)
query("SHOW GRANTS FOR #{quote_user(user, host)}").each do |row|
#puts row
yield(row.values.first)
end
end
Expand Down Expand Up @@ -68,6 +69,7 @@ def create_user(user, host, options = {})

objects.each do |object_or_regexp, object_options|
expand_object(object_or_regexp).each do |object|
create(user, host, object, grant_options.merge(object_options))
grant(user, host, object, grant_options.merge(object_options))
granted = true
end
Expand All @@ -83,6 +85,31 @@ def drop_user(user, host)
delete(sql)
end

def create(user, host, object, options)
privs = options.fetch(:privs)
identified = options[:identified]
required = options[:required]
with_option = options[:with]

sql = 'create user if not exists %s' % [
quote_user(user, host),
]

sql << " IDENTIFIED BY #{quote_identifier(identified)}" if identified
sql << " REQUIRE #{required}" if required
sql << " WITH #{with_option}" if with_option

begin
update(sql)
rescue Mysql2::Error => e
if @options[:ignore_not_exist] and e.error_number == ER_NO_SUCH_TABLE
log(:warn, e.message, :color => :yellow)
else
raise e
end
end
end

def grant(user, host, object, options)
privs = options.fetch(:privs)
identified = options[:identified]
Expand All @@ -95,9 +122,9 @@ def grant(user, host, object, options)
quote_user(user, host),
]

sql << " IDENTIFIED BY #{quote_identifier(identified)}" if identified
sql << " REQUIRE #{required}" if required
sql << " WITH #{with_option}" if with_option
#sql << " IDENTIFIED BY #{quote_identifier(identified)}" if identified
#sql << " REQUIRE #{required}" if required
#sql << " WITH #{with_option}" if with_option

begin
update(sql)
Expand All @@ -111,9 +138,10 @@ def grant(user, host, object, options)
end

def identify(user, host, identifier)
sql = 'GRANT USAGE ON *.* TO %s IDENTIFIED BY %s' % [
#sql = 'GRANT USAGE ON *.* TO %s IDENTIFIED BY %s' % [
sql = 'GRANT USAGE ON *.* TO %s ' % [
quote_user(user, host),
quote_identifier(identifier),
#quote_identifier(identifier),
]

update(sql)
Expand Down Expand Up @@ -262,8 +290,9 @@ def quote_identifier(identifier)

unless identifier =~ /\APASSWORD\s+'.+'\z/
identifier = "'#{escape(identifier)}'"
else
identifier = identifier.sub(/\APASSWORD\s+/,'')
end

identifier
end
end
10 changes: 9 additions & 1 deletion lib/gratan/grant_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,20 @@ def parse_identified
end

def parse_main
md = /\AGRANT\s+(.+?)\s+ON\s+(.+?)\s+TO\s+'(.*)'@'(.+)'\z/.match(@stmt)
#md = /\AGRANT\s+(.+?)\s+ON\s+(.+?)\s+TO\s+'(.*)'@'(.+)'\z/.match(@stmt)
md = /\AGRANT\s+(.+?)\s+ON\s+(.+?)\s+TO\s+`(.*)`@`(.+)`\z/.match(@stmt)
puts "stmt :#{@stmt}"
puts "md :#{md}"
privs, object, user, host = md.captures
privs, object, user, host = md.captures
@parsed[:privs] = parse_privs(privs.strip)
@parsed[:object] = object.gsub('`', '').strip
@parsed[:user] = user
@parsed[:host] = host
puts "privs :#{@parsed[:privs]}"
puts "object:#{@parsed[:object]}"
puts "user :#{@parsed[:user]}"
puts "host :#{@parsed[:host]}"
end

def parse_privs(privs)
Expand Down