Skip to content

Commit 9c4807d

Browse files
committed
Merge pull request #1513 from dhermes/remove-svc-acct-wrappers
Removing get_for_service_account_* helpers.
2 parents a77e7fa + 97790d9 commit 9c4807d

File tree

4 files changed

+39
-173
lines changed

4 files changed

+39
-173
lines changed

gcloud/client.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@
1414

1515
"""Base classes for client used to interact with Google Cloud APIs."""
1616

17+
from oauth2client.service_account import ServiceAccountCredentials
1718
import six
1819

1920
from gcloud._helpers import _determine_default_project
2021
from gcloud.connection import Connection
2122
from gcloud.credentials import get_credentials
22-
from gcloud.credentials import get_for_service_account_json
23-
from gcloud.credentials import get_for_service_account_p12
2423

2524

2625
class _ClientFactoryMixin(object):
@@ -56,7 +55,8 @@ def from_service_account_json(cls, json_credentials_path, *args, **kwargs):
5655
"""
5756
if 'credentials' in kwargs:
5857
raise TypeError('credentials must not be in keyword arguments')
59-
credentials = get_for_service_account_json(json_credentials_path)
58+
credentials = ServiceAccountCredentials.from_json_keyfile_name(
59+
json_credentials_path)
6060
kwargs['credentials'] = credentials
6161
return cls(*args, **kwargs)
6262

@@ -90,8 +90,8 @@ def from_service_account_p12(cls, client_email, private_key_path,
9090
"""
9191
if 'credentials' in kwargs:
9292
raise TypeError('credentials must not be in keyword arguments')
93-
credentials = get_for_service_account_p12(client_email,
94-
private_key_path)
93+
credentials = ServiceAccountCredentials.from_p12_keyfile(
94+
client_email, private_key_path)
9595
kwargs['credentials'] = credentials
9696
return cls(*args, **kwargs)
9797

gcloud/credentials.py

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -102,63 +102,6 @@ def get_credentials():
102102
return client.GoogleCredentials.get_application_default()
103103

104104

105-
def get_for_service_account_json(json_credentials_path, scope=None):
106-
"""Gets the credentials for a service account with JSON key.
107-
108-
:type json_credentials_path: string
109-
:param json_credentials_path: The path to a private key file (this file was
110-
given to you when you created the service
111-
account). This file must contain a JSON
112-
object with a private key and other
113-
credentials information (downloaded from the
114-
Google APIs console).
115-
116-
:type scope: string or tuple of string
117-
:param scope: The scope against which to authenticate. (Different services
118-
require different scopes, check the documentation for which
119-
scope is required for the different levels of access to any
120-
particular API.)
121-
122-
:rtype: :class:`oauth2client.client.GoogleCredentials`,
123-
:class:`oauth2client.service_account.ServiceAccountCredentials`
124-
:returns: New service account or Google (for a user JSON key file)
125-
credentials object.
126-
"""
127-
return ServiceAccountCredentials.from_json_keyfile_name(
128-
json_credentials_path, scopes=scope)
129-
130-
131-
def get_for_service_account_p12(client_email, private_key_path, scope=None):
132-
"""Gets the credentials for a service account with PKCS12 / p12 key.
133-
134-
.. note::
135-
This method is not used by default, instead :func:`get_credentials`
136-
is used. This method is intended to be used when the environment is
137-
known explicitly and detecting the environment implicitly would be
138-
superfluous.
139-
140-
:type client_email: string
141-
:param client_email: The e-mail attached to the service account.
142-
143-
:type private_key_path: string
144-
:param private_key_path: The path to a private key file (this file was
145-
given to you when you created the service
146-
account). This file must be in P12 format.
147-
148-
:type scope: string or tuple of string
149-
:param scope: The scope against which to authenticate. (Different services
150-
require different scopes, check the documentation for which
151-
scope is required for the different levels of access to any
152-
particular API.)
153-
154-
:rtype: :class:`oauth2client.service_account.ServiceAccountCredentials`
155-
:returns: A new ``ServiceAccountCredentials`` instance with the
156-
needed service account settings.
157-
"""
158-
return ServiceAccountCredentials.from_p12_keyfile(
159-
client_email, private_key_path, scopes=scope)
160-
161-
162105
def _get_pem_key(credentials):
163106
"""Gets private key for a PEM payload from a credentials object.
164107

gcloud/test_client.py

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,14 @@ def test_from_service_account_json(self):
7676
from gcloud import client
7777

7878
KLASS = self._getTargetClass()
79-
CREDENTIALS = object()
80-
_CALLED = []
81-
82-
def mock_creds(arg1):
83-
_CALLED.append((arg1,))
84-
return CREDENTIALS
85-
86-
BOGUS_ARG = object()
87-
with _Monkey(client, get_for_service_account_json=mock_creds):
88-
client_obj = KLASS.from_service_account_json(BOGUS_ARG)
79+
MOCK_FILENAME = 'foo.path'
80+
mock_creds = _MockServiceAccountCredentials()
81+
with _Monkey(client, ServiceAccountCredentials=mock_creds):
82+
client_obj = KLASS.from_service_account_json(MOCK_FILENAME)
8983

90-
self.assertTrue(client_obj.connection.credentials is CREDENTIALS)
91-
self.assertEqual(_CALLED, [(BOGUS_ARG,)])
84+
self.assertTrue(client_obj.connection.credentials is
85+
mock_creds._result)
86+
self.assertEqual(mock_creds.json_called, [MOCK_FILENAME])
9287

9388
def test_from_service_account_json_fail(self):
9489
KLASS = self._getTargetClass()
@@ -101,20 +96,17 @@ def test_from_service_account_p12(self):
10196
from gcloud import client
10297

10398
KLASS = self._getTargetClass()
104-
CREDENTIALS = object()
105-
_CALLED = []
106-
107-
def mock_creds(arg1, arg2):
108-
_CALLED.append((arg1, arg2))
109-
return CREDENTIALS
110-
111-
BOGUS_ARG1 = object()
112-
BOGUS_ARG2 = object()
113-
with _Monkey(client, get_for_service_account_p12=mock_creds):
114-
client_obj = KLASS.from_service_account_p12(BOGUS_ARG1, BOGUS_ARG2)
115-
116-
self.assertTrue(client_obj.connection.credentials is CREDENTIALS)
117-
self.assertEqual(_CALLED, [(BOGUS_ARG1, BOGUS_ARG2)])
99+
CLIENT_EMAIL = 'phred@example.com'
100+
MOCK_FILENAME = 'foo.path'
101+
mock_creds = _MockServiceAccountCredentials()
102+
with _Monkey(client, ServiceAccountCredentials=mock_creds):
103+
client_obj = KLASS.from_service_account_p12(CLIENT_EMAIL,
104+
MOCK_FILENAME)
105+
106+
self.assertTrue(client_obj.connection.credentials is
107+
mock_creds._result)
108+
self.assertEqual(mock_creds.p12_called,
109+
[(CLIENT_EMAIL, MOCK_FILENAME)])
118110

119111
def test_from_service_account_p12_fail(self):
120112
KLASS = self._getTargetClass()
@@ -220,3 +212,19 @@ class _MockConnection(object):
220212
def __init__(self, credentials=None, http=None):
221213
self.credentials = credentials
222214
self.http = http
215+
216+
217+
class _MockServiceAccountCredentials(object):
218+
219+
def __init__(self):
220+
self.p12_called = []
221+
self.json_called = []
222+
self._result = object()
223+
224+
def from_p12_keyfile(self, email, path):
225+
self.p12_called.append((email, path))
226+
return self._result
227+
228+
def from_json_keyfile_name(self, path):
229+
self.json_called.append(path)
230+
return self._result

gcloud/test_credentials.py

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -80,75 +80,6 @@ def test_it(self):
8080
self.assertTrue(client._get_app_default_called)
8181

8282

83-
class Test_get_for_service_account_p12(unittest2.TestCase):
84-
85-
def _callFUT(self, client_email, private_key_path, scope=None):
86-
from gcloud.credentials import get_for_service_account_p12
87-
return get_for_service_account_p12(client_email, private_key_path,
88-
scope=scope)
89-
90-
def test_it(self):
91-
from gcloud import credentials as MUT
92-
from gcloud._testing import _Monkey
93-
94-
CLIENT_EMAIL = 'phred@example.com'
95-
MOCK_FILENAME = 'foo.path'
96-
MOCK_CRED_CLASS = _MockServiceAccountCredentials()
97-
with _Monkey(MUT, ServiceAccountCredentials=MOCK_CRED_CLASS):
98-
found = self._callFUT(CLIENT_EMAIL, MOCK_FILENAME)
99-
100-
self.assertTrue(found is MOCK_CRED_CLASS._result)
101-
self.assertEqual(MOCK_CRED_CLASS.p12_called,
102-
[(CLIENT_EMAIL, MOCK_FILENAME, None)])
103-
104-
def test_it_with_scope(self):
105-
from gcloud import credentials as MUT
106-
from gcloud._testing import _Monkey
107-
108-
CLIENT_EMAIL = 'phred@example.com'
109-
SCOPE = 'SCOPE'
110-
MOCK_FILENAME = 'foo.path'
111-
MOCK_CRED_CLASS = _MockServiceAccountCredentials()
112-
with _Monkey(MUT, ServiceAccountCredentials=MOCK_CRED_CLASS):
113-
found = self._callFUT(CLIENT_EMAIL, MOCK_FILENAME, SCOPE)
114-
115-
self.assertTrue(found is MOCK_CRED_CLASS._result)
116-
self.assertEqual(MOCK_CRED_CLASS.p12_called,
117-
[(CLIENT_EMAIL, MOCK_FILENAME, SCOPE)])
118-
119-
120-
class Test_get_for_service_account_json(unittest2.TestCase):
121-
122-
def _callFUT(self, json_credentials_path, scope=None):
123-
from gcloud.credentials import get_for_service_account_json
124-
return get_for_service_account_json(json_credentials_path, scope=scope)
125-
126-
def test_it(self):
127-
from gcloud._testing import _Monkey
128-
from gcloud import credentials as MUT
129-
130-
FILENAME = object()
131-
MOCK_CRED_CLASS = _MockServiceAccountCredentials()
132-
with _Monkey(MUT, ServiceAccountCredentials=MOCK_CRED_CLASS):
133-
result = self._callFUT(FILENAME)
134-
135-
self.assertEqual(result, MOCK_CRED_CLASS._result)
136-
self.assertEqual(MOCK_CRED_CLASS.json_called, [(FILENAME, None)])
137-
138-
def test_it_with_scope(self):
139-
from gcloud._testing import _Monkey
140-
from gcloud import credentials as MUT
141-
142-
FILENAME = object()
143-
SCOPE = object()
144-
MOCK_CRED_CLASS = _MockServiceAccountCredentials()
145-
with _Monkey(MUT, ServiceAccountCredentials=MOCK_CRED_CLASS):
146-
result = self._callFUT(FILENAME, scope=SCOPE)
147-
148-
self.assertEqual(result, MOCK_CRED_CLASS._result)
149-
self.assertEqual(MOCK_CRED_CLASS.json_called, [(FILENAME, SCOPE)])
150-
151-
15283
class Test_generate_signed_url(unittest2.TestCase):
15384

15485
def _callFUT(self, *args, **kwargs):
@@ -665,19 +596,3 @@ def non_transactional(*args, **kwargs):
665596
def do_nothing_wrapper(func):
666597
return func
667598
return do_nothing_wrapper
668-
669-
670-
class _MockServiceAccountCredentials(object):
671-
672-
def __init__(self):
673-
self.p12_called = []
674-
self.json_called = []
675-
self._result = _Credentials()
676-
677-
def from_p12_keyfile(self, email, path, scopes=None):
678-
self.p12_called.append((email, path, scopes))
679-
return self._result
680-
681-
def from_json_keyfile_name(self, path, scopes=None):
682-
self.json_called.append((path, scopes))
683-
return self._result

0 commit comments

Comments
 (0)