def _key_from_filepath(self, filename, klass=None, password=None): """ Attempt to derive a `.PKey` from given string path ``filename``: - If ``filename`` appears to be a cert, the matching private key is loaded. - Otherwise, the filename is assumed to be a private key, and the matching public cert will be loaded if it exists. """ cert_suffix = '-cert.pub' # Assume privkey, not cert, by default if filename.endswith(cert_suffix): key_path = filename[:-len(cert_suffix)] cert_path = filename else: key_path = filename cert_path = filename + cert_suffix # Blindly try the key path; if no private key, nothing will work. if klass: key = klass.from_private_key_file(key_path, password) else: key = load_private_key_file(key_path, password) # TODO: change this to 'Loading' instead of 'Trying' sometime; probably # when #387 is released, since this is a critical log message users are # likely testing/filtering for (bah.) msg = "Trying discovered key {} in {}".format( hexlify(key.get_fingerprint()).decode(), key_path, ) self._log(DEBUG, msg) # Attempt to load cert if it exists. if os.path.isfile(cert_path): key.load_certificate(cert_path) self._log(DEBUG, "Adding public certificate {}".format(cert_path)) return key
def _key_from_filepath(self, filename, klass=None, password=None): """ Attempt to derive a `.PKey` from given string path ``filename``: - If ``filename`` appears to be a cert, the matching private key is loaded. - Otherwise, the filename is assumed to be a private key, and the matching public cert will be loaded if it exists. """ cert_suffix = '-cert.pub' # Assume privkey, not cert, by default if filename.endswith(cert_suffix): key_path = filename[:-len(cert_suffix)] cert_path = filename else: key_path = filename cert_path = filename + cert_suffix # Blindly try the key path; if no private key, nothing will work. if klass: key = klass.from_private_key_file(key_path, password) else: key = load_private_key_file(key_path, password) self._log(DEBUG, "Trying discovered key %s in %s" % ( key.get_fingerprint_sha256_b64(), key_path, )) # Attempt to load cert if it exists. if os.path.isfile(cert_path): key.load_certificate(cert_path) self._log(DEBUG, "Adding public certificate {}".format(cert_path)) return key
def test_autodetect_password(self): key = load_private_key_file(_support("test_rsa_password.key"), password="******") self.assertIsInstance(key, RSAKey)
def test_autodetect_dsa(self): key = load_private_key_file(_support("test_dss.key")) self.assertIsInstance(key, DSSKey)
def test_autodetect_ecdsa(self): key = load_private_key_file(_support("test_ecdsa_384.key")) self.assertIsInstance(key, ECDSAKey)
def test_autodetect_ed25519(self): key = load_private_key_file(_support("test_ed25519.key")) self.assertIsInstance(key, Ed25519Key)