storage.access.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "path/filepath"
  7. "sync"
  8. "imuslab.com/arozos/mod/cluster/metadata"
  9. fs "imuslab.com/arozos/mod/filesystem"
  10. "imuslab.com/arozos/mod/storage/access"
  11. )
  12. /*
  13. Drive access lists
  14. Wires mod/storage/access into the system: where the rules of this host are
  15. kept, the replicated rule of cluster:/ and what has to follow a rule change.
  16. */
  17. const (
  18. driveAccessRuleFile = "storage.access.json" //Under system/, not system/storage/ where files are named after groups
  19. driveBridgeFile = "bridge.json" //What earlier builds kept their bridges in, see DriveAccessInit
  20. clusterDriveRuleKey = "storage.access.cluster"
  21. )
  22. var driveAccessStore *access.Store
  23. // driveRuleSource answers which rule a drive has. Every member of a cluster
  24. // serves the same files, so the rule of cluster:/ lives with the cluster
  25. // metadata; a local rule there would leave the files open through another node.
  26. type driveRuleSource struct{}
  27. func (driveRuleSource) Rule(uuid string) (access.Rule, bool) {
  28. if uuid == access.ClusterDriveID {
  29. return clusterDriveRule()
  30. }
  31. return driveAccessStore.Rule(uuid)
  32. }
  33. // DriveAccessInit opens the rules. It runs once every pool is loaded, since
  34. // converting the old bridges needs to know where each drive lives.
  35. func DriveAccessInit() {
  36. driveAccessStore = access.NewStore(filepath.Join("system", driveAccessRuleFile))
  37. if err := driveAccessStore.Err(); err != nil {
  38. systemWideLogger.PrintAndLog("Storage", "Unable to read the drive access rules, every drive is closed to non admins until "+driveAccessRuleFile+" is repaired", err)
  39. }
  40. userHandler.SetDriveRules(driveRuleSource{})
  41. permissionHandler.OnGroupRemoved = func(group string) {
  42. if err := driveAccessStore.RemoveGroup(group); err != nil {
  43. systemWideLogger.PrintAndLog("Storage", "Unable to remove group "+group+" from the drive access rules", err)
  44. }
  45. driveAccessChanged()
  46. }
  47. count, err := driveAccessStore.MigrateBridges(filepath.Join("system", driveBridgeFile), drivePoolHome)
  48. if err != nil {
  49. systemWideLogger.PrintAndLog("Storage", "Unable to convert the storage bridges into drive access rules", err)
  50. } else if count > 0 {
  51. systemWideLogger.PrintAndLog("Storage", "Converted the storage bridges into drive access rules", nil)
  52. }
  53. for _, uuid := range duplicateDriveIDs(collectPoolStates()) {
  54. systemWideLogger.PrintAndLog("Storage", "Drive id "+uuid+":/ is used by more than one storage pool. Its access rule applies to all of them; give each drive its own id", nil)
  55. }
  56. }
  57. // drivePoolHome returns the home group of a drive, empty for the system pool,
  58. // and whether any pool holds or configures it
  59. func drivePoolHome(uuid string) (string, bool) {
  60. for _, pool := range collectPoolStates() {
  61. if pool.holds(uuid) {
  62. if pool.owner == storagePoolSystem {
  63. return "", true
  64. }
  65. return pool.owner, true
  66. }
  67. }
  68. return "", false
  69. }
  70. // driveAccessChanged is called after anything that changes who may use a
  71. // drive. Requests read the rules directly; these are the parts that keep a
  72. // copy of the outcome.
  73. func driveAccessChanged() {
  74. userHandler.ResetQuotaManagers()
  75. if SambaShareManager != nil {
  76. go func() {
  77. if err := SambaShareManager.SyncDriveAccess(); err != nil {
  78. systemWideLogger.PrintAndLog("Storage", "Unable to update the Samba shares after a drive access change", err)
  79. }
  80. }()
  81. }
  82. }
  83. // setDriveRule saves the rule of a drive where that drive keeps it
  84. func setDriveRule(uuid string, rule access.Rule) error {
  85. if uuid == access.ClusterDriveID {
  86. return clusterSetDriveRule(rule)
  87. }
  88. return driveAccessStore.Set(uuid, rule)
  89. }
  90. // effectiveDriveRule returns the rule a drive has right now, saved or default
  91. func effectiveDriveRule(rules access.Source, uuid string, home string) access.Rule {
  92. if rule, ok := rules.Rule(uuid); ok {
  93. return rule
  94. }
  95. return access.DefaultRule(uuid, home)
  96. }
  97. /*
  98. The replicated rule of cluster:/
  99. */
  100. // The last parsed rule. Access is checked on every request, so the rule is
  101. // only parsed again when the replicated value differs.
  102. var (
  103. clusterRuleMu sync.Mutex
  104. clusterRuleRaw []byte
  105. clusterRuleValue access.Rule
  106. )
  107. // clusterDriveRule returns the rule of cluster:/ this node knows
  108. func clusterDriveRule() (access.Rule, bool) {
  109. if clusterMetadata == nil {
  110. return access.Rule{}, false
  111. }
  112. setting, ok := clusterMetadata.Setting(clusterDriveRuleKey)
  113. if !ok {
  114. return access.Rule{}, false
  115. }
  116. clusterRuleMu.Lock()
  117. defer clusterRuleMu.Unlock()
  118. if clusterRuleRaw == nil || !bytes.Equal(clusterRuleRaw, setting.Value) {
  119. clusterRuleValue = parseClusterDriveRule(setting.Value)
  120. clusterRuleRaw = append([]byte{}, setting.Value...)
  121. }
  122. return clusterRuleValue.Clone(), true
  123. }
  124. // parseClusterDriveRule reads a replicated rule. One that cannot be read, say
  125. // from a newer member, closes the drive rather than opening it.
  126. func parseClusterDriveRule(raw []byte) access.Rule {
  127. rule := access.Rule{}
  128. if err := json.Unmarshal(raw, &rule); err != nil || rule.Validate() != nil {
  129. systemWideLogger.PrintAndLog("Cluster", "The cluster:/ access rule cannot be read, closing the drive to non admins", err)
  130. return access.Rule{Other: access.None}
  131. }
  132. return rule
  133. }
  134. // clusterSetDriveRule replicates a new rule of cluster:/ to every member
  135. func clusterSetDriveRule(rule access.Rule) error {
  136. if clusterMetadata == nil || clusterManager == nil || !clusterManager.InCluster() {
  137. return errors.New("this node is not in a cluster")
  138. }
  139. js, err := json.Marshal(rule)
  140. if err != nil {
  141. return err
  142. }
  143. return clusterMetadata.Submit(metadata.KindSetting, &metadata.Setting{Key: clusterDriveRuleKey, Value: js})
  144. }
  145. // clusterSettingChanged follows a change of a replicated setting
  146. func clusterSettingChanged(payload []byte) {
  147. setting := metadata.Setting{}
  148. if err := json.Unmarshal(payload, &setting); err != nil || setting.Key != clusterDriveRuleKey {
  149. return
  150. }
  151. driveAccessChanged()
  152. }
  153. // clusterDatasetAllowed refuses a job dataset to a user who cannot read
  154. // cluster:/, because the dataset pattern is expanded from the metadata index
  155. // and would hand the job the names of files the user may not see.
  156. func clusterDatasetAllowed(username string, dataset string) error {
  157. if dataset == "" {
  158. return nil
  159. }
  160. userinfo, err := userHandler.GetUserInfoFromUsername(username)
  161. if err != nil {
  162. return err
  163. }
  164. if !userinfo.CanRead(access.ClusterDriveID + ":/") {
  165. return errors.New("access to cluster:/ denied")
  166. }
  167. return nil
  168. }
  169. // isClusterOrShareDrive reports whether a drive is built by code rather than
  170. // from a config row, so it cannot be rebuilt from its start options
  171. func isClusterOrShareDrive(fsh *fs.FileSystemHandler) bool {
  172. return fsh.UUID == access.ClusterDriveID || fsh.UUID == access.ShareDriveID
  173. }