From 3fc86235eb3b537785f6b36266ee27059755fa81 Mon Sep 17 00:00:00 2001 From: davidbossanyi Date: Sun, 16 Oct 2022 13:50:44 +0100 Subject: [PATCH 1/3] Parse credential as a dict when using Azurite emulator This more flexible credential allows the use of Azurite for integration testing in local docker-compose configurations. --- kombu/transport/azurestoragequeues.py | 8 +++++++- t/unit/transport/test_azurestoragequeues.py | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/kombu/transport/azurestoragequeues.py b/kombu/transport/azurestoragequeues.py index 0e3837a8e..b3202aee8 100644 --- a/kombu/transport/azurestoragequeues.py +++ b/kombu/transport/azurestoragequeues.py @@ -35,6 +35,7 @@ import string from queue import Empty +from typing import Union from azure.core.exceptions import ResourceExistsError @@ -178,7 +179,7 @@ class Transport(virtual.Transport): can_parse_url = True @staticmethod - def parse_uri(uri: str) -> tuple[str, str]: + def parse_uri(uri: str) -> tuple[Union[str, dict], str]: # URL like: # azurestoragequeues://STORAGE_ACCOUNT_ACCESS_KEY@STORAGE_ACCOUNT_URL # azurestoragequeues://SAS_TOKEN@STORAGE_ACCOUNT_URL @@ -191,6 +192,11 @@ def parse_uri(uri: str) -> tuple[str, str]: uri = uri.replace('azurestoragequeues://', '') # > 'some/key', 'url' credential, url = uri.rsplit('@', 1) + if "devstoreaccount1" in url and not ".core.windows.net" in url: # azurite + credential = { + "account_name": "devstoreaccount1", + "account_key": credential, + } # Validate parameters assert all([credential, url]) diff --git a/t/unit/transport/test_azurestoragequeues.py b/t/unit/transport/test_azurestoragequeues.py index d5568cbbe..332618d0a 100644 --- a/t/unit/transport/test_azurestoragequeues.py +++ b/t/unit/transport/test_azurestoragequeues.py @@ -11,6 +11,8 @@ URL_NOCREDS = 'azurestoragequeues://' URL_CREDS = 'azurestoragequeues://sas/key%@https://STORAGE_ACCOUNT_NAME.queue.core.windows.net/' # noqa +AZURITE_CREDS = 'azurestoragequeues://Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==@http://localhost:10001/devstoreaccount1' +AZURITE_CREDS_DOCKER_COMPOSE = 'azurestoragequeues://Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==@http://azurite:10001/devstoreaccount1' def test_queue_service_nocredentials(): @@ -31,3 +33,16 @@ def test_queue_service(): # Check the SAS token "sas/key%" has been parsed from the url correctly assert channel._credential == 'sas/key%' assert channel._url == 'https://STORAGE_ACCOUNT_NAME.queue.core.windows.net/' # noqa + + +@pytest.mark.parametrize("creds, hostname", [(AZURITE_CREDS, 'localhost'), (AZURITE_CREDS_DOCKER_COMPOSE, 'azurite')]) +def test_queue_service_works_for_azurite(creds, hostname): + conn = Connection(creds, transport=azurestoragequeues.Transport) + with patch('kombu.transport.azurestoragequeues.QueueServiceClient'): + channel = conn.channel() + + assert channel._credential == { + 'account_name': 'devstoreaccount1', + 'account_key': 'Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==' + } + assert channel._url == 'http://{}:10001/devstoreaccount1'.format(hostname) # noqa From 7ee071a9e0ec195a0e4257802d9d231926d5c403 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 16 Oct 2022 13:04:18 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- kombu/transport/azurestoragequeues.py | 2 +- t/unit/transport/test_azurestoragequeues.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/kombu/transport/azurestoragequeues.py b/kombu/transport/azurestoragequeues.py index b3202aee8..21324c849 100644 --- a/kombu/transport/azurestoragequeues.py +++ b/kombu/transport/azurestoragequeues.py @@ -179,7 +179,7 @@ class Transport(virtual.Transport): can_parse_url = True @staticmethod - def parse_uri(uri: str) -> tuple[Union[str, dict], str]: + def parse_uri(uri: str) -> tuple[str | dict, str]: # URL like: # azurestoragequeues://STORAGE_ACCOUNT_ACCESS_KEY@STORAGE_ACCOUNT_URL # azurestoragequeues://SAS_TOKEN@STORAGE_ACCOUNT_URL diff --git a/t/unit/transport/test_azurestoragequeues.py b/t/unit/transport/test_azurestoragequeues.py index 332618d0a..a7829c632 100644 --- a/t/unit/transport/test_azurestoragequeues.py +++ b/t/unit/transport/test_azurestoragequeues.py @@ -45,4 +45,4 @@ def test_queue_service_works_for_azurite(creds, hostname): 'account_name': 'devstoreaccount1', 'account_key': 'Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==' } - assert channel._url == 'http://{}:10001/devstoreaccount1'.format(hostname) # noqa + assert channel._url == f'http://{hostname}:10001/devstoreaccount1' # noqa From afff7a492fcbc2526d8aedb04f837b56f3824337 Mon Sep 17 00:00:00 2001 From: davidbossanyi Date: Sun, 16 Oct 2022 14:12:09 +0100 Subject: [PATCH 3/3] Fix some lint errors --- kombu/transport/azurestoragequeues.py | 5 +++-- t/unit/transport/test_azurestoragequeues.py | 14 ++++++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/kombu/transport/azurestoragequeues.py b/kombu/transport/azurestoragequeues.py index 21324c849..ab5900195 100644 --- a/kombu/transport/azurestoragequeues.py +++ b/kombu/transport/azurestoragequeues.py @@ -35,7 +35,6 @@ import string from queue import Empty -from typing import Union from azure.core.exceptions import ResourceExistsError @@ -192,7 +191,9 @@ def parse_uri(uri: str) -> tuple[str | dict, str]: uri = uri.replace('azurestoragequeues://', '') # > 'some/key', 'url' credential, url = uri.rsplit('@', 1) - if "devstoreaccount1" in url and not ".core.windows.net" in url: # azurite + + # parse credential as a dict if Azurite is being used + if "devstoreaccount1" in url and ".core.windows.net" not in url: credential = { "account_name": "devstoreaccount1", "account_key": credential, diff --git a/t/unit/transport/test_azurestoragequeues.py b/t/unit/transport/test_azurestoragequeues.py index a7829c632..44fa859be 100644 --- a/t/unit/transport/test_azurestoragequeues.py +++ b/t/unit/transport/test_azurestoragequeues.py @@ -11,8 +11,8 @@ URL_NOCREDS = 'azurestoragequeues://' URL_CREDS = 'azurestoragequeues://sas/key%@https://STORAGE_ACCOUNT_NAME.queue.core.windows.net/' # noqa -AZURITE_CREDS = 'azurestoragequeues://Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==@http://localhost:10001/devstoreaccount1' -AZURITE_CREDS_DOCKER_COMPOSE = 'azurestoragequeues://Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==@http://azurite:10001/devstoreaccount1' +AZURITE_CREDS = 'azurestoragequeues://Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==@http://localhost:10001/devstoreaccount1' # noqa +AZURITE_CREDS_DOCKER_COMPOSE = 'azurestoragequeues://Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==@http://azurite:10001/devstoreaccount1' # noqa def test_queue_service_nocredentials(): @@ -35,7 +35,13 @@ def test_queue_service(): assert channel._url == 'https://STORAGE_ACCOUNT_NAME.queue.core.windows.net/' # noqa -@pytest.mark.parametrize("creds, hostname", [(AZURITE_CREDS, 'localhost'), (AZURITE_CREDS_DOCKER_COMPOSE, 'azurite')]) +@pytest.mark.parametrize( + "creds, hostname", + [ + (AZURITE_CREDS, 'localhost'), + (AZURITE_CREDS_DOCKER_COMPOSE, 'azurite'), + ] +) def test_queue_service_works_for_azurite(creds, hostname): conn = Connection(creds, transport=azurestoragequeues.Transport) with patch('kombu.transport.azurestoragequeues.QueueServiceClient'): @@ -43,6 +49,6 @@ def test_queue_service_works_for_azurite(creds, hostname): assert channel._credential == { 'account_name': 'devstoreaccount1', - 'account_key': 'Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==' + 'account_key': 'Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==' # noqa } assert channel._url == f'http://{hostname}:10001/devstoreaccount1' # noqa