How to use the sshtunnel.SSHTunnelForwarder.read_private_key_file function in sshtunnel

To help you get started, we’ve selected a few sshtunnel examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pahaz / sshtunnel / tests / test_forwarder.py View on Github external
get_test_data_path(PKEY_FILE)
        )
        self.assertEqual(pkey, _pkey)

        # Using a wrong password returns None
        self.assertIsNone(sshtunnel.SSHTunnelForwarder.read_private_key_file(
            encr_pkey,
            pkey_password='bad password',
            logger=self.log
        ))
        self.assertIn("Private key file ({0}) could not be loaded as type "
                      "{1} or bad password"
                      .format(encr_pkey, type(_pkey)),
                      self.sshtunnel_log_messages['debug'])
        # Using no password on an encrypted key returns None
        self.assertIsNone(sshtunnel.SSHTunnelForwarder.read_private_key_file(
            encr_pkey,
            logger=self.log
        ))
        self.assertIn('Password is required for key {0}'.format(encr_pkey),
                      self.sshtunnel_log_messages['error'])
github pahaz / sshtunnel / tests / test_forwarder.py View on Github external
def test_read_private_key_file(self):
        """ Test that an encrypted private key can be opened """
        encr_pkey = get_test_data_path(ENCRYPTED_PKEY_FILE)
        pkey = sshtunnel.SSHTunnelForwarder.read_private_key_file(
            encr_pkey,
            pkey_password='sshtunnel',
            logger=self.log
        )
        _pkey = paramiko.RSAKey.from_private_key_file(
            get_test_data_path(PKEY_FILE)
        )
        self.assertEqual(pkey, _pkey)

        # Using a wrong password returns None
        self.assertIsNone(sshtunnel.SSHTunnelForwarder.read_private_key_file(
            encr_pkey,
            pkey_password='bad password',
            logger=self.log
        ))
        self.assertIn("Private key file ({0}) could not be loaded as type "
github pahaz / sshtunnel / tests / test_forwarder.py View on Github external
def test_read_private_key_file(self):
        """ Test that an encrypted private key can be opened """
        encr_pkey = get_test_data_path(ENCRYPTED_PKEY_FILE)
        pkey = sshtunnel.SSHTunnelForwarder.read_private_key_file(
            encr_pkey,
            pkey_password='sshtunnel',
            logger=self.log
        )
        _pkey = paramiko.RSAKey.from_private_key_file(
            get_test_data_path(PKEY_FILE)
        )
        self.assertEqual(pkey, _pkey)

        # Using a wrong password returns None
        self.assertIsNone(sshtunnel.SSHTunnelForwarder.read_private_key_file(
            encr_pkey,
            pkey_password='bad password',
            logger=self.log
        ))
        self.assertIn("Private key file ({0}) could not be loaded as type "
                      "{1} or bad password"
                      .format(encr_pkey, type(_pkey)),
                      self.sshtunnel_log_messages['debug'])
        # Using no password on an encrypted key returns None
        self.assertIsNone(sshtunnel.SSHTunnelForwarder.read_private_key_file(
            encr_pkey,
            logger=self.log
        ))
        self.assertIn('Password is required for key {0}'.format(encr_pkey),
                      self.sshtunnel_log_messages['error'])
github pahaz / sshtunnel / sshtunnel.py View on Github external
"""
        keys = SSHTunnelForwarder.get_agent_keys(logger=logger) \
            if allow_agent else []

        if host_pkey_directories is not None:
            paramiko_key_types = {'rsa': paramiko.RSAKey,
                                  'dsa': paramiko.DSSKey,
                                  'ecdsa': paramiko.ECDSAKey,
                                  'ed25519': paramiko.Ed25519Key}
            for directory in host_pkey_directories or [DEFAULT_SSH_DIRECTORY]:
                for keytype in paramiko_key_types.keys():
                    ssh_pkey_expanded = os.path.expanduser(
                        os.path.join(directory, 'id_{}'.format(keytype))
                    )
                    if os.path.isfile(ssh_pkey_expanded):
                        ssh_pkey = SSHTunnelForwarder.read_private_key_file(
                            pkey_file=ssh_pkey_expanded,
                            logger=logger,
                            key_type=paramiko_key_types[keytype]
                        )
                        if ssh_pkey:
                            keys.append(ssh_pkey)
        if logger:
            logger.info('{0} keys loaded from host directory'.format(
                len(keys))
            )

        return keys