diff --git a/lib/gratan/driver.rb b/lib/gratan/driver.rb index 9e24154..ae114d1 100644 --- a/lib/gratan/driver.rb +++ b/lib/gratan/driver.rb @@ -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 @@ -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 @@ -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] @@ -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) @@ -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) @@ -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 diff --git a/lib/gratan/grant_parser.rb b/lib/gratan/grant_parser.rb index e6a6f6a..bc480f5 100644 --- a/lib/gratan/grant_parser.rb +++ b/lib/gratan/grant_parser.rb @@ -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)