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
43 changes: 42 additions & 1 deletion pyiceberg/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

from pyiceberg.io.content_cache import ContentCache
from pyiceberg.typedef import EMPTY_DICT, Properties
from pyiceberg.utils.properties import property_as_bool, property_as_int
from pyiceberg.utils.properties import property_as_bool, property_as_float, property_as_int

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -89,6 +89,18 @@ def _is_local_path(path: str) -> bool:
S3_ALLOW_DELAYED_OPEN = "s3.allow-delayed-open"
S3_SERVER_SIDE_ENCRYPTION = "s3.server-side-encryption"
S3_SSE_KMS_KEY_ID = "s3.sse-kms-key-id"
S3_SSE_TYPE = "s3.sse.type"
S3_SSE_KEY = "s3.sse.key"
S3_PATH_STYLE_ACCESS = "s3.path-style-access"
S3_PROXY_ENDPOINT = "http-client.proxy-endpoint"
S3_CONNECT_TIMEOUT_MS = "http-client.connection-timeout-ms"
S3_SOCKET_TIMEOUT_MS = "http-client.socket-timeout-ms"

S3_SSE_TYPE_TO_ENCRYPTION = {
"s3": "AES256",
"kms": "aws:kms",
"dsse-kms": "aws:kms:dsse",
}
HDFS_HOST = "hdfs.host"
HDFS_PORT = "hdfs.port"
HDFS_USER = "hdfs.user"
Expand Down Expand Up @@ -132,6 +144,35 @@ def _is_local_path(path: str) -> bool:
IO_MANIFEST_CACHE_EXPIRATION_INTERVAL_MS_DEFAULT = 60_000


def _resolve_s3_properties(properties: Properties) -> Properties:
resolved: Properties = dict(properties)

if S3_FORCE_VIRTUAL_ADDRESSING not in resolved and resolved.get(S3_PATH_STYLE_ACCESS) not in (None, ""):
virtual_addressing = not property_as_bool(resolved, S3_PATH_STYLE_ACCESS, False)
resolved[S3_FORCE_VIRTUAL_ADDRESSING] = str(virtual_addressing).lower()

if S3_PROXY_URI not in resolved and (proxy_endpoint := resolved.get(S3_PROXY_ENDPOINT)) not in (None, ""):
resolved[S3_PROXY_URI] = proxy_endpoint

if S3_CONNECT_TIMEOUT not in resolved and (timeout_ms := property_as_float(resolved, S3_CONNECT_TIMEOUT_MS)) is not None:
resolved[S3_CONNECT_TIMEOUT] = str(timeout_ms / 1000)

if S3_REQUEST_TIMEOUT not in resolved and (timeout_ms := property_as_float(resolved, S3_SOCKET_TIMEOUT_MS)) is not None:
resolved[S3_REQUEST_TIMEOUT] = str(timeout_ms / 1000)

if S3_SERVER_SIDE_ENCRYPTION not in resolved and (sse_type := resolved.get(S3_SSE_TYPE)) not in (None, ""):
normalized_sse_type = str(sse_type).lower()
if normalized_sse_type != "none":
if (encryption := S3_SSE_TYPE_TO_ENCRYPTION.get(normalized_sse_type)) is None:
raise ValueError(f"Unsupported {S3_SSE_TYPE}: {sse_type}")
resolved[S3_SERVER_SIDE_ENCRYPTION] = encryption
if normalized_sse_type in ("kms", "dsse-kms") and S3_SSE_KMS_KEY_ID not in resolved:
if (sse_key := resolved.get(S3_SSE_KEY)) not in (None, ""):
resolved[S3_SSE_KMS_KEY_ID] = sse_key

return resolved


@runtime_checkable
class InputStream(Protocol):
"""A protocol for the file-like object returned by InputFile.open(...).
Expand Down
3 changes: 2 additions & 1 deletion pyiceberg/io/fsspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
OutputFile,
OutputStream,
_is_local_path,
_resolve_s3_properties,
)
from pyiceberg.typedef import Properties
from pyiceberg.types import strtobool
Expand Down Expand Up @@ -470,7 +471,7 @@ class FsspecFileIO(_TransientStateMixin, FileIO):
def __init__(self, properties: Properties):
self._scheme_to_fs: dict[str, Callable[..., AbstractFileSystem]] = dict(SCHEME_TO_FS)
self._init_transient()
super().__init__(properties=properties)
super().__init__(properties=_resolve_s3_properties(properties))

def _init_transient(self) -> None:
self._thread_locals = threading.local()
Expand Down
3 changes: 2 additions & 1 deletion pyiceberg/io/pyarrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
OutputFile,
OutputStream,
_is_local_path,
_resolve_s3_properties,
)
from pyiceberg.io.fileformat import DataFileStatistics as DataFileStatistics
from pyiceberg.io.fileformat import FileFormatFactory, FileFormatModel, FileFormatWriter
Expand Down Expand Up @@ -413,7 +414,7 @@ class PyArrowFileIO(_TransientStateMixin, FileIO):

def __init__(self, properties: Properties = EMPTY_DICT):
self._init_transient()
super().__init__(properties=properties)
super().__init__(properties=_resolve_s3_properties(properties))

def _init_transient(self) -> None:
self.fs_by_scheme = lru_cache(self._initialize_fs)
Expand Down
80 changes: 80 additions & 0 deletions tests/io/test_fsspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,86 @@ def test_fsspec_s3_session_properties_force_virtual_addressing(
)


def test_fsspec_s3_accepts_java_client_properties() -> None:
session_properties: Properties = {
"s3.path-style-access": "false",
"http-client.proxy-endpoint": "http://proxy:8080",
"http-client.connection-timeout-ms": "1500",
"http-client.socket-timeout-ms": "2500",
"s3.endpoint": "http://localhost:9000",
"s3.access-key-id": "admin",
"s3.secret-access-key": "password",
"s3.region": "us-east-1",
"s3.session-token": "s3.session-token",
**UNIFIED_AWS_SESSION_PROPERTIES,
}

with mock.patch("s3fs.S3FileSystem") as mock_s3fs:
FsspecFileIO(properties=session_properties).new_input(location=f"s3://warehouse/{uuid.uuid4()}")

config_kwargs = mock_s3fs.call_args.kwargs["config_kwargs"]
assert config_kwargs["s3"] == {"addressing_style": "virtual"}
assert config_kwargs["proxies"] == {"http": "http://proxy:8080", "https": "http://proxy:8080"}
assert config_kwargs["connect_timeout"] == 1.5
assert config_kwargs["read_timeout"] == 2.5


@pytest.mark.parametrize(
("sse_type", "server_side_encryption", "kms_key_id"),
[
("s3", "AES256", None),
("kms", "aws:kms", "test-key"),
("dsse-kms", "aws:kms:dsse", "test-key"),
("none", None, None),
],
)
def test_fsspec_s3_accepts_java_sse_type(sse_type: str, server_side_encryption: str | None, kms_key_id: str | None) -> None:
session_properties: Properties = {
"s3.sse.type": sse_type,
"s3.sse.key": "test-key",
"s3.endpoint": "http://localhost:9000",
"s3.access-key-id": "admin",
"s3.secret-access-key": "password",
"s3.region": "us-east-1",
"s3.session-token": "s3.session-token",
**UNIFIED_AWS_SESSION_PROPERTIES,
}

with mock.patch("s3fs.S3FileSystem") as mock_s3fs:
FsspecFileIO(properties=session_properties).new_input(location=f"s3://warehouse/{uuid.uuid4()}")

additional_kwargs = mock_s3fs.call_args.kwargs.get("s3_additional_kwargs", {})
assert additional_kwargs.get("ServerSideEncryption") == server_side_encryption
assert additional_kwargs.get("SSEKMSKeyId") == kms_key_id


def test_fsspec_s3_native_sse_takes_precedence_over_java_sse_type() -> None:
session_properties: Properties = {
"s3.sse.type": "kms",
"s3.sse.key": "java-key",
"s3.server-side-encryption": "AES256",
"s3.sse-kms-key-id": "native-key",
"s3.endpoint": "http://localhost:9000",
"s3.access-key-id": "admin",
"s3.secret-access-key": "password",
"s3.region": "us-east-1",
"s3.session-token": "s3.session-token",
**UNIFIED_AWS_SESSION_PROPERTIES,
}

with mock.patch("s3fs.S3FileSystem") as mock_s3fs:
FsspecFileIO(properties=session_properties).new_input(location=f"s3://warehouse/{uuid.uuid4()}")

additional_kwargs = mock_s3fs.call_args.kwargs["s3_additional_kwargs"]
assert additional_kwargs["ServerSideEncryption"] == "AES256"
assert additional_kwargs["SSEKMSKeyId"] == "native-key"


def test_fsspec_s3_rejects_unsupported_java_sse_type() -> None:
with pytest.raises(ValueError, match="Unsupported s3.sse.type: custom"):
FsspecFileIO(properties={"s3.sse.type": "custom"})


def test_fsspec_s3_session_properties_with_anonymous() -> None:
session_properties: Properties = {
"s3.anonymous": "true",
Expand Down
44 changes: 44 additions & 0 deletions tests/io/test_pyarrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,50 @@ def test_pyarrow_s3_allow_delayed_open_requires_supported_pyarrow_version() -> N
PyArrowFileIO(properties={S3_ALLOW_DELAYED_OPEN: "true"}).new_input(location=f"s3://warehouse/{uuid.uuid4()}")


def test_pyarrow_s3_accepts_java_client_properties() -> None:
session_properties: Properties = {
"s3.region": "us-east-1",
"s3.path-style-access": "true",
"http-client.proxy-endpoint": "http://proxy:8080",
"http-client.connection-timeout-ms": "1500",
"http-client.socket-timeout-ms": "2500",
}

with patch("pyarrow.fs.S3FileSystem") as mock_s3fs, patch("pyarrow.fs.resolve_s3_region") as mock_s3_region_resolver:
mock_s3_region_resolver.side_effect = OSError("S3 bucket is not found")
PyArrowFileIO(properties=session_properties).new_input(location=f"s3://warehouse/{uuid.uuid4()}")

client_kwargs = mock_s3fs.call_args.kwargs
assert client_kwargs["force_virtual_addressing"] is False
assert client_kwargs["proxy_options"] == "http://proxy:8080"
assert client_kwargs["connect_timeout"] == 1.5
assert client_kwargs["request_timeout"] == 2.5


def test_pyarrow_s3_native_properties_take_precedence_over_java_properties() -> None:
session_properties: Properties = {
"s3.region": "us-east-1",
"s3.path-style-access": "true",
"s3.force-virtual-addressing": "true",
"http-client.proxy-endpoint": "http://java-proxy:8080",
"s3.proxy-uri": "http://native-proxy:8080",
"http-client.connection-timeout-ms": "1500",
"s3.connect-timeout": "2",
"http-client.socket-timeout-ms": "2500",
"s3.request-timeout": "3",
}

with patch("pyarrow.fs.S3FileSystem") as mock_s3fs, patch("pyarrow.fs.resolve_s3_region") as mock_s3_region_resolver:
mock_s3_region_resolver.side_effect = OSError("S3 bucket is not found")
PyArrowFileIO(properties=session_properties).new_input(location=f"s3://warehouse/{uuid.uuid4()}")

client_kwargs = mock_s3fs.call_args.kwargs
assert client_kwargs["force_virtual_addressing"] is True
assert client_kwargs["proxy_options"] == "http://native-proxy:8080"
assert client_kwargs["connect_timeout"] == 2.0
assert client_kwargs["request_timeout"] == 3.0


def test_pyarrow_s3_session_properties_with_anonymous() -> None:
session_properties: Properties = {
"s3.anonymous": "true",
Expand Down
Loading