Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove paramiko #1606

Merged
merged 19 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ include_package_data = true
python_requires = >=3.7
install_requires =
appdirs
paramiko
pyqt5
peewee
psutil
Expand Down
1 change: 0 additions & 1 deletion src/vorta/borg/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ def started_event(self):

@classmethod
def prepare(cls, params):

# Build fake profile because we don't have it in the DB yet.
profile = FakeProfile(
999,
Expand Down
1 change: 0 additions & 1 deletion src/vorta/qt_single_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class QtSingleApplication(QApplication):
message_received_event = pyqtSignal(str)

def __init__(self, id, *argv):

super().__init__(*argv)
self._id = id

Expand Down
3 changes: 0 additions & 3 deletions src/vorta/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class ScheduleStatus(NamedTuple):


class VortaScheduler(QtCore.QObject):

#: The schedule for the profile with the given id changed.
schedule_changed = QtCore.pyqtSignal(int)

Expand Down Expand Up @@ -196,7 +195,6 @@ def set_timer_for_profile(self, profile_id: int):
return

with self.lock: # Acquire lock

self.remove_job(profile_id) # reset schedule

pause = self.pauses.get(profile_id)
Expand Down Expand Up @@ -290,7 +288,6 @@ def set_timer_for_profile(self, profile_id: int):

# handle missing of a scheduled time
if next_time <= dt.now():

if profile.schedule_make_up_missed:
self.lock.release()
try:
Expand Down
60 changes: 25 additions & 35 deletions src/vorta/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@
import unicodedata
from datetime import datetime as dt
from functools import reduce
from typing import Any, Callable, Iterable, Optional, Tuple, TypeVar
from typing import Any, Callable, Iterable, List, Optional, Tuple, TypeVar
import psutil
from paramiko import SSHException
from paramiko.ecdsakey import ECDSAKey
from paramiko.ed25519key import Ed25519Key
from paramiko.rsakey import RSAKey
from PyQt5 import QtCore
from PyQt5.QtCore import QFileInfo, QThread, pyqtSignal
from PyQt5.QtWidgets import QApplication, QFileDialog, QSystemTrayIcon
Expand Down Expand Up @@ -179,9 +175,16 @@ def choose_file_dialog(parent, title, want_folder=True):
return dialog


def get_private_keys():
def is_ssh_file(filepath: str) -> bool:
real-yfprojects marked this conversation as resolved.
Show resolved Hide resolved
"""Check if the file is a SSH key."""
with open(filepath, 'r') as f:
first_line = f.readline()
pattern = r'^-----BEGIN(\s\w+)? PRIVATE KEY-----'
return re.match(pattern, first_line) is not None


def get_private_keys() -> List[str]:
"""Find SSH keys in standard folder."""
key_formats = [RSAKey, ECDSAKey, Ed25519Key]

ssh_folder = os.path.expanduser('~/.ssh')

Expand All @@ -191,35 +194,22 @@ def get_private_keys():
key_file = os.path.join(ssh_folder, key)
if not os.path.isfile(key_file):
continue
for key_format in key_formats:
try:
parsed_key = key_format.from_private_key_file(key_file)
key_details = {
'filename': key,
'format': parsed_key.get_name(),
'bits': parsed_key.get_bits(),
'fingerprint': parsed_key.get_fingerprint().hex(),
}
available_private_keys.append(key_details)
except (
SSHException,
UnicodeDecodeError,
IsADirectoryError,
IndexError,
ValueError,
PermissionError,
NotImplementedError,
):
logger.debug(
f'Expected error parsing file in .ssh: {key} (You can safely ignore this)', exc_info=True
)
# ignore config, known_hosts*, *.pub, etc.
if key.endswith('.pub') or key.startswith('known_hosts') or key == 'config':
continue
try:
if is_ssh_file(key_file):
available_private_keys.append(key)
except (PermissionError, UnicodeDecodeError):
real-yfprojects marked this conversation as resolved.
Show resolved Hide resolved
# Handling PermissionError separately from OSError because it can be safely ignored
# (If the user doesn't have permission to read the file, it's not an unexpected error)
# If UnicodeDecodeError is raised, it's because the file is not a valid SSH key
diivi marked this conversation as resolved.
Show resolved Hide resolved
logger.debug(f'Expected error parsing file in .ssh: {key} (You can safely ignore this)', exc_info=True)
real-yfprojects marked this conversation as resolved.
Show resolved Hide resolved
continue
except OSError as e:
if e.errno == errno.ENXIO:
# when key_file is a (ControlPath) socket
continue
except OSError as e:
if e.errno == errno.ENXIO:
# when key_file is a (ControlPath) socket
continue
else:
raise
real-yfprojects marked this conversation as resolved.
Show resolved Hide resolved

return available_private_keys

Expand Down
5 changes: 1 addition & 4 deletions src/vorta/views/repo_add_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,7 @@ def init_encryption(self):
def init_ssh_key(self):
keys = get_private_keys()
for key in keys:
self.sshComboBox.addItem(
f'{key["filename"]} ({key["format"]}:{key["fingerprint"]})',
key['filename'],
)
self.sshComboBox.addItem(f'{key}', key)

def validate(self):
"""Pre-flight check for valid input and borg binary."""
Expand Down
2 changes: 1 addition & 1 deletion src/vorta/views/repo_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def init_ssh(self):
self.sshComboBox.clear()
self.sshComboBox.addItem(self.tr('Automatically choose SSH Key (default)'), None)
for key in keys:
self.sshComboBox.addItem(f'{key["filename"]} ({key["format"]})', key['filename'])
self.sshComboBox.addItem(f'{key}', key)

def toggle_available_compression(self):
use_zstd = borg_compat.check('ZSTD')
Expand Down
5 changes: 0 additions & 5 deletions src/vorta/views/ssh_dialog.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import os
from paramiko.ecdsakey import ECDSAKey
from paramiko.ed25519key import Ed25519Key
from paramiko.rsakey import RSAKey
from PyQt5 import uic
from PyQt5.QtCore import QProcess, Qt
from PyQt5.QtWidgets import QApplication, QDialogButtonBox
Expand All @@ -10,8 +7,6 @@
uifile = get_asset('UI/sshadd.ui')
SSHAddUI, SSHAddBase = uic.loadUiType(uifile)

FORMAT_MAPPING = {'ed25519': Ed25519Key, 'rsa': RSAKey, 'ecdsa': ECDSAKey}


class SSHAddWindow(SSHAddBase, SSHAddUI):
def __init__(self):
Expand Down