This is an automated email from the ASF dual-hosted git repository.

dabla pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 7389c32a68b Fix ObjectStoragePath on Azure for wasb and full-URL 
connections (#77376)
7389c32a68b is described below

commit 7389c32a68b0f2a20217dfe605f2a8540bd50454
Author: TEJA P C <[email protected]>
AuthorDate: Thu Nov 26 11:07:00 2026 -0600

    Fix ObjectStoragePath on Azure for wasb and full-URL connections (#77376)
    
    * Fix ObjectStoragePath on Azure for wasb and full-URL connections
    
    get_fs() passed account_url to adlfs.AzureBlobFileSystem, which has no
    such parameter and silently dropped it, so connections whose account is
    known only through host or login failed with 'Must provide either a
    connection_string or account_name with credentials'. Derive account_name
    and account_host from the account URL instead, keeping the Extra overlay
    on top, and add a test that builds the real filesystem.
    
    * Only set account_host for non-default Azure hosts
    
    adlfs already defaults to <account>.blob.core.windows.net, so passing the 
default host explicitly is redundant. Only a non-default host, such as a custom 
domain or sovereign cloud endpoint, needs to be passed through.
    
    * Keep dfs endpoints and the endpoint check in line with the blob client
    
    A dfs host was passed through as account_host, which moved connections that 
work on main from the blob endpoint to the Data Lake one, and the dot check for 
endpoint hosts disagreed with _bare_account_name_from_host when the only dot 
was in the path. Map standard Azure dfs endpoints back to their blob form, 
judge endpoint hosts by the parsed hostname the bare-host helper already uses, 
and cover both with tests, including a service principal dfs case against the 
real filesystem.
    
    * Fix mypy failure for None host in ADLS bare account name check
    
    The MyPy providers check fails on the bare-host comparison because host is 
typed str | None. Guarding the comparison keeps the check working for a None 
host, which already returned None through the hostname lookup.
    
    * Derive Azure account name and host from the parsed hostname in get_fs
    
    A host with a port and no scheme was misread by parse_blob_account_url
    as a scheme, so a connection without a login derived the account name
    "none" from the login-built URL. The dfs to blob mapping also only
    matched the dfs label in second position against a fixed cloud suffix
    set, so private-link endpoints and clouds outside the set stayed on
    the Data Lake endpoint. Derive the account name and account_host from
    the parsed hostname instead, map ".dfs.core." to ".blob.core."
    wherever it appears, and carry the port through urlparse in a small
    helper next to _hostname_from_host.
    
    * Unify ADLS host parsing into one helper and fix mypy narrowing
    
    mypy does not narrow conn_hostname through the host_is_endpoint boolean,
    so the endpoint branch failed the MyPy providers check with
    'Item "None" of "str | None" has no attribute "split"'. Test the value
    directly so it narrows, and parse the host once in get_fs.
    
    _hostname_from_host and _account_host_from_host parsed the host with two
    copies of the same scheme-less rule. Parse it once in _parsed_host and
    read the hostname and the account host from that result.
    
    The invalid-port fallback dropped a malformed port silently; it now
    raises ValueError from urlparse instead, since a malformed host is a
    configuration error and silently connecting to the wrong endpoint is
    worse. Add cases proving an out-of-range and a non-numeric port raise.
    
    ---------
    
    Co-authored-by: TEJA P C <[email protected]>
---
 .../airflow/providers/microsoft/azure/fs/adls.py   |  73 ++++-
 .../tests/unit/microsoft/azure/fs/test_adls.py     | 327 ++++++++++++++++++++-
 2 files changed, 385 insertions(+), 15 deletions(-)

diff --git 
a/providers/microsoft/azure/src/airflow/providers/microsoft/azure/fs/adls.py 
b/providers/microsoft/azure/src/airflow/providers/microsoft/azure/fs/adls.py
index 9fcce00a16b..3ab62397059 100644
--- a/providers/microsoft/azure/src/airflow/providers/microsoft/azure/fs/adls.py
+++ b/providers/microsoft/azure/src/airflow/providers/microsoft/azure/fs/adls.py
@@ -17,7 +17,7 @@
 from __future__ import annotations
 
 from typing import TYPE_CHECKING, Any
-from urllib.parse import urlparse
+from urllib.parse import ParseResult, urlparse
 
 from azure.identity import ClientSecretCredential
 
@@ -30,22 +30,53 @@ if TYPE_CHECKING:
 schemes = ["abfs", "abfss", "adl"]
 
 
+def _parsed_host(host: str | None) -> ParseResult | None:
+    """
+    Parse a connection host as a URL, treating a scheme-less host as a netloc.
+
+    Returns ``None`` for an empty host. The scheme-less handling lives here
+    alone, so the hostname and the account host are always read from the
+    same parse.
+    """
+    if not host:
+        return None
+    return urlparse(host if "://" in host else f"//{host}")
+
+
+def _hostname_from_host(host: str | None) -> str | None:
+    parsed = _parsed_host(host)
+    return parsed.hostname if parsed is not None else None
+
+
+def _account_host_from_parsed(parsed: ParseResult | None) -> str | None:
+    """
+    Return the blob account host for a parsed endpoint host, else ``None``.
+
+    adlfs is a Blob client, so a Data Lake (dfs) endpoint is mapped back to
+    its blob form wherever the ".dfs.core." labels appear in the hostname.
+    An invalid port raises ValueError: a malformed host is a configuration
+    error, and dropping the port silently would connect to the wrong endpoint.
+    """
+    if parsed is None or not parsed.hostname:
+        return None
+    hostname = parsed.hostname.replace(".dfs.core.", ".blob.core.")
+    port = parsed.port
+    return f"{hostname}:{port}" if port is not None else hostname
+
+
 def _bare_account_name_from_host(host: str | None) -> str | None:
     """
     Return the storage account name when the host is a bare account name, else 
``None``.
 
     A bare host such as ``mystorageaccount`` is the ADLS account name itself. 
A full host
     like ``mystorageaccount.dfs.core.windows.net`` (or anything with a scheme, 
port or path)
-    is an endpoint and should keep the ``account_url`` handling instead.
+    is an endpoint instead and goes through the account URL derivation in 
``get_fs``.
     """
-    if not host:
-        return None
-
     # urlparse().hostname is already lowercased, so compare against a 
lowercased host to keep
     # the check case-insensitive (Azure account names are lowercase anyway). 
The dot guard
     # keeps full endpoints like "acct.dfs.core.windows.net" out of the 
account_name path.
-    hostname = urlparse(host if "://" in host else f"//{host}").hostname
-    if hostname and "." not in hostname and hostname == host.lower():
+    hostname = _hostname_from_host(host)
+    if hostname and "." not in hostname and host is not None and hostname == 
host.lower():
         return hostname
     return None
 
@@ -77,8 +108,32 @@ def get_fs(conn_id: str | None, storage_options: dict[str, 
Any] | None = None) -
         # is the storage account name rather than part of an account URL.
         options["account_name"] = account_name
     elif conn_type != "adls" or conn.host or not tenant_id:
-        # WASB, full-URL, and custom-endpoint connections keep the historical 
account_url path.
-        options["account_url"] = parse_blob_account_url(conn.host, conn.login)
+        # adlfs has no account_url parameter and would silently drop it; it 
reads
+        # account_name and builds the URL from account_host, so derive both 
from
+        # the host. Only an endpoint host (a hostname with a dot, the same
+        # parsing _bare_account_name_from_host uses) supplies an account_host,
+        # and only when it is not the default blob host, which adlfs already
+        # defaults to. The name and host come from the parsed hostname, not the
+        # rebuilt account URL: parse_blob_account_url misreads a host with a
+        # port and no scheme as a scheme, and would build the URL from the
+        # login instead. A bare identifier is not an endpoint, and for those
+        # parse_blob_account_url builds the URL from the login. A bare login is
+        # the account name (wasb shared key), but with a tenant id the login 
is a
+        # service principal client id, so without an endpoint host nothing is
+        # derived and adlfs falls back to account_name from extras or the
+        # AZURE_STORAGE_ACCOUNT_NAME env var.
+        parsed_host = _parsed_host(conn.host)
+        conn_hostname = parsed_host.hostname if parsed_host is not None else 
None
+        if conn_hostname and "." in conn_hostname:
+            derived_account_name = conn_hostname.split(".")[0]
+            options["account_name"] = derived_account_name
+            account_host = _account_host_from_parsed(parsed_host)
+            if account_host and account_host != 
f"{derived_account_name}.blob.core.windows.net":
+                options["account_host"] = account_host
+        elif conn.login and not tenant_id:
+            account_url = urlparse(parse_blob_account_url(conn.host, 
conn.login))
+            if account_url.hostname:
+                options["account_name"] = account_url.hostname.split(".")[0]
     # Otherwise (adls + no host + a tenant_id) no account is set here: adlfs 
needs it from
     # ``account_name`` in Extra or the AZURE_STORAGE_ACCOUNT_NAME environment 
variable.
 
diff --git 
a/providers/microsoft/azure/tests/unit/microsoft/azure/fs/test_adls.py 
b/providers/microsoft/azure/tests/unit/microsoft/azure/fs/test_adls.py
index 8fed975750b..783618a7482 100644
--- a/providers/microsoft/azure/tests/unit/microsoft/azure/fs/test_adls.py
+++ b/providers/microsoft/azure/tests/unit/microsoft/azure/fs/test_adls.py
@@ -49,7 +49,7 @@ def mocked_blob_file_system():
                 host="testaccountname.blob.core.windows.net",
             ),
             {
-                "account_url": "https://testaccountname.blob.core.windows.net";,
+                "account_name": "testaccountname",
             },
         ),
         (
@@ -59,7 +59,7 @@ def mocked_blob_file_system():
                 login="testaccountname",
             ),
             {
-                "account_url": 
"https://testaccountname.blob.core.windows.net/";,
+                "account_name": "testaccountname",
             },
         ),
         (
@@ -96,10 +96,9 @@ def mocked_blob_file_system():
                 },
             ),
             {
-                "account_url": 
"https://testaccountname.blob.core.windows.net/";,
-                "account_name": "account_name",
                 "client_id": "testaccountname",
                 "client_secret": "password",
+                "account_name": "account_name",
                 "account_key": "account_key",
                 "sas_token": "sas_token",
                 "tenant_id": "tenant_id",
@@ -118,7 +117,7 @@ def mocked_blob_file_system():
                 extra={},
             ),
             {
-                "account_url": 
"https://testaccountname.blob.core.windows.net/";,
+                "account_name": "testaccountname",
                 "account_key": "password",
             },
         ),
@@ -134,11 +133,260 @@ def mocked_blob_file_system():
                 },
             ),
             {
-                "account_url": 
"https://testaccountname.blob.core.windows.net/";,
+                "account_name": "testaccountname",
                 "account_host": "mystorageaccount.blob.core.mydomain.io",
                 "account_key": "password",
             },
         ),
+        (
+            # Service principal without a host: the login is a client id, so
+            # no account_name or account_host may be derived from it; the
+            # account comes from extras alone.
+            Connection(
+                conn_id="testconn",
+                conn_type="wasb",
+                login="testclientid",
+                password="testclientsecret",
+                extra={
+                    "account_name": "actualstorage",
+                    "tenant_id": "tenant_id",
+                },
+            ),
+            {
+                "client_id": "testclientid",
+                "client_secret": "testclientsecret",
+                "account_name": "actualstorage",
+                "tenant_id": "tenant_id",
+            },
+        ),
+        (
+            # Same, but with a bare identifier in host: it is not an endpoint,
+            # so no account_host may be derived from the login-built URL.
+            Connection(
+                conn_id="testconn",
+                conn_type="wasb",
+                login="testclientid",
+                password="testclientsecret",
+                host="testaccountID",
+                extra={
+                    "account_name": "actualstorage",
+                    "tenant_id": "tenant_id",
+                },
+            ),
+            {
+                "client_id": "testclientid",
+                "client_secret": "testclientsecret",
+                "account_name": "actualstorage",
+                "tenant_id": "tenant_id",
+            },
+        ),
+        (
+            # Service principal with a bare identifier host and no account in
+            # extras: nothing may be derived from the client id login, so
+            # adlfs can fall back to AZURE_STORAGE_ACCOUNT_NAME.
+            Connection(
+                conn_id="testconn",
+                conn_type="wasb",
+                login="testclientid",
+                password="testclientsecret",
+                host="testaccountID",
+                extra={
+                    "tenant_id": "tenant_id",
+                },
+            ),
+            {
+                "client_id": "testclientid",
+                "client_secret": "testclientsecret",
+                "tenant_id": "tenant_id",
+            },
+        ),
+        (
+            # A Data Lake (dfs) endpoint is an endpoint host, but adlfs is a 
Blob
+            # client: the derived account_host is mapped back to the blob form,
+            # which is the default host here, so no account_host is set.
+            Connection(
+                conn_id="testconn",
+                conn_type="adls",
+                host="testaccountname.dfs.core.windows.net",
+                login="client_id",
+                password="client_secret",
+                extra={
+                    "tenant_id": "tenant_id",
+                },
+            ),
+            {
+                "account_name": "testaccountname",
+                "client_id": "client_id",
+                "client_secret": "client_secret",
+                "tenant_id": "tenant_id",
+            },
+        ),
+        (
+            # Same dfs host, with the account in extras: that account decides 
the
+            # URL, as on main, so no account_host may be derived from the host.
+            Connection(
+                conn_id="testconn",
+                conn_type="adls",
+                host="testaccountname.dfs.core.windows.net",
+                login="client_id",
+                password="client_secret",
+                extra={
+                    "account_name": "otheraccount",
+                    "tenant_id": "tenant_id",
+                },
+            ),
+            {
+                "account_name": "otheraccount",
+                "client_id": "client_id",
+                "client_secret": "client_secret",
+                "tenant_id": "tenant_id",
+            },
+        ),
+        (
+            # Hostnames are case-insensitive: an uppercase dfs host maps and
+            # compares like the lowercase one, so the account in extras still
+            # decides the URL and no account_host is set.
+            Connection(
+                conn_id="testconn",
+                conn_type="adls",
+                host="TestAccountName.DFS.core.windows.net",
+                login="client_id",
+                password="client_secret",
+                extra={
+                    "account_name": "otheraccount",
+                    "tenant_id": "tenant_id",
+                },
+            ),
+            {
+                "account_name": "otheraccount",
+                "client_id": "client_id",
+                "client_secret": "client_secret",
+                "tenant_id": "tenant_id",
+            },
+        ),
+        (
+            # A custom domain with a dfs label is not an Azure endpoint, so it
+            # is passed on unchanged instead of being mapped to a blob label.
+            Connection(
+                conn_id="testconn",
+                conn_type="wasb",
+                host="testaccountname.dfs.example.com",
+                login="testaccountname",
+                password="account_key",
+            ),
+            {
+                "account_name": "testaccountname",
+                "account_host": "testaccountname.dfs.example.com",
+                "account_key": "account_key",
+            },
+        ),
+        (
+            # The dfs mapping replaces the ".dfs.core." labels wherever they
+            # appear and does not depend on the cloud suffix, so a host on a
+            # cloud outside the well-known set maps to its blob form too.
+            Connection(
+                conn_id="testconn",
+                conn_type="wasb",
+                host="testaccountname.dfs.core.example.com",
+                login="testaccountname",
+                password="account_key",
+            ),
+            {
+                "account_name": "testaccountname",
+                "account_host": "testaccountname.blob.core.example.com",
+                "account_key": "account_key",
+            },
+        ),
+        (
+            # An explicit port does not stop the mapping: a dfs endpoint with a
+            # port maps to the blob endpoint with the same port, which is then
+            # not the default host and is passed on.
+            Connection(
+                conn_id="testconn",
+                conn_type="wasb",
+                host="https://testaccountname.dfs.core.windows.net:443";,
+                login="testaccountname",
+                password="account_key",
+            ),
+            {
+                "account_name": "testaccountname",
+                "account_host": "testaccountname.blob.core.windows.net:443",
+                "account_key": "account_key",
+            },
+        ),
+        (
+            # A dfs endpoint on a sovereign cloud maps to the blob endpoint of
+            # the same cloud, which is not the default host and is passed on.
+            Connection(
+                conn_id="testconn",
+                conn_type="wasb",
+                host="testaccountname.dfs.core.chinacloudapi.cn",
+                login="testaccountname",
+                password="account_key",
+            ),
+            {
+                "account_name": "testaccountname",
+                "account_host": "testaccountname.blob.core.chinacloudapi.cn",
+                "account_key": "account_key",
+            },
+        ),
+        (
+            # A host with a port and no scheme, on a connection without a
+            # login: the account comes from the host itself, not from the
+            # account URL parse_blob_account_url builds from the login.
+            Connection(
+                conn_id="testconn",
+                conn_type="wasb",
+                host="testaccountname.blob.core.windows.net:443",
+                password="account_key",
+            ),
+            {
+                "account_name": "testaccountname",
+                "account_host": "testaccountname.blob.core.windows.net:443",
+                "account_key": "account_key",
+            },
+        ),
+        (
+            # On a private-link name the dfs label is not the second label,
+            # but the endpoint still maps back to its blob form.
+            Connection(
+                conn_id="testconn",
+                conn_type="adls",
+                host="testaccountname.privatelink.dfs.core.windows.net",
+                login="client_id",
+                password="client_secret",
+                extra={
+                    "tenant_id": "tenant_id",
+                },
+            ),
+            {
+                "account_name": "testaccountname",
+                "account_host": 
"testaccountname.privatelink.blob.core.windows.net",
+                "client_id": "client_id",
+                "client_secret": "client_secret",
+                "tenant_id": "tenant_id",
+            },
+        ),
+        (
+            # A dot in the path does not make the host an endpoint: the 
hostname
+            # has no dot, so with a tenant id nothing is derived from the
+            # client id login.
+            Connection(
+                conn_id="testconn",
+                conn_type="wasb",
+                host="testaccountID/some.path",
+                login="testclientid",
+                password="testclientsecret",
+                extra={
+                    "tenant_id": "tenant_id",
+                },
+            ),
+            {
+                "client_id": "testclientid",
+                "client_secret": "testclientsecret",
+                "tenant_id": "tenant_id",
+            },
+        ),
         (
             Connection(
                 conn_id="testconn",
@@ -273,3 +521,70 @@ def test_get_fs(mocked_connection, expected_options, 
mocked_blob_file_system):
 )
 def test_bare_account_name_from_host(host, expected):
     assert _bare_account_name_from_host(host) == expected
+
+
[email protected](
+    "mocked_connection",
+    [
+        # An invalid port is a configuration error: it must raise instead of
+        # being dropped silently and connecting to the wrong endpoint.
+        Connection(
+            conn_id="testconn",
+            conn_type="wasb",
+            host="testaccountname.blob.core.windows.net:99999",
+            password="account_key",
+        ),
+        Connection(
+            conn_id="testconn",
+            conn_type="wasb",
+            host="testaccountname.blob.core.windows.net:notaport",
+            password="account_key",
+        ),
+    ],
+    indirect=["mocked_connection"],
+)
+def test_get_fs_invalid_port_raises(mocked_connection, 
mocked_blob_file_system):
+    with pytest.raises(ValueError, match="Port"):
+        get_fs(mocked_connection.conn_id)
+
+
[email protected](
+    "mocked_connection",
+    [
+        Connection(
+            conn_id="testconn",
+            conn_type="wasb",
+            login="testaccountname",
+            password="testaccountkey",
+        ),
+        Connection(
+            conn_id="testconn",
+            conn_type="wasb",
+            host="https://testaccountname.blob.core.windows.net";,
+            password="testaccountkey",
+        ),
+        # Service principal with a Data Lake endpoint in host: it must build
+        # against the blob endpoint, not the dfs one. The tenant id is a GUID
+        # because the real credential validates its format.
+        Connection(
+            conn_id="testconn",
+            conn_type="adls",
+            host="testaccountname.dfs.core.windows.net",
+            login="client_id",
+            password="client_secret",
+            extra={
+                "tenant_id": "11111111-1111-1111-1111-111111111111",
+            },
+        ),
+    ],
+    indirect=["mocked_connection"],
+)
+def test_get_fs_with_real_filesystem(mocked_connection, monkeypatch):
+    # The mocked test above cannot catch a keyword adlfs does not accept, so
+    # build the real filesystem: with only the account name or URL on the
+    # connection it must construct instead of raising
+    # "Must provide either a connection_string or account_name".
+    monkeypatch.delenv("AZURE_STORAGE_ACCOUNT_NAME", raising=False)
+    fs = get_fs(mocked_connection.conn_id)
+    assert fs.account_name == "testaccountname"
+    assert fs.account_url == "https://testaccountname.blob.core.windows.net";

Reply via email to