-
Notifications
You must be signed in to change notification settings - Fork 309
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. (#956)
* Make code changes in _default * Add unit tests. * Fix docstring. Co-authored-by: arithmetic1728 <[email protected]>
- Loading branch information
1 parent
87706fd
commit a8eb4c8
Showing
5 changed files
with
251 additions
and
36 deletions.
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
13 changes: 13 additions & 0 deletions
13
tests/data/impersonated_service_account_authorized_user_source.json
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"delegates": [ | ||
"[email protected]" | ||
], | ||
"service_account_impersonation_url": "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/[email protected]:generateAccessToken", | ||
"source_credentials": { | ||
"client_id": "123", | ||
"client_secret": "secret", | ||
"refresh_token": "alabalaportocala", | ||
"type": "authorized_user" | ||
}, | ||
"type": "impersonated_service_account" | ||
} |
17 changes: 17 additions & 0 deletions
17
tests/data/impersonated_service_account_service_account_source.json
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"delegates": [ | ||
"[email protected]" | ||
], | ||
"service_account_impersonation_url": "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/[email protected]:generateAccessToken", | ||
"source_credentials": { | ||
"type": "service_account", | ||
"project_id": "example-project", | ||
"private_key_id": "1", | ||
"private_key": "-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEA4ej0p7bQ7L/r4rVGUz9RN4VQWoej1Bg1m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ymYMIlqG+JatXQD1rR6ThgqOt8sgRyJqFCVT++YFMOAqXOBBLnaObZZ\nCFbh1fJ66BlSjoXff0W+SuOx5HuJJAa5+WtFHrPajwxeuRcNa8jwxUsB7n41wADu\nUqWWSRedVBg4Ijbw3nWwYDECgYB0pLew4z4bVuvdt+HgnJA9n0EuYowVdadpTEJg\nsoBjNHV4msLzdNqbjrAqgz6M/n8Ztg8D2PNHMNDNJPVHjJwcR7duSTA6w2p/4k28\nbvvk/45Ta3XmzlxZcZSOct3O31Cw0i2XDVc018IY5be8qendDYM08icNo7vQYkRH\n504kQQKBgQDjx60zpz8ozvm1XAj0wVhi7GwXe+5lTxiLi9Fxq721WDxPMiHDW2XL\nYXfFVy/9/GIMvEiGYdmarK1NW+VhWl1DC5xhDg0kvMfxplt4tynoq1uTsQTY31Mx\nBeF5CT/JuNYk3bEBF0H/Q3VGO1/ggVS+YezdFbLWIRoMnLj6XCFEGg==\n-----END RSA PRIVATE KEY-----\n", | ||
"client_email": "[email protected]", | ||
"client_id": "1234", | ||
"auth_uri": "https://accounts.google.com/o/oauth2/auth", | ||
"token_uri": "https://accounts.google.com/o/oauth2/token" | ||
}, | ||
"type": "impersonated_service_account" | ||
} |
14 changes: 14 additions & 0 deletions
14
tests/data/impersonated_service_account_with_quota_project.json
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"delegates": [ | ||
"[email protected]" | ||
], | ||
"quota_project_id": "quota_project", | ||
"service_account_impersonation_url": "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/[email protected]:generateAccessToken", | ||
"source_credentials": { | ||
"client_id": "123", | ||
"client_secret": "secret", | ||
"refresh_token": "alabalaportocala", | ||
"type": "authorized_user" | ||
}, | ||
"type": "impersonated_service_account" | ||
} |
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 |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
from google.auth import exceptions | ||
from google.auth import external_account | ||
from google.auth import identity_pool | ||
from google.auth import impersonated_credentials | ||
from google.oauth2 import service_account | ||
import google.oauth2.credentials | ||
|
||
|
@@ -128,6 +129,19 @@ | |
"workforce_pool_user_project": WORKFORCE_POOL_USER_PROJECT, | ||
} | ||
|
||
IMPERSONATED_SERVICE_ACCOUNT_AUTHORIZED_USER_SOURCE_FILE = os.path.join( | ||
DATA_DIR, "impersonated_service_account_authorized_user_source.json" | ||
) | ||
|
||
IMPERSONATED_SERVICE_ACCOUNT_WITH_QUOTA_PROJECT_FILE = os.path.join( | ||
DATA_DIR, "impersonated_service_account_with_quota_project.json" | ||
) | ||
|
||
IMPERSONATED_SERVICE_ACCOUNT_SERVICE_ACCOUNT_SOURCE_FILE = os.path.join( | ||
DATA_DIR, "impersonated_service_account_service_account_source.json" | ||
) | ||
|
||
|
||
MOCK_CREDENTIALS = mock.Mock(spec=credentials.CredentialsWithQuotaProject) | ||
MOCK_CREDENTIALS.with_quota_project.return_value = MOCK_CREDENTIALS | ||
|
||
|
@@ -278,6 +292,84 @@ def test_load_credentials_from_file_service_account_bad_format(tmpdir): | |
assert excinfo.match(r"missing fields") | ||
|
||
|
||
def test_load_credentials_from_file_impersonated_with_authorized_user_source(): | ||
credentials, project_id = _default.load_credentials_from_file( | ||
IMPERSONATED_SERVICE_ACCOUNT_AUTHORIZED_USER_SOURCE_FILE | ||
) | ||
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 | ||
assert project_id is None | ||
|
||
|
||
def test_load_credentials_from_file_impersonated_with_quota_project(): | ||
credentials, _ = _default.load_credentials_from_file( | ||
IMPERSONATED_SERVICE_ACCOUNT_WITH_QUOTA_PROJECT_FILE | ||
) | ||
assert isinstance(credentials, impersonated_credentials.Credentials) | ||
assert credentials._quota_project_id == "quota_project" | ||
|
||
|
||
def test_load_credentials_from_file_impersonated_with_service_account_source(): | ||
credentials, _ = _default.load_credentials_from_file( | ||
IMPERSONATED_SERVICE_ACCOUNT_SERVICE_ACCOUNT_SOURCE_FILE | ||
) | ||
assert isinstance(credentials, impersonated_credentials.Credentials) | ||
assert isinstance(credentials._source_credentials, service_account.Credentials) | ||
assert not credentials._quota_project_id | ||
|
||
|
||
def test_load_credentials_from_file_impersonated_passing_quota_project(): | ||
credentials, _ = _default.load_credentials_from_file( | ||
IMPERSONATED_SERVICE_ACCOUNT_SERVICE_ACCOUNT_SOURCE_FILE, | ||
quota_project_id="new_quota_project", | ||
) | ||
assert credentials._quota_project_id == "new_quota_project" | ||
|
||
|
||
def test_load_credentials_from_file_impersonated_passing_scopes(): | ||
credentials, _ = _default.load_credentials_from_file( | ||
IMPERSONATED_SERVICE_ACCOUNT_SERVICE_ACCOUNT_SOURCE_FILE, | ||
scopes=["scope1", "scope2"], | ||
) | ||
assert credentials._target_scopes == ["scope1", "scope2"] | ||
|
||
|
||
def test_load_credentials_from_file_impersonated_wrong_target_principal(tmpdir): | ||
|
||
with open(IMPERSONATED_SERVICE_ACCOUNT_AUTHORIZED_USER_SOURCE_FILE) as fh: | ||
impersonated_credentials_info = json.load(fh) | ||
impersonated_credentials_info[ | ||
"service_account_impersonation_url" | ||
] = "something_wrong" | ||
|
||
jsonfile = tmpdir.join("invalid.json") | ||
jsonfile.write(json.dumps(impersonated_credentials_info)) | ||
with pytest.raises(exceptions.DefaultCredentialsError) as excinfo: | ||
_default.load_credentials_from_file(str(jsonfile)) | ||
|
||
assert excinfo.match(r"Cannot extract target principal") | ||
|
||
|
||
def test_load_credentials_from_file_impersonated_wrong_source_type(tmpdir): | ||
|
||
with open(IMPERSONATED_SERVICE_ACCOUNT_AUTHORIZED_USER_SOURCE_FILE) as fh: | ||
impersonated_credentials_info = json.load(fh) | ||
impersonated_credentials_info["source_credentials"]["type"] = "external_account" | ||
|
||
jsonfile = tmpdir.join("invalid.json") | ||
jsonfile.write(json.dumps(impersonated_credentials_info)) | ||
with pytest.raises(exceptions.DefaultCredentialsError) as excinfo: | ||
_default.load_credentials_from_file(str(jsonfile)) | ||
|
||
assert excinfo.match(r"source credential of type external_account is not supported") | ||
|
||
|
||
@EXTERNAL_ACCOUNT_GET_PROJECT_ID_PATCH | ||
def test_load_credentials_from_file_external_account_identity_pool( | ||
get_project_id, tmpdir | ||
|