Skip to content

Commit 9e2d607

Browse files
Truncate Wasb storage account name if it's more than 24 characters (#33851)
* Truncate Wasb storage account name if it's more than 24 characters Storage account names must be between 3 and 24 characters in length but for some reasons that I can't explain, we saw a situation where the storage name is more than 24 characters and had to be truncated before it could work. Maybe it was possible in the past to have more than 24 characters or it could come from cluster but whichever way, the solution that worked was truncating the account name to 24 characters. * Apply suggestions from code review Co-authored-by: Jed Cunningham <[email protected]> * Also add the change to the async part --------- Co-authored-by: Jed Cunningham <[email protected]>
1 parent 6c943ca commit 9e2d607

File tree

2 files changed

+30
-10
lines changed
  • airflow/providers/microsoft/azure/hooks
  • tests/providers/microsoft/azure/hooks

2 files changed

+30
-10
lines changed

airflow/providers/microsoft/azure/hooks/wasb.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,18 @@ def get_conn(self) -> BlobServiceClient:
163163
account_url = conn.host if conn.host else f"https://{conn.login}.blob.core.windows.net/"
164164
parsed_url = urlparse(account_url)
165165

166-
if not parsed_url.netloc and "." not in parsed_url.path:
167-
# if there's no netloc and no dots in the path, then user only
168-
# provided the Active Directory ID, not the full URL or DNS name
169-
account_url = f"https://{conn.login}.blob.core.windows.net/"
166+
if not parsed_url.netloc:
167+
if "." not in parsed_url.path:
168+
# if there's no netloc and no dots in the path, then user only
169+
# provided the Active Directory ID, not the full URL or DNS name
170+
account_url = f"https://{conn.login}.blob.core.windows.net/"
171+
else:
172+
# if there's no netloc but there are dots in the path, then user
173+
# provided the DNS name without the https:// prefix.
174+
# Azure storage account name can only be 3 to 24 characters in length
175+
# https://learn.microsoft.com/en-us/azure/storage/common/storage-account-overview#storage-account-name
176+
acc_name = account_url.split(".")[0][:24]
177+
account_url = f"https://{acc_name}." + ".".join(account_url.split(".")[1:])
170178

171179
tenant = self._get_field(extra, "tenant_id")
172180
if tenant:
@@ -568,10 +576,18 @@ async def get_async_conn(self) -> AsyncBlobServiceClient:
568576
account_url = conn.host if conn.host else f"https://{conn.login}.blob.core.windows.net/"
569577
parsed_url = urlparse(account_url)
570578

571-
if not parsed_url.netloc and "." not in parsed_url.path:
572-
# if there's no netloc and no dots in the path, then user only
573-
# provided the Active Directory ID, not the full URL or DNS name
574-
account_url = f"https://{conn.login}.blob.core.windows.net/"
579+
if not parsed_url.netloc:
580+
if "." not in parsed_url.path:
581+
# if there's no netloc and no dots in the path, then user only
582+
# provided the Active Directory ID, not the full URL or DNS name
583+
account_url = f"https://{conn.login}.blob.core.windows.net/"
584+
else:
585+
# if there's no netloc but there are dots in the path, then user
586+
# provided the DNS name without the https:// prefix.
587+
# Azure storage account name can only be 3 to 24 characters in length
588+
# https://learn.microsoft.com/en-us/azure/storage/common/storage-account-overview#storage-account-name
589+
acc_name = account_url.split(".")[0][:24]
590+
account_url = f"https://{acc_name}." + ".".join(account_url.split(".")[1:])
575591

576592
tenant = self._get_field(extra, "tenant_id")
577593
if tenant:

tests/providers/microsoft/azure/hooks/test_wasb.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,12 @@ def test_extra_client_secret_auth_config_ad_connection(self):
346346
"https://testaccountname.blob.core.windows.net",
347347
),
348348
("testhost", "https://accountlogin.blob.core.windows.net/"),
349-
("testhost.dns", "testhost.dns"),
350-
("testhost.blob.net", "testhost.blob.net"),
349+
("testhost.dns", "https://testhost.dns"),
350+
("testhost.blob.net", "https://testhost.blob.net"),
351+
(
352+
"testhostakjhdisdfbearioyo.blob.core.windows.net",
353+
"https://testhostakjhdisdfbearioy.blob.core.windows.net",
354+
), # more than 24 characters
351355
],
352356
)
353357
def test_proper_account_url_update(

0 commit comments

Comments
 (0)