From 58000735cc9ddf4f6b9615213678263f9bc7be26 Mon Sep 17 00:00:00 2001 From: adoneria Date: Fri, 6 Sep 2019 15:59:20 -0700 Subject: [PATCH 1/2] Add retry loop for fetching authentication token if any 'Internal failure' occurs This is to prevent errors while client tries to connect with server and fails with 'Internal Failure' because it was unable to fetch OAuth2 token. --- docs/reference/google.auth.app_engine.rst | 10 +++---- ...google.auth.compute_engine.credentials.rst | 10 +++---- docs/reference/google.auth.compute_engine.rst | 11 ++++---- docs/reference/google.auth.credentials.rst | 6 ++--- docs/reference/google.auth.crypt.rst | 7 +++-- .../google.auth.environment_vars.rst | 10 +++---- docs/reference/google.auth.exceptions.rst | 6 ++--- docs/reference/google.auth.iam.rst | 6 ++--- .../google.auth.impersonated_credentials.rst | 6 ++--- docs/reference/google.auth.jwt.rst | 6 ++--- docs/reference/google.auth.rst | 13 +++++----- docs/reference/google.auth.transport.grpc.rst | 6 ++--- .../google.auth.transport.requests.rst | 6 ++--- docs/reference/google.auth.transport.rst | 7 +++-- .../google.auth.transport.urllib3.rst | 6 ++--- docs/reference/google.oauth2.credentials.rst | 6 ++--- docs/reference/google.oauth2.id_token.rst | 10 +++---- docs/reference/google.oauth2.rst | 7 +++-- .../google.oauth2.service_account.rst | 10 +++---- docs/reference/google.rst | 11 ++++---- google/oauth2/_client.py | 26 ++++++++++++++----- tests/oauth2/test__client.py | 12 +++++++++ 22 files changed, 108 insertions(+), 90 deletions(-) diff --git a/docs/reference/google.auth.app_engine.rst b/docs/reference/google.auth.app_engine.rst index 4525c089e..2142b6f50 100644 --- a/docs/reference/google.auth.app_engine.rst +++ b/docs/reference/google.auth.app_engine.rst @@ -1,7 +1,7 @@ -google.auth.app_engine module -============================= +google.auth.app\_engine module +============================== .. automodule:: google.auth.app_engine - :members: - :inherited-members: - :show-inheritance: + :members: + :inherited-members: + :show-inheritance: diff --git a/docs/reference/google.auth.compute_engine.credentials.rst b/docs/reference/google.auth.compute_engine.credentials.rst index ebe62c07c..782d95f20 100644 --- a/docs/reference/google.auth.compute_engine.credentials.rst +++ b/docs/reference/google.auth.compute_engine.credentials.rst @@ -1,7 +1,7 @@ -google.auth.compute_engine.credentials module -============================================= +google.auth.compute\_engine.credentials module +============================================== .. automodule:: google.auth.compute_engine.credentials - :members: - :inherited-members: - :show-inheritance: + :members: + :inherited-members: + :show-inheritance: diff --git a/docs/reference/google.auth.compute_engine.rst b/docs/reference/google.auth.compute_engine.rst index 2e7b830db..38785c803 100644 --- a/docs/reference/google.auth.compute_engine.rst +++ b/docs/reference/google.auth.compute_engine.rst @@ -1,10 +1,10 @@ -google.auth.compute_engine package -================================== +google.auth.compute\_engine package +=================================== .. automodule:: google.auth.compute_engine - :members: - :inherited-members: - :show-inheritance: + :members: + :inherited-members: + :show-inheritance: Submodules ---------- @@ -12,4 +12,3 @@ Submodules .. toctree:: google.auth.compute_engine.credentials - diff --git a/docs/reference/google.auth.credentials.rst b/docs/reference/google.auth.credentials.rst index b710a8c8c..18d1d8cdf 100644 --- a/docs/reference/google.auth.credentials.rst +++ b/docs/reference/google.auth.credentials.rst @@ -2,6 +2,6 @@ google.auth.credentials module ============================== .. automodule:: google.auth.credentials - :members: - :inherited-members: - :show-inheritance: + :members: + :inherited-members: + :show-inheritance: diff --git a/docs/reference/google.auth.crypt.rst b/docs/reference/google.auth.crypt.rst index a3e2b1206..0833e7f2f 100644 --- a/docs/reference/google.auth.crypt.rst +++ b/docs/reference/google.auth.crypt.rst @@ -2,9 +2,9 @@ google.auth.crypt package ========================= .. automodule:: google.auth.crypt - :members: - :inherited-members: - :show-inheritance: + :members: + :inherited-members: + :show-inheritance: Submodules ---------- @@ -13,4 +13,3 @@ Submodules google.auth.crypt.base google.auth.crypt.rsa - diff --git a/docs/reference/google.auth.environment_vars.rst b/docs/reference/google.auth.environment_vars.rst index fe34849dd..5996e9935 100644 --- a/docs/reference/google.auth.environment_vars.rst +++ b/docs/reference/google.auth.environment_vars.rst @@ -1,7 +1,7 @@ -google.auth.environment_vars module -=================================== +google.auth.environment\_vars module +==================================== .. automodule:: google.auth.environment_vars - :members: - :inherited-members: - :show-inheritance: + :members: + :inherited-members: + :show-inheritance: diff --git a/docs/reference/google.auth.exceptions.rst b/docs/reference/google.auth.exceptions.rst index d603eaf89..c87a7f2af 100644 --- a/docs/reference/google.auth.exceptions.rst +++ b/docs/reference/google.auth.exceptions.rst @@ -2,6 +2,6 @@ google.auth.exceptions module ============================= .. automodule:: google.auth.exceptions - :members: - :inherited-members: - :show-inheritance: + :members: + :inherited-members: + :show-inheritance: diff --git a/docs/reference/google.auth.iam.rst b/docs/reference/google.auth.iam.rst index 8a5edb450..8472ed731 100644 --- a/docs/reference/google.auth.iam.rst +++ b/docs/reference/google.auth.iam.rst @@ -2,6 +2,6 @@ google.auth.iam module ====================== .. automodule:: google.auth.iam - :members: - :inherited-members: - :show-inheritance: + :members: + :inherited-members: + :show-inheritance: diff --git a/docs/reference/google.auth.impersonated_credentials.rst b/docs/reference/google.auth.impersonated_credentials.rst index 653708ef7..f139ccf4f 100644 --- a/docs/reference/google.auth.impersonated_credentials.rst +++ b/docs/reference/google.auth.impersonated_credentials.rst @@ -2,6 +2,6 @@ google.auth.impersonated\_credentials module ============================================ .. automodule:: google.auth.impersonated_credentials - :members: - :inherited-members: - :show-inheritance: + :members: + :inherited-members: + :show-inheritance: diff --git a/docs/reference/google.auth.jwt.rst b/docs/reference/google.auth.jwt.rst index c58be5fbd..c7c2fdf62 100644 --- a/docs/reference/google.auth.jwt.rst +++ b/docs/reference/google.auth.jwt.rst @@ -2,6 +2,6 @@ google.auth.jwt module ====================== .. automodule:: google.auth.jwt - :members: - :inherited-members: - :show-inheritance: + :members: + :inherited-members: + :show-inheritance: diff --git a/docs/reference/google.auth.rst b/docs/reference/google.auth.rst index 53ab699c6..f6ea073c5 100644 --- a/docs/reference/google.auth.rst +++ b/docs/reference/google.auth.rst @@ -2,18 +2,18 @@ google.auth package =================== .. automodule:: google.auth - :members: - :inherited-members: - :show-inheritance: + :members: + :inherited-members: + :show-inheritance: Subpackages ----------- .. toctree:: - google.auth.compute_engine - google.auth.crypt - google.auth.transport + google.auth.compute_engine + google.auth.crypt + google.auth.transport Submodules ---------- @@ -27,4 +27,3 @@ Submodules google.auth.iam google.auth.impersonated_credentials google.auth.jwt - diff --git a/docs/reference/google.auth.transport.grpc.rst b/docs/reference/google.auth.transport.grpc.rst index e16d1d853..f9f344295 100644 --- a/docs/reference/google.auth.transport.grpc.rst +++ b/docs/reference/google.auth.transport.grpc.rst @@ -2,6 +2,6 @@ google.auth.transport.grpc module ================================= .. automodule:: google.auth.transport.grpc - :members: - :inherited-members: - :show-inheritance: + :members: + :inherited-members: + :show-inheritance: diff --git a/docs/reference/google.auth.transport.requests.rst b/docs/reference/google.auth.transport.requests.rst index f31830c56..5f0c23c2d 100644 --- a/docs/reference/google.auth.transport.requests.rst +++ b/docs/reference/google.auth.transport.requests.rst @@ -2,6 +2,6 @@ google.auth.transport.requests module ===================================== .. automodule:: google.auth.transport.requests - :members: - :inherited-members: - :show-inheritance: + :members: + :inherited-members: + :show-inheritance: diff --git a/docs/reference/google.auth.transport.rst b/docs/reference/google.auth.transport.rst index 1f802c3b5..48e2e0551 100644 --- a/docs/reference/google.auth.transport.rst +++ b/docs/reference/google.auth.transport.rst @@ -2,9 +2,9 @@ google.auth.transport package ============================= .. automodule:: google.auth.transport - :members: - :inherited-members: - :show-inheritance: + :members: + :inherited-members: + :show-inheritance: Submodules ---------- @@ -14,4 +14,3 @@ Submodules google.auth.transport.grpc google.auth.transport.requests google.auth.transport.urllib3 - diff --git a/docs/reference/google.auth.transport.urllib3.rst b/docs/reference/google.auth.transport.urllib3.rst index 339c77ead..667bb0978 100644 --- a/docs/reference/google.auth.transport.urllib3.rst +++ b/docs/reference/google.auth.transport.urllib3.rst @@ -2,6 +2,6 @@ google.auth.transport.urllib3 module ==================================== .. automodule:: google.auth.transport.urllib3 - :members: - :inherited-members: - :show-inheritance: + :members: + :inherited-members: + :show-inheritance: diff --git a/docs/reference/google.oauth2.credentials.rst b/docs/reference/google.oauth2.credentials.rst index ca978d148..d3bdc16de 100644 --- a/docs/reference/google.oauth2.credentials.rst +++ b/docs/reference/google.oauth2.credentials.rst @@ -2,6 +2,6 @@ google.oauth2.credentials module ================================ .. automodule:: google.oauth2.credentials - :members: - :inherited-members: - :show-inheritance: + :members: + :inherited-members: + :show-inheritance: diff --git a/docs/reference/google.oauth2.id_token.rst b/docs/reference/google.oauth2.id_token.rst index db38b6085..fbe6eab8d 100644 --- a/docs/reference/google.oauth2.id_token.rst +++ b/docs/reference/google.oauth2.id_token.rst @@ -1,7 +1,7 @@ -google.oauth2.id_token module -============================= +google.oauth2.id\_token module +============================== .. automodule:: google.oauth2.id_token - :members: - :inherited-members: - :show-inheritance: + :members: + :inherited-members: + :show-inheritance: diff --git a/docs/reference/google.oauth2.rst b/docs/reference/google.oauth2.rst index adb9403ef..4f1df071f 100644 --- a/docs/reference/google.oauth2.rst +++ b/docs/reference/google.oauth2.rst @@ -2,9 +2,9 @@ google.oauth2 package ===================== .. automodule:: google.oauth2 - :members: - :inherited-members: - :show-inheritance: + :members: + :inherited-members: + :show-inheritance: Submodules ---------- @@ -14,4 +14,3 @@ Submodules google.oauth2.credentials google.oauth2.id_token google.oauth2.service_account - diff --git a/docs/reference/google.oauth2.service_account.rst b/docs/reference/google.oauth2.service_account.rst index cc4e43899..8d8fcd3f1 100644 --- a/docs/reference/google.oauth2.service_account.rst +++ b/docs/reference/google.oauth2.service_account.rst @@ -1,7 +1,7 @@ -google.oauth2.service_account module -==================================== +google.oauth2.service\_account module +===================================== .. automodule:: google.oauth2.service_account - :members: - :inherited-members: - :show-inheritance: + :members: + :inherited-members: + :show-inheritance: diff --git a/docs/reference/google.rst b/docs/reference/google.rst index fc63d1fdb..4b1e08537 100644 --- a/docs/reference/google.rst +++ b/docs/reference/google.rst @@ -2,15 +2,14 @@ google package ============== .. automodule:: google - :members: - :inherited-members: - :show-inheritance: + :members: + :inherited-members: + :show-inheritance: Subpackages ----------- .. toctree:: - google.auth - google.oauth2 - + google.auth + google.oauth2 diff --git a/google/oauth2/_client.py b/google/oauth2/_client.py index 5121a3274..d37502c61 100644 --- a/google/oauth2/_client.py +++ b/google/oauth2/_client.py @@ -102,13 +102,25 @@ def _token_endpoint_request(request, token_uri, body): 'content-type': _URLENCODED_CONTENT_TYPE, } - response = request( - method='POST', url=token_uri, headers=headers, body=body) - - response_body = response.data.decode('utf-8') - - if response.status != http_client.OK: - _handle_error_response(response_body) + retry = 0 + + # retry to fetch token for maximum of two times if any internal failure + # occurs. + while True: + response = request( + method='POST', url=token_uri, headers=headers, body=body) + + response_body = response.data.decode('utf-8') + + if response.status == http_client.OK: + break + else: + error_desc = json.loads( + response_body).get('error_description') or '' + if error_desc == 'internal_failure' and retry < 1: + retry += 1 + continue + _handle_error_response(response_body) response_data = json.loads(response_body) diff --git a/tests/oauth2/test__client.py b/tests/oauth2/test__client.py index 5a4a56745..6fc4c3b12 100644 --- a/tests/oauth2/test__client.py +++ b/tests/oauth2/test__client.py @@ -106,6 +106,18 @@ def test__token_endpoint_request_error(): _client._token_endpoint_request(request, 'http://example.com', {}) +def test__token_endpoint_request_internal_failure_error(): + request = make_request({'error': 'internal_failure', + 'error_description': 'internal_failure'}, + status=http_client.BAD_REQUEST) + + with pytest.raises(exceptions.RefreshError): + _client._token_endpoint_request( + request, 'http://example.com', + {'error': 'internal_failure', + 'error_description': 'internal_failure'}) + + def verify_request_params(request, params): request_body = request.call_args[1]['body'] request_params = urllib.parse.parse_qs(request_body) From b6436ad6c178eba6a2f6444e8fdeb4f143694f8d Mon Sep 17 00:00:00 2001 From: adoneria Date: Fri, 6 Sep 2019 15:59:20 -0700 Subject: [PATCH 2/2] Add retry loop for fetching authentication token if any 'Internal failure' occurs This is to prevent errors while client tries to connect with server and fails with 'Internal Failure' because it was unable to fetch OAuth2 token. --- google/oauth2/_client.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/google/oauth2/_client.py b/google/oauth2/_client.py index d37502c61..eac01b7ec 100644 --- a/google/oauth2/_client.py +++ b/google/oauth2/_client.py @@ -103,13 +103,11 @@ def _token_endpoint_request(request, token_uri, body): } retry = 0 - # retry to fetch token for maximum of two times if any internal failure # occurs. while True: response = request( method='POST', url=token_uri, headers=headers, body=body) - response_body = response.data.decode('utf-8') if response.status == http_client.OK: