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

Commit 2cb3cc2

Browse files
feat: add always_use_jwt_access (#178)
... 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 2d06a28 commit 2cb3cc2

File tree

27 files changed

+226
-741
lines changed

27 files changed

+226
-741
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/datacatalog/__init__.py

google/cloud/datacatalog_v1/services/data_catalog/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.datacatalog_v1.types import datacatalog
2930
from google.cloud.datacatalog_v1.types import tags
@@ -49,8 +50,6 @@
4950
except pkg_resources.DistributionNotFound: # pragma: NO COVER
5051
_GOOGLE_AUTH_VERSION = None
5152

52-
_API_CORE_VERSION = google.api_core.__version__
53-
5453

5554
class DataCatalogTransport(abc.ABC):
5655
"""Abstract transport class for DataCatalog."""
@@ -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:
@@ -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/datacatalog_v1/services/data_catalog/transports/grpc.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ def __init__(
154154
scopes=scopes,
155155
quota_project_id=quota_project_id,
156156
client_info=client_info,
157+
always_use_jwt_access=True,
157158
)
158159

159160
if not self._grpc_channel:
@@ -209,14 +210,14 @@ def create_channel(
209210
and ``credentials_file`` are passed.
210211
"""
211212

212-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
213-
214213
return grpc_helpers.create_channel(
215214
host,
216215
credentials=credentials,
217216
credentials_file=credentials_file,
218217
quota_project_id=quota_project_id,
219-
**self_signed_jwt_kwargs,
218+
default_scopes=cls.AUTH_SCOPES,
219+
scopes=scopes,
220+
default_host=cls.DEFAULT_HOST,
220221
**kwargs,
221222
)
222223

google/cloud/datacatalog_v1/services/data_catalog/transports/grpc_asyncio.py

Lines changed: 4 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

@@ -200,6 +200,7 @@ def __init__(
200200
scopes=scopes,
201201
quota_project_id=quota_project_id,
202202
client_info=client_info,
203+
always_use_jwt_access=True,
203204
)
204205

205206
if not self._grpc_channel:

google/cloud/datacatalog_v1/services/policy_tag_manager/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.datacatalog_v1.types import policytagmanager
2930
from google.iam.v1 import iam_policy_pb2 # type: ignore
@@ -48,8 +49,6 @@
4849
except pkg_resources.DistributionNotFound: # pragma: NO COVER
4950
_GOOGLE_AUTH_VERSION = None
5051

51-
_API_CORE_VERSION = google.api_core.__version__
52-
5352

5453
class PolicyTagManagerTransport(abc.ABC):
5554
"""Abstract transport class for PolicyTagManager."""
@@ -67,6 +66,7 @@ def __init__(
6766
scopes: Optional[Sequence[str]] = None,
6867
quota_project_id: Optional[str] = None,
6968
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
69+
always_use_jwt_access: Optional[bool] = False,
7070
**kwargs,
7171
) -> None:
7272
"""Instantiate the transport.
@@ -90,6 +90,8 @@ def __init__(
9090
API requests. If ``None``, then default info will be used.
9191
Generally, you only need to set this if you're developing
9292
your own client library.
93+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
94+
be used for service account credentials.
9395
"""
9496
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
9597
if ":" not in host:
@@ -118,13 +120,20 @@ def __init__(
118120
**scopes_kwargs, quota_project_id=quota_project_id
119121
)
120122

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

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

129138
# TODO: Remove this function once google-auth >= 1.25.0 is required
130139
@classmethod
@@ -145,27 +154,6 @@ def _get_scopes_kwargs(
145154

146155
return scopes_kwargs
147156

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

google/cloud/datacatalog_v1/services/policy_tag_manager/transports/grpc.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ def __init__(
157157
scopes=scopes,
158158
quota_project_id=quota_project_id,
159159
client_info=client_info,
160+
always_use_jwt_access=True,
160161
)
161162

162163
if not self._grpc_channel:
@@ -212,14 +213,14 @@ def create_channel(
212213
and ``credentials_file`` are passed.
213214
"""
214215

215-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
216-
217216
return grpc_helpers.create_channel(
218217
host,
219218
credentials=credentials,
220219
credentials_file=credentials_file,
221220
quota_project_id=quota_project_id,
222-
**self_signed_jwt_kwargs,
221+
default_scopes=cls.AUTH_SCOPES,
222+
scopes=scopes,
223+
default_host=cls.DEFAULT_HOST,
223224
**kwargs,
224225
)
225226

google/cloud/datacatalog_v1/services/policy_tag_manager/transports/grpc_asyncio.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ def create_channel(
8686
aio.Channel: A gRPC AsyncIO channel object.
8787
"""
8888

89-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
90-
9189
return grpc_helpers_async.create_channel(
9290
host,
9391
credentials=credentials,
9492
credentials_file=credentials_file,
9593
quota_project_id=quota_project_id,
96-
**self_signed_jwt_kwargs,
94+
default_scopes=cls.AUTH_SCOPES,
95+
scopes=scopes,
96+
default_host=cls.DEFAULT_HOST,
9797
**kwargs,
9898
)
9999

@@ -203,6 +203,7 @@ def __init__(
203203
scopes=scopes,
204204
quota_project_id=quota_project_id,
205205
client_info=client_info,
206+
always_use_jwt_access=True,
206207
)
207208

208209
if not self._grpc_channel:

google/cloud/datacatalog_v1/services/policy_tag_manager_serialization/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.datacatalog_v1.types import policytagmanagerserialization
2930

@@ -45,8 +46,6 @@
4546
except pkg_resources.DistributionNotFound: # pragma: NO COVER
4647
_GOOGLE_AUTH_VERSION = None
4748

48-
_API_CORE_VERSION = google.api_core.__version__
49-
5049

5150
class PolicyTagManagerSerializationTransport(abc.ABC):
5251
"""Abstract transport class for PolicyTagManagerSerialization."""
@@ -64,6 +63,7 @@ def __init__(
6463
scopes: Optional[Sequence[str]] = None,
6564
quota_project_id: Optional[str] = None,
6665
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
66+
always_use_jwt_access: Optional[bool] = False,
6767
**kwargs,
6868
) -> None:
6969
"""Instantiate the transport.
@@ -87,6 +87,8 @@ def __init__(
8787
API requests. If ``None``, then default info will be used.
8888
Generally, you only need to set this if you're developing
8989
your own client library.
90+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
91+
be used for service account credentials.
9092
"""
9193
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
9294
if ":" not in host:
@@ -115,13 +117,20 @@ def __init__(
115117
**scopes_kwargs, quota_project_id=quota_project_id
116118
)
117119

120+
# If the credentials is service account credentials, then always try to use self signed JWT.
121+
if (
122+
always_use_jwt_access
123+
and isinstance(credentials, service_account.Credentials)
124+
and hasattr(service_account.Credentials, "with_always_use_jwt_access")
125+
):
126+
credentials = credentials.with_always_use_jwt_access(True)
127+
118128
# Save the credentials.
119129
self._credentials = credentials
120130

121-
# TODO(busunkim): These two class methods are in the base transport
131+
# TODO(busunkim): This method is in the base transport
122132
# to avoid duplicating code across the transport classes. These functions
123-
# should be deleted once the minimum required versions of google-api-core
124-
# and google-auth are increased.
133+
# should be deleted once the minimum required versions of google-auth is increased.
125134

126135
# TODO: Remove this function once google-auth >= 1.25.0 is required
127136
@classmethod
@@ -142,27 +151,6 @@ def _get_scopes_kwargs(
142151

143152
return scopes_kwargs
144153

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

google/cloud/datacatalog_v1/services/policy_tag_manager_serialization/transports/grpc.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ def __init__(
153153
scopes=scopes,
154154
quota_project_id=quota_project_id,
155155
client_info=client_info,
156+
always_use_jwt_access=True,
156157
)
157158

158159
if not self._grpc_channel:
@@ -208,14 +209,14 @@ def create_channel(
208209
and ``credentials_file`` are passed.
209210
"""
210211

211-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
212-
213212
return grpc_helpers.create_channel(
214213
host,
215214
credentials=credentials,
216215
credentials_file=credentials_file,
217216
quota_project_id=quota_project_id,
218-
**self_signed_jwt_kwargs,
217+
default_scopes=cls.AUTH_SCOPES,
218+
scopes=scopes,
219+
default_host=cls.DEFAULT_HOST,
219220
**kwargs,
220221
)
221222

0 commit comments

Comments
 (0)