From a2b06b797e4b4657e8dcb73a762d15779b24de7b Mon Sep 17 00:00:00 2001 From: Vladislav Doster Date: Wed, 30 Oct 2024 17:59:12 -0500 Subject: [PATCH 1/3] refactor: remove reimported module Remove modules that are reimported multiple times. Signed-off-by: Vladislav Doster --- app/backup.py | 2 +- app/import_to_bitwarden.py | 1 - app/import_to_keepass.py | 1 - app/main.py | 1 - app/schedule_backup.py | 1 - scripts/alldecrypt-zip.py | 1 - scripts/json-only-decrypt.py | 1 - 7 files changed, 1 insertion(+), 7 deletions(-) diff --git a/app/backup.py b/app/backup.py index 6be3f20..364c8c0 100755 --- a/app/backup.py +++ b/app/backup.py @@ -19,7 +19,7 @@ from botocore.client import Config import pytz from botocore.exceptions import ClientError, NoCredentialsError, EndpointConnectionError -from botocore.exceptions import NoCredentialsError, PartialCredentialsError +from botocore.exceptions import PartialCredentialsError # Carga las variables de entorno desde el archivo .env load_dotenv() diff --git a/app/import_to_bitwarden.py b/app/import_to_bitwarden.py index f9b80a1..d3d8bbc 100755 --- a/app/import_to_bitwarden.py +++ b/app/import_to_bitwarden.py @@ -9,7 +9,6 @@ from config import configure_logging from tqdm import tqdm from colorama import init, Fore -import time from terminaltexteffects.effects import effect_rain, effect_beams, effect_wipe, effect_matrix # Initialize colorama diff --git a/app/import_to_keepass.py b/app/import_to_keepass.py index e9b4eb3..fe67b3c 100755 --- a/app/import_to_keepass.py +++ b/app/import_to_keepass.py @@ -10,7 +10,6 @@ from dotenv import load_dotenv from secrets_manager import retrieve_secrets from bitwarden_client import BitwardenClient, client_settings_from_dict -import time from terminaltexteffects.effects import effect_rain, effect_beams, effect_decrypt, effect_matrix ##-------------Interactive------------- diff --git a/app/main.py b/app/main.py index 08b1cbe..7786c24 100755 --- a/app/main.py +++ b/app/main.py @@ -4,7 +4,6 @@ from secrets_manager import retrieve_secrets from backup import backup_bitwarden from colorama import init, Fore, Style # Importar colorama -import time from terminaltexteffects.effects import effect_rain, effect_beams def clear_screen(): diff --git a/app/schedule_backup.py b/app/schedule_backup.py index 968f110..8bdb0ff 100755 --- a/app/schedule_backup.py +++ b/app/schedule_backup.py @@ -11,7 +11,6 @@ from icalendar import Calendar as iCalendar, Event as iEvent from terminaltexteffects.effects import effect_rain, effect_beams, effect_wipe import time -import os import sys # Load environment variables from .env file diff --git a/scripts/alldecrypt-zip.py b/scripts/alldecrypt-zip.py index f1814d4..2aa5f6e 100755 --- a/scripts/alldecrypt-zip.py +++ b/scripts/alldecrypt-zip.py @@ -11,7 +11,6 @@ from argon2.low_level import hash_secret_raw, Type from colorama import init, Fore, Style from tqdm import tqdm -import time from terminaltexteffects.effects import effect_rain, effect_beams, effect_decrypt, effect_matrix # Initialize colorama diff --git a/scripts/json-only-decrypt.py b/scripts/json-only-decrypt.py index c5cc0b8..27b1de4 100755 --- a/scripts/json-only-decrypt.py +++ b/scripts/json-only-decrypt.py @@ -10,7 +10,6 @@ from argon2.low_level import hash_secret_raw, Type from colorama import init, Fore, Style from tqdm import tqdm -import time from terminaltexteffects.effects import effect_rain, effect_beams, effect_decrypt, effect_matrix # Initialize colorama From 9be2aee445d672f807d520c6fd0d210280632506 Mon Sep 17 00:00:00 2001 From: Vladislav Doster Date: Wed, 30 Oct 2024 17:31:59 -0500 Subject: [PATCH 2/3] refactor: remove unnecessary f-string It is wasteful to use `f-string` mechanism if there are no expressions to be extrapolated. Signed-off-by: Vladislav Doster --- app/backup.py | 20 ++++++++++---------- app/import_to_bitwarden.py | 8 ++++---- app/import_to_keepass.py | 2 +- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/app/backup.py b/app/backup.py index 364c8c0..875a5af 100755 --- a/app/backup.py +++ b/app/backup.py @@ -1106,7 +1106,7 @@ def backup_bitwarden(env_vars, secrets, drive_service): with open(zip_filepath, "rb") as f: dbx.files_upload(f.read(), f"{folder_path}/bw-backup_{timestamp}.zip") logging.info(f"{Fore.GREEN}ZIP file uploaded to Dropbox") - notification_message = f"ZIP File Uploaded and Encrypted to Dropbox Successfully ✅📚🔐☁️" + notification_message = "ZIP File Uploaded and Encrypted to Dropbox Successfully ✅📚🔐☁️" send_telegram_notification(notification_message, env_vars["TELEGRAM_TOKEN"], env_vars["TELEGRAM_CHAT_ID"]) send_discord_notification(notification_message, env_vars["DISCORD_WEBHOOK_URL"]) send_slack_notification(notification_message, env_vars["SLACK_WEBHOOK_URL"]) @@ -1123,7 +1123,7 @@ def backup_bitwarden(env_vars, secrets, drive_service): backup_folder_id = create_folder_if_not_exists(drive_service, "bitwarden-drive-backup", parent_folder_id=env_vars["GOOGLE_FOLDER_ID"]) upload_file_to_drive(drive_service, zip_filepath, backup_folder_id) logging.info(f"{Fore.GREEN}ZIP file uploaded to Google Drive") - notification_message = f"ZIP File Uploaded and Encrypted to Google Drive Successfully ✅📚🔐☁️" + notification_message = "ZIP File Uploaded and Encrypted to Google Drive Successfully ✅📚🔐☁️" send_telegram_notification(notification_message, env_vars["TELEGRAM_TOKEN"], env_vars["TELEGRAM_CHAT_ID"]) send_discord_notification(notification_message, env_vars["DISCORD_WEBHOOK_URL"]) send_slack_notification(notification_message, env_vars["SLACK_WEBHOOK_URL"]) @@ -1139,7 +1139,7 @@ def backup_bitwarden(env_vars, secrets, drive_service): try: upload_file_to_pcloud(zip_filepath, "bitwarden-drive-backup", secrets["PCLOUD_USERNAME"], secrets["PCLOUD_PASSWORD"]) logging.info(f"{Fore.GREEN}ZIP file uploaded to pCloud") - notification_message = f"ZIP File Uploaded and Encrypted to pCloud Successfully ✅📚🔐☁️" + notification_message = "ZIP File Uploaded and Encrypted to pCloud Successfully ✅📚🔐☁️" send_telegram_notification(notification_message, env_vars["TELEGRAM_TOKEN"], env_vars["TELEGRAM_CHAT_ID"]) send_discord_notification(notification_message, env_vars["DISCORD_WEBHOOK_URL"]) send_slack_notification(notification_message, env_vars["SLACK_WEBHOOK_URL"]) @@ -1155,7 +1155,7 @@ def backup_bitwarden(env_vars, secrets, drive_service): try: upload_file_to_mega(zip_filepath, secrets["MEGA_EMAIL"], secrets["MEGA_PASSWORD"]) logging.info(f"{Fore.GREEN}ZIP file uploaded to Mega") - notification_message = f"ZIP File Uploaded and Encrypted to Mega Successfully ✅📚🔐☁️" + notification_message = "ZIP File Uploaded and Encrypted to Mega Successfully ✅📚🔐☁️" send_telegram_notification(notification_message, env_vars["TELEGRAM_TOKEN"], env_vars["TELEGRAM_CHAT_ID"]) send_discord_notification(notification_message, env_vars["DISCORD_WEBHOOK_URL"]) send_slack_notification(notification_message, env_vars["SLACK_WEBHOOK_URL"]) @@ -1171,7 +1171,7 @@ def backup_bitwarden(env_vars, secrets, drive_service): try: upload_file_to_nextcloud(zip_filepath, secrets["NEXTCLOUD_URL"], secrets["NEXTCLOUD_USERNAME"], secrets["NEXTCLOUD_PASSWORD"]) logging.info(f"{Fore.GREEN}ZIP file uploaded to Nextcloud") - notification_message = f"ZIP File Uploaded and Encrypted to Nextcloud Successfully ✅📚🔐☁️" + notification_message = "ZIP File Uploaded and Encrypted to Nextcloud Successfully ✅📚🔐☁️" send_telegram_notification(notification_message, env_vars["TELEGRAM_TOKEN"], env_vars["TELEGRAM_CHAT_ID"]) send_discord_notification(notification_message, env_vars["DISCORD_WEBHOOK_URL"]) send_slack_notification(notification_message, env_vars["SLACK_WEBHOOK_URL"]) @@ -1187,7 +1187,7 @@ def backup_bitwarden(env_vars, secrets, drive_service): try: upload_file_to_seafile(zip_filepath, secrets["SEAFILE_SERVER_URL"], secrets["SEAFILE_USERNAME"], secrets["SEAFILE_PASSWORD"]) logging.info(f"{Fore.GREEN}ZIP file uploaded to Seafile") - notification_message = f"ZIP File Uploaded and Encrypted to Seafile Successfully ✅📚🔐☁️" + notification_message = "ZIP File Uploaded and Encrypted to Seafile Successfully ✅📚🔐☁️" send_telegram_notification(notification_message, env_vars["TELEGRAM_TOKEN"], env_vars["TELEGRAM_CHAT_ID"]) send_discord_notification(notification_message, env_vars["DISCORD_WEBHOOK_URL"]) send_slack_notification(notification_message, env_vars["SLACK_WEBHOOK_URL"]) @@ -1203,7 +1203,7 @@ def backup_bitwarden(env_vars, secrets, drive_service): try: upload_file_to_filebase(zip_filepath, secrets["FILEBASE_ACCESS_KEY"], secrets["FILEBASE_SECRET_KEY"], f"bw-backup_{timestamp}.zip") logging.info(f"{Fore.GREEN}ZIP file uploaded to Filebase") - notification_message = f"ZIP File Uploaded and Encrypted to Filebase Successfully ✅📚🔐☁️" + notification_message = "ZIP File Uploaded and Encrypted to Filebase Successfully ✅📚🔐☁️" send_telegram_notification(notification_message, env_vars["TELEGRAM_TOKEN"], env_vars["TELEGRAM_CHAT_ID"]) send_discord_notification(notification_message, env_vars["DISCORD_WEBHOOK_URL"]) send_slack_notification(notification_message, env_vars["SLACK_WEBHOOK_URL"]) @@ -1241,7 +1241,7 @@ def backup_bitwarden(env_vars, secrets, drive_service): pbar.update(progress_stages[10]["update"]) else: logging.error(f"{Fore.RED}Failed to create Todoist task") - notification_message = f"Failed to create Todoist task" + notification_message = "Failed to create Todoist task" except Exception as e: logging.error(f"{Fore.RED}Error creating Todoist task: {e}") notification_message = f"Error creating Todoist task: {e}" @@ -1263,7 +1263,7 @@ def backup_bitwarden(env_vars, secrets, drive_service): caldav_username=secrets["CALDAV_USERNAME"], caldav_password=secrets["CALDAV_PASSWORD"] ) - notification_message = f"CalDAV Event Successfully Created on Bitwarden New Backup Calendar ✅📅" + notification_message = "CalDAV Event Successfully Created on Bitwarden New Backup Calendar ✅📅" send_telegram_notification(notification_message, env_vars["TELEGRAM_TOKEN"], env_vars["TELEGRAM_CHAT_ID"]) send_discord_notification(notification_message, env_vars["DISCORD_WEBHOOK_URL"]) send_slack_notification(notification_message, env_vars["SLACK_WEBHOOK_URL"]) @@ -1281,7 +1281,7 @@ def backup_bitwarden(env_vars, secrets, drive_service): send_email_with_attachment(env_vars["SMTP_SERVER"], env_vars["SMTP_PORT"], env_vars["SMTP_USERNAME"], env_vars["SMTP_PASSWORD"], env_vars["SENDER_EMAIL"], env_vars["EMAIL_RECIPIENT"], "Bitwarden Backup", f"", zip_filepath) logging.info(f"{Fore.GREEN}Email with attachment sent successfully") - notification_message = f"ZIP File Sent and Encrypted to Email Successfully ✅📚🔐📧" + notification_message = "ZIP File Sent and Encrypted to Email Successfully ✅📚🔐📧" send_telegram_notification(notification_message, env_vars["TELEGRAM_TOKEN"], env_vars["TELEGRAM_CHAT_ID"]) send_discord_notification(notification_message, env_vars["DISCORD_WEBHOOK_URL"]) send_slack_notification(notification_message, env_vars["SLACK_WEBHOOK_URL"]) diff --git a/app/import_to_bitwarden.py b/app/import_to_bitwarden.py index d3d8bbc..0ac517a 100755 --- a/app/import_to_bitwarden.py +++ b/app/import_to_bitwarden.py @@ -371,7 +371,7 @@ def restore_items_and_attachments(env_vars, secrets, bw_session, sleep_milliseco if result.returncode != 0: logging.error(f"Error during import: {result.stderr}") raise subprocess.CalledProcessError(result.returncode, result.args, result.stdout, result.stderr) - logging.info(f"Backup imported to Bitwarden successfully") + logging.info("Backup imported to Bitwarden successfully") except subprocess.CalledProcessError as e: logging.error(f"Error during import: {e}. Trying to unlock the vault again.") bw_session = unlock_vault(secrets["BW_PASSWORD"]) @@ -380,9 +380,9 @@ def restore_items_and_attachments(env_vars, secrets, bw_session, sleep_milliseco if result.returncode != 0: logging.error(f"Error during import after unlocking the vault: {result.stderr}") raise subprocess.CalledProcessError(result.returncode, result.args, result.stdout, result.stderr) - logging.info(f"Backup imported to Bitwarden successfully after unlocking the vault") + logging.info("Backup imported to Bitwarden successfully after unlocking the vault") - logging.info(f"Restoring items finished") + logging.info("Restoring items finished") for _ in tqdm(range(100), desc=f"{Fore.GREEN}Bitwarden JSON Import{Fore.RESET}", ncols=100, bar_format="{l_bar}%s{bar}%s{r_bar}" % (Fore.BLUE, Fore.RESET)): time.sleep(0.01) @@ -392,7 +392,7 @@ def restore_items_and_attachments(env_vars, secrets, bw_session, sleep_milliseco bitwarden_items = list_bitwarden_items(bw_session) if not bitwarden_items: - logging.error(f"Failed to retrieve Bitwarden items. Cannot proceed with attachment.") + logging.error("Failed to retrieve Bitwarden items. Cannot proceed with attachment.") return attach_files_using_info(attachments_info_file_path, decrypted_attachments_dir_path, bitwarden_items, bw_session, secrets["BW_PASSWORD"]) diff --git a/app/import_to_keepass.py b/app/import_to_keepass.py index fe67b3c..4758fb3 100755 --- a/app/import_to_keepass.py +++ b/app/import_to_keepass.py @@ -405,7 +405,7 @@ def main(): zf.pwd = ZIP_PASSWORD.encode() zf.extractall(temp_dir) - print(f"Decrypted ZIP contents extracted to temporary directory.") + print("Decrypted ZIP contents extracted to temporary directory.") encrypted_json_file_path = os.path.join(temp_dir, f"bw-backup_{TIMESTAMP}.json") From 9abff9112b18d059f93e78a4d7421b35b97351b0 Mon Sep 17 00:00:00 2001 From: Vladislav Doster Date: Wed, 30 Oct 2024 17:50:03 -0500 Subject: [PATCH 3/3] refactor: use `sys.exit()` calls The `exit` or `quit` functions don't exist at top-level if python is started with the `-S` flag, and will raise an error. Signed-off-by: Vladislav Doster --- app/import_to_bitwarden.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/import_to_bitwarden.py b/app/import_to_bitwarden.py index 0ac517a..34c6464 100755 --- a/app/import_to_bitwarden.py +++ b/app/import_to_bitwarden.py @@ -7,6 +7,7 @@ from bitwarden_client import BitwardenClient, client_settings_from_dict, DeviceType, login_bitwarden, unlock_vault from secrets_manager import retrieve_secrets from config import configure_logging +import sys from tqdm import tqdm from colorama import init, Fore from terminaltexteffects.effects import effect_rain, effect_beams, effect_wipe, effect_matrix @@ -507,7 +508,7 @@ def attach_files_using_info(attachments_info_file_path, attachments_dir, bitward bw_session = login_bitwarden(secrets["BW_USERNAME"], secrets["BW_PASSWORD"], secrets["BW_TOTP_SECRET"]) if bw_session is None: logging.error("Failed to obtain Bitwarden session") - exit(1) + sys.exit(1) except Exception as e: logging.error(f"Error authenticating to Bitwarden: {e}")