Skip to content

Commit 4551aea

Browse files
feat: add always_use_jwt_access (#87)
... 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: googleapis/googleapis-gen@27e4c88
1 parent 86a7b37 commit 4551aea

File tree

7 files changed

+42
-124
lines changed

7 files changed

+42
-124
lines changed

packages/google-cloud-recaptcha-enterprise/.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/recaptchaenterprise/__init__.py

packages/google-cloud-recaptcha-enterprise/google/cloud/recaptchaenterprise_v1/services/recaptcha_enterprise_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.recaptchaenterprise_v1.types import recaptchaenterprise
2930
from google.protobuf import empty_pb2 # type: ignore
@@ -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 RecaptchaEnterpriseServiceTransport(abc.ABC):
5352
"""Abstract transport class for RecaptchaEnterpriseService."""
@@ -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 = {

packages/google-cloud-recaptcha-enterprise/google/cloud/recaptchaenterprise_v1/services/recaptcha_enterprise_service/transports/grpc.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ def __init__(
150150
scopes=scopes,
151151
quota_project_id=quota_project_id,
152152
client_info=client_info,
153+
always_use_jwt_access=True,
153154
)
154155

155156
if not self._grpc_channel:
@@ -205,14 +206,14 @@ def create_channel(
205206
and ``credentials_file`` are passed.
206207
"""
207208

208-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
209-
210209
return grpc_helpers.create_channel(
211210
host,
212211
credentials=credentials,
213212
credentials_file=credentials_file,
214213
quota_project_id=quota_project_id,
215-
**self_signed_jwt_kwargs,
214+
default_scopes=cls.AUTH_SCOPES,
215+
scopes=scopes,
216+
default_host=cls.DEFAULT_HOST,
216217
**kwargs,
217218
)
218219

packages/google-cloud-recaptcha-enterprise/google/cloud/recaptchaenterprise_v1/services/recaptcha_enterprise_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:

packages/google-cloud-recaptcha-enterprise/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
platforms="Posix; MacOS X; Windows",
4545
include_package_data=True,
4646
install_requires=(
47-
"google-api-core[grpc] >= 1.22.2, < 2.0.0dev",
47+
"google-api-core[grpc] >= 1.26.0, <2.0.0dev",
4848
"proto-plus >= 1.10.0",
4949
"packaging >= 14.3",
5050
),

packages/google-cloud-recaptcha-enterprise/testing/constraints-3.6.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
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==1.10.0
1010
packaging==14.3

packages/google-cloud-recaptcha-enterprise/tests/unit/gapic/recaptchaenterprise_v1/test_recaptcha_enterprise_service.py

Lines changed: 18 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@
4343
from google.cloud.recaptchaenterprise_v1.services.recaptcha_enterprise_service import (
4444
transports,
4545
)
46-
from google.cloud.recaptchaenterprise_v1.services.recaptcha_enterprise_service.transports.base import (
47-
_API_CORE_VERSION,
48-
)
4946
from google.cloud.recaptchaenterprise_v1.services.recaptcha_enterprise_service.transports.base import (
5047
_GOOGLE_AUTH_VERSION,
5148
)
@@ -56,8 +53,9 @@
5653
import google.auth
5754

5855

59-
# TODO(busunkim): Once google-api-core >= 1.26.0 is required:
60-
# - Delete all the api-core and auth "less than" test cases
56+
# TODO(busunkim): Once google-auth >= 1.25.0 is required transitively
57+
# through google-api-core:
58+
# - Delete the auth "less than" test cases
6159
# - Delete these pytest markers (Make the "greater than or equal to" tests the default).
6260
requires_google_auth_lt_1_25_0 = pytest.mark.skipif(
6361
packaging.version.parse(_GOOGLE_AUTH_VERSION) >= packaging.version.parse("1.25.0"),
@@ -68,16 +66,6 @@
6866
reason="This test requires google-auth >= 1.25.0",
6967
)
7068

71-
requires_api_core_lt_1_26_0 = pytest.mark.skipif(
72-
packaging.version.parse(_API_CORE_VERSION) >= packaging.version.parse("1.26.0"),
73-
reason="This test requires google-api-core < 1.26.0",
74-
)
75-
76-
requires_api_core_gte_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-
8169

8270
def client_cert_source_callback():
8371
return b"cert bytes", b"key bytes"
@@ -144,6 +132,21 @@ def test_recaptcha_enterprise_service_client_from_service_account_info(client_cl
144132
assert client.transport._host == "recaptchaenterprise.googleapis.com:443"
145133

146134

135+
@pytest.mark.parametrize(
136+
"client_class",
137+
[RecaptchaEnterpriseServiceClient, RecaptchaEnterpriseServiceAsyncClient,],
138+
)
139+
def test_recaptcha_enterprise_service_client_service_account_always_use_jwt(
140+
client_class,
141+
):
142+
with mock.patch.object(
143+
service_account.Credentials, "with_always_use_jwt_access", create=True
144+
) as use_jwt:
145+
creds = service_account.Credentials(None, None, None)
146+
client = client_class(credentials=creds)
147+
use_jwt.assert_called_with(True)
148+
149+
147150
@pytest.mark.parametrize(
148151
"client_class",
149152
[RecaptchaEnterpriseServiceClient, RecaptchaEnterpriseServiceAsyncClient,],
@@ -2075,7 +2078,6 @@ def test_recaptcha_enterprise_service_transport_auth_adc_old_google_auth(
20752078
(transports.RecaptchaEnterpriseServiceGrpcAsyncIOTransport, grpc_helpers_async),
20762079
],
20772080
)
2078-
@requires_api_core_gte_1_26_0
20792081
def test_recaptcha_enterprise_service_transport_create_channel(
20802082
transport_class, grpc_helpers
20812083
):
@@ -2106,79 +2108,6 @@ def test_recaptcha_enterprise_service_transport_create_channel(
21062108
)
21072109

21082110

2109-
@pytest.mark.parametrize(
2110-
"transport_class,grpc_helpers",
2111-
[
2112-
(transports.RecaptchaEnterpriseServiceGrpcTransport, grpc_helpers),
2113-
(transports.RecaptchaEnterpriseServiceGrpcAsyncIOTransport, grpc_helpers_async),
2114-
],
2115-
)
2116-
@requires_api_core_lt_1_26_0
2117-
def test_recaptcha_enterprise_service_transport_create_channel_old_api_core(
2118-
transport_class, grpc_helpers
2119-
):
2120-
# If credentials and host are not provided, the transport class should use
2121-
# ADC credentials.
2122-
with mock.patch.object(
2123-
google.auth, "default", autospec=True
2124-
) as adc, mock.patch.object(
2125-
grpc_helpers, "create_channel", autospec=True
2126-
) as create_channel:
2127-
creds = ga_credentials.AnonymousCredentials()
2128-
adc.return_value = (creds, None)
2129-
transport_class(quota_project_id="octopus")
2130-
2131-
create_channel.assert_called_with(
2132-
"recaptchaenterprise.googleapis.com:443",
2133-
credentials=creds,
2134-
credentials_file=None,
2135-
quota_project_id="octopus",
2136-
scopes=("https://www.googleapis.com/auth/cloud-platform",),
2137-
ssl_credentials=None,
2138-
options=[
2139-
("grpc.max_send_message_length", -1),
2140-
("grpc.max_receive_message_length", -1),
2141-
],
2142-
)
2143-
2144-
2145-
@pytest.mark.parametrize(
2146-
"transport_class,grpc_helpers",
2147-
[
2148-
(transports.RecaptchaEnterpriseServiceGrpcTransport, grpc_helpers),
2149-
(transports.RecaptchaEnterpriseServiceGrpcAsyncIOTransport, grpc_helpers_async),
2150-
],
2151-
)
2152-
@requires_api_core_lt_1_26_0
2153-
def test_recaptcha_enterprise_service_transport_create_channel_user_scopes(
2154-
transport_class, grpc_helpers
2155-
):
2156-
# If credentials and host are not provided, the transport class should use
2157-
# ADC credentials.
2158-
with mock.patch.object(
2159-
google.auth, "default", autospec=True
2160-
) as adc, mock.patch.object(
2161-
grpc_helpers, "create_channel", autospec=True
2162-
) as create_channel:
2163-
creds = ga_credentials.AnonymousCredentials()
2164-
adc.return_value = (creds, None)
2165-
2166-
transport_class(quota_project_id="octopus", scopes=["1", "2"])
2167-
2168-
create_channel.assert_called_with(
2169-
"recaptchaenterprise.googleapis.com:443",
2170-
credentials=creds,
2171-
credentials_file=None,
2172-
quota_project_id="octopus",
2173-
scopes=["1", "2"],
2174-
ssl_credentials=None,
2175-
options=[
2176-
("grpc.max_send_message_length", -1),
2177-
("grpc.max_receive_message_length", -1),
2178-
],
2179-
)
2180-
2181-
21822111
@pytest.mark.parametrize(
21832112
"transport_class",
21842113
[

0 commit comments

Comments
 (0)