Skip to content

Commit b30d7c7

Browse files
feat: add always_use_jwt_access (#86)
... 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 af3bbed commit b30d7c7

File tree

11 files changed

+72
-245
lines changed

11 files changed

+72
-245
lines changed

packages/google-cloud-billing/.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/billing/__init__.py

packages/google-cloud-billing/google/cloud/billing_v1/services/cloud_billing/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.billing_v1.types import cloud_billing
2930
from google.iam.v1 import iam_policy_pb2 # type: ignore
@@ -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 CloudBillingTransport(abc.ABC):
5251
"""Abstract transport class for CloudBilling."""
@@ -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 = {

packages/google-cloud-billing/google/cloud/billing_v1/services/cloud_billing/transports/grpc.py

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

157158
if not self._grpc_channel:
@@ -207,14 +208,14 @@ def create_channel(
207208
and ``credentials_file`` are passed.
208209
"""
209210

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

packages/google-cloud-billing/google/cloud/billing_v1/services/cloud_billing/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-billing/google/cloud/billing_v1/services/cloud_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.billing_v1.types import cloud_catalog
2930

@@ -43,8 +44,6 @@
4344
except pkg_resources.DistributionNotFound: # pragma: NO COVER
4445
_GOOGLE_AUTH_VERSION = None
4546

46-
_API_CORE_VERSION = google.api_core.__version__
47-
4847

4948
class CloudCatalogTransport(abc.ABC):
5049
"""Abstract transport class for CloudCatalog."""
@@ -62,6 +61,7 @@ def __init__(
6261
scopes: Optional[Sequence[str]] = None,
6362
quota_project_id: Optional[str] = None,
6463
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
64+
always_use_jwt_access: Optional[bool] = False,
6565
**kwargs,
6666
) -> None:
6767
"""Instantiate the transport.
@@ -85,6 +85,8 @@ def __init__(
8585
API requests. If ``None``, then default info will be used.
8686
Generally, you only need to set this if you're developing
8787
your own client library.
88+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
89+
be used for service account credentials.
8890
"""
8991
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
9092
if ":" not in host:
@@ -113,13 +115,20 @@ def __init__(
113115
**scopes_kwargs, quota_project_id=quota_project_id
114116
)
115117

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

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

124133
# TODO: Remove this function once google-auth >= 1.25.0 is required
125134
@classmethod
@@ -140,27 +149,6 @@ def _get_scopes_kwargs(
140149

141150
return scopes_kwargs
142151

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

packages/google-cloud-billing/google/cloud/billing_v1/services/cloud_catalog/transports/grpc.py

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

156157
if not self._grpc_channel:
@@ -206,14 +207,14 @@ def create_channel(
206207
and ``credentials_file`` are passed.
207208
"""
208209

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

packages/google-cloud-billing/google/cloud/billing_v1/services/cloud_catalog/transports/grpc_asyncio.py

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

@@ -197,6 +197,7 @@ def __init__(
197197
scopes=scopes,
198198
quota_project_id=quota_project_id,
199199
client_info=client_info,
200+
always_use_jwt_access=True,
200201
)
201202

202203
if not self._grpc_channel:

packages/google-cloud-billing/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
"grpc-google-iam-v1 >= 0.12.3, < 0.13.0",
4949
"proto-plus >= 1.10.0",
5050
"packaging >= 14.3",

packages/google-cloud-billing/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
grpc-google-iam-v1==0.12.3
1010
proto-plus==1.10.0
1111
packaging==14.3

0 commit comments

Comments
 (0)