diff --git a/dbsync.py b/dbsync.py index f5c8c68..a08bf23 100644 --- a/dbsync.py +++ b/dbsync.py @@ -32,7 +32,14 @@ class DbSyncError(Exception): - pass + default_print_password = "password='*****'" + + def __init__(self, message): + # escaped password + message = re.sub(r'password=[\"\'].+[\"\'](?=\s)', self.default_print_password, message) + # not escaped password + message = re.sub(r'password=\S+', self.default_print_password, message) + super().__init__(message) def _add_quotes_to_schema_name(schema: str) -> str: diff --git a/test/test_dbsyncerror.py b/test/test_dbsyncerror.py new file mode 100644 index 0000000..e759c6c --- /dev/null +++ b/test/test_dbsyncerror.py @@ -0,0 +1,21 @@ +import pytest + +from dbsync import DbSyncError + + +@pytest.mark.parametrize("password", ['password=\"my_secret password 8417\\.\"', + 'password=\'my_secret password\'', + "password=my_secret_password84189./+-" + ]) +def test_DbSyncError_password_print(password: str): + host = "host=\"localhost\"" + user = "user=user" + + conn_string = f"{user} {password} {host}" + + with pytest.raises(DbSyncError) as err: + raise DbSyncError(conn_string) + assert password not in str(err) + assert user in str(err) + assert host in str(err) + assert DbSyncError.default_print_password in str(err.value)