Repository navigation
PYTHON-6147 Add CSFLE HTTP proxy KMS connect helpers #3101
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
29cfcc0
PYTHON-6147 Add HTTP proxy KMS connect helpers
blink1073 7e3828c
PYTHON-6147 Normalize malformed proxy URLs as configuration errors
blink1073 4c6ab22
Require a full token match for proxy header names
blink1073 ef5c2f6
PYTHON-6147 Keep the tunnel reverse direction alive on a half-close
blink1073 1a0e819
PYTHON-6147 Reject proxy port zero and bound the async proxy wait
blink1073 32d4126
PYTHON-6147 Close the late bridge and any socket returned at the dead…
blink1073 df84a1f
PYTHON-6147 Validate the KMS port before the CONNECT request
blink1073 715557a
Revert "PYTHON-6147 Validate the KMS port before the CONNECT request"
blink1073 7022136
PYTHON-6147 Redact userinfo from proxy_url validation errors
blink1073 53667dc
PYTHON-6147 Redact userinfo from proxy_url validation errors
blink1073 546f7fe
PYTHON-6147 Redact userinfo and revalidate proxy headers at connect time
blink1073 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| """Integration tests for the KMS connect callback and HTTP proxy support. | ||
|
|
||
| The unit tests live in ``test/test_kms_connect.py`` (a file that synchro | ||
| does not process). This module adds the integration tests, which run real | ||
| KMS traffic through a local proxy and are executed against both APIs via | ||
| the generated synchronous mirror. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| import http.client | ||
| import ssl | ||
| import unittest | ||
| from typing import Any | ||
|
|
||
| import pytest | ||
|
|
||
| from bson.binary import Binary | ||
| from pymongo.encryption_options import AsyncHTTPProxyKMSConnect, AutoEncryptionOpts | ||
| from pymongo.errors import EncryptionError | ||
| from test.asynchronous.test_encryption import OPTS, AsyncEncryptionIntegrationTest | ||
| from test.helpers_shared import AWS_CREDS, CA_PEM | ||
|
|
||
| _IS_SYNC = False | ||
|
|
||
| pytestmark = pytest.mark.encryption | ||
|
|
||
| KMS_PROXY_HOST = "127.0.0.1" | ||
| KMS_PROXY_PORT = 9004 | ||
| KMS_TLS_PROXY_PORT = 9005 | ||
|
|
||
| AWS_MASTER_KEY = { | ||
| "region": "us-east-1", | ||
| "key": "arn:aws:kms:us-east-1:579766882180:key/89fcc2c4-08b0-4bd9-9f25-e30687b580d0", | ||
| } | ||
|
|
||
|
|
||
| class TestKmsConnectCallbackProse(AsyncEncryptionIntegrationTest): | ||
| @unittest.skipUnless(any(AWS_CREDS.values()), "AWS environment credentials are not set") | ||
| async def asyncSetUp(self): | ||
| await super().asyncSetUp() | ||
| self.callback_calls: list[Any] = [] | ||
|
|
||
| async def plain_callback(self, context): | ||
| self.callback_calls.append(context) | ||
| return await AsyncHTTPProxyKMSConnect(f"http://{KMS_PROXY_HOST}:{KMS_PROXY_PORT}")(context) | ||
|
|
||
| def _proxy_tls_context(self): | ||
| ctx = ssl.create_default_context(cafile=CA_PEM) | ||
| ctx.check_hostname = False | ||
| # PYTHON-5040 tracks re-enabling verification once the test CA cert | ||
| # is fixed. The evergreen-tools CA lacks an Authority Key Identifier | ||
| # that newer OpenSSL requires, so verification fails on Windows 3.14. | ||
| ctx.verify_mode = ssl.CERT_NONE | ||
| return ctx | ||
|
|
||
| async def tls_callback(self, context): | ||
| self.callback_calls.append(context) | ||
| callback = AsyncHTTPProxyKMSConnect( | ||
| f"https://{KMS_PROXY_HOST}:{KMS_TLS_PROXY_PORT}", self._proxy_tls_context() | ||
| ) | ||
| return await callback(context) | ||
|
|
||
| async def proxy_request(self, method, path, tls=False): | ||
| """Call the proxy's control endpoints and return the body.""" | ||
| if _IS_SYNC: | ||
| return self._proxy_request(method, path, tls) | ||
| return await asyncio.get_running_loop().run_in_executor( | ||
| None, self._proxy_request, method, path, tls | ||
| ) | ||
|
|
||
| def _proxy_request(self, method, path, tls=False): | ||
| if tls: | ||
| conn = http.client.HTTPSConnection( | ||
| f"{KMS_PROXY_HOST}:{KMS_TLS_PROXY_PORT}", context=self._proxy_tls_context() | ||
| ) | ||
| else: | ||
| conn = http.client.HTTPConnection(f"{KMS_PROXY_HOST}:{KMS_PROXY_PORT}") | ||
| try: | ||
| conn.request(method, path) | ||
| return conn.getresponse().read().decode() | ||
| finally: | ||
| conn.close() | ||
|
|
||
| async def connect_count(self, tls=False): | ||
| body = await self.proxy_request("GET", "/metrics", tls=tls) | ||
| # One "key value" per line. The server also emits connect_target. | ||
| for line in body.splitlines(): | ||
| key, _, value = line.partition(" ") | ||
| if key == "connect_count": | ||
| return int(value) | ||
| raise AssertionError(f"no connect_count in metrics body: {body!r}") | ||
|
|
||
| async def test_01_plain_http_proxy(self): | ||
| await self.proxy_request("POST", "/reset") | ||
| encryption = self.create_client_encryption( | ||
| {"aws": AWS_CREDS}, | ||
| "keyvault.datakeys", | ||
| self.client, | ||
| OPTS, | ||
| kms_connect_callback=self.plain_callback, | ||
| ) | ||
| await encryption.create_data_key("aws", master_key=AWS_MASTER_KEY) | ||
| self.assertGreaterEqual(await self.connect_count(), 1) | ||
|
|
||
| async def test_02_https_proxy(self): | ||
| await self.proxy_request("POST", "/reset", tls=True) | ||
| encryption = self.create_client_encryption( | ||
| {"aws": AWS_CREDS}, | ||
| "keyvault.datakeys", | ||
| self.client, | ||
| OPTS, | ||
| kms_connect_callback=self.tls_callback, | ||
| ) | ||
| await encryption.create_data_key("aws", master_key=AWS_MASTER_KEY) | ||
| self.assertGreaterEqual(await self.connect_count(tls=True), 1) | ||
|
|
||
| async def test_03_auto_encryption_through_proxy(self): | ||
| await self.client.keyvault.datakeys.drop() | ||
| await self.client.db.coll.drop() | ||
|
|
||
| encryption = self.create_client_encryption( | ||
| {"aws": AWS_CREDS}, | ||
| "keyvault.datakeys", | ||
| self.client, | ||
| OPTS, | ||
| kms_connect_callback=self.plain_callback, | ||
| ) | ||
| data_key_id = await encryption.create_data_key("aws", master_key=AWS_MASTER_KEY) | ||
| schema = { | ||
| "bsonType": "object", | ||
| "properties": { | ||
| "encrypted_string": { | ||
| "encrypt": { | ||
| "keyId": [data_key_id], | ||
| "bsonType": "string", | ||
| "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic", | ||
| } | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| await self.proxy_request("POST", "/reset") | ||
| opts = AutoEncryptionOpts( | ||
| {"aws": AWS_CREDS}, | ||
| "keyvault.datakeys", | ||
| schema_map={"db.coll": schema}, | ||
| kms_connect_callback=self.plain_callback, | ||
| ) | ||
| client_encrypted = await self.async_rs_or_single_client(auto_encryption_opts=opts) | ||
|
|
||
| await client_encrypted.db.coll.insert_one({"_id": 1, "encrypted_string": "hello"}) | ||
| decrypted = await client_encrypted.db.coll.find_one({"_id": 1}) | ||
| self.assertEqual(decrypted["encrypted_string"], "hello") | ||
|
|
||
| raw = await self.client.db.coll.find_one({"_id": 1}) | ||
| self.assertIsInstance(raw["encrypted_string"], Binary) | ||
|
|
||
| # The decrypt reuses the cached key, so exactly one KMS request follows | ||
| # the reset. | ||
| self.assertEqual(await self.connect_count(), 1) | ||
|
|
||
| async def test_04_callback_error(self): | ||
| async def failing_callback(context): | ||
| raise OSError("proxy is on fire") | ||
|
|
||
| encryption = self.create_client_encryption( | ||
| {"aws": AWS_CREDS}, | ||
| "keyvault.datakeys", | ||
| self.client, | ||
| OPTS, | ||
| kms_connect_callback=failing_callback, | ||
| ) | ||
| with self.assertRaisesRegex(EncryptionError, "proxy is on fire"): | ||
| await encryption.create_data_key("aws", master_key=AWS_MASTER_KEY) | ||
|
|
||
| @unittest.skip( | ||
| "PYTHON-6037 ClientEncryption does not support timeoutMS, so the " | ||
| "callback always receives the default KMS connect timeout" | ||
| ) | ||
| async def test_05_callback_receives_timeout(self): | ||
| key_vault_client = await self.async_rs_or_single_client(timeoutMS=1000) | ||
| encryption = self.create_client_encryption( | ||
| {"aws": AWS_CREDS}, | ||
| "keyvault.datakeys", | ||
| key_vault_client, | ||
| OPTS, | ||
| kms_connect_callback=self.plain_callback, | ||
| ) | ||
| await encryption.create_data_key("aws", master_key=AWS_MASTER_KEY) | ||
|
|
||
| self.assertTrue(self.callback_calls, "callback was never invoked") | ||
| for context in self.callback_calls: | ||
| # Checks only the spec's non-zero requirement, which cannot fail. | ||
| self.assertIsNotNone(context.timeout) | ||
| self.assertGreater(context.timeout, 0) | ||
|
|
||
| async def test_06_retry_after_network_error(self): | ||
| state = {"calls": 0} | ||
|
|
||
| async def flaky_callback(context): | ||
| state["calls"] += 1 | ||
| if state["calls"] == 1: | ||
| raise OSError("first attempt fails") | ||
| return await AsyncHTTPProxyKMSConnect(f"http://{KMS_PROXY_HOST}:{KMS_PROXY_PORT}")( | ||
| context | ||
| ) | ||
|
|
||
| encryption = self.create_client_encryption( | ||
| {"aws": AWS_CREDS}, | ||
| "keyvault.datakeys", | ||
| self.client, | ||
| OPTS, | ||
| kms_connect_callback=flaky_callback, | ||
| ) | ||
| await encryption.create_data_key("aws", master_key=AWS_MASTER_KEY) | ||
| self.assertGreaterEqual(state["calls"], 2) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.