class Luban::Deployment::Application::Authenticator

Public Instance Methods

app() click to toggle source
# File lib/luban/deployment/cli/application/authenticator.rb, line 56
def app; task.opts.app; end
authen_keys_path() click to toggle source
# File lib/luban/deployment/cli/application/authenticator.rb, line 15
def authen_keys_path
  @authen_keys_path ||= Pathname.new(user_home).join('.ssh')
end
authorized_keys_file_path() click to toggle source
# File lib/luban/deployment/cli/application/authenticator.rb, line 27
def authorized_keys_file_path
  @authorized_keys_file_path ||= authen_keys_path.join('authorized_keys')
end
generate_key_pairs() click to toggle source
# File lib/luban/deployment/cli/application/authenticator.rb, line 48
def generate_key_pairs
  execute(keygen_command) unless key_pairs_generated?
end
get_public_key() click to toggle source
# File lib/luban/deployment/cli/application/authenticator.rb, line 43
def get_public_key
  generate_key_pairs
  capture(keyget_command)
end
key_pairs_generated?() click to toggle source
# File lib/luban/deployment/cli/application/authenticator.rb, line 52
def key_pairs_generated?
  file?(private_key_file_path) and file?(public_key_file_path)
end
keygen_command() click to toggle source
# File lib/luban/deployment/cli/application/authenticator.rb, line 39
def keygen_command
  @keygen_command ||= "ssh-keygen -t #{authen_key_type} -f #{private_key_file_path} -N '' 2>&1"
end
keyget_command() click to toggle source
# File lib/luban/deployment/cli/application/authenticator.rb, line 35
def keyget_command
  @keyget_command ||= "cat #{public_key_file_path} 2>&1"
end
private_key_file_name() click to toggle source
# File lib/luban/deployment/cli/application/authenticator.rb, line 11
def private_key_file_name
  @private_key_file_name ||= "id_#{authen_key_type}"
end
private_key_file_path() click to toggle source
# File lib/luban/deployment/cli/application/authenticator.rb, line 19
def private_key_file_path
  @private_key_file ||= authen_keys_path.join(private_key_file_name)
end
promptless_authen() click to toggle source
# File lib/luban/deployment/cli/application/authenticator.rb, line 58
def promptless_authen
  if host.local?
    update_result "Skipped! Promptless authentication is not necessary for local host.",
                  status: :skipped
  elsif promptless_authen_enabled?
    update_result "Skipped! Promptless authentication has been enabled ALREADY.",
                  status: :skipped, public_key: public_key
  else
    setup_password_authen
    generate_key_pairs
    add_authorized_keys
    update_result "Promptless authentication is enabled.", public_key: public_key
  end
end
promptless_authen_enabled?() click to toggle source
# File lib/luban/deployment/cli/application/authenticator.rb, line 73
def promptless_authen_enabled?
  origin_auth_methods = host.ssh_options[:auth_methods]
  host.ssh_options[:auth_methods] = %w(publickey)
  capture('echo ok') == 'ok' and keys_authorzied?
rescue Net::SSH::AuthenticationFailed
  false
ensure
  if origin_auth_methods.nil?
    host.ssh_options.delete(:auth_methods)
  else
    host.ssh_options[:auth_methods] = origin_auth_methods
  end
end
public_key() click to toggle source
# File lib/luban/deployment/cli/application/authenticator.rb, line 31
def public_key
  @public_key ||= get_public_key
end
public_key_file_path() click to toggle source
# File lib/luban/deployment/cli/application/authenticator.rb, line 23
def public_key_file_path
  @public_key_file_path ||= authen_keys_path.join("#{private_key_file_name}.pub")
end
public_keys() click to toggle source
# File lib/luban/deployment/cli/application/authenticator.rb, line 5
def public_keys
  @public_keys = task.opts.public_keys || []
  @public_keys.uniq!
  @public_keys
end

Protected Instance Methods

add_authorized_key(key) click to toggle source
# File lib/luban/deployment/cli/application/authenticator.rb, line 103
def add_authorized_key(key)
  execute("umask 077; echo #{key} >> #{authorized_keys_file_path} 2>&1")
end
add_authorized_keys() click to toggle source
# File lib/luban/deployment/cli/application/authenticator.rb, line 95
def add_authorized_keys
  if file?(authorized_keys_file_path)
    public_keys.each { |k| add_authorized_key(k) unless key_authorized?(k) }
  else
    public_keys.each { |k| add_authorized_key(k) }
  end
end
key_authorized?(key) click to toggle source
# File lib/luban/deployment/cli/application/authenticator.rb, line 107
def key_authorized?(key)
  test("grep -v \"^#\" #{authorized_keys_file_path} | grep -Fxq \"#{key}\"")
end
keys_authorzied?() click to toggle source
# File lib/luban/deployment/cli/application/authenticator.rb, line 111
def keys_authorzied?
  if file?(authorized_keys_file_path)
    public_keys.all? { |k| key_authorized?(k) }
  else
    false
  end
end
setup_password_authen() click to toggle source
# File lib/luban/deployment/cli/application/authenticator.rb, line 89
def setup_password_authen
  host.user, host.password = user, nil if host.user.nil?
  host.password = app.password_for(host.user) if host.password.nil?
  host.ssh_options[:auth_methods] = %w(keyboard-interactive password)
end