-
Notifications
You must be signed in to change notification settings - Fork 0
fix(error-handling): improve error messages for missing S3 objects and instruction files #149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bbaed91
fix(error-handling): improve error messages for missing S3 objects an…
texastony 99a707f
fix(instruction-file): address PR #149 review comments from kessplas …
texastony 146f525
Merge branch 'staging' into tonyknap/feat-handle-no-object
texastony ea46039
chore: fix error message
texastony c137c60
merge staging into tonyknap/feat-handle-no-object
texastony File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| import threading | ||
|
|
||
| from attrs import define, field | ||
| from botocore.exceptions import ClientError | ||
| from botocore.response import StreamingBody | ||
|
|
||
| from .exceptions import S3EncryptionClientError | ||
|
|
@@ -225,6 +226,11 @@ def on_get_object_after_call(self, parsed, **kwargs): | |
| # Get encryption context from thread-local storage (set by get_object wrapper) | ||
| encryption_context = getattr(self._context, _CTX_ENCRYPTION_CONTEXT, None) | ||
|
|
||
| # If Body is None, the S3 request failed (e.g., NoSuchKey). | ||
| # Return early and let boto3 raise the original error. | ||
| if parsed.get("Body", None) is None: | ||
| return | ||
|
|
||
| # The parsed response already has the Body as a StreamingBody | ||
| # We need to read it, decrypt it, and replace it | ||
|
|
||
|
|
@@ -272,9 +278,16 @@ def process_instruction_file(self, parsed): | |
| """ | ||
| instruction_key = getattr(self._context, _CTX_KEY, None) | ||
|
|
||
| body = parsed.get("Body", None) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto, redundant None |
||
| if body is None: | ||
| raise S3EncryptionClientError( | ||
| f"Instruction file body is empty for key: {instruction_key}" | ||
| ) | ||
|
|
||
| # In plaintext mode, parse instruction file and append to metadata | ||
| existing_metadata = parsed.get("Metadata", {}) | ||
| instruction_data = parsed.get("Body").read() | ||
| # Metadata may be present but None, so `or {}` handles that case | ||
| existing_metadata = parsed.get("Metadata", {}) or {} | ||
|
texastony marked this conversation as resolved.
|
||
| instruction_data = body.read() | ||
| instruction_metadata = parse_instruction_file(instruction_data, instruction_key) | ||
|
|
||
| # Append parsed instruction file content to existing metadata | ||
|
|
@@ -385,9 +398,16 @@ def get_object(self, **kwargs): | |
| except S3EncryptionClientError: | ||
| # Re-raise our own exceptions without wrapping | ||
| raise | ||
| except ClientError as e: | ||
| # Wrap S3 service errors (e.g., NoSuchKey) with context | ||
| raise S3EncryptionClientError( | ||
| f"Failed to retrieve and/or decrypt object: {str(e)}" | ||
| ) from e | ||
| except Exception as e: | ||
| # Wrap any unexpected errors during decryption | ||
| raise S3EncryptionClientError(f"Failed to decrypt object: {str(e)}") from e | ||
| raise S3EncryptionClientError( | ||
| f"Failed to retrieve and/or decrypt object: {str(e)}" | ||
| ) from e | ||
| finally: | ||
| # Clean up thread-local storage; | ||
| # do not clean up the client as it is not thread local only | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| # Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| """Unit tests for S3EncryptionClient get_object error handling.""" | ||
|
|
||
| from unittest.mock import Mock | ||
|
|
||
| import pytest | ||
| from botocore.exceptions import ClientError | ||
|
|
||
| from s3_encryption import S3EncryptionClient, S3EncryptionClientConfig | ||
| from s3_encryption.exceptions import S3EncryptionClientError | ||
| from s3_encryption.materials.keyring import S3Keyring | ||
|
|
||
|
|
||
| class TestGetObjectNonExistentObject: | ||
| """S3EncryptionClient wraps S3 errors with context, preserving the original cause.""" | ||
|
|
||
| def _build_client(self): | ||
| mock_s3 = Mock() | ||
| mock_s3.meta.events = Mock() | ||
| mock_s3.meta.events.register = Mock() | ||
| mock_keyring = Mock(spec=S3Keyring) | ||
| config = S3EncryptionClientConfig(keyring=mock_keyring) | ||
| return S3EncryptionClient(wrapped_s3_client=mock_s3, config=config), mock_s3 | ||
|
|
||
| def test_no_such_key_raises_s3_encryption_client_error(self): | ||
| client, mock_s3 = self._build_client() | ||
| error_response = { | ||
| "Error": {"Code": "NoSuchKey", "Message": "The specified key does not exist."} | ||
| } | ||
| mock_s3.get_object.side_effect = ClientError(error_response, "GetObject") | ||
|
|
||
| with pytest.raises( | ||
| S3EncryptionClientError, match="Failed to retrieve and/or decrypt object" | ||
| ) as exc_info: | ||
| client.get_object(Bucket="test-bucket", Key="nonexistent-key") | ||
|
|
||
| assert isinstance(exc_info.value.__cause__, ClientError) | ||
| assert exc_info.value.__cause__.response["Error"]["Code"] == "NoSuchKey" | ||
|
|
||
| def test_access_denied_raises_s3_encryption_client_error(self): | ||
| client, mock_s3 = self._build_client() | ||
| error_response = {"Error": {"Code": "AccessDenied", "Message": "Access Denied"}} | ||
| mock_s3.get_object.side_effect = ClientError(error_response, "GetObject") | ||
|
|
||
| with pytest.raises( | ||
| S3EncryptionClientError, match="Failed to retrieve and/or decrypt object" | ||
| ) as exc_info: | ||
| client.get_object(Bucket="test-bucket", Key="forbidden-key") | ||
|
|
||
| assert isinstance(exc_info.value.__cause__, ClientError) | ||
| assert exc_info.value.__cause__.response["Error"]["Code"] == "AccessDenied" | ||
|
|
||
|
|
||
| class TestFetchMissingInstructionFile: | ||
| """fetch_instruction_file wraps NoSuchKey with instruction-file-specific message.""" | ||
|
|
||
| def test_missing_instruction_file_raises_s3_encryption_client_error(self): | ||
| mock_s3 = Mock() | ||
| mock_s3._s3ec_plugin_context = Mock() | ||
| error_response = { | ||
| "Error": {"Code": "NoSuchKey", "Message": "The specified key does not exist."} | ||
| } | ||
| mock_s3.get_object.side_effect = ClientError(error_response, "GetObject") | ||
|
|
||
| from s3_encryption.instruction_file import fetch_instruction_file | ||
|
|
||
| with pytest.raises(S3EncryptionClientError, match="fetching Instruction File") as exc_info: | ||
| fetch_instruction_file(mock_s3, "test-bucket", "test-key.instruction") | ||
|
|
||
| assert isinstance(exc_info.value.__cause__, ClientError) | ||
| assert exc_info.value.__cause__.response["Error"]["Code"] == "NoSuchKey" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't care to enough to quibble about this more than in this comment, so leave it if you want but, it's redundant given
get()will returnNoneby default anyway. Or is this a linter thing?