Skip to content

Commit e1e833b

Browse files
authored
Update GoogleBaseHook to not follow 308 and use 60s timeout (#8816)
1 parent 7d69987 commit e1e833b

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

airflow/providers/google/common/hooks/base_google.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,13 @@
3232
import google.auth.credentials
3333
import google.oauth2.service_account
3434
import google_auth_httplib2
35-
import httplib2
3635
import tenacity
3736
from google.api_core.exceptions import Forbidden, ResourceExhausted, TooManyRequests
3837
from google.api_core.gapic_v1.client_info import ClientInfo
3938
from google.auth import _cloud_sdk
4039
from google.auth.environment_vars import CREDENTIALS
4140
from googleapiclient.errors import HttpError
42-
from googleapiclient.http import MediaIoBaseDownload, set_user_agent
41+
from googleapiclient.http import MediaIoBaseDownload, build_http, set_user_agent
4342

4443
from airflow import version
4544
from airflow.exceptions import AirflowException
@@ -213,7 +212,7 @@ def _authorize(self) -> google_auth_httplib2.AuthorizedHttp:
213212
service hook connection.
214213
"""
215214
credentials = self._get_credentials()
216-
http = httplib2.Http()
215+
http = build_http()
217216
http = set_user_agent(http, "airflow/" + version.version)
218217
authed_http = google_auth_httplib2.AuthorizedHttp(credentials, http=http)
219218
return authed_http

tests/providers/google/common/hooks/test_base_google.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ def test_num_retries_is_not_none_by_default(self, get_con_mock):
591591
}
592592
self.assertEqual(self.instance.num_retries, 5)
593593

594-
@mock.patch("airflow.providers.google.common.hooks.base_google.httplib2.Http")
594+
@mock.patch("airflow.providers.google.common.hooks.base_google.build_http")
595595
@mock.patch("airflow.providers.google.common.hooks.base_google.GoogleBaseHook._get_credentials")
596596
def test_authorize_assert_user_agent_is_sent(self, mock_get_credentials, mock_http):
597597
"""
@@ -616,6 +616,22 @@ def test_authorize_assert_user_agent_is_sent(self, mock_get_credentials, mock_ht
616616
self.assertEqual(response, new_response)
617617
self.assertEqual(content, new_content)
618618

619+
@mock.patch("airflow.providers.google.common.hooks.base_google.GoogleBaseHook._get_credentials")
620+
def test_authorize_assert_http_308_is_excluded(self, mock_get_credentials):
621+
"""
622+
Verify that 308 status code is excluded from httplib2's redirect codes
623+
"""
624+
http_authorized = self.instance._authorize().http
625+
self.assertTrue(308 not in http_authorized.redirect_codes)
626+
627+
@mock.patch("airflow.providers.google.common.hooks.base_google.GoogleBaseHook._get_credentials")
628+
def test_authorize_assert_http_timeout_is_present(self, mock_get_credentials):
629+
"""
630+
Verify that http client has a timeout set
631+
"""
632+
http_authorized = self.instance._authorize().http
633+
self.assertNotEqual(http_authorized.timeout, None)
634+
619635

620636
class TestProvideAuthorizedGcloud(unittest.TestCase):
621637
def setUp(self):

0 commit comments

Comments
 (0)