This is an automated email from the ASF dual-hosted git repository.
JingsongLi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/paimon.git
The following commit(s) were added to refs/heads/master by this push:
new 2295897e8b [python] Discover DLF token files from DLF_TOKEN_PATH
(#11088)
2295897e8b is described below
commit 2295897e8b068e9a212b707cbe19d3c9b724ffcb
Author: Zouxxyy <[email protected]>
AuthorDate: Wed Nov 11 13:33:09 2026 +0800
[python] Discover DLF token files from DLF_TOKEN_PATH (#11088)
---
docs/docs/pypaimon/catalogs.mdx | 6 +++
paimon-python/pypaimon/api/token_loader.py | 9 +++-
.../pypaimon/tests/rest/token_loader_test.py | 51 ++++++++++++++++++++++
3 files changed, 65 insertions(+), 1 deletion(-)
diff --git a/docs/docs/pypaimon/catalogs.mdx b/docs/docs/pypaimon/catalogs.mdx
index 17f8f354b4..78937c5ec7 100644
--- a/docs/docs/pypaimon/catalogs.mdx
+++ b/docs/docs/pypaimon/catalogs.mdx
@@ -162,6 +162,12 @@ catalog_options = {
catalog = CatalogFactory.create(catalog_options)
```
+For DLF authentication (`token.provider='dlf'`), PyPaimon can discover a JSON
token
+file through the `DLF_TOKEN_PATH` environment variable. Explicit token loaders,
+token paths, and inline AK/STS credentials take precedence over this
environment
+variable. The discovered file uses the existing local-file loader, including
+credential refresh according to its `Expiration` field.
+
</TabItem>
</Tabs>
diff --git a/paimon-python/pypaimon/api/token_loader.py
b/paimon-python/pypaimon/api/token_loader.py
index 8f2affd220..19d07a0a8b 100644
--- a/paimon-python/pypaimon/api/token_loader.py
+++ b/paimon-python/pypaimon/api/token_loader.py
@@ -15,6 +15,7 @@
# specific language governing permissions and limitations
# under the License.
+import os
import time
from abc import ABC, abstractmethod
from dataclasses import dataclass
@@ -271,4 +272,10 @@ class DLFTokenLoaderFactory:
token_path = options.get(CatalogOptions.DLF_TOKEN_PATH)
if token_path is not None:
return DLFLocalFileTokenLoader(token_path)
- return None
+ if any(options.get(key) is not None for key in (
+ CatalogOptions.DLF_ACCESS_KEY_ID,
+ CatalogOptions.DLF_ACCESS_KEY_SECRET,
+ CatalogOptions.DLF_ACCESS_SECURITY_TOKEN)):
+ return None
+ token_path = os.environ.get('DLF_TOKEN_PATH')
+ return DLFLocalFileTokenLoader(token_path) if token_path else None
diff --git a/paimon-python/pypaimon/tests/rest/token_loader_test.py
b/paimon-python/pypaimon/tests/rest/token_loader_test.py
index 23a9d18874..ff80c1bb38 100644
--- a/paimon-python/pypaimon/tests/rest/token_loader_test.py
+++ b/paimon-python/pypaimon/tests/rest/token_loader_test.py
@@ -16,9 +16,11 @@
# under the License.
import json
+import os
import tempfile
import unittest
from pathlib import Path
+from unittest.mock import patch
from pypaimon.api.auth.factory import AuthProviderFactory
from pypaimon.api.token_loader import (
@@ -92,6 +94,54 @@ class DLFLocalFileTokenLoaderTest(unittest.TestCase):
self.assertIsInstance(provider.token_loader,
DLFLocalFileTokenLoader)
self.assertEqual("access-key-id",
provider.get_token().access_key_id)
+ def test_auth_provider_uses_token_path_from_environment(self):
+ first_token = DLFToken("first-ak", "first-sk", "first-sts",
"2000-01-01T00:00:00Z")
+ fresh_token = DLFToken("fresh-ak", "fresh-sk", "fresh-sts",
"2099-01-01T00:00:00Z")
+
+ with tempfile.TemporaryDirectory() as temp_dir:
+ token_path = Path(temp_dir) / "token.json"
+ token_path.write_text(JSON.to_json(first_token), encoding="utf-8")
+ options = Options({
+ CatalogOptions.URI.key():
"https://cn-hangzhou-vpc.dlf.aliyuncs.com",
+ CatalogOptions.TOKEN_PROVIDER.key(): "dlf",
+ })
+ with patch.dict(os.environ, {"DLF_TOKEN_PATH": str(token_path)}):
+ provider = AuthProviderFactory.create_auth_provider(options)
+
+ self.assertIsInstance(provider.token_loader,
DLFLocalFileTokenLoader)
+ self.assertEqual(str(token_path),
provider.token_loader.description())
+ self.assertIsNone(options.get(CatalogOptions.DLF_TOKEN_PATH))
+ self.assertEqual("first-ak", provider.get_token().access_key_id)
+ token_path.write_text(JSON.to_json(fresh_token), encoding="utf-8")
+ self.assertEqual("fresh-ak", provider.get_token().access_key_id)
+
+ def test_explicit_credentials_take_precedence_over_environment(self):
+ with patch.dict(os.environ, {"DLF_TOKEN_PATH": "unused-token.json"}):
+ options = Options({
+ CatalogOptions.URI.key():
"https://cn-hangzhou-vpc.dlf.aliyuncs.com",
+ CatalogOptions.TOKEN_PROVIDER.key(): "dlf",
+ CatalogOptions.DLF_ACCESS_KEY_ID.key(): "explicit-ak",
+ CatalogOptions.DLF_ACCESS_KEY_SECRET.key(): "explicit-sk",
+ CatalogOptions.DLF_ACCESS_SECURITY_TOKEN.key(): "explicit-sts",
+ })
+ provider = AuthProviderFactory.create_auth_provider(options)
+ self.assertIsNone(provider.token_loader)
+ self.assertEqual("explicit-ak", provider.get_token().access_key_id)
+ self.assertEqual("explicit-sts",
provider.get_token().security_token)
+
+ for key in (CatalogOptions.DLF_ACCESS_KEY_ID,
+ CatalogOptions.DLF_ACCESS_KEY_SECRET,
+ CatalogOptions.DLF_ACCESS_SECURITY_TOKEN):
+ with self.subTest(key=key.key()):
+
self.assertIsNone(DLFTokenLoaderFactory.create_token_loader(
+ Options({key.key(): "explicit-value"})))
+
+ def test_missing_or_empty_environment_does_not_select_a_loader(self):
+ for environment in ({}, {"DLF_TOKEN_PATH": ""}):
+ with self.subTest(environment=environment), patch.dict(os.environ,
environment, clear=True):
+
self.assertIsNone(DLFTokenLoaderFactory.create_token_loader(Options({})))
+
+ @patch.dict(os.environ, {"DLF_TOKEN_PATH": "unused-token.json"})
def test_token_loader_takes_precedence_over_static_credentials(self):
file_token = DLFToken("file-ak", "file-sk", "file-sts")
@@ -120,6 +170,7 @@ class DLFLocalFileTokenLoaderTest(unittest.TestCase):
self.assertEqual("file-ak",
provider.get_token().access_key_id)
+ @patch.dict(os.environ, {"DLF_TOKEN_PATH": "unused-token.json"})
def
test_unknown_token_loader_does_not_fallback_to_static_credentials(self):
options = Options({
CatalogOptions.URI.key():