policies_management.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package dash
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "github.com/seaweedfs/seaweedfs/weed/credential"
  7. "github.com/seaweedfs/seaweedfs/weed/glog"
  8. "github.com/seaweedfs/seaweedfs/weed/s3api/policy_engine"
  9. )
  10. type IAMPolicy struct {
  11. Name string `json:"name"`
  12. Document policy_engine.PolicyDocument `json:"document"`
  13. DocumentJSON string `json:"document_json"`
  14. CreatedAt time.Time `json:"created_at"`
  15. UpdatedAt time.Time `json:"updated_at"`
  16. }
  17. type PoliciesCollection struct {
  18. Policies map[string]policy_engine.PolicyDocument `json:"policies"`
  19. }
  20. type PoliciesData struct {
  21. Username string `json:"username"`
  22. Policies []IAMPolicy `json:"policies"`
  23. TotalPolicies int `json:"total_policies"`
  24. LastUpdated time.Time `json:"last_updated"`
  25. }
  26. // Policy management request structures
  27. type CreatePolicyRequest struct {
  28. Name string `json:"name" binding:"required"`
  29. Document policy_engine.PolicyDocument `json:"document" binding:"required"`
  30. DocumentJSON string `json:"document_json"`
  31. }
  32. type UpdatePolicyRequest struct {
  33. Document policy_engine.PolicyDocument `json:"document" binding:"required"`
  34. DocumentJSON string `json:"document_json"`
  35. }
  36. // PolicyManager interface is now in the credential package
  37. // CredentialStorePolicyManager implements credential.PolicyManager by delegating to the credential store
  38. type CredentialStorePolicyManager struct {
  39. credentialManager *credential.CredentialManager
  40. }
  41. // NewCredentialStorePolicyManager creates a new CredentialStorePolicyManager
  42. func NewCredentialStorePolicyManager(credentialManager *credential.CredentialManager) *CredentialStorePolicyManager {
  43. return &CredentialStorePolicyManager{
  44. credentialManager: credentialManager,
  45. }
  46. }
  47. // GetPolicies retrieves all IAM policies via credential store
  48. func (cspm *CredentialStorePolicyManager) GetPolicies(ctx context.Context) (map[string]policy_engine.PolicyDocument, error) {
  49. // Get policies from credential store
  50. // We'll use the credential store to access the filer indirectly
  51. // Since policies are stored separately, we need to access the underlying store
  52. store := cspm.credentialManager.GetStore()
  53. glog.V(1).Infof("Getting policies from credential store: %T", store)
  54. // Check if the store supports policy management
  55. if policyStore, ok := store.(credential.PolicyManager); ok {
  56. glog.V(1).Infof("Store supports policy management, calling GetPolicies")
  57. policies, err := policyStore.GetPolicies(ctx)
  58. if err != nil {
  59. glog.Errorf("Error getting policies from store: %v", err)
  60. return nil, err
  61. }
  62. glog.V(1).Infof("Got %d policies from store", len(policies))
  63. return policies, nil
  64. } else {
  65. // Fallback: use empty policies for stores that don't support policies
  66. glog.V(1).Infof("Credential store doesn't support policy management, returning empty policies")
  67. return make(map[string]policy_engine.PolicyDocument), nil
  68. }
  69. }
  70. // CreatePolicy creates a new IAM policy via credential store
  71. func (cspm *CredentialStorePolicyManager) CreatePolicy(ctx context.Context, name string, document policy_engine.PolicyDocument) error {
  72. store := cspm.credentialManager.GetStore()
  73. if policyStore, ok := store.(credential.PolicyManager); ok {
  74. return policyStore.CreatePolicy(ctx, name, document)
  75. }
  76. return fmt.Errorf("credential store doesn't support policy creation")
  77. }
  78. // UpdatePolicy updates an existing IAM policy via credential store
  79. func (cspm *CredentialStorePolicyManager) UpdatePolicy(ctx context.Context, name string, document policy_engine.PolicyDocument) error {
  80. store := cspm.credentialManager.GetStore()
  81. if policyStore, ok := store.(credential.PolicyManager); ok {
  82. return policyStore.UpdatePolicy(ctx, name, document)
  83. }
  84. return fmt.Errorf("credential store doesn't support policy updates")
  85. }
  86. // DeletePolicy deletes an IAM policy via credential store
  87. func (cspm *CredentialStorePolicyManager) DeletePolicy(ctx context.Context, name string) error {
  88. store := cspm.credentialManager.GetStore()
  89. if policyStore, ok := store.(credential.PolicyManager); ok {
  90. return policyStore.DeletePolicy(ctx, name)
  91. }
  92. return fmt.Errorf("credential store doesn't support policy deletion")
  93. }
  94. // GetPolicy retrieves a specific IAM policy via credential store
  95. func (cspm *CredentialStorePolicyManager) GetPolicy(ctx context.Context, name string) (*policy_engine.PolicyDocument, error) {
  96. store := cspm.credentialManager.GetStore()
  97. if policyStore, ok := store.(credential.PolicyManager); ok {
  98. return policyStore.GetPolicy(ctx, name)
  99. }
  100. return nil, fmt.Errorf("credential store doesn't support policy retrieval")
  101. }
  102. // AdminServer policy management methods using credential.PolicyManager
  103. func (s *AdminServer) GetPolicyManager() credential.PolicyManager {
  104. if s.credentialManager == nil {
  105. glog.V(1).Infof("Credential manager is nil, policy management not available")
  106. return nil
  107. }
  108. glog.V(1).Infof("Credential manager available, creating CredentialStorePolicyManager")
  109. return NewCredentialStorePolicyManager(s.credentialManager)
  110. }
  111. // GetPolicies retrieves all IAM policies
  112. func (s *AdminServer) GetPolicies() ([]IAMPolicy, error) {
  113. policyManager := s.GetPolicyManager()
  114. if policyManager == nil {
  115. return nil, fmt.Errorf("policy manager not available")
  116. }
  117. ctx := context.Background()
  118. policyMap, err := policyManager.GetPolicies(ctx)
  119. if err != nil {
  120. return nil, err
  121. }
  122. // Convert map[string]PolicyDocument to []IAMPolicy
  123. var policies []IAMPolicy
  124. for name, doc := range policyMap {
  125. policy := IAMPolicy{
  126. Name: name,
  127. Document: doc,
  128. DocumentJSON: "", // Will be populated if needed
  129. CreatedAt: time.Now(),
  130. UpdatedAt: time.Now(),
  131. }
  132. policies = append(policies, policy)
  133. }
  134. return policies, nil
  135. }
  136. // CreatePolicy creates a new IAM policy
  137. func (s *AdminServer) CreatePolicy(name string, document policy_engine.PolicyDocument) error {
  138. policyManager := s.GetPolicyManager()
  139. if policyManager == nil {
  140. return fmt.Errorf("policy manager not available")
  141. }
  142. ctx := context.Background()
  143. return policyManager.CreatePolicy(ctx, name, document)
  144. }
  145. // UpdatePolicy updates an existing IAM policy
  146. func (s *AdminServer) UpdatePolicy(name string, document policy_engine.PolicyDocument) error {
  147. policyManager := s.GetPolicyManager()
  148. if policyManager == nil {
  149. return fmt.Errorf("policy manager not available")
  150. }
  151. ctx := context.Background()
  152. return policyManager.UpdatePolicy(ctx, name, document)
  153. }
  154. // DeletePolicy deletes an IAM policy
  155. func (s *AdminServer) DeletePolicy(name string) error {
  156. policyManager := s.GetPolicyManager()
  157. if policyManager == nil {
  158. return fmt.Errorf("policy manager not available")
  159. }
  160. ctx := context.Background()
  161. return policyManager.DeletePolicy(ctx, name)
  162. }
  163. // GetPolicy retrieves a specific IAM policy
  164. func (s *AdminServer) GetPolicy(name string) (*IAMPolicy, error) {
  165. policyManager := s.GetPolicyManager()
  166. if policyManager == nil {
  167. return nil, fmt.Errorf("policy manager not available")
  168. }
  169. ctx := context.Background()
  170. policyDoc, err := policyManager.GetPolicy(ctx, name)
  171. if err != nil {
  172. return nil, err
  173. }
  174. if policyDoc == nil {
  175. return nil, nil
  176. }
  177. // Convert PolicyDocument to IAMPolicy
  178. policy := &IAMPolicy{
  179. Name: name,
  180. Document: *policyDoc,
  181. DocumentJSON: "", // Will be populated if needed
  182. CreatedAt: time.Now(),
  183. UpdatedAt: time.Now(),
  184. }
  185. return policy, nil
  186. }