Skip to content

Commit cc89650

Browse files
PYTHON-3110 [v3.13] Remove use of example.com in CSFLE tests (#848) (#851)
Co-authored-by: Shane Harvey <[email protected]>
1 parent 4c851aa commit cc89650

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

test/test_encryption.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import base64
1818
import copy
1919
import os
20+
import re
2021
import traceback
2122
import socket
2223
import sys
@@ -1146,13 +1147,15 @@ def setUp(self):
11461147
codec_options=OPTS)
11471148

11481149
kms_providers_invalid = copy.deepcopy(kms_providers)
1149-
kms_providers_invalid['azure']['identityPlatformEndpoint'] = 'example.com:443'
1150-
kms_providers_invalid['gcp']['endpoint'] = 'example.com:443'
1150+
1151+
kms_providers_invalid['azure']['identityPlatformEndpoint'] = 'doesnotexist.invalid:443'
1152+
kms_providers_invalid['gcp']['endpoint'] = 'doesnotexist.invalid:443'
11511153
self.client_encryption_invalid = ClientEncryption(
11521154
kms_providers=kms_providers_invalid,
11531155
key_vault_namespace='keyvault.datakeys',
11541156
key_vault_client=client_context.client,
11551157
codec_options=OPTS)
1158+
self._invalid_host_error = None
11561159

11571160
def tearDown(self):
11581161
self.client_encryption.close()
@@ -1233,9 +1236,9 @@ def test_06_aws_endpoint_invalid_host(self):
12331236
"region": "us-east-1",
12341237
"key": ("arn:aws:kms:us-east-1:579766882180:key/"
12351238
"89fcc2c4-08b0-4bd9-9f25-e30687b580d0"),
1236-
"endpoint": "example.com"
1239+
"endpoint": "doesnotexist.invalid"
12371240
}
1238-
with self.assertRaisesRegex(EncryptionError, 'parse error'):
1241+
with self.assertRaisesRegex(EncryptionError, self.invalid_host_error):
12391242
self.client_encryption.create_data_key(
12401243
'aws', master_key=master_key)
12411244

@@ -1247,8 +1250,8 @@ def test_07_azure(self):
12471250
self.run_test_expected_success('azure', master_key)
12481251

12491252
# The full error should be something like:
1250-
# "Invalid JSON in KMS response. HTTP status=404. Error: Got parse error at '<', position 0: 'SPECIAL_EXPECTED'"
1251-
with self.assertRaisesRegex(EncryptionError, 'parse error'):
1253+
# "[Errno 8] nodename nor servname provided, or not known"
1254+
with self.assertRaisesRegex(EncryptionError, self.invalid_host_error):
12521255
self.client_encryption_invalid.create_data_key(
12531256
'azure', master_key=master_key)
12541257

@@ -1264,8 +1267,8 @@ def test_08_gcp_valid_endpoint(self):
12641267
self.run_test_expected_success('gcp', master_key)
12651268

12661269
# The full error should be something like:
1267-
# "Invalid JSON in KMS response. HTTP status=404. Error: Got parse error at '<', position 0: 'SPECIAL_EXPECTED'"
1268-
with self.assertRaisesRegex(EncryptionError, 'parse error'):
1270+
# "[Errno 8] nodename nor servname provided, or not known"
1271+
with self.assertRaisesRegex(EncryptionError, self.invalid_host_error):
12691272
self.client_encryption_invalid.create_data_key(
12701273
'gcp', master_key=master_key)
12711274

@@ -1277,7 +1280,7 @@ def test_09_gcp_invalid_endpoint(self):
12771280
"location": "global",
12781281
"keyRing": "key-ring-csfle",
12791282
"keyName": "key-name-csfle",
1280-
"endpoint": "example.com:443"}
1283+
"endpoint": "doesnotexist.invalid:443"}
12811284

12821285
# The full error should be something like:
12831286
# "Invalid KMS response, no access_token returned. HTTP status=200"
@@ -1286,6 +1289,20 @@ def test_09_gcp_invalid_endpoint(self):
12861289
'gcp', master_key=master_key)
12871290

12881291

1292+
def dns_error(self, host, port):
1293+
# The full error should be something like:
1294+
# "[Errno 8] nodename nor servname provided, or not known"
1295+
with self.assertRaises(Exception) as ctx:
1296+
socket.getaddrinfo(host, port, socket.AF_INET, socket.SOCK_STREAM)
1297+
return re.escape(str(ctx.exception))
1298+
1299+
@property
1300+
def invalid_host_error(self):
1301+
if self._invalid_host_error is None:
1302+
self._invalid_host_error = self.dns_error(
1303+
'doesnotexist.invalid', 443)
1304+
return self._invalid_host_error
1305+
12891306
class AzureGCPEncryptionTestMixin(object):
12901307
DEK = None
12911308
KMS_PROVIDER_MAP = None
@@ -1366,7 +1383,7 @@ def test_explicit(self):
13661383
'AQGVERPgAAAAAAAAAAAAAAAC5DbBSwPwfSlBrDtRuglvNvCXD1KzDuCKY2P+4bRFtHDjpTOE2XuytPAUaAbXf1orsPq59PVZmsbTZbt2CB8qaQ==')
13671384

13681385
def test_automatic(self):
1369-
expected_document_extjson = textwrap.dedent("""
1386+
expected_document_extjson = textwrap.dedent("""
13701387
{"secret_azure": {
13711388
"$binary": {
13721389
"base64": "AQGVERPgAAAAAAAAAAAAAAAC5DbBSwPwfSlBrDtRuglvNvCXD1KzDuCKY2P+4bRFtHDjpTOE2XuytPAUaAbXf1orsPq59PVZmsbTZbt2CB8qaQ==",
@@ -1392,7 +1409,7 @@ def test_explicit(self):
13921409
'ARgj/gAAAAAAAAAAAAAAAAACwFd+Y5Ojw45GUXNvbcIpN9YkRdoHDHkR4kssdn0tIMKlDQOLFkWFY9X07IRlXsxPD8DcTiKnl6XINK28vhcGlg==')
13931410

13941411
def test_automatic(self):
1395-
expected_document_extjson = textwrap.dedent("""
1412+
expected_document_extjson = textwrap.dedent("""
13961413
{"secret_gcp": {
13971414
"$binary": {
13981415
"base64": "ARgj/gAAAAAAAAAAAAAAAAACwFd+Y5Ojw45GUXNvbcIpN9YkRdoHDHkR4kssdn0tIMKlDQOLFkWFY9X07IRlXsxPD8DcTiKnl6XINK28vhcGlg==",

0 commit comments

Comments
 (0)