Skip to content

Commit 5f9068d

Browse files
committed
logging cleanup to explicitly expect number values where appropriate
1 parent 8f047fb commit 5f9068d

File tree

4 files changed

+28
-16
lines changed

4 files changed

+28
-16
lines changed

src/aws_encryption_sdk/internal/formatting/deserialize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ def deserialize_frame(stream, header, verifier=None):
344344
(sequence_number,) = unpack_values(">I", stream, verifier)
345345
final_frame = True
346346
else:
347-
_LOGGER.debug("Deserializing frame sequence number %s", int(sequence_number))
347+
_LOGGER.debug("Deserializing frame sequence number %d", int(sequence_number))
348348
frame_data["final_frame"] = final_frame
349349
frame_data["sequence_number"] = sequence_number
350350
(frame_iv,) = unpack_values(">{iv_len}s".format(iv_len=header.algorithm.iv_len), stream, verifier)

src/aws_encryption_sdk/internal/formatting/encryption_context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def deserialize_encryption_context(serialized_encryption_context):
150150
encryption_context = {}
151151

152152
dict_size, deserialized_size = read_short(source=serialized_encryption_context, offset=deserialized_size)
153-
_LOGGER.debug("Found %s keys", dict_size)
153+
_LOGGER.debug("Found %d keys", dict_size)
154154
for _ in range(dict_size):
155155
key_size, deserialized_size = read_short(source=serialized_encryption_context, offset=deserialized_size)
156156
key, deserialized_size = read_string(

src/aws_encryption_sdk/streaming_client.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def read(self, b=-1):
214214
if b is None or b < 0:
215215
b = -1
216216

217-
_LOGGER.debug("Stream read called, requesting %s bytes", b)
217+
_LOGGER.debug("Stream read called, requesting %d bytes", b)
218218
output = io.BytesIO()
219219

220220
if not self._message_prepped:
@@ -234,7 +234,7 @@ def read(self, b=-1):
234234
self.output_buffer = b""
235235

236236
self.bytes_read += output.tell()
237-
_LOGGER.debug("Returning %s bytes of %s bytes requested", output.tell(), b)
237+
_LOGGER.debug("Returning %d bytes of %d bytes requested", output.tell(), b)
238238
return output.getvalue()
239239

240240
def tell(self):
@@ -538,7 +538,7 @@ def _read_bytes_to_framed_body(self, b):
538538
:returns: Bytes read from source stream, encrypted, and serialized
539539
:rtype: bytes
540540
"""
541-
_LOGGER.debug("collecting %s bytes", b)
541+
_LOGGER.debug("collecting %d bytes", b)
542542
_b = b
543543

544544
if b > 0:
@@ -565,10 +565,11 @@ def _read_bytes_to_framed_body(self, b):
565565
# If finalizing on this pass, wait until final frame is written
566566
or (finalize and not final_frame_written)
567567
):
568-
is_final_frame = finalize and len(plaintext) < self.config.frame_length
569-
bytes_in_frame = min(len(plaintext), self.config.frame_length)
568+
current_plaintext_length = len(plaintext)
569+
is_final_frame = finalize and current_plaintext_length < self.config.frame_length
570+
bytes_in_frame = min(current_plaintext_length, self.config.frame_length)
570571
_LOGGER.debug(
571-
"Writing %s bytes into%s frame %s",
572+
"Writing %d bytes into%s frame %d",
572573
bytes_in_frame,
573574
" final" if is_final_frame else "",
574575
self.sequence_number,
@@ -719,7 +720,7 @@ def _read_header(self):
719720
and header.frame_length > self.config.max_body_length
720721
):
721722
raise CustomMaximumValueExceeded(
722-
"Frame Size in header found larger than custom value: {found} > {custom}".format(
723+
"Frame Size in header found larger than custom value: {found:d} > {custom:d}".format(
723724
found=header.frame_length, custom=self.config.max_body_length
724725
)
725726
)
@@ -758,7 +759,7 @@ def _prep_non_framed(self):
758759

759760
if self.config.max_body_length is not None and self.body_length > self.config.max_body_length:
760761
raise CustomMaximumValueExceeded(
761-
"Non-framed message content length found larger than custom value: {found} > {custom}".format(
762+
"Non-framed message content length found larger than custom value: {found:d} > {custom:d}".format(
762763
found=self.body_length, custom=self.config.max_body_length
763764
)
764765
)
@@ -792,7 +793,7 @@ def _read_bytes_from_non_framed_body(self, b):
792793
_LOGGER.debug("starting non-framed body read")
793794
# Always read the entire message for non-framed message bodies.
794795
bytes_to_read = self.body_end - self.source_stream.tell()
795-
_LOGGER.debug("%s bytes requested; reading %s bytes", b, bytes_to_read)
796+
_LOGGER.debug("%d bytes requested; reading %d bytes", b, bytes_to_read)
796797
ciphertext = self.source_stream.read(bytes_to_read)
797798

798799
if len(self.output_buffer) + len(ciphertext) < self.body_length:
@@ -821,13 +822,13 @@ def _read_bytes_from_framed_body(self, b):
821822
"""
822823
plaintext = b""
823824
final_frame = False
824-
_LOGGER.debug("collecting %s bytes", b)
825+
_LOGGER.debug("collecting %d bytes", b)
825826
while len(plaintext) < b and not final_frame:
826827
_LOGGER.debug("Reading frame")
827828
frame_data, final_frame = aws_encryption_sdk.internal.formatting.deserialize.deserialize_frame(
828829
stream=self.source_stream, header=self._header, verifier=self.verifier
829830
)
830-
_LOGGER.debug("Read complete for frame %s", frame_data.sequence_number)
831+
_LOGGER.debug("Read complete for frame %d", frame_data.sequence_number)
831832
if frame_data.sequence_number != self.last_sequence_number + 1:
832833
raise SerializationError("Malformed message: frames out of order")
833834
self.last_sequence_number += 1
@@ -846,7 +847,8 @@ def _read_bytes_from_framed_body(self, b):
846847
encrypted_data=frame_data,
847848
associated_data=associated_data,
848849
)
849-
_LOGGER.debug("bytes collected: %s", len(plaintext))
850+
plaintext_length = len(plaintext)
851+
_LOGGER.debug("bytes collected: %d", plaintext_length)
850852
if final_frame:
851853
_LOGGER.debug("Reading footer")
852854
self.footer = aws_encryption_sdk.internal.formatting.deserialize.deserialize_footer(

test/functional/test_f_aws_encryption_sdk_client.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,7 @@ def _prep_plaintext_and_logs(log_catcher, plaintext_length):
685685

686686

687687
def _look_in_logs(log_catcher, plaintext):
688+
# Verify that no plaintext chunks are in the logs
688689
logs = log_catcher.text
689690
# look for every possible 32-byte chunk
690691
start = 0
@@ -698,25 +699,33 @@ def _look_in_logs(log_catcher, plaintext):
698699
end += 1
699700

700701

702+
def _error_check(capsys_instance):
703+
# Verify that no error were caught and ignored.
704+
# The intent is to catch logging errors, but others will be caught as well.
705+
stderr = capsys_instance.readouterr().err
706+
assert 'Call stack:' not in stderr
707+
708+
701709
@pytest.mark.parametrize("frame_size", (0, LINE_LENGTH // 2, LINE_LENGTH, LINE_LENGTH * 2))
702710
@pytest.mark.parametrize(
703711
"plaintext_length", (1, LINE_LENGTH // 2, LINE_LENGTH, int(LINE_LENGTH * 1.5), LINE_LENGTH * 2)
704712
)
705-
def test_plaintext_logs_oneshot(caplog, plaintext_length, frame_size):
713+
def test_plaintext_logs_oneshot(caplog, capsys, plaintext_length, frame_size):
706714
plaintext, key_provider = _prep_plaintext_and_logs(caplog, plaintext_length)
707715

708716
_ciphertext, _header = aws_encryption_sdk.encrypt(
709717
source=plaintext, key_provider=key_provider, frame_length=frame_size
710718
)
711719

712720
_look_in_logs(caplog, plaintext)
721+
_error_check(capsys)
713722

714723

715724
@pytest.mark.parametrize("frame_size", (0, LINE_LENGTH // 2, LINE_LENGTH, LINE_LENGTH * 2))
716725
@pytest.mark.parametrize(
717726
"plaintext_length", (1, LINE_LENGTH // 2, LINE_LENGTH, int(LINE_LENGTH * 1.5), LINE_LENGTH * 2)
718727
)
719-
def test_plaintext_logs_stream(caplog, plaintext_length, frame_size):
728+
def test_plaintext_logs_stream(caplog, capsys, plaintext_length, frame_size):
720729
plaintext, key_provider = _prep_plaintext_and_logs(caplog, plaintext_length)
721730

722731
ciphertext = b""
@@ -727,3 +736,4 @@ def test_plaintext_logs_stream(caplog, plaintext_length, frame_size):
727736
ciphertext += line
728737

729738
_look_in_logs(caplog, plaintext)
739+
_error_check(capsys)

0 commit comments

Comments
 (0)