-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathexpress.rs
412 lines (364 loc) · 13.7 KB
/
express.rs
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
use std::time::{Duration, SystemTime};
use aws_config::Region;
use aws_sdk_s3::config::{Builder, Credentials};
use aws_sdk_s3::presigning::PresigningConfig;
use aws_sdk_s3::primitives::SdkBody;
use aws_sdk_s3::types::ChecksumAlgorithm;
use aws_sdk_s3::{Client, Config};
use aws_smithy_runtime::client::http::test_util::dvr::ReplayingClient;
use aws_smithy_runtime::client::http::test_util::{
capture_request, ReplayEvent, StaticReplayClient,
};
use aws_smithy_runtime::test_util::capture_test_logs::capture_test_logs;
use http::Uri;
async fn test_client<F>(update_builder: F) -> Client
where
F: Fn(Builder) -> Builder,
{
let sdk_config = aws_config::from_env().region("us-west-2").load().await;
let config = Config::from(&sdk_config).to_builder().with_test_defaults();
aws_sdk_s3::Client::from_conf(update_builder(config).build())
}
// TODO(S3Express): Convert this test to the S3 express section in canary
#[tokio::test]
async fn list_objects_v2() {
let _logs = capture_test_logs();
let http_client =
ReplayingClient::from_file("tests/data/express/list-objects-v2.json").unwrap();
let client = test_client(|b| b.http_client(http_client.clone())).await;
let result = client
.list_objects_v2()
.bucket("s3express-test-bucket--usw2-az1--x-s3")
.send()
.await;
dbg!(result).expect("success");
http_client
.validate_body_and_headers(Some(&["x-amz-s3session-token"]), "application/xml")
.await
.unwrap();
}
#[tokio::test]
async fn mixed_auths() {
let _logs = capture_test_logs();
let http_client = ReplayingClient::from_file("tests/data/express/mixed-auths.json").unwrap();
let client = test_client(|b| b.http_client(http_client.clone())).await;
// A call to an S3 Express bucket where we should see two request/response pairs,
// one for the `create_session` API and the other for `list_objects_v2` in S3 Express bucket.
let result = client
.list_objects_v2()
.bucket("s3express-test-bucket--usw2-az1--x-s3")
.send()
.await;
dbg!(result).expect("success");
// A call to a regular bucket, and request headers should not contain `x-amz-s3session-token`.
let result = client
.list_objects_v2()
.bucket("regular-test-bucket")
.send()
.await;
dbg!(result).expect("success");
// A call to another S3 Express bucket where we should again see two request/response pairs,
// one for the `create_session` API and the other for `list_objects_v2` in S3 Express bucket.
let result = client
.list_objects_v2()
.bucket("s3express-test-bucket-2--usw2-az3--x-s3")
.send()
.await;
dbg!(result).expect("success");
// This call should be an identity cache hit for the first S3 Express bucket,
// thus no HTTP request should be sent to the `create_session` API.
let result = client
.list_objects_v2()
.bucket("s3express-test-bucket--usw2-az1--x-s3")
.send()
.await;
dbg!(result).expect("success");
http_client
.validate_body_and_headers(Some(&["x-amz-s3session-token"]), "application/xml")
.await
.unwrap();
}
fn create_session_request() -> http::Request<SdkBody> {
http::Request::builder()
.uri("https://s3express-test-bucket--usw2-az1--x-s3.s3express-usw2-az1.us-west-2.amazonaws.com/?session")
.header("x-amz-create-session-mode", "ReadWrite")
.method("GET")
.body(SdkBody::empty())
.unwrap()
}
fn create_session_response() -> http::Response<SdkBody> {
http::Response::builder()
.status(200)
.body(SdkBody::from(
r#"<?xml version="1.0" encoding="UTF-8"?>
<CreateSessionResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Credentials>
<SessionToken>TESTSESSIONTOKEN</SessionToken>
<SecretAccessKey>TESTSECRETKEY</SecretAccessKey>
<AccessKeyId>ASIARTESTID</AccessKeyId>
<Expiration>2024-01-29T18:53:01Z</Expiration>
</Credentials>
</CreateSessionResult>
"#,
))
.unwrap()
}
#[tokio::test]
async fn presigning() {
let http_client = StaticReplayClient::new(vec![ReplayEvent::new(
create_session_request(),
create_session_response(),
)]);
let client = test_client(|b| b.http_client(http_client.clone())).await;
let presigning_config = PresigningConfig::builder()
.start_time(SystemTime::UNIX_EPOCH + Duration::from_secs(1234567891))
.expires_in(Duration::from_secs(30))
.build()
.unwrap();
let presigned = client
.get_object()
.bucket("s3express-test-bucket--usw2-az1--x-s3")
.key("ferris.png")
.presigned(presigning_config)
.await
.unwrap();
let uri = presigned.uri().parse::<Uri>().unwrap();
let pq = uri.path_and_query().unwrap();
let path = pq.path();
let query = pq.query().unwrap();
let mut query_params: Vec<&str> = query.split('&').collect();
query_params.sort();
pretty_assertions::assert_eq!(
"s3express-test-bucket--usw2-az1--x-s3.s3express-usw2-az1.us-west-2.amazonaws.com",
uri.authority().unwrap()
);
assert_eq!("GET", presigned.method());
assert_eq!("/ferris.png", path);
pretty_assertions::assert_eq!(
&[
"X-Amz-Algorithm=AWS4-HMAC-SHA256",
"X-Amz-Credential=ASIARTESTID%2F20090213%2Fus-west-2%2Fs3express%2Faws4_request",
"X-Amz-Date=20090213T233131Z",
"X-Amz-Expires=30",
"X-Amz-S3session-Token=TESTSESSIONTOKEN",
"X-Amz-Signature=c09c93c7878184492cb960d59e148af932dff6b19609e63e3484599903d97e44",
"X-Amz-SignedHeaders=host",
"x-id=GetObject"
][..],
&query_params
);
assert_eq!(presigned.headers().count(), 0);
}
fn operation_request_with_checksum(
query: &str,
kv: Option<(&str, &str)>,
) -> http::Request<SdkBody> {
let mut b = http::Request::builder()
.uri(&format!("https://s3express-test-bucket--usw2-az1--x-s3.s3express-usw2-az1.us-west-2.amazonaws.com/{query}"))
.method("GET");
if let Some((key, value)) = kv {
b = b.header(key, value);
}
b.body(SdkBody::empty()).unwrap()
}
fn response_ok() -> http::Response<SdkBody> {
http::Response::builder()
.status(200)
.body(SdkBody::empty())
.unwrap()
}
#[tokio::test]
async fn user_specified_checksum_should_be_respected() {
async fn runner(checksum: ChecksumAlgorithm, value: &str) {
let http_client = StaticReplayClient::new(vec![
ReplayEvent::new(create_session_request(), create_session_response()),
ReplayEvent::new(
operation_request_with_checksum(
"test?x-id=PutObject",
Some((
&format!("x-amz-checksum-{}", checksum.as_str().to_lowercase()),
&format!("{value}"),
)),
),
response_ok(),
),
]);
let client = test_client(|b| b.http_client(http_client.clone())).await;
let _ = client
.put_object()
.bucket("s3express-test-bucket--usw2-az1--x-s3")
.key("test")
.body(SdkBody::empty().into())
.checksum_algorithm(checksum)
.send()
.await;
http_client.assert_requests_match(&[""]);
}
let checksum_value_pairs = &[
(ChecksumAlgorithm::Crc32, "AAAAAA=="),
(ChecksumAlgorithm::Crc32C, "AAAAAA=="),
(ChecksumAlgorithm::Sha1, "2jmj7l5rSw0yVb/vlWAYkK/YBwk="),
(
ChecksumAlgorithm::Sha256,
"47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=",
),
];
for (checksum, value) in checksum_value_pairs {
runner(checksum.clone(), *value).await;
}
}
#[tokio::test]
async fn default_checksum_should_be_crc32_for_operation_requiring_checksum() {
let http_client = StaticReplayClient::new(vec![
ReplayEvent::new(create_session_request(), create_session_response()),
ReplayEvent::new(
operation_request_with_checksum(
"?delete&x-id=DeleteObjects",
Some(("x-amz-checksum-crc32", "AAAAAA==")),
),
response_ok(),
),
]);
let client = test_client(|b| b.http_client(http_client.clone())).await;
let _ = client
.delete_objects()
.bucket("s3express-test-bucket--usw2-az1--x-s3")
.send()
.await;
http_client.assert_requests_match(&[""]);
}
#[tokio::test]
async fn default_checksum_should_be_none() {
let http_client = StaticReplayClient::new(vec![
ReplayEvent::new(create_session_request(), create_session_response()),
ReplayEvent::new(
operation_request_with_checksum("test?x-id=PutObject", None),
response_ok(),
),
]);
let client = test_client(|b| b.http_client(http_client.clone())).await;
let _ = client
.put_object()
.bucket("s3express-test-bucket--usw2-az1--x-s3")
.key("test")
.body(SdkBody::empty().into())
.send()
.await;
http_client.assert_requests_match(&[""]);
let mut all_checksums = ChecksumAlgorithm::values()
.iter()
.map(|checksum| format!("amz-checksum-{}", checksum.to_lowercase()))
.chain(std::iter::once("content-md5".to_string()));
assert!(!all_checksums.any(|checksum| http_client
.actual_requests()
.any(|req| req.headers().iter().any(|(key, _)| key == checksum))));
}
#[tokio::test]
async fn disable_s3_express_session_auth_at_service_client_level() {
let (http_client, request) = capture_request(None);
let conf = Config::builder()
.http_client(http_client)
.region(Region::new("us-west-2"))
.with_test_defaults()
.disable_s3_express_session_auth(true)
.build();
let client = Client::from_conf(conf);
let _ = client
.list_objects_v2()
.bucket("s3express-test-bucket--usw2-az1--x-s3")
.send()
.await;
let req = request.expect_request();
assert!(
!req.headers()
.get("authorization")
.unwrap()
.contains("x-amz-create-session-mode"),
"x-amz-create-session-mode should not appear in headers when S3 Express session auth is disabled"
);
}
#[tokio::test]
async fn disable_s3_express_session_auth_at_operation_level() {
let (http_client, request) = capture_request(None);
let conf = Config::builder()
.http_client(http_client)
.region(Region::new("us-west-2"))
.with_test_defaults()
.build();
let client = Client::from_conf(conf);
let _ = client
.list_objects_v2()
.bucket("s3express-test-bucket--usw2-az1--x-s3")
.customize()
.config_override(Config::builder().disable_s3_express_session_auth(true))
.send()
.await;
let req = request.expect_request();
assert!(
!req.headers()
.get("authorization")
.unwrap()
.contains("x-amz-create-session-mode"),
"x-amz-create-session-mode should not appear in headers when S3 Express session auth is disabled"
);
}
#[tokio::test]
async fn support_customer_overriding_express_credentials_provider() {
let expected_session_token = "testsessiontoken";
let client_overriding_express_credentials_provider = || async move {
let (http_client, rx) = capture_request(None);
let client = test_client(|b| {
let credentials = Credentials::new(
"testaccess",
"testsecret",
Some(expected_session_token.to_owned()),
None,
"test",
);
b.http_client(http_client.clone())
// Pass a credential with a session token so that
// `x-amz-s3session-token` should appear in the request header
// when s3 session auth is enabled.
.express_credentials_provider(credentials.clone())
// Pass a credential with a session token so that
// `x-amz-security-token` should appear in the request header
// when s3 session auth is disabled.
.credentials_provider(credentials)
})
.await;
(client, rx)
};
// Test `x-amz-s3session-token` should be present with `expected_session_token`.
let (client, rx) = client_overriding_express_credentials_provider().await;
let _ = client
.list_objects_v2()
.bucket("s3express-test-bucket--usw2-az1--x-s3")
.send()
.await;
let req = rx.expect_request();
let actual_session_token = req
.headers()
.get("x-amz-s3session-token")
.expect("x-amz-s3session-token should be present");
assert_eq!(expected_session_token, actual_session_token);
assert!(req.headers().get("x-amz-security-token").is_none());
// With a regular S3 bucket, test `x-amz-security-token` should be present with `expected_session_token`,
// instead of `x-amz-s3session-token`.
let (client, rx) = client_overriding_express_credentials_provider().await;
let _ = client
.list_objects_v2()
.bucket("regular-test-bucket")
.send()
.await;
let req = rx.expect_request();
let actual_session_token = req
.headers()
.get("x-amz-security-token")
.expect("x-amz-security-token should be present");
assert_eq!(expected_session_token, actual_session_token);
assert!(req.headers().get("x-amz-s3session-token").is_none());
}