Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
<version>3.12.13</version>
</dependency>
<dependency>
<groupId>org.ini4j</groupId>
<artifactId>ini4j</artifactId>
<version>0.5.4</version>
<groupId>org.apache.commons</groupId>
<artifactId>commons-configuration2</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import org.ini4j.Wini;
import org.apache.commons.configuration2.INIConfiguration;
import org.apache.commons.configuration2.ex.ConfigurationException;

import java.io.File;
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 Wini ini;
private static INIConfiguration ini;

private static Wini getIni() throws TencentCloudSDKException {
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";
Expand All @@ -24,18 +25,19 @@ private static Wini getIni() throws TencentCloudSDKException {
throw new TencentCloudSDKException("Not found file");
}
try {
ini = new Wini(new File(fileName));
} catch (IOException e) {
throw new TencentCloudSDKException("IOException");
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 {
Wini ini = getIni();
String secretId = ini.get("default", "secret_id");
String secretKey = ini.get("default", "secret_key");
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");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.tencentcloudapi.integration.common.provider;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.provider.ProfileCredentialsProvider;
import org.junit.Test;
import java.nio.file.Path;
import java.nio.file.Files;

import static org.junit.Assert.*;

public class ProfileCredentialsProviderTest {

@Test
public void testGetCredentials() throws Exception {
// 创建临时目录模拟用户主目录
Path tempHomeDir = Files.createTempDirectory("tencentcloud-test-home");
Path credentialsDir = tempHomeDir.resolve(".tencentcloud");
Files.createDirectories(credentialsDir);
Path credentialsFile = credentialsDir.resolve("credentials");

// 保存原始user.home属性
String originalUserHome = System.getProperty("user.home");

try {
// 设置临时目录为用户主目录
System.setProperty("user.home", tempHomeDir.toString());

// 写入配置文件内容
String configContent = "[default]\n" +
"secret_id = secret_id_test\n" +
"secret_key = secret_key_test";
Files.write(credentialsFile, configContent.getBytes());

// 测试ProfileCredentialsProvider是否能正确读取
ProfileCredentialsProvider provider = new ProfileCredentialsProvider();
Credential cred = provider.getCredentials();

// 验证读取的凭据是否正确
assertEquals("secret_id_test", cred.getSecretId());
assertEquals("secret_key_test", cred.getSecretKey());

} finally {
// 恢复原始user.home属性
if (originalUserHome != null) {
System.setProperty("user.home", originalUserHome);
}

// 清理临时文件
Files.deleteIfExists(credentialsFile);
Files.deleteIfExists(credentialsDir);
Files.deleteIfExists(tempHomeDir);
}
}
}