range_perm_cache.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. var rangeEnd []byte
  36. if len(perm.RangeEnd) != 1 || perm.RangeEnd[0] != 0 {
  37. rangeEnd = perm.RangeEnd
  38. }
  39. if len(perm.RangeEnd) != 0 {
  40. ivl = adt.NewBytesAffineInterval(perm.Key, rangeEnd)
  41. } else {
  42. ivl = adt.NewBytesAffinePoint(perm.Key)
  43. }
  44. switch perm.PermType {
  45. case authpb.READWRITE:
  46. readPerms.Insert(ivl, struct{}{})
  47. writePerms.Insert(ivl, struct{}{})
  48. case authpb.READ:
  49. readPerms.Insert(ivl, struct{}{})
  50. case authpb.WRITE:
  51. writePerms.Insert(ivl, struct{}{})
  52. }
  53. }
  54. }
  55. return &unifiedRangePermissions{
  56. readPerms: readPerms,
  57. writePerms: writePerms,
  58. }
  59. }
  60. func checkKeyInterval(cachedPerms *unifiedRangePermissions, key, rangeEnd []byte, permtyp authpb.Permission_Type) bool {
  61. if len(rangeEnd) == 1 && rangeEnd[0] == 0 {
  62. rangeEnd = nil
  63. }
  64. ivl := adt.NewBytesAffineInterval(key, rangeEnd)
  65. switch permtyp {
  66. case authpb.READ:
  67. return cachedPerms.readPerms.Contains(ivl)
  68. case authpb.WRITE:
  69. return cachedPerms.writePerms.Contains(ivl)
  70. default:
  71. plog.Panicf("unknown auth type: %v", permtyp)
  72. }
  73. return false
  74. }
  75. func checkKeyPoint(cachedPerms *unifiedRangePermissions, key []byte, permtyp authpb.Permission_Type) bool {
  76. pt := adt.NewBytesAffinePoint(key)
  77. switch permtyp {
  78. case authpb.READ:
  79. return cachedPerms.readPerms.Intersects(pt)
  80. case authpb.WRITE:
  81. return cachedPerms.writePerms.Intersects(pt)
  82. default:
  83. plog.Panicf("unknown auth type: %v", permtyp)
  84. }
  85. return false
  86. }
  87. func (as *authStore) isRangeOpPermitted(tx backend.BatchTx, userName string, key, rangeEnd []byte, permtyp authpb.Permission_Type) bool {
  88. // assumption: tx is Lock()ed
  89. _, ok := as.rangePermCache[userName]
  90. if !ok {
  91. perms := getMergedPerms(tx, userName)
  92. if perms == nil {
  93. plog.Errorf("failed to create a unified permission of user %s", userName)
  94. return false
  95. }
  96. as.rangePermCache[userName] = perms
  97. }
  98. if len(rangeEnd) == 0 {
  99. return checkKeyPoint(as.rangePermCache[userName], key, permtyp)
  100. }
  101. return checkKeyInterval(as.rangePermCache[userName], key, rangeEnd, permtyp)
  102. }
  103. func (as *authStore) clearCachedPerm() {
  104. as.rangePermCache = make(map[string]*unifiedRangePermissions)
  105. }
  106. func (as *authStore) invalidateCachedPerm(userName string) {
  107. delete(as.rangePermCache, userName)
  108. }
  109. type unifiedRangePermissions struct {
  110. readPerms *adt.IntervalTree
  111. writePerms *adt.IntervalTree
  112. }