Skip to content

Commit 0fe4514

Browse files
ssy352959096JacksonTian
authored andcommitted
InvalidAccessKeySecret and signaturenotmatch errors when the response status code is 400
1 parent bfb187f commit 0fe4514

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

aliyun-java-sdk-core/src/main/java/com/aliyuncs/DefaultAcsClient.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
import com.aliyuncs.exceptions.ServerException;
2525
import com.aliyuncs.http.FormatType;
2626
import com.aliyuncs.http.HttpClientFactory;
27-
import com.aliyuncs.http.HttpUtil;
2827
import com.aliyuncs.http.HttpRequest;
2928
import com.aliyuncs.http.HttpResponse;
29+
import com.aliyuncs.http.HttpUtil;
3030
import com.aliyuncs.http.IHttpClient;
3131
import com.aliyuncs.http.UserAgentConfig;
3232
import com.aliyuncs.profile.DefaultProfile;
@@ -193,7 +193,8 @@ private <T extends AcsResponse> T parseAcsResponse(AcsRequest<T> request, HttpRe
193193
AcsError error = readError(baseResponse, format);
194194
if (500 <= baseResponse.getStatus()) {
195195
throw new ServerException(error.getErrorCode(), error.getErrorMessage(), error.getRequestId());
196-
} else if ("IncompleteSignature".equals(error.getErrorCode())) {
196+
} else if (400 == baseResponse.getStatus() && ("IncompleteSignature".equals(error.getErrorCode())
197+
|| "SignatureDoesNotMatch".equals(error.getErrorCode()))) {
197198
String errorMessage = error.getErrorMessage();
198199
Pattern startPattern = Pattern.compile(SIGNATURE_BEGIN);
199200
Matcher startMatcher = startPattern.matcher(errorMessage);

aliyun-java-sdk-core/src/test/java/com/aliyuncs/DefaultAcsClientTest.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ public void testGetCommonResponseClientException() throws ClientException, IOExc
475475
Mockito.when(response.isSuccess()).thenReturn(false);
476476
Mockito.when(response.getHttpContentType()).thenReturn(FormatType.XML);
477477
Mockito.when(response.getHttpContentString()).thenReturn(makeAcsErrorXML("", "", "500", "ServerException", ""));
478-
Mockito.when(response.getStatus()).thenReturn(401);
478+
Mockito.when(response.getStatus()).thenReturn(404);
479479
thrown.expect(ClientException.class);
480480
spyClient.getCommonResponse(commonRequest);
481481
}
@@ -516,7 +516,7 @@ public void testResponseNotIncompleteSignatureError() throws ClientException {
516516
DefaultAcsClient client = new DefaultAcsClient(profile);
517517
DefaultAcsClient spyClient = Mockito.spy(client);
518518
HttpResponse response = Mockito.mock(HttpResponse.class);
519-
Mockito.when(response.getStatus()).thenReturn(401);
519+
Mockito.when(response.getStatus()).thenReturn(400);
520520
Mockito.when(response.getHttpContentType()).thenReturn(FormatType.XML);
521521
Mockito.when(response.getHttpContentString()).thenReturn(makeAcsErrorXML("", "", "Not IncompleteSignature",
522522
"ClientException", ""));
@@ -537,7 +537,7 @@ public void testResponseInvalidAccessKeySecretError() throws ClientException {
537537
DefaultAcsClient client = new DefaultAcsClient(profile);
538538
DefaultAcsClient spyClient = Mockito.spy(client);
539539
HttpResponse response = Mockito.mock(HttpResponse.class);
540-
Mockito.when(response.getStatus()).thenReturn(401);
540+
Mockito.when(response.getStatus()).thenReturn(400);
541541
Mockito.when(response.getHttpContentType()).thenReturn(FormatType.XML);
542542
AcsRequest request = Mockito.mock(AcsRequest.class);
543543
request.strToSign = "GET&%2F%3DDescriat%3DXML%26";
@@ -560,7 +560,7 @@ public void testResponseSignatureNullError() throws ClientException {
560560
DefaultAcsClient client = new DefaultAcsClient(profile);
561561
DefaultAcsClient spyClient = Mockito.spy(client);
562562
HttpResponse response = Mockito.mock(HttpResponse.class);
563-
Mockito.when(response.getStatus()).thenReturn(401);
563+
Mockito.when(response.getStatus()).thenReturn(400);
564564
Mockito.when(response.getHttpContentType()).thenReturn(FormatType.XML);
565565
AcsRequest request = Mockito.mock(AcsRequest.class);
566566
request.strToSign = "GET&%2F&scribeInstancesDXML%26";
@@ -583,7 +583,7 @@ public void testResponseSignatureError() throws ClientException {
583583
DefaultAcsClient client = new DefaultAcsClient(profile);
584584
HttpResponse response = Mockito.mock(HttpResponse.class);
585585
DefaultAcsClient spyClient = Mockito.spy(client);
586-
Mockito.when(response.getStatus()).thenReturn(401);
586+
Mockito.when(response.getStatus()).thenReturn(400);
587587
Mockito.when(response.getHttpContentType()).thenReturn(FormatType.XML);
588588
AcsRequest request = Mockito.mock(AcsRequest.class);
589589
request.strToSign = "GET&%2F&Action%3DDescribeInances%26Format%3DXML%26";
@@ -596,6 +596,28 @@ public void testResponseSignatureError() throws ClientException {
596596
spyClient.getAcsResponse(request);
597597
}
598598

599+
@Test
600+
public void testResponseSignatureDoesNotMatch() throws ClientException {
601+
Credential credential = Mockito.mock(Credential.class);
602+
Mockito.when(credential.getSecurityToken()).thenReturn(null);
603+
DefaultProfile profile = Mockito.mock(DefaultProfile.class);
604+
Mockito.when(profile.getCredential()).thenReturn(credential);
605+
DefaultAcsClient client = new DefaultAcsClient(profile);
606+
HttpResponse response = Mockito.mock(HttpResponse.class);
607+
DefaultAcsClient spyClient = Mockito.spy(client);
608+
Mockito.when(response.getStatus()).thenReturn(400);
609+
Mockito.when(response.getHttpContentType()).thenReturn(FormatType.XML);
610+
AcsRequest request = Mockito.mock(AcsRequest.class);
611+
request.strToSign = "GET&%2F&Action%3DDescribeInances%26Format%3DXML%26";
612+
String errorMessage = "signature does not conform to standards. server string to sign is:Error Signature";
613+
Mockito.when(response.getHttpContentString()).thenReturn(makeAcsErrorXML("", "", "SignatureDoesNotMatch",
614+
errorMessage, ""));
615+
Mockito.doReturn(response).when(spyClient).doAction(request);
616+
thrown.expect(ClientException.class);
617+
thrown.expectMessage("SignatureDoesNotMatch : " + errorMessage);
618+
spyClient.getAcsResponse(request);
619+
}
620+
599621
@SuppressWarnings({ "unchecked", "rawtypes", "deprecation" })
600622
@Test
601623
public void testResponseStringContentNull() throws ClientException {
@@ -606,7 +628,7 @@ public void testResponseStringContentNull() throws ClientException {
606628
DefaultAcsClient client = new DefaultAcsClient(profile);
607629
HttpResponse response = Mockito.mock(HttpResponse.class);
608630
DefaultAcsClient spyClient = Mockito.spy(client);
609-
Mockito.when(response.getStatus()).thenReturn(401);
631+
Mockito.when(response.getStatus()).thenReturn(400);
610632
Mockito.when(response.getHttpContentType()).thenReturn(FormatType.XML);
611633
AcsRequest request = Mockito.mock(AcsRequest.class);
612634
request.strToSign = "GET&%2F&ssddddfgfK";

java-sdk-function-test/src/test/java/com/aliyuncs/NewEndpointTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ public void testInvalidAccessKeySecret() {
332332
resolve("cn-hangzhou", "Ecs", "ecs", "innerAPI");
333333
Assert.fail();
334334
} catch (ClientException e) {
335-
Assert.assertEquals("SignatureDoesNotMatch", e.getErrCode());
335+
Assert.assertEquals("SDK.InvalidAccessKeySecret", e.getErrCode());
336336
}
337337
}
338338

0 commit comments

Comments
 (0)