From 635e1706389d22c8fc065ab6fbab4a6f6e5e0034 Mon Sep 17 00:00:00 2001 From: Josh Schneier Date: Fri, 29 Sep 2023 01:06:24 -0400 Subject: [PATCH] [azure] do not require both Account(Name|Key) in connection_string (#1312) --- storages/backends/azure_storage.py | 13 ++++++++----- tests/test_azure.py | 7 +++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/storages/backends/azure_storage.py b/storages/backends/azure_storage.py index 77e45c96..0ae14802 100644 --- a/storages/backends/azure_storage.py +++ b/storages/backends/azure_storage.py @@ -1,10 +1,10 @@ import mimetypes -import re from datetime import datetime from datetime import timedelta from tempfile import SpooledTemporaryFile from azure.core.exceptions import ResourceNotFoundError +from azure.core.utils import parse_connection_string from azure.storage.blob import BlobClient from azure.storage.blob import BlobSasPermissions from azure.storage.blob import BlobServiceClient @@ -124,10 +124,13 @@ def __init__(self, **settings): self._user_delegation_key = None self._user_delegation_key_expiry = datetime.utcnow() if self.connection_string and (not self.account_name or not self.account_key): - self.account_name = re.search( - r"AccountName=(\w+);", self.connection_string - )[1] - self.account_key = re.search(r"AccountKey=(.*);", self.connection_string)[1] + parsed = parse_connection_string( + self.connection_string, case_sensitive_keys=True + ) + if not self.account_name and "AccountName" in parsed: + self.account_name = parsed["AccountName"] + if not self.account_key and "AccountKey" in parsed: + self.account_key = parsed["AccountKey"] def get_default_settings(self): return { diff --git a/tests/test_azure.py b/tests/test_azure.py index b454fc2f..9ae2e01c 100644 --- a/tests/test_azure.py +++ b/tests/test_azure.py @@ -292,6 +292,13 @@ def test_container_client_params_token_credential(self): "https://test.blob.core.windows.net", credential="foo_cred" ) + def test_connection_string_can_have_missing(self): + storage = azure_storage.AzureStorage( + connection_string="AccountKey=abc;Foobar=xyz;" + ) + self.assertEqual(storage.account_key, "abc") + self.assertIsNone(storage.account_name) + def test_container_client_params_connection_string(self): storage = azure_storage.AzureStorage() storage.account_name = self.account_name