Skip to content
This repository was archived by the owner on Jul 6, 2023. It is now read-only.

Commit 45621e5

Browse files
feat: add always_use_jwt_access (#64)
... chore: update gapic-generator-ruby to the latest commit chore: release gapic-generator-typescript 1.5.0 Committer: @miraleung PiperOrigin-RevId: 380641501 Source-Link: googleapis/googleapis@076f7e9 Source-Link: https://github.com/googleapis/googleapis-gen/commit/27e4c88b4048e5f56508d4e1aa417d60a3380892
1 parent 2802e46 commit 45621e5

File tree

6 files changed

+39
-123
lines changed

6 files changed

+39
-123
lines changed

google/cloud/channel_v1/services/cloud_channel_service/transports/base.py

+14-26
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from google.api_core import retry as retries # type: ignore
2626
from google.api_core import operations_v1 # type: ignore
2727
from google.auth import credentials as ga_credentials # type: ignore
28+
from google.oauth2 import service_account # type: ignore
2829

2930
from google.cloud.channel_v1.types import channel_partner_links
3031
from google.cloud.channel_v1.types import customers
@@ -50,8 +51,6 @@
5051
except pkg_resources.DistributionNotFound: # pragma: NO COVER
5152
_GOOGLE_AUTH_VERSION = None
5253

53-
_API_CORE_VERSION = google.api_core.__version__
54-
5554

5655
class CloudChannelServiceTransport(abc.ABC):
5756
"""Abstract transport class for CloudChannelService."""
@@ -69,6 +68,7 @@ def __init__(
6968
scopes: Optional[Sequence[str]] = None,
7069
quota_project_id: Optional[str] = None,
7170
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
71+
always_use_jwt_access: Optional[bool] = False,
7272
**kwargs,
7373
) -> None:
7474
"""Instantiate the transport.
@@ -92,6 +92,8 @@ def __init__(
9292
API requests. If ``None``, then default info will be used.
9393
Generally, you only need to set this if you're developing
9494
your own client library.
95+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
96+
be used for service account credentials.
9597
"""
9698
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
9799
if ":" not in host:
@@ -120,13 +122,20 @@ def __init__(
120122
**scopes_kwargs, quota_project_id=quota_project_id
121123
)
122124

125+
# If the credentials is service account credentials, then always try to use self signed JWT.
126+
if (
127+
always_use_jwt_access
128+
and isinstance(credentials, service_account.Credentials)
129+
and hasattr(service_account.Credentials, "with_always_use_jwt_access")
130+
):
131+
credentials = credentials.with_always_use_jwt_access(True)
132+
123133
# Save the credentials.
124134
self._credentials = credentials
125135

126-
# TODO(busunkim): These two class methods are in the base transport
136+
# TODO(busunkim): This method is in the base transport
127137
# to avoid duplicating code across the transport classes. These functions
128-
# should be deleted once the minimum required versions of google-api-core
129-
# and google-auth are increased.
138+
# should be deleted once the minimum required versions of google-auth is increased.
130139

131140
# TODO: Remove this function once google-auth >= 1.25.0 is required
132141
@classmethod
@@ -147,27 +156,6 @@ def _get_scopes_kwargs(
147156

148157
return scopes_kwargs
149158

150-
# TODO: Remove this function once google-api-core >= 1.26.0 is required
151-
@classmethod
152-
def _get_self_signed_jwt_kwargs(
153-
cls, host: str, scopes: Optional[Sequence[str]]
154-
) -> Dict[str, Union[Optional[Sequence[str]], str]]:
155-
"""Returns kwargs to pass to grpc_helpers.create_channel depending on the google-api-core version"""
156-
157-
self_signed_jwt_kwargs: Dict[str, Union[Optional[Sequence[str]], str]] = {}
158-
159-
if _API_CORE_VERSION and (
160-
packaging.version.parse(_API_CORE_VERSION)
161-
>= packaging.version.parse("1.26.0")
162-
):
163-
self_signed_jwt_kwargs["default_scopes"] = cls.AUTH_SCOPES
164-
self_signed_jwt_kwargs["scopes"] = scopes
165-
self_signed_jwt_kwargs["default_host"] = cls.DEFAULT_HOST
166-
else:
167-
self_signed_jwt_kwargs["scopes"] = scopes or cls.AUTH_SCOPES
168-
169-
return self_signed_jwt_kwargs
170-
171159
def _prep_wrapped_messages(self, client_info):
172160
# Precompute the wrapped methods.
173161
self._wrapped_methods = {

google/cloud/channel_v1/services/cloud_channel_service/transports/grpc.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ def __init__(
179179
scopes=scopes,
180180
quota_project_id=quota_project_id,
181181
client_info=client_info,
182+
always_use_jwt_access=True,
182183
)
183184

184185
if not self._grpc_channel:
@@ -234,14 +235,14 @@ def create_channel(
234235
and ``credentials_file`` are passed.
235236
"""
236237

237-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
238-
239238
return grpc_helpers.create_channel(
240239
host,
241240
credentials=credentials,
242241
credentials_file=credentials_file,
243242
quota_project_id=quota_project_id,
244-
**self_signed_jwt_kwargs,
243+
default_scopes=cls.AUTH_SCOPES,
244+
scopes=scopes,
245+
default_host=cls.DEFAULT_HOST,
245246
**kwargs,
246247
)
247248

google/cloud/channel_v1/services/cloud_channel_service/transports/grpc_asyncio.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,14 @@ def create_channel(
107107
aio.Channel: A gRPC AsyncIO channel object.
108108
"""
109109

110-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
111-
112110
return grpc_helpers_async.create_channel(
113111
host,
114112
credentials=credentials,
115113
credentials_file=credentials_file,
116114
quota_project_id=quota_project_id,
117-
**self_signed_jwt_kwargs,
115+
default_scopes=cls.AUTH_SCOPES,
116+
scopes=scopes,
117+
default_host=cls.DEFAULT_HOST,
118118
**kwargs,
119119
)
120120

@@ -225,6 +225,7 @@ def __init__(
225225
scopes=scopes,
226226
quota_project_id=quota_project_id,
227227
client_info=client_info,
228+
always_use_jwt_access=True,
228229
)
229230

230231
if not self._grpc_channel:

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
platforms="Posix; MacOS X; Windows",
4646
include_package_data=True,
4747
install_requires=(
48-
"google-api-core[grpc] >= 1.22.2, < 2.0.0dev",
48+
"google-api-core[grpc] >= 1.26.0, <2.0.0dev",
4949
"proto-plus >= 1.4.0",
5050
"packaging >= 14.3",
5151
),

testing/constraints-3.6.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
#
2020
# e.g., if setup.py has "foo >= 1.14.0, < 2.0.0dev",
2121
# Then this file should have foo==1.14.0
22-
google-api-core==1.22.2
22+
google-api-core==1.26.0
2323
proto-plus==1.4.0
2424
packaging==14.3

tests/unit/gapic/channel_v1/test_cloud_channel_service.py

+15-89
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@
4242
)
4343
from google.cloud.channel_v1.services.cloud_channel_service import pagers
4444
from google.cloud.channel_v1.services.cloud_channel_service import transports
45-
from google.cloud.channel_v1.services.cloud_channel_service.transports.base import (
46-
_API_CORE_VERSION,
47-
)
4845
from google.cloud.channel_v1.services.cloud_channel_service.transports.base import (
4946
_GOOGLE_AUTH_VERSION,
5047
)
@@ -65,8 +62,9 @@
6562
import google.auth
6663

6764

68-
# TODO(busunkim): Once google-api-core >= 1.26.0 is required:
69-
# - Delete all the api-core and auth "less than" test cases
65+
# TODO(busunkim): Once google-auth >= 1.25.0 is required transitively
66+
# through google-api-core:
67+
# - Delete the auth "less than" test cases
7068
# - Delete these pytest markers (Make the "greater than or equal to" tests the default).
7169
requires_google_auth_lt_1_25_0 = pytest.mark.skipif(
7270
packaging.version.parse(_GOOGLE_AUTH_VERSION) >= packaging.version.parse("1.25.0"),
@@ -77,16 +75,6 @@
7775
reason="This test requires google-auth >= 1.25.0",
7876
)
7977

80-
requires_api_core_lt_1_26_0 = pytest.mark.skipif(
81-
packaging.version.parse(_API_CORE_VERSION) >= packaging.version.parse("1.26.0"),
82-
reason="This test requires google-api-core < 1.26.0",
83-
)
84-
85-
requires_api_core_gte_1_26_0 = pytest.mark.skipif(
86-
packaging.version.parse(_API_CORE_VERSION) < packaging.version.parse("1.26.0"),
87-
reason="This test requires google-api-core >= 1.26.0",
88-
)
89-
9078

9179
def client_cert_source_callback():
9280
return b"cert bytes", b"key bytes"
@@ -150,6 +138,18 @@ def test_cloud_channel_service_client_from_service_account_info(client_class):
150138
assert client.transport._host == "cloudchannel.googleapis.com:443"
151139

152140

141+
@pytest.mark.parametrize(
142+
"client_class", [CloudChannelServiceClient, CloudChannelServiceAsyncClient,]
143+
)
144+
def test_cloud_channel_service_client_service_account_always_use_jwt(client_class):
145+
with mock.patch.object(
146+
service_account.Credentials, "with_always_use_jwt_access", create=True
147+
) as use_jwt:
148+
creds = service_account.Credentials(None, None, None)
149+
client = client_class(credentials=creds)
150+
use_jwt.assert_called_with(True)
151+
152+
153153
@pytest.mark.parametrize(
154154
"client_class", [CloudChannelServiceClient, CloudChannelServiceAsyncClient,]
155155
)
@@ -7508,7 +7508,6 @@ def test_cloud_channel_service_transport_auth_adc_old_google_auth(transport_clas
75087508
(transports.CloudChannelServiceGrpcAsyncIOTransport, grpc_helpers_async),
75097509
],
75107510
)
7511-
@requires_api_core_gte_1_26_0
75127511
def test_cloud_channel_service_transport_create_channel(transport_class, grpc_helpers):
75137512
# If credentials and host are not provided, the transport class should use
75147513
# ADC credentials.
@@ -7537,79 +7536,6 @@ def test_cloud_channel_service_transport_create_channel(transport_class, grpc_he
75377536
)
75387537

75397538

7540-
@pytest.mark.parametrize(
7541-
"transport_class,grpc_helpers",
7542-
[
7543-
(transports.CloudChannelServiceGrpcTransport, grpc_helpers),
7544-
(transports.CloudChannelServiceGrpcAsyncIOTransport, grpc_helpers_async),
7545-
],
7546-
)
7547-
@requires_api_core_lt_1_26_0
7548-
def test_cloud_channel_service_transport_create_channel_old_api_core(
7549-
transport_class, grpc_helpers
7550-
):
7551-
# If credentials and host are not provided, the transport class should use
7552-
# ADC credentials.
7553-
with mock.patch.object(
7554-
google.auth, "default", autospec=True
7555-
) as adc, mock.patch.object(
7556-
grpc_helpers, "create_channel", autospec=True
7557-
) as create_channel:
7558-
creds = ga_credentials.AnonymousCredentials()
7559-
adc.return_value = (creds, None)
7560-
transport_class(quota_project_id="octopus")
7561-
7562-
create_channel.assert_called_with(
7563-
"cloudchannel.googleapis.com:443",
7564-
credentials=creds,
7565-
credentials_file=None,
7566-
quota_project_id="octopus",
7567-
scopes=("https://www.googleapis.com/auth/apps.order",),
7568-
ssl_credentials=None,
7569-
options=[
7570-
("grpc.max_send_message_length", -1),
7571-
("grpc.max_receive_message_length", -1),
7572-
],
7573-
)
7574-
7575-
7576-
@pytest.mark.parametrize(
7577-
"transport_class,grpc_helpers",
7578-
[
7579-
(transports.CloudChannelServiceGrpcTransport, grpc_helpers),
7580-
(transports.CloudChannelServiceGrpcAsyncIOTransport, grpc_helpers_async),
7581-
],
7582-
)
7583-
@requires_api_core_lt_1_26_0
7584-
def test_cloud_channel_service_transport_create_channel_user_scopes(
7585-
transport_class, grpc_helpers
7586-
):
7587-
# If credentials and host are not provided, the transport class should use
7588-
# ADC credentials.
7589-
with mock.patch.object(
7590-
google.auth, "default", autospec=True
7591-
) as adc, mock.patch.object(
7592-
grpc_helpers, "create_channel", autospec=True
7593-
) as create_channel:
7594-
creds = ga_credentials.AnonymousCredentials()
7595-
adc.return_value = (creds, None)
7596-
7597-
transport_class(quota_project_id="octopus", scopes=["1", "2"])
7598-
7599-
create_channel.assert_called_with(
7600-
"cloudchannel.googleapis.com:443",
7601-
credentials=creds,
7602-
credentials_file=None,
7603-
quota_project_id="octopus",
7604-
scopes=["1", "2"],
7605-
ssl_credentials=None,
7606-
options=[
7607-
("grpc.max_send_message_length", -1),
7608-
("grpc.max_receive_message_length", -1),
7609-
],
7610-
)
7611-
7612-
76137539
@pytest.mark.parametrize(
76147540
"transport_class",
76157541
[

0 commit comments

Comments
 (0)