class Bcome::Ssh::ConnectionWrangler

Attributes

proxy_details[RW]

Public Class Methods

new(ssh_driver) click to toggle source
# File lib/objects/ssh/connection_wrangler.rb, line 9
def initialize(ssh_driver)
  @ssh_driver = ssh_driver
  @config = ssh_driver.config[:proxy]
  @context_node = ssh_driver.context_node
  @user = ssh_driver.user
  set_proxy_details
end

Public Instance Methods

create_proxy() click to toggle source
# File lib/objects/ssh/connection_wrangler.rb, line 34
def create_proxy
  proxy = Net::SSH::Proxy::Jump.new(hops.reverse.collect(&:get_ssh_string).join(','))
  proxy
end
first_hop() click to toggle source

Accessors –

# File lib/objects/ssh/connection_wrangler.rb, line 18
def first_hop
  hops.reverse.first
end
get_local_port_forward_command(start_port, end_port) click to toggle source
# File lib/objects/ssh/connection_wrangler.rb, line 54
def get_local_port_forward_command(start_port, end_port)
  # TODO: - below check is not actually true... you might still want to proxy over SSH...
  raise ::Bcome::Exception::InvalidPortForwardRequest, 'Connections to this node are not via a proxy. Rather than port forward, try connecting directly.' unless has_hop?

  cmd = "ssh -N -L #{start_port}:localhost:#{end_port} -J"
  cmd += "\s" + hops.collect(&:get_ssh_string).join(',') if has_hop?
  cmd += "\s#{@ssh_driver.user}@#{target_machine_ingress_ip}"

  cmd
end
get_rsync_command(local_path, remote_path) click to toggle source
# File lib/objects/ssh/connection_wrangler.rb, line 47
def get_rsync_command(local_path, remote_path)
  cmd = 'rsync -azv'
  cmd += "\s-e 'ssh\s-A -J\s" + hops.collect(&:get_ssh_string).join(',') + "'" if has_hop?
  cmd += "\s#{local_path}\s#{@ssh_driver.user}@#{target_machine_ingress_ip}:#{remote_path}"
  cmd
end
get_ssh_command(config = {}, _proxy_only = false) click to toggle source
# File lib/objects/ssh/connection_wrangler.rb, line 39
def get_ssh_command(config = {}, _proxy_only = false)
  cmd = has_hop? ? 'ssh -J' : 'ssh'
  cmd += "\s" + hops.collect(&:get_ssh_string).join(',') if has_hop?
  cmd += "\s#{@ssh_driver.user}@#{target_machine_ingress_ip}"

  config[:as_pseudo_tty] ? "#{cmd} -t" : cmd
end
has_hop?() click to toggle source
# File lib/objects/ssh/connection_wrangler.rb, line 22
def has_hop?
  hops.any?
end
hops() click to toggle source
# File lib/objects/ssh/connection_wrangler.rb, line 65
def hops
  @hops ||= set_hops
end
proxy() click to toggle source
# File lib/objects/ssh/connection_wrangler.rb, line 30
def proxy
  @proxy ||= create_proxy
end
single_hop?() click to toggle source
# File lib/objects/ssh/connection_wrangler.rb, line 26
def single_hop?
  has_hop? && hops.size == 1
end

Protected Instance Methods

set_proxy_details() click to toggle source
# File lib/objects/ssh/connection_wrangler.rb, line 71
def set_proxy_details
  @proxy_details ||= hops.compact.collect(&:proxy_details)
end
target_machine_ingress_ip() click to toggle source
# File lib/objects/ssh/connection_wrangler.rb, line 75
def target_machine_ingress_ip
  return @context_node.internal_ip_address if @context_node.local_network?

  if has_hop?
    @context_node.internal_ip_address
  elsif @context_node.public_ip_address
    @context_node.public_ip_address
  else
    @context_node.internal_ip_address
  end
end

Private Instance Methods

get_proxy_hop(config, parent) click to toggle source
# File lib/objects/ssh/connection_wrangler.rb, line 110
def get_proxy_hop(config, parent)
  config[:fallback_bastion_host_user] = @ssh_driver.fallback_bastion_host_user
  h = ::Bcome::Ssh::ProxyHop.new(config, @context_node, parent)
  return h
end
iterable_configs() click to toggle source
# File lib/objects/ssh/connection_wrangler.rb, line 116
def iterable_configs
  @iterable ||= if @config
                  @config.is_a?(Hash) ? [@config] : @config
                else
                  []
                end
end
set_hops() click to toggle source
# File lib/objects/ssh/connection_wrangler.rb, line 89
def set_hops
  hop_collection = []

  parent = nil
  iterable_configs.each do |config|
    hop = get_proxy_hop(config, parent)
  
    if @context_node.is_same_machine?(hop.bcome_proxy_node)
      # We don't hop through ourselves.  If we're reached ourselves in the proxy chain,
      # then we'll break the chain at that point.
      break
    end  

    # Set proxy hop
    hop_collection << hop
    parent = hop
  end

  hop_collection.compact
end