-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathS3EncryptionClientGetObjectStreamCloseTest.java
More file actions
232 lines (203 loc) · 10.2 KB
/
Copy pathS3EncryptionClientGetObjectStreamCloseTest.java
File metadata and controls
232 lines (203 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package software.amazon.encryption.s3;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import javax.crypto.spec.SecretKeySpec;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.ResponseInputStream;
import software.amazon.awssdk.core.async.AsyncRequestBody;
import software.amazon.awssdk.core.async.SdkPublisher;
import software.amazon.awssdk.core.checksums.RequestChecksumCalculation;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.core.sync.ResponseTransformer;
import software.amazon.awssdk.http.SdkHttpFullResponse;
import software.amazon.awssdk.http.SdkHttpMethod;
import software.amazon.awssdk.http.async.AsyncExecuteRequest;
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3AsyncClient;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
/**
* Verifies that {@link S3EncryptionClient#getObject} closes the response stream unless the caller
* owns it, so the underlying HTTP connection is released.
* <p>
* The wrapped async client runs over an in-memory transport whose response body is delivered in
* chunks on demand, like a real connection. The object is larger than the blocking input stream
* buffers and delayed authentication streams plaintext, so a transformer that stops reading early
* leaves the body unconsumed; only closing the stream cancels it.
*/
public class S3EncryptionClientGetObjectStreamCloseTest {
private static final GetObjectRequest GET_REQUEST = GetObjectRequest.builder().bucket("bucket").key("key").build();
private final ExecutorService executor = Executors.newCachedThreadPool();
private final InMemoryTransport transport = new InMemoryTransport(executor);
private S3EncryptionClient client;
private byte[] plaintext;
@BeforeEach
public void setUp() {
StaticCredentialsProvider creds = StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"));
byte[] key = new byte[32];
new SecureRandom().nextBytes(key);
client = S3EncryptionClient.builderV4()
.wrappedClient(S3Client.builder().region(Region.US_WEST_2).credentialsProvider(creds).build())
.wrappedAsyncClient(S3AsyncClient.builder()
.region(Region.US_WEST_2)
.credentialsProvider(creds)
// Keeps the PUT body unencoded so the transport can store it as-is.
.requestChecksumCalculation(RequestChecksumCalculation.WHEN_REQUIRED)
.httpClient(transport)
.build())
.aesKey(new SecretKeySpec(key, "AES"))
.enableDelayedAuthenticationMode(true)
.build();
plaintext = new byte[8 * 1024 * 1024];
new SecureRandom().nextBytes(plaintext);
client.putObject(PutObjectRequest.builder().bucket("bucket").key("key").build(), RequestBody.fromBytes(plaintext));
}
@AfterEach
public void tearDown() {
client.close();
executor.shutdownNow();
}
@Test
public void transformerThatStopsEarlyReleasesResponseStream() {
int firstByte = client.getObject(GET_REQUEST, (response, inputStream) -> inputStream.read());
assertEquals(plaintext[0] & 0xFF, firstByte);
assertTrue(transport.awaitBodyCancelled(), "response stream was not released after getObject returned");
}
@Test
public void transformerThatThrowsReleasesResponseStream() {
assertThrows(S3EncryptionClientException.class, () -> client.getObject(GET_REQUEST, (response, inputStream) -> {
inputStream.read();
throw new IllegalStateException("transform failed");
}));
assertTrue(transport.awaitBodyCancelled(), "response stream was not released after transform threw");
}
@Test
public void streamingTransformerLeavesResponseStreamOpenForCaller() throws Exception {
try (ResponseInputStream<GetObjectResponse> stream = client.getObject(GET_REQUEST, ResponseTransformer.toInputStream())) {
assertEquals(plaintext[0] & 0xFF, stream.read());
assertFalse(transport.bodyCancelled.isDone(), "caller-owned stream was closed by getObject");
}
assertTrue(transport.awaitBodyCancelled(), "response stream was not released after the caller closed it");
}
/** Stores the object on PUT and serves it on GET, recording whether the GET body was cancelled. */
private static final class InMemoryTransport implements SdkAsyncHttpClient {
private final ExecutorService executor;
private final Map<String, String> metadata = new HashMap<>();
private byte[] body;
private volatile CompletableFuture<Void> bodyCancelled = new CompletableFuture<>();
InMemoryTransport(ExecutorService executor) {
this.executor = executor;
}
boolean awaitBodyCancelled() {
try {
bodyCancelled.get(5, TimeUnit.SECONDS);
return true;
} catch (Exception e) {
return false;
}
}
@Override
public CompletableFuture<Void> execute(AsyncExecuteRequest request) {
SdkHttpFullResponse.Builder response = SdkHttpFullResponse.builder().statusCode(200).putHeader("ETag", "\"etag\"");
if (request.request().method() == SdkHttpMethod.PUT) {
ByteArrayOutputStream received = new ByteArrayOutputStream();
return SdkPublisher.adapt(request.requestContentPublisher())
.subscribe(buffer -> {
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
received.write(bytes, 0, bytes.length);
})
.thenRun(() -> {
body = received.toByteArray();
// S3EC stores its encryption metadata in user metadata headers.
request.request().headers().forEach((name, values) -> {
if (name.toLowerCase().startsWith("x-amz-meta-")) {
metadata.put(name, values.get(0));
}
});
request.responseHandler().onHeaders(response.build());
request.responseHandler().onStream(AsyncRequestBody.empty());
});
}
metadata.forEach(response::putHeader);
response.putHeader("Content-Length", String.valueOf(body.length));
bodyCancelled = new CompletableFuture<>();
CompletableFuture<Void> cancelled = bodyCancelled;
request.responseHandler().onHeaders(response.build());
request.responseHandler().onStream(new OnDemandBody(body, executor, cancelled));
return CompletableFuture.completedFuture(null);
}
@Override
public String clientName() {
return "in-memory-transport";
}
@Override
public void close() {
}
}
/**
* Emits the body in 64 KiB chunks only as they are requested, like a real connection applying
* backpressure, and completes {@code cancelled} if the subscriber cancels.
*/
private static final class OnDemandBody implements SdkPublisher<ByteBuffer> {
private final byte[] body;
private final ExecutorService executor;
private final CompletableFuture<Void> cancelled;
OnDemandBody(byte[] body, ExecutorService executor, CompletableFuture<Void> cancelled) {
this.body = body;
this.executor = executor;
this.cancelled = cancelled;
}
@Override
public void subscribe(Subscriber<? super ByteBuffer> subscriber) {
subscriber.onSubscribe(new Subscription() {
private int position;
@Override
public void request(long n) {
// Deliver on another thread so onNext never re-enters the caller of request().
executor.execute(() -> {
synchronized (this) {
for (long i = 0; i < n && !cancelled.isDone(); i++) {
if (position >= body.length) {
if (position++ == body.length) {
subscriber.onComplete();
}
return;
}
int length = Math.min(64 * 1024, body.length - position);
subscriber.onNext(ByteBuffer.wrap(body, position, length).slice());
position += length;
}
}
});
}
@Override
public void cancel() {
cancelled.complete(null);
}
});
}
}
}