From b7bda0c477109993c3c49662889f104011718ab1 Mon Sep 17 00:00:00 2001 From: Juan Lee Date: Thu, 19 Nov 2026 02:38:25 -0700 Subject: [PATCH] test: add KMS Encryption plugin integration tests 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. --- .../kms_encryption_integration_tests.yml | 264 +++ docs/development-guide/IntegrationTests.md | 178 +- .../UsingTheKmsEncryptionPlugin.md | 3 + tests/integration/container/conftest.py | 16 + .../django/test_django_kms_encryption.py | 151 ++ .../test_sqlalchemy_kms_encryption.py | 302 +++ .../container/test_kms_encryption_aurora.py | 380 ++++ .../test_kms_encryption_key_management.py | 619 ++++++ .../container/test_kms_encryption_kms.py | 516 +++++ .../test_kms_encryption_lifecycle.py | 481 +++++ .../container/test_kms_encryption_real_kms.py | 507 +++++ .../test_kms_encryption_round_trip.py | 599 ++++++ .../test_kms_encryption_server_logs.py | 462 ++++ .../test_kms_encryption_statements.py | 630 ++++++ .../container/test_kms_encryption_triggers.py | 488 +++++ .../container/utils/connection_utils.py | 22 +- .../container/utils/driver_helper.py | 5 +- .../container/utils/kms_encryption_helper.py | 844 ++++++++ .../container/utils/kms_encryption_schema.py | 83 + .../container/utils/test_environment.py | 2 +- .../utils/test_environment_features.py | 3 + .../container/utils/test_timings.py | 43 +- tests/integration/host/build.gradle.kts | 70 + .../integration/TestEnvironmentFeatures.java | 5 +- .../integration/host/TestEnvironment.java | 63 +- .../host/TestEnvironmentConfiguration.java | 15 + .../host/TestEnvironmentProvider.java | 93 +- .../integration/util/AuroraTestUtility.java | 9 +- .../integration/util/ContainerHelper.java | 38 +- .../java/integration/util/KmsTestUtility.java | 465 ++++ tests/integration/kms_local/compose.yaml | 28 + tests/integration/kms_local/run.sh | 170 ++ tests/integration/test_kms_encryption_live.py | 1914 ----------------- tests/unit/test_aio_plugin_factory.py | 26 + tests/unit/test_kms_encryption_plugin.py | 33 + ...est_kms_encryption_schema_matches_guide.py | 68 + tests/unit/test_kms_trigger_sql.py | 7 + 37 files changed, 7664 insertions(+), 1938 deletions(-) create mode 100644 .github/workflows/kms_encryption_integration_tests.yml create mode 100644 tests/integration/container/django/test_django_kms_encryption.py create mode 100644 tests/integration/container/sqlalchemy/test_sqlalchemy_kms_encryption.py create mode 100644 tests/integration/container/test_kms_encryption_aurora.py create mode 100644 tests/integration/container/test_kms_encryption_key_management.py create mode 100644 tests/integration/container/test_kms_encryption_kms.py create mode 100644 tests/integration/container/test_kms_encryption_lifecycle.py create mode 100644 tests/integration/container/test_kms_encryption_real_kms.py create mode 100644 tests/integration/container/test_kms_encryption_round_trip.py create mode 100644 tests/integration/container/test_kms_encryption_server_logs.py create mode 100644 tests/integration/container/test_kms_encryption_statements.py create mode 100644 tests/integration/container/test_kms_encryption_triggers.py create mode 100644 tests/integration/container/utils/kms_encryption_helper.py create mode 100644 tests/integration/container/utils/kms_encryption_schema.py create mode 100644 tests/integration/host/src/test/java/integration/util/KmsTestUtility.java create mode 100644 tests/integration/kms_local/compose.yaml create mode 100755 tests/integration/kms_local/run.sh delete mode 100644 tests/integration/test_kms_encryption_live.py create mode 100644 tests/unit/test_kms_encryption_schema_matches_guide.py diff --git a/.github/workflows/kms_encryption_integration_tests.yml b/.github/workflows/kms_encryption_integration_tests.yml new file mode 100644 index 000000000..70d92e99f --- /dev/null +++ b/.github/workflows/kms_encryption_integration_tests.yml @@ -0,0 +1,264 @@ +name: KMS Encryption Integration Tests + +# The LocalStack job needs no AWS account and no secrets, so it also runs on pull requests from forks. +# The real-key and Aurora jobs read the key ARN, its region and the roles from repository variables, +# which an administrator sets up once; CI never creates or changes KMS keys. +# On a push to main they run only when the variables they need are set, so main stays green until then; +# a manual run of either fails when KMS_KEY_ARN is missing. + +on: + pull_request: + paths: + - 'aws_advanced_python_wrapper/**' + - 'tests/integration/**' + - 'pyproject.toml' + - 'poetry.lock' + - '.github/workflows/kms_encryption_integration_tests.yml' + push: + branches: [ main ] + paths: + - 'aws_advanced_python_wrapper/**' + - 'tests/integration/**' + - 'pyproject.toml' + - 'poetry.lock' + - '.github/workflows/kms_encryption_integration_tests.yml' + workflow_dispatch: + inputs: + lane: + description: 'Which KMS environment to run' + type: choice + options: [ localstack, real-kms, aurora ] + default: localstack + +permissions: + contents: read + +concurrency: + # Each push has its own group, so a long Aurora job never holds back or cancels a later push's other jobs. + group: ${{ github.workflow }}-${{ github.ref }}-${{ inputs.lane }}-${{ github.event_name == 'push' && github.sha || '' }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + +jobs: + kms-localstack: + name: Run KMS Encryption Tests with LocalStack + if: github.event_name != 'workflow_dispatch' || inputs.lane == 'localstack' + runs-on: ubuntu-latest + timeout-minutes: 45 + strategy: + fail-fast: false + matrix: + include: + - { engine: pg, skip: mysql } + - { engine: mysql, skip: pg } + + steps: + - name: 'Clone repository' + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 + + - name: 'Set up JDK 8' + uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654 # v5 + with: + distribution: 'corretto' + java-version: 8 + + - name: 'Set up Python 3.12' + uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0 + with: + python-version: '3.12' + + - name: Install poetry + shell: bash + run: | + pipx install poetry==1.8.2 + poetry config virtualenvs.prefer-active-python true + + - name: Install dependencies + run: poetry install + + - name: 'Run the KMS tests on Docker with LocalStack' + run: | + DB_PASSWORD="$(openssl rand -hex 16)" + echo "::add-mask::${DB_PASSWORD}" + export DB_PASSWORD DB_USERNAME=test_user DB_DATABASE_NAME=test_db + ./gradlew --no-parallel --no-daemon test-kms-encryption-docker \ + -Dexclude-${{ matrix.skip }}-engine=true -Dexclude-${{ matrix.skip }}-driver=true + + # pytest exits 0 when every test is skipped, so count the KMS test cases that ran. + - name: 'Fail when no KMS test ran' + run: | + python3 -c 'import glob,sys,xml.etree.ElementTree as E; c=[t for f in glob.glob("tests/integration/container/reports/*.xml") for t in E.parse(f).iter("testcase") if "kms_encryption" in t.get("classname","") and t.find("skipped") is None]; print("KMS tests run:", len(c)); sys.exit(0 if c else 1)' + + - name: 'Archive results' + if: always() + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: kms-localstack-${{ matrix.engine }} + path: ./tests/integration/container/reports + retention-days: 5 + + kms-real-key: + name: Run KMS Encryption Tests with a real KMS key + if: >- + (github.event_name == 'push' && github.ref == 'refs/heads/main' + && vars.KMS_KEY_ARN != '' && vars.KMS_REGION != '' && vars.KMS_DECRYPT_ONLY_ROLE_ARN != '' && vars.KMS_IT_ROLE_ARN != '') + || inputs.lane == 'real-kms' + runs-on: ubuntu-latest + timeout-minutes: 45 + permissions: + id-token: write # This is required for requesting the JWT + contents: read # This is required for actions/checkout + strategy: + fail-fast: false + matrix: + include: + - { engine: pg, skip: mysql } + - { engine: mysql, skip: pg } + + steps: + - name: 'Clone repository' + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 + + - name: 'Set up JDK 8' + uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654 # v5 + with: + distribution: 'corretto' + java-version: 8 + + - name: 'Set up Python 3.12' + uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0 + with: + python-version: '3.12' + + - name: Install poetry + shell: bash + run: | + pipx install poetry==1.8.2 + poetry config virtualenvs.prefer-active-python true + + - name: Install dependencies + run: poetry install + + - name: 'Configure AWS Credentials' + uses: aws-actions/configure-aws-credentials@d979d5b3a71173a29b74b5b88418bfda9437d885 # v6 + with: + role-to-assume: ${{ vars.KMS_IT_ROLE_ARN }} + role-session-name: python_kms_integration_tests + role-duration-seconds: 3600 + aws-region: ${{ vars.KMS_REGION }} + + - name: 'Run the KMS tests on Docker with a real KMS key' + run: | + test -n "${KMS_KEY_ARN}" || { echo "::error::Set the KMS_KEY_ARN repository variable: CI uses the administrator's key and never makes one."; exit 1; } + DB_PASSWORD="$(openssl rand -hex 16)" + echo "::add-mask::${DB_PASSWORD}" + export DB_PASSWORD DB_USERNAME=test_user DB_DATABASE_NAME=test_db + ./gradlew --no-parallel --no-daemon test-kms-encryption-docker-kms \ + -Dexclude-${{ matrix.skip }}-engine=true -Dexclude-${{ matrix.skip }}-driver=true + env: + KMS_KEY_ARN: ${{ vars.KMS_KEY_ARN }} + KMS_REGION: ${{ vars.KMS_REGION }} + KMS_DECRYPT_ONLY_ROLE_ARN: ${{ vars.KMS_DECRYPT_ONLY_ROLE_ARN }} + + - name: 'Fail when no KMS test ran' + run: | + python3 -c 'import glob,sys,xml.etree.ElementTree as E; c=[t for f in glob.glob("tests/integration/container/reports/*.xml") for t in E.parse(f).iter("testcase") if "kms_encryption" in t.get("classname","") and t.find("skipped") is None]; print("KMS tests run:", len(c)); sys.exit(0 if c else 1)' + + - name: 'Archive results' + if: always() + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: kms-real-key-${{ matrix.engine }} + path: ./tests/integration/container/reports + retention-days: 5 + + kms-aurora: + name: Run KMS Encryption Tests on Aurora + if: >- + (github.event_name == 'push' && github.ref == 'refs/heads/main' && vars.KMS_KEY_ARN != '' && vars.KMS_REGION != '') + || inputs.lane == 'aurora' + runs-on: ubuntu-latest + timeout-minutes: 150 + permissions: + id-token: write # This is required for requesting the JWT + contents: read # This is required for actions/checkout + strategy: + fail-fast: false + matrix: + include: + - { engine: pg, skip: mysql } + - { engine: mysql, skip: pg } + concurrency: + group: kms-aurora-${{ matrix.engine }} + cancel-in-progress: false + env: + # The harness doesn't delete its cluster when a run is cancelled, so the last step deletes this one by name. + RDS_DB_NAME: kms-it-${{ github.run_id }}-${{ github.run_attempt }}-${{ matrix.engine }} + + steps: + - name: 'Clone repository' + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 + + - name: 'Set up JDK 8' + uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654 # v5 + with: + distribution: 'corretto' + java-version: 8 + + - name: 'Set up Python 3.12' + uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0 + with: + python-version: '3.12' + + - name: Install poetry + shell: bash + run: | + pipx install poetry==1.8.2 + poetry config virtualenvs.prefer-active-python true + + - name: Install dependencies + run: poetry install + + - name: 'Configure AWS Credentials' + uses: aws-actions/configure-aws-credentials@d979d5b3a71173a29b74b5b88418bfda9437d885 # v6 + with: + role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/${{ secrets.AWS_DEPLOY_ROLE }} + role-session-name: python_integration_tests + role-duration-seconds: 21600 + aws-region: ${{ secrets.AWS_DEFAULT_REGION }} + + - name: 'Run the KMS tests on Aurora' + run: | + test -n "${KMS_KEY_ARN}" || { echo "::error::Set the KMS_KEY_ARN repository variable: CI uses the administrator's key and never makes one."; exit 1; } + DB_PASSWORD="$(openssl rand -hex 16)" + echo "::add-mask::${DB_PASSWORD}" + export DB_PASSWORD DB_USERNAME=test_user DB_DATABASE_NAME=test_db + ./gradlew --no-parallel --no-daemon test-kms-encryption-aurora \ + -Dexclude-${{ matrix.skip }}-engine=true -Dexclude-${{ matrix.skip }}-driver=true + env: + NUM_INSTANCES: 2 + RDS_DB_REGION: ${{ secrets.AWS_DEFAULT_REGION }} + KMS_KEY_ARN: ${{ vars.KMS_KEY_ARN }} + KMS_REGION: ${{ vars.KMS_REGION }} + + - name: 'Fail when no KMS test ran' + run: | + python3 -c 'import glob,sys,xml.etree.ElementTree as E; c=[t for f in glob.glob("tests/integration/container/reports/*.xml") for t in E.parse(f).iter("testcase") if "kms_encryption" in t.get("classname","") and t.find("skipped") is None]; print("KMS tests run:", len(c)); sys.exit(0 if c else 1)' + + - name: 'Archive results' + if: always() + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: kms-aurora-${{ matrix.engine }} + path: ./tests/integration/container/reports + retention-days: 5 + + # Deletes the cluster named RDS_DB_NAME and its member instances, never clusters matched by a name prefix. + - name: 'Delete the cluster of this run' + if: always() + run: | + instances="$(aws rds describe-db-clusters --db-cluster-identifier "${RDS_DB_NAME}" \ + --query 'DBClusters[0].DBClusterMembers[].DBInstanceIdentifier' --output text 2>/dev/null)" || exit 0 + for instance in ${instances}; do + aws rds delete-db-instance --db-instance-identifier "${instance}" --skip-final-snapshot > /dev/null || true + done + aws rds delete-db-cluster --db-cluster-identifier "${RDS_DB_NAME}" --skip-final-snapshot > /dev/null || true diff --git a/docs/development-guide/IntegrationTests.md b/docs/development-guide/IntegrationTests.md index 3e018297b..52779c4e4 100644 --- a/docs/development-guide/IntegrationTests.md +++ b/docs/development-guide/IntegrationTests.md @@ -64,9 +64,36 @@ unset FILTER # Done testing the IAM tests, unset FILTER 6. In the git bash terminal, navigate to the project root directory and execute a gradle integration test task, e.g.: `./gradlew test-pg-aurora`. Other tasks are defined in `tests/integration/host/build.gradle.kts`. +### Existing clusters and leftovers + +When `REUSE_RDS_DB` isn't `true` and `RDS_DB_NAME` names a cluster that exists, an Aurora run fails before it adds the rule for the runner's IP or makes a cluster. +Then set `REUSE_RDS_DB=true` to use that cluster, or set `RDS_DB_NAME` to a new name. +An Aurora run adds an all-traffic rule for the runner's IP to the `default` security group, described `Test run at