Skip to content

Commit d5a3f66

Browse files
fix: disable always_use_jwt_access (#97)
Committer: @busunkim96 PiperOrigin-RevId: 382142900 Source-Link: googleapis/googleapis@513440f Source-Link: googleapis/googleapis-gen@7b1e2c3 feat: add always_use_jwt_access
1 parent 3753860 commit d5a3f66

File tree

11 files changed

+132
-277
lines changed

11 files changed

+132
-277
lines changed

packages/google-analytics-data/.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/data/__init__.py

packages/google-analytics-data/google/analytics/data_v1alpha/services/alpha_analytics_data/transports/base.py

Lines changed: 15 additions & 27 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.data_v1alpha.types import analytics_data_api
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 AlphaAnalyticsDataTransport(abc.ABC):
5049
"""Abstract transport class for AlphaAnalyticsData."""
@@ -65,6 +64,7 @@ def __init__(
6564
scopes: Optional[Sequence[str]] = None,
6665
quota_project_id: Optional[str] = None,
6766
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
67+
always_use_jwt_access: Optional[bool] = False,
6868
**kwargs,
6969
) -> None:
7070
"""Instantiate the transport.
@@ -88,6 +88,8 @@ def __init__(
8888
API requests. If ``None``, then default info will be used.
8989
Generally, you only need to set this if you're developing
9090
your own client library.
91+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
92+
be used for service account credentials.
9193
"""
9294
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
9395
if ":" not in host:
@@ -97,7 +99,7 @@ def __init__(
9799
scopes_kwargs = self._get_scopes_kwargs(self._host, scopes)
98100

99101
# Save the scopes.
100-
self._scopes = scopes or self.AUTH_SCOPES
102+
self._scopes = scopes
101103

102104
# If no credentials are provided, then determine the appropriate
103105
# defaults.
@@ -116,13 +118,20 @@ def __init__(
116118
**scopes_kwargs, quota_project_id=quota_project_id
117119
)
118120

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

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

127136
# TODO: Remove this function once google-auth >= 1.25.0 is required
128137
@classmethod
@@ -143,27 +152,6 @@ def _get_scopes_kwargs(
143152

144153
return scopes_kwargs
145154

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

packages/google-analytics-data/google/analytics/data_v1alpha/services/alpha_analytics_data/transports/grpc.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def __init__(
5757
client_cert_source_for_mtls: Callable[[], Tuple[bytes, bytes]] = None,
5858
quota_project_id: Optional[str] = None,
5959
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
60+
always_use_jwt_access: Optional[bool] = False,
6061
) -> None:
6162
"""Instantiate the transport.
6263
@@ -97,6 +98,8 @@ def __init__(
9798
API requests. If ``None``, then default info will be used.
9899
Generally, you only need to set this if you're developing
99100
your own client library.
101+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
102+
be used for service account credentials.
100103
101104
Raises:
102105
google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport
@@ -149,6 +152,7 @@ def __init__(
149152
scopes=scopes,
150153
quota_project_id=quota_project_id,
151154
client_info=client_info,
155+
always_use_jwt_access=always_use_jwt_access,
152156
)
153157

154158
if not self._grpc_channel:
@@ -204,14 +208,14 @@ def create_channel(
204208
and ``credentials_file`` are passed.
205209
"""
206210

207-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
208-
209211
return grpc_helpers.create_channel(
210212
host,
211213
credentials=credentials,
212214
credentials_file=credentials_file,
213215
quota_project_id=quota_project_id,
214-
**self_signed_jwt_kwargs,
216+
default_scopes=cls.AUTH_SCOPES,
217+
scopes=scopes,
218+
default_host=cls.DEFAULT_HOST,
215219
**kwargs,
216220
)
217221

packages/google-analytics-data/google/analytics/data_v1alpha/services/alpha_analytics_data/transports/grpc_asyncio.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ def create_channel(
7878
aio.Channel: A gRPC AsyncIO channel object.
7979
"""
8080

81-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
82-
8381
return grpc_helpers_async.create_channel(
8482
host,
8583
credentials=credentials,
8684
credentials_file=credentials_file,
8785
quota_project_id=quota_project_id,
88-
**self_signed_jwt_kwargs,
86+
default_scopes=cls.AUTH_SCOPES,
87+
scopes=scopes,
88+
default_host=cls.DEFAULT_HOST,
8989
**kwargs,
9090
)
9191

@@ -103,6 +103,7 @@ def __init__(
103103
client_cert_source_for_mtls: Callable[[], Tuple[bytes, bytes]] = None,
104104
quota_project_id=None,
105105
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
106+
always_use_jwt_access: Optional[bool] = False,
106107
) -> None:
107108
"""Instantiate the transport.
108109
@@ -144,6 +145,8 @@ def __init__(
144145
API requests. If ``None``, then default info will be used.
145146
Generally, you only need to set this if you're developing
146147
your own client library.
148+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
149+
be used for service account credentials.
147150
148151
Raises:
149152
google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport
@@ -195,6 +198,7 @@ def __init__(
195198
scopes=scopes,
196199
quota_project_id=quota_project_id,
197200
client_info=client_info,
201+
always_use_jwt_access=always_use_jwt_access,
198202
)
199203

200204
if not self._grpc_channel:

packages/google-analytics-data/google/analytics/data_v1beta/services/beta_analytics_data/transports/base.py

Lines changed: 15 additions & 27 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.data_v1beta.types import analytics_data_api
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 BetaAnalyticsDataTransport(abc.ABC):
5049
"""Abstract transport class for BetaAnalyticsData."""
@@ -65,6 +64,7 @@ def __init__(
6564
scopes: Optional[Sequence[str]] = None,
6665
quota_project_id: Optional[str] = None,
6766
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
67+
always_use_jwt_access: Optional[bool] = False,
6868
**kwargs,
6969
) -> None:
7070
"""Instantiate the transport.
@@ -88,6 +88,8 @@ def __init__(
8888
API requests. If ``None``, then default info will be used.
8989
Generally, you only need to set this if you're developing
9090
your own client library.
91+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
92+
be used for service account credentials.
9193
"""
9294
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
9395
if ":" not in host:
@@ -97,7 +99,7 @@ def __init__(
9799
scopes_kwargs = self._get_scopes_kwargs(self._host, scopes)
98100

99101
# Save the scopes.
100-
self._scopes = scopes or self.AUTH_SCOPES
102+
self._scopes = scopes
101103

102104
# If no credentials are provided, then determine the appropriate
103105
# defaults.
@@ -116,13 +118,20 @@ def __init__(
116118
**scopes_kwargs, quota_project_id=quota_project_id
117119
)
118120

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

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

127136
# TODO: Remove this function once google-auth >= 1.25.0 is required
128137
@classmethod
@@ -143,27 +152,6 @@ def _get_scopes_kwargs(
143152

144153
return scopes_kwargs
145154

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

packages/google-analytics-data/google/analytics/data_v1beta/services/beta_analytics_data/transports/grpc.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def __init__(
5757
client_cert_source_for_mtls: Callable[[], Tuple[bytes, bytes]] = None,
5858
quota_project_id: Optional[str] = None,
5959
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
60+
always_use_jwt_access: Optional[bool] = False,
6061
) -> None:
6162
"""Instantiate the transport.
6263
@@ -97,6 +98,8 @@ def __init__(
9798
API requests. If ``None``, then default info will be used.
9899
Generally, you only need to set this if you're developing
99100
your own client library.
101+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
102+
be used for service account credentials.
100103
101104
Raises:
102105
google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport
@@ -149,6 +152,7 @@ def __init__(
149152
scopes=scopes,
150153
quota_project_id=quota_project_id,
151154
client_info=client_info,
155+
always_use_jwt_access=always_use_jwt_access,
152156
)
153157

154158
if not self._grpc_channel:
@@ -204,14 +208,14 @@ def create_channel(
204208
and ``credentials_file`` are passed.
205209
"""
206210

207-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
208-
209211
return grpc_helpers.create_channel(
210212
host,
211213
credentials=credentials,
212214
credentials_file=credentials_file,
213215
quota_project_id=quota_project_id,
214-
**self_signed_jwt_kwargs,
216+
default_scopes=cls.AUTH_SCOPES,
217+
scopes=scopes,
218+
default_host=cls.DEFAULT_HOST,
215219
**kwargs,
216220
)
217221

packages/google-analytics-data/google/analytics/data_v1beta/services/beta_analytics_data/transports/grpc_asyncio.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ def create_channel(
7878
aio.Channel: A gRPC AsyncIO channel object.
7979
"""
8080

81-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
82-
8381
return grpc_helpers_async.create_channel(
8482
host,
8583
credentials=credentials,
8684
credentials_file=credentials_file,
8785
quota_project_id=quota_project_id,
88-
**self_signed_jwt_kwargs,
86+
default_scopes=cls.AUTH_SCOPES,
87+
scopes=scopes,
88+
default_host=cls.DEFAULT_HOST,
8989
**kwargs,
9090
)
9191

@@ -103,6 +103,7 @@ def __init__(
103103
client_cert_source_for_mtls: Callable[[], Tuple[bytes, bytes]] = None,
104104
quota_project_id=None,
105105
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
106+
always_use_jwt_access: Optional[bool] = False,
106107
) -> None:
107108
"""Instantiate the transport.
108109
@@ -144,6 +145,8 @@ def __init__(
144145
API requests. If ``None``, then default info will be used.
145146
Generally, you only need to set this if you're developing
146147
your own client library.
148+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
149+
be used for service account credentials.
147150
148151
Raises:
149152
google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport
@@ -195,6 +198,7 @@ def __init__(
195198
scopes=scopes,
196199
quota_project_id=quota_project_id,
197200
client_info=client_info,
201+
always_use_jwt_access=always_use_jwt_access,
198202
)
199203

200204
if not self._grpc_channel:

packages/google-analytics-data/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
"libcst >= 0.2.5",
5050
"proto-plus >= 1.4.0",
5151
"packaging >= 14.3",

packages/google-analytics-data/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
libcst==0.2.5
1010
proto-plus==1.4.0
1111
packaging==14.3

0 commit comments

Comments
 (0)