1717import collections
1818
1919import grpc
20+ from packaging import version
21+ import pkg_resources
2022import six
2123
2224from google .api_core import exceptions
3335except ImportError :
3436 HAS_GRPC_GCP = False
3537
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+
3652# The list of gRPC Callable interfaces that return iterators.
3753_STREAM_WRAP_CLASSES = (grpc .UnaryStreamMultiCallable , grpc .StreamStreamMultiCallable )
3854
@@ -179,9 +195,11 @@ def wrap_errors(callable_):
179195def _create_composite_credentials (
180196 credentials = None ,
181197 credentials_file = None ,
198+ default_scopes = None ,
182199 scopes = None ,
183200 ssl_credentials = None ,
184- quota_project_id = None ):
201+ quota_project_id = None ,
202+ default_host = None ):
185203 """Create the composite credentials for secure channels.
186204
187205 Args:
@@ -191,12 +209,16 @@ def _create_composite_credentials(
191209 credentials_file (str): A file with credentials that can be loaded with
192210 :func:`google.auth.load_credentials_from_file`. This argument is
193211 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`.
194215 scopes (Sequence[str]): A optional list of scopes needed for this
195216 service. These are only used when credentials are not specified and
196217 are passed to :func:`google.auth.default`.
197218 ssl_credentials (grpc.ChannelCredentials): Optional SSL channel
198219 credentials. This can be used to specify different certificates.
199220 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".
200222
201223 Returns:
202224 grpc.ChannelCredentials: The composed channel credentials object.
@@ -210,21 +232,55 @@ def _create_composite_credentials(
210232 )
211233
212234 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+ )
214247 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+
216261 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 )
218267
219268 if quota_project_id and isinstance (credentials , google .auth .credentials .CredentialsWithQuotaProject ):
220269 credentials = credentials .with_quota_project (quota_project_id )
221270
222271 request = google .auth .transport .requests .Request ()
223272
224273 # 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+ )
228284
229285 # Create a set of grpc.CallCredentials using the metadata plugin.
230286 google_auth_credentials = grpc .metadata_call_credentials (metadata_plugin )
@@ -245,6 +301,8 @@ def create_channel(
245301 ssl_credentials = None ,
246302 credentials_file = None ,
247303 quota_project_id = None ,
304+ default_scopes = None ,
305+ default_host = None ,
248306 ** kwargs ):
249307 """Create a secure channel with credentials.
250308
@@ -262,6 +320,9 @@ def create_channel(
262320 :func:`google.auth.load_credentials_from_file`. This argument is
263321 mutually exclusive with credentials.
264322 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".
265326 kwargs: Additional key-word args passed to
266327 :func:`grpc_gcp.secure_channel` or :func:`grpc.secure_channel`.
267328
@@ -275,9 +336,11 @@ def create_channel(
275336 composite_credentials = _create_composite_credentials (
276337 credentials = credentials ,
277338 credentials_file = credentials_file ,
339+ default_scopes = default_scopes ,
278340 scopes = scopes ,
279341 ssl_credentials = ssl_credentials ,
280342 quota_project_id = quota_project_id ,
343+ default_host = default_host ,
281344 )
282345
283346 if HAS_GRPC_GCP :
0 commit comments