range_perm_cache.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package auth
  15. import (
  16. "bytes"
  17. "sort"
  18. "github.com/coreos/etcd/auth/authpb"
  19. "github.com/coreos/etcd/mvcc/backend"
  20. )
  21. // isSubset returns true if a is a subset of b.
  22. // If a is a prefix of b, then a is a subset of b.
  23. // Given intervals [a1,a2) and [b1,b2), is
  24. // the a interval a subset of b?
  25. func isSubset(a, b *rangePerm) bool {
  26. switch {
  27. case len(a.end) == 0 && len(b.end) == 0:
  28. // a, b are both keys
  29. return bytes.Equal(a.begin, b.begin)
  30. case len(b.end) == 0:
  31. // b is a key, a is a range
  32. return false
  33. case len(a.end) == 0:
  34. // a is a key, b is a range. need b1 <= a1 and a1 < b2
  35. return bytes.Compare(b.begin, a.begin) <= 0 && bytes.Compare(a.begin, b.end) < 0
  36. default:
  37. // both are ranges. need b1 <= a1 and a2 <= b2
  38. return bytes.Compare(b.begin, a.begin) <= 0 && bytes.Compare(a.end, b.end) <= 0
  39. }
  40. }
  41. func isRangeEqual(a, b *rangePerm) bool {
  42. return bytes.Equal(a.begin, b.begin) && bytes.Equal(a.end, b.end)
  43. }
  44. // removeSubsetRangePerms removes any rangePerms that are subsets of other rangePerms.
  45. // If there are equal ranges, removeSubsetRangePerms only keeps one of them.
  46. func removeSubsetRangePerms(perms []*rangePerm) []*rangePerm {
  47. // TODO(mitake): currently it is O(n^2), we need a better algorithm
  48. newp := make([]*rangePerm, 0)
  49. for i := range perms {
  50. skip := false
  51. for j := range perms {
  52. if i == j {
  53. continue
  54. }
  55. if isRangeEqual(perms[i], perms[j]) {
  56. // if ranges are equal, we only keep the first range.
  57. if i > j {
  58. skip = true
  59. break
  60. }
  61. } else if isSubset(perms[i], perms[j]) {
  62. // if a range is a strict subset of the other one, we skip the subset.
  63. skip = true
  64. break
  65. }
  66. }
  67. if skip {
  68. continue
  69. }
  70. newp = append(newp, perms[i])
  71. }
  72. return newp
  73. }
  74. // mergeRangePerms merges adjacent rangePerms.
  75. func mergeRangePerms(perms []*rangePerm) []*rangePerm {
  76. merged := make([]*rangePerm, 0)
  77. perms = removeSubsetRangePerms(perms)
  78. sort.Sort(RangePermSliceByBegin(perms))
  79. i := 0
  80. for i < len(perms) {
  81. begin, next := i, i
  82. for next+1 < len(perms) && bytes.Compare(perms[next].end, perms[next+1].begin) >= 0 {
  83. next++
  84. }
  85. // don't merge ["a", "b") with ["b", ""), because perms[next+1].end is empty.
  86. if next != begin && len(perms[next].end) > 0 {
  87. merged = append(merged, &rangePerm{begin: perms[begin].begin, end: perms[next].end})
  88. } else {
  89. merged = append(merged, perms[begin])
  90. if next != begin {
  91. merged = append(merged, perms[next])
  92. }
  93. }
  94. i = next + 1
  95. }
  96. return merged
  97. }
  98. func getMergedPerms(tx backend.BatchTx, userName string) *unifiedRangePermissions {
  99. user := getUser(tx, userName)
  100. if user == nil {
  101. plog.Errorf("invalid user name %s", userName)
  102. return nil
  103. }
  104. var readPerms, writePerms []*rangePerm
  105. for _, roleName := range user.Roles {
  106. role := getRole(tx, roleName)
  107. if role == nil {
  108. continue
  109. }
  110. for _, perm := range role.KeyPermission {
  111. rp := &rangePerm{begin: perm.Key, end: perm.RangeEnd}
  112. switch perm.PermType {
  113. case authpb.READWRITE:
  114. readPerms = append(readPerms, rp)
  115. writePerms = append(writePerms, rp)
  116. case authpb.READ:
  117. readPerms = append(readPerms, rp)
  118. case authpb.WRITE:
  119. writePerms = append(writePerms, rp)
  120. }
  121. }
  122. }
  123. return &unifiedRangePermissions{
  124. readPerms: mergeRangePerms(readPerms),
  125. writePerms: mergeRangePerms(writePerms),
  126. }
  127. }
  128. func checkKeyPerm(cachedPerms *unifiedRangePermissions, key, rangeEnd []byte, permtyp authpb.Permission_Type) bool {
  129. var tocheck []*rangePerm
  130. switch permtyp {
  131. case authpb.READ:
  132. tocheck = cachedPerms.readPerms
  133. case authpb.WRITE:
  134. tocheck = cachedPerms.writePerms
  135. default:
  136. plog.Panicf("unknown auth type: %v", permtyp)
  137. }
  138. requiredPerm := &rangePerm{begin: key, end: rangeEnd}
  139. for _, perm := range tocheck {
  140. if isSubset(requiredPerm, perm) {
  141. return true
  142. }
  143. }
  144. return false
  145. }
  146. func (as *authStore) isRangeOpPermitted(tx backend.BatchTx, userName string, key, rangeEnd []byte, permtyp authpb.Permission_Type) bool {
  147. // assumption: tx is Lock()ed
  148. _, ok := as.rangePermCache[userName]
  149. if !ok {
  150. perms := getMergedPerms(tx, userName)
  151. if perms == nil {
  152. plog.Errorf("failed to create a unified permission of user %s", userName)
  153. return false
  154. }
  155. as.rangePermCache[userName] = perms
  156. }
  157. return checkKeyPerm(as.rangePermCache[userName], key, rangeEnd, permtyp)
  158. }
  159. func (as *authStore) clearCachedPerm() {
  160. as.rangePermCache = make(map[string]*unifiedRangePermissions)
  161. }
  162. func (as *authStore) invalidateCachedPerm(userName string) {
  163. delete(as.rangePermCache, userName)
  164. }
  165. type unifiedRangePermissions struct {
  166. // readPerms[i] and readPerms[j] (i != j) don't overlap
  167. readPerms []*rangePerm
  168. // writePerms[i] and writePerms[j] (i != j) don't overlap, too
  169. writePerms []*rangePerm
  170. }
  171. type rangePerm struct {
  172. begin, end []byte
  173. }
  174. type RangePermSliceByBegin []*rangePerm
  175. func (slice RangePermSliceByBegin) Len() int {
  176. return len(slice)
  177. }
  178. func (slice RangePermSliceByBegin) Less(i, j int) bool {
  179. switch bytes.Compare(slice[i].begin, slice[j].begin) {
  180. case 0: // begin(i) == begin(j)
  181. return bytes.Compare(slice[i].end, slice[j].end) == -1
  182. case -1: // begin(i) < begin(j)
  183. return true
  184. default:
  185. return false
  186. }
  187. }
  188. func (slice RangePermSliceByBegin) Swap(i, j int) {
  189. slice[i], slice[j] = slice[j], slice[i]
  190. }