range_perm_cache.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. "sort"
  17. "strings"
  18. "github.com/coreos/etcd/auth/authpb"
  19. "github.com/coreos/etcd/mvcc/backend"
  20. )
  21. func isSubset(a, b *rangePerm) bool {
  22. // return true if a is a subset of b
  23. return 0 <= strings.Compare(a.begin, b.begin) && strings.Compare(a.end, b.end) <= 0
  24. }
  25. func reduceSubsets(perms []*rangePerm) []*rangePerm {
  26. // TODO(mitake): currently it is O(n^2), we need a better algorithm
  27. ret := make([]*rangePerm, 0)
  28. for i := range perms {
  29. subset := false
  30. for j := range perms {
  31. if i != j && isSubset(perms[i], perms[j]) {
  32. subset = true
  33. break
  34. }
  35. }
  36. if subset {
  37. continue
  38. }
  39. ret = append(ret, perms[i])
  40. }
  41. return ret
  42. }
  43. func unifyPerms(perms []*rangePerm) []*rangePerm {
  44. ret := make([]*rangePerm, 0)
  45. perms = reduceSubsets(perms)
  46. sort.Sort(RangePermSliceByBegin(perms))
  47. i := 0
  48. for i < len(perms) {
  49. begin := i
  50. for i+1 < len(perms) && perms[i].end >= perms[i+1].begin {
  51. i++
  52. }
  53. if i == begin {
  54. ret = append(ret, &rangePerm{begin: perms[i].begin, end: perms[i].end})
  55. } else {
  56. ret = append(ret, &rangePerm{begin: perms[begin].begin, end: perms[i].end})
  57. }
  58. i++
  59. }
  60. return ret
  61. }
  62. func (as *authStore) makeUnifiedPerms(tx backend.BatchTx, userName string) *unifiedRangePermissions {
  63. user := getUser(tx, userName)
  64. if user == nil {
  65. plog.Errorf("invalid user name %s", userName)
  66. return nil
  67. }
  68. var readPerms, writePerms []*rangePerm
  69. for _, roleName := range user.Roles {
  70. _, vs := tx.UnsafeRange(authRolesBucketName, []byte(roleName), nil, 0)
  71. if len(vs) != 1 {
  72. plog.Errorf("invalid role name %s", roleName)
  73. return nil
  74. }
  75. role := &authpb.Role{}
  76. err := role.Unmarshal(vs[0])
  77. if err != nil {
  78. plog.Errorf("failed to unmarshal a role %s: %s", roleName, err)
  79. return nil
  80. }
  81. for _, perm := range role.KeyPermission {
  82. if len(perm.RangeEnd) == 0 {
  83. continue
  84. }
  85. if perm.PermType == authpb.READWRITE || perm.PermType == authpb.READ {
  86. readPerms = append(readPerms, &rangePerm{begin: string(perm.Key), end: string(perm.RangeEnd)})
  87. }
  88. if perm.PermType == authpb.READWRITE || perm.PermType == authpb.WRITE {
  89. writePerms = append(writePerms, &rangePerm{begin: string(perm.Key), end: string(perm.RangeEnd)})
  90. }
  91. }
  92. }
  93. return &unifiedRangePermissions{readPerms: unifyPerms(readPerms), writePerms: unifyPerms(writePerms)}
  94. }
  95. func checkCachedPerm(cachedPerms *unifiedRangePermissions, userName string, key, rangeEnd string, write, read bool) bool {
  96. var perms []*rangePerm
  97. if write {
  98. perms = cachedPerms.writePerms
  99. } else {
  100. perms = cachedPerms.readPerms
  101. }
  102. for _, perm := range perms {
  103. if strings.Compare(rangeEnd, "") != 0 {
  104. if strings.Compare(perm.begin, key) <= 0 && strings.Compare(rangeEnd, perm.end) <= 0 {
  105. return true
  106. }
  107. } else {
  108. if strings.Compare(perm.begin, key) <= 0 && strings.Compare(key, perm.end) <= 0 {
  109. return true
  110. }
  111. }
  112. }
  113. return false
  114. }
  115. func (as *authStore) isRangeOpPermitted(tx backend.BatchTx, userName string, key, rangeEnd string, write, read bool) bool {
  116. // assumption: tx is Lock()ed
  117. _, ok := as.rangePermCache[userName]
  118. if ok {
  119. return checkCachedPerm(as.rangePermCache[userName], userName, key, rangeEnd, write, read)
  120. }
  121. perms := as.makeUnifiedPerms(tx, userName)
  122. if perms == nil {
  123. plog.Errorf("failed to create a unified permission of user %s", userName)
  124. return false
  125. }
  126. as.rangePermCache[userName] = perms
  127. return checkCachedPerm(as.rangePermCache[userName], userName, key, rangeEnd, write, read)
  128. }
  129. func (as *authStore) clearCachedPerm() {
  130. as.rangePermCache = make(map[string]*unifiedRangePermissions)
  131. }
  132. func (as *authStore) invalidateCachedPerm(userName string) {
  133. delete(as.rangePermCache, userName)
  134. }
  135. type unifiedRangePermissions struct {
  136. // readPerms[i] and readPerms[j] (i != j) don't overlap
  137. readPerms []*rangePerm
  138. // writePerms[i] and writePerms[j] (i != j) don't overlap, too
  139. writePerms []*rangePerm
  140. }
  141. type rangePerm struct {
  142. begin, end string
  143. }
  144. type RangePermSliceByBegin []*rangePerm
  145. func (slice RangePermSliceByBegin) Len() int {
  146. return len(slice)
  147. }
  148. func (slice RangePermSliceByBegin) Less(i, j int) bool {
  149. if slice[i].begin == slice[j].begin {
  150. return slice[i].end < slice[j].end
  151. }
  152. return slice[i].begin < slice[j].begin
  153. }
  154. func (slice RangePermSliceByBegin) Swap(i, j int) {
  155. slice[i], slice[j] = slice[j], slice[i]
  156. }