Exemplo n.º 1
0
    def _process_ssh_config(
            host: str, ssh_config_file: str) -> Tuple[Optional[int], str, str]:
        """
        Method to parse ssh config file

        In the future this may move to be a 'helper' function as it should be very similar between
        asyncssh and and paramiko/ssh2-python... for now it can be a static method as there may be
        varying supported args between the transport drivers.

        Args:
            host: host to lookup in ssh config file
            ssh_config_file: string path to ssh config file; passed down from `Scrape`, or the
                `NetworkDriver` or subclasses of it, in most cases.

        Returns:
            Tuple: port to use for ssh, username to use for ssh, identity file (private key) to
                use for ssh auth

        Raises:
            N/A

        """
        ssh = SSHConfig(ssh_config_file)
        host_config = ssh.lookup(host)
        return host_config.port, host_config.user or "", host_config.identity_file or ""
Exemplo n.º 2
0
    def _process_ssh_config(self, ssh_config_file: str) -> None:
        """
        Method to parse ssh config file

        Ensure ssh_config_file is valid (if providing a string path to config file), or resolve
        config file if passed True. Search config file for any private key, if ANY matching key is
        found and user has not provided a private key, set `auth_private_key` to the value of the
        found key. This is because we prefer to use `open_pipes` over `open_pty`!

        Args:
            ssh_config_file: string path to ssh config file; passed down from `Scrape`, or the
                `NetworkDriver` or subclasses of it, in most cases.

        Returns:
            N/A  # noqa: DAR202

        Raises:
            N/A

        """
        ssh = SSHConfig(ssh_config_file=ssh_config_file)
        self.ssh_config_file = ssh.ssh_config_file
        host_config = ssh.lookup(host=self.host)
        if not self.auth_private_key and host_config.identity_file:
            self.auth_private_key = os.path.expanduser(
                host_config.identity_file.strip())
Exemplo n.º 3
0
def test_host_lookup_merged_data():
    ssh_conf = SSHConfig(f"{TEST_DATA_DIR}/files/_ssh_config")
    host = ssh_conf.lookup("scrapli")
    assert repr(host) == (
        "Host {'hosts': 'scrapli', 'hostname': None, 'port': None, 'user': '******', 'address_family': None, "
        "'bind_address': None, 'connect_timeout': None, 'identities_only': None, 'identity_file': "
        f"'{os.path.expanduser('~')}/.ssh/lastresortkey', 'keyboard_interactive': None, 'password_authentication': None, "
        "'preferred_authentication': None}")
Exemplo n.º 4
0
def test_host_lookup_exact_host_in_list():
    ssh_conf = SSHConfig(f"{UNIT_TEST_DIR}_ssh_config")
    host = ssh_conf.lookup("someswitch1")
    assert repr(host) == (
        "Host {'hosts': '1.2.3.4 someswitch1', 'hostname': 'someswitch1.bogus.com', 'port': "
        "1234, 'user': '******', 'address_family': None, 'bind_address': None, 'connect_timeout': "
        f"None, 'identities_only': 'yes', 'identity_file': '{os.path.expanduser('~')}/.ssh/mysshkey', "
        "'keyboard_interactive': None, 'password_authentication': None, 'preferred_authentication':"
        " None}")
Exemplo n.º 5
0
def test_host_lookup_host_fuzzy_multi_match():
    ssh_conf = SSHConfig(f"{TEST_DATA_DIR}/files/_ssh_config")
    host = ssh_conf.lookup("someswitch9999")
    assert repr(host) == (
        "Host {'hosts': 'someswitch?', 'hostname': 'someswitch1.bogus.com', 'port': "
        "1234, 'user': '******', 'address_family': None, 'bind_address': None, 'connect_timeout': "
        f"None, 'identities_only': 'yes', 'identity_file': '{os.path.expanduser('~')}/.ssh/mysshkey', "
        "'keyboard_interactive': None, 'password_authentication': None, 'preferred_authentication': "
        "None}")
Exemplo n.º 6
0
def test_host__repr(real_ssh_config_file_path):
    ssh_conf = SSHConfig(real_ssh_config_file_path)
    assert repr(ssh_conf.hosts["1.2.3.4 someswitch1"]) == (
        "Host {'hosts': '1.2.3.4 someswitch1', 'hostname': 'someswitch1.bogus.com', 'port': "
        "1234, 'user': '******', 'address_family': None, 'bind_address': None, 'connect_timeout': "
        f"None, 'identities_only': 'yes', 'identity_file': '{os.path.expanduser('~')}/.ssh/mysshkey', "
        "'keyboard_interactive': None, 'password_authentication': None, 'preferred_authentication': "
        "None}")
Exemplo n.º 7
0
    def _update_ssh_args_from_ssh_config(self) -> None:
        """
        Update ssh args based on ssh config file data

        Args:
            N/A

        Returns:
            None

        Raises:
            N/A

        """
        ssh = SSHConfig(self.ssh_config_file)
        host_config = ssh.lookup(self.host)

        if host_config.port:
            self.logger.info(
                f"found port for host in ssh configuration file, using this value "
                f"'{host_config.port}' for port!"
            )
            # perhaps this should not override already set port because we dont know if the user
            # provided the port or we just are accepting the default port value... in any case for
            # port, if it is in the ssh config file we will override whatever we currently have
            self.port = host_config.port
        if host_config.user and not self.auth_username:
            self.logger.info(
                f"found username for host in ssh configuration file, using this value "
                f"'{host_config.user}' for auth_username!"
            )
            # only override auth_username if it is not truthy
            self.auth_username = host_config.user
        if host_config.identity_file and not self.auth_private_key:
            self.logger.info(
                f"found identity file for host in ssh configuration file, using this value "
                f"'{host_config.identity_file}' for auth_private_key!"
            )
            # only override auth_private_key if it is not truthy
            self.auth_private_key = host_config.identity_file
Exemplo n.º 8
0
def test_init_ssh_config_file_no_hosts():
    ssh_conf = SSHConfig(f"{TEST_DATA_DIR}/files/__init__.py")
    assert ["*"] == list(ssh_conf.hosts.keys())
    assert ssh_conf.hosts["*"].hosts == "*"
    assert ssh_conf.hosts["*"].hostname is None
    assert ssh_conf.hosts["*"].port is None
    assert ssh_conf.hosts["*"].user == ""
    assert ssh_conf.hosts["*"].address_family is None
    assert ssh_conf.hosts["*"].bind_address is None
    assert ssh_conf.hosts["*"].connect_timeout is None
    assert ssh_conf.hosts["*"].identities_only is None
    assert ssh_conf.hosts["*"].identity_file is None
    assert ssh_conf.hosts["*"].keyboard_interactive is None
    assert ssh_conf.hosts["*"].password_authentication is None
    assert ssh_conf.hosts["*"].preferred_authentication is None
Exemplo n.º 9
0
def test_init_ssh_config_file_no_config_file(fs):
    ssh_conf = SSHConfig("")
    # should only have a single splat host w/ all values set to None/empty
    assert ["*"] == list(ssh_conf.hosts.keys())
    assert ssh_conf.hosts["*"].hosts == "*"
    assert ssh_conf.hosts["*"].hostname is None
    assert ssh_conf.hosts["*"].port is None
    assert ssh_conf.hosts["*"].user == ""
    assert ssh_conf.hosts["*"].address_family is None
    assert ssh_conf.hosts["*"].bind_address is None
    assert ssh_conf.hosts["*"].connect_timeout is None
    assert ssh_conf.hosts["*"].identities_only is None
    assert ssh_conf.hosts["*"].identity_file is None
    assert ssh_conf.hosts["*"].keyboard_interactive is None
    assert ssh_conf.hosts["*"].password_authentication is None
    assert ssh_conf.hosts["*"].preferred_authentication is None
Exemplo n.º 10
0
def test_repr():
    ssh_conf = SSHConfig(f"{UNIT_TEST_DIR}_ssh_config")
    assert repr(ssh_conf) == (
        f"SSHConfig {{'ssh_config_file': '{UNIT_TEST_DIR}_ssh_config', "
        "'hosts': {'1.2.3.4 someswitch1': Host {'hosts': '1.2.3.4 someswitch1', "
        "'hostname': 'someswitch1.bogus.com', 'port': 1234, 'user': '******', 'address_family': None,"
        " 'bind_address': None, 'connect_timeout': None, 'identities_only': 'yes', 'identity_file':"
        f" '{os.path.expanduser('~')}/.ssh/mysshkey', 'keyboard_interactive': None, 'password_authentication': None, "
        "'preferred_authentication': None}, 'someswitch?': Host {'hosts': 'someswitch?', "
        "'hostname': 'someswitch1.bogus.com', 'port': 1234, 'user': '******', 'address_family': "
        "None, 'bind_address': None, 'connect_timeout': None, 'identities_only': 'yes', "
        f"'identity_file': '{os.path.expanduser('~')}/.ssh/mysshkey', 'keyboard_interactive': None, "
        "'password_authentication': None, 'preferred_authentication': None}, '*': Host "
        "{'hosts': '*', 'hostname': None, 'port': None, 'user': '******', 'address_family': None, "
        "'bind_address': None, 'connect_timeout': None, 'identities_only': None, 'identity_file': "
        "None, 'keyboard_interactive': None, 'password_authentication': None, "
        "'preferred_authentication': None}}}")
Exemplo n.º 11
0
    def _process_ssh_config(self, ssh_config_file: str) -> None:
        """
        Method to parse ssh config file

        Ensure ssh_config_file is valid (if providing a string path to config file), or resolve
        config file if passed True.

        Args:
            ssh_config_file: string path to ssh config file; passed down from `Scrape`, or the
                `NetworkDriver` or subclasses of it, in most cases.

        Returns:
            N/A  # noqa: DAR202

        Raises:
            N/A

        """
        ssh = SSHConfig(ssh_config_file=ssh_config_file)
        self.ssh_config_file = ssh.ssh_config_file
Exemplo n.º 12
0
def test_bool_false():
    ssh_conf = SSHConfig(f"{UNIT_TEST_DIR}__init__.py")
    assert bool(ssh_conf) is False
Exemplo n.º 13
0
def test_init_ssh_config_file_explicit():
    ssh_conf = SSHConfig(f"{TEST_DATA_DIR}/files/_ssh_config")
    with open(f"{TEST_DATA_DIR}/files/_ssh_config", "r") as f:
        ssh_config_file = f.read()
    assert ssh_conf.ssh_config == ssh_config_file
Exemplo n.º 14
0
def test_init_ssh_config_invalid_config():
    with pytest.raises(ScrapliTypeError) as exc:
        SSHConfig(None)
    assert str(exc.value) == "`ssh_config_file` expected str, got <class 'NoneType'>"
Exemplo n.º 15
0
def test_bool_false():
    ssh_conf = SSHConfig(f"{TEST_DATA_DIR}/files/__init__.py")
    assert bool(ssh_conf) is False
Exemplo n.º 16
0
def test_bool_true():
    ssh_conf = SSHConfig(f"{TEST_DATA_DIR}/files/_ssh_config")
    assert bool(ssh_conf) is True
Exemplo n.º 17
0
def test_repr():
    ssh_conf = SSHConfig(f"{TEST_DATA_DIR}/files/_ssh_config")
    assert repr(ssh_conf) == (
        f"SSHConfig {{'ssh_config_file': '{TEST_DATA_DIR}/files/_ssh_config', 'hosts': {{'1.2.3.4 someswitch1': Host {{'hosts': '1.2.3.4 someswitch1', 'hostname': 'someswitch1.bogus.com', 'port': 1234, 'user': '******', 'address_family': None, 'bind_address': None, 'connect_timeout': None, 'identities_only': 'yes', 'identity_file': '{os.path.expanduser('~')}/.ssh/mysshkey', 'keyboard_interactive': None, 'password_authentication': None, 'preferred_authentication': None}}, 'someswitch?': Host {{'hosts': 'someswitch?', 'hostname': 'someswitch1.bogus.com', 'port': 1234, 'user': '******', 'address_family': None, 'bind_address': None, 'connect_timeout': None, 'identities_only': 'yes', 'identity_file': '{os.path.expanduser('~')}/.ssh/mysshkey', 'keyboard_interactive': None, 'password_authentication': None, 'preferred_authentication': None}}, 'scrapli': Host {{'hosts': 'scrapli', 'hostname': None, 'port': None, 'user': '******', 'address_family': None, 'bind_address': None, 'connect_timeout': None, 'identities_only': None, 'identity_file': '{os.path.expanduser('~')}/.ssh/lastresortkey', 'keyboard_interactive': None, 'password_authentication': None, 'preferred_authentication': None}}, '*': Host {{'hosts': '*', 'hostname': None, 'port': None, 'user': '******', 'address_family': None, 'bind_address': None, 'connect_timeout': None, 'identities_only': None, 'identity_file': '{os.path.expanduser('~')}/.ssh/lastresortkey', 'keyboard_interactive': None, 'password_authentication': None, 'preferred_authentication': None}}}}}}"
    )
Exemplo n.º 18
0
def test_str():
    ssh_conf = SSHConfig(f"{TEST_DATA_DIR}/files/_ssh_config")
    assert str(ssh_conf) == "SSHConfig Object"
Exemplo n.º 19
0
def test_init_ssh_config_file_explicit(real_ssh_config_file_path):
    ssh_conf = SSHConfig(real_ssh_config_file_path)
    with open(real_ssh_config_file_path, "r") as f:
        ssh_config_file = f.read()
    assert ssh_conf.ssh_config == ssh_config_file
Exemplo n.º 20
0
def test_bool_false(test_data_path):
    ssh_conf = SSHConfig(f"{test_data_path}/files/__init__.py")
    assert bool(ssh_conf) is False
Exemplo n.º 21
0
def test_bool_true():
    ssh_conf = SSHConfig(f"{UNIT_TEST_DIR}_ssh_config")
    assert bool(ssh_conf) is True
Exemplo n.º 22
0
def test_str(real_ssh_config_file_path):
    ssh_conf = SSHConfig(real_ssh_config_file_path)
    assert str(ssh_conf) == "SSHConfig Object"
Exemplo n.º 23
0
def test_str():
    ssh_conf = SSHConfig(f"{UNIT_TEST_DIR}_ssh_config")
    assert str(ssh_conf) == "SSHConfig Object"
Exemplo n.º 24
0
def test_bool_true(real_ssh_config_file_path):
    ssh_conf = SSHConfig(real_ssh_config_file_path)
    assert bool(ssh_conf) is True