memory_policy.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package memory
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/s3api/policy_engine"
  6. )
  7. // GetPolicies retrieves all IAM policies from memory
  8. func (store *MemoryStore) GetPolicies(ctx context.Context) (map[string]policy_engine.PolicyDocument, error) {
  9. store.mu.RLock()
  10. defer store.mu.RUnlock()
  11. if !store.initialized {
  12. return nil, fmt.Errorf("store not initialized")
  13. }
  14. // Create a copy of the policies map to avoid mutation issues
  15. policies := make(map[string]policy_engine.PolicyDocument)
  16. for name, doc := range store.policies {
  17. policies[name] = doc
  18. }
  19. return policies, nil
  20. }
  21. // GetPolicy retrieves a specific IAM policy by name from memory
  22. func (store *MemoryStore) GetPolicy(ctx context.Context, name string) (*policy_engine.PolicyDocument, error) {
  23. store.mu.RLock()
  24. defer store.mu.RUnlock()
  25. if policy, exists := store.policies[name]; exists {
  26. return &policy, nil
  27. }
  28. return nil, nil // Policy not found
  29. }
  30. // CreatePolicy creates a new IAM policy in memory
  31. func (store *MemoryStore) CreatePolicy(ctx context.Context, name string, document policy_engine.PolicyDocument) error {
  32. store.mu.Lock()
  33. defer store.mu.Unlock()
  34. if !store.initialized {
  35. return fmt.Errorf("store not initialized")
  36. }
  37. store.policies[name] = document
  38. return nil
  39. }
  40. // UpdatePolicy updates an existing IAM policy in memory
  41. func (store *MemoryStore) UpdatePolicy(ctx context.Context, name string, document policy_engine.PolicyDocument) error {
  42. store.mu.Lock()
  43. defer store.mu.Unlock()
  44. if !store.initialized {
  45. return fmt.Errorf("store not initialized")
  46. }
  47. store.policies[name] = document
  48. return nil
  49. }
  50. // DeletePolicy deletes an IAM policy from memory
  51. func (store *MemoryStore) DeletePolicy(ctx context.Context, name string) error {
  52. store.mu.Lock()
  53. defer store.mu.Unlock()
  54. if !store.initialized {
  55. return fmt.Errorf("store not initialized")
  56. }
  57. delete(store.policies, name)
  58. return nil
  59. }