Skip to content

Commit abfb8b9

Browse files
JacksonTianyndu13
authored andcommitted
rename AuthUtils.getPrivateKey() to AuthUtils.readFile()
1 parent 989bbc8 commit abfb8b9

File tree

5 files changed

+12
-23
lines changed

5 files changed

+12
-23
lines changed

aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/OIDCCredentialsProvider.java

+4-18
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.aliyuncs.http.HttpResponse;
1818
import com.aliyuncs.http.MethodType;
1919
import com.aliyuncs.http.clients.CompatibleUrlConnClient;
20+
import com.aliyuncs.utils.AuthUtils;
2021
import com.aliyuncs.utils.ParameterHelper;
2122
import com.aliyuncs.utils.StringUtils;
2223
import com.google.gson.Gson;
@@ -147,24 +148,9 @@ private String invokeAssumeRoleWithOIDC() throws ClientException {
147148
httpRequest.setHttpContentType(FormatType.FORM);
148149
httpRequest.setSysConnectTimeout(1000);
149150
httpRequest.setSysReadTimeout(3000);
150-
String oidcToken;
151-
FileInputStream in = null;
152-
byte[] buffer;
153-
try {
154-
in = new FileInputStream(oidcTokenFilePath);
155-
buffer = new byte[in.available()];
156-
in.read(buffer);
157-
oidcToken = new String(buffer, "UTF-8");
158-
} catch (IOException e) {
159-
throw new ClientException("Read OIDC token failed " + e.toString());
160-
} finally {
161-
if (in != null) {
162-
try {
163-
in.close();
164-
} catch (IOException e) {
165-
e.printStackTrace();
166-
}
167-
}
151+
String oidcToken = AuthUtils.readFile(oidcTokenFilePath);
152+
if (oidcToken == null) {
153+
throw new ClientException("Read OIDC token failed");
168154
}
169155

170156
Map<String, String> body = new HashMap<String, String>();

aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/ProfileCredentialsProvider.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private AlibabaCloudCredentials getSTSGetSessionAccessKeyCredentials(Map<String,
117117
if (StringUtils.isEmpty(privateKeyFile)) {
118118
throw new ClientException("The configured private_key_file is empty");
119119
}
120-
String privateKey = AuthUtils.getPrivateKey(privateKeyFile);
120+
String privateKey = AuthUtils.readFile(privateKeyFile);
121121
if (StringUtils.isEmpty(publicKeyId) || StringUtils.isEmpty(privateKey)) {
122122
throw new ClientException("The configured public_key_id or private_key_file content is empty");
123123
}

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ public class AuthUtils {
1111
private static volatile String environmentECSMetaData;
1212
private static volatile String environmentCredentialsFile;
1313

14-
public static String getPrivateKey(String filePath) {
14+
15+
public static String readFile(String filePath) {
1516
FileInputStream in = null;
1617
byte[] buffer;
1718
try {
@@ -33,6 +34,11 @@ public static String getPrivateKey(String filePath) {
3334
}
3435
}
3536

37+
@Deprecated
38+
public static String getPrivateKey(String filePath) {
39+
return readFile(filePath);
40+
}
41+
3642
@Deprecated
3743
public static void setPrivateKey(String key) {
3844
}

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

-2
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,13 @@ public void getSTSGetSessionAccessKeyCredentialsTest() throws NoSuchMethodExcep
135135
e.getCause().getLocalizedMessage());
136136
}
137137
client.put(AuthConstant.INI_PRIVATE_KEY_FILE, "sads");
138-
AuthUtils.setPrivateKey("test");
139138
try {
140139
createCredential.invoke(provider, client, factory);
141140
Assert.fail();
142141
} catch (Exception e) {
143142
Assert.assertEquals("The configured public_key_id or private_key_file content is empty",
144143
e.getCause().getLocalizedMessage());
145144
}
146-
AuthUtils.setPrivateKey(null);
147145
}
148146

149147

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

-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,5 @@ public void getPrivateKeyTest(){
1111
String path = AuthUtils.class.getClassLoader().getResource("project.properties").getPath();
1212
String privateKey = AuthUtils.getPrivateKey(path);
1313
Assert.assertNotNull(privateKey);
14-
Assert.assertEquals(privateKey , AuthUtils.getPrivateKey(path));
1514
}
1615
}

0 commit comments

Comments
 (0)