Skip to content

fix: copy log records only when redaction is needed - #444

Open
MaxonT wants to merge 1 commit into
aws:masterfrom
MaxonT:contrib/145-auto
Open

MaxonT wants to merge 1 commit into
aws:masterfrom
MaxonT:contrib/145-auto

Conversation

@MaxonT

@MaxonT MaxonT commented Nov 25, 2026

Copy link
Copy Markdown

Having an issue that names the log handler's record handling so plainly made it easy to find where to look, so this is my fix there; the title is all I had, so please check it.

Issue #, if available: #145

Description of changes:

_KMSKeyRedactingFormatter.format deep-copied every log record before checking whether it was a KMS Encrypt request or a KMS response containing Plaintext. If a record's arguments could not be deep-copied, format raised, as in the traceback in the issue (TypeError from copy.deepcopy on a urllib3 connection-pool record), and the log line was lost.

__redact_record now makes the copy only for the two record kinds that are redacted, and returns every other record unchanged. Redaction of those two kinds is not changed. The record that reaches the formatter is still never modified in place, because the deep copy is kept for the redacted cases. The copy for the redacted cases can still fail for the same reason in principle, but those are botocore records with plain dict and bytes arguments.

Tests: two cases in test/unit/internal/test_logging_utils.py. One formats an unrelated record whose argument raises in __deepcopy__; it fails with TypeError: cannot deep copy when the source change is reverted and passes with it. The other checks that a KMS response body still has Plaintext redacted and the original record is untouched. pytest test/unit/internal/test_logging_utils.py in a fresh Python 3.14 venv: 13 passed.

I could not reproduce the original Python 2.7 failure with the real urllib3 object, so the test uses a stand-in object. In the rest of test/unit (excluding test_arg_parsing.py), 4 tests fail both with and without this change (3 in test_aws_encryption_sdk_cli.py, 1 in test_compatability.py). test_arg_parsing.py fails at collection on this Python version with ValueError: mutually exclusive groups cannot be nested, also without the change. I did not run the integration tests.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Fixes #145

_KMSKeyRedactingFormatter deep-copied every record before checking
whether it held KMS plaintext. Records whose arguments cannot be
deep-copied made format() raise instead of logging. Copy only the
records that are redacted and pass all others through unchanged.

Fixes aws#145

Signed-off-by: Ym <173550318+MaxonT@users.noreply.github.com>
@MaxonT
MaxonT requested a review from a team as a code owner November 25, 2026 04:58

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug in log handler record handling

1 participant