Skip to content

Commit 989bbc8

Browse files
JacksonTianyndu13
authored andcommitted
refine AuthUtils.getPrivateKey()
1 parent f8cf820 commit 989bbc8

File tree

4 files changed

+19
-28
lines changed

4 files changed

+19
-28
lines changed

aliyun-java-sdk-core/src/main/java/com/aliyuncs/utils/AuthUtils.java

+17-24
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,31 @@ public class AuthUtils {
1010
private static volatile String environmentAccesskeySecret;
1111
private static volatile String environmentECSMetaData;
1212
private static volatile String environmentCredentialsFile;
13-
private static volatile String privateKey;
1413

1514
public static String getPrivateKey(String filePath) {
16-
if (null == privateKey) {
17-
synchronized (AuthUtils.class) {
18-
if (null == privateKey) {
19-
FileInputStream in = null;
20-
byte[] buffer;
21-
try {
22-
in = new FileInputStream(filePath);
23-
buffer = new byte[in.available()];
24-
in.read(buffer);
25-
privateKey = new String(buffer, "UTF-8");
26-
} catch (IOException e) {
27-
e.printStackTrace();
28-
} finally {
29-
if (in != null) {
30-
try {
31-
in.close();
32-
} catch (IOException e) {
33-
e.printStackTrace();
34-
}
35-
}
36-
}
15+
FileInputStream in = null;
16+
byte[] buffer;
17+
try {
18+
in = new FileInputStream(filePath);
19+
buffer = new byte[in.available()];
20+
in.read(buffer);
21+
return new String(buffer, "UTF-8");
22+
} catch (IOException e) {
23+
e.printStackTrace();
24+
return null;
25+
} finally {
26+
if (in != null) {
27+
try {
28+
in.close();
29+
} catch (IOException e) {
30+
e.printStackTrace();
3731
}
3832
}
3933
}
40-
return privateKey;
4134
}
4235

36+
@Deprecated
4337
public static void setPrivateKey(String key) {
44-
privateKey = key;
4538
}
4639

4740
public static String getClientType() {

aliyun-java-sdk-core/src/test/java/com/aliyuncs/auth/ProfileCredentialsProviderTest.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,13 @@ public void createCredentialsProviderTest() throws
174174
client.put(AuthConstant.INI_TYPE, AuthConstant.INI_TYPE_KEY_PAIR);
175175
client.put(AuthConstant.INI_PUBLIC_KEY_ID, AuthConstant.INI_TYPE_KEY_PAIR);
176176
client.put(AuthConstant.INI_PRIVATE_KEY, AuthConstant.INI_TYPE_KEY_PAIR);
177-
client.put(AuthConstant.INI_PRIVATE_KEY_FILE, AuthConstant.INI_TYPE_KEY_PAIR);
178-
AuthUtils.setPrivateKey("test");
177+
client.put(AuthConstant.INI_PRIVATE_KEY_FILE, ProfileCredentialsProviderTest.class.getClassLoader().getResource("test").getPath());
179178
STSGetSessionAccessKeyCredentialsProvider stsGetSessionAccessKeyCredentialsProvider =
180179
Mockito.mock(STSGetSessionAccessKeyCredentialsProvider.class);
181180
Mockito.when(stsGetSessionAccessKeyCredentialsProvider.getCredentials()).thenReturn(null);
182181
Mockito.when(factory.createCredentialsProvider(Mockito.any(STSGetSessionAccessKeyCredentialsProvider.class))).
183182
thenReturn(stsGetSessionAccessKeyCredentialsProvider);
184183
Assert.assertNull(createCredential.invoke(profileCredentialsProvider, client, factory));
185-
AuthUtils.setPrivateKey(null);
186184

187185
client.clear();
188186
client.put(AuthConstant.INI_TYPE, AuthConstant.INI_TYPE_RAM);

aliyun-java-sdk-core/src/test/java/com/aliyuncs/utils/AuthUtilsTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ public class AuthUtilsTest {
88
@Test
99
public void getPrivateKeyTest(){
1010
new AuthUtils();
11-
AuthUtils.setPrivateKey(null);
1211
String path = AuthUtils.class.getClassLoader().getResource("project.properties").getPath();
1312
String privateKey = AuthUtils.getPrivateKey(path);
1413
Assert.assertNotNull(privateKey);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
private key content

0 commit comments

Comments
 (0)