Skip to content

Commit 8d24f3a

Browse files
feat: add always_use_jwt_access (#61)
... 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 eaa04fe commit 8d24f3a

File tree

7 files changed

+39
-131
lines changed

7 files changed

+39
-131
lines changed

packages/google-area120-tables/.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/area120/tables/__init__.py

packages/google-area120-tables/google/area120/tables_v1alpha1/services/tables_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.area120.tables_v1alpha1.types import tables
2930
from google.protobuf import empty_pb2 # type: ignore
@@ -44,8 +45,6 @@
4445
except pkg_resources.DistributionNotFound: # pragma: NO COVER
4546
_GOOGLE_AUTH_VERSION = None
4647

47-
_API_CORE_VERSION = google.api_core.__version__
48-
4948

5049
class TablesServiceTransport(abc.ABC):
5150
"""Abstract transport class for TablesService."""
@@ -70,6 +69,7 @@ def __init__(
7069
scopes: Optional[Sequence[str]] = None,
7170
quota_project_id: Optional[str] = None,
7271
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
72+
always_use_jwt_access: Optional[bool] = False,
7373
**kwargs,
7474
) -> None:
7575
"""Instantiate the transport.
@@ -93,6 +93,8 @@ def __init__(
9393
API requests. If ``None``, then default info will be used.
9494
Generally, you only need to set this if you're developing
9595
your own client library.
96+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
97+
be used for service account credentials.
9698
"""
9799
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
98100
if ":" not in host:
@@ -121,13 +123,20 @@ def __init__(
121123
**scopes_kwargs, quota_project_id=quota_project_id
122124
)
123125

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

127-
# TODO(busunkim): These two class methods are in the base transport
137+
# TODO(busunkim): This method is in the base transport
128138
# to avoid duplicating code across the transport classes. These functions
129-
# should be deleted once the minimum required versions of google-api-core
130-
# and google-auth are increased.
139+
# should be deleted once the minimum required versions of google-auth is increased.
131140

132141
# TODO: Remove this function once google-auth >= 1.25.0 is required
133142
@classmethod
@@ -148,27 +157,6 @@ def _get_scopes_kwargs(
148157

149158
return scopes_kwargs
150159

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

packages/google-area120-tables/google/area120/tables_v1alpha1/services/tables_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

packages/google-area120-tables/google/area120/tables_v1alpha1/services/tables_service/transports/grpc_asyncio.py

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

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

@@ -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:

packages/google-area120-tables/setup.py

Lines changed: 1 addition & 1 deletion
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
),

packages/google-area120-tables/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.4.0
1010
packaging==14.3

packages/google-area120-tables/tests/unit/gapic/tables_v1alpha1/test_tables_service.py

Lines changed: 15 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@
3535
from google.area120.tables_v1alpha1.services.tables_service import TablesServiceClient
3636
from google.area120.tables_v1alpha1.services.tables_service import pagers
3737
from google.area120.tables_v1alpha1.services.tables_service import transports
38-
from google.area120.tables_v1alpha1.services.tables_service.transports.base import (
39-
_API_CORE_VERSION,
40-
)
4138
from google.area120.tables_v1alpha1.services.tables_service.transports.base import (
4239
_GOOGLE_AUTH_VERSION,
4340
)
@@ -50,8 +47,9 @@
5047
import google.auth
5148

5249

53-
# TODO(busunkim): Once google-api-core >= 1.26.0 is required:
54-
# - Delete all the api-core and auth "less than" test cases
50+
# TODO(busunkim): Once google-auth >= 1.25.0 is required transitively
51+
# through google-api-core:
52+
# - Delete the auth "less than" test cases
5553
# - Delete these pytest markers (Make the "greater than or equal to" tests the default).
5654
requires_google_auth_lt_1_25_0 = pytest.mark.skipif(
5755
packaging.version.parse(_GOOGLE_AUTH_VERSION) >= packaging.version.parse("1.25.0"),
@@ -62,16 +60,6 @@
6260
reason="This test requires google-auth >= 1.25.0",
6361
)
6462

65-
requires_api_core_lt_1_26_0 = pytest.mark.skipif(
66-
packaging.version.parse(_API_CORE_VERSION) >= packaging.version.parse("1.26.0"),
67-
reason="This test requires google-api-core < 1.26.0",
68-
)
69-
70-
requires_api_core_gte_1_26_0 = pytest.mark.skipif(
71-
packaging.version.parse(_API_CORE_VERSION) < packaging.version.parse("1.26.0"),
72-
reason="This test requires google-api-core >= 1.26.0",
73-
)
74-
7563

7664
def client_cert_source_callback():
7765
return b"cert bytes", b"key bytes"
@@ -134,6 +122,18 @@ def test_tables_service_client_from_service_account_info(client_class):
134122
assert client.transport._host == "area120tables.googleapis.com:443"
135123

136124

125+
@pytest.mark.parametrize(
126+
"client_class", [TablesServiceClient, TablesServiceAsyncClient,]
127+
)
128+
def test_tables_service_client_service_account_always_use_jwt(client_class):
129+
with mock.patch.object(
130+
service_account.Credentials, "with_always_use_jwt_access", create=True
131+
) as use_jwt:
132+
creds = service_account.Credentials(None, None, None)
133+
client = client_class(credentials=creds)
134+
use_jwt.assert_called_with(True)
135+
136+
137137
@pytest.mark.parametrize(
138138
"client_class", [TablesServiceClient, TablesServiceAsyncClient,]
139139
)
@@ -3089,7 +3089,6 @@ def test_tables_service_transport_auth_adc_old_google_auth(transport_class):
30893089
(transports.TablesServiceGrpcAsyncIOTransport, grpc_helpers_async),
30903090
],
30913091
)
3092-
@requires_api_core_gte_1_26_0
30933092
def test_tables_service_transport_create_channel(transport_class, grpc_helpers):
30943093
# If credentials and host are not provided, the transport class should use
30953094
# ADC credentials.
@@ -3125,86 +3124,6 @@ def test_tables_service_transport_create_channel(transport_class, grpc_helpers):
31253124
)
31263125

31273126

3128-
@pytest.mark.parametrize(
3129-
"transport_class,grpc_helpers",
3130-
[
3131-
(transports.TablesServiceGrpcTransport, grpc_helpers),
3132-
(transports.TablesServiceGrpcAsyncIOTransport, grpc_helpers_async),
3133-
],
3134-
)
3135-
@requires_api_core_lt_1_26_0
3136-
def test_tables_service_transport_create_channel_old_api_core(
3137-
transport_class, grpc_helpers
3138-
):
3139-
# If credentials and host are not provided, the transport class should use
3140-
# ADC credentials.
3141-
with mock.patch.object(
3142-
google.auth, "default", autospec=True
3143-
) as adc, mock.patch.object(
3144-
grpc_helpers, "create_channel", autospec=True
3145-
) as create_channel:
3146-
creds = ga_credentials.AnonymousCredentials()
3147-
adc.return_value = (creds, None)
3148-
transport_class(quota_project_id="octopus")
3149-
3150-
create_channel.assert_called_with(
3151-
"area120tables.googleapis.com:443",
3152-
credentials=creds,
3153-
credentials_file=None,
3154-
quota_project_id="octopus",
3155-
scopes=(
3156-
"https://www.googleapis.com/auth/drive",
3157-
"https://www.googleapis.com/auth/drive.file",
3158-
"https://www.googleapis.com/auth/drive.readonly",
3159-
"https://www.googleapis.com/auth/spreadsheets",
3160-
"https://www.googleapis.com/auth/spreadsheets.readonly",
3161-
"https://www.googleapis.com/auth/tables",
3162-
),
3163-
ssl_credentials=None,
3164-
options=[
3165-
("grpc.max_send_message_length", -1),
3166-
("grpc.max_receive_message_length", -1),
3167-
],
3168-
)
3169-
3170-
3171-
@pytest.mark.parametrize(
3172-
"transport_class,grpc_helpers",
3173-
[
3174-
(transports.TablesServiceGrpcTransport, grpc_helpers),
3175-
(transports.TablesServiceGrpcAsyncIOTransport, grpc_helpers_async),
3176-
],
3177-
)
3178-
@requires_api_core_lt_1_26_0
3179-
def test_tables_service_transport_create_channel_user_scopes(
3180-
transport_class, grpc_helpers
3181-
):
3182-
# If credentials and host are not provided, the transport class should use
3183-
# ADC credentials.
3184-
with mock.patch.object(
3185-
google.auth, "default", autospec=True
3186-
) as adc, mock.patch.object(
3187-
grpc_helpers, "create_channel", autospec=True
3188-
) as create_channel:
3189-
creds = ga_credentials.AnonymousCredentials()
3190-
adc.return_value = (creds, None)
3191-
3192-
transport_class(quota_project_id="octopus", scopes=["1", "2"])
3193-
3194-
create_channel.assert_called_with(
3195-
"area120tables.googleapis.com:443",
3196-
credentials=creds,
3197-
credentials_file=None,
3198-
quota_project_id="octopus",
3199-
scopes=["1", "2"],
3200-
ssl_credentials=None,
3201-
options=[
3202-
("grpc.max_send_message_length", -1),
3203-
("grpc.max_receive_message_length", -1),
3204-
],
3205-
)
3206-
3207-
32083127
@pytest.mark.parametrize(
32093128
"transport_class",
32103129
[

0 commit comments

Comments
 (0)