Skip to content

Commit 2636cc9

Browse files
authored
Raise exception when GCP credential doesn't support account impersonation (#8213)
1 parent eee4eba commit 2636cc9

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

airflow/providers/google/cloud/utils/credentials_provider.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,14 @@ def get_credentials_and_project_id(
241241
project_id = credentials.project_id
242242

243243
if delegate_to:
244-
credentials = credentials.with_subject(delegate_to)
244+
if hasattr(credentials, 'with_subject'):
245+
credentials = credentials.with_subject(delegate_to)
246+
else:
247+
raise AirflowException(
248+
"The `delegate_to` parameter cannot be used here as the current "
249+
"authentication method does not support account impersonate. "
250+
"Please use service-account for authorization."
251+
)
245252

246253
return credentials, project_id
247254

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,20 @@ def test_get_credentials_and_project_id_with_default_auth_and_delegate(
421421
)
422422
self.assertEqual((mock_credentials, "PROJECT_ID"), result)
423423

424+
@mock.patch('google.auth.default')
425+
def test_get_credentials_and_project_id_with_default_auth_and_unsupported_delegate(
426+
self, mock_auth_default
427+
):
428+
self.instance.delegate_to = "TEST_DELLEGATE_TO"
429+
mock_credentials = mock.MagicMock(spec=google.auth.compute_engine.Credentials)
430+
mock_auth_default.return_value = (mock_credentials, "PROJECT_ID")
431+
432+
with self.assertRaisesRegex(AirflowException, re.escape(
433+
"The `delegate_to` parameter cannot be used here as the current authentication method does not "
434+
"support account impersonate. Please use service-account for authorization."
435+
)):
436+
self.instance._get_credentials_and_project_id()
437+
424438
@mock.patch( # type: ignore
425439
MODULE_NAME + '.get_credentials_and_project_id',
426440
return_value=("CREDENTIALS", "PROJECT_ID")

0 commit comments

Comments
 (0)