diff --git a/airflow/providers/google/common/hooks/base_google.py b/airflow/providers/google/common/hooks/base_google.py index a149831cab5..0e2c6b17ad5 100644 --- a/airflow/providers/google/common/hooks/base_google.py +++ b/airflow/providers/google/common/hooks/base_google.py @@ -515,9 +515,6 @@ def provide_authorized_gcloud(self): f"--key-file={os.environ[CREDENTIALS]}", ] ) - if project_id: - # Don't display stdout/stderr for security reason - check_output(["gcloud", "config", "set", "core/project", project_id]) elif os.path.exists(credentials_path): # If we are logged in by `gcloud auth application-default` then we need to log in manually. # This will make the `gcloud auth application-default` and `gcloud auth` credentials equals. @@ -539,6 +536,11 @@ def provide_authorized_gcloud(self): creds_content["refresh_token"], ] ) + + if project_id: + # Don't display stdout/stderr for security reason + check_output(["gcloud", "config", "set", "core/project", project_id]) + yield @staticmethod diff --git a/tests/providers/google/common/hooks/test_base_google.py b/tests/providers/google/common/hooks/test_base_google.py index fd4cd613bb3..93b3b2d2bfd 100644 --- a/tests/providers/google/common/hooks/test_base_google.py +++ b/tests/providers/google/common/hooks/test_base_google.py @@ -742,12 +742,12 @@ def test_provide_authorized_gcloud_via_gcloud_application_default( # Do nothing pass - mock_check_output.has_calls( + mock_check_output.assert_has_calls( [ mock.call(['gcloud', 'config', 'set', 'auth/client_id', 'CLIENT_ID']), mock.call(['gcloud', 'config', 'set', 'auth/client_secret', 'CLIENT_SECRET']), - mock.call(['gcloud', 'config', 'set', 'core/project', 'PROJECT_ID']), mock.call(['gcloud', 'auth', 'activate-refresh-token', 'CLIENT_ID', 'REFRESH_TOKEN']), + mock.call(['gcloud', 'config', 'set', 'core/project', 'PROJECT_ID']), ], any_order=False, )