Add token expired status code into regenerate triggers

Issue: #2561101
Change-Id: Id8c04b64a138a18ed06fada54711296188687fa3
This commit is contained in:
Yang Ding 2020-05-05 17:53:50 -07:00
parent 917ac30039
commit 09bcdaa164
3 changed files with 22 additions and 20 deletions

View File

@ -409,7 +409,7 @@ class ClusteredAPITestCase(nsxlib_testcase.NsxClientTestCase):
# This exception does not ground endpoint
self.assertEqual(cluster.ClusterHealth.GREEN, api.health)
def test_non_retriable_error(self):
def test_regenerate_error(self):
def server_error():
return mocks.MockRequestsResponse(
@ -425,7 +425,8 @@ class ClusteredAPITestCase(nsxlib_testcase.NsxClientTestCase):
session_response=[server_error for i in range(0, max_attempts)])
api.nsxlib_config.exception_config = exceptions
api.get('api/v1/transport-zones')
self.assertRaises(nsxlib_exc.ClientCertificateNotTrusted,
api.get, 'api/v1/transport-zones')
self.assertEqual(cluster.ClusterHealth.GREEN, api.health)
def test_exception_translation_on_ground_error(self):

View File

@ -549,20 +549,19 @@ class ClusteredAPI(object):
endpoint,
conn)
endpoint.set_state(EndpointState.UP)
except exceptions.ClientCertificateNotTrusted:
LOG.warning("Failed to validate API cluster endpoint "
"'%(ep)s' due to untrusted client certificate",
{'ep': endpoint})
# regenerate connection pool based on new certificate
endpoint.regenerate_pool()
except exceptions.BadXSRFToken:
LOG.warning("Failed to validate API cluster endpoint "
"'%(ep)s' due to expired XSRF token",
{'ep': endpoint})
# regenerate connection pool based on token
endpoint.regenerate_pool()
except Exception as e:
if self.nsxlib_config.exception_config.should_retry(e):
if self.nsxlib_config.exception_config.should_regenerate(e):
LOG.warning("Failed to validate API cluster endpoint "
"'%(ep)s' due to an exception that calls for "
"regeneration. Re-generating pool.",
{'ep': endpoint})
if bool(self.nsxlib_config.token_provider):
# get new jwt token for authentication
self.nsxlib_config.token_provider.get_token(refresh=True)
# refresh endpoint with new headers that have updated token
endpoint.regenerate_pool()
return
elif self.nsxlib_config.exception_config.should_retry(e):
LOG.info("Exception is retriable, endpoint stays UP")
endpoint.set_state(EndpointState.UP)
else:
@ -657,10 +656,10 @@ class ClusteredAPI(object):
# This exception is irrelevant for endpoint decisions
return
if (self.nsxlib_config.exception_config.should_regenerate(exc) and
bool(self.nsxlib_config.token_provider)):
# get new jwt token for authentication
self.nsxlib_config.token_provider.get_token(refresh=True)
if self.nsxlib_config.exception_config.should_regenerate(exc):
if bool(self.nsxlib_config.token_provider):
# get new jwt token for authentication
self.nsxlib_config.token_provider.get_token(refresh=True)
# refresh endpoint so that it gets new header with updated token
endpoint.regenerate_pool()
raise exc

View File

@ -42,7 +42,9 @@ class ExceptionConfig(object):
# When hit during API call, these exceptions will be retried
# after the endpoints are regenerated with up-to-date auth
# credentials / tokens
self.regenerate_triggers = [v3_exceptions.InvalidCredentials]
self.regenerate_triggers = [v3_exceptions.InvalidCredentials,
v3_exceptions.ClientCertificateNotTrusted,
v3_exceptions.BadXSRFToken]
def should_ground_endpoint(self, ex):
for exception in self.ground_triggers: