Skip to content

Commit a888f64

Browse files
committed
Merge pull request #840 from dhermes/create-scoped-help
Adding scope to credentials in Connection constructors.
2 parents e62fc58 + b44b25b commit a888f64

File tree

13 files changed

+69
-69
lines changed

13 files changed

+69
-69
lines changed

gcloud/connection.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import httplib2
2323

24-
from gcloud.credentials import get_credentials
2524
from gcloud.exceptions import make_exception
2625

2726

@@ -94,6 +93,25 @@ def http(self):
9493
self._http = self._credentials.authorize(self._http)
9594
return self._http
9695

96+
@staticmethod
97+
def _create_scoped_credentials(credentials, scope):
98+
"""Create a scoped set of credentials if it is required.
99+
100+
:type credentials: :class:`oauth2client.client.OAuth2Credentials` or
101+
:class:`NoneType`
102+
:param credentials: The OAuth2 Credentials to add a scope to.
103+
104+
:type scope: list of URLs
105+
:param scope: the effective service auth scopes for the connection.
106+
107+
:rtype: :class:`oauth2client.client.OAuth2Credentials` or
108+
:class:`NoneType`
109+
:returns: A new credentials object that has a scope added (if needed).
110+
"""
111+
if credentials and credentials.create_scoped_required():
112+
credentials = credentials.create_scoped(scope)
113+
return credentials
114+
97115

98116
class JSONConnection(Connection):
99117
"""A connection to a Google JSON-based API.
@@ -320,20 +338,3 @@ def api_request(self, method, path, query_params=None,
320338
return json.loads(content)
321339

322340
return content
323-
324-
325-
def get_scoped_connection(klass, scopes):
326-
"""Create a scoped connection to GCloud.
327-
328-
:type klass: subclass of :class:`gcloud.connection.Connection`
329-
:param klass: the specific ``Connection`` class to instantiate.
330-
331-
:type scopes: list of URLs
332-
:param scopes: the effective service auth scopes for the connection.
333-
334-
:rtype: instance of ``klass``
335-
:returns: A connection defined with the proper credentials.
336-
"""
337-
implicit_credentials = get_credentials()
338-
scoped_credentials = implicit_credentials.create_scoped(scopes)
339-
return klass(credentials=scoped_credentials)

gcloud/datastore/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
when race conditions may occur.
4949
"""
5050

51-
from gcloud.datastore._implicit_environ import SCOPE
5251
from gcloud.datastore._implicit_environ import get_connection
5352
from gcloud.datastore._implicit_environ import get_default_connection
5453
from gcloud.datastore._implicit_environ import get_default_dataset_id
@@ -59,6 +58,7 @@
5958
from gcloud.datastore.api import get
6059
from gcloud.datastore.api import put
6160
from gcloud.datastore.batch import Batch
61+
from gcloud.datastore.connection import SCOPE
6262
from gcloud.datastore.connection import Connection
6363
from gcloud.datastore.dataset import Dataset
6464
from gcloud.datastore.entity import Entity

gcloud/datastore/_implicit_environ.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,10 @@
2323
from gcloud._helpers import _app_engine_id
2424
from gcloud._helpers import _compute_engine_id
2525
from gcloud._helpers import _lazy_property_deco
26-
from gcloud.connection import get_scoped_connection
26+
from gcloud.credentials import get_credentials
2727
from gcloud.datastore.connection import Connection
2828

2929

30-
SCOPE = ('https://www.googleapis.com/auth/datastore',
31-
'https://www.googleapis.com/auth/userinfo.email')
32-
"""The scopes required for authenticating as a Cloud Datastore consumer."""
33-
3430
_DATASET_ENV_VAR_NAME = 'GCLOUD_DATASET_ID'
3531
_GCD_DATASET_ENV_VAR_NAME = 'DATASTORE_DATASET'
3632

@@ -126,7 +122,8 @@ def get_connection():
126122
:rtype: :class:`gcloud.datastore.connection.Connection`
127123
:returns: A connection defined with the proper credentials.
128124
"""
129-
return get_scoped_connection(Connection, SCOPE)
125+
credentials = get_credentials()
126+
return Connection(credentials=credentials)
130127

131128

132129
def set_default_connection(connection=None):

gcloud/datastore/connection.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
from gcloud.datastore import _datastore_v1_pb2 as datastore_pb
2222

2323

24+
SCOPE = ('https://www.googleapis.com/auth/datastore',
25+
'https://www.googleapis.com/auth/userinfo.email')
26+
"""The scopes required for authenticating as a Cloud Datastore consumer."""
27+
2428
_GCD_HOST_ENV_VAR_NAME = 'DATASTORE_HOST'
2529

2630

@@ -46,6 +50,7 @@ class Connection(connection.Connection):
4650
"""A template for the URL of a particular API call."""
4751

4852
def __init__(self, credentials=None, http=None, api_base_url=None):
53+
credentials = self._create_scoped_credentials(credentials, SCOPE)
4954
super(Connection, self).__init__(credentials=credentials, http=http)
5055
if api_base_url is None:
5156
api_base_url = os.getenv(_GCD_HOST_ENV_VAR_NAME,

gcloud/datastore/test__implicit_environ.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ def _callFUT(self):
305305

306306
def test_it(self):
307307
from gcloud import credentials
308-
from gcloud.datastore._implicit_environ import SCOPE
308+
from gcloud.datastore.connection import SCOPE
309309
from gcloud.datastore.connection import Connection
310310
from gcloud.test_credentials import _Client
311311
from gcloud._testing import _Monkey

gcloud/datastore/test_connection.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,12 @@ def test_ctor_defaults(self):
9797
self.assertEqual(conn.credentials, None)
9898

9999
def test_ctor_explicit(self):
100-
creds = object()
100+
class Creds(object):
101+
102+
def create_scoped_required(self):
103+
return False
104+
105+
creds = Creds()
101106
conn = self._makeOne(creds)
102107
self.assertTrue(conn.credentials is creds)
103108

@@ -122,6 +127,10 @@ class Creds(object):
122127
def authorize(self, http):
123128
self._called_with = http
124129
return authorized
130+
131+
def create_scoped_required(self):
132+
return False
133+
125134
creds = Creds()
126135
conn = self._makeOne(creds)
127136
self.assertTrue(conn.http is authorized)

gcloud/pubsub/__init__.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,17 @@
2626

2727
from gcloud._helpers import get_default_project
2828
from gcloud._helpers import set_default_project
29-
from gcloud.connection import get_scoped_connection
29+
from gcloud.credentials import get_credentials
3030
from gcloud.pubsub import _implicit_environ
3131
from gcloud.pubsub._implicit_environ import get_default_connection
3232
from gcloud.pubsub.api import list_subscriptions
3333
from gcloud.pubsub.api import list_topics
34+
from gcloud.pubsub.connection import SCOPE
3435
from gcloud.pubsub.connection import Connection
3536
from gcloud.pubsub.subscription import Subscription
3637
from gcloud.pubsub.topic import Topic
3738

3839

39-
SCOPE = ('https://www.googleapis.com/auth/pubsub',
40-
'https://www.googleapis.com/auth/cloud-platform')
41-
42-
4340
def set_default_connection(connection=None):
4441
"""Set default connection either explicitly or implicitly as fall-back.
4542
@@ -78,4 +75,5 @@ def get_connection():
7875
:rtype: :class:`gcloud.pubsub.connection.Connection`
7976
:returns: A connection defined with the proper credentials.
8077
"""
81-
return get_scoped_connection(Connection, SCOPE)
78+
credentials = get_credentials()
79+
return Connection(credentials=credentials)

gcloud/pubsub/connection.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
from gcloud import connection as base_connection
1818

1919

20+
SCOPE = ('https://www.googleapis.com/auth/pubsub',
21+
'https://www.googleapis.com/auth/cloud-platform')
22+
"""The scopes required for authenticating as a Cloud Pub/Sub consumer."""
23+
24+
2025
class Connection(base_connection.JSONConnection):
2126
"""A connection to Google Cloud Pubsub via the JSON REST API."""
2227

@@ -28,3 +33,7 @@ class Connection(base_connection.JSONConnection):
2833

2934
API_URL_TEMPLATE = '{api_base_url}/{api_version}{path}'
3035
"""A template for the URL of a particular API call."""
36+
37+
def __init__(self, credentials=None, http=None):
38+
credentials = self._create_scoped_credentials(credentials, SCOPE)
39+
super(Connection, self).__init__(credentials=credentials, http=http)

gcloud/storage/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
from gcloud._helpers import get_default_project
4444
from gcloud._helpers import set_default_project
4545
from gcloud.storage import _implicit_environ
46-
from gcloud.storage._implicit_environ import SCOPE
4746
from gcloud.storage._implicit_environ import get_connection
4847
from gcloud.storage._implicit_environ import get_default_bucket
4948
from gcloud.storage._implicit_environ import get_default_connection
@@ -55,6 +54,7 @@
5554
from gcloud.storage.batch import Batch
5655
from gcloud.storage.blob import Blob
5756
from gcloud.storage.bucket import Bucket
57+
from gcloud.storage.connection import SCOPE
5858
from gcloud.storage.connection import Connection
5959

6060

gcloud/storage/_implicit_environ.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,10 @@
2020

2121

2222
from gcloud._helpers import _lazy_property_deco
23-
from gcloud.connection import get_scoped_connection
23+
from gcloud.credentials import get_credentials
2424
from gcloud.storage.connection import Connection
2525

2626

27-
SCOPE = ('https://www.googleapis.com/auth/devstorage.full_control',
28-
'https://www.googleapis.com/auth/devstorage.read_only',
29-
'https://www.googleapis.com/auth/devstorage.read_write')
30-
31-
3227
class _DefaultsContainer(object):
3328
"""Container for defaults.
3429
@@ -83,7 +78,8 @@ def get_connection():
8378
:rtype: :class:`gcloud.storage.connection.Connection`
8479
:returns: A connection defined with the proper credentials.
8580
"""
86-
return get_scoped_connection(Connection, SCOPE)
81+
credentials = get_credentials()
82+
return Connection(credentials=credentials)
8783

8884

8985
def set_default_connection(connection=None):

0 commit comments

Comments
 (0)