Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pyiceberg/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def _is_local_path(path: str) -> bool:
ADLS_CREDENTIAL = "adls.credential"
ADLS_ACCOUNT_NAME = "adls.account-name"
ADLS_ACCOUNT_KEY = "adls.account-key"
ADLS_SHARED_KEY_ACCOUNT_NAME = "adls.auth.shared-key.account.name"
ADLS_SHARED_KEY_ACCOUNT_KEY = "adls.auth.shared-key.account.key"
ADLS_SAS_TOKEN = "adls.sas-token"
ADLS_TENANT_ID = "adls.tenant-id"
ADLS_CLIENT_ID = "adls.client-id"
Expand Down
12 changes: 9 additions & 3 deletions pyiceberg/io/fsspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
ADLS_CONNECTION_STRING,
ADLS_CREDENTIAL,
ADLS_SAS_TOKEN,
ADLS_SHARED_KEY_ACCOUNT_KEY,
ADLS_SHARED_KEY_ACCOUNT_NAME,
ADLS_TENANT_ID,
ADLS_TOKEN,
AWS_ACCESS_KEY_ID,
Expand Down Expand Up @@ -287,7 +289,7 @@ def _adls(properties: Properties, hostname: str | None = None) -> AbstractFileSy
from azure.core.credentials import AccessToken
from azure.core.credentials_async import AsyncTokenCredential

account_name = properties.get(ADLS_ACCOUNT_NAME)
account_name = get_first_property_value(properties, ADLS_ACCOUNT_NAME, ADLS_SHARED_KEY_ACCOUNT_NAME)
sas_token = properties.get(ADLS_SAS_TOKEN)

for key, per_account_sas_token in {
Expand All @@ -302,6 +304,10 @@ def _adls(properties: Properties, hostname: str | None = None) -> AbstractFileSy
if hostname and account_name is None:
account_name = hostname.split(".")[0]

connection_string = properties.get(ADLS_CONNECTION_STRING)
if connection_string is None and account_name is not None:
connection_string = properties.get(f"{ADLS_CONNECTION_STRING}.{account_name}")

class StaticTokenCredential(AsyncTokenCredential):
_DEFAULT_EXPIRY_SECONDS = 3600

Expand All @@ -321,10 +327,10 @@ async def get_token(self, *scopes: str, **kwargs: Any) -> AccessToken:
credential = properties.get(ADLS_CREDENTIAL) # type: ignore

return AzureBlobFileSystem(
connection_string=properties.get(ADLS_CONNECTION_STRING),
connection_string=connection_string,
credential=credential,
account_name=account_name,
account_key=properties.get(ADLS_ACCOUNT_KEY),
account_key=get_first_property_value(properties, ADLS_ACCOUNT_KEY, ADLS_SHARED_KEY_ACCOUNT_KEY),
sas_token=sas_token,
tenant_id=properties.get(ADLS_TENANT_ID),
client_id=properties.get(ADLS_CLIENT_ID),
Expand Down
6 changes: 4 additions & 2 deletions pyiceberg/io/pyarrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@
ADLS_DFS_STORAGE_AUTHORITY,
ADLS_DFS_STORAGE_SCHEME,
ADLS_SAS_TOKEN,
ADLS_SHARED_KEY_ACCOUNT_KEY,
ADLS_SHARED_KEY_ACCOUNT_NAME,
ADLS_TENANT_ID,
AWS_ACCESS_KEY_ID,
AWS_ASSUME_ROLE_ARN,
Expand Down Expand Up @@ -601,14 +603,14 @@ def _initialize_azure_fs(self, netloc: str | None = None) -> FileSystem:

client_kwargs: dict[str, str] = {}

account_name = self.properties.get(ADLS_ACCOUNT_NAME)
account_name = get_first_property_value(self.properties, ADLS_ACCOUNT_NAME, ADLS_SHARED_KEY_ACCOUNT_NAME)
if account_name is None and netloc and "@" in netloc:
account_name = netloc.rpartition("@")[2].split(".")[0] or None

if account_name:
client_kwargs["account_name"] = account_name

if account_key := self.properties.get(ADLS_ACCOUNT_KEY):
if account_key := get_first_property_value(self.properties, ADLS_ACCOUNT_KEY, ADLS_SHARED_KEY_ACCOUNT_KEY):
client_kwargs["account_key"] = account_key

if blob_storage_authority := self.properties.get(ADLS_BLOB_STORAGE_AUTHORITY):
Expand Down
43 changes: 43 additions & 0 deletions tests/io/test_fsspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,49 @@ def test_adls_account_name_sas_token_extraction() -> None:
)


def test_fsspec_adls_accepts_java_shared_key_properties() -> None:
session_properties: Properties = {
"adls.auth.shared-key.account.name": "testaccount",
"adls.auth.shared-key.account.key": "test-key",
}

with mock.patch("adlfs.AzureBlobFileSystem") as mock_adlfs:
FsspecFileIO(properties=session_properties).new_input(location="abfss://tests/file.parquet")

client_kwargs = mock_adlfs.call_args.kwargs
assert client_kwargs["account_name"] == "testaccount"
assert client_kwargs["account_key"] == "test-key"


def test_fsspec_adls_accepts_per_account_connection_string() -> None:
session_properties: Properties = {
"adls.connection-string.connectionaccount": "DefaultEndpointsProtocol=https;AccountName=connectionaccount",
}

with mock.patch("adlfs.AzureBlobFileSystem") as mock_adlfs:
FsspecFileIO(properties=session_properties).new_input(
location="abfss://container@connectionaccount.dfs.core.windows.net/file.parquet"
)

assert mock_adlfs.call_args.kwargs["connection_string"] == (
"DefaultEndpointsProtocol=https;AccountName=connectionaccount"
)


def test_fsspec_adls_single_connection_string_takes_precedence() -> None:
session_properties: Properties = {
"adls.connection-string": "single",
"adls.connection-string.connectionaccount": "per-account",
}

with mock.patch("adlfs.AzureBlobFileSystem") as mock_adlfs:
FsspecFileIO(properties=session_properties).new_input(
location="abfss://container@connectionaccount.dfs.core.windows.net/file.parquet"
)

assert mock_adlfs.call_args.kwargs["connection_string"] == "single"


def test_adls_account_name_extracted_from_uri_hostname() -> None:
"""Test that account_name is extracted from the ABFSS URI hostname when not in properties."""
session_properties: Properties = {
Expand Down
31 changes: 31 additions & 0 deletions tests/io/test_pyarrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4713,6 +4713,37 @@ def test_adls_account_name_property_wins_over_location() -> None:
assert mock_azure_fs.call_args.kwargs["account_name"] == "configured"


@skip_if_pyarrow_too_old
def test_adls_java_shared_key_properties() -> None:
"""Java configs name the shared key with adls.auth.shared-key.*."""
with patch("pyarrow.fs.AzureFileSystem") as mock_azure_fs:
PyArrowFileIO(
{
"adls.auth.shared-key.account.name": "configured",
"adls.auth.shared-key.account.key": "secret",
}
).fs_by_scheme("abfss", "warehouse")

assert mock_azure_fs.call_args.kwargs["account_name"] == "configured"
assert mock_azure_fs.call_args.kwargs["account_key"] == "secret"


@skip_if_pyarrow_too_old
def test_adls_native_shared_key_properties_take_precedence() -> None:
with patch("pyarrow.fs.AzureFileSystem") as mock_azure_fs:
PyArrowFileIO(
{
"adls.account-name": "native",
"adls.account-key": "native-secret",
"adls.auth.shared-key.account.name": "java",
"adls.auth.shared-key.account.key": "java-secret",
}
).fs_by_scheme("abfss", "warehouse")

assert mock_azure_fs.call_args.kwargs["account_name"] == "native"
assert mock_azure_fs.call_args.kwargs["account_key"] == "native-secret"


@skip_if_pyarrow_too_old
def test_adls_no_account_name_from_container_only_location() -> None:
"""A container only netloc carries no account, so nothing should be inferred from it."""
Expand Down
Loading