Only turn warnings into errors where needed
warnings.filterwarnings('error') applied to the whole program, so
any warning raised by a library (e.g. a DeprecationWarning) would
crash it. Limit it to the ssh connection (unknown host key) and
the password prompt (no terminal available).
This commit is contained in:
@@ -37,8 +37,6 @@ from glob import glob
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
warnings.filterwarnings('error')
|
||||
|
||||
try:
|
||||
import paramiko
|
||||
from paramiko import RSAKey, Ed25519Key, ECDSAKey, DSSKey
|
||||
@@ -346,7 +344,10 @@ class Backup:
|
||||
ssh.set_missing_host_key_policy(paramiko.WarningPolicy())
|
||||
|
||||
try:
|
||||
ssh.connect(self.ssh_host, username=self.ssh_user)
|
||||
# WarningPolicy emits a UserWarning for unknown host keys
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter('error', UserWarning)
|
||||
ssh.connect(self.ssh_host, username=self.ssh_user)
|
||||
|
||||
return ssh
|
||||
except UserWarning:
|
||||
@@ -374,8 +375,12 @@ class Backup:
|
||||
|
||||
if self.ssh_keyfile is None:
|
||||
try:
|
||||
password = getpass(
|
||||
f'{self.ssh_user}@{self.ssh_host}\'s password: ')
|
||||
# Fail instead of reading the password with echo if no terminal is available
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter('error', GetPassWarning)
|
||||
password = getpass(
|
||||
f'{self.ssh_user}@{self.ssh_host}\'s password: ')
|
||||
|
||||
ssh.connect(self.ssh_host, username=self.ssh_user,
|
||||
password=password)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user