simple_sse_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package sse_test
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/md5"
  6. "crypto/rand"
  7. "encoding/base64"
  8. "fmt"
  9. "io"
  10. "testing"
  11. "time"
  12. "github.com/aws/aws-sdk-go-v2/aws"
  13. "github.com/aws/aws-sdk-go-v2/config"
  14. "github.com/aws/aws-sdk-go-v2/credentials"
  15. "github.com/aws/aws-sdk-go-v2/service/s3"
  16. "github.com/stretchr/testify/assert"
  17. "github.com/stretchr/testify/require"
  18. )
  19. // TestSimpleSSECIntegration tests basic SSE-C with a fixed bucket name
  20. func TestSimpleSSECIntegration(t *testing.T) {
  21. ctx := context.Background()
  22. // Create S3 client
  23. customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
  24. return aws.Endpoint{
  25. URL: "http://127.0.0.1:8333",
  26. HostnameImmutable: true,
  27. }, nil
  28. })
  29. awsCfg, err := config.LoadDefaultConfig(ctx,
  30. config.WithRegion("us-east-1"),
  31. config.WithEndpointResolverWithOptions(customResolver),
  32. config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(
  33. "some_access_key1",
  34. "some_secret_key1",
  35. "",
  36. )),
  37. )
  38. require.NoError(t, err)
  39. client := s3.NewFromConfig(awsCfg, func(o *s3.Options) {
  40. o.UsePathStyle = true
  41. })
  42. bucketName := "test-debug-bucket"
  43. objectKey := fmt.Sprintf("test-object-prefixed-%d", time.Now().UnixNano())
  44. // Generate SSE-C key
  45. key := make([]byte, 32)
  46. rand.Read(key)
  47. keyB64 := base64.StdEncoding.EncodeToString(key)
  48. keyMD5Hash := md5.Sum(key)
  49. keyMD5 := base64.StdEncoding.EncodeToString(keyMD5Hash[:])
  50. testData := []byte("Hello, simple SSE-C integration test!")
  51. // Ensure bucket exists
  52. _, err = client.CreateBucket(ctx, &s3.CreateBucketInput{
  53. Bucket: aws.String(bucketName),
  54. })
  55. if err != nil {
  56. t.Logf("Bucket creation result: %v (might be OK if exists)", err)
  57. }
  58. // Wait a moment for bucket to be ready
  59. time.Sleep(1 * time.Second)
  60. t.Run("PUT with SSE-C", func(t *testing.T) {
  61. _, err := client.PutObject(ctx, &s3.PutObjectInput{
  62. Bucket: aws.String(bucketName),
  63. Key: aws.String(objectKey),
  64. Body: bytes.NewReader(testData),
  65. SSECustomerAlgorithm: aws.String("AES256"),
  66. SSECustomerKey: aws.String(keyB64),
  67. SSECustomerKeyMD5: aws.String(keyMD5),
  68. })
  69. require.NoError(t, err, "Failed to upload SSE-C object")
  70. t.Log("✅ SSE-C PUT succeeded!")
  71. })
  72. t.Run("GET with SSE-C", func(t *testing.T) {
  73. resp, err := client.GetObject(ctx, &s3.GetObjectInput{
  74. Bucket: aws.String(bucketName),
  75. Key: aws.String(objectKey),
  76. SSECustomerAlgorithm: aws.String("AES256"),
  77. SSECustomerKey: aws.String(keyB64),
  78. SSECustomerKeyMD5: aws.String(keyMD5),
  79. })
  80. require.NoError(t, err, "Failed to retrieve SSE-C object")
  81. defer resp.Body.Close()
  82. retrievedData, err := io.ReadAll(resp.Body)
  83. require.NoError(t, err, "Failed to read retrieved data")
  84. assert.Equal(t, testData, retrievedData, "Retrieved data doesn't match original")
  85. // Verify SSE-C headers
  86. assert.Equal(t, "AES256", aws.ToString(resp.SSECustomerAlgorithm))
  87. assert.Equal(t, keyMD5, aws.ToString(resp.SSECustomerKeyMD5))
  88. t.Log("✅ SSE-C GET succeeded and data matches!")
  89. })
  90. t.Run("GET without key should fail", func(t *testing.T) {
  91. _, err := client.GetObject(ctx, &s3.GetObjectInput{
  92. Bucket: aws.String(bucketName),
  93. Key: aws.String(objectKey),
  94. })
  95. assert.Error(t, err, "Should fail to retrieve SSE-C object without key")
  96. t.Log("✅ GET without key correctly failed")
  97. })
  98. }