|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.services.sqs; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; |
| 20 | + |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.Map; |
| 24 | +import org.junit.jupiter.api.AfterEach; |
| 25 | +import org.junit.jupiter.api.BeforeEach; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; |
| 28 | +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; |
| 29 | +import software.amazon.awssdk.http.AbortableInputStream; |
| 30 | +import software.amazon.awssdk.http.HttpExecuteResponse; |
| 31 | +import software.amazon.awssdk.http.SdkHttpResponse; |
| 32 | +import software.amazon.awssdk.services.sqs.internal.MessageMD5ChecksumInterceptor; |
| 33 | +import software.amazon.awssdk.services.sqs.model.MessageAttributeValue; |
| 34 | +import software.amazon.awssdk.services.sqs.model.SendMessageResponse; |
| 35 | +import software.amazon.awssdk.testutils.service.http.MockAsyncHttpClient; |
| 36 | +import software.amazon.awssdk.testutils.service.http.MockSyncHttpClient; |
| 37 | +import software.amazon.awssdk.utils.StringInputStream; |
| 38 | + |
| 39 | +/** |
| 40 | + * Verifies that the logic in {@link MessageMD5ChecksumInterceptor} can be disabled, which is needed for use cases like |
| 41 | + * FIPS cryptography libraries that don't have MD5 support. SendMessage is used as the test API, but the flow is the |
| 42 | + * same for ReceiveMessage and SendMessageBatch. |
| 43 | + */ |
| 44 | +public class MessageMD5ChecksumValidationDisableTest { |
| 45 | + private static final AwsBasicCredentials CLIENT_CREDENTIALS = AwsBasicCredentials.create("ca", "cs"); |
| 46 | + private static final String MESSAGE_ID = "0f433476-621e-4638-811a-112d2c2e41d7"; |
| 47 | + |
| 48 | + private MockAsyncHttpClient asyncHttpClient; |
| 49 | + private MockSyncHttpClient syncHttpClient; |
| 50 | + |
| 51 | + @BeforeEach |
| 52 | + public void setupClient() { |
| 53 | + asyncHttpClient = new MockAsyncHttpClient(); |
| 54 | + syncHttpClient = new MockSyncHttpClient(); |
| 55 | + } |
| 56 | + |
| 57 | + @AfterEach |
| 58 | + public void cleanup() { |
| 59 | + asyncHttpClient.reset(); |
| 60 | + syncHttpClient.reset(); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void md5ValidationEnabled_default_md5InResponse_Works() { |
| 65 | + asyncHttpClient.stubResponses(responseWithMd5()); |
| 66 | + SqsAsyncClient client = SqsAsyncClient.builder() |
| 67 | + .credentialsProvider(StaticCredentialsProvider.create(CLIENT_CREDENTIALS)) |
| 68 | + .httpClient(asyncHttpClient) |
| 69 | + .build(); |
| 70 | + |
| 71 | + SendMessageResponse sendMessageResponse = |
| 72 | + client.sendMessage(r -> r.messageBody(messageBody()).messageAttributes(createAttributeValues())).join(); |
| 73 | + |
| 74 | + assertThat(sendMessageResponse.messageId()).isEqualTo(MESSAGE_ID); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void md5ValidationEnabled_default_noMd5InResponse_throwsException() { |
| 79 | + asyncHttpClient.stubResponses(responseWithoutMd5()); |
| 80 | + SqsAsyncClient client = SqsAsyncClient.builder() |
| 81 | + .credentialsProvider(StaticCredentialsProvider.create(CLIENT_CREDENTIALS)) |
| 82 | + .httpClient(asyncHttpClient) |
| 83 | + .build(); |
| 84 | + |
| 85 | + assertThatThrownBy(() -> client.sendMessage(r -> r.messageBody(messageBody()) |
| 86 | + .messageAttributes(createAttributeValues())) |
| 87 | + .join()) |
| 88 | + .hasMessageContaining("MD5 returned by SQS does not match the calculation on the original request"); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void md5ValidationDisabled_md5InResponse_Works() { |
| 93 | + asyncHttpClient.stubResponses(responseWithMd5()); |
| 94 | + SqsAsyncClient client = SqsAsyncClient.builder() |
| 95 | + .credentialsProvider(StaticCredentialsProvider.create(CLIENT_CREDENTIALS)) |
| 96 | + .httpClient(asyncHttpClient) |
| 97 | + .checksumValidationEnabled(false) |
| 98 | + .build(); |
| 99 | + |
| 100 | + SendMessageResponse sendMessageResponse = |
| 101 | + client.sendMessage(r -> r.messageBody(messageBody()).messageAttributes(createAttributeValues())).join(); |
| 102 | + |
| 103 | + assertThat(sendMessageResponse.messageId()).isEqualTo(MESSAGE_ID); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void md5ValidationDisabled_noMd5InResponse_Works() { |
| 108 | + asyncHttpClient.stubResponses(responseWithoutMd5()); |
| 109 | + SqsAsyncClient client = SqsAsyncClient.builder() |
| 110 | + .credentialsProvider(StaticCredentialsProvider.create(CLIENT_CREDENTIALS)) |
| 111 | + .httpClient(asyncHttpClient) |
| 112 | + .checksumValidationEnabled(false) |
| 113 | + .build(); |
| 114 | + |
| 115 | + SendMessageResponse sendMessageResponse = |
| 116 | + client.sendMessage(r -> r.messageBody(messageBody()).messageAttributes(createAttributeValues())).join(); |
| 117 | + |
| 118 | + assertThat(sendMessageResponse.messageId()).isEqualTo(MESSAGE_ID); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void sync_md5ValidationDisabled_noMd5InResponse_Works() { |
| 123 | + syncHttpClient.stubResponses(responseWithoutMd5()); |
| 124 | + SqsClient client = SqsClient.builder() |
| 125 | + .credentialsProvider(StaticCredentialsProvider.create(CLIENT_CREDENTIALS)) |
| 126 | + .httpClient(syncHttpClient) |
| 127 | + .checksumValidationEnabled(false) |
| 128 | + .build(); |
| 129 | + |
| 130 | + SendMessageResponse sendMessageResponse = |
| 131 | + client.sendMessage(r -> r.messageBody(messageBody()).messageAttributes(createAttributeValues())); |
| 132 | + |
| 133 | + assertThat(sendMessageResponse.messageId()).isEqualTo(MESSAGE_ID); |
| 134 | + } |
| 135 | + |
| 136 | + private static String messageBody() { |
| 137 | + return "Body"; |
| 138 | + } |
| 139 | + |
| 140 | + private static HttpExecuteResponse responseWithMd5() { |
| 141 | + return HttpExecuteResponse.builder().response(SdkHttpResponse.builder().statusCode(200).build()).responseBody( |
| 142 | + AbortableInputStream.create(new StringInputStream( |
| 143 | + "{\"MD5OfMessageAttributes\":\"43eeb333d10515533e317490584ea243\"," |
| 144 | + + "\"MD5OfMessageBody\":\"ac101b32dda4448cf13a93fe283dddd8\"," |
| 145 | + + "\"MessageId\":\"" + MESSAGE_ID + "\"} "))) |
| 146 | + .build(); |
| 147 | + } |
| 148 | + |
| 149 | + private static HttpExecuteResponse responseWithoutMd5() { |
| 150 | + return HttpExecuteResponse.builder().response(SdkHttpResponse.builder().statusCode(200).build()) |
| 151 | + .responseBody(AbortableInputStream |
| 152 | + .create(new StringInputStream("{\"MessageId\":\"" + MESSAGE_ID + "\"} "))) |
| 153 | + .build(); |
| 154 | + } |
| 155 | + |
| 156 | + protected static Map<String, MessageAttributeValue> createAttributeValues() { |
| 157 | + Map<String, MessageAttributeValue> attrs = new HashMap(); |
| 158 | + attrs.put("attribute-1", MessageAttributeValue.builder().dataType("String").stringValue("tmp").build()); |
| 159 | + return Collections.unmodifiableMap(attrs); |
| 160 | + } |
| 161 | +} |
0 commit comments