Class: Luban::Deployment::Application::Authenticator

Inherits:
Worker::Base
  • Object
show all
Defined in:
lib/luban/deployment/cli/application/authenticator.rb

Constant Summary

Constants included from Parameters::Application

Parameters::Application::DefaultLogrotateInterval, Parameters::Application::DefaultLogrotateMaxAge

Constants included from Parameters::General

Parameters::General::DefaultLubanRootPath

Constants included from Helpers::Utils

Helpers::Utils::LogLevels

Instance Attribute Summary

Attributes inherited from Worker::Base

#task

Attributes included from Helpers::Utils

#backend

Attributes included from Helpers::Configuration

#config

Instance Method Summary collapse

Methods inherited from Worker::Base

#dry_run?, #force?, #initialize, #linux?, #method_missing, #osx?, #packages, #run, #target_full_name, #target_major_version, #target_name, #target_patch_level, #target_version

Methods included from Parameters::Application

#env_name, #logrotate_count, #monitor_itself?, #monitorable?

Methods included from Parameters::Base

#parameter

Methods included from Parameters::Project

#monitor_defined?, #process_monitor_via

Methods included from Parameters::General

included

Methods included from Helpers::Utils

#assure, #assure_dirs, #assure_symlink, #capture, #check_pass?, #chmod, #cp, #directory?, #exists?, #file?, #hardware_name, #host, #hostname, #ln, #match?, #md5_for, #md5_for_dir, #md5_for_file, #md5_matched?, #mkdir, #mv, #now, #os_name, #os_release, #readlink, #render_template, #revision_match?, #rm, #rmdir, #sudo, #symlink?, #touch, #truncate, #upload_by_template, #url_exists?, #user_home

Methods included from Helpers::Configuration

#ask, #fetch, #find_template_file, #load_configuration_file, #primary, #release_roles, #role, #roles, #server, #set, #set_default, #syntax_error?

Constructor Details

This class inherits a constructor from Luban::Deployment::Worker::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Luban::Deployment::Worker::Base

Instance Method Details

#appObject



56
# File 'lib/luban/deployment/cli/application/authenticator.rb', line 56

def app; task.opts.app; end

#authen_keys_pathObject



15
16
17
# 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_pathObject



27
28
29
# 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_pairsObject



48
49
50
# File 'lib/luban/deployment/cli/application/authenticator.rb', line 48

def generate_key_pairs
  execute(keygen_command) unless key_pairs_generated?
end

#get_public_keyObject



43
44
45
46
# File 'lib/luban/deployment/cli/application/authenticator.rb', line 43

def get_public_key
  generate_key_pairs
  capture(keyget_command)
end

#key_pairs_generated?Boolean

Returns:

  • (Boolean)


52
53
54
# 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_commandObject



39
40
41
# 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_commandObject



35
36
37
# 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_nameObject



11
12
13
# 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_pathObject



19
20
21
# 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_authenObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/luban/deployment/cli/application/authenticator.rb', line 58

def promptless_authen
  if 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?Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/luban/deployment/cli/application/authenticator.rb', line 70

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_keyObject



31
32
33
# File 'lib/luban/deployment/cli/application/authenticator.rb', line 31

def public_key
  @public_key ||= get_public_key
end

#public_key_file_pathObject



23
24
25
# 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_keysObject



5
6
7
8
9
# 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