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

Commit 6414a3b

Browse files
feat: add always_use_jwt_access (#118)
... 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 36c4a83 commit 6414a3b

File tree

6 files changed

+37
-123
lines changed

6 files changed

+37
-123
lines changed

google/cloud/devtools/cloudbuild_v1/services/cloud_build/transports/base.py

+14-26
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.devtools.cloudbuild_v1.types import cloudbuild
3031
from google.longrunning import operations_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 CloudBuildTransport(abc.ABC):
5554
"""Abstract transport class for CloudBuild."""
@@ -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/devtools/cloudbuild_v1/services/cloud_build/transports/grpc.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ def __init__(
160160
scopes=scopes,
161161
quota_project_id=quota_project_id,
162162
client_info=client_info,
163+
always_use_jwt_access=True,
163164
)
164165

165166
if not self._grpc_channel:
@@ -215,14 +216,14 @@ def create_channel(
215216
and ``credentials_file`` are passed.
216217
"""
217218

218-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
219-
220219
return grpc_helpers.create_channel(
221220
host,
222221
credentials=credentials,
223222
credentials_file=credentials_file,
224223
quota_project_id=quota_project_id,
225-
**self_signed_jwt_kwargs,
224+
default_scopes=cls.AUTH_SCOPES,
225+
scopes=scopes,
226+
default_host=cls.DEFAULT_HOST,
226227
**kwargs,
227228
)
228229

google/cloud/devtools/cloudbuild_v1/services/cloud_build/transports/grpc_asyncio.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ def create_channel(
8888
aio.Channel: A gRPC AsyncIO channel object.
8989
"""
9090

91-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
92-
9391
return grpc_helpers_async.create_channel(
9492
host,
9593
credentials=credentials,
9694
credentials_file=credentials_file,
9795
quota_project_id=quota_project_id,
98-
**self_signed_jwt_kwargs,
96+
default_scopes=cls.AUTH_SCOPES,
97+
scopes=scopes,
98+
default_host=cls.DEFAULT_HOST,
9999
**kwargs,
100100
)
101101

@@ -206,6 +206,7 @@ def __init__(
206206
scopes=scopes,
207207
quota_project_id=quota_project_id,
208208
client_info=client_info,
209+
always_use_jwt_access=True,
209210
)
210211

211212
if not self._grpc_channel:

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
# 'Development Status :: 5 - Production/Stable'
2929
release_status = "Development Status :: 5 - Production/Stable"
3030
dependencies = [
31-
"google-api-core[grpc] >= 1.22.2, < 2.0.0dev",
31+
"google-api-core[grpc] >= 1.26.0, <2.0.0dev",
3232
"proto-plus >= 0.4.0",
3333
"packaging >= 14.3",
3434
]

testing/constraints-3.6.txt

+1-1
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==0.4.0
1010
libcst==0.2.5
1111
packaging==14.3

tests/unit/gapic/cloudbuild_v1/test_cloud_build.py

+13-89
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@
4141
from google.cloud.devtools.cloudbuild_v1.services.cloud_build import CloudBuildClient
4242
from google.cloud.devtools.cloudbuild_v1.services.cloud_build import pagers
4343
from google.cloud.devtools.cloudbuild_v1.services.cloud_build import transports
44-
from google.cloud.devtools.cloudbuild_v1.services.cloud_build.transports.base import (
45-
_API_CORE_VERSION,
46-
)
4744
from google.cloud.devtools.cloudbuild_v1.services.cloud_build.transports.base import (
4845
_GOOGLE_AUTH_VERSION,
4946
)
@@ -56,8 +53,9 @@
5653
import google.auth
5754

5855

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

71-
requires_api_core_lt_1_26_0 = pytest.mark.skipif(
72-
packaging.version.parse(_API_CORE_VERSION) >= packaging.version.parse("1.26.0"),
73-
reason="This test requires google-api-core < 1.26.0",
74-
)
75-
76-
requires_api_core_gte_1_26_0 = pytest.mark.skipif(
77-
packaging.version.parse(_API_CORE_VERSION) < packaging.version.parse("1.26.0"),
78-
reason="This test requires google-api-core >= 1.26.0",
79-
)
80-
8169

8270
def client_cert_source_callback():
8371
return b"cert bytes", b"key bytes"
@@ -135,6 +123,16 @@ def test_cloud_build_client_from_service_account_info(client_class):
135123
assert client.transport._host == "cloudbuild.googleapis.com:443"
136124

137125

126+
@pytest.mark.parametrize("client_class", [CloudBuildClient, CloudBuildAsyncClient,])
127+
def test_cloud_build_client_service_account_always_use_jwt(client_class):
128+
with mock.patch.object(
129+
service_account.Credentials, "with_always_use_jwt_access", create=True
130+
) as use_jwt:
131+
creds = service_account.Credentials(None, None, None)
132+
client = client_class(credentials=creds)
133+
use_jwt.assert_called_with(True)
134+
135+
138136
@pytest.mark.parametrize("client_class", [CloudBuildClient, CloudBuildAsyncClient,])
139137
def test_cloud_build_client_from_service_account_file(client_class):
140138
creds = ga_credentials.AnonymousCredentials()
@@ -3494,7 +3492,6 @@ def test_cloud_build_transport_auth_adc_old_google_auth(transport_class):
34943492
(transports.CloudBuildGrpcAsyncIOTransport, grpc_helpers_async),
34953493
],
34963494
)
3497-
@requires_api_core_gte_1_26_0
34983495
def test_cloud_build_transport_create_channel(transport_class, grpc_helpers):
34993496
# If credentials and host are not provided, the transport class should use
35003497
# ADC credentials.
@@ -3523,79 +3520,6 @@ def test_cloud_build_transport_create_channel(transport_class, grpc_helpers):
35233520
)
35243521

35253522

3526-
@pytest.mark.parametrize(
3527-
"transport_class,grpc_helpers",
3528-
[
3529-
(transports.CloudBuildGrpcTransport, grpc_helpers),
3530-
(transports.CloudBuildGrpcAsyncIOTransport, grpc_helpers_async),
3531-
],
3532-
)
3533-
@requires_api_core_lt_1_26_0
3534-
def test_cloud_build_transport_create_channel_old_api_core(
3535-
transport_class, grpc_helpers
3536-
):
3537-
# If credentials and host are not provided, the transport class should use
3538-
# ADC credentials.
3539-
with mock.patch.object(
3540-
google.auth, "default", autospec=True
3541-
) as adc, mock.patch.object(
3542-
grpc_helpers, "create_channel", autospec=True
3543-
) as create_channel:
3544-
creds = ga_credentials.AnonymousCredentials()
3545-
adc.return_value = (creds, None)
3546-
transport_class(quota_project_id="octopus")
3547-
3548-
create_channel.assert_called_with(
3549-
"cloudbuild.googleapis.com:443",
3550-
credentials=creds,
3551-
credentials_file=None,
3552-
quota_project_id="octopus",
3553-
scopes=("https://www.googleapis.com/auth/cloud-platform",),
3554-
ssl_credentials=None,
3555-
options=[
3556-
("grpc.max_send_message_length", -1),
3557-
("grpc.max_receive_message_length", -1),
3558-
],
3559-
)
3560-
3561-
3562-
@pytest.mark.parametrize(
3563-
"transport_class,grpc_helpers",
3564-
[
3565-
(transports.CloudBuildGrpcTransport, grpc_helpers),
3566-
(transports.CloudBuildGrpcAsyncIOTransport, grpc_helpers_async),
3567-
],
3568-
)
3569-
@requires_api_core_lt_1_26_0
3570-
def test_cloud_build_transport_create_channel_user_scopes(
3571-
transport_class, grpc_helpers
3572-
):
3573-
# If credentials and host are not provided, the transport class should use
3574-
# ADC credentials.
3575-
with mock.patch.object(
3576-
google.auth, "default", autospec=True
3577-
) as adc, mock.patch.object(
3578-
grpc_helpers, "create_channel", autospec=True
3579-
) as create_channel:
3580-
creds = ga_credentials.AnonymousCredentials()
3581-
adc.return_value = (creds, None)
3582-
3583-
transport_class(quota_project_id="octopus", scopes=["1", "2"])
3584-
3585-
create_channel.assert_called_with(
3586-
"cloudbuild.googleapis.com:443",
3587-
credentials=creds,
3588-
credentials_file=None,
3589-
quota_project_id="octopus",
3590-
scopes=["1", "2"],
3591-
ssl_credentials=None,
3592-
options=[
3593-
("grpc.max_send_message_length", -1),
3594-
("grpc.max_receive_message_length", -1),
3595-
],
3596-
)
3597-
3598-
35993523
@pytest.mark.parametrize(
36003524
"transport_class",
36013525
[transports.CloudBuildGrpcTransport, transports.CloudBuildGrpcAsyncIOTransport],

0 commit comments

Comments
 (0)