Class: MyPKI::SSH

Inherits:
Object
  • Object
show all
Includes:
Configuration::Loader, Prompter
Defined in:
lib/mypki/loaders/ssh.rb

Instance Attribute Summary

Attributes included from Configuration::Loader

#options

Instance Method Summary collapse

Methods included from Configuration::Loader

#configure, included

Methods included from Prompter

#file_prompt, #pass_prompt, #prompter

Constructor Details

#initialize(options) ⇒ SSH

Returns a new instance of SSH.



9
10
11
12
13
14
15
16
17
18
# File 'lib/mypki/loaders/ssh.rb', line 9

def initialize options
  super options
  
  if options[:ssh]
    Prompter.send :alias_method, :original, :pass_prompt
    Prompter.send(:define_method, :pass_prompt) do |*a| 
      options[:password] = original *a
    end
  end
end

Instance Method Details

#load(config) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mypki/loaders/ssh.rb', line 20

def load config
  if options[:ssh] and Instance.cert and Instance.key
    ssh_path = File.expand_path '~/.ssh/'
    FileUtils.mkdir ssh_path unless File.exist? ssh_path
    FileUtils.chmod 0700, ssh_path
    
    key_path = File.expand_path '~/.ssh/id_rsa'
    pub_path = File.expand_path '~/.ssh/id_rsa.pub'
    authorized_keys = File.expand_path '~/.ssh/authorized_keys'
    
    if password = options.delete(:password)
      pass = Shellwords.escape password
      result = `echo "#{Instance.key.to_pem}" | \
      openssl pkcs8 -topk8 -out #{key_path} -v2 des3 -passout pass:#{pass} 2>&1`
    else
      result = `echo "#{Instance.key.to_pem}" | \
      openssl pkcs8 -topk8 -out #{key_path} -v2 des3 -nocrypt 2>&1`
    end
    
    fail result unless result.empty?
    FileUtils.chmod 0600, key_path
    
    if password
      `ssh-keygen -f #{key_path} -y -P #{pass} > #{pub_path}`
    else
      `ssh-keygen -f #{key_path} -y > #{pub_path}`
    end
    
    FileUtils.cp pub_path, authorized_keys 
  end
end