Skip to content
This repository was archived by the owner on Jan 17, 2024. It is now read-only.

Commit a907b7d

Browse files
feat: add always_use_jwt_access (#80)
... 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 2417d5a commit a907b7d

File tree

6 files changed

+39
-128
lines changed

6 files changed

+39
-128
lines changed

google/cloud/monitoring_dashboard_v1/services/dashboards_service/transports/base.py

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from google.api_core import gapic_v1 # type: ignore
2525
from google.api_core import retry as retries # type: ignore
2626
from google.auth import credentials as ga_credentials # type: ignore
27+
from google.oauth2 import service_account # type: ignore
2728

2829
from google.cloud.monitoring_dashboard_v1.types import dashboard
2930
from google.cloud.monitoring_dashboard_v1.types import dashboards_service
@@ -47,8 +48,6 @@
4748
except pkg_resources.DistributionNotFound: # pragma: NO COVER
4849
_GOOGLE_AUTH_VERSION = None
4950

50-
_API_CORE_VERSION = google.api_core.__version__
51-
5251

5352
class DashboardsServiceTransport(abc.ABC):
5453
"""Abstract transport class for DashboardsService."""
@@ -71,6 +70,7 @@ def __init__(
7170
scopes: Optional[Sequence[str]] = None,
7271
quota_project_id: Optional[str] = None,
7372
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
73+
always_use_jwt_access: Optional[bool] = False,
7474
**kwargs,
7575
) -> None:
7676
"""Instantiate the transport.
@@ -94,6 +94,8 @@ def __init__(
9494
API requests. If ``None``, then default info will be used.
9595
Generally, you only need to set this if you're developing
9696
your own client library.
97+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
98+
be used for service account credentials.
9799
"""
98100
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
99101
if ":" not in host:
@@ -122,13 +124,20 @@ def __init__(
122124
**scopes_kwargs, quota_project_id=quota_project_id
123125
)
124126

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

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

133142
# TODO: Remove this function once google-auth >= 1.25.0 is required
134143
@classmethod
@@ -149,27 +158,6 @@ def _get_scopes_kwargs(
149158

150159
return scopes_kwargs
151160

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

google/cloud/monitoring_dashboard_v1/services/dashboards_service/transports/grpc.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ def __init__(
152152
scopes=scopes,
153153
quota_project_id=quota_project_id,
154154
client_info=client_info,
155+
always_use_jwt_access=True,
155156
)
156157

157158
if not self._grpc_channel:
@@ -207,14 +208,14 @@ def create_channel(
207208
and ``credentials_file`` are passed.
208209
"""
209210

210-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
211-
212211
return grpc_helpers.create_channel(
213212
host,
214213
credentials=credentials,
215214
credentials_file=credentials_file,
216215
quota_project_id=quota_project_id,
217-
**self_signed_jwt_kwargs,
216+
default_scopes=cls.AUTH_SCOPES,
217+
scopes=scopes,
218+
default_host=cls.DEFAULT_HOST,
218219
**kwargs,
219220
)
220221

google/cloud/monitoring_dashboard_v1/services/dashboards_service/transports/grpc_asyncio.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@ def create_channel(
8181
aio.Channel: A gRPC AsyncIO channel object.
8282
"""
8383

84-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
85-
8684
return grpc_helpers_async.create_channel(
8785
host,
8886
credentials=credentials,
8987
credentials_file=credentials_file,
9088
quota_project_id=quota_project_id,
91-
**self_signed_jwt_kwargs,
89+
default_scopes=cls.AUTH_SCOPES,
90+
scopes=scopes,
91+
default_host=cls.DEFAULT_HOST,
9292
**kwargs,
9393
)
9494

@@ -198,6 +198,7 @@ def __init__(
198198
scopes=scopes,
199199
quota_project_id=quota_project_id,
200200
client_info=client_info,
201+
always_use_jwt_access=True,
201202
)
202203

203204
if not self._grpc_channel:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
version = "2.1.0"
2626
release_status = "Development Status :: 5 - Production/Stable"
2727
dependencies = [
28-
"google-api-core[grpc] >= 1.22.2, < 2.0.0dev",
28+
"google-api-core[grpc] >= 1.26.0, <2.0.0dev",
2929
"proto-plus >= 0.4.0",
3030
"packaging >= 14.3",
3131
]

testing/constraints-3.6.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#
66
# e.g., if setup.py has "foo >= 1.14.0, < 2.0.0dev",
77
# Then this file should have foo==1.14.0
8-
google-api-core==1.22.2
8+
google-api-core==1.26.0
99
proto-plus==0.4.0
1010
libcst==0.2.5
1111
packaging==14.3

tests/unit/gapic/dashboard_v1/test_dashboards_service.py

Lines changed: 15 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@
3939
)
4040
from google.cloud.monitoring_dashboard_v1.services.dashboards_service import pagers
4141
from google.cloud.monitoring_dashboard_v1.services.dashboards_service import transports
42-
from google.cloud.monitoring_dashboard_v1.services.dashboards_service.transports.base import (
43-
_API_CORE_VERSION,
44-
)
4542
from google.cloud.monitoring_dashboard_v1.services.dashboards_service.transports.base import (
4643
_GOOGLE_AUTH_VERSION,
4744
)
@@ -59,8 +56,9 @@
5956
import google.auth
6057

6158

62-
# TODO(busunkim): Once google-api-core >= 1.26.0 is required:
63-
# - Delete all the api-core and auth "less than" test cases
59+
# TODO(busunkim): Once google-auth >= 1.25.0 is required transitively
60+
# through google-api-core:
61+
# - Delete the auth "less than" test cases
6462
# - Delete these pytest markers (Make the "greater than or equal to" tests the default).
6563
requires_google_auth_lt_1_25_0 = pytest.mark.skipif(
6664
packaging.version.parse(_GOOGLE_AUTH_VERSION) >= packaging.version.parse("1.25.0"),
@@ -71,16 +69,6 @@
7169
reason="This test requires google-auth >= 1.25.0",
7270
)
7371

74-
requires_api_core_lt_1_26_0 = pytest.mark.skipif(
75-
packaging.version.parse(_API_CORE_VERSION) >= packaging.version.parse("1.26.0"),
76-
reason="This test requires google-api-core < 1.26.0",
77-
)
78-
79-
requires_api_core_gte_1_26_0 = pytest.mark.skipif(
80-
packaging.version.parse(_API_CORE_VERSION) < packaging.version.parse("1.26.0"),
81-
reason="This test requires google-api-core >= 1.26.0",
82-
)
83-
8472

8573
def client_cert_source_callback():
8674
return b"cert bytes", b"key bytes"
@@ -144,6 +132,18 @@ def test_dashboards_service_client_from_service_account_info(client_class):
144132
assert client.transport._host == "monitoring.googleapis.com:443"
145133

146134

135+
@pytest.mark.parametrize(
136+
"client_class", [DashboardsServiceClient, DashboardsServiceAsyncClient,]
137+
)
138+
def test_dashboards_service_client_service_account_always_use_jwt(client_class):
139+
with mock.patch.object(
140+
service_account.Credentials, "with_always_use_jwt_access", create=True
141+
) as use_jwt:
142+
creds = service_account.Credentials(None, None, None)
143+
client = client_class(credentials=creds)
144+
use_jwt.assert_called_with(True)
145+
146+
147147
@pytest.mark.parametrize(
148148
"client_class", [DashboardsServiceClient, DashboardsServiceAsyncClient,]
149149
)
@@ -1611,7 +1611,6 @@ def test_dashboards_service_transport_auth_adc_old_google_auth(transport_class):
16111611
(transports.DashboardsServiceGrpcAsyncIOTransport, grpc_helpers_async),
16121612
],
16131613
)
1614-
@requires_api_core_gte_1_26_0
16151614
def test_dashboards_service_transport_create_channel(transport_class, grpc_helpers):
16161615
# If credentials and host are not provided, the transport class should use
16171616
# ADC credentials.
@@ -1645,84 +1644,6 @@ def test_dashboards_service_transport_create_channel(transport_class, grpc_helpe
16451644
)
16461645

16471646

1648-
@pytest.mark.parametrize(
1649-
"transport_class,grpc_helpers",
1650-
[
1651-
(transports.DashboardsServiceGrpcTransport, grpc_helpers),
1652-
(transports.DashboardsServiceGrpcAsyncIOTransport, grpc_helpers_async),
1653-
],
1654-
)
1655-
@requires_api_core_lt_1_26_0
1656-
def test_dashboards_service_transport_create_channel_old_api_core(
1657-
transport_class, grpc_helpers
1658-
):
1659-
# If credentials and host are not provided, the transport class should use
1660-
# ADC credentials.
1661-
with mock.patch.object(
1662-
google.auth, "default", autospec=True
1663-
) as adc, mock.patch.object(
1664-
grpc_helpers, "create_channel", autospec=True
1665-
) as create_channel:
1666-
creds = ga_credentials.AnonymousCredentials()
1667-
adc.return_value = (creds, None)
1668-
transport_class(quota_project_id="octopus")
1669-
1670-
create_channel.assert_called_with(
1671-
"monitoring.googleapis.com:443",
1672-
credentials=creds,
1673-
credentials_file=None,
1674-
quota_project_id="octopus",
1675-
scopes=(
1676-
"https://www.googleapis.com/auth/cloud-platform",
1677-
"https://www.googleapis.com/auth/monitoring",
1678-
"https://www.googleapis.com/auth/monitoring.read",
1679-
"https://www.googleapis.com/auth/monitoring.write",
1680-
),
1681-
ssl_credentials=None,
1682-
options=[
1683-
("grpc.max_send_message_length", -1),
1684-
("grpc.max_receive_message_length", -1),
1685-
],
1686-
)
1687-
1688-
1689-
@pytest.mark.parametrize(
1690-
"transport_class,grpc_helpers",
1691-
[
1692-
(transports.DashboardsServiceGrpcTransport, grpc_helpers),
1693-
(transports.DashboardsServiceGrpcAsyncIOTransport, grpc_helpers_async),
1694-
],
1695-
)
1696-
@requires_api_core_lt_1_26_0
1697-
def test_dashboards_service_transport_create_channel_user_scopes(
1698-
transport_class, grpc_helpers
1699-
):
1700-
# If credentials and host are not provided, the transport class should use
1701-
# ADC credentials.
1702-
with mock.patch.object(
1703-
google.auth, "default", autospec=True
1704-
) as adc, mock.patch.object(
1705-
grpc_helpers, "create_channel", autospec=True
1706-
) as create_channel:
1707-
creds = ga_credentials.AnonymousCredentials()
1708-
adc.return_value = (creds, None)
1709-
1710-
transport_class(quota_project_id="octopus", scopes=["1", "2"])
1711-
1712-
create_channel.assert_called_with(
1713-
"monitoring.googleapis.com:443",
1714-
credentials=creds,
1715-
credentials_file=None,
1716-
quota_project_id="octopus",
1717-
scopes=["1", "2"],
1718-
ssl_credentials=None,
1719-
options=[
1720-
("grpc.max_send_message_length", -1),
1721-
("grpc.max_receive_message_length", -1),
1722-
],
1723-
)
1724-
1725-
17261647
@pytest.mark.parametrize(
17271648
"transport_class",
17281649
[

0 commit comments

Comments
 (0)