Skip to content

Commit

Permalink
ServiceBusAdministrationClient now uses _parse_conn_str from base_han… (
Browse files Browse the repository at this point in the history
Azure#14228)

* ServiceBusAdministrationClient now uses _parse_conn_str from base_handlers

* Remove parse_conn_str() from common.utils and replace parse_conn_str() with _parse_conn_str() from base_handler

* add underscore to base_handler import statement

new statement is:
`from ..._base_handler import _parse_conn_str`

Co-authored-by: Adam Ling (MSFT) <adam_ling@outlook.com>

* Fixing pylint: removed unused module time from _common/utils.py

Co-authored-by: Bradley D'Amato <bradley.damato@Bradleys-MacBook-Pro.local>
Co-authored-by: Adam Ling (MSFT) <adam_ling@outlook.com>
  • Loading branch information
3 people authored Oct 9, 2020
1 parent 9d7c6ac commit dad3cbe
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 61 deletions.
56 changes: 0 additions & 56 deletions sdk/servicebus/azure-servicebus/azure/servicebus/_common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import logging
import functools
import platform
import time
from typing import Optional, Dict, Tuple
from msrest.serialization import UTC

Expand Down Expand Up @@ -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=<timestamp>" 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://<FQDN>/;SharedAccessKeyName=<KeyName>;SharedAccessKey=<KeyValue>"
"\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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -139,7 +139,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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,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
Expand Down Expand Up @@ -133,7 +132,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:
Expand Down

0 comments on commit dad3cbe

Please sign in to comment.