Module: ZTK::SSH::Private

Included in:
ZTK::SSH
Defined in:
lib/ztk/ssh/private.rb

Overview

SSH Private Functionality

Instance Method Summary (collapse)

Instance Method Details

- (Object) base_options

Builds our core options



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ztk/ssh/private.rb', line 8

def base_options
  options = Hash.new

  config.encryption.nil? or              options.merge!(:encryption => config.encryption)
  config.compression.nil? or             options.merge!(:compression => config.compression)
  config.compression_level.nil? or       options.merge!(:compression_level => config.compression_level)
  config.timeout.nil? or                 options.merge!(:timeout => config.timeout)
  config.forward_agent.nil? or           options.merge!(:forward_agent => config.forward_agent)
  config.global_known_hosts_file.nil? or options.merge!(:global_known_hosts_file => config.global_known_hosts_file)
  config.auth_methods.nil? or            options.merge!(:auth_methods => config.auth_methods)
  config.host_key.nil? or                options.merge!(:host_key => config.host_key)
  config.host_key_alias.nil? or          options.merge!(:host_key_alias => config.host_key_alias)
  config.keys_only.nil? or               options.merge!(:keys_only => config.keys_only)
  config.hmac.nil? or                    options.merge!(:hmac => config.hmac)
  config.rekey_limit.nil? or             options.merge!(:rekey_limit => config.rekey_limit)
  config.user_known_hosts_file.nil? or   options.merge!(:user_known_hosts_file => config.user_known_hosts_file)

  options
end

- (Object) gateway_options

Builds our SSH gateway options hash.



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ztk/ssh/private.rb', line 42

def gateway_options
  process_keys
  options = base_options

  config.proxy_port.nil? or     options.merge!(:port => config.proxy_port)
  config.proxy_password.nil? or options.merge!(:password => config.proxy_password)
  config.proxy_keys.nil? or     options.merge!(:keys => config.proxy_keys)

  config.ui.logger.debug { "gateway_options(#{options.inspect})" }
  options
end

- (Object) log_header(what, char = '=')



102
103
104
105
106
107
# File 'lib/ztk/ssh/private.rb', line 102

def log_header(what, char='=')
  count = 16
  sep = (char * count)
  header = [sep, "[ #{tag} >>> #{what} ]", sep].join
  "#{header}\n"
end

- (Object) process_key(key)

Process a individual key, rendering it to a temporary file if needed.



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ztk/ssh/private.rb', line 70

def process_key(key)
  if ::File.exists?(key)
    key
  else
    tempfile = ::Tempfile.new('key')
    tempfile.write(key)
    tempfile.flush

    tempfile.path
  end
end

- (Object) process_keys

Iterate the keys and proxy_keys, converting them as needed.



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ztk/ssh/private.rb', line 55

def process_keys
  if (!config.keys.nil? && !config.keys.empty?)
    config.keys = [config.keys].flatten.compact.collect do |key|
      process_key(key)
    end
  end

  if (!config.proxy_keys.nil? && !config.proxy_keys.empty?)
    config.proxy_keys = [config.proxy_keys].flatten.compact.collect do |proxy_key|
      process_key(proxy_key)
    end
  end
end

- (Object) ssh_options

Builds our SSH options hash.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ztk/ssh/private.rb', line 29

def ssh_options
  process_keys
  options = base_options

  config.port.nil? or     options.merge!(:port => config.port)
  config.password.nil? or options.merge!(:password => config.password)
  config.keys.nil? or     options.merge!(:keys => config.keys)

  config.ui.logger.debug { "ssh_options(#{options.inspect})" }
  options
end

- (Object) tag

Builds a human readable tag about our connection. Used for internal logging purposes.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ztk/ssh/private.rb', line 84

def tag
  tags = Array.new

  user_host = "#{config.user}@#{config.host_name}"
  port = (config.port ? ":#{config.port}" : nil)
  tags << [user_host, port].compact.join

  if config.proxy_host_name
    tags << " via "

    proxy_user_host = "#{config.proxy_user}@#{config.proxy_host_name}"
    proxy_port = (config.proxy_port ? ":#{config.proxy_port}" : nil)
    tags << [proxy_user_host, proxy_port].compact.join
  end

  tags.join.strip
end