17
17
import collections
18
18
19
19
import grpc
20
+ from packaging import version
21
+ import pkg_resources
20
22
import six
21
23
22
24
from google .api_core import exceptions
33
35
except ImportError :
34
36
HAS_GRPC_GCP = False
35
37
38
+ try :
39
+ # google.auth.__version__ was added in 1.26.0
40
+ _GOOGLE_AUTH_VERSION = google .auth .__version__
41
+ except AttributeError :
42
+ try : # try pkg_resources if it is available
43
+ _GOOGLE_AUTH_VERSION = pkg_resources .get_distribution ("google-auth" ).version
44
+ except pkg_resources .DistributionNotFound : # pragma: NO COVER
45
+ _GOOGLE_AUTH_VERSION = None
46
+
47
+ if _GOOGLE_AUTH_VERSION is not None and version .parse (_GOOGLE_AUTH_VERSION ) >= version .parse ("1.25.0" ):
48
+ _GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST = True
49
+ else :
50
+ _GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST = False
51
+
36
52
# The list of gRPC Callable interfaces that return iterators.
37
53
_STREAM_WRAP_CLASSES = (grpc .UnaryStreamMultiCallable , grpc .StreamStreamMultiCallable )
38
54
@@ -179,9 +195,11 @@ def wrap_errors(callable_):
179
195
def _create_composite_credentials (
180
196
credentials = None ,
181
197
credentials_file = None ,
198
+ default_scopes = None ,
182
199
scopes = None ,
183
200
ssl_credentials = None ,
184
- quota_project_id = None ):
201
+ quota_project_id = None ,
202
+ default_host = None ):
185
203
"""Create the composite credentials for secure channels.
186
204
187
205
Args:
@@ -191,12 +209,16 @@ def _create_composite_credentials(
191
209
credentials_file (str): A file with credentials that can be loaded with
192
210
:func:`google.auth.load_credentials_from_file`. This argument is
193
211
mutually exclusive with credentials.
212
+ default_scopes (Sequence[str]): A optional list of scopes needed for this
213
+ service. These are only used when credentials are not specified and
214
+ are passed to :func:`google.auth.default`.
194
215
scopes (Sequence[str]): A optional list of scopes needed for this
195
216
service. These are only used when credentials are not specified and
196
217
are passed to :func:`google.auth.default`.
197
218
ssl_credentials (grpc.ChannelCredentials): Optional SSL channel
198
219
credentials. This can be used to specify different certificates.
199
220
quota_project_id (str): An optional project to use for billing and quota.
221
+ default_host (str): The default endpoint. e.g., "pubsub.googleapis.com".
200
222
201
223
Returns:
202
224
grpc.ChannelCredentials: The composed channel credentials object.
@@ -210,21 +232,55 @@ def _create_composite_credentials(
210
232
)
211
233
212
234
if credentials_file :
213
- credentials , _ = google .auth .load_credentials_from_file (credentials_file , scopes = scopes )
235
+ # TODO: remove this if/else once google-auth >= 1.25.0 is required
236
+ if _GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST :
237
+ credentials , _ = google .auth .load_credentials_from_file (
238
+ credentials_file ,
239
+ scopes = scopes ,
240
+ default_scopes = default_scopes
241
+ )
242
+ else :
243
+ credentials , _ = google .auth .load_credentials_from_file (
244
+ credentials_file ,
245
+ scopes = scopes or default_scopes ,
246
+ )
214
247
elif credentials :
215
- credentials = google .auth .credentials .with_scopes_if_required (credentials , scopes )
248
+ # TODO: remove this if/else once google-auth >= 1.25.0 is required
249
+ if _GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST :
250
+ credentials = google .auth .credentials .with_scopes_if_required (
251
+ credentials ,
252
+ scopes = scopes ,
253
+ default_scopes = default_scopes
254
+ )
255
+ else :
256
+ credentials = google .auth .credentials .with_scopes_if_required (
257
+ credentials ,
258
+ scopes = scopes or default_scopes ,
259
+ )
260
+
216
261
else :
217
- credentials , _ = google .auth .default (scopes = scopes )
262
+ # TODO: remove this if/else once google-auth >= 1.25.0 is required
263
+ if _GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST :
264
+ credentials , _ = google .auth .default (scopes = scopes , default_scopes = default_scopes )
265
+ else :
266
+ credentials , _ = google .auth .default (scopes = scopes or default_scopes )
218
267
219
268
if quota_project_id and isinstance (credentials , google .auth .credentials .CredentialsWithQuotaProject ):
220
269
credentials = credentials .with_quota_project (quota_project_id )
221
270
222
271
request = google .auth .transport .requests .Request ()
223
272
224
273
# Create the metadata plugin for inserting the authorization header.
225
- metadata_plugin = google .auth .transport .grpc .AuthMetadataPlugin (
226
- credentials , request
227
- )
274
+
275
+ # TODO: remove this if/else once google-auth >= 1.25.0 is required
276
+ if _GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST :
277
+ metadata_plugin = google .auth .transport .grpc .AuthMetadataPlugin (
278
+ credentials , request , default_host = default_host ,
279
+ )
280
+ else :
281
+ metadata_plugin = google .auth .transport .grpc .AuthMetadataPlugin (
282
+ credentials , request
283
+ )
228
284
229
285
# Create a set of grpc.CallCredentials using the metadata plugin.
230
286
google_auth_credentials = grpc .metadata_call_credentials (metadata_plugin )
@@ -245,6 +301,8 @@ def create_channel(
245
301
ssl_credentials = None ,
246
302
credentials_file = None ,
247
303
quota_project_id = None ,
304
+ default_scopes = None ,
305
+ default_host = None ,
248
306
** kwargs ):
249
307
"""Create a secure channel with credentials.
250
308
@@ -262,6 +320,9 @@ def create_channel(
262
320
:func:`google.auth.load_credentials_from_file`. This argument is
263
321
mutually exclusive with credentials.
264
322
quota_project_id (str): An optional project to use for billing and quota.
323
+ default_scopes (Sequence[str]): Default scopes passed by a Google client
324
+ library. Use 'scopes' for user-defined scopes.
325
+ default_host (str): The default endpoint. e.g., "pubsub.googleapis.com".
265
326
kwargs: Additional key-word args passed to
266
327
:func:`grpc_gcp.secure_channel` or :func:`grpc.secure_channel`.
267
328
@@ -275,9 +336,11 @@ def create_channel(
275
336
composite_credentials = _create_composite_credentials (
276
337
credentials = credentials ,
277
338
credentials_file = credentials_file ,
339
+ default_scopes = default_scopes ,
278
340
scopes = scopes ,
279
341
ssl_credentials = ssl_credentials ,
280
342
quota_project_id = quota_project_id ,
343
+ default_host = default_host ,
281
344
)
282
345
283
346
if HAS_GRPC_GCP :
0 commit comments