Skip to content

Commit

Permalink
refine AuthUtils.getPrivateKey()
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Jul 23, 2024
1 parent f8cf820 commit 5613819
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,31 @@ public class AuthUtils {
private static volatile String environmentAccesskeySecret;
private static volatile String environmentECSMetaData;
private static volatile String environmentCredentialsFile;
private static volatile String privateKey;

public static String getPrivateKey(String filePath) {
if (null == privateKey) {
synchronized (AuthUtils.class) {
if (null == privateKey) {
FileInputStream in = null;
byte[] buffer;
try {
in = new FileInputStream(filePath);
buffer = new byte[in.available()];
in.read(buffer);
privateKey = new String(buffer, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileInputStream in = null;
byte[] buffer;
try {
in = new FileInputStream(filePath);
buffer = new byte[in.available()];
in.read(buffer);
return new String(buffer, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return privateKey;
}

@Deprecated
public static void setPrivateKey(String key) {
privateKey = key;
}

public static String getClientType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,13 @@ public void createCredentialsProviderTest() throws
client.put(AuthConstant.INI_TYPE, AuthConstant.INI_TYPE_KEY_PAIR);
client.put(AuthConstant.INI_PUBLIC_KEY_ID, AuthConstant.INI_TYPE_KEY_PAIR);
client.put(AuthConstant.INI_PRIVATE_KEY, AuthConstant.INI_TYPE_KEY_PAIR);
client.put(AuthConstant.INI_PRIVATE_KEY_FILE, AuthConstant.INI_TYPE_KEY_PAIR);
AuthUtils.setPrivateKey("test");
client.put(AuthConstant.INI_PRIVATE_KEY_FILE, ProfileCredentialsProviderTest.class.getClassLoader().getResource("test").getPath());
STSGetSessionAccessKeyCredentialsProvider stsGetSessionAccessKeyCredentialsProvider =
Mockito.mock(STSGetSessionAccessKeyCredentialsProvider.class);
Mockito.when(stsGetSessionAccessKeyCredentialsProvider.getCredentials()).thenReturn(null);
Mockito.when(factory.createCredentialsProvider(Mockito.any(STSGetSessionAccessKeyCredentialsProvider.class))).
thenReturn(stsGetSessionAccessKeyCredentialsProvider);
Assert.assertNull(createCredential.invoke(profileCredentialsProvider, client, factory));
AuthUtils.setPrivateKey(null);

client.clear();
client.put(AuthConstant.INI_TYPE, AuthConstant.INI_TYPE_RAM);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public class AuthUtilsTest {
@Test
public void getPrivateKeyTest(){
new AuthUtils();
AuthUtils.setPrivateKey(null);
String path = AuthUtils.class.getClassLoader().getResource("project.properties").getPath();
String privateKey = AuthUtils.getPrivateKey(path);
Assert.assertNotNull(privateKey);
Expand Down
1 change: 1 addition & 0 deletions aliyun-java-sdk-core/src/test/resources/test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
private key content

0 comments on commit 5613819

Please sign in to comment.