policy_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package test
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/seaweedfs/seaweedfs/weed/credential"
  6. "github.com/seaweedfs/seaweedfs/weed/credential/memory"
  7. "github.com/seaweedfs/seaweedfs/weed/s3api/policy_engine"
  8. // Import all store implementations to register them
  9. _ "github.com/seaweedfs/seaweedfs/weed/credential/filer_etc"
  10. _ "github.com/seaweedfs/seaweedfs/weed/credential/memory"
  11. _ "github.com/seaweedfs/seaweedfs/weed/credential/postgres"
  12. )
  13. // TestPolicyManagement tests policy management across all credential stores
  14. func TestPolicyManagement(t *testing.T) {
  15. ctx := context.Background()
  16. // Test with memory store (easiest to test)
  17. credentialManager, err := credential.NewCredentialManager(credential.StoreTypeMemory, nil, "")
  18. if err != nil {
  19. t.Fatalf("Failed to create credential manager: %v", err)
  20. }
  21. // Test policy operations
  22. testPolicyOperations(t, ctx, credentialManager)
  23. }
  24. func testPolicyOperations(t *testing.T, ctx context.Context, credentialManager *credential.CredentialManager) {
  25. store := credentialManager.GetStore()
  26. // Cast to memory store to access policy methods
  27. memoryStore, ok := store.(*memory.MemoryStore)
  28. if !ok {
  29. t.Skip("Store is not a memory store")
  30. }
  31. // Test GetPolicies (should be empty initially)
  32. policies, err := memoryStore.GetPolicies(ctx)
  33. if err != nil {
  34. t.Fatalf("Failed to get policies: %v", err)
  35. }
  36. if len(policies) != 0 {
  37. t.Errorf("Expected 0 policies, got %d", len(policies))
  38. }
  39. // Test CreatePolicy
  40. testPolicy := policy_engine.PolicyDocument{
  41. Version: "2012-10-17",
  42. Statement: []policy_engine.PolicyStatement{
  43. {
  44. Effect: policy_engine.PolicyEffectAllow,
  45. Action: policy_engine.NewStringOrStringSlice("s3:GetObject"),
  46. Resource: policy_engine.NewStringOrStringSlice("arn:aws:s3:::test-bucket/*"),
  47. },
  48. },
  49. }
  50. err = memoryStore.CreatePolicy(ctx, "test-policy", testPolicy)
  51. if err != nil {
  52. t.Fatalf("Failed to create policy: %v", err)
  53. }
  54. // Test GetPolicies (should have 1 policy now)
  55. policies, err = memoryStore.GetPolicies(ctx)
  56. if err != nil {
  57. t.Fatalf("Failed to get policies: %v", err)
  58. }
  59. if len(policies) != 1 {
  60. t.Errorf("Expected 1 policy, got %d", len(policies))
  61. }
  62. // Verify policy content
  63. policy, exists := policies["test-policy"]
  64. if !exists {
  65. t.Error("test-policy not found")
  66. }
  67. if policy.Version != "2012-10-17" {
  68. t.Errorf("Expected policy version '2012-10-17', got '%s'", policy.Version)
  69. }
  70. if len(policy.Statement) != 1 {
  71. t.Errorf("Expected 1 statement, got %d", len(policy.Statement))
  72. }
  73. // Test UpdatePolicy
  74. updatedPolicy := policy_engine.PolicyDocument{
  75. Version: "2012-10-17",
  76. Statement: []policy_engine.PolicyStatement{
  77. {
  78. Effect: policy_engine.PolicyEffectAllow,
  79. Action: policy_engine.NewStringOrStringSlice("s3:GetObject", "s3:PutObject"),
  80. Resource: policy_engine.NewStringOrStringSlice("arn:aws:s3:::test-bucket/*"),
  81. },
  82. },
  83. }
  84. err = memoryStore.UpdatePolicy(ctx, "test-policy", updatedPolicy)
  85. if err != nil {
  86. t.Fatalf("Failed to update policy: %v", err)
  87. }
  88. // Verify the update
  89. policies, err = memoryStore.GetPolicies(ctx)
  90. if err != nil {
  91. t.Fatalf("Failed to get policies after update: %v", err)
  92. }
  93. updatedPolicyResult, exists := policies["test-policy"]
  94. if !exists {
  95. t.Error("test-policy not found after update")
  96. }
  97. if len(updatedPolicyResult.Statement) != 1 {
  98. t.Errorf("Expected 1 statement after update, got %d", len(updatedPolicyResult.Statement))
  99. }
  100. if len(updatedPolicyResult.Statement[0].Action.Strings()) != 2 {
  101. t.Errorf("Expected 2 actions after update, got %d", len(updatedPolicyResult.Statement[0].Action.Strings()))
  102. }
  103. // Test DeletePolicy
  104. err = memoryStore.DeletePolicy(ctx, "test-policy")
  105. if err != nil {
  106. t.Fatalf("Failed to delete policy: %v", err)
  107. }
  108. // Verify deletion
  109. policies, err = memoryStore.GetPolicies(ctx)
  110. if err != nil {
  111. t.Fatalf("Failed to get policies after deletion: %v", err)
  112. }
  113. if len(policies) != 0 {
  114. t.Errorf("Expected 0 policies after deletion, got %d", len(policies))
  115. }
  116. }
  117. // TestPolicyManagementWithFilerEtc tests policy management with filer_etc store
  118. func TestPolicyManagementWithFilerEtc(t *testing.T) {
  119. // Skip this test if we can't connect to a filer
  120. t.Skip("Filer connection required for filer_etc store testing")
  121. }
  122. // TestPolicyManagementWithPostgres tests policy management with postgres store
  123. func TestPolicyManagementWithPostgres(t *testing.T) {
  124. // Skip this test if we can't connect to PostgreSQL
  125. t.Skip("PostgreSQL connection required for postgres store testing")
  126. }