@@ -1174,16 +1174,17 @@ def setUp(self):
11741174 kms_tls_options = KMS_TLS_OPTS )
11751175
11761176 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'
11791179 kms_providers_invalid ['kmip' ]['endpoint' ] = 'doesnotexist.local:5698'
11801180 self .client_encryption_invalid = ClientEncryption (
11811181 kms_providers = kms_providers_invalid ,
11821182 key_vault_namespace = 'keyvault.datakeys' ,
11831183 key_vault_client = client_context .client ,
11841184 codec_options = OPTS ,
11851185 kms_tls_options = KMS_TLS_OPTS )
1186- self ._kmip_host_error = ''
1186+ self ._kmip_host_error = None
1187+ self ._invalid_host_error = None
11871188
11881189 def tearDown (self ):
11891190 self .client_encryption .close ()
@@ -1264,9 +1265,9 @@ def test_06_aws_endpoint_invalid_host(self):
12641265 "region" : "us-east-1" ,
12651266 "key" : ("arn:aws:kms:us-east-1:579766882180:key/"
12661267 "89fcc2c4-08b0-4bd9-9f25-e30687b580d0" ),
1267- "endpoint" : "example.com "
1268+ "endpoint" : "doesnotexist.invalid "
12681269 }
1269- with self .assertRaisesRegex (EncryptionError , 'parse error' ):
1270+ with self .assertRaisesRegex (EncryptionError , self . invalid_host_error ):
12701271 self .client_encryption .create_data_key (
12711272 'aws' , master_key = master_key )
12721273
@@ -1278,8 +1279,8 @@ def test_07_azure(self):
12781279 self .run_test_expected_success ('azure' , master_key )
12791280
12801281 # 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 ):
12831284 self .client_encryption_invalid .create_data_key (
12841285 'azure' , master_key = master_key )
12851286
@@ -1295,8 +1296,8 @@ def test_08_gcp_valid_endpoint(self):
12951296 self .run_test_expected_success ('gcp' , master_key )
12961297
12971298 # 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 ):
13001301 self .client_encryption_invalid .create_data_key (
13011302 'gcp' , master_key = master_key )
13021303
@@ -1308,30 +1309,38 @@ def test_09_gcp_invalid_endpoint(self):
13081309 "location" : "global" ,
13091310 "keyRing" : "key-ring-csfle" ,
13101311 "keyName" : "key-name-csfle" ,
1311- "endpoint" : "example.com :443" }
1312+ "endpoint" : "doesnotexist.invalid :443" }
13121313
13131314 # The full error should be something like:
13141315 # "Invalid KMS response, no access_token returned. HTTP status=200"
13151316 with self .assertRaisesRegex (EncryptionError , "Invalid KMS response" ):
13161317 self .client_encryption .create_data_key (
13171318 'gcp' , master_key = master_key )
13181319
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 ):
13221321 # The full error should be something like:
13231322 # "[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
13301339
13311340 def test_10_kmip_invalid_endpoint (self ):
13321341 key = {'keyId' : '1' }
13331342 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 ):
13351344 self .client_encryption_invalid .create_data_key ('kmip' , key )
13361345
13371346 def test_11_kmip_master_key_endpoint (self ):
@@ -1348,7 +1357,7 @@ def test_11_kmip_master_key_endpoint(self):
13481357
13491358 def test_12_kmip_master_key_invalid_endpoint (self ):
13501359 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 ):
13521361 self .client_encryption .create_data_key ('kmip' , key )
13531362
13541363
0 commit comments