Skip to content

Commit be0d126

Browse files
committed
Add auth to ossfs initialization.
1. Add `auth` to allow ossfs directly use user provided any kinds of `Auth` 2. Add a new tests for `EcsRamRoleCredentialsProvider` auth fix #75
1 parent 1c4611d commit be0d126

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/ossfs/core.py

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import oss2
1313
from fsspec.spec import AbstractBufferedFile, AbstractFileSystem
1414
from fsspec.utils import stringify_path
15+
from oss2.auth import AnonymousAuth, AuthBase, StsAuth
1516

1617
from ossfs.exceptions import translate_boto_error
1718

@@ -52,6 +53,7 @@ def __init__(
5253
key: Optional[str] = None,
5354
secret: Optional[str] = None,
5455
token: Optional[str] = None,
56+
auth: Optional[Union[AuthBase, AnonymousAuth, StsAuth]] = None,
5557
default_cache_type: Optional[str] = "readahead",
5658
**kwargs, # pylint: disable=too-many-arguments
5759
):
@@ -71,6 +73,12 @@ def __init__(
7173
https://oss-me-east-1.aliyuncs.com
7274
"""
7375
super().__init__(**kwargs)
76+
if auth:
77+
if any([key, secret, token]):
78+
logger.warning(
79+
"Auth provided 'key', 'secret', and 'token' will be ignored"
80+
)
81+
self._auth = auth
7482
if token:
7583
self._auth = oss2.StsAuth(key, secret, token)
7684
elif key:

tests/test_login.py

+28-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import pytest
1212
from aliyunsdkcore import client
1313
from aliyunsdksts.request.v20150401 import AssumeRoleRequest
14+
from oss2.credentials import EcsRamRoleCredentialsProvider
1415

1516
from ossfs import OSSFileSystem
1617

@@ -25,7 +26,7 @@ def fetch_sts_token(access_key_id, access_key_secret, role_arn):
2526

2627
req.set_accept_format("json")
2728
req.set_RoleArn(role_arn)
28-
req.set_RoleSessionName("oss-python-sdk-example")
29+
req.set_RoleSessionName("ossfs-login-test")
2930

3031
body = clt.do_action_with_exception(req)
3132

@@ -73,3 +74,29 @@ def test_env_endpoint(endpoint, test_bucket_name, monkeypatch):
7374
def test_anonymous_login(file_in_anonymous, endpoint):
7475
ossfs = OSSFileSystem(endpoint=endpoint)
7576
ossfs.cat(f"{file_in_anonymous}")
77+
78+
79+
def test_auth_login(endpoint, test_bucket_name):
80+
key, secret, token = fetch_sts_token(STSAccessKeyId, STSAccessKeySecret, STSArn)
81+
auth = oss2.StsAuth(key, secret, token)
82+
ossfs = OSSFileSystem(
83+
auth=auth,
84+
endpoint=endpoint,
85+
)
86+
ossfs.ls(test_bucket_name)
87+
88+
89+
def test_ecs_ram_role_credential(endpoint, test_bucket_name, test_directory):
90+
key, secret, token = fetch_sts_token(STSAccessKeyId, STSAccessKeySecret, STSArn)
91+
auth = oss2.StsAuth(key, secret, token)
92+
bucket = oss2.Bucket(auth, endpoint, test_bucket_name)
93+
path = f"{test_directory}/test_ecs_ram_role_credential/file"
94+
bucket.put_object(path, "test ecs ram role credential")
95+
auth_server_host = bucket.sign_url("GET", path, 10)
96+
credentials_provider = EcsRamRoleCredentialsProvider(auth_server_host, 3, 10)
97+
provider_auth = oss2.ProviderAuth(credentials_provider)
98+
ossfs = OSSFileSystem(
99+
auth=provider_auth,
100+
endpoint=endpoint,
101+
)
102+
ossfs.ls(test_bucket_name)

0 commit comments

Comments
 (0)