access.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package access
  2. /*
  3. Drive access lists
  4. Who may use a drive is decided by one list per drive: a row per permission
  5. group, plus a default access level for the users no row matches. The pool
  6. a drive is configured in stays its home, and the members of that group
  7. always have full access to it.
  8. This replaces both the per pool "other permission" (now the default access
  9. level, per drive) and bridging a drive into another pool (now a group row).
  10. The rules are kept by Store, except for cluster:/ whose rule is replicated
  11. with the cluster metadata, so that every member serves the same answer.
  12. */
  13. import (
  14. "errors"
  15. "sort"
  16. )
  17. // The levels a drive can be used at, from low to high. These are the values of
  18. // arozfs.FsDenied, FsReadOnly and FsReadWrite, which are variables and so
  19. // cannot define a constant.
  20. const (
  21. None = "denied"
  22. Read = "readonly"
  23. ReadWrite = "readwrite"
  24. )
  25. // IDs of the drives the system itself provides
  26. const (
  27. UserDriveID = "user"
  28. TmpDriveID = "tmp"
  29. ShareDriveID = "share"
  30. ClusterDriveID = "cluster"
  31. )
  32. // Why a user got the level they got, see Explain
  33. const (
  34. ReasonFixed = "fixed" //user:/ and tmp:/ are open to everyone
  35. ReasonHome = "home" //The user is in the drive's home group
  36. ReasonAdmin = "admin" //Admins keep access to every drive of the system pool
  37. ReasonGroup = "group" //A row of one of the user's groups
  38. ReasonOther = "other" //No row matched, the default access applied
  39. )
  40. // Rule is the access list of one drive
  41. type Rule struct {
  42. Other string `json:"other"` //Level of the users no group row matches
  43. Groups map[string]string `json:"groups,omitempty"` //Level per permission group
  44. }
  45. // Source tells which rule a drive has. ok is false for a drive nobody changed,
  46. // which then gets DefaultRule.
  47. type Source interface {
  48. Rule(uuid string) (rule Rule, ok bool)
  49. }
  50. // Drive is the drive a level is worked out for
  51. type Drive struct {
  52. UUID string
  53. HomeGroup string //Permission group whose pool holds the drive, empty for the system pool
  54. ReadOnly bool //The whole drive is read only, whatever the rule says
  55. }
  56. // Subject is the user a level is worked out for
  57. type Subject struct {
  58. Groups []string //Names of the permission groups the user is in
  59. IsAdmin bool //One of those groups is an admin group
  60. }
  61. // Decision is the result of Explain
  62. type Decision struct {
  63. Level string `json:"level"` //The level the user ends up with
  64. Reason string `json:"reason"` //One of the Reason constants
  65. Group string `json:"group,omitempty"` //The group behind ReasonHome and ReasonGroup
  66. Capped bool `json:"capped"` //The drive is read only, so the level is lower than the rule gave
  67. }
  68. // IsFixed reports whether a drive is open to every user, with no rule
  69. func IsFixed(uuid string) bool {
  70. return uuid == UserDriveID || uuid == TmpDriveID
  71. }
  72. // IsBuiltin reports whether a drive is provided by the system rather than
  73. // configured by an admin. Built-in drives have no config row and cannot be
  74. // edited, stopped or removed.
  75. func IsBuiltin(uuid string) bool {
  76. return IsFixed(uuid) || uuid == ShareDriveID || uuid == ClusterDriveID
  77. }
  78. // DefaultRule is the rule of a drive nobody changed: what the system pool
  79. // and the group pools gave their users before access lists existed.
  80. func DefaultRule(uuid string, homeGroup string) Rule {
  81. switch {
  82. case uuid == ShareDriveID:
  83. return Rule{Other: Read}
  84. case homeGroup == "":
  85. return Rule{Other: ReadWrite}
  86. default:
  87. return Rule{Other: None}
  88. }
  89. }
  90. // IsLevel reports whether a value is a level a rule may hold
  91. func IsLevel(level string) bool {
  92. return level == None || level == Read || level == ReadWrite
  93. }
  94. // rank orders the levels. Anything that is not a level ranks as None, so a
  95. // damaged rule can only take access away.
  96. func rank(level string) int {
  97. switch level {
  98. case ReadWrite:
  99. return 2
  100. case Read:
  101. return 1
  102. default:
  103. return 0
  104. }
  105. }
  106. // Higher returns the higher of two levels
  107. func Higher(a string, b string) string {
  108. if rank(b) > rank(a) {
  109. return b
  110. }
  111. return a
  112. }
  113. // Validate reports whether a rule can be saved
  114. func (r Rule) Validate() error {
  115. if !IsLevel(r.Other) {
  116. return errors.New("invalid default access level: " + r.Other)
  117. }
  118. for group, level := range r.Groups {
  119. if group == "" {
  120. return errors.New("a row has no group")
  121. }
  122. if !IsLevel(level) {
  123. return errors.New("invalid level for group " + group + ": " + level)
  124. }
  125. }
  126. return nil
  127. }
  128. // Clone returns a copy that shares nothing with r
  129. func (r Rule) Clone() Rule {
  130. clone := Rule{Other: r.Other}
  131. if len(r.Groups) > 0 {
  132. clone.Groups = make(map[string]string, len(r.Groups))
  133. for group, level := range r.Groups {
  134. clone.Groups[group] = level
  135. }
  136. }
  137. return clone
  138. }
  139. // Explain works out a user's level on a drive and why. The first of these
  140. // that applies wins:
  141. //
  142. // 1. user:/ and tmp:/ are open to everyone
  143. // 2. members of the drive's home group have full access
  144. // 3. admins have full access to the drives of the system pool
  145. // 4. the highest row of the user's groups, which may be None
  146. // 5. the default access level
  147. //
  148. // The result is then capped at Read on a read only drive.
  149. func Explain(drive Drive, subject Subject, rule Rule) Decision {
  150. decision := decide(drive, subject, rule)
  151. decision.Level = normalize(decision.Level)
  152. if drive.ReadOnly && rank(decision.Level) > rank(Read) {
  153. decision.Level = Read
  154. decision.Capped = true
  155. }
  156. return decision
  157. }
  158. func decide(drive Drive, subject Subject, rule Rule) Decision {
  159. if IsFixed(drive.UUID) {
  160. return Decision{Level: ReadWrite, Reason: ReasonFixed}
  161. }
  162. if drive.HomeGroup != "" {
  163. for _, group := range subject.Groups {
  164. if group == drive.HomeGroup {
  165. return Decision{Level: ReadWrite, Reason: ReasonHome, Group: group}
  166. }
  167. }
  168. } else if subject.IsAdmin {
  169. return Decision{Level: ReadWrite, Reason: ReasonAdmin}
  170. }
  171. //Groups are walked in a fixed order so a tie names the same group each time
  172. groups := append([]string{}, subject.Groups...)
  173. sort.Strings(groups)
  174. matched := false
  175. best := Decision{Level: None, Reason: ReasonGroup}
  176. for _, group := range groups {
  177. level, ok := rule.Groups[group]
  178. if !ok {
  179. continue
  180. }
  181. if !matched || rank(level) > rank(best.Level) {
  182. best.Level = level
  183. best.Group = group
  184. }
  185. matched = true
  186. }
  187. if matched {
  188. return best
  189. }
  190. return Decision{Level: rule.Other, Reason: ReasonOther}
  191. }
  192. // normalize turns anything that is not a level into None
  193. func normalize(level string) string {
  194. if IsLevel(level) {
  195. return level
  196. }
  197. return None
  198. }
  199. // Level returns a user's level on a drive, see Explain
  200. func Level(drive Drive, subject Subject, rule Rule) string {
  201. return Explain(drive, subject, rule).Level
  202. }