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

Commit 3123e99

Browse files
feat: add always_use_jwt_access (#123)
... 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 6a24d94 commit 3123e99

7 files changed

Lines changed: 39 additions & 127 deletions

File tree

‎.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/bigquery_reservation/__init__.py

‎google/cloud/bigquery_reservation_v1/services/reservation_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.bigquery_reservation_v1.types import reservation
2930
from google.cloud.bigquery_reservation_v1.types import reservation as gcbr_reservation
@@ -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 ReservationServiceTransport(abc.ABC):
5453
"""Abstract transport class for ReservationService."""
@@ -69,6 +68,7 @@ def __init__(
6968
scopes: Optional[Sequence[str]] = None,
7069
quota_project_id: Optional[str] = None,
7170
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
71+
always_use_jwt_access: Optional[bool] = False,
7272
**kwargs,
7373
) -> None:
7474
"""Instantiate the transport.
@@ -92,6 +92,8 @@ def __init__(
9292
API requests. If ``None``, then default info will be used.
9393
Generally, you only need to set this if you're developing
9494
your own client library.
95+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
96+
be used for service account credentials.
9597
"""
9698
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
9799
if ":" not in host:
@@ -120,13 +122,20 @@ def __init__(
120122
**scopes_kwargs, quota_project_id=quota_project_id
121123
)
122124

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

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

131140
# TODO: Remove this function once google-auth >= 1.25.0 is required
132141
@classmethod
@@ -147,27 +156,6 @@ def _get_scopes_kwargs(
147156

148157
return scopes_kwargs
149158

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

‎google/cloud/bigquery_reservation_v1/services/reservation_service/transports/grpc.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ def __init__(
167167
scopes=scopes,
168168
quota_project_id=quota_project_id,
169169
client_info=client_info,
170+
always_use_jwt_access=True,
170171
)
171172

172173
if not self._grpc_channel:
@@ -222,14 +223,14 @@ def create_channel(
222223
and ``credentials_file`` are passed.
223224
"""
224225

225-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
226-
227226
return grpc_helpers.create_channel(
228227
host,
229228
credentials=credentials,
230229
credentials_file=credentials_file,
231230
quota_project_id=quota_project_id,
232-
**self_signed_jwt_kwargs,
231+
default_scopes=cls.AUTH_SCOPES,
232+
scopes=scopes,
233+
default_host=cls.DEFAULT_HOST,
233234
**kwargs,
234235
)
235236

‎google/cloud/bigquery_reservation_v1/services/reservation_service/transports/grpc_asyncio.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,14 @@ def create_channel(
9696
aio.Channel: A gRPC AsyncIO channel object.
9797
"""
9898

99-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
100-
10199
return grpc_helpers_async.create_channel(
102100
host,
103101
credentials=credentials,
104102
credentials_file=credentials_file,
105103
quota_project_id=quota_project_id,
106-
**self_signed_jwt_kwargs,
104+
default_scopes=cls.AUTH_SCOPES,
105+
scopes=scopes,
106+
default_host=cls.DEFAULT_HOST,
107107
**kwargs,
108108
)
109109

@@ -213,6 +213,7 @@ def __init__(
213213
scopes=scopes,
214214
quota_project_id=quota_project_id,
215215
client_info=client_info,
216+
always_use_jwt_access=True,
216217
)
217218

218219
if not self._grpc_channel:

‎setup.py‎

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

‎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==1.4.0
1010
packaging==14.3
1111
google-auth==1.24.0 # TODO: remove when google-auth>=1.25.0 is transitively required through google-api-core

‎tests/unit/gapic/bigquery_reservation_v1/test_reservation_service.py‎

Lines changed: 15 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@
3939
)
4040
from google.cloud.bigquery_reservation_v1.services.reservation_service import pagers
4141
from google.cloud.bigquery_reservation_v1.services.reservation_service import transports
42-
from google.cloud.bigquery_reservation_v1.services.reservation_service.transports.base import (
43-
_API_CORE_VERSION,
44-
)
4542
from google.cloud.bigquery_reservation_v1.services.reservation_service.transports.base import (
4643
_GOOGLE_AUTH_VERSION,
4744
)
@@ -55,8 +52,9 @@
5552
import google.auth
5653

5754

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

70-
requires_api_core_lt_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-
75-
requires_api_core_gte_1_26_0 = pytest.mark.skipif(
76-
packaging.version.parse(_API_CORE_VERSION) < packaging.version.parse("1.26.0"),
77-
reason="This test requires google-api-core >= 1.26.0",
78-
)
79-
8068

8169
def client_cert_source_callback():
8270
return b"cert bytes", b"key bytes"
@@ -140,6 +128,18 @@ def test_reservation_service_client_from_service_account_info(client_class):
140128
assert client.transport._host == "bigqueryreservation.googleapis.com:443"
141129

142130

131+
@pytest.mark.parametrize(
132+
"client_class", [ReservationServiceClient, ReservationServiceAsyncClient,]
133+
)
134+
def test_reservation_service_client_service_account_always_use_jwt(client_class):
135+
with mock.patch.object(
136+
service_account.Credentials, "with_always_use_jwt_access", create=True
137+
) as use_jwt:
138+
creds = service_account.Credentials(None, None, None)
139+
client = client_class(credentials=creds)
140+
use_jwt.assert_called_with(True)
141+
142+
143143
@pytest.mark.parametrize(
144144
"client_class", [ReservationServiceClient, ReservationServiceAsyncClient,]
145145
)
@@ -5847,7 +5847,6 @@ def test_reservation_service_transport_auth_adc_old_google_auth(transport_class)
58475847
(transports.ReservationServiceGrpcAsyncIOTransport, grpc_helpers_async),
58485848
],
58495849
)
5850-
@requires_api_core_gte_1_26_0
58515850
def test_reservation_service_transport_create_channel(transport_class, grpc_helpers):
58525851
# If credentials and host are not provided, the transport class should use
58535852
# ADC credentials.
@@ -5879,82 +5878,6 @@ def test_reservation_service_transport_create_channel(transport_class, grpc_help
58795878
)
58805879

58815880

5882-
@pytest.mark.parametrize(
5883-
"transport_class,grpc_helpers",
5884-
[
5885-
(transports.ReservationServiceGrpcTransport, grpc_helpers),
5886-
(transports.ReservationServiceGrpcAsyncIOTransport, grpc_helpers_async),
5887-
],
5888-
)
5889-
@requires_api_core_lt_1_26_0
5890-
def test_reservation_service_transport_create_channel_old_api_core(
5891-
transport_class, grpc_helpers
5892-
):
5893-
# If credentials and host are not provided, the transport class should use
5894-
# ADC credentials.
5895-
with mock.patch.object(
5896-
google.auth, "default", autospec=True
5897-
) as adc, mock.patch.object(
5898-
grpc_helpers, "create_channel", autospec=True
5899-
) as create_channel:
5900-
creds = ga_credentials.AnonymousCredentials()
5901-
adc.return_value = (creds, None)
5902-
transport_class(quota_project_id="octopus")
5903-
5904-
create_channel.assert_called_with(
5905-
"bigqueryreservation.googleapis.com:443",
5906-
credentials=creds,
5907-
credentials_file=None,
5908-
quota_project_id="octopus",
5909-
scopes=(
5910-
"https://www.googleapis.com/auth/bigquery",
5911-
"https://www.googleapis.com/auth/cloud-platform",
5912-
),
5913-
ssl_credentials=None,
5914-
options=[
5915-
("grpc.max_send_message_length", -1),
5916-
("grpc.max_receive_message_length", -1),
5917-
],
5918-
)
5919-
5920-
5921-
@pytest.mark.parametrize(
5922-
"transport_class,grpc_helpers",
5923-
[
5924-
(transports.ReservationServiceGrpcTransport, grpc_helpers),
5925-
(transports.ReservationServiceGrpcAsyncIOTransport, grpc_helpers_async),
5926-
],
5927-
)
5928-
@requires_api_core_lt_1_26_0
5929-
def test_reservation_service_transport_create_channel_user_scopes(
5930-
transport_class, grpc_helpers
5931-
):
5932-
# If credentials and host are not provided, the transport class should use
5933-
# ADC credentials.
5934-
with mock.patch.object(
5935-
google.auth, "default", autospec=True
5936-
) as adc, mock.patch.object(
5937-
grpc_helpers, "create_channel", autospec=True
5938-
) as create_channel:
5939-
creds = ga_credentials.AnonymousCredentials()
5940-
adc.return_value = (creds, None)
5941-
5942-
transport_class(quota_project_id="octopus", scopes=["1", "2"])
5943-
5944-
create_channel.assert_called_with(
5945-
"bigqueryreservation.googleapis.com:443",
5946-
credentials=creds,
5947-
credentials_file=None,
5948-
quota_project_id="octopus",
5949-
scopes=["1", "2"],
5950-
ssl_credentials=None,
5951-
options=[
5952-
("grpc.max_send_message_length", -1),
5953-
("grpc.max_receive_message_length", -1),
5954-
],
5955-
)
5956-
5957-
59585881
@pytest.mark.parametrize(
59595882
"transport_class",
59605883
[

0 commit comments

Comments
 (0)