Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
Add identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
Genki Sugawara committed Oct 5, 2014
1 parent d011742 commit 12bf749
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 4 deletions.
10 changes: 7 additions & 3 deletions bin/gratan
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ mysql_options = {
}

options = {
:dry_run => false,
:color => true,
:debug => false,
:dry_run => false,
:identifier => Gratan::Identifier::Null.new,
:color => true,
:debug => false,
}

ARGV.options do |opt|
Expand All @@ -42,6 +43,9 @@ ARGV.options do |opt|
opt.on('' , '--no-color') { options[:color] = false }
opt.on('' , '--debug') { options[:debug] = true }

opt.on('' , '--auto-identify OUTPUT') {|v| options[:identifier] = Gratan::Identifier::Auto.new(v) }
opt.on('' , '--csv-identify CSV') {|v| options[:identifier] = Gratan::Identifier::CSV.new(v) }

opt.on('-h', '--help') do
puts opt.help
exit 1
Expand Down
4 changes: 4 additions & 0 deletions lib/gratan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ module Gratan; end
require 'gratan/exporter'
require 'gratan/ext/string_ext'
require 'gratan/grant_parser'
require 'gratan/identifier'
require 'gratan/identifier/auto'
require 'gratan/identifier/csv'
require 'gratan/identifier/null'
require 'gratan/version'
8 changes: 7 additions & 1 deletion lib/gratan/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ def walk(file, options)
end

def create_user(user, host, attrs)
# XXX: Add password proc
attrs[:options] ||= {}

unless attrs.has_key?(:identified)
identified = @options[:identifier].identify(user, host)
attrs[:options][:identified] = identified if identified
end

@driver.create_user(user, host, attrs)
update!
end
Expand Down
2 changes: 2 additions & 0 deletions lib/gratan/identifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Gratan::Identifier
end
26 changes: 26 additions & 0 deletions lib/gratan/identifier/auto.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Gratan::Identifier::Auto
def initialize(output)
if output == '-'
@output = $stdout
else
@output = open(output, 'w')
end
end

def identify(user, host)
password = mkpasswd
puts_password(user, host, password)
password
end

private

def mkpasswd(len = 8)
[*1..9, *'A'..'Z', *'a'..'z'].shuffle.slice(0, len).join
end

def puts_password(user, host, password)
@output.puts("#{user}@#{host},#{password}")
@output.flush
end
end
24 changes: 24 additions & 0 deletions lib/gratan/identifier/csv.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'csv'

class Gratan::Identifier::CSV
include Gratan::Logger::Helper

def initialize(path)
@passwords = {}

CSV.foreach(path) do |row|
@passwords[row[0]] = row[1]
end
end

def identify(user, host)
user_host = "#{user}@#{host}"
password = @passwords[user_host]

unless password
log(:warn, 'password for `#{user_host}` can not be found', :yellow)
end

password
end
end
5 changes: 5 additions & 0 deletions lib/gratan/identifier/null.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Gratan::Identifier::Null
def identify
nil
end
end

0 comments on commit 12bf749

Please sign in to comment.