Skip to content

Commit 61c6814

Browse files
committed
Merge pull request #641 from dhermes/fix-638
Adding get_for_service_account_json function in credentials.
2 parents f64fc55 + a80f1dd commit 61c6814

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

gcloud/credentials.py

Lines changed: 31 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,37 @@ def get_credentials():
7273
return client.GoogleCredentials.get_application_default()
7374

7475

76+
def get_for_service_account_json(json_credentials_path, scope=None):
77+
"""Gets the credentials for a service account with JSON key.
78+
79+
:type json_credentials_path: string
80+
:param json_credentials_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 contain a JSON
83+
object with a private key and other
84+
credentials information (downloaded from the
85+
Google APIs console).
86+
87+
:type scope: string or tuple of string
88+
:param scope: The scope against which to authenticate. (Different services
89+
require different scopes, check the documentation for which
90+
scope is required for the different levels of access to any
91+
particular API.)
92+
93+
:rtype: :class:`oauth2client.client.GoogleCredentials`,
94+
:class:`oauth2client.service_account._ServiceAccountCredentials`
95+
:returns: New service account or Google (for a user JSON key file)
96+
credentials object.
97+
"""
98+
credentials = _get_application_default_credential_from_file(
99+
json_credentials_path)
100+
if scope is not None:
101+
credentials = credentials.create_scoped(scope)
102+
return credentials
103+
104+
75105
def get_for_service_account_p12(client_email, private_key_path, scope=None):
76-
"""Gets the credentials for a service account.
106+
"""Gets the credentials for a service account with PKCS12 / p12 key.
77107
78108
.. note::
79109
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, json_credentials_path, scope=None):
67+
from gcloud.credentials import get_for_service_account_json
68+
return get_for_service_account_json(json_credentials_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)