class Bosh::Cli::SSHSession

Constants

SSH_USER_PREFIX

Attributes

public_key[R]
user[R]

Public Class Methods

new() click to toggle source
# File lib/cli/ssh_session.rb, line 10
def initialize
  @session_uuid = SecureRandom::uuid
  @public_key = generate_rsa_key
  @user = random_ssh_username
end

Public Instance Methods

cleanup() click to toggle source
# File lib/cli/ssh_session.rb, line 40
def cleanup
  remove_private_key
  remove_known_host_file
end
set_host_session(session) click to toggle source
# File lib/cli/ssh_session.rb, line 16
def set_host_session(session)
  @host_session = session
end
ssh_known_host_option(port) click to toggle source
# File lib/cli/ssh_session.rb, line 20
def ssh_known_host_option(port)
  path = user_known_host_path(port)
  if path.length > 0
    path = "-o UserKnownHostsFile=#{path}"
  end
  return path
end
ssh_known_host_path(port) click to toggle source
# File lib/cli/ssh_session.rb, line 28
def ssh_known_host_path(port)
  user_known_host_path(port)
end
ssh_private_key_option() click to toggle source
# File lib/cli/ssh_session.rb, line 32
def ssh_private_key_option
  "-i#{ssh_private_key_path}"
end
ssh_private_key_path() click to toggle source
# File lib/cli/ssh_session.rb, line 36
def ssh_private_key_path
  File.join(ENV['HOME'], '.bosh', 'tmp', "#{@session_uuid}_key")
end

Private Instance Methods

add_known_host_file(hostEntry) click to toggle source
# File lib/cli/ssh_session.rb, line 94
def add_known_host_file(hostEntry)
  file_name = known_host_file_path

  create_dir_for_file(file_name)

  known_host_file = File.new(file_name, "w")
  known_host_file.puts(hostEntry)
  known_host_file.close
end
add_private_key(private_key) click to toggle source
# File lib/cli/ssh_session.rb, line 66
def add_private_key(private_key)
  file_name = private_key_file_name
  create_dir_for_file(file_name)

  key_File = File.new(file_name, "w", 0400)
  key_File.puts(private_key)
  key_File.close
end
create_dir_for_file(file_name) click to toggle source
# File lib/cli/ssh_session.rb, line 109
def create_dir_for_file(file_name)
  dirname = File.dirname(file_name)
  unless File.directory?(dirname)
    FileUtils.mkdir_p(dirname)
  end
end
generate_rsa_key() click to toggle source
# File lib/cli/ssh_session.rb, line 47
def generate_rsa_key
  key = SSHKey.generate(
      type:       "RSA",
      bits:       2048,
      comment:    "bosh-ssh",
  )
  add_private_key(key.private_key)
  return key.ssh_public_key
end
known_host_file_path() click to toggle source
# File lib/cli/ssh_session.rb, line 90
def known_host_file_path
  File.join(ENV['HOME'], '.bosh', 'tmp', "#{@session_uuid}_known_hosts")
end
private_key_file_name() click to toggle source
# File lib/cli/ssh_session.rb, line 62
def private_key_file_name
  File.join(ENV['HOME'], '.bosh', 'tmp', "#{@session_uuid}_key")
end
random_ssh_username() click to toggle source
# File lib/cli/ssh_session.rb, line 75
def random_ssh_username
  SSH_USER_PREFIX + rand(36**9).to_s(36)
end
remove_known_host_file() click to toggle source
# File lib/cli/ssh_session.rb, line 104
def remove_known_host_file
  file_name = known_host_file_path
  FileUtils.rm_rf(file_name) if File.exist?(file_name)
end
remove_private_key() click to toggle source
# File lib/cli/ssh_session.rb, line 57
def remove_private_key
  file_name = private_key_file_name
  FileUtils.rm_rf(file_name) if File.exist?(file_name)
end
user_known_host_path(gatewayPort) click to toggle source
# File lib/cli/ssh_session.rb, line 79
def user_known_host_path(gatewayPort)
  if @host_session.include?('host_public_key')
    hostEntryIP =  if gatewayPort then "[localhost]:#{gatewayPort}" else @host_session['ip'] end
    hostEntry = "#{hostEntryIP} #{@host_session['host_public_key']}"
    add_known_host_file(hostEntry)
    return known_host_file_path
  else
    return String.new
  end
end