Skip to content

Commit e9ced05

Browse files
authored
[filesystem/oss] OssFileSystem should allow to configure credential provider (#939)
1 parent 91d8adb commit e9ced05

File tree

3 files changed

+105
-14
lines changed

3 files changed

+105
-14
lines changed

fluss-filesystems/fluss-fs-oss/src/main/java/com/alibaba/fluss/fs/oss/OSSFileSystemPlugin.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,19 @@ public FileSystem create(URI fsUri, Configuration flussConfig) throws IOExceptio
6868

6969
// set credential provider
7070
if (hadoopConfig.get(ACCESS_KEY_ID) == null) {
71-
LOG.info(
72-
"{} is not set, using credential provider {}.",
73-
ACCESS_KEY_ID,
74-
hadoopConfig.get(CREDENTIALS_PROVIDER_KEY));
75-
setCredentialProvider(flussConfig, hadoopConfig);
71+
String credentialsProvider = hadoopConfig.get(CREDENTIALS_PROVIDER_KEY);
72+
if (credentialsProvider != null) {
73+
LOG.info(
74+
"{} is not set, but {} is set, using credential provider {}.",
75+
ACCESS_KEY_ID,
76+
CREDENTIALS_PROVIDER_KEY,
77+
credentialsProvider);
78+
} else {
79+
// no ak, no credentialsProvider,
80+
// set default credential provider which will get token from
81+
// OSSSecurityTokenReceiver
82+
setDefaultCredentialProvider(flussConfig, hadoopConfig);
83+
}
7684
} else {
7785
LOG.info("{} is set, using provided access key id and secret.", ACCESS_KEY_ID);
7886
}
@@ -101,6 +109,12 @@ protected org.apache.hadoop.fs.FileSystem initFileSystem(
101109
return fileSystem;
102110
}
103111

112+
protected void setDefaultCredentialProvider(
113+
Configuration flussConfig, org.apache.hadoop.conf.Configuration hadoopConfig) {
114+
// use OSSSecurityTokenReceiver to update hadoop config to set credentialsProvider
115+
OSSSecurityTokenReceiver.updateHadoopConfig(hadoopConfig);
116+
}
117+
104118
@VisibleForTesting
105119
org.apache.hadoop.conf.Configuration getHadoopConfiguration(Configuration flussConfig) {
106120
org.apache.hadoop.conf.Configuration conf = new org.apache.hadoop.conf.Configuration();
@@ -127,11 +141,6 @@ org.apache.hadoop.conf.Configuration getHadoopConfiguration(Configuration flussC
127141
return conf;
128142
}
129143

130-
protected void setCredentialProvider(
131-
Configuration flussConfig, org.apache.hadoop.conf.Configuration hadoopConfig) {
132-
OSSSecurityTokenReceiver.updateHadoopConfig(hadoopConfig);
133-
}
134-
135144
private void setSignatureVersion4(
136145
AliyunOSSFileSystem aliyunOSSFileSystem,
137146
org.apache.hadoop.conf.Configuration hadoopConfig) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2025 Alibaba Group Holding Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.alibaba.fluss.fs.oss;
18+
19+
import com.alibaba.fluss.config.Configuration;
20+
import com.alibaba.fluss.fs.FileSystem;
21+
import com.alibaba.fluss.fs.FileSystemBehaviorTestSuite;
22+
import com.alibaba.fluss.fs.FsPath;
23+
24+
import com.aliyun.oss.common.auth.CredentialsProvider;
25+
import com.aliyun.oss.common.auth.SystemPropertiesCredentialsProvider;
26+
import org.junit.jupiter.api.BeforeAll;
27+
28+
import java.util.UUID;
29+
30+
import static org.apache.hadoop.fs.aliyun.oss.Constants.CREDENTIALS_PROVIDER_KEY;
31+
32+
/** IT case for access oss via set {@link CredentialsProvider}. */
33+
class OSSWithCredentialsProviderFileSystemBehaviorITCase extends FileSystemBehaviorTestSuite {
34+
35+
private static final String TEST_DATA_DIR = "tests-" + UUID.randomUUID();
36+
37+
@BeforeAll
38+
static void setup() {
39+
OSSTestCredentials.assumeCredentialsAvailable();
40+
41+
// use SystemPropertiesCredentialsProvider
42+
final Configuration conf = new Configuration();
43+
conf.setString(
44+
CREDENTIALS_PROVIDER_KEY,
45+
SystemPropertiesCredentialsProvider.class.getCanonicalName());
46+
conf.setString("fs.oss.endpoint", OSSTestCredentials.getOSSEndpoint());
47+
conf.setString("fs.oss.region", OSSTestCredentials.getOSSRegion());
48+
49+
// now, we need to set oss config to system properties
50+
System.setProperty("oss.accessKeyId", OSSTestCredentials.getOSSAccessKey());
51+
System.setProperty("oss.accessKeySecret", OSSTestCredentials.getOSSSecretKey());
52+
FileSystem.initialize(conf, null);
53+
}
54+
55+
@Override
56+
protected FileSystem getFileSystem() throws Exception {
57+
return getBasePath().getFileSystem();
58+
}
59+
60+
@Override
61+
protected FsPath getBasePath() throws Exception {
62+
return new FsPath(OSSTestCredentials.getTestBucketUri() + TEST_DATA_DIR);
63+
}
64+
}

website/docs/maintenance/filesystems/oss.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,34 @@ To enabled OSS as remote storage, there are some required configurations that mu
3535
remote.data.dir: oss://<your-bucket>/path/to/remote/storage
3636
# Aliyun OSS endpoint to connect to, such as: oss-cn-hangzhou.aliyuncs.com
3737
fs.oss.endpoint: <your-endpoint>
38+
# Aliyun STS endpoint to connect to obtain a STS token, such as: sts.cn-hangzhou.aliyuncs.com
39+
fs.oss.sts.endpoint: <your-sts-endpoint>
40+
# For the role of the STS token obtained from the STS endpoint, such as: acs:ram::123456789012:role/testrole
41+
fs.oss.roleArn: <your-role-arn>
42+
43+
# Authentication (choose one option below)
44+
45+
# Option 1: Direct credentials
3846
# Aliyun access key ID
3947
fs.oss.accessKeyId: <your-access-key>
4048
# Aliyun access key secret
4149
fs.oss.accessKeySecret: <your-secret-key>
4250

43-
# Aliyun STS endpoint to connect to obtain a STS token, such as: sts.cn-hangzhou.aliyuncs.com
44-
fs.oss.sts.endpoint: <your-sts-endpoint>
45-
# For the role of the STS token obtained from the STS endpoint, such as: acs:ram::123456789012:role/testrole
46-
fs.oss.roleArn: <your-role-arn>
51+
# Option 2: Secure credential provider
52+
fs.oss.credentials.provider: <your-credentials-provider>
53+
```
54+
To avoid exposing sensitive access key information directly in the `server.yaml`, you can choose option2 to use a credential provider by setting the `fs.oss.credentials.provider` property.
55+
56+
For example, to use environment variables for credential management:
57+
```yaml
58+
fs.oss.credentials.provider: com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider
59+
```
60+
Then, set the following environment variables before starting the Fluss service:
61+
```bash
62+
export OSS_ACCESS_KEY_ID=<your-access-key>
63+
export OSS_ACCESS_KEY_SECRET=<your-secret-key>
4764
```
65+
This approach enhances security by keeping sensitive credentials out of configuration files.
4866

4967
## Token-based Authentication
5068

0 commit comments

Comments
 (0)