Skip to content
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

Use AWS CRT instead of cryptography for CloudTrail log validation and verify query results #9105

Draft
wants to merge 4 commits into
base: cryptography-to-crt
Choose a base branch
from

Conversation

kdaily
Copy link
Member

@kdaily kdaily commented Nov 25, 2024

Issue #, if available:

Description of changes:

Replace use of cryptography methods with AWS CRT methods to perform CloudTrail log validation when calling the aws cloudtrail validate-logs command, and verifying query results iwth the aws cloudtrail verify-query-results command.

The methods relied on to load DER-encoded key files were added to awscrt==0.23.1, so this change bumps the floor of that dependency.

This also required changes to the test suite to not rely on cryptography helper methods for public/private keys. The test logic and coverage remains the same.

Testing:

  • Existing test suites cover necessary functionality, and they pass.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@kdaily kdaily force-pushed the cloudtrail-log-validation-to-crt branch 6 times, most recently from 0f50af1 to 7d31972 Compare November 27, 2024 17:28
@kdaily kdaily changed the title Use AWS CRT instead of cryptography for CloudTrail log validation Use AWS CRT instead of cryptography for CloudTrail log validation and verify query results Nov 27, 2024
digest=hashlib.sha256(self._create_string_to_sign(sign_file)).digest(),
signature=signature_bytes
)
if not result:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should add same comment from validate module about the previously raised cryptography error.

Suggested change
if not result:
if not result:
# The previous implementation caught a cryptography.exceptions.InvalidSignature
# exception here and re-raised the DigestSignatureError to avoid the stack trace
# leaking any information about the key.

@kdaily kdaily force-pushed the cloudtrail-log-validation-to-crt branch from 7d31972 to d630ced Compare November 27, 2024 17:36
@@ -41,10 +41,14 @@ dependencies = [
"ruamel.yaml.clib>=0.2.0,<=0.2.8",
"prompt-toolkit>=3.0.24,<3.0.39",
"distro>=1.5.0,<1.9.0",
"awscrt>=0.19.18,<=0.22.0",
"awscrt==0.23.1",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this change is currently causing the dependency tests to fail, because the requirements-doc-lock.txt is not updated, which needs to occur manually. However, on my developer laptop (Apple M3 Pro) there is no wheel for awscrt on Python 3.8, which is what the lockfiles are generated with. Need to investigate further the appropriate solution for that. Since this PR is targeting the feature branch, it should be safe to merge and will resolve before merging to the v2 branch.

@kdaily kdaily force-pushed the cloudtrail-log-validation-to-crt branch 2 times, most recently from a96070a to 252f416 Compare November 27, 2024 18:52
Porting customizations from `cryptography` to `awscrt` requires
`awscrt>=0.23.1`.

Also, used GH Action to:
- Regenerate lock files for macOS and Linux
- Regenerate lock files for Windows

In order to pass the dependency tests, `zipp` needs to be pinned for
support in Python 3.8. Our strategy has been to pin to the minimum
supported dependency version across Python versions so all Python
environments are consistent.
The existing test used convenience functions from
cryptography to create a private/public key pair.
Without these functions available, the key pair
needs to exist a priori. This commit adds the
appropriately formatted keys expected: a
PEM-encoded PKCS1 private key and a DER-encoded
PKCS1 public key.
The main change is the verify method returns
True or False, where previously it raised an error
if validation failed.
This commit makes two changes that were difficult to make
separately due to a change in the interface of signing and verifying
with public and private keys.

First, it migrates all remaining use of cryptography to CRT for the
CloudTrail verify query results.

Second, the existing test cases relied on utilities from the
cryptography package to create a temporary public key from a given
private key file. This has been refactored to move the public key to a
DER-encoded PKCS1 file that is similarly read in and used directly
through the utility module  `PublicPrivateKeyLoader`. A DER-encoded file
was chosen because it is the format used in the API, and it could easily
be read in directly without other cryptographic file manipulation. The
`PublicPrivateKeyLoader` class is now used in the CloudTrail log
validation tests to keep them as similar as possible.

I left the data files separately for each test suite (unit and
functional), even though they are identical since there was no
preexisting structure for sharing data files across the test suites.
This entailed duplicating the helper file loading functions as well,
since they relied on the test module directory at import time.
@kdaily kdaily force-pushed the cloudtrail-log-validation-to-crt branch from 252f416 to 71472d3 Compare November 27, 2024 18:54
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.

1 participant