Skip to content

Commit 5a6324c

Browse files
authored
[sb] update conn str (Azure#38655)
* emulator * nit * fixing * changelog * parse_conn_string tests * mgmt client not supported yet -- don't want to have customers see a diff in behavior if they add support suddenly * changelog * pylint
1 parent 07be039 commit 5a6324c

File tree

11 files changed

+60
-22
lines changed

11 files changed

+60
-22
lines changed

sdk/servicebus/azure-servicebus/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 7.13.1 (Unreleased)
44

55
### Features Added
6+
- Added emulator support, ServiceBusAdministrationClient is currently not supported by the emulator.
67

78
### Breaking Changes
89

sdk/servicebus/azure-servicebus/azure/servicebus/_base_handler.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,16 @@
5050
_LOGGER = logging.getLogger(__name__)
5151

5252

53-
def _parse_conn_str(
53+
def _parse_conn_str( # pylint:disable=too-many-statements
5454
conn_str: str, check_case: Optional[bool] = False
55-
) -> Tuple[str, Optional[str], Optional[str], str, Optional[str], Optional[int]]:
55+
) -> Tuple[str, Optional[str], Optional[str], str, Optional[str], Optional[int], bool]:
5656
endpoint = None
5757
shared_access_key_name = None
5858
shared_access_key = None
5959
entity_path: Optional[str] = None
6060
shared_access_signature: Optional[str] = None
6161
shared_access_signature_expiry: Optional[int] = None
62+
use_emulator: Optional[str] = None
6263

6364
# split connection string into properties
6465
conn_properties = [s.split("=", 1) for s in conn_str.strip().rstrip(";").split(";")]
@@ -72,6 +73,7 @@ def _parse_conn_str(
7273
shared_access_key_name = conn_settings.get("SharedAccessKeyName")
7374
endpoint = conn_settings.get("Endpoint")
7475
entity_path = conn_settings.get("EntityPath")
76+
use_emulator = conn_settings.get("UseDevelopmentEmulator")
7577

7678
# non case sensitive check when parsing connection string for internal use
7779
for key, value in conn_settings.items():
@@ -101,6 +103,8 @@ def _parse_conn_str(
101103
shared_access_key = value
102104
elif key.lower() == "entitypath":
103105
entity_path = value
106+
elif key.lower() == "usedevelopmentemulator":
107+
use_emulator = value
104108

105109
entity = cast(str, entity_path)
106110

@@ -112,6 +116,8 @@ def _parse_conn_str(
112116
raise ValueError("Invalid Endpoint on the Connection String.")
113117
host = cast(str, parsed.netloc.strip())
114118

119+
emulator = use_emulator == "true"
120+
115121
if any([shared_access_key, shared_access_key_name]) and not all([shared_access_key, shared_access_key_name]):
116122
raise ValueError("Connection string must have both SharedAccessKeyName and SharedAccessKey.")
117123
if shared_access_signature and shared_access_key:
@@ -126,6 +132,7 @@ def _parse_conn_str(
126132
entity,
127133
str(shared_access_signature) if shared_access_signature else None,
128134
shared_access_signature_expiry,
135+
emulator,
129136
)
130137

131138

@@ -274,7 +281,7 @@ def __init__(
274281

275282
@classmethod
276283
def _convert_connection_string_to_kwargs(cls, conn_str: str, **kwargs: Any) -> Dict[str, Any]:
277-
host, policy, key, entity_in_conn_str, token, token_expiry = _parse_conn_str(conn_str)
284+
host, policy, key, entity_in_conn_str, token, token_expiry, emulator = _parse_conn_str(conn_str)
278285
queue_name = kwargs.get("queue_name")
279286
topic_name = kwargs.get("topic_name")
280287
if not (queue_name or topic_name or entity_in_conn_str):
@@ -295,6 +302,9 @@ def _convert_connection_string_to_kwargs(cls, conn_str: str, **kwargs: Any) -> D
295302

296303
kwargs["fully_qualified_namespace"] = host
297304
kwargs["entity_name"] = entity_in_conn_str or entity_in_kwargs
305+
# Check if emulator is in use, unset tls if it is
306+
if emulator:
307+
kwargs["use_tls"] = False
298308

299309
# Set the type to sync credentials, unless async credentials are passed in.
300310
token_cred_type = kwargs.pop("token_cred_type", ServiceBusSASTokenCredential)

sdk/servicebus/azure-servicebus/azure/servicebus/_common/_configuration.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def __init__(self, **kwargs):
4141
self.auto_reconnect = kwargs.get("auto_reconnect", True)
4242
self.keep_alive = kwargs.get("keep_alive", 30)
4343
self.timeout: float = kwargs.get("timeout", 60)
44+
self.use_tls = kwargs.get("use_tls", True)
4445
default_socket_timeout = 0.2
4546

4647
if self.http_proxy or self.transport_type.value == TransportType.AmqpOverWebsocket.value:

sdk/servicebus/azure-servicebus/azure/servicebus/_common/_connection_string_parser.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ def parse_connection_string(conn_str: str) -> "ServiceBusConnectionStringPropert
8686
:return: A properties model containing the parsed connection string.
8787
:rtype: ~azure.servicebus.ServiceBusConnectionStringProperties
8888
"""
89-
fully_qualified_namespace, policy, key, entity, signature = _parse_conn_str(conn_str, True)[:-1]
89+
fully_qualified_namespace, policy, key, entity, signature, emulator = _parse_conn_str(conn_str, True)[ # pylint: disable=unused-variable
90+
:-1
91+
]
9092
endpoint = "sb://" + fully_qualified_namespace + "/"
9193
return ServiceBusConnectionStringProperties(
9294
fully_qualified_namespace=fully_qualified_namespace,

sdk/servicebus/azure-servicebus/azure/servicebus/_servicebus_client.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ def from_connection_string(
259259
:caption: Create a new instance of the ServiceBusClient from connection string.
260260
261261
"""
262-
host, policy, key, entity_in_conn_str, token, token_expiry = _parse_conn_str(conn_str)
262+
host, policy, key, entity_in_conn_str, token, token_expiry, emulator = _parse_conn_str(conn_str)
263+
kwargs["use_tls"] = not emulator
263264
if token and token_expiry:
264265
credential = ServiceBusSASTokenCredential(token, token_expiry)
265266
elif policy and key:
@@ -324,6 +325,7 @@ def get_queue_sender(self, queue_name: str, **kwargs: Any) -> ServiceBusSender:
324325
connection_verify=self._connection_verify,
325326
ssl_context=self._ssl_context,
326327
amqp_transport=self._amqp_transport,
328+
use_tls=self._config.use_tls,
327329
**kwargs,
328330
)
329331
self._handlers.add(handler)
@@ -445,6 +447,7 @@ def get_queue_receiver(
445447
connection_verify=self._connection_verify,
446448
ssl_context=self._ssl_context,
447449
amqp_transport=self._amqp_transport,
450+
use_tls=self._config.use_tls,
448451
**kwargs,
449452
)
450453
self._handlers.add(handler)
@@ -507,6 +510,7 @@ def get_topic_sender(
507510
amqp_transport=self._amqp_transport,
508511
client_identifier=client_identifier,
509512
socket_timeout=socket_timeout,
513+
use_tls=self._config.use_tls,
510514
**kwargs,
511515
)
512516
self._handlers.add(handler)
@@ -630,6 +634,7 @@ def get_subscription_receiver(
630634
connection_verify=self._connection_verify,
631635
ssl_context=self._ssl_context,
632636
amqp_transport=self._amqp_transport,
637+
use_tls=self._config.use_tls,
633638
**kwargs,
634639
)
635640
except ValueError:
@@ -659,6 +664,7 @@ def get_subscription_receiver(
659664
connection_verify=self._connection_verify,
660665
ssl_context=self._ssl_context,
661666
amqp_transport=self._amqp_transport,
667+
use_tls=self._config.use_tls,
662668
**kwargs,
663669
)
664670
self._handlers.add(handler)

sdk/servicebus/azure-servicebus/azure/servicebus/_transport/_pyamqp_transport.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,7 @@ def create_send_client(config: "Configuration", **kwargs: Any) -> "SendClient":
467467
transport_type=config.transport_type,
468468
http_proxy=config.http_proxy,
469469
socket_timeout=config.socket_timeout,
470+
use_tls=config.use_tls,
470471
**kwargs,
471472
)
472473

@@ -596,6 +597,7 @@ def create_receive_client(receiver: "ServiceBusReceiver", **kwargs: "Any") -> "R
596597
),
597598
on_attach=functools.partial(PyamqpTransport.on_attach, receiver),
598599
socket_timeout=config.socket_timeout,
600+
use_tls=config.use_tls,
599601
**kwargs,
600602
)
601603

sdk/servicebus/azure-servicebus/azure/servicebus/aio/_servicebus_client_async.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ def from_connection_string(
226226
:caption: Create a new instance of the ServiceBusClient from connection string.
227227
228228
"""
229-
host, policy, key, entity_in_conn_str, token, token_expiry = _parse_conn_str(conn_str)
229+
host, policy, key, entity_in_conn_str, token, token_expiry, emulator = _parse_conn_str(conn_str)
230+
kwargs["use_tls"] = not emulator
230231
if token and token_expiry:
231232
credential = ServiceBusSASTokenCredential(token, token_expiry)
232233
elif policy and key:
@@ -312,6 +313,7 @@ def get_queue_sender(self, queue_name: str, **kwargs: Any) -> ServiceBusSender:
312313
connection_verify=self._connection_verify,
313314
ssl_context=self._ssl_context,
314315
amqp_transport=self._amqp_transport,
316+
use_tls=self._config.use_tls,
315317
**kwargs,
316318
)
317319
self._handlers.add(handler)
@@ -433,6 +435,7 @@ def get_queue_receiver(
433435
connection_verify=self._connection_verify,
434436
ssl_context=self._ssl_context,
435437
amqp_transport=self._amqp_transport,
438+
use_tls=self._config.use_tls,
436439
**kwargs,
437440
)
438441
self._handlers.add(handler)
@@ -486,6 +489,7 @@ def get_topic_sender(self, topic_name: str, **kwargs: Any) -> ServiceBusSender:
486489
connection_verify=self._connection_verify,
487490
ssl_context=self._ssl_context,
488491
amqp_transport=self._amqp_transport,
492+
use_tls=self._config.use_tls,
489493
**kwargs,
490494
)
491495
self._handlers.add(handler)
@@ -609,6 +613,7 @@ def get_subscription_receiver(
609613
connection_verify=self._connection_verify,
610614
ssl_context=self._ssl_context,
611615
amqp_transport=self._amqp_transport,
616+
use_tls=self._config.use_tls,
612617
**kwargs,
613618
)
614619
except ValueError:
@@ -638,6 +643,7 @@ def get_subscription_receiver(
638643
connection_verify=self._connection_verify,
639644
ssl_context=self._ssl_context,
640645
amqp_transport=self._amqp_transport,
646+
use_tls=self._config.use_tls,
641647
**kwargs,
642648
)
643649
self._handlers.add(handler)

sdk/servicebus/azure-servicebus/azure/servicebus/aio/_transport/_pyamqp_transport_async.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ def create_send_client_async(config: "Configuration", **kwargs: Any) -> "SendCli
116116
transport_type=config.transport_type,
117117
http_proxy=config.http_proxy,
118118
socket_timeout=config.socket_timeout,
119+
use_tls=config.use_tls,
119120
**kwargs,
120121
)
121122

@@ -219,6 +220,7 @@ def create_receive_client_async(
219220
),
220221
on_attach=functools.partial(PyamqpTransportAsync.on_attach_async, receiver),
221222
socket_timeout=config.socket_timeout,
223+
use_tls=config.use_tls,
222224
**kwargs,
223225
)
224226

sdk/servicebus/azure-servicebus/azure/servicebus/aio/management/_management_client_async.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -248,14 +248,10 @@ def from_connection_string(
248248
in reduced feature compatibility.
249249
:paramtype api_version: str or ApiVersion
250250
"""
251-
(
252-
endpoint,
253-
shared_access_key_name,
254-
shared_access_key,
255-
_,
256-
token,
257-
token_expiry,
258-
) = _parse_conn_str(conn_str)
251+
# pylint: disable=unused-variable
252+
(endpoint, shared_access_key_name, shared_access_key, _, token, token_expiry, emulator) = _parse_conn_str(
253+
conn_str
254+
)
259255
if token and token_expiry:
260256
credential = ServiceBusSASTokenCredential(token, token_expiry)
261257
elif shared_access_key_name and shared_access_key:

sdk/servicebus/azure-servicebus/azure/servicebus/management/_management_client.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,10 @@ def from_connection_string(
244244
:returns: Returns a ServiceBusAdministrationClient.
245245
:rtype: ~azure.servicebus.management.ServiceBusAdministrationClient
246246
"""
247-
(
248-
endpoint,
249-
shared_access_key_name,
250-
shared_access_key,
251-
_,
252-
token,
253-
token_expiry,
254-
) = _parse_conn_str(conn_str)
247+
# pylint: disable=unused-variable
248+
(endpoint, shared_access_key_name, shared_access_key, _, token, token_expiry, emulator) = _parse_conn_str(
249+
conn_str
250+
)
255251
if token and token_expiry:
256252
credential = ServiceBusSASTokenCredential(token, token_expiry)
257253
elif shared_access_key_name and shared_access_key:

0 commit comments

Comments
 (0)