-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathProfileCredentialsProvider.java
More file actions
52 lines (47 loc) · 2.2 KB
/
ProfileCredentialsProvider.java
File metadata and controls
52 lines (47 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.tencentcloudapi.common.provider;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import org.apache.commons.configuration2.INIConfiguration;
import org.apache.commons.configuration2.ex.ConfigurationException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class ProfileCredentialsProvider implements CredentialsProvider {
private static INIConfiguration ini;
private static INIConfiguration getIni() throws TencentCloudSDKException {
String fileName;
if (Files.exists(Paths.get(System.getProperty("user.home") + "\\.tencentcloud\\credentials"))) {
fileName = System.getProperty("user.home") + "\\.tencentcloud\\credentials";
} else if (Files.exists(Paths.get("/etc/tencentcloud/credentials"))) {
fileName = "/etc/tencentcloud/credentials";
} else if (Files.exists(Paths.get(System.getProperty("user.home") + "/.tencentcloud/credentials"))) {
fileName = System.getProperty("user.home") + "/.tencentcloud/credentials";
} else {
throw new TencentCloudSDKException("Not found file");
}
try {
ini = new INIConfiguration();
ini.read(new FileReader(fileName));
} catch (IOException | ConfigurationException e) {
throw new TencentCloudSDKException("IOException or ConfigurationException");
}
return ini;
}
@Override
public Credential getCredentials() throws TencentCloudSDKException {
INIConfiguration ini = getIni();
String secretId = ini.getString("default.secret_id");
String secretKey = ini.getString("default.secret_key");
if (secretId == null || secretKey == null) {
throw new TencentCloudSDKException("Not found secretId or secretKey");
}
if (secretId.length() == 0) {
throw new TencentCloudSDKException("SecretId cannot be empty");
} else if (secretKey.length() == 0) {
throw new TencentCloudSDKException("SecretKey cannot be empty");
} else {
return new Credential(secretId, secretKey);
}
}
}