E570 feat: add always_use_jwt_access (#11) · googleapis/python-dms@9890aff · GitHub
[go: up one dir, main page]

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

Commit 9890aff

Browse files
feat: add always_use_jwt_access (#11)
... 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 17bcbd8 commit 9890aff

File tree

6 files changed

+39
-123
lines changed

6 files changed

+39
-123
lines changed

google/cloud/clouddms_v1/services/data_migration_service/transports/base.py

Lines changed: 14 additions & 26 deletions
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.clouddms_v1.types import clouddms
3031
from google.cloud.clouddms_v1.types import clouddms_resources
@@ -46,8 +47,6 @@
4647
except pkg_resources.DistributionNotFound: # pragma: NO COVER
4748
_GOOGLE_AUTH_VERSION = None
4849

49-
_API_CORE_VERSION = google.api_core.__version__
50-
5150

5251
class DataMigrationServiceTransport(abc.ABC):
5352
"""Abstract transport class for DataMigrationService."""
@@ -65,6 +64,7 @@ def __init__(
6564
scopes: Optional[Sequence[str]] = None,
6665
quota_project_id: Optional[str] = None,
6766
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
67+
always_use_jwt_access: Optional[bool] = False,
6868
**kwargs,
6969
) -> None:
7070
"""Instantiate the transport.
@@ -88,6 +88,8 @@ def __init__(
8888
API requests. If ``None``, then default info will be used.
8989
Generally, you only need to set this if you're developing
9090
your own client library.
91+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
92+
be used for service account credentials.
9193
"""
9294
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
9395
if ":" not in host:
@@ -116,13 +118,20 @@ def __init__(
116118
**scopes_kwargs, quota_project_id=quota_project_id
117119
)
118120

121+
# If the credentials is service account credentials, then always try to use self signed JWT.
122+
if (
123+
always_use_jwt_access
124+
and isinstance(credentials, service_account.Credentials)
125+
and hasattr(service_account.Credentials, "with_always_use_jwt_access")
126+
):
127+
credentials = credentials.with_always_use_jwt_access(True)
128+
119129
# Save the credentials.
120130
self._credentials = credentials
121131

122-
# TODO(busunkim): These two class methods are in the base transport
132+
# TODO(busunkim): This method is in the base transport
123133
# to avoid duplicating code across the transport classes. These functions
124-
# should be deleted once the minimum required versions of google-api-core
125-
# and google-auth are increased.
134+
# should be deleted once the minimum required versions of google-auth is increased.
126135

127136
# TODO: Remove this function once google-auth >= 1.25.0 is required
128137
@classmethod
@@ -143,27 +152,6 @@ def _get_scopes_kwargs(
143152

144153
return scopes_kwargs
145154

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

google/cloud/clouddms_v1/services/data_migration_service/transports/grpc.py

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

158159
if not self._grpc_channel:
@@ -208,14 +209,14 @@ def create_channel(
208209
and ``credentials_file`` are passed.
209210
"""
210211

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

google/cloud/clouddms_v1/services/data_migration_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

@@ -199,6 +199,7 @@ def __init__(
199199
scopes=scopes,
200200
quota_project_id=quota_project_id,
201201
client_info=client_info,
202+
always_use_jwt_access=True,
202203
)
203204

204205
if not self._grpc_channel:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
release_status = "Development Status :: 4 - Beta"
2727
url = "https://github.com/googleapis/python-dms"
2828
dependencies = [
29-
"google-api-core[grpc] >= 1.22.2, < 2.0.0dev",
29+
"google-api-core[grpc] >= 1.26.0, <2.0.0dev",
3030
"proto-plus >= 1.15.0",
3131
"packaging >= 14.3",
3232
]

testing/constraints-3.6.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Pin the version to the lower bound.
55
# e.g., if setup.py has "google-cloud-foo >= 1.14.0, < 2.0.0dev",
66
# Then this file should have google-cloud-foo==1.14.0
7-
google-api-core==1.22.2
7+
google-api-core==1.26.0
88
proto-plus==1.15.0
99
packaging==14.3
1010
google-auth==1.24.0 # TODO: remove when google-auth>=1.25.0 si transitively required through google-api-core

tests/unit/gapic/clouddms_v1/test_data_migration_service.py

Lines changed: 15 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@
4242
)
4343
from google.cloud.clouddms_v1.services.data_migration_service import pagers
4444
from google.cloud.clouddms_v1.services.data_migration_service import transports
45-
from google.cloud.clouddms_v1.services.data_migration_service.transports.base import (
46-
_API_CORE_VERSION,
47-
)
4845
from google.cloud.clouddms_v1.services.data_migration_service.transports.base import (
4946
_GOOGLE_AUTH_VERSION,
5047
)
@@ -61,8 +58,9 @@
6158
import google.auth
6259

6360

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

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

8775
def client_cert_source_callback():
8876
return b"cert bytes", b"key bytes"
@@ -146,6 +134,18 @@ def test_data_migration_service_client_from_service_account_info(client_class):
146134
assert client.transport._host == "datamigration.googleapis.com:443"
147135

148136

137+
@pytest.mark.parametrize(
138+
"client_class", [DataMigrationServiceClient, DataMigrationServiceAsyncClient,]
139+
)
140+
def test_data_migration_service_client_service_account_always_use_jwt(client_class):
141+
with mock.patch.object(
142+
service_account.Credentials, "with_always_use_jwt_access", create=True
143+
) as use_jwt:
144+
creds = service_account.Credentials(None, None, None)
100F2
145+
client = client_class(credentials=creds)
146+
use_jwt.assert_called_with(True)
147+
148+
149149
@pytest.mark.parametrize(
150150
"client_class", [DataMigrationServiceClient, DataMigrationServiceAsyncClient,]
151151
)
@@ -4496,7 +4496,6 @@ def test_data_migration_service_transport_auth_adc_old_google_auth(transport_cla
44964496
(transports.DataMigrationServiceGrpcAsyncIOTransport, grpc_helpers_async),
44974497
],
44984498
)
4499-
@requires_api_core_gte_1_26_0
45004499
def test_data_migration_service_transport_create_channel(transport_class, grpc_helpers):
45014500
# If credentials and host are not provided, the transport class should use
45024501
# ADC credentials.
@@ -4525,79 +4524,6 @@ def test_data_migration_service_transport_create_channel(transport_class, grpc_h
45254524
)
45264525

45274526

4528-
@pytest.mark.parametrize(
4529-
"transport_class,grpc_helpers",
4530-
[
4531-
(transports.DataMigrationServiceGrpcTransport, grpc_helpers),
4532-
(transports.DataMigrationServiceGrpcAsyncIOTransport, grpc_helpers_async),
4533-
],
4534-
)
4535-
@requires_api_core_lt_1_26_0
4536-
def test_data_migration_service_transport_create_channel_old_api_core(
4537-
transport_class, grpc_helpers
4538-
):
4539-
# If credentials and host are not provided, the transport class should use
4540-
# ADC credentials.
4541-
with mock.patch.object(
4542-
google.auth, "default", autospec=True
4543-
) as adc, mock.patch.object(
4544-
grpc_helpers, "create_channel", autospec=True
4545-
) as create_channel:
4546-
creds = ga_credentials.AnonymousCredentials()
4547-
adc.return_value = (creds, None)
4548-
transport_class(quota_project_id="octopus")
4549-
4550-
create_channel.assert_called_with(
4551-
"datamigration.googleapis.com:443",
4552-
credentials=creds,
4553-
credentials_file=None,
4554-
quota_project_id="octopus",
4555-
scopes=("https://www.googleapis.com/auth/cloud-platform",),
4556-
ssl_credentials=None,
4557-
options=[
4558-
("grpc.max_send_message_length", -1),
4559-
("grpc.max_receive_message_length", -1),
4560-
],
4561-
)
4562-
4563-
4564-
@pytest.mark.parametrize(
4565-
"transport_class,grpc_helpers",
4566-
[
4567-
(transports.DataMigrationServiceGrpcTransport, grpc_helpers),
4568-
(transports.DataMigrationServiceGrpcAsyncIOTransport, grpc_helpers_async),
4569-
],
4570-
)
4571-
@requires_api_core_lt_1_26_0
4572-
def test_data_migration_service_transport_create_channel_user_scopes(
4573-
transport_class, grpc_helpers
4574-
):
4575-
# If credentials and host are not provided, the transport class should use
4576-
# ADC credentials.
4577-
with mock.patch.object(
4578-
google.auth, "default", autospec=True
4579-
) as adc, mock.patch.object(
4580-
grpc_helpers, "create_channel", autospec=True
4581-
) as create_channel:
4582-
creds = ga_credentials.AnonymousCredentials()
4583-
adc.return_value = (creds, None)
4584-
4585-
transport_class(quota_project_id="octopus", scopes=["1", "2"])
4586-
4587-
create_channel.assert_called_with(
4588-
"datamigration.googleapis.com:443",
4589-
credentials=creds,
4590-
credentials_file=None,
4591-
quota_project_id="octopus",
4592-
scopes=["1", "2"],
4593-
ssl_credentials=None,
4594-
options=[
4595-
("grpc.max_send_message_length", -1),
4596-
("grpc.max_receive_message_length", -1),
4597-
], ACE
4598-
)
4599< 4E88 span class="diff-text-marker">-
4600-
46014527
@pytest.mark.parametrize(
46024528
"transport_class",
46034529
[

0 commit comments

Comments
 (0)
0