Class: BastionChefClientProxy

Inherits:
BasicObject
Defined in:
lib/knife-bastion/chef_socks_proxy.rb

Overview

Simple class, that delegates all the calls to the base client object, except for ‘request`. The latter is overwritten to first configure SOCKS proxy, and if connection fails - show warning about the bastion setup.

Constant Summary collapse

NETWORK_ERRORS =
[
  ::SocketError,
  ::Errno::ETIMEDOUT,
  ::Errno::ECONNRESET,
  ::Errno::ECONNREFUSED,
  ::Timeout::Error,
  ::OpenSSL::SSL::SSLError,
]

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ BastionChefClientProxy

Returns a new instance of BastionChefClientProxy.



23
24
25
# File 'lib/knife-bastion/chef_socks_proxy.rb', line 23

def initialize(client)
  @client = client
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



33
34
35
# File 'lib/knife-bastion/chef_socks_proxy.rb', line 33

def method_missing(method, *args, &block)
  @client.send(method, *args, &block)
end

Instance Method Details

#request(*args, &block) ⇒ Object



27
28
29
30
31
# File 'lib/knife-bastion/chef_socks_proxy.rb', line 27

def request(*args, &block)
  with_socks_proxy do
    @client.request(*args, &block)
  end
end

#with_socks_proxyObject



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/knife-bastion/chef_socks_proxy.rb', line 37

def with_socks_proxy
  old_socks_server, old_socks_port = ::TCPSocket::socks_server, ::TCPSocket::socks_port
  ::TCPSocket::socks_server, ::TCPSocket::socks_port = '127.0.0.1', ::Chef::Config[:knife][:bastion_local_port] || 4443
  yield
rescue *NETWORK_ERRORS
  puts ::HighLine.color("WARNING:", [:bold, :red]) + " Failed to contact Chef server!"
  puts "You might need to start bastion connection with #{::HighLine.color("knife bastion start", [:bold, :magenta])} to access Chef."
  puts
  raise
ensure
  ::TCPSocket::socks_server, ::TCPSocket::socks_port = old_socks_server, old_socks_port
end