aboutsummaryrefslogtreecommitdiff
path: root/src/garage/tests/s3/ssec.rs
blob: d8f11950b59352d831a564521fe6e39c69951568 (plain) (blame)
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
use crate::common::{self, Context};
use aws_sdk_s3::primitives::ByteStream;
use aws_sdk_s3::types::{CompletedMultipartUpload, CompletedPart};

const SSEC_KEY: &str = "u8zCfnEyt5Imo/krN+sxA1DQXxLWtPJavU6T6gOVj1Y=";
const SSEC_KEY_MD5: &str = "jMGbs3GyZkYjJUP6q5jA7g==";
const SSEC_KEY2: &str = "XkYVk4Z3vVDO2yJaUqCAEZX6lL10voMxtV06d8my/eU=";
const SSEC_KEY2_MD5: &str = "kedo2ab8J1MCjHwJuLTJHw==";

const SZ_2MB: usize = 2 * 1024 * 1024;

#[tokio::test]
async fn test_ssec_object() {
	let ctx = common::context();
	let bucket = ctx.create_bucket("sse-c");

	let bytes1 = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".to_vec();
	let bytes2 = (0..400000)
		.map(|x| ((x * 3792) % 256) as u8)
		.collect::<Vec<u8>>();

	for data in vec![bytes1, bytes2] {
		let stream = ByteStream::new(data.clone().into());

		// Write encrypted object
		let r = ctx
			.client
			.put_object()
			.bucket(&bucket)
			.key("testobj")
			.sse_customer_algorithm("AES256")
			.sse_customer_key(SSEC_KEY)
			.sse_customer_key_md5(SSEC_KEY_MD5)
			.body(stream)
			.send()
			.await
			.unwrap();
		assert_eq!(r.sse_customer_algorithm, Some("AES256".into()));
		assert_eq!(r.sse_customer_key_md5, Some(SSEC_KEY_MD5.into()));

		test_read_encrypted(
			&ctx,
			&bucket,
			"testobj",
			&data,
			SSEC_KEY,
			SSEC_KEY_MD5,
			SSEC_KEY2,
			SSEC_KEY2_MD5,
		)
		.await;

		// Test copy from encrypted to non-encrypted
		let r = ctx
			.client
			.copy_object()
			.bucket(&bucket)
			.key("test-copy-enc-dec")
			.copy_source(format!("{}/{}", bucket, "testobj"))
			.copy_source_sse_customer_algorithm("AES256")
			.copy_source_sse_customer_key(SSEC_KEY)
			.copy_source_sse_customer_key_md5(SSEC_KEY_MD5)
			.send()
			.await
			.unwrap();
		assert_eq!(r.sse_customer_algorithm, None);
		assert_eq!(r.sse_customer_key_md5, None);

		// Test read decrypted file
		let r = ctx
			.client
			.get_object()
			.bucket(&bucket)
			.key("test-copy-enc-dec")
			.send()
			.await
			.unwrap();
		assert_bytes_eq!(r.body, &data);
		assert_eq!(r.sse_customer_algorithm, None);
		assert_eq!(r.sse_customer_key_md5, None);

		// Test copy from non-encrypted to encrypted
		let r = ctx
			.client
			.copy_object()
			.bucket(&bucket)
			.key("test-copy-enc-dec-enc")
			.copy_source(format!("{}/test-copy-enc-dec", bucket))
			.sse_customer_algorithm("AES256")
			.sse_customer_key(SSEC_KEY2)
			.sse_customer_key_md5(SSEC_KEY2_MD5)
			.send()
			.await
			.unwrap();
		assert_eq!(r.sse_customer_algorithm, Some("AES256".into()));
		assert_eq!(r.sse_customer_key_md5, Some(SSEC_KEY2_MD5.into()));

		test_read_encrypted(
			&ctx,
			&bucket,
			"test-copy-enc-dec-enc",
			&data,
			SSEC_KEY2,
			SSEC_KEY2_MD5,
			SSEC_KEY,
			SSEC_KEY_MD5,
		)
		.await;

		// Test copy from encrypted to encrypted with different keys
		let r = ctx
			.client
			.copy_object()
			.bucket(&bucket)
			.key("test-copy-enc-enc")
			.copy_source(format!("{}/{}", bucket, "testobj"))
			.copy_source_sse_customer_algorithm("AES256")
			.copy_source_sse_customer_key(SSEC_KEY)
			.copy_source_sse_customer_key_md5(SSEC_KEY_MD5)
			.sse_customer_algorithm("AES256")
			.sse_customer_key(SSEC_KEY2)
			.sse_customer_key_md5(SSEC_KEY2_MD5)
			.send()
			.await
			.unwrap();
		assert_eq!(r.sse_customer_algorithm, Some("AES256".into()));
		assert_eq!(r.sse_customer_key_md5, Some(SSEC_KEY2_MD5.into()));
		test_read_encrypted(
			&ctx,
			&bucket,
			"test-copy-enc-enc",
			&data,
			SSEC_KEY2,
			SSEC_KEY2_MD5,
			SSEC_KEY,
			SSEC_KEY_MD5,
		)
		.await;

		// Test copy from encrypted to encrypted with the same key
		let r = ctx
			.client
			.copy_object()
			.bucket(&bucket)
			.key("test-copy-enc-enc-same")
			.copy_source(format!("{}/{}", bucket, "testobj"))
			.copy_source_sse_customer_algorithm("AES256")
			.copy_source_sse_customer_key(SSEC_KEY)
			.copy_source_sse_customer_key_md5(SSEC_KEY_MD5)
			.sse_customer_algorithm("AES256")
			.sse_customer_key(SSEC_KEY)
			.sse_customer_key_md5(SSEC_KEY_MD5)
			.send()
			.await
			.unwrap();
		assert_eq!(r.sse_customer_algorithm, Some("AES256".into()));
		assert_eq!(r.sse_customer_key_md5, Some(SSEC_KEY_MD5.into()));
		test_read_encrypted(
			&ctx,
			&bucket,
			"test-copy-enc-enc-same",
			&data,
			SSEC_KEY,
			SSEC_KEY_MD5,
			SSEC_KEY2,
			SSEC_KEY2_MD5,
		)
		.await;
	}
}

#[tokio::test]
async fn test_multipart_upload() {
	let ctx = common::context();
	let bucket = ctx.create_bucket("test-ssec-mpu");

	let u1 = vec![0x11; SZ_2MB];
	let u2 = vec![0x22; SZ_2MB];
	let u3 = vec![0x33; SZ_2MB];
	let all = [&u1[..], &u2[..], &u3[..]].concat();

	// Test simple encrypted mpu
	{
		let up = ctx
			.client
			.create_multipart_upload()
			.bucket(&bucket)
			.key("a")
			.sse_customer_algorithm("AES256")
			.sse_customer_key(SSEC_KEY)
			.sse_customer_key_md5(SSEC_KEY_MD5)
			.send()
			.await
			.unwrap();
		assert!(up.upload_id.is_some());
		assert_eq!(up.sse_customer_algorithm, Some("AES256".into()));
		assert_eq!(up.sse_customer_key_md5, Some(SSEC_KEY_MD5.into()));

		let uid = up.upload_id.as_ref().unwrap();

		let mut etags = vec![];
		for (i, part) in vec![&u1, &u2, &u3].into_iter().enumerate() {
			let pu = ctx
				.client
				.upload_part()
				.bucket(&bucket)
				.key("a")
				.upload_id(uid)
				.part_number((i + 1) as i32)
				.sse_customer_algorithm("AES256")
				.sse_customer_key(SSEC_KEY)
				.sse_customer_key_md5(SSEC_KEY_MD5)
				.body(ByteStream::from(part.to_vec()))
				.send()
				.await
				.unwrap();
			etags.push(pu.e_tag.unwrap());
		}

		let mut cmp = CompletedMultipartUpload::builder();
		for (i, etag) in etags.into_iter().enumerate() {
			cmp = cmp.parts(
				CompletedPart::builder()
					.part_number((i + 1) as i32)
					.e_tag(etag)
					.build(),
			);
		}

		ctx.client
			.complete_multipart_upload()
			.bucket(&bucket)
			.key("a")
			.upload_id(uid)
			.multipart_upload(cmp.build())
			.send()
			.await
			.unwrap();

		test_read_encrypted(
			&ctx,
			&bucket,
			"a",
			&all,
			SSEC_KEY,
			SSEC_KEY_MD5,
			SSEC_KEY2,
			SSEC_KEY2_MD5,
		)
		.await;
	}

	// Test upload part copy from first object
	{
		// (setup) Upload a single part object
		ctx.client
			.put_object()
			.bucket(&bucket)
			.key("b")
			.body(ByteStream::from(u1.clone()))
			.sse_customer_algorithm("AES256")
			.sse_customer_key(SSEC_KEY2)
			.sse_customer_key_md5(SSEC_KEY2_MD5)
			.send()
			.await
			.unwrap();

		let up = ctx
			.client
			.create_multipart_upload()
			.bucket(&bucket)
			.key("target")
			.sse_customer_algorithm("AES256")
			.sse_customer_key(SSEC_KEY2)
			.sse_customer_key_md5(SSEC_KEY2_MD5)
			.send()
			.await
			.unwrap();
		let uid = up.upload_id.as_ref().unwrap();

		let p1 = ctx
			.client
			.upload_part()
			.bucket(&bucket)
			.key("target")
			.upload_id(uid)
			.part_number(1)
			.sse_customer_algorithm("AES256")
			.sse_customer_key(SSEC_KEY2)
			.sse_customer_key_md5(SSEC_KEY2_MD5)
			.body(ByteStream::from(u3.clone()))
			.send()
			.await
			.unwrap();

		let p2 = ctx
			.client
			.upload_part_copy()
			.bucket(&bucket)
			.key("target")
			.upload_id(uid)
			.part_number(2)
			.copy_source(format!("{}/a", bucket))
			.copy_source_range("bytes=500-550000")
			.copy_source_sse_customer_algorithm("AES256")
			.copy_source_sse_customer_key(SSEC_KEY)
			.copy_source_sse_customer_key_md5(SSEC_KEY_MD5)
			.sse_customer_algorithm("AES256")
			.sse_customer_key(SSEC_KEY2)
			.sse_customer_key_md5(SSEC_KEY2_MD5)
			.send()
			.await
			.unwrap();

		let p3 = ctx
			.client
			.upload_part()
			.bucket(&bucket)
			.key("target")
			.upload_id(uid)
			.part_number(3)
			.sse_customer_algorithm("AES256")
			.sse_customer_key(SSEC_KEY2)
			.sse_customer_key_md5(SSEC_KEY2_MD5)
			.body(ByteStream::from(u2.clone()))
			.send()
			.await
			.unwrap();

		let p4 = ctx
			.client
			.upload_part_copy()
			.bucket(&bucket)
			.key("target")
			.upload_id(uid)
			.part_number(4)
			.copy_source(format!("{}/b", bucket))
			.copy_source_range("bytes=1500-20500")
			.copy_source_sse_customer_algorithm("AES256")
			.copy_source_sse_customer_key(SSEC_KEY2)
			.copy_source_sse_customer_key_md5(SSEC_KEY2_MD5)
			.sse_customer_algorithm("AES256")
			.sse_customer_key(SSEC_KEY2)
			.sse_customer_key_md5(SSEC_KEY2_MD5)
			.send()
			.await
			.unwrap();

		let cmp = CompletedMultipartUpload::builder()
			.parts(
				CompletedPart::builder()
					.part_number(1)
					.e_tag(p1.e_tag.unwrap())
					.build(),
			)
			.parts(
				CompletedPart::builder()
					.part_number(2)
					.e_tag(p2.copy_part_result.unwrap().e_tag.unwrap())
					.build(),
			)
			.parts(
				CompletedPart::builder()
					.part_number(3)
					.e_tag(p3.e_tag.unwrap())
					.build(),
			)
			.parts(
				CompletedPart::builder()
					.part_number(4)
					.e_tag(p4.copy_part_result.unwrap().e_tag.unwrap())
					.build(),
			)
			.build();

		ctx.client
			.complete_multipart_upload()
			.bucket(&bucket)
			.key("target")
			.upload_id(uid)
			.multipart_upload(cmp)
			.send()
			.await
			.unwrap();

		// (check) Get object
		let expected = [&u3[..], &all[500..550001], &u2[..], &u1[1500..20501]].concat();
		test_read_encrypted(
			&ctx,
			&bucket,
			"target",
			&expected,
			SSEC_KEY2,
			SSEC_KEY2_MD5,
			SSEC_KEY,
			SSEC_KEY_MD5,
		)
		.await;
	}
}

async fn test_read_encrypted(
	ctx: &Context,
	bucket: &str,
	obj_key: &str,
	expected_data: &[u8],
	enc_key: &str,
	enc_key_md5: &str,
	wrong_enc_key: &str,
	wrong_enc_key_md5: &str,
) {
	// Test read encrypted without key
	let o = ctx
		.client
		.get_object()
		.bucket(bucket)
		.key(obj_key)
		.send()
		.await;
	assert!(
		o.is_err(),
		"encrypted file could be read without encryption key"
	);

	// Test read encrypted with wrong key
	let o = ctx
		.client
		.get_object()
		.bucket(bucket)
		.key(obj_key)
		.sse_customer_key(wrong_enc_key)
		.sse_customer_key_md5(wrong_enc_key_md5)
		.send()
		.await;
	assert!(
		o.is_err(),
		"encrypted file could be read with incorrect encryption key"
	);

	// Test read encrypted with correct key
	let o = ctx
		.client
		.get_object()
		.bucket(bucket)
		.key(obj_key)
		.sse_customer_algorithm("AES256")
		.sse_customer_key(enc_key)
		.sse_customer_key_md5(enc_key_md5)
		.send()
		.await
		.unwrap();
	assert_bytes_eq!(o.body, expected_data);
	assert_eq!(o.sse_customer_algorithm, Some("AES256".into()));
	assert_eq!(o.sse_customer_key_md5, Some(enc_key_md5.to_string()));
}