def test_client_azure_named_key_credential( self, servicebus_queue, servicebus_namespace, servicebus_namespace_key_name, servicebus_namespace_primary_key, servicebus_namespace_connection_string, **kwargs): hostname = "{}.servicebus.windows.net".format( servicebus_namespace.name) credential = AzureNamedKeyCredential(servicebus_namespace_key_name, servicebus_namespace_primary_key) client = ServiceBusClient(hostname, credential) with client: with client.get_queue_sender(servicebus_queue.name) as sender: sender.send_messages(ServiceBusMessage("foo")) credential.update("foo", "bar") with pytest.raises(Exception): with client: with client.get_queue_sender(servicebus_queue.name) as sender: sender.send_messages(ServiceBusMessage("foo")) # update back to the right key again credential.update(servicebus_namespace_key_name, servicebus_namespace_primary_key) with client: with client.get_queue_sender(servicebus_queue.name) as sender: sender.send_messages(ServiceBusMessage("foo"))
def test_client_credential(self, servicebus_queue, servicebus_namespace, servicebus_namespace_key_name, servicebus_namespace_primary_key, servicebus_namespace_connection_string, **kwargs): # This should "just work" to validate known-good. credential = ServiceBusSharedKeyCredential( servicebus_namespace_key_name, servicebus_namespace_primary_key) hostname = "{}.servicebus.windows.net".format( servicebus_namespace.name) client = ServiceBusClient(hostname, credential) with client: assert len(client._handlers) == 0 with client.get_queue_sender(servicebus_queue.name) as sender: sender.send_messages(ServiceBusMessage("foo")) hostname = "sb://{}.servicebus.windows.net".format( servicebus_namespace.name) client = ServiceBusClient(hostname, credential) with client: assert len(client._handlers) == 0 with client.get_queue_sender(servicebus_queue.name) as sender: sender.send_messages(ServiceBusMessage("foo")) hostname = "https://{}.servicebus.windows.net \ ".format(servicebus_namespace.name) client = ServiceBusClient(hostname, credential) with client: assert len(client._handlers) == 0 with client.get_queue_sender(servicebus_queue.name) as sender: sender.send_messages(ServiceBusMessage("foo"))
def test_sb_client_bad_credentials(self, servicebus_namespace, servicebus_queue, **kwargs): client = ServiceBusClient( fully_qualified_namespace=servicebus_namespace.name + '.servicebus.windows.net', credential=ServiceBusSharedKeyCredential('invalid', 'invalid'), logging_enable=False) with client: with pytest.raises(ServiceBusError): with client.get_queue_sender(servicebus_queue.name) as sender: sender.send_messages(Message("test"))
def test_client_azure_sas_credential(self, servicebus_queue, servicebus_namespace, servicebus_namespace_key_name, servicebus_namespace_primary_key, servicebus_namespace_connection_string, **kwargs): # This should "just work" to validate known-good. credential = ServiceBusSharedKeyCredential(servicebus_namespace_key_name, servicebus_namespace_primary_key) hostname = "{}.servicebus.windows.net".format(servicebus_namespace.name) auth_uri = "sb://{}/{}".format(hostname, servicebus_queue.name) token = credential.get_token(auth_uri).token.decode() # Finally let's do it with AzureSasCredential credential = AzureSasCredential(token) client = ServiceBusClient(hostname, credential) with client: assert len(client._handlers) == 0 with client.get_queue_sender(servicebus_queue.name) as sender: sender.send_messages(ServiceBusMessage("foo"))
class SenderManager(): ''' Context Manager for the queue sender. ''' def __init__(self, namespace_name, queue_name): self.namespace_name = namespace_name self.queue_name = queue_name self.servicebus_client = None self.sender = None def __enter__(self): utils.load_env_vars() credential = EnvironmentCredential() self.servicebus_client = ServiceBusClient(self.namespace_name, credential) self.sender = self.servicebus_client.get_queue_sender( queue_name=self.queue_name) return self.sender def __exit__(self, exc_type, exc_value, exc_traceback): self.sender.close() self.servicebus_client.close()
Please refer to azure.identity library for detailed information here: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/identity/azure-identity This sample also shows the process of utilizing a different credential object, in this case, DefaultAzureCredential, both to demonstrate the ease of adjusting authentication, and to surface another method for doing so. """ import os from azure.servicebus import ServiceBusClient, Message from azure.identity import EnvironmentCredential FULLY_QUALIFIED_NAMESPACE = os.environ['SERVICE_BUS_NAMESPACE'] QUEUE_NAME = os.environ["SERVICE_BUS_QUEUE_NAME"] credential = EnvironmentCredential() # Note: One has other options to specify the credential. For instance, DefaultAzureCredential. # Default Azure Credentials attempt a chained set of authentication methods, per documentation here: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/identity/azure-identity # For example user to be logged in can be specified by the environment variable AZURE_USERNAME, consumed via the ManagedIdentityCredential # Alternately, one can specify the AZURE_TENANT_ID, AZURE_CLIENT_ID, and AZURE_CLIENT_SECRET to use the EnvironmentCredentialClass. # The docs above specify all mechanisms which the defaultCredential internally support. # credential = DefaultAzureCredential() servicebus_client = ServiceBusClient(FULLY_QUALIFIED_NAMESPACE, credential) with servicebus_client: sender = servicebus_client.get_queue_sender(queue_name=QUEUE_NAME) with sender: sender.send_messages(Message('Single Message')) print("Send message is done.")
def test_backoff_fixed_retry(self): client = ServiceBusClient('fake.host.com', 'fake_eh', retry_mode='fixed') # queue sender sender = client.get_queue_sender('fake_name') backoff = client._config.retry_backoff_factor start_time = time.time() sender._backoff(retried_times=1, last_exception=Exception('fake'), abs_timeout_time=None) sleep_time_fixed = time.time() - start_time # exp = 0.8 * (2 ** 1) = 1.6 # time.sleep() in _backoff will take AT LEAST time 'exp' for retry_mode='exponential' # check that fixed is less than 'exp' assert sleep_time_fixed < backoff * (2**1) # topic sender sender = client.get_topic_sender('fake_name') backoff = client._config.retry_backoff_factor start_time = time.time() sender._backoff(retried_times=1, last_exception=Exception('fake'), abs_timeout_time=None) sleep_time_fixed = time.time() - start_time assert sleep_time_fixed < backoff * (2**1) # queue receiver receiver = client.get_queue_receiver('fake_name') backoff = client._config.retry_backoff_factor start_time = time.time() receiver._backoff(retried_times=1, last_exception=Exception('fake'), abs_timeout_time=None) sleep_time_fixed = time.time() - start_time assert sleep_time_fixed < backoff * (2**1) # subscription receiver receiver = client.get_subscription_receiver('fake_topic', 'fake_sub') backoff = client._config.retry_backoff_factor start_time = time.time() receiver._backoff(retried_times=1, last_exception=Exception('fake'), abs_timeout_time=None) sleep_time_fixed = time.time() - start_time assert sleep_time_fixed < backoff * (2**1) client = ServiceBusClient('fake.host.com', 'fake_eh', retry_mode=RetryMode.Fixed) # queue sender sender = client.get_queue_sender('fake_name') backoff = client._config.retry_backoff_factor start_time = time.time() sender._backoff(retried_times=1, last_exception=Exception('fake'), abs_timeout_time=None) sleep_time_fixed = time.time() - start_time # exp = 0.8 * (2 ** 1) = 1.6 # time.sleep() in _backoff will take AT LEAST time 'exp' for retry_mode='exponential' # check that fixed is less than 'exp' assert sleep_time_fixed < backoff * (2**1)