Skip to content

Commit

Permalink
106 unexplained errors deleting gpg keystore after decryption (#107)
Browse files Browse the repository at this point in the history
* Move exception print statement earlier to avoid confusing log messages. Check for directories before trying to delete them.

* Upate changelog
  • Loading branch information
adammcdonagh authored Oct 19, 2024
1 parent 8060683 commit 765ed39
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- Fix issue where different protocols where not being detected properly, and proxy had to be explicitly defined when it was unnecessary
- When creating a destination directory with SFTP, it will now check whether lower level directories exist, and create them if not
- Always check for a directory before trying to delete it and thowing an exception if it doesn't exist.
- Moved exception printing for transfers to earlier in th code to ensure log messages aren't confusing.

# No release

Expand Down
20 changes: 13 additions & 7 deletions src/opentaskpy/taskhandlers/transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ def return_result(
Returns:
bool: The result of the task run.
"""
# Log the exception
if exception:
self.logger.exception(exception)

# Delete the remote connection objects
if self.source_remote_handler:
self.logger.info("Closing source connection")
Expand All @@ -125,9 +129,6 @@ def return_result(
)

# Call super to do the rest
# Log the exception
if exception:
self.logger.exception(exception)
return super().return_result(status, message, exception) # type: ignore[no-any-return]

def _get_default_class(self, protocol_name: str) -> type:
Expand Down Expand Up @@ -639,7 +640,8 @@ def run(self, kill_event: threading.Event | None = None) -> bool: # noqa: C901
and self.local_staging_dir != self.source_file_spec["directory"]
):
self.logger.debug("Removing local staging directory")
shutil.rmtree(self.local_staging_dir)
if path.exists(self.local_staging_dir):
shutil.rmtree(self.local_staging_dir)
else:
self.logger.info("Performing filewatch only")

Expand Down Expand Up @@ -769,7 +771,8 @@ def encrypt_files(
encrypted_files[output_filename] = files[file]

# Remove the temporary gnupg keychain files under f"{tmpdir}/.gnupg"
shutil.rmtree(f"{tmpdir}/.gnupg")
if path.exists(f"{tmpdir}/.gnupg"):
shutil.rmtree(f"{tmpdir}/.gnupg")

return encrypted_files

Expand Down Expand Up @@ -830,7 +833,8 @@ def decrypt_files(self, files: dict, private_key: str) -> dict:
self.logger.error(f"GPG STDERR: {decryption_data.stderr}")

# Remove the temporary gnupg keychain files under f"{tmpdir}/.gnupg"
shutil.rmtree(f"{tmpdir}/.gnupg")
if path.exists(f"{tmpdir}/.gnupg"):
shutil.rmtree(f"{tmpdir}/.gnupg")

raise exceptions.DecryptionError(
f"Error decrypting file {file}: {decryption_data.status}"
Expand All @@ -839,7 +843,9 @@ def decrypt_files(self, files: dict, private_key: str) -> dict:
decrypted_files[output_filename] = files[file]

# Remove the temporary gnupg keychain files under f"{tmpdir}/.gnupg"
shutil.rmtree(f"{tmpdir}/.gnupg")
# Check if the directory exists first
if path.exists(f"{tmpdir}/.gnupg"):
shutil.rmtree(f"{tmpdir}/.gnupg")

self.logger.debug(f"Returning decrypted files: {decrypted_files}")

Expand Down

0 comments on commit 765ed39

Please sign in to comment.