-
Notifications
You must be signed in to change notification settings - Fork 314
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ADC can load an impersonated service account credentials. (#962)
* Make the impersonated credentials to be Scoped so default func will add scopes. * Add tests for impersonated service account credentials. Co-authored-by: Anthonios Partheniou <[email protected]>
- Loading branch information
1 parent
83b20f0
commit 52c8ef9
Showing
3 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1089,6 +1089,60 @@ def test_default_no_warning_with_quota_project_id_for_user_creds(get_adc_path): | |
credentials, project_id = _default.default(quota_project_id="project-foo") | ||
|
||
|
||
@mock.patch( | ||
"google.auth._cloud_sdk.get_application_default_credentials_path", autospec=True | ||
) | ||
def test_default_impersonated_service_account(get_adc_path): | ||
get_adc_path.return_value = IMPERSONATED_SERVICE_ACCOUNT_AUTHORIZED_USER_SOURCE_FILE | ||
|
||
credentials, _ = _default.default() | ||
|
||
assert isinstance(credentials, impersonated_credentials.Credentials) | ||
assert isinstance( | ||
credentials._source_credentials, google.oauth2.credentials.Credentials | ||
) | ||
assert credentials.service_account_email == "[email protected]" | ||
assert credentials._delegates == ["[email protected]"] | ||
assert not credentials._quota_project_id | ||
assert not credentials._target_scopes | ||
|
||
|
||
@mock.patch( | ||
"google.auth._cloud_sdk.get_application_default_credentials_path", autospec=True | ||
) | ||
def test_default_impersonated_service_account_set_scopes(get_adc_path): | ||
get_adc_path.return_value = IMPERSONATED_SERVICE_ACCOUNT_AUTHORIZED_USER_SOURCE_FILE | ||
scopes = ["scope1", "scope2"] | ||
|
||
credentials, _ = _default.default(scopes=scopes) | ||
assert credentials._target_scopes == scopes | ||
|
||
|
||
@mock.patch( | ||
"google.auth._cloud_sdk.get_application_default_credentials_path", autospec=True | ||
) | ||
def test_default_impersonated_service_account_set_default_scopes(get_adc_path): | ||
get_adc_path.return_value = IMPERSONATED_SERVICE_ACCOUNT_AUTHORIZED_USER_SOURCE_FILE | ||
default_scopes = ["scope1", "scope2"] | ||
|
||
credentials, _ = _default.default(default_scopes=default_scopes) | ||
assert credentials._target_scopes == default_scopes | ||
|
||
|
||
@mock.patch( | ||
"google.auth._cloud_sdk.get_application_default_credentials_path", autospec=True | ||
) | ||
def test_default_impersonated_service_account_set_both_scopes_and_default_scopes( | ||
get_adc_path | ||
): | ||
get_adc_path.return_value = IMPERSONATED_SERVICE_ACCOUNT_AUTHORIZED_USER_SOURCE_FILE | ||
scopes = ["scope1", "scope2"] | ||
default_scopes = ["scope3", "scope4"] | ||
|
||
credentials, _ = _default.default(scopes=scopes, default_scopes=default_scopes) | ||
assert credentials._target_scopes == scopes | ||
|
||
|
||
def test__get_api_key_credentials_no_env_var(): | ||
cred, project_id = _default._get_api_key_credentials(quota_project_id="project-foo") | ||
assert cred is None | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters