Skip to content

Commit 5954550

Browse files
committed
add custom auth implementation parsing
1 parent 8fbec86 commit 5954550

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

pyiceberg/catalog/rest/__init__.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ class IdentifierKind(Enum):
134134
SIGV4_SERVICE = "rest.signing-name"
135135
OAUTH2_SERVER_URI = "oauth2-server-uri"
136136
SNAPSHOT_LOADING_MODE = "snapshot-loading-mode"
137+
AUTH = "auth"
137138

138139
NAMESPACE_SEPARATOR = b"\x1f".decode(UTF8)
139140

@@ -247,7 +248,19 @@ def _create_session(self) -> Session:
247248
elif ssl_client_cert := ssl_client.get(CERT):
248249
session.cert = ssl_client_cert
249250

250-
session.auth = AuthManagerAdapter(self._create_legacy_oauth2_auth_manager(session))
251+
if auth_config := self.properties.get(AUTH):
252+
# set up auth_manager based on the properties
253+
auth_type = auth_config.get("type")
254+
if auth_type is None:
255+
raise ValueError("auth.type must be defined")
256+
auth_type_config = auth_config.get(auth_type, {})
257+
if auth_impl := auth_config.get("impl"):
258+
session.auth = AuthManagerAdapter(AuthManagerFactory.create(auth_impl, auth_type_config))
259+
else:
260+
session.auth = AuthManagerAdapter(AuthManagerFactory.create(auth_type, auth_type_config))
261+
else:
262+
session.auth = AuthManagerAdapter(self._create_legacy_oauth2_auth_manager(session))
263+
251264
# Set HTTP headers
252265
self._config_headers(session)
253266

tests/catalog/test_rest.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,6 +1519,42 @@ def test_request_session_with_ssl_client_cert() -> None:
15191519
assert "Could not find the TLS certificate file, invalid path: path_to_client_cert" in str(e.value)
15201520

15211521

1522+
def test_rest_catalog_with_basic_auth_type() -> None:
1523+
# Given
1524+
catalog_properties = {
1525+
"uri": TEST_URI,
1526+
"auth": {
1527+
"type": "basic",
1528+
"basic": {
1529+
"username": "one",
1530+
},
1531+
},
1532+
}
1533+
with pytest.raises(TypeError) as e:
1534+
# Missing namespace
1535+
RestCatalog("rest", **catalog_properties) # type: ignore
1536+
assert "BasicAuthManager.__init__() missing 1 required positional argument: 'password'" in str(e.value)
1537+
1538+
1539+
def test_rest_catalog_with_auth_impl() -> None:
1540+
# Given
1541+
catalog_properties = {
1542+
"uri": TEST_URI,
1543+
"auth": {
1544+
"type": "custom",
1545+
"impl": "dummy.nonexistent.package",
1546+
"custom": {
1547+
"property1": "one",
1548+
"property2": "two",
1549+
},
1550+
},
1551+
}
1552+
with pytest.raises(ValueError) as e:
1553+
# Missing namespace
1554+
RestCatalog("rest", **catalog_properties) # type: ignore
1555+
assert "Could not load AuthManager class for 'dummy.nonexistent.package'" in str(e.value)
1556+
1557+
15221558
EXAMPLE_ENV = {"PYICEBERG_CATALOG__PRODUCTION__URI": TEST_URI}
15231559

15241560

0 commit comments

Comments
 (0)