Skip to content

Commit

Permalink
Raise exception when GCP credential doesn't support account impersona…
Browse files Browse the repository at this point in the history
…tion (#8213)
  • Loading branch information
iamshwin authored Apr 14, 2020
1 parent eee4eba commit 2636cc9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
9 changes: 8 additions & 1 deletion airflow/providers/google/cloud/utils/credentials_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,14 @@ def get_credentials_and_project_id(
project_id = credentials.project_id

if delegate_to:
credentials = credentials.with_subject(delegate_to)
if hasattr(credentials, 'with_subject'):
credentials = credentials.with_subject(delegate_to)
else:
raise AirflowException(
"The `delegate_to` parameter cannot be used here as the current "
"authentication method does not support account impersonate. "
"Please use service-account for authorization."
)

return credentials, project_id

Expand Down
14 changes: 14 additions & 0 deletions tests/providers/google/common/hooks/test_base_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,20 @@ def test_get_credentials_and_project_id_with_default_auth_and_delegate(
)
self.assertEqual((mock_credentials, "PROJECT_ID"), result)

@mock.patch('google.auth.default')
def test_get_credentials_and_project_id_with_default_auth_and_unsupported_delegate(
self, mock_auth_default
):
self.instance.delegate_to = "TEST_DELLEGATE_TO"
mock_credentials = mock.MagicMock(spec=google.auth.compute_engine.Credentials)
mock_auth_default.return_value = (mock_credentials, "PROJECT_ID")

with self.assertRaisesRegex(AirflowException, re.escape(
"The `delegate_to` parameter cannot be used here as the current authentication method does not "
"support account impersonate. Please use service-account for authorization."
)):
self.instance._get_credentials_and_project_id()

@mock.patch( # type: ignore
MODULE_NAME + '.get_credentials_and_project_id',
return_value=("CREDENTIALS", "PROJECT_ID")
Expand Down

0 comments on commit 2636cc9

Please sign in to comment.
  翻译: