Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make service_account_email public, read-only #76

Merged
merged 4 commits into from
Nov 8, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions google/auth/app_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ def refresh(self, request):

self.token, self.expiry = token, expiry

@property
def service_account_email(self):
"""The service account email."""
if self._service_account_id is not None:
return self._service_account_id
else:
return app_identity.get_service_account_name()

This comment was marked as spam.

This comment was marked as spam.


@property
def requires_scopes(self):
"""Checks if the credentials requires scopes.
Expand Down
9 changes: 9 additions & 0 deletions google/auth/compute_engine/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ def refresh(self, request):
except exceptions.TransportError as exc:
raise exceptions.RefreshError(exc)

@property
def service_account_email(self):
"""The service account email.

.. Note: This is not guarenteed to be set until :meth`refresh` has been

This comment was marked as spam.

This comment was marked as spam.

called.
"""
return self._service_account_email

@property
def requires_scopes(self):
"""False: Compute Engine credentials can not be scoped."""
Expand Down
5 changes: 5 additions & 0 deletions google/oauth2/service_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ def to_jwt_credentials(self):
issuer=self._service_account_email,
subject=self._service_account_email)

@property
def service_account_email(self):
"""The service account email."""
return self._service_account_email

@property
def requires_scopes(self):
"""Checks if the credentials requires scopes.
Expand Down
4 changes: 3 additions & 1 deletion tests/compute_engine/test_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def test_default_state(self):
assert not self.credentials.expired
# Scopes aren't needed
assert not self.credentials.requires_scopes
# Service account email hasn't been populated
assert self.credentials.service_account_email == 'default'

@mock.patch(
'google.auth._helpers.utcnow', return_value=datetime.datetime.min)
Expand All @@ -58,7 +60,7 @@ def test_refresh_success(self, get_mock, now_mock):
datetime.datetime.min + datetime.timedelta(seconds=500))

# Check the credential info
assert (self.credentials._service_account_email ==
assert (self.credentials.service_account_email ==

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

'[email protected]')
assert self.credentials._scopes == ['one', 'two']

Expand Down
8 changes: 4 additions & 4 deletions tests/oauth2/test_service_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def test_from_service_account_info(self):

assert (credentials._signer.key_id ==
SERVICE_ACCOUNT_INFO['private_key_id'])
assert (credentials._service_account_email ==
assert (credentials.service_account_email ==
SERVICE_ACCOUNT_INFO['client_email'])
assert credentials._token_uri == SERVICE_ACCOUNT_INFO['token_uri']

Expand All @@ -77,8 +77,8 @@ def test_from_service_account_info_args(self):
info, scopes=scopes, subject=subject,
additional_claims=additional_claims)

assert credentials.service_account_email == info['client_email']
assert credentials._signer.key_id == info['private_key_id']
assert credentials._service_account_email == info['client_email']
assert credentials._token_uri == info['token_uri']
assert credentials._scopes == scopes
assert credentials._subject == subject
Expand All @@ -90,8 +90,8 @@ def test_from_service_account_file(self):
credentials = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_JSON_FILE)

assert credentials.service_account_email == info['client_email']
assert credentials._signer.key_id == info['private_key_id']
assert credentials._service_account_email == info['client_email']
assert credentials._token_uri == info['token_uri']

def test_from_service_account_file_args(self):
Expand All @@ -104,8 +104,8 @@ def test_from_service_account_file_args(self):
SERVICE_ACCOUNT_JSON_FILE, subject=subject,
scopes=scopes, additional_claims=additional_claims)

assert credentials.service_account_email == info['client_email']
assert credentials._signer.key_id == info['private_key_id']
assert credentials._service_account_email == info['client_email']
assert credentials._token_uri == info['token_uri']
assert credentials._scopes == scopes
assert credentials._subject == subject
Expand Down
17 changes: 17 additions & 0 deletions tests/test_app_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ def test_with_scopes(self, app_identity_mock):
assert scoped_credentials.has_scopes(['email'])
assert not scoped_credentials.requires_scopes

def test_service_account_email_implicit(self, app_identity_mock):
app_identity_mock.get_service_account_name.return_value = (
mock.sentinel.service_account_email)
credentials = app_engine.Credentials()

assert (credentials.service_account_email ==
mock.sentinel.service_account_email)
assert app_identity_mock.get_service_account_name.called

def test_service_account_email_explicit(self, app_identity_mock):
credentials = app_engine.Credentials(
service_account_id=mock.sentinel.service_account_email)

assert (credentials.service_account_email ==
mock.sentinel.service_account_email)
assert not app_identity_mock.get_service_account_name.called

@mock.patch(
'google.auth._helpers.utcnow',
return_value=datetime.datetime.min)
Expand Down