From f9c3fa9b6f92075cc59ee5791d47b031ff511cf5 Mon Sep 17 00:00:00 2001 From: Ronit Jain Date: Tue, 17 Oct 2023 04:12:42 +0530 Subject: [PATCH] self review --- lean/commands/cloud/pull.py | 17 +++++++++-------- lean/commands/cloud/push.py | 10 ++++------ lean/commands/decrypt.py | 2 +- lean/commands/encrypt.py | 2 +- lean/components/util/encryption_helper.py | 8 ++++++++ 5 files changed, 23 insertions(+), 16 deletions(-) diff --git a/lean/commands/cloud/pull.py b/lean/commands/cloud/pull.py index 5740e42b..7bd683d0 100644 --- a/lean/commands/cloud/pull.py +++ b/lean/commands/cloud/pull.py @@ -24,10 +24,10 @@ @option("--pull-bootcamp", is_flag=True, default=False, help="Pull Boot Camp projects (disabled by default)") @option("--encrypt", is_flag=True, default=False, - help="Encrypt your cloud files with a key") + help="Pull your cloud files and encrypt them before saving on your local drive") @option("--decrypt", is_flag=True, default=False, - help="Decrypt your cloud files with a key") + help="Pull your cloud files and decrypt them before saving on your local drive") @option("--key", type=PathParameter(exists=True, file_okay=True, dir_okay=False), help="Path to the encryption key to use") @@ -41,11 +41,9 @@ def pull(project: Optional[str], pull_bootcamp: bool, encrypt: Optional[bool], d encryption_action = None - if encrypt and decrypt: - raise RuntimeError(f"Cannot encrypt and decrypt at the same time.") - if key is None and (encrypt or decrypt): - raise RuntimeError(f"Encryption key is required when encrypting or decrypting.") - + from lean.components.util.encryption_helper import validate_user_inputs_for_cloud_push_pull_commands + validate_user_inputs_for_cloud_push_pull_commands(encrypt, decrypt, key) + if encrypt: encryption_action = ActionType.ENCRYPT if decrypt: @@ -77,8 +75,11 @@ def pull(project: Optional[str], pull_bootcamp: bool, encrypt: Optional[bool], d if project is None and not pull_bootcamp: projects_to_pull = [p for p in projects_to_pull if not p.name.startswith("Boot Camp/")] - if len(projects_to_pull) > 1 and key is not None: + if key is not None and len(projects_to_pull) > 1: raise RuntimeError(f"Cannot encrypt or decrypt more than one project at a time.") + elif key is not None: + # the encryption key info is available when reading the project individually from API + projects_to_pull = [api_client.projects.get(project.projectId, project.organizationId) for project in projects_to_pull] pull_manager = container.pull_manager pull_manager.pull_projects(projects_to_pull, all_projects, encryption_action, key) diff --git a/lean/commands/cloud/push.py b/lean/commands/cloud/push.py index 67fce1dc..e8781dfe 100644 --- a/lean/commands/cloud/push.py +++ b/lean/commands/cloud/push.py @@ -27,10 +27,10 @@ help="Path to the local project to push (all local projects if not specified)") @option("--encrypt", is_flag=True, default=False, - help="Encrypt your cloud files with a key") + help="Push your local files and encrypt them before saving on the cloud") @option("--decrypt", is_flag=True, default=False, - help="Decrypt your cloud files with a key") + help="Push your local files and decrypt them before saving on the cloud") @option("--key", type=PathParameter(exists=True, file_okay=True, dir_okay=False), help="Path to the encryption key to use") @@ -45,10 +45,8 @@ def push(project: Optional[Path], encrypt: Optional[bool], decrypt: Optional[boo encryption_key_id = None encryption_action = None - if encrypt and decrypt: - raise RuntimeError(f"Cannot encrypt and decrypt at the same time.") - if key is None and (encrypt or decrypt): - raise RuntimeError(f"Encryption key is required when encrypting or decrypting.") + from lean.components.util.encryption_helper import validate_user_inputs_for_cloud_push_pull_commands + validate_user_inputs_for_cloud_push_pull_commands(encrypt, decrypt, key) if encrypt: encryption_action = ActionType.ENCRYPT diff --git a/lean/commands/decrypt.py b/lean/commands/decrypt.py index 29077603..7b19a562 100644 --- a/lean/commands/decrypt.py +++ b/lean/commands/decrypt.py @@ -26,7 +26,7 @@ help="Path to the decryption key to use") def decrypt(project: Path, key: Optional[Path]) -> None: - """Decrypt the project using the specified decryption key. + """Decrypt your local project using the specified decryption key. :param project: The project to decrypt :param key: The path to the decryption key to use diff --git a/lean/commands/encrypt.py b/lean/commands/encrypt.py index c9b12fc3..5c187522 100644 --- a/lean/commands/encrypt.py +++ b/lean/commands/encrypt.py @@ -26,7 +26,7 @@ help="Path to the encryption key to use") def encrypt(project: Path, key: Optional[Path]) -> None: - """Encrypt the project using the specified encryption key. + """Encrypt your local project using the specified encryption key. :param project: The project to encrypt :param key: The path to the encryption key to use diff --git a/lean/components/util/encryption_helper.py b/lean/components/util/encryption_helper.py index 86df6bd0..a9f364ea 100644 --- a/lean/components/util/encryption_helper.py +++ b/lean/components/util/encryption_helper.py @@ -137,6 +137,14 @@ def get_and_validate_user_input_encryption_key(user_input_key: Path, project_con raise RuntimeError(f"Provided encryption key ({user_input_key}) does not match the encryption key in the project ({project_config_encryption_key})") return project_config_encryption_key +def validate_user_inputs_for_cloud_push_pull_commands(encrypt: bool, decrypt: bool, key: Path): + if encrypt and decrypt: + raise RuntimeError(f"Cannot encrypt and decrypt at the same time.") + if key is None and (encrypt or decrypt): + raise RuntimeError(f"Encryption key is required when encrypting or decrypting.") + if key is not None and not encrypt and not decrypt: + raise RuntimeError(f"Encryption key can only be specified when encrypting or decrypting.") + def validate_key_and_encryption_state_for_cloud_project(project: QCProject, local_project_encryption_state: bool, encryption_key: Path) -> None: if not encryption_key and bool(project.encrypted) != bool(local_project_encryption_state): raise RuntimeError(f"Project encryption state mismatch. Local Project Encrypted: {bool(local_project_encryption_state)}. Cloud Project Encrypted {bool(project.encrypted)}. Please provide encryption key to pull project '{project.name}'")