2020-07-23 17:47:54 -07:00
|
|
|
"""Common Classes or Utilities for SSH Drivers."""
|
2020-07-23 09:20:04 -07:00
|
|
|
|
|
|
|
# Standard Library
|
2020-07-23 17:47:54 -07:00
|
|
|
from typing import Callable
|
2020-07-23 09:20:04 -07:00
|
|
|
|
|
|
|
# Project
|
|
|
|
from hyperglass.log import log
|
2020-07-23 17:47:54 -07:00
|
|
|
from hyperglass.exceptions import ScrapeError
|
2020-07-23 09:20:04 -07:00
|
|
|
from hyperglass.configuration import params
|
|
|
|
from hyperglass.compat._sshtunnel import BaseSSHTunnelForwarderError, open_tunnel
|
2020-10-05 12:10:27 -07:00
|
|
|
|
2020-10-11 13:14:57 -07:00
|
|
|
# Local
|
2020-10-05 12:10:27 -07:00
|
|
|
from ._common import Connection
|
2020-07-23 09:20:04 -07:00
|
|
|
|
|
|
|
|
|
|
|
class SSHConnection(Connection):
|
2020-07-23 17:47:54 -07:00
|
|
|
"""Base class for SSH drivers."""
|
2020-07-23 09:20:04 -07:00
|
|
|
|
|
|
|
def setup_proxy(self) -> Callable:
|
|
|
|
"""Return a preconfigured sshtunnel.SSHTunnelForwarder instance."""
|
|
|
|
|
|
|
|
proxy = self.device.proxy
|
|
|
|
|
|
|
|
def opener():
|
|
|
|
"""Set up an SSH tunnel according to a device's configuration."""
|
|
|
|
try:
|
|
|
|
return open_tunnel(
|
2020-07-30 01:30:01 -07:00
|
|
|
proxy._target,
|
2020-07-23 09:20:04 -07:00
|
|
|
proxy.port,
|
|
|
|
ssh_username=proxy.credential.username,
|
|
|
|
ssh_password=proxy.credential.password.get_secret_value(),
|
2020-07-30 01:30:01 -07:00
|
|
|
remote_bind_address=(self.device._target, self.device.port),
|
2020-07-23 09:20:04 -07:00
|
|
|
local_bind_address=("localhost", 0),
|
|
|
|
skip_tunnel_checkup=False,
|
|
|
|
gateway_timeout=params.request_timeout - 2,
|
|
|
|
)
|
|
|
|
|
|
|
|
except BaseSSHTunnelForwarderError as scrape_proxy_error:
|
|
|
|
log.error(
|
|
|
|
f"Error connecting to device {self.device.name} via "
|
|
|
|
f"proxy {proxy.name}"
|
|
|
|
)
|
|
|
|
raise ScrapeError(
|
|
|
|
params.messages.connection_error,
|
|
|
|
device_name=self.device.display_name,
|
|
|
|
proxy=proxy.name,
|
|
|
|
error=str(scrape_proxy_error),
|
|
|
|
)
|
|
|
|
|
|
|
|
return opener
|