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

Commit 268fdec

Browse files
feat: add always_use_jwt_access (#89)
... 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 f71b1f1 commit 268fdec

7 files changed

Lines changed: 39 additions & 129 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/analytics/admin/__init__.py

google/analytics/admin_v1alpha/services/analytics_admin_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.analytics.admin_v1alpha.types import analytics_admin
2930
from google.analytics.admin_v1alpha.types import resources
@@ -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 AnalyticsAdminServiceTransport(abc.ABC):
5251
"""Abstract transport class for AnalyticsAdminService."""
@@ -69,6 +68,7 @@ def __init__(
6968
scopes: Optional[Sequence[str]] = None,
7069
quota_project_id: Optional[str] = None,
7170
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
71+
always_use_jwt_access: Optional[bool] = False,
7272
**kwargs,
7373
) -> None:
7474
"""Instantiate the transport.
@@ -92,6 +92,8 @@ def __init__(
9292
API requests. If ``None``, then default info will be used.
9393
Generally, you only need to set this if you're developing
9494
your own client library.
95+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
96+
be used for service account credentials.
9597
"""
9698
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
9799
if ":" not in host:
@@ -120,13 +122,20 @@ def __init__(
120122
**scopes_kwargs, quota_project_id=quota_project_id
121123
)
122124

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

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

131140
# TODO: Remove this function once google-auth >= 1.25.0 is required
132141
@classmethod
@@ -147,27 +156,6 @@ def _get_scopes_kwargs(
147156

148157
return scopes_kwargs
149158

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

google/analytics/admin_v1alpha/services/analytics_admin_service/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

google/analytics/admin_v1alpha/services/analytics_admin_service/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:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
platforms="Posix; MacOS X; Windows",
4646
include_package_data=True,
4747
install_requires=(
48-
"google-api-core[grpc] >= 1.22.2, < 2.0.0dev",
48+
"google-api-core[grpc] >= 1.26.0, <2.0.0dev",
4949
"proto-plus >= 1.4.0",
5050
"packaging >= 14.3",
5151
),

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==1.4.0
1010
packaging==14.3
1111
google-auth==1.24.0 # TODO: remove when google-auth>=1.25.0 is required through google-api-core

tests/unit/gapic/admin_v1alpha/test_analytics_admin_service.py

Lines changed: 15 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@
3232
)
3333
from google.analytics.admin_v1alpha.services.analytics_admin_service import pagers
3434
from google.analytics.admin_v1alpha.services.analytics_admin_service import transports
35-
from google.analytics.admin_v1alpha.services.analytics_admin_service.transports.base import (
36-
_API_CORE_VERSION,
37-
)
3835
from google.analytics.admin_v1alpha.services.analytics_admin_service.transports.base import (
3936
_GOOGLE_AUTH_VERSION,
4037
)
@@ -54,8 +51,9 @@
5451
import google.auth
5552

5653

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

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

8068
def client_cert_source_callback():
8169
return b"cert bytes", b"key bytes"
@@ -139,6 +127,18 @@ def test_analytics_admin_service_client_from_service_account_info(client_class):
139127
assert client.transport._host == "analyticsadmin.googleapis.com:443"
140128

141129

130+
@pytest.mark.parametrize(
131+
"client_class", [AnalyticsAdminServiceClient, AnalyticsAdminServiceAsyncClient,]
132+
)
133+
def test_analytics_admin_service_client_service_account_always_use_jwt(client_class):
134+
with mock.patch.object(
135+
service_account.Credentials, "with_always_use_jwt_access", create=True
136+
) as use_jwt:
137+
creds = service_account.Credentials(None, None, None)
138+
client = client_class(credentials=creds)
139+
use_jwt.assert_called_with(True)
140+
141+
142142
@pytest.mark.parametrize(
143143
"client_class", [AnalyticsAdminServiceClient, AnalyticsAdminServiceAsyncClient,]
144144
)
@@ -18279,7 +18279,6 @@ def test_analytics_admin_service_transport_auth_adc_old_google_auth(transport_cl
1827918279
(transports.AnalyticsAdminServiceGrpcAsyncIOTransport, grpc_helpers_async),
1828018280
],
1828118281
)
18282-
@requires_api_core_gte_1_26_0
1828318282
def test_analytics_admin_service_transport_create_channel(
1828418283
transport_class, grpc_helpers
1828518284
):
@@ -18315,84 +18314,6 @@ def test_analytics_admin_service_transport_create_channel(
1831518314
)
1831618315

1831718316

18318-
@pytest.mark.parametrize(
18319-
"transport_class,grpc_helpers",
18320-
[
18321-
(transports.AnalyticsAdminServiceGrpcTransport, grpc_helpers),
18322-
(transports.AnalyticsAdminServiceGrpcAsyncIOTransport, grpc_helpers_async),
18323-
],
18324-
)
18325-
@requires_api_core_lt_1_26_0
18326-
def test_analytics_admin_service_transport_create_channel_old_api_core(
18327-
transport_class, grpc_helpers
18328-
):
18329-
# If credentials and host are not provided, the transport class should use
18330-
# ADC credentials.
18331-
with mock.patch.object(
18332-
google.auth, "default", autospec=True
18333-
) as adc, mock.patch.object(
18334-
grpc_helpers, "create_channel", autospec=True
18335-
) as create_channel:
18336-
creds = ga_credentials.AnonymousCredentials()
18337-
adc.return_value = (creds, None)
18338-
transport_class(quota_project_id="octopus")
18339-
18340-
create_channel.assert_called_with(
18341-
"analyticsadmin.googleapis.com:443",
18342-
credentials=creds,
18343-
credentials_file=None,
18344-
quota_project_id="octopus",
18345-
scopes=(
18346-
"https://www.googleapis.com/auth/analytics.edit",
18347-
"https://www.googleapis.com/auth/analytics.manage.users",
18348-
"https://www.googleapis.com/auth/analytics.manage.users.readonly",
18349-
"https://www.googleapis.com/auth/analytics.readonly",
18350-
),
18351-
ssl_credentials=None,
18352-
options=[
18353-
("grpc.max_send_message_length", -1),
18354-
("grpc.max_receive_message_length", -1),
18355-
],
18356-
)
18357-
18358-
18359-
@pytest.mark.parametrize(
18360-
"transport_class,grpc_helpers",
18361-
[
18362-
(transports.AnalyticsAdminServiceGrpcTransport, grpc_helpers),
18363-
(transports.AnalyticsAdminServiceGrpcAsyncIOTransport, grpc_helpers_async),
18364-
],
18365-
)
18366-
@requires_api_core_lt_1_26_0
18367-
def test_analytics_admin_service_transport_create_channel_user_scopes(
18368-
transport_class, grpc_helpers
18369-
):
18370-
# If credentials and host are not provided, the transport class should use
18371-
# ADC credentials.
18372-
with mock.patch.object(
18373-
google.auth, "default", autospec=True
18374-
) as adc, mock.patch.object(
18375-
grpc_helpers, "create_channel", autospec=True
18376-
) as create_channel:
18377-
creds = ga_credentials.AnonymousCredentials()
18378-
adc.return_value = (creds, None)
18379-
18380-
transport_class(quota_project_id="octopus", scopes=["1", "2"])
18381-
18382-
create_channel.assert_called_with(
18383-
"analyticsadmin.googleapis.com:443",
18384-
credentials=creds,
18385-
credentials_file=None,
18386-
quota_project_id="octopus",
18387-
scopes=["1", "2"],
18388-
ssl_credentials=None,
18389-
options=[
18390-
("grpc.max_send_message_length", -1),
18391-
("grpc.max_receive_message_length", -1),
18392-
],
18393-
)
18394-
18395-
1839618317
@pytest.mark.parametrize(
1839718318
"transport_class",
1839818319
[

0 commit comments

Comments
 (0)