Module: KeyHandler

Defined in:
lib/direct_ssh/key_handler.rb

Class Method Summary collapse

Class Method Details

.chmod_ssh_filesObject



43
44
45
46
47
48
49
# File 'lib/direct_ssh/key_handler.rb', line 43

def self.chmod_ssh_files
    FileUtils.chmod 0700, Dir.home + '/.ssh'
    FileUtils.chmod 0600, Dir.home + '/.ssh/id_rsa'
    FileUtils.chmod 0644, Dir.home + '/.ssh/id_rsa.pub'
    FileUtils.chmod 0644, Dir.home + '/.ssh/authorized_keys'
    FileUtils.chmod 0644, Dir.home + '/.ssh/known_hosts'
end

.create_ssh_filesObject



34
35
36
37
38
39
40
# File 'lib/direct_ssh/key_handler.rb', line 34

def self.create_ssh_files
    FileUtils.mkdir_p Dir.home + '/.ssh'
    FileUtils.touch Dir.home + '/.ssh/id_rsa'
    FileUtils.touch Dir.home + '/.ssh/id_rsa.pub'
    FileUtils.touch Dir.home + '/.ssh/authorized_keys'
    FileUtils.touch Dir.home + '/.ssh/known_hosts'
end

.get_public_key(public_key) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/direct_ssh/key_handler.rb', line 52

def self.get_public_key(public_key)
    authtype = public_key.class.to_s.split('::').last.downcase
    b64pub   = Base64.encode64(public_key.to_blob).strip.gsub(/[\r\n\t ]/, '')
    user     = ENV['USER']
    host     = ENV['HOSTNAME']
    host     = ENV['COMPUTERNAME'] if host == nil
    "ssh-%s %s %s@%s" % [authtype, b64pub, user, host]
end

.get_ssh_public_keyObject

get public key, create it if not exists



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/direct_ssh/key_handler.rb', line 17

def self.get_ssh_public_key
    if !File.exists?(Dir.home + '/.ssh/id_rsa.pub')
        create_ssh_files
        chmod_ssh_files

        private_key = OpenSSL::PKey::RSA.new(2048)
        public_key  = get_public_key(private_key.public_key)

        File.write(Dir.home + '/.ssh/id_rsa',     private_key)
        File.write(Dir.home + '/.ssh/id_rsa.pub', public_key)

        return public_key
    end

    IO.read(Dir.home + '/.ssh/id_rsa.pub')
end

.is_windows?(ssh) ⇒ Boolean

remote ssh key process

Returns:

  • (Boolean)


64
65
66
# File 'lib/direct_ssh/key_handler.rb', line 64

def self.is_windows?(ssh)
    ssh.exec!("echo %os%").chomp != "%os%"
end

.remote_append_key(ssh, key, is_win) ⇒ Object

append public_key to remote ‘~/.ssh/authorized_keys’



119
120
121
122
123
124
125
# File 'lib/direct_ssh/key_handler.rb', line 119

def self.remote_append_key(ssh, key, is_win)
    if is_win
        ssh_exec!(ssh, "echo #{key} >> .ssh\\authorized_keys")
    else
        ssh_exec!(ssh, "echo #{key} >> ~/.ssh/authorized_keys")
    end
end

.remote_chmod_ssh_files(ssh, is_win) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/direct_ssh/key_handler.rb', line 102

def self.remote_chmod_ssh_files(ssh, is_win)
    if is_win
        # puts "NOTE 1: The default mode on windows should work"
        # puts "NOTE 2: 'chmod' is not available or doesn't work on windows."
        # puts "  If password asked, try to handle according to"
        # puts "  https://social.technet.microsoft.com/Forums/Azure/en-US/e4c11aed-1d8b-4ff4-89ad-c90c62e13ce0/ssh-asking-for-password-even-i-have-private-key"
        # puts "  and log file C:\\ProgramData\\ssh\\logs\\sshd.log"
    else
        ssh_exec!(ssh, 'chmod 700 ~/.ssh')
        ssh_exec!(ssh, 'chmod 600 ~/.ssh/id_rsa')
        ssh_exec!(ssh, 'chmod 644 ~/.ssh/id_rsa.pub')
        ssh_exec!(ssh, 'chmod 644 ~/.ssh/authorized_keys')
        ssh_exec!(ssh, 'chmod 644 ~/.ssh/known_hosts')
    end
end

.remote_create_ssh_files(ssh, is_win) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/direct_ssh/key_handler.rb', line 85

def self.remote_create_ssh_files(ssh, is_win)
    if is_win
        ssh_exec!(ssh, 'mkdir .ssh')
        ssh_exec!(ssh, 'touch .ssh\id_rsa')
        ssh_exec!(ssh, 'touch .ssh\id_rsa.pub')
        ssh_exec!(ssh, 'touch .ssh\authorized_keys')
        ssh_exec!(ssh, 'touch .ssh\known_hosts')
    else
        ssh_exec!(ssh, 'mkdir ~/.ssh')
        ssh_exec!(ssh, 'touch ~/.ssh/id_rsa')
        ssh_exec!(ssh, 'touch ~/.ssh/id_rsa.pub')
        ssh_exec!(ssh, 'touch ~/.ssh/authorized_keys')
        ssh_exec!(ssh, 'touch ~/.ssh/known_hosts')
    end
end

.remote_file_exists?(ssh, path) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
83
# File 'lib/direct_ssh/key_handler.rb', line 79

def self.remote_file_exists?(ssh, path)
    # windows & linux       OK
    # path including '~/'   OK
    ssh.exec!("[ ! -f #{path} ] && echo NOT_EXIST").empty?
end

.send_key_to_remote(ssh) ⇒ Object



8
9
10
11
# File 'lib/direct_ssh/key_handler.rb', line 8

def self.send_key_to_remote(ssh)
    ssh_public_key = get_ssh_public_key.chomp
    send_ssh_public_key_to_remote(ssh, ssh_public_key)
end

.send_ssh_public_key_to_remote(ssh, key) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/direct_ssh/key_handler.rb', line 68

def self.send_ssh_public_key_to_remote(ssh, key)
    is_win = is_windows?(ssh)

    if !remote_file_exists?(ssh, '~/.ssh/authorized_keys')
        remote_create_ssh_files(ssh, is_win)
        remote_chmod_ssh_files(ssh, is_win)
    end

    remote_append_key(ssh, key, is_win)
end

.ssh_exec!(ssh, cmd) ⇒ Object



127
128
129
130
131
132
# File 'lib/direct_ssh/key_handler.rb', line 127

def self.ssh_exec!(ssh, cmd)
    # puts "# #{cmd}"
    res = ssh.exec! cmd
    # puts res.force_encoding('SJIS').encode('UTF-8')
    res
end