Repository navigation
Expand file tree
/
Copy pathtest_rustfs_kms_presign.py
More file actions
99 lines (93 loc) · 3.67 KB
/
Copy pathtest_rustfs_kms_presign.py
File metadata and controls
99 lines (93 loc) · 3.67 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
#!/usr/bin/env python3
"""Run the presigned KMS case against deterministic command fakes."""
import os
from pathlib import Path
import subprocess
import tempfile
import unittest
SCRIPT = Path(__file__).with_name('rustfs-kms-test.sh').read_text()
CASE = SCRIPT[SCRIPT.index('test_kms317() {'):SCRIPT.index('\ntest_kms318() {')]
HARNESS = r'''
set -uo pipefail
kid=k317
ENDPOINT=http://localhost:9000
REGION=us-east-1
kms_create_key() { KMS_KEY_ID=k317; }
new_bucket() { :; }
rm_bucket() { :; }
make_test_file() { printf test > "$1"; }
kms_sse_put() { :; }
kms_sse_get() { cp "${TEST_TMP}/kms-f1" "$3"; }
md5_of_file() { cksum < "$1"; }
assert_aws_ok() { return 0; }
assert_rc_ok() { [ "$RC_CODE" -eq 0 ]; }
aws() { printf 'http://localhost:9000/get-only'; }
rc_run() {
[ "$*" = 'object share local/kms317-b1/up --upload --expire 5m --json' ] || return 1
RC_CODE=0
RC_OUT='{"url":"http://localhost:9000/put-only"}'
if [ "$SCENARIO" = missing-url ]; then RC_OUT='{}'; fi
}
aws_run() {
case "$2" in
put-bucket-encryption)
printf '%s' "${@: -1}" | jq -e '.Rules[0].ApplyServerSideEncryptionByDefault == {SSEAlgorithm:"aws:kms",KMSMasterKeyID:"k317"}' >/dev/null || return 1
;;
head-object)
AWS_OUT='{"ServerSideEncryption":"aws:kms","SSEKMSKeyId":"k317"}'
if [ "$SCENARIO" = plaintext ]; then AWS_OUT='{}'; fi
if [ "$SCENARIO" = wrong-key ]; then AWS_OUT='{"ServerSideEncryption":"aws:kms","SSEKMSKeyId":"other"}'; fi
;;
*) return 1 ;;
esac
}
curl() {
if [[ " $* " == *' -X PUT '* ]]; then
[ "${@: -1}" = http://localhost:9000/put-only ] || return 1
if [ "$SCENARIO" = upload-failure ]; then return 22; fi
else
cp "${TEST_TMP}/kms-f1" "${@: -1}"
fi
}
'''
class PresignCaseTest(unittest.TestCase):
def run_case(self, scenario):
with tempfile.TemporaryDirectory() as tmp:
return subprocess.run(['bash', '-c', HARNESS + CASE + '\nif test_kms317; then exit 0; else exit 1; fi'], env={**os.environ, 'TEST_TMP': tmp, 'SCENARIO': scenario}, capture_output=True, text=True)
def test_put_signature_and_kms_metadata(self):
result = self.run_case('success')
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
def test_configure_failure_envelope_is_not_accepted(self):
case = SCRIPT[SCRIPT.index('test_kms109() {'):SCRIPT.index('\ntest_kms110() {')]
harness = r'''
set -uo pipefail
KMS_BACKEND=vault-kv2
VAULT_ADDRESS=http://fixture:8200
VAULT_TOKEN=fixture
VAULT_KV_MOUNT=secret
VAULT_KEY_PATH_PREFIX=rustfs
KMS_DEFAULT_KEY_ID=default
admin_api() {
if [[ "$*" == *Bogus* ]]; then
printf 400 > "$TEST_TMP/code"; printf 'invalid backend'
elif [ "$2" = /rustfs/admin/v3/kms/configure ]; then
printf 200 > "$TEST_TMP/code"
printf '{"success":false,"message":"Cannot configure KMS while it is running"}'
else
printf 200 > "$TEST_TMP/code"; printf '{"backend_status":"healthy"}'
fi
}
admin_api_code() { cat "$TEST_TMP/code"; }
rc_run() { RC_CODE=0; }
assert_rc_ok() { [ "$RC_CODE" -eq 0 ]; }
'''
with tempfile.TemporaryDirectory() as tmp:
result = subprocess.run(['bash', '-c', harness + case + '\nif test_kms109; then exit 0; else exit 1; fi'], env={**os.environ, 'TEST_TMP': tmp}, capture_output=True, text=True)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertNotIn('configure accepted unreachable', result.stdout)
def test_reject_invalid_uploads(self):
for scenario in ('missing-url', 'plaintext', 'wrong-key', 'upload-failure'):
with self.subTest(scenario=scenario):
self.assertNotEqual(self.run_case(scenario).returncode, 0)
if __name__ == '__main__':
unittest.main()