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

Removing gcloud.credential.Credential class. #235

Merged
merged 2 commits into from
Oct 13, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions gcloud/connection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pkg_resources import get_distribution

import httplib2


Expand All @@ -22,11 +23,10 @@ class Connection(object):
"""The user agent for gcloud-python requests."""

def __init__(self, credentials=None):
""":type credentials: :class:`gcloud.credentials.Credentials`
"""
:type credentials: :class:`oauth2client.client.OAuth2Credentials`
:param credentials: The OAuth2 Credentials to use for this connection.

"""

self._credentials = credentials

@property
Expand Down
55 changes: 26 additions & 29 deletions gcloud/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,36 @@
from oauth2client import client


class Credentials(object):
"""An object used to simplify the OAuth2 credentials library.
def get_for_service_account(client_email, private_key_path, scope=None):
"""Gets the credentials for a service account.

.. note::
You should not need to use this class directly.
You should not need to use this function directly.
Instead, use the helper methods provided in
:func:`gcloud.datastore.__init__.get_connection`
and
:func:`gcloud.datastore.__init__.get_dataset`
which use this class under the hood.
"""
which use this method under the hood.

:type client_email: string
:param client_email: The e-mail attached to the service account.

:type private_key_path: string
:param private_key_path: The path to a private key file (this file was
given to you when you created the service
account).

@classmethod
def get_for_service_account(cls, client_email, private_key_path,
scope=None):
"""Gets the credentials for a service account.

:type client_email: string
:param client_email: The e-mail attached to the service account.

:type private_key_path: string
:param private_key_path: The path to a private key file (this file was
given to you when you created the service
account).

:type scope: string or tuple of strings
:param scope: The scope against which to authenticate.
(Different services require different scopes,
check the documentation for which scope is required
for the different levels of access
to any particular API.)
"""
return client.SignedJwtAssertionCredentials(
service_account_name=client_email,
private_key=open(private_key_path).read(),
scope=scope)
:type scope: string or tuple of strings
:param scope: The scope against which to authenticate. (Different services
require different scopes, check the documentation for which
scope is required for the different levels of access to any
particular API.)

:rtype: :class:`oauth2client.client.SignedJwtAssertionCredentials`
:returns: A new SignedJwtAssertionCredentials instance with the
needed service account settings.
"""
return client.SignedJwtAssertionCredentials(
service_account_name=client_email,
private_key=open(private_key_path).read(),
scope=scope)
6 changes: 3 additions & 3 deletions gcloud/datastore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ def get_connection(client_email, private_key_path):
:rtype: :class:`gcloud.datastore.connection.Connection`
:returns: A connection defined with the proper credentials.
"""
from gcloud.credentials import Credentials
from gcloud import credentials
from gcloud.datastore.connection import Connection

credentials = Credentials.get_for_service_account(
svc_account_credentials = credentials.get_for_service_account(
client_email, private_key_path, scope=SCOPE)
return Connection(credentials=credentials)
return Connection(credentials=svc_account_credentials)


def get_dataset(dataset_id, client_email, private_key_path):
Expand Down
2 changes: 1 addition & 1 deletion gcloud/datastore/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Connection(connection.Connection):
This class should understand only the basic types (and protobufs)
in method arguments, however should be capable of returning advanced types.

:type credentials: :class:`gcloud.credentials.Credentials`
:type credentials: :class:`oauth2client.client.OAuth2Credentials`
:param credentials: The OAuth2 Credentials to use for this connection.
"""

Expand Down
6 changes: 3 additions & 3 deletions gcloud/storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ def get_connection(project, client_email, private_key_path):
:returns: A connection defined with the proper credentials.
"""

from gcloud.credentials import Credentials
from gcloud import credentials
from gcloud.storage.connection import Connection

credentials = Credentials.get_for_service_account(
svc_account_credentials = credentials.get_for_service_account(
client_email, private_key_path, scope=SCOPE)
return Connection(project=project, credentials=credentials)
return Connection(project=project, credentials=svc_account_credentials)


def get_bucket(bucket_name, project, client_email, private_key_path):
Expand Down
13 changes: 4 additions & 9 deletions gcloud/test_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,18 @@

class TestCredentials(unittest2.TestCase):

def _getTargetClass(self):
from gcloud.credentials import Credentials
return Credentials

def test_get_for_service_account_wo_scope(self):
from tempfile import NamedTemporaryFile
from gcloud import credentials
CLIENT_EMAIL = '[email protected]'
PRIVATE_KEY = 'SEEkR1t'
cls = self._getTargetClass()
client = _Client()
with _Monkey(credentials, client=client):
with NamedTemporaryFile() as f:
f.write(PRIVATE_KEY)
f.flush()
found = cls.get_for_service_account(CLIENT_EMAIL, f.name)
found = credentials.get_for_service_account(
CLIENT_EMAIL, f.name)
self.assertTrue(found is client._signed)
self.assertEqual(client._called_with,
{'service_account_name': CLIENT_EMAIL,
Expand All @@ -32,14 +28,13 @@ def test_get_for_service_account_w_scope(self):
CLIENT_EMAIL = '[email protected]'
PRIVATE_KEY = 'SEEkR1t'
SCOPE = 'SCOPE'
cls = self._getTargetClass()
client = _Client()
with _Monkey(credentials, client=client):
with NamedTemporaryFile() as f:
f.write(PRIVATE_KEY)
f.flush()
found = cls.get_for_service_account(CLIENT_EMAIL, f.name,
SCOPE)
found = credentials.get_for_service_account(
CLIENT_EMAIL, f.name, SCOPE)
self.assertTrue(found is client._signed)
self.assertEqual(client._called_with,
{'service_account_name': CLIENT_EMAIL,
Expand Down