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
Changes from 1 commit
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
22 changes: 13 additions & 9 deletions src/vorta/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,15 @@ def choose_file_dialog(parent, title, want_folder=True):
return dialog


def is_ssh_file(filepath: str) -> bool:
def is_ssh_private_key_file(filepath: str) -> bool:
"""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
try:
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
except UnicodeDecodeError:
return False


def get_private_keys() -> List[str]:
Expand All @@ -198,18 +201,19 @@ def get_private_keys() -> List[str]:
if key.endswith('.pub') or key.startswith('known_hosts') or key == 'config':
continue
try:
if is_ssh_file(key_file):
if is_ssh_private_key_file(key_file):
available_private_keys.append(key)
except (PermissionError, UnicodeDecodeError):
except PermissionError:
# 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
# (if the user doesn't have permission to read the file, it's not an unexpected error).
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
else:
real-yfprojects marked this conversation as resolved.
Show resolved Hide resolved
raise

return available_private_keys

Expand Down