Visitar URL original
feat: add always_use_jwt_access (#172) · googleapis/python-dlp@fb86805 · GitHub
Skip to content
This repository was archived by the owner on Dec 10, 2023. It is now read-only.

Commit fb86805

Browse files
feat: add always_use_jwt_access (#172)
... 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 d2da290 commit fb86805

7 files changed

Lines changed: 37 additions & 122 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/dlp/__init__.py

‎google/cloud/dlp_v2/services/dlp_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.dlp_v2.types import dlp
2930
from google.protobuf import empty_pb2 # type: ignore
@@ -44,8 +45,6 @@
4445
except pkg_resources.DistributionNotFound: # pragma: NO COVER
4546
_GOOGLE_AUTH_VERSION = None
4647

47-
_API_CORE_VERSION = google.api_core.__version__
48-
4948

5049
class DlpServiceTransport(abc.ABC):
5150
"""Abstract transport class for DlpService."""
@@ -63,6 +62,7 @@ def __init__(
6362
scopes: Optional[Sequence[str]] = None,
6463
quota_project_id: Optional[str] = None,
6564
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
65+
always_use_jwt_access: Optional[bool] = False,
6666
**kwargs,
6767
) -> None:
6868
"""Instantiate the transport.
@@ -86,6 +86,8 @@ def __init__(
8686
API requests. If ``None``, then default info will be used.
8787
Generally, you only need to set this if you're developing
8888
your own client library.
89+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
90+
be used for service account credentials.
8991
"""
9092
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
9193
if ":" not in host:
@@ -114,13 +116,20 @@ def __init__(
114116
**scopes_kwargs, quota_project_id=quota_project_id
115117
)
116118

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

120-
# TODO(busunkim): These two class methods are in the base transport
130+
# TODO(busunkim): This method is in the base transport
121131
# to avoid duplicating code across the transport classes. These functions
122-
# should be deleted once the minimum required versions of google-api-core
123-
# and google-auth are increased.
132+
# should be deleted once the minimum required versions of google-auth is increased.
124133

125134
# TODO: Remove this function once google-auth >= 1.25.0 is required
126135
@classmethod
@@ -141,27 +150,6 @@ def _get_scopes_kwargs(
141150

142151
return scopes_kwargs
143152

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

‎google/cloud/dlp_v2/services/dlp_service/transports/grpc.py‎

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

163164
if not self._grpc_channel:
@@ -213,14 +214,14 @@ def create_channel(
213214
and ``credentials_file`` are passed.
214215
"""
215216

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

‎google/cloud/dlp_v2/services/dlp_service/transports/grpc_asyncio.py‎

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

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

@@ -204,6 +204,7 @@ def __init__(
204204
scopes=scopes,
205205
quota_project_id=quota_project_id,
206206
client_info=client_info,
207+
always_use_jwt_access=True,
207208
)
208209

209210
if not self._grpc_channel:

‎setup.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
version = "3.1.1"
2525
release_status = "Development Status :: 5 - Production/Stable"
2626
dependencies = [
27-
"google-api-core[grpc] >= 1.22.2, < 2.0.0dev",
27+
"google-api-core[grpc] >= 1.26.0, <2.0.0dev",
2828
"proto-plus >= 0.4.0",
2929
"packaging >= 14.3",
3030
]

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

‎tests/unit/gapic/dlp_v2/test_dlp_service.py‎

Lines changed: 13 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
from google.cloud.dlp_v2.services.dlp_service import DlpServiceClient
3636
from google.cloud.dlp_v2.services.dlp_service import pagers
3737
from google.cloud.dlp_v2.services.dlp_service import transports
38-
from google.cloud.dlp_v2.services.dlp_service.transports.base import _API_CORE_VERSION
3938
from google.cloud.dlp_v2.services.dlp_service.transports.base import (
4039
_GOOGLE_AUTH_VERSION,
4140
)
@@ -53,8 +52,9 @@
5352
import google.auth
5453

5554

56-
# TODO(busunkim): Once google-api-core >= 1.26.0 is required:
57-
# - 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
5858
# - Delete these pytest markers (Make the "greater than or equal to" tests the default).
5959
requires_google_auth_lt_1_25_0 = pytest.mark.skipif(
6060
packaging.version.parse(_GOOGLE_AUTH_VERSION) >= packaging.version.parse("1.25.0"),
@@ -65,16 +65,6 @@
6565
reason="This test requires google-auth >= 1.25.0",
6666
)
6767

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

7969
def client_cert_source_callback():
8070
return b"cert bytes", b"key bytes"
@@ -132,6 +122,16 @@ def test_dlp_service_client_from_service_account_info(client_class):
132122
assert client.transport._host == "dlp.googleapis.com:443"
133123

134124

125+
@pytest.mark.parametrize("client_class", [DlpServiceClient, DlpServiceAsyncClient,])
126+
def test_dlp_service_client_service_account_always_use_jwt(client_class):
127+
with mock.patch.object(
128+
service_account.Credentials, "with_always_use_jwt_access", create=True
129+
) as use_jwt:
130+
creds = service_account.Credentials(None, None, None)
131+
client = client_class(credentials=creds)
132+
use_jwt.assert_called_with(True)
133+
134+
135135
@pytest.mark.parametrize("client_class", [DlpServiceClient, DlpServiceAsyncClient,])
136136
def test_dlp_service_client_from_service_account_file(client_class):
137137
creds = ga_credentials.AnonymousCredentials()
@@ -8221,7 +8221,6 @@ def test_dlp_service_transport_auth_adc_old_google_auth(transport_class):
82218221
(transports.DlpServiceGrpcAsyncIOTransport, grpc_helpers_async),
82228222
],
82238223
)
8224-
@requires_api_core_gte_1_26_0
82258224
def test_dlp_service_transport_create_channel(transport_class, grpc_helpers):
82268225
# If credentials and host are not provided, the transport class should use
82278226
# ADC credentials.
@@ -8250,79 +8249,6 @@ def test_dlp_service_transport_create_channel(transport_class, grpc_helpers):
82508249
)
82518250

82528251

8253-
@pytest.mark.parametrize(
8254-
"transport_class,grpc_helpers",
8255-
[
8256-
(transports.DlpServiceGrpcTransport, grpc_helpers),
8257-
(transports.DlpServiceGrpcAsyncIOTransport, grpc_helpers_async),
8258-
],
8259-
)
8260-
@requires_api_core_lt_1_26_0
8261-
def test_dlp_service_transport_create_channel_old_api_core(
8262-
transport_class, grpc_helpers
8263-
):
8264-
# If credentials and host are not provided, the transport class should use
8265-
# ADC credentials.
8266-
with mock.patch.object(
8267-
google.auth, "default", autospec=True
8268-
) as adc, mock.patch.object(
8269-
grpc_helpers, "create_channel", autospec=True
8270-
) as create_channel:
8271-
creds = ga_credentials.AnonymousCredentials()
8272-
adc.return_value = (creds, None)
8273-
transport_class(quota_project_id="octopus")
8274-
8275-
create_channel.assert_called_with(
8276-
"dlp.googleapis.com:443",
8277-
credentials=creds,
8278-
credentials_file=None,
8279-
quota_project_id="octopus",
8280-
scopes=("https://www.googleapis.com/auth/cloud-platform",),
8281-
ssl_credentials=None,
8282-
options=[
8283-
("grpc.max_send_message_length", -1),
8284-
("grpc.max_receive_message_length", -1),
8285-
],
8286-
)
8287-
8288-
8289-
@pytest.mark.parametrize(
8290-
"transport_class,grpc_helpers",
8291-
[
8292-
(transports.DlpServiceGrpcTransport, grpc_helpers),
8293-
(transports.DlpServiceGrpcAsyncIOTransport, grpc_helpers_async),
8294-
],
8295-
)
8296-
@requires_api_core_lt_1_26_0
8297-
def test_dlp_service_transport_create_channel_user_scopes(
8298-
transport_class, grpc_helpers
8299-
):
8300-
# If credentials and host are not provided, the transport class should use
8301-
# ADC credentials.
8302-
with mock.patch.object(
8303-
google.auth, "default", autospec=True
8304-
) as adc, mock.patch.object(
8305-
grpc_helpers, "create_channel", autospec=True
8306-
) as create_channel:
8307-
creds = ga_credentials.AnonymousCredentials()
8308-
adc.return_value = (creds, None)
8309-
8310-
transport_class(quota_project_id="octopus", scopes=["1", "2"])
8311-
8312-
create_channel.assert_called_with(
8313-
"dlp.googleapis.com:443",
8314-
credentials=creds,
8315-
credentials_file=None,
8316-
quota_project_id="octopus",
8317-
scopes=["1", "2"],
8318-
ssl_credentials=None,
8319-
options=[
8320-
("grpc.max_send_message_length", -1),
8321-
("grpc.max_receive_message_length", -1),
8322-
],
8323-
)
8324-
8325-
83268252
@pytest.mark.parametrize(
83278253
"transport_class",
83288254
[transports.DlpServiceGrpcTransport, transports.DlpServiceGrpcAsyncIOTransport],

0 commit comments

Comments
 (0)