Skip to content

Commit b086249

Browse files
Jon Wayne Parrottdhermes
authored andcommitted
Raise ValueError if credentials are not from google-auth
1 parent 8fd98e0 commit b086249

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

core/google/cloud/client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

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

17+
import google.auth.credentials
1718
from google.oauth2 import service_account
1819
import six
1920

@@ -22,6 +23,13 @@
2223
from google.cloud.credentials import get_credentials
2324

2425

26+
_GOOGLE_AUTH_CREDENTIALS_HELP = (
27+
'This library only supports credentials from google-auth-library-python. '
28+
'See https://google-cloud-python.readthedocs.io/en/latest/'
29+
'google-cloud-auth.html for help on authentication with this library.'
30+
)
31+
32+
2533
class _ClientFactoryMixin(object):
2634
"""Mixin to allow factories that create credentials.
2735
@@ -83,6 +91,10 @@ class Client(_ClientFactoryMixin):
8391
_connection_class = Connection
8492

8593
def __init__(self, credentials=None, http=None):
94+
if (credentials is not None and
95+
not isinstance(
96+
credentials, google.auth.credentials.Credentials)):
97+
raise ValueError(_GOOGLE_AUTH_CREDENTIALS_HELP)
8698
if credentials is None and http is None:
8799
credentials = get_credentials()
88100
self._connection = self._connection_class(

core/unit_tests/test_client.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
import mock
1818

1919

20+
def _make_credentials():
21+
import google.auth.credentials
22+
return mock.Mock(spec=google.auth.credentials.Credentials)
23+
24+
2025
class Test_ClientFactoryMixin(unittest.TestCase):
2126

2227
@staticmethod
@@ -52,7 +57,7 @@ def test_ctor_defaults(self):
5257
from google.cloud._testing import _Monkey
5358
from google.cloud import client
5459

55-
CREDENTIALS = object()
60+
CREDENTIALS = _make_credentials()
5661
FUNC_CALLS = []
5762

5863
def mock_get_credentials():
@@ -67,20 +72,27 @@ def mock_get_credentials():
6772
self.assertEqual(FUNC_CALLS, ['get_credentials'])
6873

6974
def test_ctor_explicit(self):
70-
CREDENTIALS = object()
75+
CREDENTIALS = _make_credentials()
7176
HTTP = object()
7277
client_obj = self._make_one(credentials=CREDENTIALS, http=HTTP)
7378

7479
self.assertIsInstance(client_obj._connection, _MockConnection)
7580
self.assertIs(client_obj._connection.credentials, CREDENTIALS)
7681
self.assertIs(client_obj._connection.http, HTTP)
7782

83+
def test_ctor_bad_credentials(self):
84+
CREDENTIALS = object()
85+
86+
with self.assertRaises(ValueError):
87+
self._make_one(credentials=CREDENTIALS)
88+
7889
def test_from_service_account_json(self):
7990
KLASS = self._get_target_class()
8091

8192
constructor_patch = mock.patch(
8293
'google.oauth2.service_account.Credentials.'
83-
'from_service_account_file')
94+
'from_service_account_file',
95+
return_value=_make_credentials())
8496

8597
with constructor_patch as constructor:
8698
client_obj = KLASS.from_service_account_json(
@@ -122,7 +134,7 @@ def test_ctor_defaults(self):
122134
from google.cloud import client
123135

124136
PROJECT = 'PROJECT'
125-
CREDENTIALS = object()
137+
CREDENTIALS = _make_credentials()
126138
FUNC_CALLS = []
127139

128140
def mock_determine_proj(project):
@@ -160,7 +172,7 @@ def mock_determine_proj(project):
160172
self.assertEqual(FUNC_CALLS, [(None, '_determine_default_project')])
161173

162174
def test_ctor_w_invalid_project(self):
163-
CREDENTIALS = object()
175+
CREDENTIALS = _make_credentials()
164176
HTTP = object()
165177
with self.assertRaises(ValueError):
166178
self._make_one(project=object(), credentials=CREDENTIALS,
@@ -169,7 +181,7 @@ def test_ctor_w_invalid_project(self):
169181
def _explicit_ctor_helper(self, project):
170182
import six
171183

172-
CREDENTIALS = object()
184+
CREDENTIALS = _make_credentials()
173185
HTTP = object()
174186

175187
client_obj = self._make_one(project=project, credentials=CREDENTIALS,

0 commit comments

Comments
 (0)