object_lock_reproduce_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package retention
  2. import (
  3. "context"
  4. "fmt"
  5. "testing"
  6. "time"
  7. "github.com/aws/aws-sdk-go-v2/aws"
  8. "github.com/aws/aws-sdk-go-v2/service/s3"
  9. "github.com/stretchr/testify/require"
  10. )
  11. // TestReproduceObjectLockIssue reproduces the Object Lock header processing issue step by step
  12. func TestReproduceObjectLockIssue(t *testing.T) {
  13. client := getS3Client(t)
  14. bucketName := fmt.Sprintf("object-lock-test-%d", time.Now().UnixNano())
  15. t.Logf("=== Reproducing Object Lock Header Processing Issue ===")
  16. t.Logf("Bucket name: %s", bucketName)
  17. // Step 1: Create bucket with Object Lock enabled header
  18. t.Logf("\n1. Creating bucket with ObjectLockEnabledForBucket=true")
  19. t.Logf(" This should send x-amz-bucket-object-lock-enabled: true header")
  20. createResp, err := client.CreateBucket(context.TODO(), &s3.CreateBucketInput{
  21. Bucket: aws.String(bucketName),
  22. ObjectLockEnabledForBucket: aws.Bool(true), // This sets the x-amz-bucket-object-lock-enabled header
  23. })
  24. if err != nil {
  25. t.Fatalf("Bucket creation failed: %v", err)
  26. }
  27. t.Logf("✅ Bucket created successfully")
  28. t.Logf(" Response: %+v", createResp)
  29. // Step 2: Check if Object Lock is actually enabled
  30. t.Logf("\n2. Checking Object Lock configuration to verify it was enabled")
  31. objectLockResp, err := client.GetObjectLockConfiguration(context.TODO(), &s3.GetObjectLockConfigurationInput{
  32. Bucket: aws.String(bucketName),
  33. })
  34. if err != nil {
  35. t.Logf("❌ GetObjectLockConfiguration FAILED: %v", err)
  36. t.Logf(" This demonstrates the issue with header processing!")
  37. t.Logf(" S3 clients expect this call to succeed if Object Lock is supported")
  38. t.Logf(" When this fails, clients conclude that Object Lock is not supported")
  39. // This failure demonstrates the bug - the bucket was created but Object Lock wasn't enabled
  40. t.Logf("\n🐛 BUG CONFIRMED:")
  41. t.Logf(" - Bucket creation with ObjectLockEnabledForBucket=true succeeded")
  42. t.Logf(" - But GetObjectLockConfiguration fails")
  43. t.Logf(" - This means the x-amz-bucket-object-lock-enabled header was ignored")
  44. } else {
  45. t.Logf("✅ GetObjectLockConfiguration succeeded!")
  46. t.Logf(" Response: %+v", objectLockResp)
  47. t.Logf(" Object Lock is properly enabled - this is the expected behavior")
  48. }
  49. // Step 3: Check versioning status (required for Object Lock)
  50. t.Logf("\n3. Checking bucket versioning status (required for Object Lock)")
  51. versioningResp, err := client.GetBucketVersioning(context.TODO(), &s3.GetBucketVersioningInput{
  52. Bucket: aws.String(bucketName),
  53. })
  54. require.NoError(t, err)
  55. t.Logf(" Versioning status: %v", versioningResp.Status)
  56. if versioningResp.Status != "Enabled" {
  57. t.Logf(" ⚠️ Versioning should be automatically enabled when Object Lock is enabled")
  58. }
  59. // Cleanup
  60. t.Logf("\n4. Cleaning up test bucket")
  61. _, err = client.DeleteBucket(context.TODO(), &s3.DeleteBucketInput{
  62. Bucket: aws.String(bucketName),
  63. })
  64. if err != nil {
  65. t.Logf(" Warning: Failed to delete bucket: %v", err)
  66. }
  67. t.Logf("\n=== Issue Reproduction Complete ===")
  68. t.Logf("Expected behavior after fix:")
  69. t.Logf(" - CreateBucket with ObjectLockEnabledForBucket=true should enable Object Lock")
  70. t.Logf(" - GetObjectLockConfiguration should return enabled configuration")
  71. t.Logf(" - Versioning should be automatically enabled")
  72. }
  73. // TestNormalBucketCreationStillWorks tests that normal bucket creation still works
  74. func TestNormalBucketCreationStillWorks(t *testing.T) {
  75. client := getS3Client(t)
  76. bucketName := fmt.Sprintf("normal-test-%d", time.Now().UnixNano())
  77. t.Logf("=== Testing Normal Bucket Creation ===")
  78. // Create bucket without Object Lock
  79. _, err := client.CreateBucket(context.TODO(), &s3.CreateBucketInput{
  80. Bucket: aws.String(bucketName),
  81. })
  82. require.NoError(t, err)
  83. t.Logf("✅ Normal bucket creation works")
  84. // Object Lock should NOT be enabled
  85. _, err = client.GetObjectLockConfiguration(context.TODO(), &s3.GetObjectLockConfigurationInput{
  86. Bucket: aws.String(bucketName),
  87. })
  88. require.Error(t, err, "GetObjectLockConfiguration should fail for bucket without Object Lock")
  89. t.Logf("✅ GetObjectLockConfiguration correctly fails for normal bucket")
  90. // Cleanup
  91. client.DeleteBucket(context.TODO(), &s3.DeleteBucketInput{Bucket: aws.String(bucketName)})
  92. }