diff --git a/airflow/providers/google/cloud/utils/credentials_provider.py b/airflow/providers/google/cloud/utils/credentials_provider.py index 0af03d24595..071d19fec34 100644 --- a/airflow/providers/google/cloud/utils/credentials_provider.py +++ b/airflow/providers/google/cloud/utils/credentials_provider.py @@ -29,8 +29,10 @@ import google.auth import google.auth.credentials import google.oauth2.service_account -from google.auth import impersonated_credentials +from google.auth import compute_engine, impersonated_credentials from google.auth.environment_vars import CREDENTIALS, LEGACY_PROJECT, PROJECT +from google.auth.exceptions import RefreshError +from google.auth.transport import _http_client from airflow.exceptions import AirflowException from airflow.providers.google.cloud._internal_client.secret_manager_client import _SecretManagerClient @@ -248,6 +250,16 @@ def get_credentials_and_project(self) -> Tuple[google.auth.credentials.Credentia project_id = _get_project_id_from_service_account_email(self.target_principal) + if isinstance(credentials, compute_engine.Credentials): + try: + credentials.refresh(_http_client.Request()) + except RefreshError as msg: + """ + If the Compute Engine metadata service can't be reached in this case the instance has not + credentials. + """ + self._log_debug(msg) + return credentials, project_id def _get_credentials_using_keyfile_dict(self): diff --git a/dist/apache-airflow-providers-google-2022.5.18+composer.tar.gz b/dist/apache-airflow-providers-google-2022.5.18+composer.tar.gz index b7f4003e50b..dcc407e2428 100644 Binary files a/dist/apache-airflow-providers-google-2022.5.18+composer.tar.gz and b/dist/apache-airflow-providers-google-2022.5.18+composer.tar.gz differ diff --git a/dist/apache_airflow_providers_google-2022.5.18+composer-py3-none-any.whl b/dist/apache_airflow_providers_google-2022.5.18+composer-py3-none-any.whl index 6663fab2ebd..2f25fbbc35a 100644 Binary files a/dist/apache_airflow_providers_google-2022.5.18+composer-py3-none-any.whl and b/dist/apache_airflow_providers_google-2022.5.18+composer-py3-none-any.whl differ