diff --git a/sdk/servicebus/azure-servicebus/azure/servicebus/_common/utils.py b/sdk/servicebus/azure-servicebus/azure/servicebus/_common/utils.py index f52f1d6d1237..25e5c82cffb1 100644 --- a/sdk/servicebus/azure-servicebus/azure/servicebus/_common/utils.py +++ b/sdk/servicebus/azure-servicebus/azure/servicebus/_common/utils.py @@ -9,7 +9,6 @@ import logging import functools import platform -import time from typing import Optional, Dict, Tuple from msrest.serialization import UTC @@ -41,61 +40,6 @@ def utc_now(): return datetime.datetime.now(UTC()) -# This parse_conn_str is used for mgmt, the other in base_handler for handlers. Should be unified. -def parse_conn_str(conn_str): - # type: (str) -> Tuple[str, Optional[str], Optional[str], str, Optional[str], Optional[int]] - endpoint = "" - shared_access_key_name = None # type: Optional[str] - shared_access_key = None # type: Optional[str] - entity_path = "" - shared_access_signature = None # type: Optional[str] - shared_access_signature_expiry = None # type: Optional[int] - for element in conn_str.split(";"): - key, _, value = element.partition("=") - if key.lower() == "endpoint": - endpoint = value.rstrip("/") - elif key.lower() == "sharedaccesskeyname": - shared_access_key_name = value - elif key.lower() == "sharedaccesskey": - shared_access_key = value - elif key.lower() == "entitypath": - entity_path = value - elif key.lower() == "sharedaccesssignature": - shared_access_signature = value - try: - # Expiry can be stored in the "se=" clause of the token. ('&'-separated key-value pairs) - # type: ignore - shared_access_signature_expiry = int( - shared_access_signature.split("se=")[1].split("&")[0] - ) - except ( - IndexError, - TypeError, - ValueError, - ): # Fallback since technically expiry is optional. - # An arbitrary, absurdly large number, since you can't renew. - shared_access_signature_expiry = int(time.time() * 2) - if not ( - all((endpoint, shared_access_key_name, shared_access_key)) - or all((endpoint, shared_access_signature)) - ) or all( - (shared_access_key_name, shared_access_signature) - ): # this latter clause since we don't accept both - raise ValueError( - "Invalid connection string. Should be in the format: " - "Endpoint=sb:///;SharedAccessKeyName=;SharedAccessKey=" - "\nWith alternate option of providing SharedAccessSignature instead of SharedAccessKeyName and Key" - ) - return ( - endpoint, - str(shared_access_key_name) if shared_access_key_name else None, - str(shared_access_key) if shared_access_key else None, - entity_path, - str(shared_access_signature) if shared_access_signature else None, - shared_access_signature_expiry, - ) - - def build_uri(address, entity): parsed = urlparse(address) if parsed.path: diff --git a/sdk/servicebus/azure-servicebus/azure/servicebus/aio/management/_management_client_async.py b/sdk/servicebus/azure-servicebus/azure/servicebus/aio/management/_management_client_async.py index f70e7a675394..e26bfc25be23 100644 --- a/sdk/servicebus/azure-servicebus/azure/servicebus/aio/management/_management_client_async.py +++ b/sdk/servicebus/azure-servicebus/azure/servicebus/aio/management/_management_client_async.py @@ -22,7 +22,7 @@ TopicDescriptionFeed, CreateSubscriptionBody, CreateSubscriptionBodyContent, CreateRuleBody, \ CreateRuleBodyContent, CreateQueueBody, CreateQueueBodyContent -from ..._common.utils import parse_conn_str +from ..._base_handler import _parse_conn_str from ..._common.constants import JWT_TOKEN_SCOPE from ...aio._base_handler_async import ServiceBusSharedKeyCredential, ServiceBusSASTokenCredential from ...management._generated.aio._configuration_async import ServiceBusManagementClientConfiguration @@ -135,7 +135,7 @@ def from_connection_string(cls, conn_str: str, **kwargs: Any) -> "ServiceBusAdmi :param str conn_str: The connection string of the Service Bus Namespace. :rtype: ~azure.servicebus.management.aio.ServiceBusAdministrationClient """ - endpoint, shared_access_key_name, shared_access_key, _, token, token_expiry = parse_conn_str(conn_str) + endpoint, shared_access_key_name, shared_access_key, _, token, token_expiry = _parse_conn_str(conn_str) if token and token_expiry: credential = ServiceBusSASTokenCredential(token, token_expiry) elif shared_access_key_name and shared_access_key: diff --git a/sdk/servicebus/azure-servicebus/azure/servicebus/management/_management_client.py b/sdk/servicebus/azure-servicebus/azure/servicebus/management/_management_client.py index a2f680e1d2c1..0024c069974f 100644 --- a/sdk/servicebus/azure-servicebus/azure/servicebus/management/_management_client.py +++ b/sdk/servicebus/azure-servicebus/azure/servicebus/management/_management_client.py @@ -26,8 +26,7 @@ from ._xml_workaround_policy import ServiceBusXMLWorkaroundPolicy from .._common.constants import JWT_TOKEN_SCOPE -from .._common.utils import parse_conn_str -from .._base_handler import ServiceBusSharedKeyCredential, ServiceBusSASTokenCredential +from .._base_handler import _parse_conn_str, ServiceBusSharedKeyCredential, ServiceBusSASTokenCredential from ._shared_key_policy import ServiceBusSharedKeyCredentialPolicy from ._generated._configuration import ServiceBusManagementClientConfiguration from ._generated._service_bus_management_client import ServiceBusManagementClient as ServiceBusManagementClientImpl @@ -130,7 +129,7 @@ def from_connection_string(cls, conn_str, **kwargs): :param str conn_str: The connection string of the Service Bus Namespace. :rtype: ~azure.servicebus.management.ServiceBusAdministrationClient """ - endpoint, shared_access_key_name, shared_access_key, _, token, token_expiry = parse_conn_str(conn_str) + endpoint, shared_access_key_name, shared_access_key, _, token, token_expiry = _parse_conn_str(conn_str) if token and token_expiry: credential = ServiceBusSASTokenCredential(token, token_expiry) elif shared_access_key_name and shared_access_key: