test_s3_kms.sh 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #!/bin/bash
  2. # End-to-end S3 KMS integration tests
  3. set -e
  4. SEAWEEDFS_S3_ENDPOINT=${SEAWEEDFS_S3_ENDPOINT:-"http://127.0.0.1:8333"}
  5. ACCESS_KEY=${ACCESS_KEY:-"any"}
  6. SECRET_KEY=${SECRET_KEY:-"any"}
  7. echo "🧪 Running S3 KMS Integration Tests"
  8. echo "S3 Endpoint: $SEAWEEDFS_S3_ENDPOINT"
  9. # Test file content
  10. TEST_CONTENT="Hello, SeaweedFS KMS Integration! This is test data that should be encrypted."
  11. TEST_FILE="/tmp/seaweedfs-kms-test.txt"
  12. DOWNLOAD_FILE="/tmp/seaweedfs-kms-download.txt"
  13. # Create test file
  14. echo "$TEST_CONTENT" > "$TEST_FILE"
  15. # AWS CLI configuration
  16. export AWS_ACCESS_KEY_ID="$ACCESS_KEY"
  17. export AWS_SECRET_ACCESS_KEY="$SECRET_KEY"
  18. export AWS_DEFAULT_REGION="us-east-1"
  19. echo "📁 Creating test buckets..."
  20. # Create test buckets
  21. BUCKETS=("test-openbao" "test-vault" "test-local" "secure-data")
  22. for bucket in "${BUCKETS[@]}"; do
  23. echo " Creating bucket: $bucket"
  24. aws s3 mb "s3://$bucket" --endpoint-url "$SEAWEEDFS_S3_ENDPOINT" || {
  25. echo " ⚠️ Bucket $bucket might already exist"
  26. }
  27. done
  28. echo "🔐 Setting up bucket encryption..."
  29. # Test 1: OpenBao KMS Encryption
  30. echo " Setting OpenBao encryption for test-openbao bucket..."
  31. cat > /tmp/openbao-encryption.json << EOF
  32. {
  33. "Rules": [
  34. {
  35. "ApplyServerSideEncryptionByDefault": {
  36. "SSEAlgorithm": "aws:kms",
  37. "KMSMasterKeyID": "test-key-1"
  38. },
  39. "BucketKeyEnabled": false
  40. }
  41. ]
  42. }
  43. EOF
  44. aws s3api put-bucket-encryption \
  45. --endpoint-url "$SEAWEEDFS_S3_ENDPOINT" \
  46. --bucket test-openbao \
  47. --server-side-encryption-configuration file:///tmp/openbao-encryption.json || {
  48. echo " ⚠️ Failed to set bucket encryption for test-openbao"
  49. }
  50. # Test 2: Verify bucket encryption
  51. echo " Verifying bucket encryption configuration..."
  52. aws s3api get-bucket-encryption \
  53. --endpoint-url "$SEAWEEDFS_S3_ENDPOINT" \
  54. --bucket test-openbao | jq '.' || {
  55. echo " ⚠️ Failed to get bucket encryption for test-openbao"
  56. }
  57. echo "⬆️ Testing object uploads with KMS encryption..."
  58. # Test 3: Upload objects with default bucket encryption
  59. echo " Uploading object with default bucket encryption..."
  60. aws s3 cp "$TEST_FILE" "s3://test-openbao/encrypted-object-1.txt" \
  61. --endpoint-url "$SEAWEEDFS_S3_ENDPOINT"
  62. # Test 4: Upload object with explicit SSE-KMS
  63. echo " Uploading object with explicit SSE-KMS headers..."
  64. aws s3 cp "$TEST_FILE" "s3://test-openbao/encrypted-object-2.txt" \
  65. --endpoint-url "$SEAWEEDFS_S3_ENDPOINT" \
  66. --sse aws:kms \
  67. --sse-kms-key-id "test-key-2"
  68. # Test 5: Upload to unencrypted bucket
  69. echo " Uploading object to unencrypted bucket..."
  70. aws s3 cp "$TEST_FILE" "s3://test-local/unencrypted-object.txt" \
  71. --endpoint-url "$SEAWEEDFS_S3_ENDPOINT"
  72. echo "⬇️ Testing object downloads and decryption..."
  73. # Test 6: Download encrypted objects
  74. echo " Downloading encrypted object 1..."
  75. aws s3 cp "s3://test-openbao/encrypted-object-1.txt" "$DOWNLOAD_FILE" \
  76. --endpoint-url "$SEAWEEDFS_S3_ENDPOINT"
  77. # Verify content
  78. if cmp -s "$TEST_FILE" "$DOWNLOAD_FILE"; then
  79. echo " ✅ Encrypted object 1 downloaded and decrypted successfully"
  80. else
  81. echo " ❌ Encrypted object 1 content mismatch"
  82. exit 1
  83. fi
  84. echo " Downloading encrypted object 2..."
  85. aws s3 cp "s3://test-openbao/encrypted-object-2.txt" "$DOWNLOAD_FILE" \
  86. --endpoint-url "$SEAWEEDFS_S3_ENDPOINT"
  87. # Verify content
  88. if cmp -s "$TEST_FILE" "$DOWNLOAD_FILE"; then
  89. echo " ✅ Encrypted object 2 downloaded and decrypted successfully"
  90. else
  91. echo " ❌ Encrypted object 2 content mismatch"
  92. exit 1
  93. fi
  94. echo "📊 Testing object metadata..."
  95. # Test 7: Check encryption metadata
  96. echo " Checking encryption metadata..."
  97. METADATA=$(aws s3api head-object \
  98. --endpoint-url "$SEAWEEDFS_S3_ENDPOINT" \
  99. --bucket test-openbao \
  100. --key encrypted-object-1.txt)
  101. echo "$METADATA" | jq '.'
  102. # Verify SSE headers are present
  103. if echo "$METADATA" | grep -q "ServerSideEncryption"; then
  104. echo " ✅ SSE metadata found in object headers"
  105. else
  106. echo " ⚠️ No SSE metadata found (might be internal only)"
  107. fi
  108. echo "📋 Testing list operations..."
  109. # Test 8: List objects
  110. echo " Listing objects in encrypted bucket..."
  111. aws s3 ls "s3://test-openbao/" --endpoint-url "$SEAWEEDFS_S3_ENDPOINT"
  112. echo "🔄 Testing multipart uploads with encryption..."
  113. # Test 9: Multipart upload with encryption
  114. LARGE_FILE="/tmp/large-test-file.txt"
  115. echo " Creating large test file..."
  116. for i in {1..1000}; do
  117. echo "Line $i: $TEST_CONTENT" >> "$LARGE_FILE"
  118. done
  119. echo " Uploading large file with multipart and SSE-KMS..."
  120. aws s3 cp "$LARGE_FILE" "s3://test-openbao/large-encrypted-file.txt" \
  121. --endpoint-url "$SEAWEEDFS_S3_ENDPOINT" \
  122. --sse aws:kms \
  123. --sse-kms-key-id "multipart-key"
  124. # Download and verify
  125. echo " Downloading and verifying large encrypted file..."
  126. DOWNLOAD_LARGE_FILE="/tmp/downloaded-large-file.txt"
  127. aws s3 cp "s3://test-openbao/large-encrypted-file.txt" "$DOWNLOAD_LARGE_FILE" \
  128. --endpoint-url "$SEAWEEDFS_S3_ENDPOINT"
  129. if cmp -s "$LARGE_FILE" "$DOWNLOAD_LARGE_FILE"; then
  130. echo " ✅ Large encrypted file uploaded and downloaded successfully"
  131. else
  132. echo " ❌ Large encrypted file content mismatch"
  133. exit 1
  134. fi
  135. echo "🧹 Cleaning up test files..."
  136. rm -f "$TEST_FILE" "$DOWNLOAD_FILE" "$LARGE_FILE" "$DOWNLOAD_LARGE_FILE" /tmp/*-encryption.json
  137. echo "📈 Running performance test..."
  138. # Test 10: Performance test
  139. PERF_FILE="/tmp/perf-test.txt"
  140. for i in {1..100}; do
  141. echo "Performance test line $i: $TEST_CONTENT" >> "$PERF_FILE"
  142. done
  143. echo " Testing upload/download performance with encryption..."
  144. start_time=$(date +%s)
  145. aws s3 cp "$PERF_FILE" "s3://test-openbao/perf-test.txt" \
  146. --endpoint-url "$SEAWEEDFS_S3_ENDPOINT" \
  147. --sse aws:kms \
  148. --sse-kms-key-id "performance-key"
  149. aws s3 cp "s3://test-openbao/perf-test.txt" "/tmp/perf-download.txt" \
  150. --endpoint-url "$SEAWEEDFS_S3_ENDPOINT"
  151. end_time=$(date +%s)
  152. duration=$((end_time - start_time))
  153. echo " ⏱️ Performance test completed in ${duration} seconds"
  154. rm -f "$PERF_FILE" "/tmp/perf-download.txt"
  155. echo ""
  156. echo "🎉 S3 KMS Integration Tests Summary:"
  157. echo " ✅ Bucket creation and encryption configuration"
  158. echo " ✅ Default bucket encryption"
  159. echo " ✅ Explicit SSE-KMS encryption"
  160. echo " ✅ Object upload and download"
  161. echo " ✅ Encryption/decryption verification"
  162. echo " ✅ Metadata handling"
  163. echo " ✅ Multipart upload with encryption"
  164. echo " ✅ Performance test"
  165. echo ""
  166. echo "🔐 All S3 KMS integration tests passed successfully!"
  167. echo ""
  168. # Optional: Show bucket sizes and object counts
  169. echo "📊 Final Statistics:"
  170. for bucket in "${BUCKETS[@]}"; do
  171. COUNT=$(aws s3 ls "s3://$bucket/" --endpoint-url "$SEAWEEDFS_S3_ENDPOINT" | wc -l)
  172. echo " Bucket $bucket: $COUNT objects"
  173. done