range_perm_cache.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. "github.com/coreos/etcd/auth/authpb"
  17. "github.com/coreos/etcd/mvcc/backend"
  18. "github.com/coreos/etcd/pkg/adt"
  19. )
  20. func getMergedPerms(tx backend.BatchTx, userName string) *unifiedRangePermissions {
  21. user := getUser(tx, userName)
  22. if user == nil {
  23. plog.Errorf("invalid user name %s", userName)
  24. return nil
  25. }
  26. readPerms := &adt.IntervalTree{}
  27. writePerms := &adt.IntervalTree{}
  28. for _, roleName := range user.Roles {
  29. role := getRole(tx, roleName)
  30. if role == nil {
  31. continue
  32. }
  33. for _, perm := range role.KeyPermission {
  34. var ivl adt.Interval
  35. if len(perm.RangeEnd) != 0 {
  36. ivl = adt.NewStringInterval(string(perm.Key), string(perm.RangeEnd))
  37. } else {
  38. ivl = adt.NewStringPoint(string(perm.Key))
  39. }
  40. switch perm.PermType {
  41. case authpb.READWRITE:
  42. readPerms.Insert(ivl, struct{}{})
  43. writePerms.Insert(ivl, struct{}{})
  44. case authpb.READ:
  45. readPerms.Insert(ivl, struct{}{})
  46. case authpb.WRITE:
  47. writePerms.Insert(ivl, struct{}{})
  48. }
  49. }
  50. }
  51. return &unifiedRangePermissions{
  52. readPerms: readPerms,
  53. writePerms: writePerms,
  54. }
  55. }
  56. func checkKeyInterval(cachedPerms *unifiedRangePermissions, key, rangeEnd string, permtyp authpb.Permission_Type) bool {
  57. var tocheck *adt.IntervalTree
  58. switch permtyp {
  59. case authpb.READ:
  60. tocheck = cachedPerms.readPerms
  61. case authpb.WRITE:
  62. tocheck = cachedPerms.writePerms
  63. default:
  64. plog.Panicf("unknown auth type: %v", permtyp)
  65. }
  66. ivl := adt.NewStringInterval(key, rangeEnd)
  67. isContiguous := true
  68. var maxEnd, minBegin adt.Comparable
  69. tocheck.Visit(ivl, func(n *adt.IntervalValue) bool {
  70. if minBegin == nil {
  71. minBegin = n.Ivl.Begin
  72. maxEnd = n.Ivl.End
  73. return true
  74. }
  75. if maxEnd.Compare(n.Ivl.Begin) < 0 {
  76. isContiguous = false
  77. return false
  78. }
  79. if n.Ivl.End.Compare(maxEnd) > 0 {
  80. maxEnd = n.Ivl.End
  81. }
  82. return true
  83. })
  84. return isContiguous && maxEnd.Compare(ivl.End) >= 0 && minBegin.Compare(ivl.Begin) <= 0
  85. }
  86. func checkKeyPoint(cachedPerms *unifiedRangePermissions, key string, permtyp authpb.Permission_Type) bool {
  87. var tocheck *adt.IntervalTree
  88. switch permtyp {
  89. case authpb.READ:
  90. tocheck = cachedPerms.readPerms
  91. case authpb.WRITE:
  92. tocheck = cachedPerms.writePerms
  93. default:
  94. plog.Panicf("unknown auth type: %v", permtyp)
  95. }
  96. pt := adt.NewStringPoint(key)
  97. return tocheck.Contains(pt)
  98. }
  99. func (as *authStore) isRangeOpPermitted(tx backend.BatchTx, userName string, key, rangeEnd string, permtyp authpb.Permission_Type) bool {
  100. // assumption: tx is Lock()ed
  101. _, ok := as.rangePermCache[userName]
  102. if !ok {
  103. perms := getMergedPerms(tx, userName)
  104. if perms == nil {
  105. plog.Errorf("failed to create a unified permission of user %s", userName)
  106. return false
  107. }
  108. as.rangePermCache[userName] = perms
  109. }
  110. if len(rangeEnd) == 0 {
  111. return checkKeyPoint(as.rangePermCache[userName], key, permtyp)
  112. }
  113. return checkKeyInterval(as.rangePermCache[userName], key, rangeEnd, permtyp)
  114. }
  115. func (as *authStore) clearCachedPerm() {
  116. as.rangePermCache = make(map[string]*unifiedRangePermissions)
  117. }
  118. func (as *authStore) invalidateCachedPerm(userName string) {
  119. delete(as.rangePermCache, userName)
  120. }
  121. type unifiedRangePermissions struct {
  122. readPerms *adt.IntervalTree
  123. writePerms *adt.IntervalTree
  124. }