Skip to content

Commit

Permalink
self review
Browse files Browse the repository at this point in the history
  • Loading branch information
rjra2611 committed Oct 16, 2023
1 parent be370d9 commit f9c3fa9
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
17 changes: 9 additions & 8 deletions lean/commands/cloud/pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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:
Expand Down Expand Up @@ -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)
10 changes: 4 additions & 6 deletions lean/commands/cloud/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lean/commands/decrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lean/commands/encrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions lean/components/util/encryption_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}'")
Expand Down

0 comments on commit f9c3fa9

Please sign in to comment.