17
17
import base64
18
18
import copy
19
19
import os
20
+ import re
20
21
import traceback
21
22
import socket
22
23
import sys
@@ -1146,13 +1147,15 @@ def setUp(self):
1146
1147
codec_options = OPTS )
1147
1148
1148
1149
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'
1151
1153
self .client_encryption_invalid = ClientEncryption (
1152
1154
kms_providers = kms_providers_invalid ,
1153
1155
key_vault_namespace = 'keyvault.datakeys' ,
1154
1156
key_vault_client = client_context .client ,
1155
1157
codec_options = OPTS )
1158
+ self ._invalid_host_error = None
1156
1159
1157
1160
def tearDown (self ):
1158
1161
self .client_encryption .close ()
@@ -1233,9 +1236,9 @@ def test_06_aws_endpoint_invalid_host(self):
1233
1236
"region" : "us-east-1" ,
1234
1237
"key" : ("arn:aws:kms:us-east-1:579766882180:key/"
1235
1238
"89fcc2c4-08b0-4bd9-9f25-e30687b580d0" ),
1236
- "endpoint" : "example.com "
1239
+ "endpoint" : "doesnotexist.invalid "
1237
1240
}
1238
- with self .assertRaisesRegex (EncryptionError , 'parse error' ):
1241
+ with self .assertRaisesRegex (EncryptionError , self . invalid_host_error ):
1239
1242
self .client_encryption .create_data_key (
1240
1243
'aws' , master_key = master_key )
1241
1244
@@ -1247,8 +1250,8 @@ def test_07_azure(self):
1247
1250
self .run_test_expected_success ('azure' , master_key )
1248
1251
1249
1252
# 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 ):
1252
1255
self .client_encryption_invalid .create_data_key (
1253
1256
'azure' , master_key = master_key )
1254
1257
@@ -1264,8 +1267,8 @@ def test_08_gcp_valid_endpoint(self):
1264
1267
self .run_test_expected_success ('gcp' , master_key )
1265
1268
1266
1269
# 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 ):
1269
1272
self .client_encryption_invalid .create_data_key (
1270
1273
'gcp' , master_key = master_key )
1271
1274
@@ -1277,7 +1280,7 @@ def test_09_gcp_invalid_endpoint(self):
1277
1280
"location" : "global" ,
1278
1281
"keyRing" : "key-ring-csfle" ,
1279
1282
"keyName" : "key-name-csfle" ,
1280
- "endpoint" : "example.com :443" }
1283
+ "endpoint" : "doesnotexist.invalid :443" }
1281
1284
1282
1285
# The full error should be something like:
1283
1286
# "Invalid KMS response, no access_token returned. HTTP status=200"
@@ -1286,6 +1289,20 @@ def test_09_gcp_invalid_endpoint(self):
1286
1289
'gcp' , master_key = master_key )
1287
1290
1288
1291
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
+
1289
1306
class AzureGCPEncryptionTestMixin (object ):
1290
1307
DEK = None
1291
1308
KMS_PROVIDER_MAP = None
@@ -1366,7 +1383,7 @@ def test_explicit(self):
1366
1383
'AQGVERPgAAAAAAAAAAAAAAAC5DbBSwPwfSlBrDtRuglvNvCXD1KzDuCKY2P+4bRFtHDjpTOE2XuytPAUaAbXf1orsPq59PVZmsbTZbt2CB8qaQ==' )
1367
1384
1368
1385
def test_automatic (self ):
1369
- expected_document_extjson = textwrap .dedent ("""
1386
+ expected_document_extjson = textwrap .dedent ("""
1370
1387
{"secret_azure": {
1371
1388
"$binary": {
1372
1389
"base64": "AQGVERPgAAAAAAAAAAAAAAAC5DbBSwPwfSlBrDtRuglvNvCXD1KzDuCKY2P+4bRFtHDjpTOE2XuytPAUaAbXf1orsPq59PVZmsbTZbt2CB8qaQ==",
@@ -1392,7 +1409,7 @@ def test_explicit(self):
1392
1409
'ARgj/gAAAAAAAAAAAAAAAAACwFd+Y5Ojw45GUXNvbcIpN9YkRdoHDHkR4kssdn0tIMKlDQOLFkWFY9X07IRlXsxPD8DcTiKnl6XINK28vhcGlg==' )
1393
1410
1394
1411
def test_automatic (self ):
1395
- expected_document_extjson = textwrap .dedent ("""
1412
+ expected_document_extjson = textwrap .dedent ("""
1396
1413
{"secret_gcp": {
1397
1414
"$binary": {
1398
1415
"base64": "ARgj/gAAAAAAAAAAAAAAAAACwFd+Y5Ojw45GUXNvbcIpN9YkRdoHDHkR4kssdn0tIMKlDQOLFkWFY9X07IRlXsxPD8DcTiKnl6XINK28vhcGlg==",
0 commit comments