Skip to content
This repository has been archived by the owner on Jan 6, 2024. It is now read-only.

fix: disable always_use_jwt_access #119

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions google/cloud/dialogflowcx_v3/services/agents/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,10 @@ async def create_agent(
) -> gcdc_agent.Agent:
r"""Creates an agent in the specified location.

Note: You should always train flows prior to sending them
queries. See the `training
documentation <https://cloud.google.com/dialogflow/cx/docs/concept/training>`__.

Args:
request (:class:`google.cloud.dialogflowcx_v3.types.CreateAgentRequest`):
The request object. The request message for
Expand Down Expand Up @@ -443,6 +447,10 @@ async def update_agent(
) -> gcdc_agent.Agent:
r"""Updates the specified agent.

Note: You should always train flows prior to sending them
queries. See the `training
documentation <https://cloud.google.com/dialogflow/cx/docs/concept/training>`__.

Args:
request (:class:`google.cloud.dialogflowcx_v3.types.UpdateAgentRequest`):
The request object. The request message for
Expand Down Expand Up @@ -659,9 +667,14 @@ async def restore_agent(
metadata: Sequence[Tuple[str, str]] = (),
) -> operation_async.AsyncOperation:
r"""Restores the specified agent from a binary file.

Replaces the current agent with a new one. Note that all
existing resources in agent (e.g. intents, entity types,
flows) will be removed.
existing resources in agent (e.g. intents, entity types, flows)
will be removed.

Note: You should always train flows prior to sending them
queries. See the `training
documentation <https://cloud.google.com/dialogflow/cx/docs/concept/training>`__.

Args:
request (:class:`google.cloud.dialogflowcx_v3.types.RestoreAgentRequest`):
Expand Down
17 changes: 15 additions & 2 deletions google/cloud/dialogflowcx_v3/services/agents/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,10 @@ def create_agent(
) -> gcdc_agent.Agent:
r"""Creates an agent in the specified location.

Note: You should always train flows prior to sending them
queries. See the `training
documentation <https://cloud.google.com/dialogflow/cx/docs/concept/training>`__.

Args:
request (google.cloud.dialogflowcx_v3.types.CreateAgentRequest):
The request object. The request message for
Expand Down Expand Up @@ -704,6 +708,10 @@ def update_agent(
) -> gcdc_agent.Agent:
r"""Updates the specified agent.

Note: You should always train flows prior to sending them
queries. See the `training
documentation <https://cloud.google.com/dialogflow/cx/docs/concept/training>`__.

Args:
request (google.cloud.dialogflowcx_v3.types.UpdateAgentRequest):
The request object. The request message for
Expand Down Expand Up @@ -921,9 +929,14 @@ def restore_agent(
metadata: Sequence[Tuple[str, str]] = (),
) -> operation.Operation:
r"""Restores the specified agent from a binary file.

Replaces the current agent with a new one. Note that all
existing resources in agent (e.g. intents, entity types,
flows) will be removed.
existing resources in agent (e.g. intents, entity types, flows)
will be removed.

Note: You should always train flows prior to sending them
queries. See the `training
documentation <https://cloud.google.com/dialogflow/cx/docs/concept/training>`__.

Args:
request (google.cloud.dialogflowcx_v3.types.RestoreAgentRequest):
Expand Down
42 changes: 15 additions & 27 deletions google/cloud/dialogflowcx_v3/services/agents/transports/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from google.api_core import retry as retries # type: ignore
from google.api_core import operations_v1 # type: ignore
from google.auth import credentials as ga_credentials # type: ignore
from google.oauth2 import service_account # type: ignore

from google.cloud.dialogflowcx_v3.types import agent
from google.cloud.dialogflowcx_v3.types import agent as gcdc_agent
Expand All @@ -49,8 +50,6 @@
except pkg_resources.DistributionNotFound: # pragma: NO COVER
_GOOGLE_AUTH_VERSION = None

_API_CORE_VERSION = google.api_core.__version__


class AgentsTransport(abc.ABC):
"""Abstract transport class for Agents."""
Expand All @@ -71,6 +70,7 @@ def __init__(
scopes: Optional[Sequence[str]] = None,
quota_project_id: Optional[str] = None,
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
always_use_jwt_access: Optional[bool] = False,
**kwargs,
) -> None:
"""Instantiate the transport.
Expand All @@ -94,6 +94,8 @@ def __init__(
API requests. If ``None``, then default info will be used.
Generally, you only need to set this if you're developing
your own client library.
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
be used for service account credentials.
"""
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
if ":" not in host:
Expand All @@ -103,7 +105,7 @@ def __init__(
scopes_kwargs = self._get_scopes_kwargs(self._host, scopes)

# Save the scopes.
self._scopes = scopes or self.AUTH_SCOPES
self._scopes = scopes

# If no credentials are provided, then determine the appropriate
# defaults.
Expand All @@ -122,13 +124,20 @@ def __init__(
**scopes_kwargs, quota_project_id=quota_project_id
)

# If the credentials is service account credentials, then always try to use self signed JWT.
if (
always_use_jwt_access
and isinstance(credentials, service_account.Credentials)
and hasattr(service_account.Credentials, "with_always_use_jwt_access")
):
credentials = credentials.with_always_use_jwt_access(True)

# Save the credentials.
self._credentials = credentials

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

# TODO: Remove this function once google-auth >= 1.25.0 is required
@classmethod
Expand All @@ -149,27 +158,6 @@ def _get_scopes_kwargs(

return scopes_kwargs

# TODO: Remove this function once google-api-core >= 1.26.0 is required
@classmethod
def _get_self_signed_jwt_kwargs(
cls, host: str, scopes: Optional[Sequence[str]]
) -> Dict[str, Union[Optional[Sequence[str]], str]]:
"""Returns kwargs to pass to grpc_helpers.create_channel depending on the google-api-core version"""

self_signed_jwt_kwargs: Dict[str, Union[Optional[Sequence[str]], str]] = {}

if _API_CORE_VERSION and (
packaging.version.parse(_API_CORE_VERSION)
>= packaging.version.parse("1.26.0")
):
self_signed_jwt_kwargs["default_scopes"] = cls.AUTH_SCOPES
self_signed_jwt_kwargs["scopes"] = scopes
self_signed_jwt_kwargs["default_host"] = cls.DEFAULT_HOST
else:
self_signed_jwt_kwargs["scopes"] = scopes or cls.AUTH_SCOPES

return self_signed_jwt_kwargs

def _prep_wrapped_messages(self, client_info):
# Precompute the wrapped methods.
self._wrapped_methods = {
Expand Down
27 changes: 22 additions & 5 deletions google/cloud/dialogflowcx_v3/services/agents/transports/grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def __init__(
client_cert_source_for_mtls: Callable[[], Tuple[bytes, bytes]] = None,
quota_project_id: Optional[str] = None,
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
always_use_jwt_access: Optional[bool] = False,
) -> None:
"""Instantiate the transport.

Expand Down Expand Up @@ -101,6 +102,8 @@ def __init__(
API requests. If ``None``, then default info will be used.
Generally, you only need to set this if you're developing
your own client library.
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
be used for service account credentials.

Raises:
google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport
Expand Down Expand Up @@ -154,6 +157,7 @@ def __init__(
scopes=scopes,
quota_project_id=quota_project_id,
client_info=client_info,
always_use_jwt_access=always_use_jwt_access,
)

if not self._grpc_channel:
Expand Down Expand Up @@ -209,14 +213,14 @@ def create_channel(
and ``credentials_file`` are passed.
"""

self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)

return grpc_helpers.create_channel(
host,
credentials=credentials,
credentials_file=credentials_file,
quota_project_id=quota_project_id,
**self_signed_jwt_kwargs,
default_scopes=cls.AUTH_SCOPES,
scopes=scopes,
default_host=cls.DEFAULT_HOST,
**kwargs,
)

Expand Down Expand Up @@ -299,6 +303,10 @@ def create_agent(

Creates an agent in the specified location.

Note: You should always train flows prior to sending them
queries. See the `training
documentation <https://cloud.google.com/dialogflow/cx/docs/concept/training>`__.

Returns:
Callable[[~.CreateAgentRequest],
~.Agent]:
Expand All @@ -325,6 +333,10 @@ def update_agent(

Updates the specified agent.

Note: You should always train flows prior to sending them
queries. See the `training
documentation <https://cloud.google.com/dialogflow/cx/docs/concept/training>`__.

Returns:
Callable[[~.UpdateAgentRequest],
~.Agent]:
Expand Down Expand Up @@ -400,9 +412,14 @@ def restore_agent(
r"""Return a callable for the restore agent method over gRPC.

Restores the specified agent from a binary file.

Replaces the current agent with a new one. Note that all
existing resources in agent (e.g. intents, entity types,
flows) will be removed.
existing resources in agent (e.g. intents, entity types, flows)
will be removed.

Note: You should always train flows prior to sending them
queries. See the `training
documentation <https://cloud.google.com/dialogflow/cx/docs/concept/training>`__.

Returns:
Callable[[~.RestoreAgentRequest],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ def create_channel(
aio.Channel: A gRPC AsyncIO channel object.
"""

self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)

return grpc_helpers_async.create_channel(
host,
credentials=credentials,
credentials_file=credentials_file,
quota_project_id=quota_project_id,
**self_signed_jwt_kwargs,
default_scopes=cls.AUTH_SCOPES,
scopes=scopes,
default_host=cls.DEFAULT_HOST,
**kwargs,
)

Expand All @@ -107,6 +107,7 @@ def __init__(
client_cert_source_for_mtls: Callable[[], Tuple[bytes, bytes]] = None,
quota_project_id=None,
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
always_use_jwt_access: Optional[bool] = False,
) -> None:
"""Instantiate the transport.

Expand Down Expand Up @@ -148,6 +149,8 @@ def __init__(
API requests. If ``None``, then default info will be used.
Generally, you only need to set this if you're developing
your own client library.
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
be used for service account credentials.

Raises:
google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport
Expand Down Expand Up @@ -200,6 +203,7 @@ def __init__(
scopes=scopes,
quota_project_id=quota_project_id,
client_info=client_info,
always_use_jwt_access=always_use_jwt_access,
)

if not self._grpc_channel:
Expand Down Expand Up @@ -304,6 +308,10 @@ def create_agent(

Creates an agent in the specified location.

Note: You should always train flows prior to sending them
queries. See the `training
documentation <https://cloud.google.com/dialogflow/cx/docs/concept/training>`__.

Returns:
Callable[[~.CreateAgentRequest],
Awaitable[~.Agent]]:
Expand All @@ -330,6 +338,10 @@ def update_agent(

Updates the specified agent.

Note: You should always train flows prior to sending them
queries. See the `training
documentation <https://cloud.google.com/dialogflow/cx/docs/concept/training>`__.

Returns:
Callable[[~.UpdateAgentRequest],
Awaitable[~.Agent]]:
Expand Down Expand Up @@ -407,9 +419,14 @@ def restore_agent(
r"""Return a callable for the restore agent method over gRPC.

Restores the specified agent from a binary file.

Replaces the current agent with a new one. Note that all
existing resources in agent (e.g. intents, entity types,
flows) will be removed.
existing resources in agent (e.g. intents, entity types, flows)
will be removed.

Note: You should always train flows prior to sending them
queries. See the `training
documentation <https://cloud.google.com/dialogflow/cx/docs/concept/training>`__.

Returns:
Callable[[~.RestoreAgentRequest],
Expand Down
12 changes: 12 additions & 0 deletions google/cloud/dialogflowcx_v3/services/entity_types/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,10 @@ async def create_entity_type(
) -> gcdc_entity_type.EntityType:
r"""Creates an entity type in the specified agent.

Note: You should always train a flow prior to sending it
queries. See the `training
documentation <https://cloud.google.com/dialogflow/cx/docs/concept/training>`__.

Args:
request (:class:`google.cloud.dialogflowcx_v3.types.CreateEntityTypeRequest`):
The request object. The request message for
Expand Down Expand Up @@ -469,6 +473,10 @@ async def update_entity_type(
) -> gcdc_entity_type.EntityType:
r"""Updates the specified entity type.

Note: You should always train a flow prior to sending it
queries. See the `training
documentation <https://cloud.google.com/dialogflow/cx/docs/concept/training>`__.

Args:
request (:class:`google.cloud.dialogflowcx_v3.types.UpdateEntityTypeRequest`):
The request object. The request message for
Expand Down Expand Up @@ -581,6 +589,10 @@ async def delete_entity_type(
) -> None:
r"""Deletes the specified entity type.

Note: You should always train a flow prior to sending it
queries. See the `training
documentation <https://cloud.google.com/dialogflow/cx/docs/concept/training>`__.

Args:
request (:class:`google.cloud.dialogflowcx_v3.types.DeleteEntityTypeRequest`):
The request object. The request message for
Expand Down
Loading