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

Commit 416b3e9

Browse files
feat: add always_use_jwt_access (#113)
... 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 4ae0da5 commit 416b3e9

File tree

7 files changed

+39
-124
lines changed

7 files changed

+39
-124
lines changed

.coveragerc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
branch = True
33

44
[report]
5-
fail_under = 100
65
show_missing = True
76
omit =
87
google/cloud/datalabeling/__init__.py

google/cloud/datalabeling_v1beta1/services/data_labeling_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.datalabeling_v1beta1.types import annotation_spec_set
3031
from google.cloud.datalabeling_v1beta1.types import (
@@ -58,8 +59,6 @@
5859
except pkg_resources.DistributionNotFound: # pragma: NO COVER
5960
_GOOGLE_AUTH_VERSION = None
6061

61-
_API_CORE_VERSION = google.api_core.__version__
62-
6362

6463
class DataLabelingServiceTransport(abc.ABC):
6564
"""Abstract transport class for DataLabelingService."""
@@ -77,6 +76,7 @@ def __init__(
7776
scopes: Optional[Sequence[str]] = None,
7877
quota_project_id: Optional[str] = None,
7978
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
79+
always_use_jwt_access: Optional[bool] = False,
8080
**kwargs,
8181
) -> None:
8282
"""Instantiate the transport.
@@ -100,6 +100,8 @@ def __init__(
100100
API requests. If ``None``, then default info will be used.
101101
Generally, you only need to set this if you're developing
102102
your own client library.
103+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
104+
be used for service account credentials.
103105
"""
104106
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
105107
if ":" not in host:
@@ -128,13 +130,20 @@ def __init__(
128130
**scopes_kwargs, quota_project_id=quota_project_id
129131
)
130132

133+
# If the credentials is service account credentials, then always try to use self signed JWT.
134+
if (
135+
always_use_jwt_access
136+
and isinstance(credentials, service_account.Credentials)
137+
and hasattr(service_account.Credentials, "with_always_use_jwt_access")
138+
):
139+
credentials = credentials.with_always_use_jwt_access(True)
140+
131141
# Save the credentials.
132142
self._credentials = credentials
133143

134-
# TODO(busunkim): These two class methods are in the base transport
144+
# TODO(busunkim): This method is in the base transport
135145
# to avoid duplicating code across the transport classes. These functions
136-
# should be deleted once the minimum required versions of google-api-core
137-
# and google-auth are increased.
146+
# should be deleted once the minimum required versions of google-auth is increased.
138147

139148
# TODO: Remove this function once google-auth >= 1.25.0 is required
140149
@classmethod
@@ -155,27 +164,6 @@ def _get_scopes_kwargs(
155164

156165
return scopes_kwargs
157166

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

google/cloud/datalabeling_v1beta1/services/data_labeling_service/transports/grpc.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ def __init__(
163163
scopes=scopes,
164164
quota_project_id=quota_project_id,
165165
client_info=client_info,
166+
always_use_jwt_access=True,
166167
)
167168

168169
if not self._grpc_channel:
@@ -218,14 +219,14 @@ def create_channel(
218219
and ``credentials_file`` are passed.
219220
"""
220221

221-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
222-
223222
return grpc_helpers.create_channel(
224223
host,
225224
credentials=credentials,
226225
credentials_file=credentials_file,
227226
quota_project_id=quota_project_id,
228-
**self_signed_jwt_kwargs,
227+
default_scopes=cls.AUTH_SCOPES,
228+
scopes=scopes,
229+
default_host=cls.DEFAULT_HOST,
229230
**kwargs,
230231
)
231232

google/cloud/datalabeling_v1beta1/services/data_labeling_service/transports/grpc_asyncio.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,14 @@ def create_channel(
9191
aio.Channel: A gRPC AsyncIO channel object.
9292
"""
9393

94-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
95-
9694
return grpc_helpers_async.create_channel(
9795
host,
9896
credentials=credentials,
9997
credentials_file=credentials_file,
10098
quota_project_id=quota_project_id,
101-
**self_signed_jwt_kwargs,
99+
default_scopes=cls.AUTH_SCOPES,
100+
scopes=scopes,
101+
default_host=cls.DEFAULT_HOST,
102102
**kwargs,
103103
)
104104

@@ -209,6 +209,7 @@ def __init__(
209209
scopes=scopes,
210210
quota_project_id=quota_project_id,
211211
client_info=client_info,
212+
always_use_jwt_access=True,
212213
)
213214

214215
if not self._grpc_channel:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
version = "1.1.0"
2525
release_status = "Development Status :: 4 - Beta"
2626
dependencies = [
27-
"google-api-core[grpc] >= 1.22.2, < 2.0.0dev",
27+
"google-api-core[grpc] >= 1.26.0, <2.0.0dev",
2828
"proto-plus >= 1.4.0",
2929
"packaging >= 14.3",
3030
]

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
libcst==0.2.5
1010
proto-plus==1.4.0
1111
packaging==14.3

tests/unit/gapic/datalabeling_v1beta1/test_data_labeling_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.datalabeling_v1beta1.services.data_labeling_service import pagers
4444
from google.cloud.datalabeling_v1beta1.services.data_labeling_service import transports
45-
from google.cloud.datalabeling_v1beta1.services.data_labeling_service.transports.base import (
46-
_API_CORE_VERSION,
47-
)
4845
from google.cloud.datalabeling_v1beta1.services.data_labeling_service.transports.base import (
4946
_GOOGLE_AUTH_VERSION,
5047
)
@@ -74,8 +71,9 @@
7471
import google.auth
7572

7673

77-
# TODO(busunkim): Once google-api-core >= 1.26.0 is required:
78-
# - Delete all the api-core and auth "less than" test cases
74+
# TODO(busunkim): Once google-auth >= 1.25.0 is required transitively
75+
# through google-api-core:
76+
# - Delete the auth "less than" test cases
7977
# - Delete these pytest markers (Make the "greater than or equal to" tests the default).
8078
requires_google_auth_lt_1_25_0 = pytest.mark.skipif(
8179
packaging.version.parse(_GOOGLE_AUTH_VERSION) >= packaging.version.parse("1.25.0"),
@@ -86,16 +84,6 @@
8684
reason="This test requires google-auth >= 1.25.0",
8785
)
8886

89-
requires_api_core_lt_1_26_0 = pytest.mark.skipif(
90-
packaging.version.parse(_API_CORE_VERSION) >= packaging.version.parse("1.26.0"),
91-
reason="This test requires google-api-core < 1.26.0",
92-
)
93-
94-
requires_api_core_gte_1_26_0 = pytest.mark.skipif(
95-
packaging.version.parse(_API_CORE_VERSION) < packaging.version.parse("1.26.0"),
96-
reason="This test requires google-api-core >= 1.26.0",
97-
)
98-
9987

10088
def client_cert_source_callback():
10189
return b"cert bytes", b"key bytes"
@@ -159,6 +147,18 @@ def test_data_labeling_service_client_from_service_account_info(client_class):
159147
assert client.transport._host == "datalabeling.googleapis.com:443"
160148

161149

150+
@pytest.mark.parametrize(
151+
"client_class", [DataLabelingServiceClient, DataLabelingServiceAsyncClient,]
152+
)
153+
def test_data_labeling_service_client_service_account_always_use_jwt(client_class):
154+
with mock.patch.object(
155+
service_account.Credentials, "with_always_use_jwt_access", create=True
156+
) as use_jwt:
157+
creds = service_account.Credentials(None, None, None)
158+
client = client_class(credentials=creds)
159+
use_jwt.assert_called_with(True)
160+
161+
162162
@pytest.mark.parametrize(
163163
"client_class", [DataLabelingServiceClient, DataLabelingServiceAsyncClient,]
164164
)
@@ -10078,7 +10078,6 @@ def test_data_labeling_service_transport_auth_adc_old_google_auth(transport_clas
1007810078
(transports.DataLabelingServiceGrpcAsyncIOTransport, grpc_helpers_async),
1007910079
],
1008010080
)
10081-
@requires_api_core_gte_1_26_0
1008210081
def test_data_labeling_service_transport_create_channel(transport_class, grpc_helpers):
1008310082
# If credentials and host are not provided, the transport class should use
1008410083
# ADC credentials.
@@ -10107,79 +10106,6 @@ def test_data_labeling_service_transport_create_channel(transport_class, grpc_he
1010710106
)
1010810107

1010910108

10110-
@pytest.mark.parametrize(
10111-
"transport_class,grpc_helpers",
10112-
[
10113-
(transports.DataLabelingServiceGrpcTransport, grpc_helpers),
10114-
(transports.DataLabelingServiceGrpcAsyncIOTransport, grpc_helpers_async),
10115-
],
10116-
)
10117-
@requires_api_core_lt_1_26_0
10118-
def test_data_labeling_service_transport_create_channel_old_api_core(
10119-
transport_class, grpc_helpers
10120-
):
10121-
# If credentials and host are not provided, the transport class should use
10122-
# ADC credentials.
10123-
with mock.patch.object(
10124-
google.auth, "default", autospec=True
10125-
) as adc, mock.patch.object(
10126-
grpc_helpers, "create_channel", autospec=True
10127-
) as create_channel:
10128-
creds = ga_credentials.AnonymousCredentials()
10129-
adc.return_value = (creds, None)
10130-
transport_class(quota_project_id="octopus")
10131-
10132-
create_channel.assert_called_with(
10133-
"datalabeling.googleapis.com:443",
10134-
credentials=creds,
10135-
credentials_file=None,
10136-
quota_project_id="octopus",
10137-
scopes=("https://www.googleapis.com/auth/cloud-platform",),
10138-
ssl_credentials=None,
10139-
options=[
10140-
("grpc.max_send_message_length", -1),
10141-
("grpc.max_receive_message_length", -1),
10142-
],
10143-
)
10144-
10145-
10146-
@pytest.mark.parametrize(
10147-
"transport_class,grpc_helpers",
10148-
[
10149-
(transports.DataLabelingServiceGrpcTransport, grpc_helpers),
10150-
(transports.DataLabelingServiceGrpcAsyncIOTransport, grpc_helpers_async),
10151-
],
10152-
)
10153-
@requires_api_core_lt_1_26_0
10154-
def test_data_labeling_service_transport_create_channel_user_scopes(
10155-
transport_class, grpc_helpers
10156-
):
10157-
# If credentials and host are not provided, the transport class should use
10158-
# ADC credentials.
10159-
with mock.patch.object(
10160-
google.auth, "default", autospec=True
10161-
) as adc, mock.patch.object(
10162-
grpc_helpers, "create_channel", autospec=True
10163-
) as create_channel:
10164-
creds = ga_credentials.AnonymousCredentials()
10165-
adc.return_value = (creds, None)
10166-
10167-
transport_class(quota_project_id="octopus", scopes=["1", "2"])
10168-
10169-
create_channel.assert_called_with(
10170-
"datalabeling.googleapis.com:443",
10171-
credentials=creds,
10172-
credentials_file=None,
10173-
quota_project_id="octopus",
10174-
scopes=["1", "2"],
10175-
ssl_credentials=None,
10176-
options=[
10177-
("grpc.max_send_message_length", -1),
10178-
("grpc.max_receive_message_length", -1),
10179-
],
10180-
)
10181-
10182-
1018310109
@pytest.mark.parametrize(
1018410110
"transport_class",
1018510111
[

0 commit comments

Comments
 (0)