async def test_client_named_key_credential_async(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) async with client: async with client.get_queue_sender(servicebus_queue.name) as sender: await sender.send_messages(ServiceBusMessage("foo")) credential.update("foo", "bar") with pytest.raises(Exception): async with client: async with client.get_queue_sender(servicebus_queue.name) as sender: await sender.send_messages(ServiceBusMessage("foo")) # update back to the right key again credential.update(servicebus_namespace_key_name, servicebus_namespace_primary_key) async with client: async with client.get_queue_sender(servicebus_queue.name) as sender: await sender.send_messages(ServiceBusMessage("foo"))
async def test_client_credential_async(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) async with client: assert len(client._handlers) == 0 async with client.get_queue_sender(servicebus_queue.name) as sender: await sender.send_messages(ServiceBusMessage("foo")) hostname = "sb://{}.servicebus.windows.net".format(servicebus_namespace.name) client = ServiceBusClient(hostname, credential) async with client: assert len(client._handlers) == 0 async with client.get_queue_sender(servicebus_queue.name) as sender: await sender.send_messages(ServiceBusMessage("foo")) hostname = "https://{}.servicebus.windows.net \ ".format(servicebus_namespace.name) client = ServiceBusClient(hostname, credential) async with client: assert len(client._handlers) == 0 async with client.get_queue_sender(servicebus_queue.name) as sender: await sender.send_messages(ServiceBusMessage("foo"))
async def test_sb_client_bad_credentials_async(self, servicebus_namespace, servicebus_queue, **kwargs): client = ServiceBusClient( fully_qualified_namespace=servicebus_namespace.name + '.servicebus.windows.net', credential=ServiceBusSharedKeyCredential('invalid', 'invalid'), logging_enable=False) async with client: with pytest.raises(ServiceBusAuthenticationError): async with client.get_queue_sender(servicebus_queue.name) as sender: await sender.send_messages(ServiceBusMessage("test"))
async def run(): 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) async with servicebus_client: sender = servicebus_client.get_queue_sender(queue_name=QUEUE_NAME) async with sender: await sender.send_messages(ServiceBusMessage('Single Message')) await credential.close()
async def _send_message(message: ServiceBusMessage, queue: str): """ Sends the given message to the given queue in the Service Bus. :param message: The message to send. :type message: ServiceBusMessage :param queue: The Service Bus queue to send the message to. :type queue: str """ async with default_credentials() as credential: service_bus_client = ServiceBusClient(config.SERVICE_BUS_FULLY_QUALIFIED_NAMESPACE, credential) async with service_bus_client: sender = service_bus_client.get_queue_sender(queue_name=queue) async with sender: await sender.send_messages(message)
async def test_client_azure_sas_credential_async(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 = (await credential.get_token(auth_uri)).token.decode() credential = AzureSasCredential(token) client = ServiceBusClient(hostname, credential) async with client: assert len(client._handlers) == 0 async with client.get_queue_sender(servicebus_queue.name) as sender: await sender.send_messages(ServiceBusMessage("foo"))
async def invoke_porter_action(msg_body: dict, sb_client: ServiceBusClient, message_logger_adapter: logging.LoggerAdapter, config: dict) -> bool: """ Handle resource message by invoking specified porter action (i.e. install, uninstall) """ installation_id = get_installation_id(msg_body) action = msg_body["action"] message_logger_adapter.info( f"{installation_id}: {action} action starting...") sb_sender = sb_client.get_queue_sender( queue_name=config["deployment_status_queue"]) # If the action is install/upgrade, post message on sb queue to start a deployment job if action == "install" or action == "upgrade": resource_request_message = service_bus_message_generator( msg_body, strings.RESOURCE_STATUS_DEPLOYING, "Deployment job starting") await sb_sender.send_messages( ServiceBusMessage(body=resource_request_message, correlation_id=msg_body["id"])) # Build and run porter command (flagging if its a built-in action or custom so we can adapt porter command appropriately) is_custom_action = action not in ["install", "upgrade", "uninstall"] porter_command = await build_porter_command(config, message_logger_adapter, msg_body, is_custom_action) returncode, _, err = await run_porter(porter_command, message_logger_adapter, config) # Handle command output if returncode != 0: error_message = "Error context message = " + " ".join( err.split('\n')) + " ; Command executed: ".join(porter_command) resource_request_message = service_bus_message_generator( msg_body, statuses.failed_status_string_for[action], error_message) # Post message on sb queue to notify receivers of action failure await sb_sender.send_messages( ServiceBusMessage(body=resource_request_message, correlation_id=msg_body["id"])) message_logger_adapter.info( f"{installation_id}: Porter action failed with error = {error_message}" ) return False else: # Get the outputs # TODO: decide if this should "fail" the deployment _, outputs = await get_porter_outputs(msg_body, message_logger_adapter, config) success_message = f"{action} action completed successfully." resource_request_message = service_bus_message_generator( msg_body, statuses.pass_status_string_for[action], success_message, outputs) await sb_sender.send_messages( ServiceBusMessage(body=resource_request_message, correlation_id=msg_body["id"])) message_logger_adapter.info(f"{installation_id}: {success_message}") return True
async def run(): servicebus_client = ServiceBusClient(FULLY_QUALIFIED_NAMESPACE, credential) async with servicebus_client: sender = servicebus_client.get_queue_sender(queue_name=QUEUE_NAME) async with sender: await sender.send(Message("DATA" * 64))