diff --git a/gcloud/connection.py b/gcloud/connection.py index 003dbdfd2d12..b25e9cf70d61 100644 --- a/gcloud/connection.py +++ b/gcloud/connection.py @@ -1,4 +1,5 @@ from pkg_resources import get_distribution + import httplib2 @@ -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 diff --git a/gcloud/credentials.py b/gcloud/credentials.py index e4bcb7d32fe0..9e96f5317285 100644 --- a/gcloud/credentials.py +++ b/gcloud/credentials.py @@ -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) diff --git a/gcloud/datastore/__init__.py b/gcloud/datastore/__init__.py index 492b56f3180f..fee5a98b47b9 100644 --- a/gcloud/datastore/__init__.py +++ b/gcloud/datastore/__init__.py @@ -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): diff --git a/gcloud/datastore/connection.py b/gcloud/datastore/connection.py index 7015d205db45..9916615a2781 100644 --- a/gcloud/datastore/connection.py +++ b/gcloud/datastore/connection.py @@ -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. """ diff --git a/gcloud/storage/__init__.py b/gcloud/storage/__init__.py index 405ee2393e55..69b6707861a6 100644 --- a/gcloud/storage/__init__.py +++ b/gcloud/storage/__init__.py @@ -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): diff --git a/gcloud/test_credentials.py b/gcloud/test_credentials.py index 69a06ec4ef0d..51d83e5d0b03 100644 --- a/gcloud/test_credentials.py +++ b/gcloud/test_credentials.py @@ -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 = 'phred@example.com' 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, @@ -32,14 +28,13 @@ def test_get_for_service_account_w_scope(self): CLIENT_EMAIL = 'phred@example.com' 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,