Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: require google-auth>=1.25.0 #190

Merged
merged 1 commit into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 14 additions & 46 deletions google/api_core/grpc_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import collections

import grpc
from packaging import version
import pkg_resources
import six

Expand All @@ -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)

Expand Down Expand Up @@ -232,55 +226,29 @@ 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)

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)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion testing/constraints-3.6.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
86 changes: 13 additions & 73 deletions tests/asyncio/test_grpc_helpers_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)


Expand All @@ -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)


Expand All @@ -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)
Expand All @@ -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)


Expand All @@ -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)


Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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
Expand Down
Loading