@@ -1205,16 +1205,17 @@ def setUp(self):
12051205 kms_tls_options = KMS_TLS_OPTS )
12061206
12071207 kms_providers_invalid = copy .deepcopy (kms_providers )
1208- kms_providers_invalid ['azure' ]['identityPlatformEndpoint' ] = 'example.com :443'
1209- kms_providers_invalid ['gcp' ]['endpoint' ] = 'example.com :443'
1208+ kms_providers_invalid ['azure' ]['identityPlatformEndpoint' ] = 'doesnotexist.invalid :443'
1209+ kms_providers_invalid ['gcp' ]['endpoint' ] = 'doesnotexist.invalid :443'
12101210 kms_providers_invalid ['kmip' ]['endpoint' ] = 'doesnotexist.local:5698'
12111211 self .client_encryption_invalid = ClientEncryption (
12121212 kms_providers = kms_providers_invalid ,
12131213 key_vault_namespace = 'keyvault.datakeys' ,
12141214 key_vault_client = client_context .client ,
12151215 codec_options = OPTS ,
12161216 kms_tls_options = KMS_TLS_OPTS )
1217- self ._kmip_host_error = ''
1217+ self ._kmip_host_error = None
1218+ self ._invalid_host_error = None
12181219
12191220 def tearDown (self ):
12201221 self .client_encryption .close ()
@@ -1295,9 +1296,9 @@ def test_06_aws_endpoint_invalid_host(self):
12951296 "region" : "us-east-1" ,
12961297 "key" : ("arn:aws:kms:us-east-1:579766882180:key/"
12971298 "89fcc2c4-08b0-4bd9-9f25-e30687b580d0" ),
1298- "endpoint" : "example.com "
1299+ "endpoint" : "doesnotexist.invalid "
12991300 }
1300- with self .assertRaisesRegex (EncryptionError , 'parse error' ):
1301+ with self .assertRaisesRegex (EncryptionError , self . invalid_host_error ):
13011302 self .client_encryption .create_data_key (
13021303 'aws' , master_key = master_key )
13031304
@@ -1309,8 +1310,8 @@ def test_07_azure(self):
13091310 self .run_test_expected_success ('azure' , master_key )
13101311
13111312 # The full error should be something like:
1312- # "Invalid JSON in KMS response. HTTP status=404. Error: Got parse error at '<', position 0: 'SPECIAL_EXPECTED' "
1313- with self .assertRaisesRegex (EncryptionError , 'parse error' ):
1313+ # "[Errno 8] nodename nor servname provided, or not known "
1314+ with self .assertRaisesRegex (EncryptionError , self . invalid_host_error ):
13141315 self .client_encryption_invalid .create_data_key (
13151316 'azure' , master_key = master_key )
13161317
@@ -1326,8 +1327,8 @@ def test_08_gcp_valid_endpoint(self):
13261327 self .run_test_expected_success ('gcp' , master_key )
13271328
13281329 # The full error should be something like:
1329- # "Invalid JSON in KMS response. HTTP status=404. Error: Got parse error at '<', position 0: 'SPECIAL_EXPECTED' "
1330- with self .assertRaisesRegex (EncryptionError , 'parse error' ):
1330+ # "[Errno 8] nodename nor servname provided, or not known "
1331+ with self .assertRaisesRegex (EncryptionError , self . invalid_host_error ):
13311332 self .client_encryption_invalid .create_data_key (
13321333 'gcp' , master_key = master_key )
13331334
@@ -1339,30 +1340,38 @@ def test_09_gcp_invalid_endpoint(self):
13391340 "location" : "global" ,
13401341 "keyRing" : "key-ring-csfle" ,
13411342 "keyName" : "key-name-csfle" ,
1342- "endpoint" : "example.com :443" }
1343+ "endpoint" : "doesnotexist.invalid :443" }
13431344
13441345 # The full error should be something like:
13451346 # "Invalid KMS response, no access_token returned. HTTP status=200"
13461347 with self .assertRaisesRegex (EncryptionError , "Invalid KMS response" ):
13471348 self .client_encryption .create_data_key (
13481349 'gcp' , master_key = master_key )
13491350
1350- def kmip_host_error (self ):
1351- if self ._kmip_host_error :
1352- return self ._kmip_host_error
1351+ def dns_error (self , host , port ):
13531352 # The full error should be something like:
13541353 # "[Errno 8] nodename nor servname provided, or not known"
1355- try :
1356- socket .getaddrinfo ('doesnotexist.local' , 5698 , socket .AF_INET ,
1357- socket .SOCK_STREAM )
1358- except Exception as exc :
1359- self ._kmip_host_error = re .escape (str (exc ))
1360- return self ._kmip_host_error
1354+ with self .assertRaises (Exception ) as ctx :
1355+ socket .getaddrinfo (host , port , socket .AF_INET , socket .SOCK_STREAM )
1356+ return re .escape (str (ctx .exception ))
1357+
1358+ @property
1359+ def invalid_host_error (self ):
1360+ if self ._invalid_host_error is None :
1361+ self ._invalid_host_error = self .dns_error (
1362+ 'doesnotexist.invalid' , 443 )
1363+ return self ._invalid_host_error
1364+
1365+ @property
1366+ def kmip_host_error (self ):
1367+ if self ._kmip_host_error is None :
1368+ self ._kmip_host_error = self .dns_error ('doesnotexist.local' , 5698 )
1369+ return self ._kmip_host_error
13611370
13621371 def test_10_kmip_invalid_endpoint (self ):
13631372 key = {'keyId' : '1' }
13641373 self .run_test_expected_success ('kmip' , key )
1365- with self .assertRaisesRegex (EncryptionError , self .kmip_host_error () ):
1374+ with self .assertRaisesRegex (EncryptionError , self .kmip_host_error ):
13661375 self .client_encryption_invalid .create_data_key ('kmip' , key )
13671376
13681377 def test_11_kmip_master_key_endpoint (self ):
@@ -1379,7 +1388,7 @@ def test_11_kmip_master_key_endpoint(self):
13791388
13801389 def test_12_kmip_master_key_invalid_endpoint (self ):
13811390 key = {'keyId' : '1' , 'endpoint' : 'doesnotexist.local:5698' }
1382- with self .assertRaisesRegex (EncryptionError , self .kmip_host_error () ):
1391+ with self .assertRaisesRegex (EncryptionError , self .kmip_host_error ):
13831392 self .client_encryption .create_data_key ('kmip' , key )
13841393
13851394
0 commit comments