diff --git a/airflow/providers/google/cloud/hooks/base.py b/airflow/providers/google/cloud/hooks/base.py index 91ca0354a7b8b..d1132c9b702bf 100644 --- a/airflow/providers/google/cloud/hooks/base.py +++ b/airflow/providers/google/cloud/hooks/base.py @@ -141,11 +141,16 @@ def __init__(self, gcp_conn_id: str = 'google_cloud_default', delegate_to: Optio self.gcp_conn_id = gcp_conn_id self.delegate_to = delegate_to self.extras = self.get_connection(self.gcp_conn_id).extra_dejson # type: Dict + self._cached_credentials: Optional[google.auth.credentials.Credentials] = None + self._cached_project_id: Optional[str] = None def _get_credentials_and_project_id(self) -> Tuple[google.auth.credentials.Credentials, Optional[str]]: """ Returns the Credentials object for Google API and the associated project_id """ + if self._cached_credentials is not None: + return self._cached_credentials, self._cached_project_id + key_path = self._get_field('key_path', None) # type: Optional[str] keyfile_dict = self._get_field('keyfile_dict', None) # type: Optional[str] if key_path and keyfile_dict: @@ -200,6 +205,9 @@ def _get_credentials_and_project_id(self) -> Tuple[google.auth.credentials.Crede if overridden_project_id: project_id = overridden_project_id + self._cached_credentials = credentials + self._cached_project_id = project_id + return credentials, project_id def _get_credentials(self) -> google.auth.credentials.Credentials: