@@ -1174,16 +1174,17 @@ def setUp(self):
1174
1174
kms_tls_options = KMS_TLS_OPTS )
1175
1175
1176
1176
kms_providers_invalid = copy .deepcopy (kms_providers )
1177
- kms_providers_invalid ['azure' ]['identityPlatformEndpoint' ] = 'example.com :443'
1178
- kms_providers_invalid ['gcp' ]['endpoint' ] = 'example.com :443'
1177
+ kms_providers_invalid ['azure' ]['identityPlatformEndpoint' ] = 'doesnotexist.invalid :443'
1178
+ kms_providers_invalid ['gcp' ]['endpoint' ] = 'doesnotexist.invalid :443'
1179
1179
kms_providers_invalid ['kmip' ]['endpoint' ] = 'doesnotexist.local:5698'
1180
1180
self .client_encryption_invalid = ClientEncryption (
1181
1181
kms_providers = kms_providers_invalid ,
1182
1182
key_vault_namespace = 'keyvault.datakeys' ,
1183
1183
key_vault_client = client_context .client ,
1184
1184
codec_options = OPTS ,
1185
1185
kms_tls_options = KMS_TLS_OPTS )
1186
- self ._kmip_host_error = ''
1186
+ self ._kmip_host_error = None
1187
+ self ._invalid_host_error = None
1187
1188
1188
1189
def tearDown (self ):
1189
1190
self .client_encryption .close ()
@@ -1264,9 +1265,9 @@ def test_06_aws_endpoint_invalid_host(self):
1264
1265
"region" : "us-east-1" ,
1265
1266
"key" : ("arn:aws:kms:us-east-1:579766882180:key/"
1266
1267
"89fcc2c4-08b0-4bd9-9f25-e30687b580d0" ),
1267
- "endpoint" : "example.com "
1268
+ "endpoint" : "doesnotexist.invalid "
1268
1269
}
1269
- with self .assertRaisesRegex (EncryptionError , 'parse error' ):
1270
+ with self .assertRaisesRegex (EncryptionError , self . invalid_host_error ):
1270
1271
self .client_encryption .create_data_key (
1271
1272
'aws' , master_key = master_key )
1272
1273
@@ -1278,8 +1279,8 @@ def test_07_azure(self):
1278
1279
self .run_test_expected_success ('azure' , master_key )
1279
1280
1280
1281
# The full error should be something like:
1281
- # "Invalid JSON in KMS response. HTTP status=404. Error: Got parse error at '<', position 0: 'SPECIAL_EXPECTED' "
1282
- with self .assertRaisesRegex (EncryptionError , 'parse error' ):
1282
+ # "[Errno 8] nodename nor servname provided, or not known "
1283
+ with self .assertRaisesRegex (EncryptionError , self . invalid_host_error ):
1283
1284
self .client_encryption_invalid .create_data_key (
1284
1285
'azure' , master_key = master_key )
1285
1286
@@ -1295,8 +1296,8 @@ def test_08_gcp_valid_endpoint(self):
1295
1296
self .run_test_expected_success ('gcp' , master_key )
1296
1297
1297
1298
# The full error should be something like:
1298
- # "Invalid JSON in KMS response. HTTP status=404. Error: Got parse error at '<', position 0: 'SPECIAL_EXPECTED' "
1299
- with self .assertRaisesRegex (EncryptionError , 'parse error' ):
1299
+ # "[Errno 8] nodename nor servname provided, or not known "
1300
+ with self .assertRaisesRegex (EncryptionError , self . invalid_host_error ):
1300
1301
self .client_encryption_invalid .create_data_key (
1301
1302
'gcp' , master_key = master_key )
1302
1303
@@ -1308,30 +1309,38 @@ def test_09_gcp_invalid_endpoint(self):
1308
1309
"location" : "global" ,
1309
1310
"keyRing" : "key-ring-csfle" ,
1310
1311
"keyName" : "key-name-csfle" ,
1311
- "endpoint" : "example.com :443" }
1312
+ "endpoint" : "doesnotexist.invalid :443" }
1312
1313
1313
1314
# The full error should be something like:
1314
1315
# "Invalid KMS response, no access_token returned. HTTP status=200"
1315
1316
with self .assertRaisesRegex (EncryptionError , "Invalid KMS response" ):
1316
1317
self .client_encryption .create_data_key (
1317
1318
'gcp' , master_key = master_key )
1318
1319
1319
- def kmip_host_error (self ):
1320
- if self ._kmip_host_error :
1321
- return self ._kmip_host_error
1320
+ def dns_error (self , host , port ):
1322
1321
# The full error should be something like:
1323
1322
# "[Errno 8] nodename nor servname provided, or not known"
1324
- try :
1325
- socket .getaddrinfo ('doesnotexist.local' , 5698 , socket .AF_INET ,
1326
- socket .SOCK_STREAM )
1327
- except Exception as exc :
1328
- self ._kmip_host_error = re .escape (str (exc ))
1329
- return self ._kmip_host_error
1323
+ with self .assertRaises (Exception ) as ctx :
1324
+ socket .getaddrinfo (host , port , socket .AF_INET , socket .SOCK_STREAM )
1325
+ return re .escape (str (ctx .exception ))
1326
+
1327
+ @property
1328
+ def invalid_host_error (self ):
1329
+ if self ._invalid_host_error is None :
1330
+ self ._invalid_host_error = self .dns_error (
1331
+ 'doesnotexist.invalid' , 443 )
1332
+ return self ._invalid_host_error
1333
+
1334
+ @property
1335
+ def kmip_host_error (self ):
1336
+ if self ._kmip_host_error is None :
1337
+ self ._kmip_host_error = self .dns_error ('doesnotexist.local' , 5698 )
1338
+ return self ._kmip_host_error
1330
1339
1331
1340
def test_10_kmip_invalid_endpoint (self ):
1332
1341
key = {'keyId' : '1' }
1333
1342
self .run_test_expected_success ('kmip' , key )
1334
- with self .assertRaisesRegex (EncryptionError , self .kmip_host_error () ):
1343
+ with self .assertRaisesRegex (EncryptionError , self .kmip_host_error ):
1335
1344
self .client_encryption_invalid .create_data_key ('kmip' , key )
1336
1345
1337
1346
def test_11_kmip_master_key_endpoint (self ):
@@ -1348,7 +1357,7 @@ def test_11_kmip_master_key_endpoint(self):
1348
1357
1349
1358
def test_12_kmip_master_key_invalid_endpoint (self ):
1350
1359
key = {'keyId' : '1' , 'endpoint' : 'doesnotexist.local:5698' }
1351
- with self .assertRaisesRegex (EncryptionError , self .kmip_host_error () ):
1360
+ with self .assertRaisesRegex (EncryptionError , self .kmip_host_error ):
1352
1361
self .client_encryption .create_data_key ('kmip' , key )
1353
1362
1354
1363
0 commit comments