memory_store.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package memory
  2. import (
  3. "sync"
  4. "github.com/seaweedfs/seaweedfs/weed/credential"
  5. "github.com/seaweedfs/seaweedfs/weed/pb/iam_pb"
  6. "github.com/seaweedfs/seaweedfs/weed/s3api/policy_engine"
  7. "github.com/seaweedfs/seaweedfs/weed/util"
  8. )
  9. func init() {
  10. credential.Stores = append(credential.Stores, &MemoryStore{})
  11. }
  12. // MemoryStore implements CredentialStore using in-memory storage
  13. // This is primarily intended for testing purposes
  14. type MemoryStore struct {
  15. mu sync.RWMutex
  16. users map[string]*iam_pb.Identity // username -> identity
  17. accessKeys map[string]string // access_key -> username
  18. policies map[string]policy_engine.PolicyDocument // policy_name -> policy_document
  19. initialized bool
  20. }
  21. func (store *MemoryStore) GetName() credential.CredentialStoreTypeName {
  22. return credential.StoreTypeMemory
  23. }
  24. func (store *MemoryStore) Initialize(configuration util.Configuration, prefix string) error {
  25. store.mu.Lock()
  26. defer store.mu.Unlock()
  27. if store.initialized {
  28. return nil
  29. }
  30. store.users = make(map[string]*iam_pb.Identity)
  31. store.accessKeys = make(map[string]string)
  32. store.policies = make(map[string]policy_engine.PolicyDocument)
  33. store.initialized = true
  34. return nil
  35. }
  36. func (store *MemoryStore) Shutdown() {
  37. store.mu.Lock()
  38. defer store.mu.Unlock()
  39. store.users = nil
  40. store.accessKeys = nil
  41. store.policies = nil
  42. store.initialized = false
  43. }
  44. // Reset clears all data in the store (useful for testing)
  45. func (store *MemoryStore) Reset() {
  46. store.mu.Lock()
  47. defer store.mu.Unlock()
  48. if store.initialized {
  49. store.users = make(map[string]*iam_pb.Identity)
  50. store.accessKeys = make(map[string]string)
  51. }
  52. }
  53. // GetUserCount returns the number of users in the store (useful for testing)
  54. func (store *MemoryStore) GetUserCount() int {
  55. store.mu.RLock()
  56. defer store.mu.RUnlock()
  57. return len(store.users)
  58. }
  59. // GetAccessKeyCount returns the number of access keys in the store (useful for testing)
  60. func (store *MemoryStore) GetAccessKeyCount() int {
  61. store.mu.RLock()
  62. defer store.mu.RUnlock()
  63. return len(store.accessKeys)
  64. }