range_perm_cache.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. ivl := adt.NewStringInterval(key, rangeEnd)
  58. switch permtyp {
  59. case authpb.READ:
  60. return cachedPerms.readPerms.Contains(ivl)
  61. case authpb.WRITE:
  62. return cachedPerms.writePerms.Contains(ivl)
  63. default:
  64. plog.Panicf("unknown auth type: %v", permtyp)
  65. }
  66. return false
  67. }
  68. func checkKeyPoint(cachedPerms *unifiedRangePermissions, key string, permtyp authpb.Permission_Type) bool {
  69. pt := adt.NewStringPoint(key)
  70. switch permtyp {
  71. case authpb.READ:
  72. return cachedPerms.readPerms.Intersects(pt)
  73. case authpb.WRITE:
  74. return cachedPerms.writePerms.Intersects(pt)
  75. default:
  76. plog.Panicf("unknown auth type: %v", permtyp)
  77. }
  78. return false
  79. }
  80. func (as *authStore) isRangeOpPermitted(tx backend.BatchTx, userName string, key, rangeEnd string, permtyp authpb.Permission_Type) bool {
  81. // assumption: tx is Lock()ed
  82. _, ok := as.rangePermCache[userName]
  83. if !ok {
  84. perms := getMergedPerms(tx, userName)
  85. if perms == nil {
  86. plog.Errorf("failed to create a unified permission of user %s", userName)
  87. return false
  88. }
  89. as.rangePermCache[userName] = perms
  90. }
  91. if len(rangeEnd) == 0 {
  92. return checkKeyPoint(as.rangePermCache[userName], key, permtyp)
  93. }
  94. return checkKeyInterval(as.rangePermCache[userName], key, rangeEnd, permtyp)
  95. }
  96. func (as *authStore) clearCachedPerm() {
  97. as.rangePermCache = make(map[string]*unifiedRangePermissions)
  98. }
  99. func (as *authStore) invalidateCachedPerm(userName string) {
  100. delete(as.rangePermCache, userName)
  101. }
  102. type unifiedRangePermissions struct {
  103. readPerms *adt.IntervalTree
  104. writePerms *adt.IntervalTree
  105. }