Skip to content

Commit 66b47b5

Browse files
committed
Adding get_for_service_account_json function in credentials.
Fixes #638.
1 parent c6718f3 commit 66b47b5

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

gcloud/credentials.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from Crypto.PublicKey import RSA
2525
from Crypto.Signature import PKCS1_v1_5
2626
from oauth2client import client
27+
from oauth2client.client import _get_application_default_credential_from_file
2728
from oauth2client import crypt
2829
from oauth2client import service_account
2930
import pytz
@@ -72,8 +73,34 @@ def get_credentials():
7273
return client.GoogleCredentials.get_application_default()
7374

7475

76+
def get_for_service_account_json(private_key_path, scope=None):
77+
"""Gets the credentials for a service account with JSON key.
78+
79+
:type private_key_path: string
80+
:param private_key_path: The path to a private key file (this file was
81+
given to you when you created the service
82+
account). This file must be in JSON key format.
83+
84+
:type scope: string or tuple of string
85+
:param scope: The scope against which to authenticate. (Different services
86+
require different scopes, check the documentation for which
87+
scope is required for the different levels of access to any
88+
particular API.)
89+
90+
:rtype: :class:`oauth2client.client.GoogleCredentials`,
91+
:class:`oauth2client.service_account._ServiceAccountCredentials`
92+
:returns: New service account or Google (for a user JSON key file)
93+
credentials object.
94+
"""
95+
credentials = _get_application_default_credential_from_file(
96+
private_key_path)
97+
if scope is not None:
98+
credentials = credentials.create_scoped(scope)
99+
return credentials
100+
101+
75102
def get_for_service_account_p12(client_email, private_key_path, scope=None):
76-
"""Gets the credentials for a service account.
103+
"""Gets the credentials for a service account with PKCS12 / p12 key.
77104
78105
.. note::
79106
This method is not used by default, instead :func:`get_credentials`

gcloud/test_credentials.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,54 @@ def test_get_for_service_account_p12_w_scope(self):
6161
self.assertEqual(client._called_with, expected_called_with)
6262

6363

64+
class Test_get_for_service_account_json(unittest2.TestCase):
65+
66+
def _callFUT(self, private_key_path, scope=None):
67+
from gcloud.credentials import get_for_service_account_json
68+
return get_for_service_account_json(private_key_path, scope=scope)
69+
70+
def test_it(self):
71+
from gcloud._testing import _Monkey
72+
from gcloud import credentials as MUT
73+
74+
CREDS = _Credentials()
75+
_filenames = []
76+
77+
def get_creds(filename):
78+
_filenames.append(filename)
79+
return CREDS
80+
81+
FILENAME = object()
82+
83+
renames = {'_get_application_default_credential_from_file': get_creds}
84+
with _Monkey(MUT, **renames):
85+
self._callFUT(FILENAME)
86+
87+
self.assertEqual(_filenames, [FILENAME])
88+
self.assertFalse(hasattr(CREDS, '_scopes'))
89+
90+
def test_it_with_scope(self):
91+
from gcloud._testing import _Monkey
92+
from gcloud import credentials as MUT
93+
94+
CREDS = _Credentials()
95+
_filenames = []
96+
97+
def get_creds(filename):
98+
_filenames.append(filename)
99+
return CREDS
100+
101+
FILENAME = object()
102+
SCOPE = object()
103+
104+
renames = {'_get_application_default_credential_from_file': get_creds}
105+
with _Monkey(MUT, **renames):
106+
self._callFUT(FILENAME, scope=SCOPE)
107+
108+
self.assertEqual(_filenames, [FILENAME])
109+
self.assertEqual(CREDS._scopes, SCOPE)
110+
111+
64112
class Test_generate_signed_url(unittest2.TestCase):
65113

66114
def _callFUT(self, *args, **kwargs):

0 commit comments

Comments
 (0)