Handle bad XSRF token in exception handler

PR 2907548 shows the need of implementing also a regeneration trigger
in the exception handler to help with recovering from
invalid XSRF Token issue.

Change-Id: I51897596259bf6abcee26b148c5b70c5eb02d459
(cherry picked from commit 8a5b39e90b)
This commit is contained in:
Xiaotong Luo 2022-03-07 14:36:58 -08:00 committed by Salvatore Orlando
parent 962c73b21d
commit d57a84f063
2 changed files with 30 additions and 1 deletions

View File

@ -436,6 +436,25 @@ class ClusteredAPITestCase(nsxlib_testcase.NsxClientTestCase):
api.get, 'api/v1/transport-zones')
self.assertEqual(cluster.ClusterHealth.GREEN, api.health)
def test_xsrf_token_error(self):
def server_error():
return mocks.MockRequestsResponse(
codes.FORBIDDEN,
jsonutils.dumps({'error_message': 'test', 'error_code': 98}))
conf_managers = ['8.9.10.11', '9.10.11.12', '10.11.12.13']
exceptions = config.ExceptionConfig()
max_attempts = 2
api = self.mock_nsx_clustered_api(
nsx_api_managers=conf_managers,
max_attempts=max_attempts,
session_response=[server_error for i in range(0, max_attempts)])
api.nsxlib_config.exception_config = exceptions
self.assertRaises(nsxlib_exc.BadXSRFToken,
api.get, 'api/v1/transport-zones')
self.assertEqual(cluster.ClusterHealth.GREEN, api.health)
def test_exception_translation_on_ground_error(self):
def server_error():

View File

@ -825,7 +825,17 @@ class ClusteredAPI(object):
# consider endpoint inaccessible and move to next
# endpoint
endpoint.set_state(EndpointState.DOWN)
elif exc_config.should_regenerate(e):
LOG.warning("[%x] Request failed due to an exception "
"that calls for regeneration. "
"Re-generating pool.", id(conn))
if bool(self.nsxlib_config.token_provider):
# get new jwt token for authentication
self.nsxlib_config.token_provider.get_token(
refresh_token=True)
# refresh endpoint with new headers
# that have updated token
endpoint.regenerate_pool()
elif not exc_config.should_retry(e):
LOG.info("Exception %s is configured as not retriable",
e)