From 8f6927ae2692ec94fc70389df391abd48e867b20 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Wed, 19 May 2021 23:23:50 +0000 Subject: [PATCH] fix: require google-auth>=1.25.0 --- google/api_core/grpc_helpers.py | 60 ++++------------- setup.py | 2 +- testing/constraints-3.6.txt | 2 +- tests/asyncio/test_grpc_helpers_async.py | 86 ++++-------------------- tests/unit/test_grpc_helpers.py | 84 +++++------------------ 5 files changed, 44 insertions(+), 190 deletions(-) diff --git a/google/api_core/grpc_helpers.py b/google/api_core/grpc_helpers.py index 5937f186..e8f9ee8a 100644 --- a/google/api_core/grpc_helpers.py +++ b/google/api_core/grpc_helpers.py @@ -17,7 +17,6 @@ import collections import grpc -from packaging import version import pkg_resources import six @@ -44,11 +43,6 @@ except pkg_resources.DistributionNotFound: # pragma: NO COVER _GOOGLE_AUTH_VERSION = None -if _GOOGLE_AUTH_VERSION is not None and version.parse(_GOOGLE_AUTH_VERSION) >= version.parse("1.25.0"): - _GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST = True -else: - _GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST = False - # The list of gRPC Callable interfaces that return iterators. _STREAM_WRAP_CLASSES = (grpc.UnaryStreamMultiCallable, grpc.StreamStreamMultiCallable) @@ -232,38 +226,19 @@ def _create_composite_credentials( ) if credentials_file: - # TODO: remove this if/else once google-auth >= 1.25.0 is required - if _GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST: - credentials, _ = google.auth.load_credentials_from_file( - credentials_file, - scopes=scopes, - default_scopes=default_scopes - ) - else: - credentials, _ = google.auth.load_credentials_from_file( - credentials_file, - scopes=scopes or default_scopes, - ) + credentials, _ = google.auth.load_credentials_from_file( + credentials_file, + scopes=scopes, + default_scopes=default_scopes + ) elif credentials: - # TODO: remove this if/else once google-auth >= 1.25.0 is required - if _GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST: - credentials = google.auth.credentials.with_scopes_if_required( - credentials, - scopes=scopes, - default_scopes=default_scopes - ) - else: - credentials = google.auth.credentials.with_scopes_if_required( - credentials, - scopes=scopes or default_scopes, - ) - + credentials = google.auth.credentials.with_scopes_if_required( + credentials, + scopes=scopes, + default_scopes=default_scopes + ) else: - # TODO: remove this if/else once google-auth >= 1.25.0 is required - if _GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST: - credentials, _ = google.auth.default(scopes=scopes, default_scopes=default_scopes) - else: - credentials, _ = google.auth.default(scopes=scopes or default_scopes) + credentials, _ = google.auth.default(scopes=scopes, default_scopes=default_scopes) if quota_project_id and isinstance(credentials, google.auth.credentials.CredentialsWithQuotaProject): credentials = credentials.with_quota_project(quota_project_id) @@ -271,16 +246,9 @@ def _create_composite_credentials( request = google.auth.transport.requests.Request() # Create the metadata plugin for inserting the authorization header. - - # TODO: remove this if/else once google-auth >= 1.25.0 is required - if _GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST: - metadata_plugin = google.auth.transport.grpc.AuthMetadataPlugin( - credentials, request, default_host=default_host, - ) - else: - metadata_plugin = google.auth.transport.grpc.AuthMetadataPlugin( - credentials, request - ) + metadata_plugin = google.auth.transport.grpc.AuthMetadataPlugin( + credentials, request, default_host=default_host, + ) # Create a set of grpc.CallCredentials using the metadata plugin. google_auth_credentials = grpc.metadata_call_credentials(metadata_plugin) diff --git a/setup.py b/setup.py index 5de5aafb..26fab7e6 100644 --- a/setup.py +++ b/setup.py @@ -31,7 +31,7 @@ dependencies = [ "googleapis-common-protos >= 1.6.0, < 2.0dev", "protobuf >= 3.12.0", - "google-auth >= 1.21.1, < 2.0dev", + "google-auth >= 1.25.0, < 2.0dev", "requests >= 2.18.0, < 3.0.0dev", "setuptools >= 40.3.0", "packaging >= 14.3", diff --git a/testing/constraints-3.6.txt b/testing/constraints-3.6.txt index 1fcd1934..ff5b4a7f 100644 --- a/testing/constraints-3.6.txt +++ b/testing/constraints-3.6.txt @@ -7,7 +7,7 @@ # Then this file should have foo==1.14.0 googleapis-common-protos==1.6.0 protobuf==3.12.0 -google-auth==1.21.1 +google-auth==1.25.0 requests==2.18.0 setuptools==40.3.0 packaging==14.3 diff --git a/tests/asyncio/test_grpc_helpers_async.py b/tests/asyncio/test_grpc_helpers_async.py index 3461cbe8..868018cf 100644 --- a/tests/asyncio/test_grpc_helpers_async.py +++ b/tests/asyncio/test_grpc_helpers_async.py @@ -18,7 +18,6 @@ import pytest from google.api_core import exceptions -from google.api_core import grpc_helpers from google.api_core import grpc_helpers_async import google.auth.credentials @@ -276,11 +275,7 @@ def test_create_channel_implicit(grpc_secure_channel, default, composite_creds_c assert channel is grpc_secure_channel.return_value - # TODO: remove this if/else once google-auth >= 1.25.0 is required - if grpc_helpers._GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST: - default.assert_called_once_with(scopes=None, default_scopes=None) - else: - default.assert_called_once_with(scopes=None) + default.assert_called_once_with(scopes=None, default_scopes=None) grpc_secure_channel.assert_called_once_with(target, composite_creds) @@ -306,14 +301,8 @@ def test_create_channel_implicit_with_default_host(grpc_secure_channel, default, assert channel is grpc_secure_channel.return_value - # TODO: remove this if/else once google-auth >= 1.25.0 is required - if grpc_helpers._GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST: - default.assert_called_once_with(scopes=None, default_scopes=None) - auth_metadata_plugin.assert_called_once_with(mock.sentinel.credentials, mock.sentinel.Request, default_host=default_host) - else: - default.assert_called_once_with(scopes=None) - auth_metadata_plugin.assert_called_once_with(mock.sentinel.credentials, mock.sentinel.Request) - + default.assert_called_once_with(scopes=None, default_scopes=None) + auth_metadata_plugin.assert_called_once_with(mock.sentinel.credentials, mock.sentinel.Request, default_host=default_host) grpc_secure_channel.assert_called_once_with(target, composite_creds) @@ -332,12 +321,7 @@ def test_create_channel_implicit_with_ssl_creds( grpc_helpers_async.create_channel(target, ssl_credentials=ssl_creds) - # TODO: remove this if/else once google-auth >= 1.25.0 is required - if grpc_helpers._GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST: - default.assert_called_once_with(scopes=None, default_scopes=None) - else: - default.assert_called_once_with(scopes=None) - + default.assert_called_once_with(scopes=None, default_scopes=None) composite_creds_call.assert_called_once_with(ssl_creds, mock.ANY) composite_creds = composite_creds_call.return_value grpc_secure_channel.assert_called_once_with(target, composite_creds) @@ -360,12 +344,7 @@ def test_create_channel_implicit_with_scopes( assert channel is grpc_secure_channel.return_value - # TODO: remove this if/else once google-auth >= 1.25.0 is required - if grpc_helpers._GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST: - default.assert_called_once_with(scopes=["one", "two"], default_scopes=None) - else: - default.assert_called_once_with(scopes=["one", "two"]) - + default.assert_called_once_with(scopes=["one", "two"], default_scopes=None) grpc_secure_channel.assert_called_once_with(target, composite_creds) @@ -386,12 +365,7 @@ def test_create_channel_implicit_with_default_scopes( assert channel is grpc_secure_channel.return_value - # TODO: remove this if/else once google-auth >= 1.25.0 is required - if grpc_helpers._GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST: - default.assert_called_once_with(scopes=None, default_scopes=["three", "four"]) - else: - default.assert_called_once_with(scopes=["three", "four"]) - + default.assert_called_once_with(scopes=None, default_scopes=["three", "four"]) grpc_secure_channel.assert_called_once_with(target, composite_creds) @@ -417,12 +391,7 @@ def test_create_channel_explicit(grpc_secure_channel, auth_creds, composite_cred channel = grpc_helpers_async.create_channel(target, credentials=mock.sentinel.credentials) - # TODO: remove this if/else once google-auth >= 1.25.0 is required - if grpc_helpers._GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST: - auth_creds.assert_called_once_with(mock.sentinel.credentials, scopes=None, default_scopes=None) - else: - auth_creds.assert_called_once_with(mock.sentinel.credentials, scopes=None) - + auth_creds.assert_called_once_with(mock.sentinel.credentials, scopes=None, default_scopes=None) assert channel is grpc_secure_channel.return_value grpc_secure_channel.assert_called_once_with(target, composite_creds) @@ -440,12 +409,8 @@ def test_create_channel_explicit_scoped(grpc_secure_channel, composite_creds_cal channel = grpc_helpers_async.create_channel( target, credentials=credentials, scopes=scopes ) - # TODO: remove this if/else once google-auth >= 1.25.0 is required - if grpc_helpers._GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST: - credentials.with_scopes.assert_called_once_with(scopes, default_scopes=None) - else: - credentials.with_scopes.assert_called_once_with(scopes) + credentials.with_scopes.assert_called_once_with(scopes, default_scopes=None) assert channel is grpc_secure_channel.return_value grpc_secure_channel.assert_called_once_with(target, composite_creds) @@ -464,12 +429,7 @@ def test_create_channel_explicit_default_scopes(grpc_secure_channel, composite_c target, credentials=credentials, default_scopes=default_scopes ) - # TODO: remove this if/else once google-auth >= 1.25.0 is required - if grpc_helpers._GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST: - credentials.with_scopes.assert_called_once_with(scopes=None, default_scopes=default_scopes) - else: - credentials.with_scopes.assert_called_once_with(scopes=default_scopes) - + credentials.with_scopes.assert_called_once_with(scopes=None, default_scopes=default_scopes) assert channel is grpc_secure_channel.return_value grpc_secure_channel.assert_called_once_with(target, composite_creds) @@ -508,12 +468,7 @@ def test_create_channnel_with_credentials_file(load_credentials_from_file, grpc_ target, credentials_file=credentials_file ) - # TODO: remove this if/else once google-auth >= 1.25.0 is required - if grpc_helpers._GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST: - google.auth.load_credentials_from_file.assert_called_once_with(credentials_file, scopes=None, default_scopes=None) - else: - google.auth.load_credentials_from_file.assert_called_once_with(credentials_file, scopes=None) - + google.auth.load_credentials_from_file.assert_called_once_with(credentials_file, scopes=None, default_scopes=None) assert channel is grpc_secure_channel.return_value grpc_secure_channel.assert_called_once_with(target, composite_creds) @@ -536,12 +491,7 @@ def test_create_channel_with_credentials_file_and_scopes(load_credentials_from_f target, credentials_file=credentials_file, scopes=scopes ) - # TODO: remove this if/else once google-auth >= 1.25.0 is required - if grpc_helpers._GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST: - google.auth.load_credentials_from_file.assert_called_once_with(credentials_file, scopes=scopes, default_scopes=None) - else: - google.auth.load_credentials_from_file.assert_called_once_with(credentials_file, scopes=scopes) - + google.auth.load_credentials_from_file.assert_called_once_with(credentials_file, scopes=scopes, default_scopes=None) assert channel is grpc_secure_channel.return_value grpc_secure_channel.assert_called_once_with(target, composite_creds) @@ -564,12 +514,7 @@ def test_create_channel_with_credentials_file_and_default_scopes(load_credential target, credentials_file=credentials_file, default_scopes=default_scopes ) - # TODO: remove this if/else once google-auth >= 1.25.0 is required - if grpc_helpers._GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST: - google.auth.load_credentials_from_file.assert_called_once_with(credentials_file, scopes=None, default_scopes=default_scopes) - else: - google.auth.load_credentials_from_file.assert_called_once_with(credentials_file, scopes=default_scopes) - + google.auth.load_credentials_from_file.assert_called_once_with(credentials_file, scopes=None, default_scopes=default_scopes) assert channel is grpc_secure_channel.return_value grpc_secure_channel.assert_called_once_with(target, composite_creds) @@ -585,12 +530,7 @@ def test_create_channel_without_grpc_gcp(grpc_secure_channel): grpc_helpers_async.create_channel(target, credentials=credentials, scopes=scopes) grpc_secure_channel.assert_called() - - # TODO: remove this if/else once google-auth >= 1.25.0 is required - if grpc_helpers._GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST: - credentials.with_scopes.assert_called_once_with(scopes, default_scopes=None) - else: - credentials.with_scopes.assert_called_once_with(scopes) + credentials.with_scopes.assert_called_once_with(scopes, default_scopes=None) @pytest.mark.asyncio diff --git a/tests/unit/test_grpc_helpers.py b/tests/unit/test_grpc_helpers.py index 4e0ab806..8c6202bc 100644 --- a/tests/unit/test_grpc_helpers.py +++ b/tests/unit/test_grpc_helpers.py @@ -234,11 +234,7 @@ def test_create_channel_implicit(grpc_secure_channel, default, composite_creds_c assert channel is grpc_secure_channel.return_value - # TODO: remove this if/else once google-auth >= 1.25.0 is required - if grpc_helpers._GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST: - default.assert_called_once_with(scopes=None, default_scopes=None) - else: - default.assert_called_once_with(scopes=None) + default.assert_called_once_with(scopes=None, default_scopes=None) if grpc_helpers.HAS_GRPC_GCP: grpc_secure_channel.assert_called_once_with(target, composite_creds, None) @@ -268,15 +264,8 @@ def test_create_channel_implicit_with_default_host(grpc_secure_channel, default, assert channel is grpc_secure_channel.return_value - # TODO: remove this if/else once google-auth >= 1.25.0 is required - print(grpc_helpers._GOOGLE_AUTH_VERSION) - print(grpc_helpers._GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST) - if grpc_helpers._GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST: - default.assert_called_once_with(scopes=None, default_scopes=None) - auth_metadata_plugin.assert_called_once_with(mock.sentinel.credentials, mock.sentinel.Request, default_host=default_host) - else: - default.assert_called_once_with(scopes=None) - auth_metadata_plugin.assert_called_once_with(mock.sentinel.credentials, mock.sentinel.Request) + default.assert_called_once_with(scopes=None, default_scopes=None) + auth_metadata_plugin.assert_called_once_with(mock.sentinel.credentials, mock.sentinel.Request, default_host=default_host) if grpc_helpers.HAS_GRPC_GCP: grpc_secure_channel.assert_called_once_with(target, composite_creds, None) @@ -300,11 +289,7 @@ def test_create_channel_implicit_with_ssl_creds( grpc_helpers.create_channel(target, ssl_credentials=ssl_creds) - # TODO: remove this if/else once google-auth >= 1.25.0 is required - if grpc_helpers._GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST: - default.assert_called_once_with(scopes=None, default_scopes=None) - else: - default.assert_called_once_with(scopes=None) + default.assert_called_once_with(scopes=None, default_scopes=None) composite_creds_call.assert_called_once_with(ssl_creds, mock.ANY) composite_creds = composite_creds_call.return_value @@ -331,11 +316,7 @@ def test_create_channel_implicit_with_scopes( assert channel is grpc_secure_channel.return_value - # TODO: remove this if/else once google-auth >= 1.25.0 is required - if grpc_helpers._GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST: - default.assert_called_once_with(scopes=["one", "two"], default_scopes=None) - else: - default.assert_called_once_with(scopes=["one", "two"]) + default.assert_called_once_with(scopes=["one", "two"], default_scopes=None) if grpc_helpers.HAS_GRPC_GCP: grpc_secure_channel.assert_called_once_with(target, composite_creds, None) @@ -360,11 +341,7 @@ def test_create_channel_implicit_with_default_scopes( assert channel is grpc_secure_channel.return_value - # TODO: remove this if/else once google-auth >= 1.25.0 is required - if grpc_helpers._GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST: - default.assert_called_once_with(scopes=None, default_scopes=["three", "four"]) - else: - default.assert_called_once_with(scopes=["three", "four"]) + default.assert_called_once_with(scopes=None, default_scopes=["three", "four"]) if grpc_helpers.HAS_GRPC_GCP: grpc_secure_channel.assert_called_once_with(target, composite_creds, None) @@ -392,11 +369,8 @@ def test_create_channel_explicit(grpc_secure_channel, auth_creds, composite_cred channel = grpc_helpers.create_channel(target, credentials=mock.sentinel.credentials) - # TODO: remove this if/else once google-auth >= 1.25.0 is required - if grpc_helpers._GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST: - auth_creds.assert_called_once_with(mock.sentinel.credentials, scopes=None, default_scopes=None) - else: - auth_creds.assert_called_once_with(mock.sentinel.credentials, scopes=None) + auth_creds.assert_called_once_with(mock.sentinel.credentials, scopes=None, default_scopes=None) + assert channel is grpc_secure_channel.return_value if grpc_helpers.HAS_GRPC_GCP: grpc_secure_channel.assert_called_once_with(target, composite_creds, None) @@ -418,11 +392,7 @@ def test_create_channel_explicit_scoped(grpc_secure_channel, composite_creds_cal target, credentials=credentials, scopes=scopes ) - # TODO: remove this if/else once google-auth >= 1.25.0 is required - if grpc_helpers._GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST: - credentials.with_scopes.assert_called_once_with(scopes, default_scopes=None) - else: - credentials.with_scopes.assert_called_once_with(scopes) + credentials.with_scopes.assert_called_once_with(scopes, default_scopes=None) assert channel is grpc_secure_channel.return_value if grpc_helpers.HAS_GRPC_GCP: @@ -445,11 +415,7 @@ def test_create_channel_explicit_default_scopes(grpc_secure_channel, composite_c target, credentials=credentials, default_scopes=default_scopes ) - # TODO: remove this if/else once google-auth >= 1.25.0 is required - if grpc_helpers._GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST: - credentials.with_scopes.assert_called_once_with(scopes=None, default_scopes=default_scopes) - else: - credentials.with_scopes.assert_called_once_with(scopes=default_scopes) + credentials.with_scopes.assert_called_once_with(scopes=None, default_scopes=default_scopes) assert channel is grpc_secure_channel.return_value if grpc_helpers.HAS_GRPC_GCP: @@ -498,11 +464,7 @@ def test_create_channel_with_credentials_file(load_credentials_from_file, grpc_s target, credentials_file=credentials_file ) - # TODO: remove this if/else once google-auth >= 1.25.0 is required - if grpc_helpers._GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST: - google.auth.load_credentials_from_file.assert_called_once_with(credentials_file, scopes=None, default_scopes=None) - else: - google.auth.load_credentials_from_file.assert_called_once_with(credentials_file, scopes=None) + google.auth.load_credentials_from_file.assert_called_once_with(credentials_file, scopes=None, default_scopes=None) assert channel is grpc_secure_channel.return_value if grpc_helpers.HAS_GRPC_GCP: @@ -529,11 +491,7 @@ def test_create_channel_with_credentials_file_and_scopes(load_credentials_from_f target, credentials_file=credentials_file, scopes=scopes ) - # TODO: remove this if/else once google-auth >= 1.25.0 is required - if grpc_helpers._GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST: - google.auth.load_credentials_from_file.assert_called_once_with(credentials_file, scopes=scopes, default_scopes=None) - else: - google.auth.load_credentials_from_file.assert_called_once_with(credentials_file, scopes=scopes) + google.auth.load_credentials_from_file.assert_called_once_with(credentials_file, scopes=scopes, default_scopes=None) assert channel is grpc_secure_channel.return_value if grpc_helpers.HAS_GRPC_GCP: @@ -560,11 +518,7 @@ def test_create_channel_with_credentials_file_and_default_scopes(load_credential target, credentials_file=credentials_file, default_scopes=default_scopes ) - # TODO: remove this if/else once google-auth >= 1.25.0 is required - if grpc_helpers._GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST: - load_credentials_from_file.assert_called_once_with(credentials_file, scopes=None, default_scopes=default_scopes) - else: - load_credentials_from_file.assert_called_once_with(credentials_file, scopes=default_scopes) + load_credentials_from_file.assert_called_once_with(credentials_file, scopes=None, default_scopes=default_scopes) assert channel is grpc_secure_channel.return_value if grpc_helpers.HAS_GRPC_GCP: @@ -587,11 +541,7 @@ def test_create_channel_with_grpc_gcp(grpc_gcp_secure_channel): grpc_helpers.create_channel(target, credentials=credentials, scopes=scopes) grpc_gcp_secure_channel.assert_called() - # TODO: remove this if/else once google-auth >= 1.25.0 is required - if grpc_helpers._GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST: - credentials.with_scopes.assert_called_once_with(scopes, default_scopes=None) - else: - credentials.with_scopes.assert_called_once_with(scopes) + credentials.with_scopes.assert_called_once_with(scopes, default_scopes=None) @pytest.mark.skipif(grpc_helpers.HAS_GRPC_GCP, reason="grpc_gcp module not available") @@ -606,11 +556,7 @@ def test_create_channel_without_grpc_gcp(grpc_secure_channel): grpc_helpers.create_channel(target, credentials=credentials, scopes=scopes) grpc_secure_channel.assert_called() - # TODO: remove this if/else once google-auth >= 1.25.0 is required - if grpc_helpers._GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST: - credentials.with_scopes.assert_called_once_with(scopes, default_scopes=None) - else: - credentials.with_scopes.assert_called_once_with(scopes) + credentials.with_scopes.assert_called_once_with(scopes, default_scopes=None) class TestChannelStub(object):