Skip to content

test: add kms encryption plugin integration tests - #1349

Draft
JuanLeee wants to merge 1 commit into
feat/kms-stack-4-pluginfrom
feat/kms-stack-5-integration-tests
Draft

JuanLeee wants to merge 1 commit into
feat/kms-stack-4-pluginfrom
feat/kms-stack-5-integration-tests

Conversation

@JuanLeee

@JuanLeee JuanLeee commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Description

The KMS Encryption Plugin (#1331) has unit tests, but nothing runs it against real servers, drivers, KMS or Aurora in the integration harness or in CI. The only end-to-end checks were tests/integration/test_kms_encryption_live.py, a module run by hand against local servers, which CI never ran. Testing against a real key also needs a key and a decrypt-only role, which the harness had no way to find, make or remove.

This PR adds the plugin's integration tests on three environments (LocalStack's KMS, a real KMS key and Aurora), the Gradle tasks and workflow that run them, and harness code that finds or makes the real test key. It changes no product code. Its base is #1331's branch, so the diff holds none of #1331's code, and it should merge with #1331, which builds on #1330, #1300 and #1317.

  1. 11 new container modules hold 109 test functions: 96 run against LocalStack, 8 need a real key (3 of them manual) and 5 need Aurora. They cover the read and write paths with each driver's cursor kinds and row factories, statement shapes and the refusals that leave rows unchanged, the packaged triggers, KeyManagementUtility, KMS refusals and the allow-list. Others check that the server's statement log holds no written plaintext or plaintext data key, and run SQLAlchemy and Django round trips. On MySQL the tests run on both the C extension and the pure build, except 15 that skip the pure build. tests/integration/kms_local/ runs the LocalStack modules without Gradle, with every port on 127.0.0.1, and replaces the deleted test_kms_encryption_live.py.
  2. The harness gains the RUN_KMS_ENCRYPTION_TESTS_ONLY, KMS_LOCALSTACK and KMS_MANUAL_TESTS features and three Gradle tasks: test-kms-encryption-docker (LocalStack, no AWS account), test-kms-encryption-docker-kms (a real key) and test-kms-encryption-aurora (two-instance clusters), all on Python 3.12 without async drivers. LocalStack (3.8.1, per the harness comment) is pinned by digest alone, because Testcontainers rejects a tag with a digest, and it publishes no port. In KMS environments the PostgreSQL container logs every statement, so tests can prove the server never saw a written plaintext or a plaintext data key. Inside a KMS task every non-KMS test skips, and outside one the KMS classes skip. KMS_MANUAL_TESTS=1 adds the 3 manual real-key tests (a kms:ViaService condition, CloudTrail records, expired credentials), which CI never runs.
  3. KmsTestUtility, new in the harness, gives a real-key run its key when KMS_KEY_ARN is unset. It uses the key behind KMS_KEY_ALIAS or alias/aws-advanced-python-wrapper/integration-tests only when that key is enabled and symmetric, its description starts with "aws-advanced-python-wrapper test KMS key" and it has the tag aws-advanced-python-wrapper=integration-tests. It makes the key and the alias only when KMS_ALLOW_KEY_CREATION is 1, true or yes, as the Ruby wrapper reads that variable, and keeps them for later runs. The Docker task makes a per-run role that may only call kms:Decrypt on the key and deletes it at the end, so that task needs the opt-in even to reuse the key; the Aurora task makes no role and reuses the key without it.
  4. Clean-up runs at the end of the run and from a shutdown hook. It never deletes or re-points an alias, and it schedules a key this run made for deletion only when no alias names it. A run with KMS_KEY_ARN looks up and makes nothing; before any environment starts, it checks that the value is a key ARN (not an alias or key ID) in KMS_REGION and, for the Docker task, that KMS_DECRYPT_ONLY_ROLE_ARN is set, so an Aurora run never creates a cluster without a key.
  5. Every test-* Gradle task now writes tests/integration/container/reports/<environment>.xml beside the HTML report, which the workflow counts. A pytest_runtest_makereport wrapper in the container conftest replaces the database password, the application users' passwords and a real key's account ID and key ID with *** in both reports, because the harness runs pytest with --tb=long, which prints each frame's arguments. The live console output isn't redacted; CI makes a random database password for each job and masks it with ::add-mask::.
  6. The new kms_encryption_integration_tests.yml runs each job on PostgreSQL and MySQL. kms-localstack needs no secrets and runs on pull requests to any base and pushes to main that touch the wrapper, the integration tests, pyproject.toml, poetry.lock or the workflow. kms-real-key and kms-aurora run on dispatch, and on pushes to main once their repository variables are set, so main stays green until then; after that, each such push makes one two-instance Aurora cluster per engine. Each job fails when no KMS test ran, because pytest exits 0 when every test skips. The Aurora job ends by deleting the cluster named RDS_DB_NAME, because the harness doesn't delete it when a run is cancelled.

Behaviour changes

  1. The container conftest imports the KMS helper on every run, so every run also loads the wrapper's encryption modules at collection.

Other fixes

  1. An Aurora or Multi-AZ cluster run whose RDS_DB_NAME named an existing cluster tried to create a cluster under that name, and the failed create's catch then deleted the existing cluster (from reading the code; not reproduced). The run now fails first with "Cluster already exists: set REUSE_RDS_DB=true to use it, or set RDS_DB_NAME to a new name." Live since b525ea9b (upgrade integration test suite (host part) #430, 2024-03-12); a Multi-AZ instance run keeps the old path.
  2. Teardown revoked the runner's IP rule in the default security group even when the rule existed before the run, because ec2AuthorizeIP skipped an existing rule silently. ec2AuthorizeIP now reports whether it added the rule, and teardown revokes only then. Live since b525ea9b (upgrade integration test suite (host part) #430, 2024-03-12).
  3. pytest_runtest_setup raised TypeError for a test without parameters, since it added None to a string.
  4. TestEnvironment.get_proxy_infos raised AttributeError when no proxies were set up, as in the compose lane; it now returns [].

Not in scope

  1. The 2 strict xfails track product defects on mysql-connector's C extension, and a fix fails them until the mark goes. AwsWrapperCursor.fetchmany() defaults size to 0 and passes it on, so the C extension returns no rows; the same code is on main, and the async cursor defaults to None. With use_unicode=False, DriverDialect.fetch_rows, new in feat: kms encryption plugin #1331, keys rows by column names that the C extension returns as bytes, so the plugin raises a bare KeyError instead of InvalidAlgorithmError (seen through the xfail; the C extension's source wasn't read).
  2. On Aurora PostgreSQL, the plugin's metadata lookup after a writer failover can reach the old writer and raise MetadataError, which doesn't start failover_v2, as the guide says. The failover test accepts that on PostgreSQL by opening a new connection, while MySQL must write through.
  3. The plugin is sync only, so the KMS tasks run no async drivers, and a unit test pins that the async wrapper refuses kms_encryption.
  4. Django runs on MySQL only, because the wrapper ships only the mysql_connector Django backend.

Administrator setup, once

  1. One symmetric KMS key, at $1 a month. CI never creates or changes keys.
  2. A CI role trusted through GitHub OIDC with sub equal to repo:aws/aws-advanced-python-wrapper:ref:refs/heads/main (StringEquals) and aud equal to sts.amazonaws.com, allowed kms:GenerateDataKey, kms:Decrypt and kms:DescribeKey on the key and sts:AssumeRole on the Decrypt-only role, with an explicit deny on key and alias changes.
  3. A Decrypt-only role, trusted by the CI role, allowed only kms:Decrypt on the key.
  4. A maintainer role with the CI role's permissions, for real-key runs before merge, since the CI role trusts only main.
  5. Repository variables KMS_KEY_ARN (a key ARN, not an alias), KMS_REGION, KMS_IT_ROLE_ARN and KMS_DECRYPT_ONLY_ROLE_ARN. The Aurora job uses the existing AWS_DEPLOY_ROLE secret, whose role also needs the test container's KMS actions on the key.

Tests

./gradlew test-kms-encryption-docker on this tree passed PostgreSQL 97 and MySQL 190 cases, with the 2 strict xfails and nothing failed, and this PR's kms-localstack jobs reported "KMS tests run: 97" and "KMS tests run: 190". That lane collected every container test (475 on PostgreSQL, 585 on MySQL), so the new conftest import works at collection; no non-KMS Gradle task was run on this tree. ./gradlew test-kms-encryption-docker-kms in a test account passed MySQL 8 and PostgreSQL 5 cases three times: when the harness made the key and the alias, when it reused them and made no new key or alias and with an explicit KMS_KEY_ARN. Without an alias or the opt-in, and with an alias naming an unmarked key, the run failed before it made anything. The two runs without KMS_KEY_ARN each deleted the decrypt-only role they made, and the key and alias stayed for later runs. The 6 new unit tests (9 cases) cover the async wrapper refusing kms_encryption, a read refused when its key row names an unlisted master key (lenient mode too), the guide's DDL matching the tests' schema and MySQL routines reading no session variables. pytest tests/unit -Werror passes 4713 tests on Python 3.12 and 3.10, with flake8, isort and mypy clean. The Aurora task, the manual tests and the compose lane weren't run on this tree, and the workflow's real-key and Aurora jobs skip on pull requests. On an earlier commit, the Aurora task's auth and TLS test and trigger test passed on Aurora PostgreSQL 17.9 and MySQL 3.10.3, and its failover test passed on MySQL. IntegrationTests.md gains "Running the KMS encryption tests" and "Existing clusters and leftovers", and the plugin guide gains three lines on upserts and alias ARNs. No CHANGELOG entry or message key.

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

@JuanLeee
JuanLeee force-pushed the feat/kms-stack-5-integration-tests branch from 5b47c72 to 28b3bf3 Compare November 18, 2026 09:43
@JuanLeee
JuanLeee force-pushed the feat/kms-stack-5-integration-tests branch 2 times, most recently from ae97e03 to 42c9aa2 Compare November 19, 2026 10:24
@JuanLeee JuanLeee changed the title test: add KMS Encryption plugin integration tests test: add kms encryption plugin integration tests Nov 19, 2026
@JuanLeee
JuanLeee added this pull request to stack #1381 November 20, 2026 03:46
@JuanLeee
JuanLeee force-pushed the feat/kms-stack-5-integration-tests branch from 42c9aa2 to 3cf4b92 Compare November 27, 2026 08:46
Add container tests for the KMS Encryption plugin on PostgreSQL and
MySQL, including SQLAlchemy and Django, against LocalStack's KMS, a
real KMS key and Aurora. The harness gets the RUN_KMS_ENCRYPTION_TESTS_ONLY,
KMS_LOCALSTACK and KMS_MANUAL_TESTS features, three Gradle tasks, a
LocalStack container pinned by digest, PostgreSQL statement logging and
a JUnit XML report. No product code changes.

Without KMS_KEY_ARN, the harness finds the marked test key behind a KMS
alias (KMS_KEY_ALIAS, or alias/aws-advanced-python-wrapper/integration-tests)
and reuses it. It makes the key and the alias only when
KMS_ALLOW_KEY_CREATION is set, and for the Docker task it makes a
decrypt-only role for the run and deletes it at the end. It never
deletes or reuses a key or role that it didn't make or mark.

Two harness changes apply to every Aurora and RDS task: a run refuses an
RDS_DB_NAME that names an existing cluster, and it revokes only the IP
rule that it added itself.

Add a compose lane, tests/integration/kms_local/run.sh, that runs the
LocalStack modules without Gradle. It replaces
tests/integration/test_kms_encryption_live.py.

A new workflow runs the LocalStack job on pull requests and pushes to
main, and the real-key and Aurora jobs on dispatch and on pushes to main
once their repository variables are set. Each job fails when no KMS
test ran.

Two strict xfails track product defects on mysql-connector's C
extension: fetchmany() without a size returns no rows, and with
use_unicode=False a read or write on an encrypted table raises a bare
KeyError.

The real-key and Aurora jobs need one-time administrator setup: a
symmetric key, an OIDC role trusted only for main and limited to that
key, a Decrypt-only role, and the KMS_KEY_ARN, KMS_REGION,
KMS_IT_ROLE_ARN and KMS_DECRYPT_ONLY_ROLE_ARN repository variables.
@JuanLeee
JuanLeee force-pushed the feat/kms-stack-5-integration-tests branch from 3cf4b92 to b7bda0c Compare December 1, 2026 13:03

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.

1 participant