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

Commit 9bf371d

Browse files
feat: add always_use_jwt_access (#320)
... 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 fix: disable always_use_jwt_access
1 parent 91d3532 commit 9bf371d

128 files changed

Lines changed: 1950 additions & 4063 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/.OwlBot.lock.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
docker:
22
image: gcr.io/repo-automation-bots/owlbot-python:latest
3-
digest: sha256:99d90d097e4a4710cc8658ee0b5b963f4426d0e424819787c3ac1405c9a26719
3+
digest: sha256:0444e8caf72b8ce2f71a905dfd029ae4b01d1d07676db39e0157aadddc998c16

google/cloud/dialogflow_v2/services/agents/transports/base.py

Lines changed: 15 additions & 27 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.dialogflow_v2.types import agent
3031
from google.cloud.dialogflow_v2.types import agent as gcd_agent
@@ -50,8 +51,6 @@
5051
except pkg_resources.DistributionNotFound: # pragma: NO COVER
5152
_GOOGLE_AUTH_VERSION = None
5253

53-
_API_CORE_VERSION = google.api_core.__version__
54-
5554

5655
class AgentsTransport(abc.ABC):
5756
"""Abstract transport class for Agents."""
@@ -72,6 +71,7 @@ def __init__(
7271
scopes: Optional[Sequence[str]] = None,
7372
quota_project_id: Optional[str] = None,
7473
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
74+
always_use_jwt_access: Optional[bool] = False,
7575
**kwargs,
7676
) -> None:
7777
"""Instantiate the transport.
@@ -95,6 +95,8 @@ def __init__(
9595
API requests. If ``None``, then default info will be used.
9696
Generally, you only need to set this if you're developing
9797
your own client library.
98+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
99+
be used for service account credentials.
98100
"""
99101
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
100102
if ":" not in host:
@@ -104,7 +106,7 @@ def __init__(
104106
scopes_kwargs = self._get_scopes_kwargs(self._host, scopes)
105107

106108
# Save the scopes.
107-
self._scopes = scopes or self.AUTH_SCOPES
109+
self._scopes = scopes
108110

109111
# If no credentials are provided, then determine the appropriate
110112
# defaults.
@@ -123,13 +125,20 @@ def __init__(
123125
**scopes_kwargs, quota_project_id=quota_project_id
124126
)
125127

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

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

134143
# TODO: Remove this function once google-auth >= 1.25.0 is required
135144
@classmethod
@@ -150,27 +159,6 @@ def _get_scopes_kwargs(
150159

151160
return scopes_kwargs
152161

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

google/cloud/dialogflow_v2/services/agents/transports/grpc.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def __init__(
6262
client_cert_source_for_mtls: Callable[[], Tuple[bytes, bytes]] = None,
6363
quota_project_id: Optional[str] = None,
6464
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
65+
always_use_jwt_access: Optional[bool] = False,
6566
) -> None:
6667
"""Instantiate the transport.
6768
@@ -102,6 +103,8 @@ def __init__(
102103
API requests. If ``None``, then default info will be used.
103104
Generally, you only need to set this if you're developing
104105
your own client library.
106+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
107+
be used for service account credentials.
105108
106109
Raises:
107110
google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport
@@ -155,6 +158,7 @@ def __init__(
155158
scopes=scopes,
156159
quota_project_id=quota_project_id,
157160
client_info=client_info,
161+
always_use_jwt_access=always_use_jwt_access,
158162
)
159163

160164
if not self._grpc_channel:
@@ -210,14 +214,14 @@ def create_channel(
210214
and ``credentials_file`` are passed.
211215
"""
212216

213-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
214-
215217
return grpc_helpers.create_channel(
216218
host,
217219
credentials=credentials,
218220
credentials_file=credentials_file,
219221
quota_project_id=quota_project_id,
220-
**self_signed_jwt_kwargs,
222+
default_scopes=cls.AUTH_SCOPES,
223+
scopes=scopes,
224+
default_host=cls.DEFAULT_HOST,
221225
**kwargs,
222226
)
223227

google/cloud/dialogflow_v2/services/agents/transports/grpc_asyncio.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ def create_channel(
8383
aio.Channel: A gRPC AsyncIO channel object.
8484
"""
8585

86-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
87-
8886
return grpc_helpers_async.create_channel(
8987
host,
9088
credentials=credentials,
9189
credentials_file=credentials_file,
9290
quota_project_id=quota_project_id,
93-
**self_signed_jwt_kwargs,
91+
default_scopes=cls.AUTH_SCOPES,
92+
scopes=scopes,
93+
default_host=cls.DEFAULT_HOST,
9494
**kwargs,
9595
)
9696

@@ -108,6 +108,7 @@ def __init__(
108108
client_cert_source_for_mtls: Callable[[], Tuple[bytes, bytes]] = None,
109109
quota_project_id=None,
110110
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
111+
always_use_jwt_access: Optional[bool] = False,
111112
) -> None:
112113
"""Instantiate the transport.
113114
@@ -149,6 +150,8 @@ def __init__(
149150
API requests. If ``None``, then default info will be used.
150151
Generally, you only need to set this if you're developing
151152
your own client library.
153+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
154+
be used for service account credentials.
152155
153156
Raises:
154157
google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport
@@ -201,6 +204,7 @@ def __init__(
201204
scopes=scopes,
202205
quota_project_id=quota_project_id,
203206
client_info=client_info,
207+
always_use_jwt_access=always_use_jwt_access,
204208
)
205209

206210
if not self._grpc_channel:

google/cloud/dialogflow_v2/services/answer_records/transports/base.py

Lines changed: 15 additions & 27 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.dialogflow_v2.types import answer_record
2930
from google.cloud.dialogflow_v2.types import answer_record as gcd_answer_record
@@ -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 AnswerRecordsTransport(abc.ABC):
5352
"""Abstract transport class for AnswerRecords."""
@@ -68,6 +67,7 @@ def __init__(
6867
scopes: Optional[Sequence[str]] = None,
6968
quota_project_id: Optional[str] = None,
7069
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
70+
always_use_jwt_access: Optional[bool] = False,
7171
**kwargs,
7272
) -> None:
7373
"""Instantiate the transport.
@@ -91,6 +91,8 @@ def __init__(
9191
API requests. If ``None``, then default info will be used.
9292
Generally, you only need to set this if you're developing
9393
your own client library.
94+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
95+
be used for service account credentials.
9496
"""
9597
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
9698
if ":" not in host:
@@ -100,7 +102,7 @@ def __init__(
100102
scopes_kwargs = self._get_scopes_kwargs(self._host, scopes)
101103

102104
# Save the scopes.
103-
self._scopes = scopes or self.AUTH_SCOPES
105+
self._scopes = scopes
104106

105107
# If no credentials are provided, then determine the appropriate
106108
# defaults.
@@ -119,13 +121,20 @@ def __init__(
119121
**scopes_kwargs, quota_project_id=quota_project_id
120122
)
121123

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

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

130139
# TODO: Remove this function once google-auth >= 1.25.0 is required
131140
@classmethod
@@ -146,27 +155,6 @@ def _get_scopes_kwargs(
146155

147156
return scopes_kwargs
148157

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

google/cloud/dialogflow_v2/services/answer_records/transports/grpc.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def __init__(
5959
client_cert_source_for_mtls: Callable[[], Tuple[bytes, bytes]] = None,
6060
quota_project_id: Optional[str] = None,
6161
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
62+
always_use_jwt_access: Optional[bool] = False,
6263
) -> None:
6364
"""Instantiate the transport.
6465
@@ -99,6 +100,8 @@ def __init__(
99100
API requests. If ``None``, then default info will be used.
100101
Generally, you only need to set this if you're developing
101102
your own client library.
103+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
104+
be used for service account credentials.
102105
103106
Raises:
104107
google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport
@@ -151,6 +154,7 @@ def __init__(
151154
scopes=scopes,
152155
quota_project_id=quota_project_id,
153156
client_info=client_info,
157+
always_use_jwt_access=always_use_jwt_access,
154158
)
155159

156160
if not self._grpc_channel:
@@ -206,14 +210,14 @@ def create_channel(
206210
and ``credentials_file`` are passed.
207211
"""
208212

209-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
210-
211213
return grpc_helpers.create_channel(
212214
host,
213215
credentials=credentials,
214216
credentials_file=credentials_file,
215217
quota_project_id=quota_project_id,
216-
**self_signed_jwt_kwargs,
218+
default_scopes=cls.AUTH_SCOPES,
219+
scopes=scopes,
220+
default_host=cls.DEFAULT_HOST,
217221
**kwargs,
218222
)
219223

google/cloud/dialogflow_v2/services/answer_records/transports/grpc_asyncio.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ def create_channel(
8080
aio.Channel: A gRPC AsyncIO channel object.
8181
"""
8282

83-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
84-
8583
return grpc_helpers_async.create_channel(
8684
host,
8785
credentials=credentials,
8886
credentials_file=credentials_file,
8987
quota_project_id=quota_project_id,
90-
**self_signed_jwt_kwargs,
88+
default_scopes=cls.AUTH_SCOPES,
89+
scopes=scopes,
90+
default_host=cls.DEFAULT_HOST,
9191
**kwargs,
9292
)
9393

@@ -105,6 +105,7 @@ def __init__(
105105
client_cert_source_for_mtls: Callable[[], Tuple[bytes, bytes]] = None,
106106
quota_project_id=None,
107107
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
108+
always_use_jwt_access: Optional[bool] = False,
108109
) -> None:
109110
"""Instantiate the transport.
110111
@@ -146,6 +147,8 @@ def __init__(
146147
API requests. If ``None``, then default info will be used.
147148
Generally, you only need to set this if you're developing
148149
your own client library.
150+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
151+
be used for service account credentials.
149152
150153
Raises:
151154
google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport
@@ -197,6 +200,7 @@ def __init__(
197200
scopes=scopes,
198201
quota_project_id=quota_project_id,
199202
client_info=client_info,
203+
always_use_jwt_access=always_use_jwt_access,
200204
)
201205

202206
if not self._grpc_channel:

0 commit comments

Comments
 (0)