From 5613819d5515bb308cc20f8e9eecb45b4228de66 Mon Sep 17 00:00:00 2001 From: Jackson Tian Date: Tue, 23 Jul 2024 22:31:26 +0800 Subject: [PATCH] refine AuthUtils.getPrivateKey() --- .../java/com/aliyuncs/utils/AuthUtils.java | 41 ++++++++----------- .../auth/ProfileCredentialsProviderTest.java | 4 +- .../com/aliyuncs/utils/AuthUtilsTest.java | 1 - aliyun-java-sdk-core/src/test/resources/test | 1 + 4 files changed, 19 insertions(+), 28 deletions(-) create mode 100644 aliyun-java-sdk-core/src/test/resources/test diff --git a/aliyun-java-sdk-core/src/main/java/com/aliyuncs/utils/AuthUtils.java b/aliyun-java-sdk-core/src/main/java/com/aliyuncs/utils/AuthUtils.java index 236f1d4b0d..041804b2ef 100644 --- a/aliyun-java-sdk-core/src/main/java/com/aliyuncs/utils/AuthUtils.java +++ b/aliyun-java-sdk-core/src/main/java/com/aliyuncs/utils/AuthUtils.java @@ -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() { diff --git a/aliyun-java-sdk-core/src/test/java/com/aliyuncs/auth/ProfileCredentialsProviderTest.java b/aliyun-java-sdk-core/src/test/java/com/aliyuncs/auth/ProfileCredentialsProviderTest.java index 990c14aedd..61f5dbc637 100644 --- a/aliyun-java-sdk-core/src/test/java/com/aliyuncs/auth/ProfileCredentialsProviderTest.java +++ b/aliyun-java-sdk-core/src/test/java/com/aliyuncs/auth/ProfileCredentialsProviderTest.java @@ -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); diff --git a/aliyun-java-sdk-core/src/test/java/com/aliyuncs/utils/AuthUtilsTest.java b/aliyun-java-sdk-core/src/test/java/com/aliyuncs/utils/AuthUtilsTest.java index 73a255878f..a4d01928ea 100644 --- a/aliyun-java-sdk-core/src/test/java/com/aliyuncs/utils/AuthUtilsTest.java +++ b/aliyun-java-sdk-core/src/test/java/com/aliyuncs/utils/AuthUtilsTest.java @@ -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); diff --git a/aliyun-java-sdk-core/src/test/resources/test b/aliyun-java-sdk-core/src/test/resources/test new file mode 100644 index 0000000000..37bf0c5a3e --- /dev/null +++ b/aliyun-java-sdk-core/src/test/resources/test @@ -0,0 +1 @@ +private key content \ No newline at end of file